33import org .junit .jupiter .api .BeforeAll ;
44import org .junit .jupiter .api .Test ;
55
6+ import software .amazon .awssdk .core .ResponseBytes ;
7+ import software .amazon .awssdk .core .sync .RequestBody ;
8+ import software .amazon .awssdk .services .s3 .model .GetObjectResponse ;
9+ import software .amazon .encryption .s3 .S3EncryptionClient ;
10+
611import javax .crypto .KeyGenerator ;
712import javax .crypto .SecretKey ;
813import java .security .KeyPair ;
914import java .security .KeyPairGenerator ;
1015import java .security .NoSuchAlgorithmException ;
16+ import java .security .SecureRandom ;
1117import java .util .HashMap ;
1218import java .util .Map ;
1319
1622import static org .junit .jupiter .api .Assertions .assertNull ;
1723import static org .junit .jupiter .api .Assertions .assertTrue ;
1824import static org .junit .jupiter .api .Assertions .fail ;
25+ import static software .amazon .encryption .s3 .utils .S3EncryptionClientTestResources .BUCKET ;
26+ import static software .amazon .encryption .s3 .utils .S3EncryptionClientTestResources .deleteObject ;
1927
2028public class MaterialsDescriptionTest {
2129 private static SecretKey AES_KEY ;
@@ -98,5 +106,69 @@ public void testMaterialsDescriptionRsaKeyring() {
98106 assertEquals (2 , rsaKeyring .getMaterialsDescription ().getMaterialsDescription ().size ());
99107
100108 }
109+ @ Test
110+ public void testAesMaterialsDescriptionInObjectMetadata () {
111+ MaterialsDescription materialsDescription = MaterialsDescription .builder ()
112+ .put ("version" , "1.0" )
113+ .build ();
114+ AesKeyring aesKeyring = AesKeyring .builder ()
115+ .wrappingKey (AES_KEY )
116+ .reEncryptInstructionFile (true )
117+ .secureRandom (new SecureRandom ())
118+ .materialsDescription (materialsDescription )
119+ .build ();
120+ S3EncryptionClient client = S3EncryptionClient .builder ()
121+ .keyring (aesKeyring )
122+ .build ();
123+ final String input = "Testing Materials Description in Object Metadata!" ;
124+ final String objectKey = "test-aes-materials-description-in-object-metadata" ;
125+
126+ client .putObject (builder -> builder
127+ .bucket (BUCKET )
128+ .key (objectKey )
129+ .build (), RequestBody .fromString (input )
130+ );
131+ ResponseBytes <GetObjectResponse > responseBytes = client .getObjectAsBytes (builder -> builder
132+ .bucket (BUCKET )
133+ .key (objectKey )
134+ .build ());
135+ assertEquals (input , responseBytes .asUtf8String ());
136+ assertEquals ("{\" version\" :\" 1.0\" }" , responseBytes .response ().metadata ().get ("x-amz-matdesc" ));
137+
138+ deleteObject (BUCKET , objectKey , client );
139+
140+ }
141+ @ Test
142+ public void testRsaMaterialsDescriptionInObjectMetadata () {
143+ PartialRsaKeyPair keyPair = new PartialRsaKeyPair (RSA_KEY_PAIR .getPrivate (), RSA_KEY_PAIR .getPublic ());
144+ MaterialsDescription materialsDescription = MaterialsDescription .builder ()
145+ .put ("version" , "1.0" )
146+ .put ("admin" , "yes" )
147+ .build ();
148+ RsaKeyring rsaKeyring = RsaKeyring .builder ()
149+ .wrappingKeyPair (keyPair )
150+ .reEncryptInstructionFile (true )
151+ .materialsDescription (materialsDescription )
152+ .build ();
153+ S3EncryptionClient client = S3EncryptionClient .builder ()
154+ .keyring (rsaKeyring )
155+ .build ();
156+ final String input = "Testing Materials Description in Object Metadata!" ;
157+ final String objectKey = "test-rsa-materials-description-in-object-metadata" ;
158+
159+ client .putObject (builder -> builder
160+ .bucket (BUCKET )
161+ .key (objectKey )
162+ .build (), RequestBody .fromString (input )
163+ );
164+ ResponseBytes <GetObjectResponse > responseBytes = client .getObjectAsBytes (builder -> builder
165+ .bucket (BUCKET )
166+ .key (objectKey )
167+ .build ());
168+ assertEquals (input , responseBytes .asUtf8String ());
169+ assertEquals ("{\" admin\" :\" yes\" ,\" version\" :\" 1.0\" }" , responseBytes .response ().metadata ().get ("x-amz-matdesc" ));
170+
171+ }
172+
101173
102174}
0 commit comments