55import com .amazonaws .services .s3 .model .CryptoConfigurationV2 ;
66import com .amazonaws .services .s3 .model .CryptoMode ;
77import com .amazonaws .services .s3 .model .CryptoStorageMode ;
8+ import com .amazonaws .services .s3 .model .EncryptionMaterials ;
89import com .amazonaws .services .s3 .model .EncryptionMaterialsProvider ;
910import com .amazonaws .services .s3 .model .KMSEncryptionMaterials ;
1011import com .amazonaws .services .s3 .model .StaticEncryptionMaterialsProvider ;
1617import software .amazon .awssdk .services .s3 .model .NoSuchKeyException ;
1718import software .amazon .encryption .s3 .internal .InstructionFileConfig ;
1819
20+ import javax .crypto .KeyGenerator ;
21+ import javax .crypto .SecretKey ;
22+
23+ import java .security .KeyPair ;
24+ import java .security .KeyPairGenerator ;
25+ import java .security .NoSuchAlgorithmException ;
26+
1927import static org .junit .jupiter .api .Assertions .assertEquals ;
2028import static org .junit .jupiter .api .Assertions .assertTrue ;
2129import static org .junit .jupiter .api .Assertions .fail ;
@@ -164,10 +172,10 @@ public void testInstructionFileDelete() {
164172 s3Client .close ();
165173 defaultClient .close ();
166174 }
175+
167176 @ Test
168- public void testPutWithInstructionFile () {
169- final String objectKey = appendTestSuffix ("instruction-file-put-object" );
170- final String objectKeyV2 = appendTestSuffix ("instruction-file-put-object-v2" );
177+ public void testPutWithInstructionFileV3ToV2Kms () {
178+ final String objectKey = appendTestSuffix ("instruction-file-put-object-v3-to-v2-kms" );
171179 final String input = "SimpleTestOfV3EncryptionClient" ;
172180 S3Client wrappedClient = S3Client .create ();
173181 S3Client s3Client = S3EncryptionClient .builder ()
@@ -183,26 +191,89 @@ public void testPutWithInstructionFile() {
183191 .key (objectKey )
184192 .build (), RequestBody .fromString (input ));
185193
186- // Get the instruction file separately using a default client
187- S3Client defaultClient = S3Client .create ();
188- ResponseBytes <GetObjectResponse > directInstGetResponse = defaultClient .getObjectAsBytes (builder -> builder
194+ EncryptionMaterialsProvider materialsProvider =
195+ new StaticEncryptionMaterialsProvider (new KMSEncryptionMaterials (KMS_KEY_ID ));
196+ CryptoConfigurationV2 cryptoConfig =
197+ new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption )
198+ .withStorageMode (CryptoStorageMode .InstructionFile );
199+
200+ AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2 .encryptionBuilder ()
201+ .withCryptoConfiguration (cryptoConfig )
202+ .withEncryptionMaterialsProvider (materialsProvider )
203+ .build ();
204+
205+ String result = v2Client .getObjectAsString (BUCKET , objectKey );
206+ assertEquals (input , result );
207+
208+ // Cleanup
209+ deleteObject (BUCKET , objectKey , s3Client );
210+ s3Client .close ();
211+ }
212+
213+ @ Test
214+ public void testPutWithInstructionFileV3ToV2Aes () throws NoSuchAlgorithmException {
215+ KeyGenerator keyGen = KeyGenerator .getInstance ("AES" );
216+ keyGen .init (256 );
217+ SecretKey aesKey = keyGen .generateKey ();
218+ final String objectKey = appendTestSuffix ("instruction-file-put-object-v3-to-v2-aes" );
219+ final String input = "SimpleTestOfV3EncryptionClient" ;
220+ S3Client wrappedClient = S3Client .create ();
221+ S3Client s3Client = S3EncryptionClient .builder ()
222+ .instructionFileConfig (InstructionFileConfig .builder ()
223+ .instructionFileClient (wrappedClient )
224+ .enableInstructionFilePutObject (true )
225+ .build ())
226+ .aesKey (aesKey )
227+ .build ();
228+
229+ s3Client .putObject (builder -> builder
189230 .bucket (BUCKET )
190- .key (objectKey + ".instruction" )
191- .build ());
192- assertTrue (directInstGetResponse .response ().metadata ().containsKey ("x-amz-crypto-instr-file" ));
231+ .key (objectKey )
232+ .build (), RequestBody .fromString (input ));
193233
194- ResponseBytes <GetObjectResponse > objectResponse = s3Client .getObjectAsBytes (builder -> builder
234+ EncryptionMaterialsProvider materialsProvider =
235+ new StaticEncryptionMaterialsProvider (new EncryptionMaterials (aesKey ));
236+ CryptoConfigurationV2 cryptoConfig =
237+ new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption )
238+ .withStorageMode (CryptoStorageMode .InstructionFile );
239+
240+ AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2 .encryptionBuilder ()
241+ .withCryptoConfiguration (cryptoConfig )
242+ .withEncryptionMaterialsProvider (materialsProvider )
243+ .build ();
244+
245+ String result = v2Client .getObjectAsString (BUCKET , objectKey );
246+ assertEquals (input , result );
247+
248+ // Cleanup
249+ deleteObject (BUCKET , objectKey , s3Client );
250+ s3Client .close ();
251+ }
252+
253+ @ Test
254+ public void testPutWithInstructionFileV3ToV2Rsa () throws NoSuchAlgorithmException {
255+ KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("RSA" );
256+ keyPairGen .initialize (2048 );
257+ KeyPair rsaKey = keyPairGen .generateKeyPair ();
258+
259+ final String objectKey = appendTestSuffix ("instruction-file-put-object-v3-to-v2-rsa" );
260+ final String input = "SimpleTestOfV3EncryptionClient" ;
261+ S3Client wrappedClient = S3Client .create ();
262+ S3Client s3Client = S3EncryptionClient .builder ()
263+ .instructionFileConfig (InstructionFileConfig .builder ()
264+ .instructionFileClient (wrappedClient )
265+ .enableInstructionFilePutObject (true )
266+ .build ())
267+ .rsaKeyPair (rsaKey )
268+ .build ();
269+
270+ s3Client .putObject (builder -> builder
195271 .bucket (BUCKET )
196272 .key (objectKey )
197- .build ());
198- String output = objectResponse .asUtf8String ();
199- assertEquals (input , output );
273+ .build (), RequestBody .fromString (input ));
200274
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
204275 EncryptionMaterialsProvider materialsProvider =
205- new StaticEncryptionMaterialsProvider (new KMSEncryptionMaterials ( KMS_KEY_ID ));
276+ new StaticEncryptionMaterialsProvider (new EncryptionMaterials ( rsaKey ));
206277 CryptoConfigurationV2 cryptoConfig =
207278 new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption )
208279 .withStorageMode (CryptoStorageMode .InstructionFile );
@@ -212,10 +283,11 @@ public void testPutWithInstructionFile() {
212283 .withEncryptionMaterialsProvider (materialsProvider )
213284 .build ();
214285
215- v2Client .putObject (BUCKET , objectKeyV2 , input );
286+ String result = v2Client .getObjectAsString (BUCKET , objectKey );
287+ assertEquals (input , result );
216288
217289 // Cleanup
218- // deleteObject(BUCKET, objectKey, s3Client);
290+ deleteObject (BUCKET , objectKey , s3Client );
219291 s3Client .close ();
220292 }
221293}
0 commit comments