|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except |
| 5 | + * in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.amazonaws.crypto.examples.datakeycaching; |
| 15 | + |
| 16 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 17 | +import com.amazonaws.encryptionsdk.CryptoMaterialsManager; |
| 18 | +import com.amazonaws.encryptionsdk.EncryptRequest; |
| 19 | +import com.amazonaws.encryptionsdk.caching.CachingCryptoMaterialsManager; |
| 20 | +import com.amazonaws.encryptionsdk.caching.CryptoMaterialsCache; |
| 21 | +import com.amazonaws.encryptionsdk.caching.LocalCryptoMaterialsCache; |
| 22 | +import com.amazonaws.encryptionsdk.keyrings.Keyring; |
| 23 | +import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings; |
| 24 | +import com.amazonaws.encryptionsdk.kms.AwsKmsCmkId; |
| 25 | + |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +/** |
| 32 | + * <p> |
| 33 | + * Encrypts a string using an AWS KMS customer master key (CMK) and data key caching |
| 34 | + * |
| 35 | + * <p> |
| 36 | + * Arguments: |
| 37 | + * <ol> |
| 38 | + * <li>KMS CMK ARN: To find the Amazon Resource Name of your AWS KMS customer master key (CMK), |
| 39 | + * see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html |
| 40 | + * </ol> |
| 41 | + */ |
| 42 | +public class SimpleDataKeyCachingExample { |
| 43 | + |
| 44 | + /* |
| 45 | + * The maximum number of data keys in the cache (required). |
| 46 | + * When the cache is full, the oldest entry is evicted to |
| 47 | + * make room for a newer one. |
| 48 | + */ |
| 49 | + private static final int CAPACITY = 10; |
| 50 | + |
| 51 | + /* |
| 52 | + * The maximum number of messages encrypted under a single data key. |
| 53 | + * This value is optional, but you should configure the lowest practical value. |
| 54 | + */ |
| 55 | + private static final int MAX_ENTRY_MESSAGES = 100; |
| 56 | + |
| 57 | + /* |
| 58 | + * The time in seconds that an entry is cached (required). |
| 59 | + * The cache actively removes entries that have exceeded the thresholds. |
| 60 | + */ |
| 61 | + private static final int MAX_ENTRY_AGE_IN_SECONDS = 60; |
| 62 | + |
| 63 | + /* |
| 64 | + * Example data to encrypt |
| 65 | + */ |
| 66 | + private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 67 | + |
| 68 | + public static void main(final String[] args) { |
| 69 | + encryptWithCaching(AwsKmsCmkId.fromString(args[0])); |
| 70 | + } |
| 71 | + |
| 72 | + static byte[] encryptWithCaching(AwsKmsCmkId kmsCmkArn) { |
| 73 | + |
| 74 | + // Instantiate the SDK |
| 75 | + final AwsCrypto crypto = new AwsCrypto(); |
| 76 | + |
| 77 | + // Create an encryption context |
| 78 | + final Map<String, String> encryptionContext = Collections.singletonMap("purpose", "test"); |
| 79 | + |
| 80 | + // Create a keyring |
| 81 | + final Keyring keyring = StandardKeyrings.awsKms(kmsCmkArn); |
| 82 | + |
| 83 | + // Create a cache |
| 84 | + final CryptoMaterialsCache cache = new LocalCryptoMaterialsCache(CAPACITY); |
| 85 | + |
| 86 | + // Create a caching CMM |
| 87 | + final CryptoMaterialsManager cachingCmm = |
| 88 | + CachingCryptoMaterialsManager.newBuilder() |
| 89 | + .withKeyring(keyring) |
| 90 | + .withCache(cache) |
| 91 | + .withMaxAge(MAX_ENTRY_AGE_IN_SECONDS, TimeUnit.SECONDS) |
| 92 | + .withMessageUseLimit(MAX_ENTRY_MESSAGES) |
| 93 | + .build(); |
| 94 | + |
| 95 | + // When the call to encrypt specifies a caching CMM, |
| 96 | + // the encryption operation uses the data key cache |
| 97 | + return crypto.encrypt(EncryptRequest.builder() |
| 98 | + .cryptoMaterialsManager(cachingCmm) |
| 99 | + .plaintext(EXAMPLE_DATA) |
| 100 | + .encryptionContext(encryptionContext) |
| 101 | + .build()).getResult(); |
| 102 | + } |
| 103 | +} |
0 commit comments