Skip to content

Commit ed761d9

Browse files
committed
more tests in new class
1 parent 3546538 commit ed761d9

File tree

2 files changed

+221
-75
lines changed

2 files changed

+221
-75
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package software.amazon.encryption.s3;
2+
3+
import com.amazonaws.services.s3.AmazonS3EncryptionClientV2;
4+
import com.amazonaws.services.s3.AmazonS3EncryptionV2;
5+
import com.amazonaws.services.s3.model.CryptoConfigurationV2;
6+
import com.amazonaws.services.s3.model.CryptoMode;
7+
import com.amazonaws.services.s3.model.CryptoStorageMode;
8+
import com.amazonaws.services.s3.model.EncryptionMaterialsProvider;
9+
import com.amazonaws.services.s3.model.KMSEncryptionMaterials;
10+
import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider;
11+
import org.junit.jupiter.api.Test;
12+
import software.amazon.awssdk.core.ResponseBytes;
13+
import software.amazon.awssdk.core.sync.RequestBody;
14+
import software.amazon.awssdk.services.s3.S3Client;
15+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
16+
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
17+
import software.amazon.encryption.s3.internal.InstructionFileConfig;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.fail;
22+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.BUCKET;
23+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_KEY_ID;
24+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix;
25+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject;
26+
27+
public class S3EncryptionClientInstructionFileTest {
28+
29+
@Test
30+
public void testInstructionFileExists() {
31+
final String objectKey = appendTestSuffix("instruction-file-put-object");
32+
final String input = "SimpleTestOfV3EncryptionClient";
33+
S3Client wrappedClient = S3Client.create();
34+
S3Client s3Client = S3EncryptionClient.builder()
35+
.instructionFileConfig(InstructionFileConfig.builder()
36+
.instructionFileClient(wrappedClient)
37+
.enableInstructionFilePutObject(true)
38+
.build())
39+
.kmsKeyId(KMS_KEY_ID)
40+
.build();
41+
42+
s3Client.putObject(builder -> builder
43+
.bucket(BUCKET)
44+
.key(objectKey)
45+
.build(), RequestBody.fromString(input));
46+
47+
// Get the instruction file separately using a default client
48+
S3Client defaultClient = S3Client.create();
49+
ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder
50+
.bucket(BUCKET)
51+
.key(objectKey + ".instruction")
52+
.build());
53+
// Ensure its metadata identifies it as such
54+
assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file"));
55+
56+
// Ensure decryption succeeds
57+
ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder
58+
.bucket(BUCKET)
59+
.key(objectKey)
60+
.build());
61+
String output = objectResponse.asUtf8String();
62+
assertEquals(input, output);
63+
64+
deleteObject(BUCKET, objectKey, s3Client);
65+
s3Client.close();
66+
defaultClient.close();
67+
}
68+
69+
@Test
70+
public void testDisabledClientFails() {
71+
final String objectKey = appendTestSuffix("instruction-file-put-object");
72+
final String input = "SimpleTestOfV3EncryptionClient";
73+
S3Client wrappedClient = S3Client.create();
74+
S3Client s3Client = S3EncryptionClient.builder()
75+
.instructionFileConfig(InstructionFileConfig.builder()
76+
.instructionFileClient(wrappedClient)
77+
.enableInstructionFilePutObject(true)
78+
.build())
79+
.kmsKeyId(KMS_KEY_ID)
80+
.build();
81+
82+
// Put with Instruction File
83+
s3Client.putObject(builder -> builder
84+
.bucket(BUCKET)
85+
.key(objectKey)
86+
.build(), RequestBody.fromString(input));
87+
88+
// Disabled client should fail
89+
S3Client s3ClientDisabledInstructionFile = S3EncryptionClient.builder()
90+
.wrappedClient(wrappedClient)
91+
.instructionFileConfig(InstructionFileConfig.builder()
92+
.disableInstructionFile(true)
93+
.build())
94+
.kmsKeyId(KMS_KEY_ID)
95+
.build();
96+
97+
try {
98+
s3ClientDisabledInstructionFile.getObjectAsBytes(builder -> builder
99+
.bucket(BUCKET)
100+
.key(objectKey)
101+
.build());
102+
fail("expected exception");
103+
} catch (S3EncryptionClientException exception) {
104+
assertTrue(exception.getMessage().contains("Exception encountered while fetching Instruction File."));
105+
}
106+
107+
deleteObject(BUCKET, objectKey, s3Client);
108+
s3Client.close();
109+
s3ClientDisabledInstructionFile.close();
110+
}
111+
112+
113+
/**
114+
* This test is somewhat redundant given deletion itself is tested in
115+
* e.g. deleteObjectWithInstructionFileSuccess, but is included anyway to be thorough
116+
*/
117+
@Test
118+
public void testInstructionFileDelete() {
119+
final String objectKey = appendTestSuffix("instruction-file-put-object");
120+
final String input = "SimpleTestOfV3EncryptionClient";
121+
S3Client wrappedClient = S3Client.create();
122+
S3Client s3Client = S3EncryptionClient.builder()
123+
.instructionFileConfig(InstructionFileConfig.builder()
124+
.instructionFileClient(wrappedClient)
125+
.enableInstructionFilePutObject(true)
126+
.build())
127+
.kmsKeyId(KMS_KEY_ID)
128+
.build();
129+
130+
s3Client.putObject(builder -> builder
131+
.bucket(BUCKET)
132+
.key(objectKey)
133+
.build(), RequestBody.fromString(input));
134+
135+
// Get the instruction file separately using a default client
136+
S3Client defaultClient = S3Client.create();
137+
ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder
138+
.bucket(BUCKET)
139+
.key(objectKey + ".instruction")
140+
.build());
141+
// Ensure its metadata identifies it as such
142+
assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file"));
143+
144+
// Ensure decryption succeeds
145+
ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder
146+
.bucket(BUCKET)
147+
.key(objectKey)
148+
.build());
149+
String output = objectResponse.asUtf8String();
150+
assertEquals(input, output);
151+
152+
deleteObject(BUCKET, objectKey, s3Client);
153+
154+
try {
155+
defaultClient.getObjectAsBytes(builder -> builder
156+
.bucket(BUCKET)
157+
.key(objectKey + ".instruction")
158+
.build());
159+
fail("expected exception!");
160+
} catch (NoSuchKeyException e) {
161+
// expected
162+
}
163+
164+
s3Client.close();
165+
defaultClient.close();
166+
}
167+
@Test
168+
public void testPutWithInstructionFile() {
169+
final String objectKey = appendTestSuffix("instruction-file-put-object");
170+
final String objectKeyV2 = appendTestSuffix("instruction-file-put-object-v2");
171+
final String input = "SimpleTestOfV3EncryptionClient";
172+
S3Client wrappedClient = S3Client.create();
173+
S3Client s3Client = S3EncryptionClient.builder()
174+
.instructionFileConfig(InstructionFileConfig.builder()
175+
.instructionFileClient(wrappedClient)
176+
.enableInstructionFilePutObject(true)
177+
.build())
178+
.kmsKeyId(KMS_KEY_ID)
179+
.build();
180+
181+
s3Client.putObject(builder -> builder
182+
.bucket(BUCKET)
183+
.key(objectKey)
184+
.build(), RequestBody.fromString(input));
185+
186+
// Get the instruction file separately using a default client
187+
S3Client defaultClient = S3Client.create();
188+
ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder
189+
.bucket(BUCKET)
190+
.key(objectKey + ".instruction")
191+
.build());
192+
assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file"));
193+
194+
ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder
195+
.bucket(BUCKET)
196+
.key(objectKey)
197+
.build());
198+
String output = objectResponse.asUtf8String();
199+
assertEquals(input, output);
200+
201+
// Temporary - Generate an instruction file in V2 to compare against V3
202+
// TODO: do this for other keyrings as well
203+
// TODO: Instead, make a V3ToV2 test
204+
EncryptionMaterialsProvider materialsProvider =
205+
new StaticEncryptionMaterialsProvider(new KMSEncryptionMaterials(KMS_KEY_ID));
206+
CryptoConfigurationV2 cryptoConfig =
207+
new CryptoConfigurationV2(CryptoMode.StrictAuthenticatedEncryption)
208+
.withStorageMode(CryptoStorageMode.InstructionFile);
209+
210+
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
211+
.withCryptoConfiguration(cryptoConfig)
212+
.withEncryptionMaterialsProvider(materialsProvider)
213+
.build();
214+
215+
v2Client.putObject(BUCKET, objectKeyV2, input);
216+
217+
// Cleanup
218+
// deleteObject(BUCKET, objectKey, s3Client);
219+
s3Client.close();
220+
}
221+
}

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

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,81 +1102,6 @@ public void testInstructionFileConfig() {
11021102
s3Client.close();
11031103
}
11041104

