• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 package software.amazon.awssdk.services.s3;
16 
17 import static org.assertj.core.api.Fail.fail;
18 import static software.amazon.awssdk.services.s3.S3IntegrationTestBase.createBucket;
19 import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.security.SecureRandom;
24 import javax.crypto.KeyGenerator;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import software.amazon.awssdk.services.kms.KmsClient;
28 import software.amazon.awssdk.services.s3.model.ServerSideEncryption;
29 import software.amazon.awssdk.testutils.RandomTempFile;
30 
31 public class ServerSideEncryptionIntegrationTestBase extends S3IntegrationTestBase {
32 
33     protected static final String BUCKET = temporaryBucketName(ServerSideEncryptionIntegrationTestBase.class);
34     protected static final String BUCKET_WITH_SSE = temporaryBucketName(ServerSideEncryptionIntegrationTestBase.class);
35 
36     private static final KmsClient KMS = KmsClient.builder()
37                                                   .region(DEFAULT_REGION)
38                                                   .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
39                                                   .build();
40 
41     protected static File file;
42 
43     private static String keyId;
44 
45     @BeforeClass
setupFixture()46     public static void setupFixture() throws IOException {
47         createBucket(BUCKET);
48         createBucket(BUCKET_WITH_SSE);
49         keyId = KMS.createKey().keyMetadata().keyId();
50 
51         s3.putBucketEncryption(r -> r
52             .bucket(BUCKET_WITH_SSE)
53             .serverSideEncryptionConfiguration(ssec -> ssec
54                 .rules(rule -> rule
55                     .applyServerSideEncryptionByDefault(d -> d.kmsMasterKeyID(keyId)
56                                                               .sseAlgorithm(ServerSideEncryption.AWS_KMS)))));
57         file = new RandomTempFile(10_000);
58     }
59 
60     @AfterClass
tearDownFixture()61     public static void tearDownFixture() {
62         deleteBucketAndAllContents(BUCKET);
63         deleteBucketAndAllContents(BUCKET_WITH_SSE);
64         file.delete();
65         KMS.scheduleKeyDeletion(r -> r.keyId(keyId));
66     }
67 
generateSecretKey()68     protected static byte[] generateSecretKey() {
69         KeyGenerator generator;
70         try {
71             generator = KeyGenerator.getInstance("AES");
72             generator.init(256, new SecureRandom());
73             return generator.generateKey().getEncoded();
74         } catch (Exception e) {
75             fail("Unable to generate symmetric key: " + e.getMessage());
76             return null;
77         }
78     }
79 }
80