Skip to content

Commit 41a550e

Browse files
adding snippets to S3 Client Side Encryption articles
1 parent 1e037fa commit 41a550e

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

java/example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package aws.example.s3;
2424

25+
// snippet-start:[s3.java1.s3_encrypt.complete]
26+
// snippet-start:[s3.java1.s3_encrypt.import]
27+
2528
import com.amazonaws.regions.Region;
2629
import com.amazonaws.regions.Regions;
2730
import com.amazonaws.services.s3.AmazonS3;
@@ -34,28 +37,28 @@
3437
import com.amazonaws.services.s3.model.GetObjectRequest;
3538
import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;
3639
import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider;
40+
3741
import java.security.KeyPair;
3842
import java.security.KeyPairGenerator;
3943
import java.security.NoSuchAlgorithmException;
4044
import javax.crypto.KeyGenerator;
4145
import javax.crypto.SecretKey;
46+
// snippet-end:[s3.java1.s3_encrypt.import]
4247

4348
/**
4449
* Test out various cryptography settings for S3.
45-
*
50+
* <p>
4651
* This code expects that you have AWS credentials set up per:
4752
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
4853
* This code also requires you to install the Unlimited Strength Java(TM) Cryptography Extension Policy Files (JCE)
4954
* You can install this from the oracle site: http://www.oracle.com
5055
*/
51-
public class S3Encrypt
52-
{
56+
public class S3Encrypt {
5357
public static final String BUCKET_NAME = "s3EncryptTestBucket"; //add your bucket name
5458
public static final String ENCRYPTED_KEY = "enc-key";
5559
public static final String NON_ENCRYPTED_KEY = "some-key";
5660

57-
public static void main(String[] args)
58-
{
61+
public static void main(String[] args) {
5962
System.out.println("calling encryption with customer managed keys");
6063
S3Encrypt encrypt = new S3Encrypt();
6164

@@ -72,7 +75,9 @@ public static void main(String[] args)
7275
* encryption requires the bouncy castle provider to be on the classpath. Also, for authenticated encryption the size
7376
* of the data can be no longer than 64 GB.
7477
*/
78+
// snippet-start:[s3.java1.s3_encrypt.authenticated_encryption]
7579
public void authenticatedEncryption_CustomerManagedKey() throws NoSuchAlgorithmException {
80+
// snippet-start:[s3.java1.s3_encrypt.authenticated_encryption_build]
7681
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
7782
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
7883
.standard()
@@ -82,19 +87,23 @@ public void authenticatedEncryption_CustomerManagedKey() throws NoSuchAlgorithmE
8287
.build();
8388

8489
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
90+
// snippet-end:[s3.java1.s3_encrypt.authenticated_encryption_build]
8591

8692
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
8793
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
8894
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
8995
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
9096
}
97+
// snippet-end:[s3.java1.s3_encrypt.authenticated_encryption]
9198

9299
/**
93100
* For ranged GET we do not use authenticated encryption since we aren't reading the entire message and can't produce the
94101
* MAC. Instead we use AES/CTR, an unauthenticated encryption algorithm. If {@link CryptoMode#StrictAuthenticatedEncryption}
95102
* is enabled, ranged GETs will not be allowed since they do not use authenticated encryption..
96103
*/
104+
// snippet-start:[s3.java1.s3_encrypt.strict_authenticated_encryption]
97105
public void authenticatedEncryption_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException {
106+
// snippet-start:[s3.java1.s3_encrypt.strict_authenticated_encryption_build]
98107
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
99108
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
100109
.standard()
@@ -104,12 +113,14 @@ public void authenticatedEncryption_RangeGet_CustomerManagedKey() throws NoSuchA
104113
.build();
105114

106115
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
116+
// snippet-end:[s3.java1.s3_encrypt.strict_authenticated_encryption_build]
107117

108118
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
109119
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
110120
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
111121
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
112122
}
123+
// snippet-end:[s3.java1.s3_encrypt.strict_authenticated_encryption]
113124

114125
/**
115126
* Same as {@link #authenticatedEncryption_CustomerManagedKey()} except uses an asymmetric key pair and
@@ -204,6 +215,7 @@ public void encryptionOnly_CustomerManagedKey() throws NoSuchAlgorithmException
204215
/**
205216
* Non-authenticated encryption schemes can do range GETs without an issue.
206217
*/
218+
// snippet-start:[s3.java1.s3_encrypt.encryption_only]
207219
public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException {
208220
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
209221
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
@@ -215,15 +227,18 @@ public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmE
215227

216228
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
217229
System.out.println(s3Encryption.getObject(new GetObjectRequest(BUCKET_NAME, ENCRYPTED_KEY)
218-
.withRange(0, 2)));
230+
.withRange(0, 2)));
219231
}
232+
// snippet-end:[s3.java1.s3_encrypt.encryption_only]
220233

221234
/**
222235
* Uses an asymmetric key pair instead of a symmetric key. Note this does not change the algorithm used to encrypt
223236
* the content, that will still be a symmetric key algorithm (AES/CBC in this case) using the derived CEK. It does impact
224237
* the algorithm used to encrypt the CEK, in this case we use RSA/ECB/OAEPWithSHA-256AndMGF1Padding.
225238
*/
239+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key]
226240
public void encryptionOnly_CustomerManagedAsymetricKey() throws NoSuchAlgorithmException {
241+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_build]
227242
KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
228243
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
229244
.standard()
@@ -233,17 +248,25 @@ public void encryptionOnly_CustomerManagedAsymetricKey() throws NoSuchAlgorithmE
233248
.build();
234249

235250
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
251+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_build]
236252