1105-
@Test
1106-
public void testPutWithInstructionFile() {
1107-
final String objectKey = appendTestSuffix("instruction-file-put-object");
1108-
final String objectKeyV2 = appendTestSuffix("instruction-file-put-object-v2");
1109-
final String input = "SimpleTestOfV3EncryptionClient";
1110-
S3Client wrappedClient = S3Client.create();
1111-
S3Client s3Client = S3EncryptionClient.builder()
1112-
.instructionFileConfig(InstructionFileConfig.builder()
1113-
.instructionFileClient(wrappedClient)
1114-
.enableInstructionFilePutObject(true)
1115-
.build())
1116-
.kmsKeyId(KMS_KEY_ID)
1117-
.build();
1118-
1119-
s3Client.putObject(builder -> builder
1120-
.bucket(BUCKET)
1121-
.key(objectKey)
1122-
.build(), RequestBody.fromString(input));
1123-
1124-
// Disabled client should fail
1125-
S3Client s3ClientDisabledInstructionFile = S3EncryptionClient.builder()
1126-
.wrappedClient(wrappedClient)
1127-
.instructionFileConfig(InstructionFileConfig.builder()
1128-
.disableInstructionFile(true)
1129-
.build())
1130-
.kmsKeyId(KMS_KEY_ID)
1131-
.build();
1132-
1133-
try {
1134-
s3ClientDisabledInstructionFile.getObjectAsBytes(builder -> builder
1135-
.bucket(BUCKET)
1136-
.key(objectKey)
1137-
.build());
1138-
fail("expected exception");
1139-
} catch (S3EncryptionClientException exception) {
1140-
assertTrue(exception.getMessage().contains("Exception encountered while fetching Instruction File."));
1141-
}
1142-
1143-
// Get the instruction file separately using a default client
1144-
S3Client defaultClient = S3Client.create();
1145-
ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder
1146-
.bucket(BUCKET)
1147-
.key(objectKey + ".instruction")
1148-
.build());
1149-
assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file"));
1150-
1151-
ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder
1152-
.bucket(BUCKET)
1153-
.key(objectKey)
1154-
.build());
1155-
String output = objectResponse.asUtf8String();
1156-
assertEquals(input, output);
1157-
1158-
1159-
// Temporary - Generate an instruction file in V2 to compare against V3
1160-
// TODO: do this for other keyrings as well
1161-
EncryptionMaterialsProvider materialsProvider =
1162-
new StaticEncryptionMaterialsProvider(new KMSEncryptionMaterials(KMS_KEY_ID));
1163-
CryptoConfigurationV2 cryptoConfig =
1164-
new CryptoConfigurationV2(CryptoMode.StrictAuthenticatedEncryption)
1165-
.withStorageMode(CryptoStorageMode.InstructionFile);
1166-
1167-
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
1168-
.withCryptoConfiguration(cryptoConfig)
1169-
.withEncryptionMaterialsProvider(materialsProvider)
1170-
.build();
1171-
1172-
v2Client.putObject(BUCKET, objectKeyV2, input);
1173-
1174-
// Cleanup
1175-
// deleteObject(BUCKET, objectKey, s3Client);
1176-
s3ClientDisabledInstructionFile.close();
1177-
s3Client.close();
1178-
}
1179-
11801105
/**
11811106
* A simple, reusable round-trip (encryption + decryption) using a given
11821107
* S3Client. Useful for testing client configuration.

0 commit comments

Comments
 (0)