Skip to content

Commit 4f51c42

Browse files
author
Anirav Kareddy
committed
fixed all changes requested from PR
1 parent b9ae158 commit 4f51c42

File tree

9 files changed

+80
-65
lines changed

9 files changed

+80
-65
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package software.amazon.encryption.s3.examples;
2+
3+
import software.amazon.awssdk.core.sync.RequestBody;
4+
import software.amazon.awssdk.services.s3.S3Client;
5+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
6+
import software.amazon.encryption.s3.S3EncryptionClient;
7+
import software.amazon.awssdk.regions.Region;
8+
9+
public class HelloWorldProgramExample {
10+
public static void main(String[] args) {
11+
//Create AWS KMS key (go to KMS to do this):
12+
String kmsKeyId = "arn:aws:kms:us-east-2:597133212884:key/1483518b-144f-48d7-84ce-735ff8d6da98";
13+
//Think of object as the "plaintext message"
14+
String object = "Hello World";
15+
//Created new bucket for this program...had to update permissions to fix bugs
16+
String bucket = "testing-bucket-hello-world";
17+
//Object Key: Identifier of object in S3
18+
String object_key = "hello-world.txt";
19+
20+
try (S3Client v3Client = S3EncryptionClient.builder()
21+
.kmsKeyId(kmsKeyId)
22+
.enableLegacyUnauthenticatedModes(true)
23+
.region(Region.US_EAST_2)
24+
.build()) {
25+
26+
v3Client.putObject(PutObjectRequest.builder()
27+
.bucket(bucket)
28+
.key(object_key)
29+
.build(), RequestBody.fromString(object));
30+
31+
String output = v3Client.getObjectAsBytes(builder -> builder
32+
.bucket(bucket)
33+
.key(object_key)
34+
).asUtf8String();
35+
36+
System.out.println("Object stored in S3 is: "+output);
37+
38+
} catch (Exception e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
}
43+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
13
package software.amazon.encryption.s3.internal;
24

35
import software.amazon.awssdk.protocols.jsoncore.JsonWriter;

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
public class ConvertSDKRequests {
1414

1515
public static PutObjectRequest convertRequest(CreateMultipartUploadRequest request) {
16-
16+
/*Converts a CreateMultipartUploadRequest into a PutObjectRequest by setting optional fields needed for
17+
putInstructionFile operation.
18+
*/
1719
final PutObjectRequest.Builder output = PutObjectRequest.builder();
1820
request
1921
.toBuilder()
@@ -119,13 +121,13 @@ public static PutObjectRequest convertRequest(CreateMultipartUploadRequest reque
119121
// Rather than silently dropping the value,
120122
// we loudly signal that we don't know how to handle this field.
121123
throw new IllegalArgumentException(
122-
f.locationName() + " is an unknown field. " +
124+
f.memberName() + " is an unknown field. " +
123125
"The S3 Encryption Client does not recognize this option and cannot set it on the PutObjectRequest." +
124126
"This may be a new S3 feature." +
125127
"Please report this to the Amazon S3 Encryption Client for Java: " +
126128
"https://github.com/aws/amazon-s3-encryption-client-java/issues." +
127-
"To work around this issue you can disable multi part upload," +
128-
"use the Async client, or not set this value on PutObject." +
129+
"To work around this issue, you can disable Instruction File on PutObject or disable" +
130+
"multi part upload, or use the Async client, or not set this value on PutObject." +
129131
"You may be able to update this value after the PutObject request completes."
130132
);
131133
}
@@ -138,7 +140,9 @@ public static PutObjectRequest convertRequest(CreateMultipartUploadRequest reque
138140
}
139141

140142
public static CreateMultipartUploadRequest convertRequest(PutObjectRequest request) {
141-
143+
/*Converts a PutObjectRequest into a CreateMultipartUploadRequest by setting optional fields needed for high-level
144+
multipart upload operation.
145+
*/
142146
final CreateMultipartUploadRequest.Builder output = CreateMultipartUploadRequest.builder();
143147
request
144148
.toBuilder()
@@ -251,7 +255,7 @@ public static CreateMultipartUploadRequest convertRequest(PutObjectRequest reque
251255
// Rather than silently dropping the value,
252256
// we loudly signal that we don't know how to handle this field.
253257
throw new IllegalArgumentException(
254-
f.locationName() + " is an unknown field. " +
258+
f.memberName() + " is an unknown field. " +
255259
"The S3 Encryption Client does not recognize this option and cannot set it on the CreateMultipartUploadRequest." +
256260
"This may be a new S3 feature." +
257261
"Please report this to the Amazon S3 Encryption Client for Java: " +

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ public MultipartUploadObjectPipeline build() {
262262
.secureRandom(_secureRandom)
263263
.build();
264264
}
265+
if(_instructionFileConfig == null) {
266+
_instructionFileConfig = InstructionFileConfig.builder().build();
267+
}
265268
_contentMetadataEncodingStrategy = new ContentMetadataEncodingStrategy(_instructionFileConfig);
266269
return new MultipartUploadObjectPipeline(this);
267270
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public PutEncryptedObjectPipeline build() {
121121
.secureRandom(_secureRandom)
122122
.build();
123123
}
124+
if(_instructionFileConfig == null) {
125+
_instructionFileConfig = InstructionFileConfig.builder().build();
126+
}
124127
_contentMetadataEncodingStrategy = new ContentMetadataEncodingStrategy(_instructionFileConfig);
125128

126129
return new PutEncryptedObjectPipeline(this);

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
4646
import software.amazon.awssdk.services.s3.model.S3Exception;
4747
import software.amazon.awssdk.services.s3.model.StorageClass;
48+
import software.amazon.awssdk.services.s3.model.StorageClassAnalysisDataExport;
4849
import software.amazon.awssdk.services.s3.multipart.MultipartConfiguration;
4950
import software.amazon.encryption.s3.internal.ConvertSDKRequests;
5051
import software.amazon.encryption.s3.internal.InstructionFileConfig;
@@ -883,8 +884,8 @@ public void testAsyncInstructionFileConfigMultipart() {
883884
public void testAsyncInstructionFileConfigMultipartWithOptions() {
884885
final String objectKey = appendTestSuffix("test-multipart-async-instruction-file-config-options");
885886
final String input = "SimpleTestOfV3EncryptionClient";
887+
final StorageClass storageClass = StorageClass.STANDARD_IA;
886888

887-
AwsCredentialsProvider credentials = DefaultCredentialsProvider.create();
888889
S3Client wrappedClient = S3Client.create();
889890
S3AsyncClient v3Client = S3AsyncEncryptionClient.builder()
890891
.instructionFileConfig(InstructionFileConfig.builder()
@@ -893,25 +894,16 @@ public void testAsyncInstructionFileConfigMultipartWithOptions() {
893894
.build())
894895
.kmsKeyId(KMS_KEY_ID)
895896
.enableMultipartPutObject(true)
896-
.credentialsProvider(credentials)
897897
.build();
898-
CreateMultipartUploadRequest multipartUploadRequest = CreateMultipartUploadRequest.builder()
898+
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
899899
.bucket(BUCKET)
900900
.key(objectKey)
901-
.storageClass(StorageClass.STANDARD_IA)
901+
.storageClass(storageClass)
902902
.build();
903-
assertNotNull(multipartUploadRequest);
904-
905-
PutObjectRequest putObjectRequest = ConvertSDKRequests.convertRequest(multipartUploadRequest);
906-
907-
assertNotNull(putObjectRequest);
908-
assertEquals(putObjectRequest.storageClassAsString(), multipartUploadRequest.storageClassAsString());
909903

910904
CompletableFuture<PutObjectResponse> putObjectResponse = v3Client.putObject(putObjectRequest, AsyncRequestBody.fromString(input));
911905
putObjectResponse.join();
912906

913-
assertNotNull(putObjectResponse);
914-
915907
ResponseBytes<GetObjectResponse> instructionFileResponse = wrappedClient.getObjectAsBytes(builder -> builder
916908
.bucket(BUCKET)
917909
.key(objectKey + ".instruction")
@@ -920,12 +912,8 @@ public void testAsyncInstructionFileConfigMultipartWithOptions() {
920912
Map<String, String> metadata = instructionFileResponse.response().metadata();
921913
assertTrue(metadata.containsKey("x-amz-crypto-instr-file"));
922914

923-
HeadObjectResponse instructionHeadResponse = wrappedClient.headObject(builder -> builder
924-
.bucket(BUCKET)
925-
.key(objectKey + ".instruction")
926-
.build());
927-
assertNotNull(instructionHeadResponse.storageClass());
928-
assertEquals(instructionHeadResponse.storageClassAsString(), StorageClass.STANDARD_IA.toString());
915+
assertEquals(storageClass.toString(), instructionFileResponse.response().storageClassAsString());
916+
929917
CompletableFuture<ResponseBytes<GetObjectResponse>> futureGetObj = v3Client.getObject(builder -> builder
930918
.bucket(BUCKET)
931919
.key(objectKey)
@@ -934,7 +922,7 @@ public void testAsyncInstructionFileConfigMultipartWithOptions() {
934922
assertNotNull(getResponse);
935923
assertEquals(input, getResponse.asUtf8String());
936924

937-
assertEquals(getResponse.response().storageClassAsString(), StorageClass.STANDARD_IA.toString());
925+
assertEquals(getResponse.response().storageClassAsString(), storageClass.toString());
938926

939927
deleteObject(BUCKET, objectKey, v3Client);
940928
v3Client.close();

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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public void AesWrapV1toV3() {
138138

139139
// Asserts
140140
final String input = "AesGcmV1toV3";
141-
System.out.println(System.getenv("AWS_S3EC_TEST_BUCKET"));
142141
v1Client.putObject(BUCKET, objectKey, input);
143142

144143
ResponseBytes<GetObjectResponse> objectResponse = v3Client.getObjectAsBytes(builder -> builder
@@ -222,39 +221,6 @@ public void AesGcmV2toV3WithInstructionFile() {
222221
deleteObject(BUCKET, objectKey, v3Client);
223222
v3Client.close();
224223
}
225-
@Test
226-
public void multipartPutObjectWithOptionsAndInstructionFileV2() throws IOException, InterruptedException, ExecutionException {
227-
final String objectKey = appendTestSuffix("multipart-put-object-with-options-and-instruction-file-v2");
228-
final long fileSizeLimit = 1024 * 1024 * 10; //sets file size limit to 10 MB
229-
final InputStream inputStream = new BoundedInputStream(fileSizeLimit);
230-
231-
//Now, we will create encryption client (v2) with instruction file config enabled and multipart upload enabled
232-
EncryptionMaterialsProvider materialsProvider =
233-
new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY));
234-
CryptoConfigurationV2 cryptoConfig =
235-
new CryptoConfigurationV2(CryptoMode.StrictAuthenticatedEncryption)
236-
.withStorageMode(CryptoStorageMode.InstructionFile);
237-
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
238-
.withCryptoConfiguration(cryptoConfig)
239-
.withEncryptionMaterialsProvider(materialsProvider)
240-
.build();
241-
UploadObjectRequest uploadObjectRequest = new UploadObjectRequest(BUCKET, objectKey, inputStream, new ObjectMetadata())
242-
.withPartSize(1024 * 1024 * 5)
243-
.withStorageClass(StorageClass.StandardInfrequentAccess);
244-
v2Client.uploadObject(uploadObjectRequest);
245-
246-
//Assert that the storage class on main object matches "GLACIER"
247-
GetObjectMetadataRequest mainObjectRequest = new GetObjectMetadataRequest(BUCKET, objectKey);
248-
ObjectMetadata mainObjectMetadata = v2Client.getObjectMetadata(mainObjectRequest);
249-
assertEquals("STANDARD_IA", mainObjectMetadata.getStorageClass());
250-
251-
//Assert that the instruction file does not contain storage class (V2)
252-
GetObjectMetadataRequest instructionObjectRequest = new GetObjectMetadataRequest(BUCKET, objectKey + ".instruction");
253-
ObjectMetadata instructionFileMetadata = v2Client.getObjectMetadata(instructionObjectRequest);
254-
255-
assertNotEquals("STANDARD_IA", instructionFileMetadata.getStorageClass());
256-
257-
}
258224

259225
@Test
260226
public void AesGcmV3toV1() {
@@ -633,7 +599,7 @@ public void KmsV1toV3() {
633599
assertEquals(input, output);
634600

635601
// Cleanup
636-
// deleteObject(BUCKET, objectKey, v3Client);
602+
deleteObject(BUCKET, objectKey, v3Client);
637603
v3Client.close();
638604
}
639605

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
2222
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
2323
import software.amazon.awssdk.services.s3.model.SdkPartType;
24+
import software.amazon.awssdk.services.s3.model.StorageClass;
2425
import software.amazon.awssdk.services.s3.model.UploadPartRequest;
2526
import software.amazon.awssdk.services.s3.model.UploadPartResponse;
2627
import software.amazon.encryption.s3.internal.InstructionFileConfig;
@@ -96,7 +97,7 @@ public void testInstructionFileExists() {
9697

9798
@Test
9899
public void testDisabledClientFails() {
99-
final String objectKey = appendTestSuffix("instruction-file-put-object");
100+
final String objectKey = appendTestSuffix("instruction-file-put-object-disabled-fails");
100101
final String input = "SimpleTestOfV3EncryptionClient";
101102
S3Client wrappedClient = S3Client.create();
102103
S3Client s3Client = S3EncryptionClient.builder()
@@ -144,7 +145,7 @@ public void testDisabledClientFails() {
144145
*/
145146
@Test
146147
public void testInstructionFileDelete() {
147-
final String objectKey = appendTestSuffix("instruction-file-put-object");
148+
final String objectKey = appendTestSuffix("instruction-file-put-object-delete");
148149
final String input = "SimpleTestOfV3EncryptionClient";
149150
S3Client wrappedClient = S3Client.create();
150151
S3Client s3Client = S3EncryptionClient.builder()
@@ -318,6 +319,7 @@ public void testMultipartPutWithInstructionFile() throws IOException, NoSuchAlgo
318319
final long fileSizeLimit = 1024 * 1024 * 50; //50 MB
319320
final InputStream inputStream = new BoundedInputStream(fileSizeLimit);
320321
final InputStream objectStreamForResult = new BoundedInputStream(fileSizeLimit);
322+
final StorageClass storageClass = StorageClass.STANDARD_IA;
321323

322324
S3Client wrappedClient = S3Client.create();
323325
S3Client s3Client = S3EncryptionClient.builder()
@@ -326,6 +328,7 @@ public void testMultipartPutWithInstructionFile() throws IOException, NoSuchAlgo
326328
.enableInstructionFilePutObject(true)
327329
.build())
328330
.kmsKeyId(KMS_KEY_ID)
331+
.enableMultipartPutObject(true)
329332
.build();
330333

331334
Map<String, String> encryptionContext = new HashMap<>();
@@ -334,6 +337,7 @@ public void testMultipartPutWithInstructionFile() throws IOException, NoSuchAlgo
334337

335338
s3Client.putObject(builder -> builder
336339
.bucket(BUCKET)
340+
.storageClass(storageClass)
337341
.overrideConfiguration(withAdditionalConfiguration(encryptionContext))
338342
.key(object_key), RequestBody.fromInputStream(inputStream, fileSizeLimit));
339343

@@ -343,6 +347,7 @@ public void testMultipartPutWithInstructionFile() throws IOException, NoSuchAlgo
343347
.key(object_key + ".instruction")
344348
.build());
345349
assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file"));
350+
assertEquals(storageClass.toString(), directInstGetResponse.response().storageClassAsString());
346351

347352
ResponseInputStream<GetObjectResponse> getResponse = s3Client.getObject(builder -> builder
348353
.bucket(BUCKET)
@@ -364,6 +369,7 @@ public void testLowLevelMultipartPutWithInstructionFile() throws NoSuchAlgorithm
364369
final int PART_SIZE = 10 * 1024 * 1024;
365370
final InputStream inputStream = new BoundedInputStream(fileSizeLimit);
366371
final InputStream objectStreamForResult = new BoundedInputStream(fileSizeLimit);
372+
final StorageClass storageClass = StorageClass.STANDARD_IA;
367373

368374
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
369375
keyPairGen.initialize(2048);
@@ -382,7 +388,7 @@ public void testLowLevelMultipartPutWithInstructionFile() throws NoSuchAlgorithm
382388

383389

384390
CreateMultipartUploadResponse initiateResult = v3Client.createMultipartUpload(builder ->
385-
builder.bucket(BUCKET).key(object_key));
391+
builder.bucket(BUCKET).key(object_key).storageClass(storageClass));
386392

387393
List<CompletedPart> partETags = new ArrayList<>();
388394

@@ -443,6 +449,7 @@ public void testLowLevelMultipartPutWithInstructionFile() throws NoSuchAlgorithm
443449
.key(object_key + ".instruction")
444450
.build());
445451
assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file"));
452+
assertEquals(storageClass.toString(), directInstGetResponse.response().storageClassAsString());
446453

447454
ResponseInputStream<GetObjectResponse> getResponse = v3Client.getObject(builder -> builder
448455
.bucket(BUCKET)

src/test/java/software/amazon/encryption/s3/internal/ConvertSDKRequestsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
44
import software.amazon.awssdk.services.s3.model.*;
55

6-
import java.awt.event.ComponentListener;
76
import java.time.Duration;
87
import java.time.Instant;
98
import java.util.HashMap;
@@ -486,7 +485,7 @@ public void testBasicConvertMultipartUploadRequest() {
486485
}
487486

488487
@Test
489-
public void testConversionAllFields() {
488+
public void testConversionAllFieldsMultipartUploadRequestToPutObjectRequest() {
490489
Map<String, String> metadata = new HashMap<String, String>();
491490
metadata.put("test-key-1", "test-value-1");
492491
metadata.put("test-key-2", "test-value-2");

0 commit comments

Comments
 (0)