Skip to content

Commit c7450c5

Browse files
seebeestexastony
andauthored
feat: Update overrideEncryptionContextTableName (#1112)
The names in `overrideEncryptionContextTableName` are relative to the code and not the customer using the code. This updates these names and docs to reflect the intuition customers have when dealing with two different table names. --------- Co-authored-by: texastony <[email protected]>
1 parent 0dc4898 commit c7450c5

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

examples/src/main/java/com/amazonaws/examples/EncryptionContextOverridesWithDynamoDBMapper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
* "partition_attribute" for Strings and a sort (range) key named "sort_attribute" for numbers.
4747
*/
4848
public class EncryptionContextOverridesWithDynamoDBMapper {
49-
public static final String TABLE_NAME_TO_OVERRIDE = "ExampleTableForEncryptionContextOverrides";
49+
public static final String ORIGINAL_TABLE_NAME_TO_OVERRIDE =
50+
"ExampleTableForEncryptionContextOverrides";
5051
public static final String PARTITION_ATTRIBUTE = "partition_attribute";
5152
public static final String SORT_ATTRIBUTE = "sort_attribute";
5253

@@ -78,7 +79,7 @@ public static void main(String[] args) throws GeneralSecurityException {
7879

7980
public static void encryptRecord(
8081
final String cmkArn,
81-
final String newEncryptionContextTableName,
82+
final String currentTableName,
8283
AmazonDynamoDB ddbClient,
8384
AWSKMS kmsClient)
8485
throws GeneralSecurityException {
@@ -95,7 +96,7 @@ public static void encryptRecord(
9596
final DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(cmp);
9697

9798
Map<String, String> tableNameEncryptionContextOverrides = new HashMap<>();
98-
tableNameEncryptionContextOverrides.put(TABLE_NAME_TO_OVERRIDE, newEncryptionContextTableName);
99+
tableNameEncryptionContextOverrides.put(ORIGINAL_TABLE_NAME_TO_OVERRIDE, currentTableName);
99100
tableNameEncryptionContextOverrides.put(
100101
"AnotherExampleTableForEncryptionContextOverrides", "this table doesn't exist");
101102

@@ -133,7 +134,7 @@ public static void encryptRecord(
133134
final EnumSet<EncryptionFlags> encryptAndSign =
134135
EnumSet.of(EncryptionFlags.ENCRYPT, EncryptionFlags.SIGN);
135136
final Map<String, AttributeValue> encryptedItem =
136-
ddbClient.getItem(TABLE_NAME_TO_OVERRIDE, itemKey).getItem();
137+
ddbClient.getItem(ORIGINAL_TABLE_NAME_TO_OVERRIDE, itemKey).getItem();
137138
System.out.println("Encrypted Record: " + encryptedItem);
138139

139140
Map<String, Set<EncryptionFlags>> encryptionFlags = new HashMap<>();
@@ -151,19 +152,19 @@ public static void encryptRecord(
151152
new EncryptionContext.Builder()
152153
.withHashKeyName(PARTITION_ATTRIBUTE)
153154
.withRangeKeyName(SORT_ATTRIBUTE)
154-
.withTableName(newEncryptionContextTableName)
155+
.withTableName(currentTableName)
155156
.build());
156157
System.out.printf(
157158
"The example item was encrypted using the table name '%s' in the EncryptionContext%n",
158-
newEncryptionContextTableName);
159+
currentTableName);
159160

160161
// The decrypted field matches the original field before encryption
161162
assert record
162163
.getExample()
163164
.equals(decrypted_without_override_record.get(STRING_FIELD_NAME).getS());
164165
}
165166

166-
@DynamoDBTable(tableName = TABLE_NAME_TO_OVERRIDE)
167+
@DynamoDBTable(tableName = ORIGINAL_TABLE_NAME_TO_OVERRIDE)
167168
public static final class ExampleItem {
168169
private String partitionAttribute;
169170
private int sortAttribute;

examples/src/test/java/com/amazonaws/examples/EncryptionContextOverridesWithDynamoDBMapperIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
package com.amazonaws.examples;
55

6+
import static com.amazonaws.examples.EncryptionContextOverridesWithDynamoDBMapper.ORIGINAL_TABLE_NAME_TO_OVERRIDE;
67
import static com.amazonaws.examples.EncryptionContextOverridesWithDynamoDBMapper.PARTITION_ATTRIBUTE;
78
import static com.amazonaws.examples.EncryptionContextOverridesWithDynamoDBMapper.SORT_ATTRIBUTE;
8-
import static com.amazonaws.examples.EncryptionContextOverridesWithDynamoDBMapper.TABLE_NAME_TO_OVERRIDE;
99
import static com.amazonaws.examples.TestUtils.US_WEST_2;
1010
import static com.amazonaws.examples.TestUtils.US_WEST_2_KEY_ID;
1111
import static com.amazonaws.examples.TestUtils.createDDBTable;
@@ -26,7 +26,7 @@ public void testEncryptAndDecrypt() throws GeneralSecurityException {
2626
final AmazonDynamoDB ddb = DynamoDBEmbedded.create();
2727

2828
// Create the table under test
29-
createDDBTable(ddb, TABLE_NAME_TO_OVERRIDE, PARTITION_ATTRIBUTE, SORT_ATTRIBUTE);
29+
createDDBTable(ddb, ORIGINAL_TABLE_NAME_TO_OVERRIDE, PARTITION_ATTRIBUTE, SORT_ATTRIBUTE);
3030

3131
EncryptionContextOverridesWithDynamoDBMapper.encryptRecord(
3232
US_WEST_2_KEY_ID, OVERRIDE_TABLE_NAME, ddb, kms);

sdk1/src/main/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/utils/EncryptionContextOperators.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,32 @@ private EncryptionContextOperators() {}
2626

2727
/**
2828
* An operator for overriding EncryptionContext's table name for a specific DynamoDBEncryptor. If
29-
* any table names or the encryption context itself is null, then it returns the original
30-
* EncryptionContext.
29+
* any table names or the encryption context is null, it returns the original EncryptionContext.
3130
*
32-
* @param originalTableName the name of the table that should be overridden in the Encryption
33-
* Context
34-
* @param newTableName the table name that should be used in the Encryption Context
31+
* <p>The client automatically adds the current table name to the encryption context so it's bound
32+
* to the ciphertext. Use this method when the encryption context of encrypted table items
33+
* includes a different table name, such as when a table is backed up, or table items are
34+
* moved/copied to a different table. If you don't override the name of the current table with the
35+
* table name in the encryption context, decrypt fails. This override affects the encryption
36+
* context of all table items, including newly encrypted items.
37+
*
38+
* @param originalTableName Use this table name in the encryption context
39+
* @param currentTableName Override this table name in the encryption context
3540
* @return A UnaryOperator that produces a new EncryptionContext with the supplied table name
3641
*/
3742
public static UnaryOperator<EncryptionContext> overrideEncryptionContextTableName(
38-
String originalTableName, String newTableName) {
43+
String originalTableName, String currentTableName) {
3944
return encryptionContext -> {
4045
if (encryptionContext == null
4146
|| encryptionContext.getTableName() == null
4247
|| originalTableName == null
43-
|| newTableName == null) {
48+
|| currentTableName == null) {
4449
return encryptionContext;
4550
}
4651
if (originalTableName.equals(encryptionContext.getTableName())) {
47-
return new EncryptionContext.Builder(encryptionContext).withTableName(newTableName).build();
52+
return new EncryptionContext.Builder(encryptionContext)
53+
.withTableName(currentTableName)
54+
.build();
4855
} else {
4956
return encryptionContext;
5057
}

0 commit comments

Comments
 (0)