Skip to content

Commit 09c734d

Browse files
author
Anirav Kareddy
committed
added test cases for materials description in instruction file
1 parent db3b701 commit 09c734d

File tree

1 file changed

+114
-2
lines changed

1 file changed

+114
-2
lines changed

src/test/java/software/amazon/encryption/s3/materials/MaterialsDescriptionTest.java

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package software.amazon.encryption.s3.materials;
22

3+
34
import org.junit.jupiter.api.BeforeAll;
45
import org.junit.jupiter.api.Test;
56

67
import software.amazon.awssdk.core.ResponseBytes;
78
import software.amazon.awssdk.core.sync.RequestBody;
9+
10+
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
11+
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
12+
import software.amazon.awssdk.services.s3.S3Client;
813
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
914
import software.amazon.encryption.s3.S3EncryptionClient;
15+
import software.amazon.encryption.s3.internal.InstructionFileConfig;
1016

1117
import javax.crypto.KeyGenerator;
1218
import javax.crypto.SecretKey;
@@ -153,8 +159,8 @@ public void testRsaMaterialsDescriptionInObjectMetadata() {
153159
S3EncryptionClient client = S3EncryptionClient.builder()
154160
.keyring(rsaKeyring)
155161
.build();
156-
final String input = "Testing Materials Description in Object Metadata!";
157-
final String objectKey = "test-rsa-materials-description-in-object-metadata";
162+
final String input = "Testing Materials Description in Instruction File!";
163+
final String objectKey = "test-rsa-materials-description-in-instruction-file";
158164

159165
client.putObject(builder -> builder
160166
.bucket(BUCKET)
@@ -168,7 +174,113 @@ public void testRsaMaterialsDescriptionInObjectMetadata() {
168174
assertEquals(input, responseBytes.asUtf8String());
169175
assertEquals("{\"admin\":\"yes\",\"version\":\"1.0\"}", responseBytes.response().metadata().get("x-amz-matdesc"));
170176

177+
deleteObject(BUCKET, objectKey, client);
178+
171179
}
180+
@Test
181+
public void testAesMaterialsDescriptionInInstructionFile() {
182+
MaterialsDescription materialsDescription = MaterialsDescription.builder()
183+
.put("version", "1.0")
184+
.build();
185+
AesKeyring aesKeyring = AesKeyring.builder()
186+
.wrappingKey(AES_KEY)
187+
.reEncryptInstructionFile(true)
188+
.secureRandom(new SecureRandom())
189+
.materialsDescription(materialsDescription)
190+
.build();
191+
192+
S3Client wrappedClient= S3Client.create();
193+
S3EncryptionClient client = S3EncryptionClient.builder()
194+
.keyring(aesKeyring)
195+
.instructionFileConfig(InstructionFileConfig.builder()
196+
.enableInstructionFilePutObject(true)
197+
.instructionFileClient(wrappedClient)
198+
.build())
199+
.build();
172200

201+
final String input = "Testing Materials Description in Instruction File!";
202+
final String objectKey = "test-aes-materials-description-in-instruction-file";
203+
204+
client.putObject(builder -> builder
205+
.bucket(BUCKET)
206+
.key(objectKey)
207+
.build(), RequestBody.fromString(input)
208+
);
209+
ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(builder -> builder
210+
.bucket(BUCKET)
211+
.key(objectKey)
212+
.build());
213+
assertEquals(input, responseBytes.asUtf8String());
214+
215+
S3Client defaultClient= S3Client.create();
216+
217+
ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder
218+
.bucket(BUCKET)
219+
.key(objectKey + ".instruction")
220+
.build());
221+
222+
String instructionFileContent = directInstGetResponse.asUtf8String();
223+
JsonNodeParser parser = JsonNodeParser.create();
224+
JsonNode objectNode = parser.parse(instructionFileContent);
225+
226+
String matDesc = objectNode.asObject().get("x-amz-matdesc").asString();
227+
assertEquals("{\"version\":\"1.0\"}", matDesc);
228+
229+
}
230+
@Test
231+
public void testRsaMaterialsDescriptionInInstructionFile() {
232+
PartialRsaKeyPair keyPair = new PartialRsaKeyPair(RSA_KEY_PAIR.getPrivate(), RSA_KEY_PAIR.getPublic());
233+
MaterialsDescription materialsDescription = MaterialsDescription.builder()
234+
.put("version", "1.0")
235+
.put("admin", "yes")
236+
.build();
237+
238+
RsaKeyring rsaKeyring = RsaKeyring.builder()
239+
.wrappingKeyPair(keyPair)
240+
.reEncryptInstructionFile(true)
241+
.materialsDescription(materialsDescription)
242+
.build();
243+
244+
S3Client wrappedClient= S3Client.create();
245+
S3EncryptionClient client = S3EncryptionClient.builder()
246+
.keyring(rsaKeyring)
247+
.instructionFileConfig(InstructionFileConfig.builder()
248+
.enableInstructionFilePutObject(true)
249+
.instructionFileClient(wrappedClient)
250+
.build())
251+
.build();
252+
253+
final String input = "Testing Materials Description in Instruction File!";
254+
final String objectKey = "test-rsa-materials-description-in-object-metadata";
255+
256+
client.putObject(builder -> builder
257+
.bucket(BUCKET)
258+
.key(objectKey)
259+
.build(), RequestBody.fromString(input)
260+
);
261+
ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(builder -> builder
262+
.bucket(BUCKET)
263+
.key(objectKey)
264+
.build());
265+
assertEquals(input, responseBytes.asUtf8String());
266+
267+
S3Client defaultClient= S3Client.create();
268+
269+
ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder
270+
.bucket(BUCKET)
271+
.key(objectKey + ".instruction")
272+
.build());
273+
274+
String instructionFileContent = directInstGetResponse.asUtf8String();
275+
JsonNodeParser parser = JsonNodeParser.create();
276+
JsonNode objectNode = parser.parse(instructionFileContent);
277+
278+
String matDesc = objectNode.asObject().get("x-amz-matdesc").asString();
279+
assertEquals("{\"admin\":\"yes\",\"version\":\"1.0\"}", matDesc);
280+
281+
282+
deleteObject(BUCKET, objectKey, client);
283+
284+
}
173285

174286
}

0 commit comments

Comments
 (0)