Skip to content

Commit 5a1f292

Browse files
author
Anirav Kareddy
committed
added support for custom instruction file suffix in getObject
1 parent d16db4d commit 5a1f292

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/main/java/software/amazon/encryption/s3/S3EncryptionClient.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
77
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
88
import software.amazon.awssdk.awscore.exception.AwsServiceException;
9-
import software.amazon.awssdk.core.ResponseBytes;
109
import software.amazon.awssdk.core.ResponseInputStream;
1110
import software.amazon.awssdk.core.async.AsyncRequestBody;
1211
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
@@ -68,13 +67,11 @@
6867
import software.amazon.encryption.s3.materials.EncryptionMaterials;
6968
import software.amazon.encryption.s3.materials.Keyring;
7069
import software.amazon.encryption.s3.materials.KmsKeyring;
71-
import software.amazon.encryption.s3.materials.MaterialsDescription;
7270
import software.amazon.encryption.s3.materials.MultipartConfiguration;
7371
import software.amazon.encryption.s3.materials.PartialRsaKeyPair;
7472
import software.amazon.encryption.s3.materials.RawKeyring;
7573
import software.amazon.encryption.s3.materials.RsaKeyring;
7674

77-
import javax.crypto.DecapsulateException;
7875
import javax.crypto.SecretKey;
7976
import java.io.IOException;
8077
import java.net.URI;
@@ -85,6 +82,7 @@
8582
import java.util.Collections;
8683
import java.util.List;
8784
import java.util.Map;
85+
import java.util.Objects;
8886
import java.util.Optional;
8987
import java.util.concurrent.CompletableFuture;
9088
import java.util.concurrent.CompletionException;
@@ -110,6 +108,7 @@ public class S3EncryptionClient extends DelegatingS3Client {
110108
// Used for request-scoped encryption contexts for supporting keys
111109
public static final ExecutionAttribute<Map<String, String>> ENCRYPTION_CONTEXT = new ExecutionAttribute<>("EncryptionContext");
112110
public static final ExecutionAttribute<MultipartConfiguration> CONFIGURATION = new ExecutionAttribute<>("MultipartConfiguration");
111+
public static final ExecutionAttribute<String> CUSTOM_INSTRUCTION_FILE_SUFFIX = new ExecutionAttribute<>("CustomInstructionFileSuffix");
113112

114113
private final S3Client _wrappedClient;
115114
private final S3AsyncClient _wrappedAsyncClient;
@@ -157,6 +156,11 @@ public static Consumer<AwsRequestOverrideConfiguration.Builder> withAdditionalCo
157156
builder.putExecutionAttribute(S3EncryptionClient.ENCRYPTION_CONTEXT, encryptionContext);
158157
}
159158

159+
public static Consumer<AwsRequestOverrideConfiguration.Builder> withCustomInstructionFileSuffix(String customInstructionFileSuffix) {
160+
return builder ->
161+
builder.putExecutionAttribute(S3EncryptionClient.CUSTOM_INSTRUCTION_FILE_SUFFIX, customInstructionFileSuffix);
162+
}
163+
160164
/**
161165
* Attaches multipart configuration to a request. Must be used as a parameter to
162166
* {@link S3Request#overrideConfiguration()} in the request.
@@ -204,13 +208,15 @@ public ReEncryptInstructionFileResponse reEncryptInstructionFile(ReEncryptInstru
204208
DecryptMaterialsRequest.builder()
205209
.algorithmSuite(algorithmSuite)
206210
.encryptedDataKeys(Collections.singletonList(encryptedDataKey))
211+
.s3Request(request)
207212
.build()
208213
);
209214
byte[] plaintextDataKey = decryptedMaterials.plaintextDataKey();
210215

211216
EncryptionMaterials encryptionMaterials = EncryptionMaterials.builder()
212217
.algorithmSuite(algorithmSuite)
213218
.plaintextDataKey(plaintextDataKey)
219+
.s3Request(request)
214220
.build();
215221

216222
RawKeyring newKeyring = reEncryptInstructionFileRequest.newKeyring();
@@ -221,11 +227,18 @@ public ReEncryptInstructionFileResponse reEncryptInstructionFile(ReEncryptInstru
221227
}
222228

223229
ContentMetadataEncodingStrategy encodeStrategy = new ContentMetadataEncodingStrategy(_instructionFileConfig);
224-
encodeStrategy.encodeMetadata(encryptedMaterials, iv, PutObjectRequest.builder()
225-
.bucket(reEncryptInstructionFileRequest.bucket())
226-
.key(reEncryptInstructionFileRequest.key())
227-
.build());
228230

231+
if (reEncryptInstructionFileRequest.instructionFileSuffix().equals(INSTRUCTION_FILE_SUFFIX)) {
232+
encodeStrategy.encodeMetadata(encryptedMaterials, iv, PutObjectRequest.builder()
233+
.bucket(reEncryptInstructionFileRequest.bucket())
234+
.key(reEncryptInstructionFileRequest.key())
235+
.build());
236+
} else {
237+
encodeStrategy.encodeMetadata(encryptedMaterials, iv, PutObjectRequest.builder()
238+
.bucket(reEncryptInstructionFileRequest.bucket())
239+
.key(reEncryptInstructionFileRequest.key())
240+
.build(), reEncryptInstructionFileRequest.instructionFileSuffix());
241+
}
229242
return new ReEncryptInstructionFileResponse(reEncryptInstructionFileRequest.bucket(),
230243
reEncryptInstructionFileRequest.key(), reEncryptInstructionFileRequest.instructionFileSuffix());
231244

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
1010
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
1111
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
12+
import software.amazon.encryption.s3.S3EncryptionClient;
1213
import software.amazon.encryption.s3.S3EncryptionClientException;
1314
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
1415
import software.amazon.encryption.s3.materials.EncryptedDataKey;
@@ -22,6 +23,7 @@
2223
import java.util.Base64;
2324
import java.util.HashMap;
2425
import java.util.Map;
26+
import java.util.Optional;
2527
import java.util.concurrent.CompletionException;
2628

2729
import static software.amazon.encryption.s3.S3EncryptionClientUtilities.INSTRUCTION_FILE_SUFFIX;
@@ -224,9 +226,13 @@ private ContentMetadata decodeFromObjectMetadata(GetObjectRequest request, GetOb
224226
}
225227

226228
private ContentMetadata decodeFromInstructionFile(GetObjectRequest request, GetObjectResponse response) {
229+
String instructionFileSuffix = request.overrideConfiguration()
230+
.flatMap(config -> config.executionAttributes().getOptionalAttribute(S3EncryptionClient.CUSTOM_INSTRUCTION_FILE_SUFFIX))
231+
.orElse(INSTRUCTION_FILE_SUFFIX);
232+
227233
GetObjectRequest instructionGetObjectRequest = GetObjectRequest.builder()
228234
.bucket(request.bucket())
229-
.key(request.key() + INSTRUCTION_FILE_SUFFIX)
235+
.key(request.key() + instructionFileSuffix)
230236
.build();
231237

232238
ResponseInputStream<GetObjectResponse> instruction;

0 commit comments

Comments
 (0)