40
40
public class LambdaDecryptAndWriteExample implements RequestHandler <KinesisEvent , Void > {
41
41
private static final long MAX_ENTRY_AGE_MILLISECONDS = 600000 ;
42
42
private static final int MAX_CACHE_ENTRIES = 100 ;
43
+
44
+ // For best caching performance in Lambda, we want our cache to be a static final field
45
+ // configured by environment variables.
46
+ // However, to make this example easier for people to experiment with, we also provide a non-static
47
+ // version with simpler configuration.
48
+ private static final CachingCryptoMaterialsManager CACHING_CRYPTO_MATERIALS_MANAGER ;
49
+ private static final String TABLE_NAME = System .getProperty ("TABLE_NAME" );
50
+
51
+ static {
52
+ final String cmkArn = System .getProperty ("CMK_ARN" );
53
+ CACHING_CRYPTO_MATERIALS_MANAGER = CachingCryptoMaterialsManager .newBuilder ()
54
+ .withKeyring (StandardKeyrings .awsKms (AwsKmsCmkId .fromString (cmkArn )))
55
+ .withCache (new LocalCryptoMaterialsCache (MAX_CACHE_ENTRIES ))
56
+ .withMaxAge (MAX_ENTRY_AGE_MILLISECONDS , TimeUnit .MILLISECONDS )
57
+ .build ();
58
+ }
59
+
43
60
private final CachingCryptoMaterialsManager cachingMaterialsManager_ ;
44
61
private final AwsCrypto crypto_ ;
45
62
private final Table table_ ;
46
63
64
+ /**
65
+ * No-argument constructor for use with Lambda.
66
+ *
67
+ * This is almost equivalent to calling {@link #LambdaDecryptAndWriteExample(String, String)} with
68
+ * {@code cmkArn = System.getProperty("CMK_ARN")}
69
+ * and
70
+ * {@code tableName = System.getProperty("TABLE_NAME")}
71
+ * respectively.
72
+ * The only difference is that this constructor will re-use the underlying cache across all instances
73
+ * for better cache performance.
74
+ *
75
+ * @see #LambdaDecryptAndWriteExample(String, String)
76
+ * @see #CACHING_CRYPTO_MATERIALS_MANAGER
77
+ * @see #TABLE_NAME
78
+ */
79
+ public LambdaDecryptAndWriteExample () {
80
+ this (CACHING_CRYPTO_MATERIALS_MANAGER , TABLE_NAME );
81
+ }
82
+
47
83
/**
48
84
* This code doesn't set the max bytes or max message security thresholds that are enforced
49
85
* only on data keys used for encryption.
@@ -52,15 +88,21 @@ public class LambdaDecryptAndWriteExample implements RequestHandler<KinesisEvent
52
88
* @param tableName The name of the DynamoDB table name that stores decrypted messages
53
89
*/
54
90
public LambdaDecryptAndWriteExample (final String cmkArn , final String tableName ) {
55
- cachingMaterialsManager_ = CachingCryptoMaterialsManager .newBuilder ()
91
+ this (
92
+ CachingCryptoMaterialsManager .newBuilder ()
56
93
.withKeyring (StandardKeyrings .awsKms (AwsKmsCmkId .fromString (cmkArn )))
57
94
.withCache (new LocalCryptoMaterialsCache (MAX_CACHE_ENTRIES ))
58
95
.withMaxAge (MAX_ENTRY_AGE_MILLISECONDS , TimeUnit .MILLISECONDS )
59
- .build ();
96
+ .build (),
97
+ tableName );
98
+ }
99
+
100
+ public LambdaDecryptAndWriteExample (CachingCryptoMaterialsManager cachingMatherialsManager , String tableName ) {
101
+ cachingMaterialsManager_ = cachingMatherialsManager ;
60
102
crypto_ = new AwsCrypto ();
61
103
table_ = new DynamoDB (AmazonDynamoDBClientBuilder .defaultClient ()).getTable (tableName );
62
104
}
63
-
105
+
64
106
/**
65
107
* Decrypts Kinesis events and writes the data to DynamoDB
66
108
*
0 commit comments