Skip to content

Commit dd61547

Browse files
authored
fix: catch CompletionException instead when instruction file is not found (#379)
1 parent 54fa0cb commit dd61547

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/main/java/software/amazon/encryption/s3/internal/ContentMetadataDecodingStrategy.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import software.amazon.awssdk.services.s3.S3AsyncClient;
1111
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
1212
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
13-
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
1413
import software.amazon.encryption.s3.S3EncryptionClientException;
1514
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
1615
import software.amazon.encryption.s3.materials.EncryptedDataKey;
@@ -24,6 +23,7 @@
2423
import java.util.Base64;
2524
import java.util.HashMap;
2625
import java.util.Map;
26+
import java.util.concurrent.CompletionException;
2727

2828
import static software.amazon.encryption.s3.S3EncryptionClientUtilities.INSTRUCTION_FILE_SUFFIX;
2929

@@ -233,7 +233,8 @@ private ContentMetadata decodeFromInstructionFile(GetObjectRequest request, GetO
233233
ResponseInputStream<GetObjectResponse> instruction;
234234
try {
235235
instruction = wrappedAsyncClient_.getObject(instructionGetObjectRequest, AsyncResponseTransformer.toBlockingInputStream()).join();
236-
} catch (NoSuchKeyException exception) {
236+
}
237+
catch (CompletionException exception) {
237238
// Most likely, the customer is attempting to decrypt an object
238239
// which is not encrypted with the S3 EC.
239240
throw new S3EncryptionClientException("Instruction file not found! Please ensure the object you are" +

src/test/java/software/amazon/encryption/s3/S3EncryptionClientTest.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,15 @@ public void attemptToDecryptPlaintext() {
617617
.key(objectKey)
618618
.build(), RequestBody.fromString(input));
619619

620-
// Attempt to get (and decrypt) the (plaintext) object from S3
621-
assertThrows(S3EncryptionClientException.class, () -> v3Client.getObject(builder -> builder
622-
.bucket(BUCKET)
623-
.key(objectKey)));
620+
try {
621+
v3Client.getObject(builder -> builder
622+
.bucket(BUCKET)
623+
.key(objectKey));
624+
fail("expected exception");
625+
} catch (S3EncryptionClientException ex) {
626+
assertTrue(ex.getMessage().contains("Instruction file not found!"));
627+
assertEquals(ex.getCause().getClass(), S3EncryptionClientException.class);
628+
}
624629

625630
// Cleanup
626631
deleteObject(BUCKET, objectKey, v3Client);

0 commit comments

Comments
 (0)