253+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_put_object]
237254
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
238255
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
256+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_put_object]
257+
// snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_retrieve]
239258
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
240259
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
260+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_retrieve]
241261
}
262+
// snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key]
242263

243264
/**
244265
* This uses the V2 metadata schema with a key wrap algorithm of 'kms' and a CEK algorithm of AES/CBC/PKCS5Padding.
245266
*/
267+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only]
246268
public void encryptionOnly_KmsManagedKey() throws NoSuchAlgorithmException {
269+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_build]
247270
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
248271
.standard()
249272
.withRegion(Regions.US_WEST_2)
@@ -253,12 +276,18 @@ public void encryptionOnly_KmsManagedKey() throws NoSuchAlgorithmException {
253276
.build();
254277

255278
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
279+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_build]
256280

281+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_put_object]
257282
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
258283
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
284+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_put_object]
285+
// snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_retrieve]
259286
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
260287
System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
288+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_retrieve]
261289
}
290+
// snippet-end:[s3.java1.s3_encrypt.kms_encryption_only]
262291

263292
/**
264293
* This uses the V2 metadata schema with a key wrap algorithm of 'kms' and a CEK algorithm of AES/GCM/NoPadding.
@@ -284,7 +313,9 @@ public void authenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithmExcept
284313
* Same as authenticatedEncryption_KmsManagedKey except throws an exception when trying to get objects not encrypted with
285314
* AES/GCM.
286315
*/
316+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption]
287317
public void strictAuthenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithmException {
318+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_builder]
288319
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
289320
.standard()
290321
.withRegion(Regions.US_WEST_2)
@@ -294,14 +325,23 @@ public void strictAuthenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithm
294325
.build();
295326

296327
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.defaultClient();
328+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_builder]
297329

330+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_put_object]
298331
s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
299332
s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
333+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_put_object]
334+
// snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_exception]
300335
try {
301336
s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY);
302337
} catch (SecurityException e) {
303338
// Strict authenticated encryption will throw an exception if an object is not encrypted with AES/GCM
304339
System.err.println(NON_ENCRYPTED_KEY + " was not encrypted with AES/GCM");
305340
}
341+
342+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_exception]
306343
}
344+
// snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption]
345+
307346
}
347+
// snippet-end:[s3.java1.s3_encrypt.complete]

0 commit comments

Comments
 (0)