From: sophia-bq (@sophia-bq) <noreply+sophia-bq@github.com>
To: aws/aws-advanced-jdbc-wrapper <noreply+aws-aws-advanced-jdbc-wrapper@github.com>
Subject: [aws/aws-advanced-jdbc-wrapper] PR #2263: fix: make rotateDataKey() refuse columns with data
Date: Thu, 19 Nov 2026 23:00:48 +0000
Message-ID: <gh-aws-aws-advanced-jdbc-wrapper-2263@github.com> (raw)
### Summary
<!--- General summary / title -->
### Description
<!--- Details of what you changed -->
### Additional Reviewers
<!-- Any additional reviewers -->
### By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
inline patch 1: docs/using-the-jdbc-driver/using-plugins/UsingTheKmsEncryptionPlugin.md, wrapper/src/main/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtility.java, wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties and 5 more (+273/-47 lines) download inline-1.patchdiff --git a/docs/using-the-jdbc-driver/using-plugins/UsingTheKmsEncryptionPlugin.md b/docs/using-the-jdbc-driver/using-plugins/UsingTheKmsEncryptionPlugin.mdindex 75fee5f1b..2c0e038de 100644--- a/docs/using-the-jdbc-driver/using-plugins/UsingTheKmsEncryptionPlugin.md+++ b/docs/using-the-jdbc-driver/using-plugins/UsingTheKmsEncryptionPlugin.md@@ -685,19 +685,21 @@ Requirements:
- The HMAC triggers on both PostgreSQL and MySQL look up the key from the metadata for every row and ignore any session setting, so nothing needs priming on either database.
> [!WARNING]
-> `KeyManagementUtility.rotateDataKey()` does **not** re-encrypt existing rows. It only stores a new data key and points the column's metadata at it. After the call:+> `KeyManagementUtility.rotateDataKey()` does **not** re-encrypt existing rows. It only stores a new data key and points the column's metadata at it, so it is only for a column that holds no data. Before it changes anything it runs `SELECT 1 FROM <table> WHERE <column> IS NOT NULL LIMIT 1`. If a row comes back, it throws a `KeyManagementException` that points to `rotateDataKeyAndReencrypt()`, and the column keeps its key. On a column with no non-null value it works as before.+>+> A column that a release without this check already rotated has the following symptoms:
> - Every row encrypted under the previous data key fails to decrypt through the plugin with `HMAC verification failed`.
> - Running applications keep using the previous key until their metadata cache refreshes (`metadataCacheRefreshIntervalMs`, 5 minutes by default). With the HMAC trigger, which every encrypted column requires, their writes to the column are rejected until then.
> - The data is recoverable only while the previous `key_storage` row and its KMS key exist. Do not delete them.
>
-> Use `rotateDataKey()` only for a column that holds no data. If you already called it on a column with data, see [Finishing or repairing a re-encryption](#finishing-or-repairing-a-re-encryption).+> To recover such a column, see [Finishing or repairing a re-encryption](#finishing-or-repairing-a-re-encryption).
### Finishing or repairing a re-encryption
`KeyManagementUtility.reencryptRemainingRows(table, column, primaryKeyColumn, oldKeyIds, batchSize)` re-encrypts values that are still under one of the given old key ids into the column's current key. It uses the same batching and HMAC check as the rotation and is safe to run again: values already under the current key are skipped. It requires `allowedMasterKeyArns` on the `EncryptionConfig` and throws an `IllegalArgumentException` without it: the values are re-encrypted into the column's current data key, and the master key of that key is read from `key_storage`, so it must be checked against the list. Use it:
- To resume a failed rotation. Committed batches are kept; the exception message names the old key id to pass.
-- To recover a column after `rotateDataKey()` was called on it. Find the previous key id:+- To recover a column that an earlier release's `rotateDataKey()` was called on while it held data. Find the previous key id:
```sql
-- Data keys created for the column, oldest first
@@ -1005,7 +1007,7 @@ Statements on tables without encrypted columns are never rejected. If you encoun
5. **`Refusing to use master key arn:aws:kms:***:***:key/<key id>: it is not listed in encryptionAllowedMasterKeyArns`** (SQL state `KEY07`): The column's `key_storage` row names a master key that is not on the list. The message masks the key's region and account; the full value is in `key_storage.master_key_arn`. If the key is yours, add it to the list in exactly the form stored in `key_storage.master_key_arn`. If it is not, someone changed the row: investigate before you change anything.
6. **`HMAC verification failed`** (driver, on read):
- Data may have been tampered with, or the HMAC key in `encrypt.key_storage.hmac_key` changed
- - The column's data key was rotated without re-encrypting existing rows (for example with `rotateDataKey()`), or a re-encryption has not finished. See [Finishing or repairing a re-encryption](#finishing-or-repairing-a-re-encryption)+ - The column's data key was rotated without re-encrypting existing rows (for example with `rotateDataKey()` in a release before it refused columns with data), or a re-encryption has not finished. See [Finishing or repairing a re-encryption](#finishing-or-repairing-a-re-encryption)
7. **`HMAC verification failed for column ...` or `Invalid encrypted data length for column ...`** (PostgreSQL trigger), **`HMAC verification failed (plaintext or tampered value)`** (MySQL trigger): The trigger rejected a plaintext or tampered value. This is expected when a value reaches the database unencrypted. Write the column through a plugin connection with a statement the plugin maps (see [Limitations](#limitations)), or use an annotation: `/*@encrypt:table.column*/`. Annotations do not bypass the parameter-mapping check described in entry 9.
- The current HMAC triggers on both PostgreSQL and MySQL cache nothing — they look up the key from the metadata per row — so pooled sessions pick up a rotated key on their own. With a routine created by an earlier (caching) version, re-create it (see step 6 of the [Data key rotation procedure](#data-key-rotation-procedure)) or recycle the connection
8. **`No HMAC key found for ...`** (PostgreSQL trigger), **`No HMAC key is configured for the encrypted column`** (MySQL trigger): The trigger found no row for the column in `encryption_metadata`. Check that the column is registered, and that the metadata schema written into the script in [step 3](#3-install-the-encrypted-data-type-and-hmac-functions) is the one that holds the metadata.
diff --git a/wrapper/src/main/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtility.java b/wrapper/src/main/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtility.javaindex 2f2b2b1c4..2a5ab30b6 100644--- a/wrapper/src/main/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtility.java+++ b/wrapper/src/main/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtility.java@@ -240,6 +240,12 @@ private String getUpdateEncryptedValueSql(ColumnTarget target) {
+ " WHERE " + quoteIdentifier(target.primaryKeyColumn) + " = ? AND " + column + " = ?";
}
+ /** Finds out whether the column holds any value. The target's identifiers are validated. */+ private String getSelectAnyValueSql(ColumnTarget target) {+ return "SELECT 1 FROM " + quoteTableName(target)+ + " WHERE " + quoteIdentifier(target.column) + " IS NOT NULL LIMIT 1";+ }+
/** Reads no rows; fails if the table, the column or the primary key column does not exist. */
private String getProbeSql(ColumnTarget target) {
return "SELECT " + quoteIdentifier(target.primaryKeyColumn) + ", " + quoteIdentifier(target.column)
@@ -393,26 +399,33 @@ public void generateAndStoreDataKey(
* holds data encrypted under it. Without them that data cannot be recovered.
* </ul>
*
- * <p>This method is safe only for a column that holds no encrypted data. To rotate the data key- * of a column that holds data, use {@link #rotateDataKeyAndReencrypt(String, String, String,- * String, int)}. If this method was already called on a column with data, re-encrypt the- * remaining rows with {@link #reencryptRemainingRows(String, String, String, List, int)}, passing- * the previous key id. To rotate the KMS key instead, enable KMS automatic key rotation; it is- * transparent to the plugin.+ * <p>This method is safe only for a column that holds no data, so it refuses to run on any other+ * column: it throws before changing anything when the column has a non-null value. To rotate the+ * data key of a column that holds data, use {@link #rotateDataKeyAndReencrypt(String, String, String,+ * String, int)}. If a previous release's version of this method was already called on a column with+ * data, re-encrypt the remaining rows with+ * {@link #reencryptRemainingRows(String, String, String, List, int)}, passing the previous key id.+ * To rotate the KMS key instead, enable KMS automatic key rotation; it is transparent to the+ * plugin.
*
* @param tableName Name of the table
* @param columnName Name of the column
* @param newMasterKeyArn The master key to wrap the new data key with, or null to keep the
* current one (requires {@code encryptionAllowedMasterKeyArns})
- * @throws KeyManagementException if the master key is not allowed, or key rotation fails- * @throws IllegalArgumentException if no master key is given and no allow-list is configured+ * @throws KeyManagementException if the column holds data, the master key is not allowed, or key+ * rotation fails+ * @throws IllegalArgumentException if no master key is given and no allow-list is configured, or+ * a table or column name is not a valid identifier
*/
public void rotateDataKey(String tableName, String columnName, @Nullable String newMasterKeyArn)
throws KeyManagementException {
Objects.requireNonNull(tableName, "Table name cannot be null");
Objects.requireNonNull(columnName, "Column name cannot be null");
+ validateTableAndColumn(tableName, columnName);
requireTargetMasterKey(newMasterKeyArn);
+ requireEmptyColumn(tableName, columnName);+
LOGGER.info(() -> Messages.get("KeyManagementUtility.rotatingKey", new Object[]{tableName, columnName}));
try {
@@ -967,6 +980,34 @@ private void precheckRowsUnderKey(
return null;
}
+ /**+ * Throws if the column holds a non-null value, before anything is changed. Switching the data+ * key does not re-encrypt rows, so it would make every existing value unreadable.+ */+ private void requireEmptyColumn(String tableName, String columnName) throws KeyManagementException {+ Connection conn = null;+ try {+ conn = getConnection();+ final CurrentColumnKey current = loadCurrentKey(conn, tableName, columnName);+ final ColumnTarget target =+ new ColumnTarget(schemaPrefix(tableName), current.storedTable, current.storedColumn, "");+ try (PreparedStatement stmt = conn.prepareStatement(getSelectAnyValueSql(target));+ ResultSet rs = stmt.executeQuery()) {+ if (rs.next()) {+ throw new KeyManagementException(+ Messages.get("KeyManagementUtility.rotateColumnHasData", new Object[]{tableName, columnName}));+ }+ }+ } catch (KeyManagementException e) {+ throw e;+ } catch (SQLException e) {+ throw new KeyManagementException(+ Messages.get("KeyManagementUtility.rotateDataKeyFailed", new Object[]{e.getMessage(), e}));+ } finally {+ closeConnectionQuietly(conn);+ }+ }+
/**
* Reads the column's current key, algorithm and stored names from the database, bypassing the
* metadata cache. The metadata is looked up with the bare table name, as the plugin stores it.
@@ -1061,6 +1102,22 @@ private static void validateReencryptionArguments(
Objects.requireNonNull(columnName, Messages.get("KeyManagementUtility.columnNameNull"));
Objects.requireNonNull(primaryKeyColumn, Messages.get("KeyManagementUtility.primaryKeyColumnNull"));
+ validateTableAndColumn(tableName, columnName);+ if (!isValidIdentifier(primaryKeyColumn)) {+ throw new IllegalArgumentException(+ Messages.get("KeyManagementUtility.invalidPrimaryKeyColumn", new Object[]{primaryKeyColumn}));+ }+ if (primaryKeyColumn.equalsIgnoreCase(columnName)) {+ throw new IllegalArgumentException(+ Messages.get("KeyManagementUtility.primaryKeySameAsColumn", new Object[]{primaryKeyColumn}));+ }+ if (batchSize < 1 || batchSize > MAX_REENCRYPTION_BATCH_SIZE) {+ throw new IllegalArgumentException(Messages.get("KeyManagementUtility.invalidBatchSize",+ new Object[]{String.valueOf(MAX_REENCRYPTION_BATCH_SIZE), String.valueOf(batchSize)}));+ }+ }++ private static void validateTableAndColumn(String tableName, String columnName) {
final String[] tableParts = tableName.split("\\.", -1);
boolean validTable = tableParts.length <= 2;
for (String part : tableParts) {
@@ -1074,18 +1131,6 @@ private static void validateReencryptionArguments(
throw new IllegalArgumentException(
Messages.get("KeyManagementUtility.invalidColumnName", new Object[]{columnName}));
}
- if (!isValidIdentifier(primaryKeyColumn)) {- throw new IllegalArgumentException(- Messages.get("KeyManagementUtility.invalidPrimaryKeyColumn", new Object[]{primaryKeyColumn}));- }- if (primaryKeyColumn.equalsIgnoreCase(columnName)) {- throw new IllegalArgumentException(- Messages.get("KeyManagementUtility.primaryKeySameAsColumn", new Object[]{primaryKeyColumn}));- }- if (batchSize < 1 || batchSize > MAX_REENCRYPTION_BATCH_SIZE) {- throw new IllegalArgumentException(Messages.get("KeyManagementUtility.invalidBatchSize",- new Object[]{String.valueOf(MAX_REENCRYPTION_BATCH_SIZE), String.valueOf(batchSize)}));- }
}
/** The {@link SchemaName} identifier rule, plus a 64-character cap. */
diff --git a/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties b/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.propertiesindex 683a3a067..66f8c28a0 100644--- a/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties+++ b/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties@@ -885,6 +885,7 @@ KeyManagementUtility.createMasterKeyFailed=Failed to create KMS master key: {0}
KeyManagementUtility.generateDataKeyFailed=Failed to generate and store data key: {0}
KeyManagementUtility.noEncryptionConfigExc=No encryption configuration found for {0}
KeyManagementUtility.rotateDataKeyFailed=Failed to rotate data key: {0}
+KeyManagementUtility.rotateColumnHasData=Cannot rotate the data key of {0}.{1} with rotateDataKey: the column holds data, and rotateDataKey does not re-encrypt it, so every existing value would become unreadable. Nothing was changed. Use rotateDataKeyAndReencrypt instead.
KeyManagementUtility.checkEncryptionStatusFailed=Failed to check existing encryption status
KeyManagementUtility.refreshMetadataFailed=Failed to refresh metadata: {0}
KeyManagementUtility.removeEncryptionConfigFailed=Failed to remove encryption configuration: {0}
diff --git a/wrapper/src/test/java/integration/container/tests/KmsKeyRotationIntegrationTest.java b/wrapper/src/test/java/integration/container/tests/KmsKeyRotationIntegrationTest.javaindex f87dcf810..274df5647 100644--- a/wrapper/src/test/java/integration/container/tests/KmsKeyRotationIntegrationTest.java+++ b/wrapper/src/test/java/integration/container/tests/KmsKeyRotationIntegrationTest.java@@ -215,7 +215,10 @@ void cleanUp() throws SQLException {
tables.clear();
}
- /** Documents the reported behavior: the legacy method switches the key without re-encrypting. */+ /**+ * Documents what a key switch without re-encryption does to existing rows. The state is built+ * directly, because {@code rotateDataKey} no longer runs on a column with data.+ */
@TestTemplate
void test01LegacyRotateDataKeyLeavesOldRowsUndecryptable() throws Exception {
final String table = createEncryptedTable("legacy", SSN);
@@ -228,7 +231,7 @@ void test01LegacyRotateDataKeyLeavesOldRowsUndecryptable() throws Exception {
assertEquals(5, rawBefore.size());
try (Connection plain = KmsTestUtility.openPlainConnection()) {
- newUtility(plain).rotateDataKey(table, SSN, null);+ switchKeyWithoutReencrypting(plain, table, SSN);
}
final int newKeyId = currentKeyId(table, SSN);
@@ -313,7 +316,7 @@ void test02RotateAndReencryptOverSeveralBatches() throws Exception {
}
}
- /** Recovery of a column after the legacy method was used on it. */+ /** Recovery of a column whose key was switched without re-encrypting its rows. */
@TestTemplate
void test03ReencryptRemainingRowsRecoversAfterLegacyRotation() throws Exception {
final String table = createEncryptedTable("recover", SSN);
@@ -323,7 +326,7 @@ void test03ReencryptRemainingRowsRecoversAfterLegacyRotation() throws Exception
}
final int oldKeyId = currentKeyId(table, SSN);
try (Connection plain = KmsTestUtility.openPlainConnection()) {
- newUtility(plain).rotateDataKey(table, SSN, null);+ switchKeyWithoutReencrypting(plain, table, SSN);
}
final int newKeyId = currentKeyId(table, SSN);
assertNotEquals(oldKeyId, newKeyId);
@@ -666,8 +669,8 @@ void test11MysqlTriggerLooksUpKeyPerRow() throws Exception {
* UPDATEs to rows a rotation has not converted yet. A data key rotation re-encrypts rows in
* batches; while it runs, rows it has not reached still carry the previous key's HMAC tag. A
* single-threaded test cannot interrupt {@link KeyManagementUtility#rotateDataKeyAndReencrypt}
- * mid-batch, so the not-yet-converted state is produced with its first phase,- * {@link KeyManagementUtility#rotateDataKey} (the key is switched; re-encryption has not run), and+ * mid-batch, so the not-yet-converted state is produced with its first phase, the key switch+ * (the key is switched; re-encryption has not run), and
* {@link KeyManagementUtility#reencryptRemainingRows} finishes the rotation at the end. With the
* HMAC trigger installed, an UPDATE that leaves a not-yet-converted row's encrypted value unchanged
* is accepted, a change to a value under the current key is accepted, and a change to a value under
@@ -686,7 +689,7 @@ void test12UpdatesToRowsNotYetConvertedDuringRotation() throws Exception {
// Switch the data key without re-encrypting: rows 1..3 keep the previous key's HMAC tag.
try (Connection plain = KmsTestUtility.openPlainConnection()) {
- newUtility(plain).rotateDataKey(table, SSN, null);+ switchKeyWithoutReencrypting(plain, table, SSN);
}
assertNotEquals(oldKeyId, currentKeyId(table, SSN));
@@ -719,6 +722,62 @@ void test12UpdatesToRowsNotYetConvertedDuringRotation() throws Exception {
assertTrue(result.isComplete());
}
+ /** {@code rotateDataKey} refuses a column with data, before it changes anything. */+ @TestTemplate+ void test13RotateDataKeyRefusesColumnWithData() throws Exception {+ final String table = createEncryptedTable("refuse", SSN);+ installHmacTriggers(table, SSN);+ try (Connection writer = openPluginConnection()) {+ insertRows(writer, table, 1, 3, false);+ }+ final int keyId = currentKeyId(table, SSN);+ final Map<Integer, byte[]> rawBefore = readRawValues(table, SSN);+ final int keyRows = keyRowCount(table);++ try (Connection plain = KmsTestUtility.openPlainConnection()) {+ final KeyManagementException error = assertThrows(KeyManagementException.class,+ () -> newUtility(plain).rotateDataKey(table, SSN, null));+ assertTrue(error.getMessage().contains("rotateDataKeyAndReencrypt"), error.getMessage());+ }++ assertEquals(keyId, currentKeyId(table, SSN));+ assertEquals(keyRows, keyRowCount(table));+ assertRawValuesUnchanged(rawBefore, readRawValues(table, SSN));+ assertAllRowsDecrypt(table, 3, false);+ }++ /** {@code rotateDataKey} still works on an empty column, including one holding only NULLs. */+ @TestTemplate+ void test14RotateDataKeyWorksOnEmptyColumn() throws Exception {+ final String table = createEncryptedTable("emptycol", SSN);+ installHmacTriggers(table, SSN);+ final int oldKeyId = currentKeyId(table, SSN);++ try (Connection plain = KmsTestUtility.openPlainConnection()) {+ newUtility(plain).rotateDataKey(table, SSN, null);+ }+ final int newKeyId = currentKeyId(table, SSN);+ assertNotEquals(oldKeyId, newKeyId);++ try (Connection writer = openPluginConnection()) {+ try (PreparedStatement stmt = writer.prepareStatement(+ "INSERT INTO " + table + " (name, ssn) VALUES (?, ?)")) {+ stmt.setString(1, "row-null");+ stmt.setNull(2, Types.BINARY);+ assertEquals(1, stmt.executeUpdate());+ }+ }+ try (Connection plain = KmsTestUtility.openPlainConnection()) {+ newUtility(plain).rotateDataKey(table, SSN, null);+ }+ assertNotEquals(newKeyId, currentKeyId(table, SSN));++ try (Connection writer = openPluginConnection()) {+ insertRows(writer, table, 1, 2, false);+ }+ assertAllRowsDecrypt(table, 2, false);+ }+
// ---------------------------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------------------------
@@ -739,6 +798,15 @@ private void installHmacTriggers(final String table, final String... encryptedCo
+ " user needs the create-routine privilege.");
}
+ /**+ * The state the old {@code rotateDataKey} left on a column with data: a new key row, the metadata+ * pointing at it and the rows untouched. {@code rotateDataKey} now refuses such a column, so the+ * state is built directly.+ */+ private void switchKeyWithoutReencrypting(Connection conn, String table, String column) throws SQLException {+ KmsTestUtility.switchColumnKeyWithoutReencrypting(conn, SchemaName.of(METADATA_SCHEMA), isPg, table, column);+ }+
private Connection openWrapperConnection(String plugins) throws SQLException {
final Properties props = ConnectionStringHelper.getDefaultProperties();
props.setProperty(PropertyDefinition.PLUGINS.name, plugins);
diff --git a/wrapper/src/test/java/integration/container/tests/KmsTriggerIntegrationTest.java b/wrapper/src/test/java/integration/container/tests/KmsTriggerIntegrationTest.javaindex 4baa12e8a..0cef9b12c 100644--- a/wrapper/src/test/java/integration/container/tests/KmsTriggerIntegrationTest.java+++ b/wrapper/src/test/java/integration/container/tests/KmsTriggerIntegrationTest.java@@ -507,7 +507,9 @@ private byte[] encryptedPayload(final Object value) throws EncryptionException {
private void rotateDataKey() throws SQLException {
try (Connection conn = KmsTestUtility.openPlainConnection()) {
- newUtility(conn).rotateDataKey(TABLE, COLUMN, null);+ // rotateDataKey refuses a column with data, so switch the key directly.+ KmsTestUtility.switchColumnKeyWithoutReencrypting(+ conn, SchemaName.of(METADATA_SCHEMA), isPg, TABLE, COLUMN);
}
}
diff --git a/wrapper/src/test/java/integration/util/KmsTestUtility.java b/wrapper/src/test/java/integration/util/KmsTestUtility.javaindex 05297ce44..45699ca69 100644--- a/wrapper/src/test/java/integration/util/KmsTestUtility.java+++ b/wrapper/src/test/java/integration/util/KmsTestUtility.java@@ -342,6 +342,73 @@ public static ColumnKey configureColumnWithHmacKey(
return new ColumnKey(keyId, hmacKey, dataKey);
}
+ /**+ * Leaves a column in the state a data key switch without re-encryption produces: inserts a new+ * {@code key_storage} row (same KMS master key and wrapped data key, new random HMAC key) and+ * repoints the column's {@code encryption_metadata} at it. Rows already written stay under the+ * previous key, so reading them through the plugin fails the HMAC check.+ *+ * @return the id of the new key row+ */+ public static int switchColumnKeyWithoutReencrypting(+ final Connection conn, final SchemaName metadataSchema, final boolean isPg,+ final String table, final String column) throws SQLException {+ final String prefix = metadataSchema.getValue() + ".";+ final String name;+ final String masterKeyArn;+ final String encryptedDataKey;+ try (PreparedStatement stmt = conn.prepareStatement(+ "SELECT ks.name, ks.master_key_arn, ks.encrypted_data_key FROM " + prefix + "encryption_metadata em "+ + "JOIN " + prefix + "key_storage ks ON em.key_id = ks.id "+ + "WHERE em.table_name = ? AND em.column_name = ?")) {+ stmt.setString(1, table);+ stmt.setString(2, column);+ try (ResultSet rs = stmt.executeQuery()) {+ if (!rs.next()) {+ throw new SQLException("No encryption metadata for " + table + "." + column);+ }+ name = rs.getString(1) + "-switched-" + System.nanoTime();+ masterKeyArn = rs.getString(2);+ encryptedDataKey = rs.getString(3);+ }+ }++ final byte[] hmacKey = new byte[32];+ new SecureRandom().nextBytes(hmacKey);+ final int newKeyId;+ try (PreparedStatement stmt = conn.prepareStatement(+ "INSERT INTO " + prefix + "key_storage (name, master_key_arn, encrypted_data_key, hmac_key, key_spec) "+ + "VALUES (?, ?, ?, ?, ?)" + (isPg ? " RETURNING id" : ""),+ isPg ? Statement.NO_GENERATED_KEYS : Statement.RETURN_GENERATED_KEYS)) {+ stmt.setString(1, name);+ stmt.setString(2, masterKeyArn);+ stmt.setString(3, encryptedDataKey);+ stmt.setBytes(4, hmacKey);+ stmt.setString(5, "AES_256");+ if (isPg) {+ try (ResultSet rs = stmt.executeQuery()) {+ rs.next();+ newKeyId = rs.getInt(1);+ }+ } else {+ stmt.executeUpdate();+ try (ResultSet rs = stmt.getGeneratedKeys()) {+ rs.next();+ newKeyId = rs.getInt(1);+ }+ }+ }++ try (PreparedStatement stmt = conn.prepareStatement(+ "UPDATE " + prefix + "encryption_metadata SET key_id = ? WHERE table_name = ? AND column_name = ?")) {+ stmt.setInt(1, newKeyId);+ stmt.setString(2, table);+ stmt.setString(3, column);+ stmt.executeUpdate();+ }+ return newKeyId;+ }+
/**
* Creates an application table with an {@code id} primary key, a {@code name} column, the given
* encrypted columns ({@code encrypted_data} on PostgreSQL, {@code VARBINARY(256)} on MySQL) and any
diff --git a/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/FakeReencryptionDatabase.java b/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/FakeReencryptionDatabase.javaindex 174e72c60..f223701dd 100644--- a/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/FakeReencryptionDatabase.java+++ b/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/FakeReencryptionDatabase.java@@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Predicate;
@@ -404,6 +405,11 @@ private ResultSet executeQuery(String sql, Map<Integer, Object> params) throws S
}
return resultSet(new String[] {"pk", "value"}, Collections.emptyList());
}
+ if (sql.startsWith("SELECT 1 FROM") && sql.endsWith(" IS NOT NULL LIMIT 1")) {+ final boolean hasValue = rows.values().stream().anyMatch(Objects::nonNull);+ return resultSet(new String[] {"1"},+ hasValue ? Collections.singletonList(new Object[] {1}) : Collections.emptyList());+ }
if (sql.startsWith("SELECT ") && sql.contains(" LIMIT ?")) {
final boolean hasLastPk = sql.contains(" > ?");
final long lastPk = hasLastPk ? ((Number) params.get(1)).longValue() : Long.MIN_VALUE;
diff --git a/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtilityReencryptionTest.java b/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtilityReencryptionTest.javaindex 2c31d9dcc..9cdf64a80 100644--- a/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtilityReencryptionTest.java+++ b/wrapper/src/test/java/software/amazon/jdbc/plugin/encryption/key/KeyManagementUtilityReencryptionTest.java@@ -507,38 +507,74 @@ void generateAndStoreDataKey_reportsAnUnlistedMasterKey() {
verifyNoInteractions(kmsClient);
}
- // ---- 5. repair after the switch and after the legacy rotateDataKey --------------------------
@Test
- void repair_afterLegacyRotateDataKey_and_staleWriter() throws Exception {+ void rotateDataKey_refusesAColumnWithData_andChangesNothing() throws Exception {
init(false, TABLE);
final TestKey keyA = createKey("key-users-ssn-1");
db.setMetadataKeyId(keyA.id);
- for (long pk = 1; pk <= 4; pk++) {- db.putRow(pk, encrypt("v" + pk, keyA));- }+ db.putRow(1, encrypt("v1", keyA));
final Map<Long, byte[]> before = db.getRows();
- when(metadataManager.getColumnConfig(TABLE, COLUMN)).thenReturn(ColumnEncryptionConfig.builder()++ final KeyManagementException exception =+ assertThrows(KeyManagementException.class, () -> utility.rotateDataKey(TABLE, COLUMN, null));++ assertTrue(exception.getMessage().contains("rotateDataKeyAndReencrypt"), exception.getMessage());+ assertTrue(db.getExecutedSql().contains(+ "SELECT 1 FROM `users` WHERE `ssn` IS NOT NULL LIMIT 1"), db.getExecutedSql().toString());+ assertEquals(keyA.id, db.getMetadataKeyId());+ assertEquals(1, db.getKeyCount());+ assertRowsEqual(before, db.getRows());+ verifyNoInteractions(kmsClient);+ }++ private ColumnEncryptionConfig columnConfig(TestKey key) {+ return ColumnEncryptionConfig.builder()
.tableName(TABLE)
.columnName(COLUMN)
.algorithm(ALGORITHM)
- .keyId(keyA.id)+ .keyId(key.id)
.keyMetadata(KeyMetadata.builder()
- .keyId(keyA.id)+ .keyId(key.id)
.keyName("key-users-ssn-1")
.masterKeyArn(MASTER_KEY_ARN)
- .encryptedDataKey(keyA.encryptedDataKey)- .hmacKey(keyA.hmacKey)+ .encryptedDataKey(key.encryptedDataKey)+ .hmacKey(key.hmacKey)
.keySpec("AES_256")
.createdAt(Instant.now())
.lastUsedAt(Instant.now())
.build())
- .build());+ .build();+ }++ @Test+ void rotateDataKey_worksOnAnEmptyColumn() throws Exception {+ init(false, TABLE);+ final TestKey keyA = createKey("key-users-ssn-1");+ db.setMetadataKeyId(keyA.id);+ when(metadataManager.getColumnConfig(TABLE, COLUMN)).thenReturn(columnConfig(keyA));- // Legacy behavior (unchanged): new key row, metadata repointed, rows not touched.
utility.rotateDataKey(TABLE, COLUMN, null);
- final int keyBId = db.getMetadataKeyId();+ assertNotEquals(keyA.id, db.getMetadataKeyId());+ assertEquals(2, db.getKeyCount());+ }++ // ---- 5. repair after the switch and after the legacy rotateDataKey -------------------------++ @Test+ void repair_afterLegacyRotateDataKey_and_staleWriter() throws Exception {+ init(false, TABLE);+ final TestKey keyA = createKey("key-users-ssn-1");+ for (long pk = 1; pk <= 4; pk++) {+ db.putRow(pk, encrypt("v" + pk, keyA));+ }+ final Map<Long, byte[]> before = db.getRows();++ // The state a release that rotated a column with data left behind: a new key row, the metadata+ // repointed at it, and the rows not touched.+ final TestKey keyB = createKey("key-users-ssn-2");+ db.setMetadataKeyId(keyB.id);+ final int keyBId = keyB.id;
assertNotEquals(keyA.id, keyBId);
assertEquals(2, db.getKeyCount());
assertRowsEqual(before, db.getRows());
@@ -555,7 +591,6 @@ void repair_afterLegacyRotateDataKey_and_staleWriter() throws Exception {
assertEquals(6, result.getReencryptedCount());
assertEquals(0, result.getUnmatchedCount());
assertEquals(keyBId, result.getCurrentKeyId());
- final TestKey keyB = keyFromDb(keyBId);
for (long pk = 1; pk <= 6; pk++) {
assertDecryptsTo("v" + pk, db.getRow(pk), keyB);
}