5
5
import com .amazonaws .services .s3 .model .CryptoConfigurationV2 ;
6
6
import com .amazonaws .services .s3 .model .CryptoMode ;
7
7
import com .amazonaws .services .s3 .model .CryptoStorageMode ;
8
+ import com .amazonaws .services .s3 .model .EncryptionMaterials ;
8
9
import com .amazonaws .services .s3 .model .EncryptionMaterialsProvider ;
9
10
import com .amazonaws .services .s3 .model .KMSEncryptionMaterials ;
10
11
import com .amazonaws .services .s3 .model .StaticEncryptionMaterialsProvider ;
16
17
import software .amazon .awssdk .services .s3 .model .NoSuchKeyException ;
17
18
import software .amazon .encryption .s3 .internal .InstructionFileConfig ;
18
19
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
+
19
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
20
28
import static org .junit .jupiter .api .Assertions .assertTrue ;
21
29
import static org .junit .jupiter .api .Assertions .fail ;
@@ -164,10 +172,10 @@ public void testInstructionFileDelete() {
164
172
s3Client .close ();
165
173
defaultClient .close ();
166
174
}
175
+
167
176
@ 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" );
171
179
final String input = "SimpleTestOfV3EncryptionClient" ;
172
180
S3Client wrappedClient = S3Client .create ();
173
181
S3Client s3Client = S3EncryptionClient .builder ()
@@ -183,26 +191,89 @@ public void testPutWithInstructionFile() {
183
191
.key (objectKey )
184
192
.build (), RequestBody .fromString (input ));
185
193
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
189
230
.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 ));
193
233
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
195
271
.bucket (BUCKET )
196
272
.key (objectKey )
197
- .build ());
198
- String output = objectResponse .asUtf8String ();
199
- assertEquals (input , output );
273
+ .build (), RequestBody .fromString (input ));
200
274
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
275
EncryptionMaterialsProvider materialsProvider =
205
- new StaticEncryptionMaterialsProvider (new KMSEncryptionMaterials ( KMS_KEY_ID ));
276
+ new StaticEncryptionMaterialsProvider (new EncryptionMaterials ( rsaKey ));
206
277
CryptoConfigurationV2 cryptoConfig =
207
278
new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption )
208
279
.withStorageMode (CryptoStorageMode .InstructionFile );
@@ -212,10 +283,11 @@ public void testPutWithInstructionFile() {
212
283
.withEncryptionMaterialsProvider (materialsProvider )
213
284
.build ();
214
285
215
- v2Client .putObject (BUCKET , objectKeyV2 , input );
286
+ String result = v2Client .getObjectAsString (BUCKET , objectKey );
287
+ assertEquals (input , result );
216
288
217
289
// Cleanup
218
- // deleteObject(BUCKET, objectKey, s3Client);
290
+ deleteObject (BUCKET , objectKey , s3Client );
219
291
s3Client .close ();
220
292
}
221
293
}
0 commit comments