Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): honor customer-supplied KMS key for table SSE (#1923)
David Lopese313ab5b1dbc
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.javaindex 54c26275e2..38d973df01 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java@@ -25,6 +25,7 @@ public class DynamoDbJsonHandler {private static final String DEFAULT_ACCOUNT_ID = "000000000000";private static final String SSE_TYPE_KMS = "KMS";+ private static final Set<String> VALID_SSE_TYPES = Set.of("AES256", "KMS");private final DynamoDbService dynamoDbService;private final DynamoDbStreamService dynamoDbStreamService;@@ -186,6 +187,16 @@ public class DynamoDbJsonHandler {boolean deletionProtection = request.path("DeletionProtectionEnabled").asBoolean(false);+ // Parsed and validated before createTable below: that call and the stream-enable that+ // follows it both have side effects, so an invalid SSEType must fail before either runs+ // rather than after, or ValidationException would leave a real table (and stream) behind.+ JsonNode sseSpec = request.path("SSESpecification");+ boolean sseEnabled = !sseSpec.isMissingNode() && sseSpec.path("Enabled").asBoolean(false);+ String sseType = sseEnabled ? sseSpec.path("SSEType").asText(SSE_TYPE_KMS) : null;+ if (sseEnabled) {+ validateSseType(sseType);+ }+TableDefinition table = dynamoDbService.createTable(tableName, keySchema, attrDefs,readCapacity, writeCapacity, gsis, lsis, region);table.setTableStatus(initialStatus);@@ -232,11 +243,15 @@ public class DynamoDbJsonHandler {table.setStreamViewType(viewType);}- JsonNode sseSpec = request.path("SSESpecification");- if (!sseSpec.isMissingNode() && sseSpec.path("Enabled").asBoolean(false)) {+ if (sseEnabled) {table.setSseEnabled(true);- table.setSseType(SSE_TYPE_KMS);- table.setKmsMasterKeyArn(defaultKmsMasterKeyArn(region));+ table.setSseType(sseType);+ if (SSE_TYPE_KMS.equals(sseType)) {+ JsonNode kmsMasterKeyId = sseSpec.path("KMSMasterKeyId");+ table.setKmsMasterKeyArn(!kmsMasterKeyId.isMissingNode() && !kmsMasterKeyId.isNull()+ ? kmsMasterKeyId.asText()+ : defaultKmsMasterKeyArn(region));+ }}// Everything above mutates the table AFTER createTable stored it, and a persistent@@ -247,6 +262,15 @@ public class DynamoDbJsonHandler {return table;}+ private void validateSseType(String sseType) {+ if (!VALID_SSE_TYPES.contains(sseType)) {+ throw new AwsException("ValidationException",+ "1 validation error detected: Value '" + sseType+ + "' at 'sSESpecification.sSEType' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [AES256, KMS]", 400);+ }+ }+private Response handleDeleteTable(JsonNode request, String region) {String tableName = request.path("TableName").asText();TableDefinition table = dynamoDbService.describeTable(tableName, region);@@ -1349,6 +1373,18 @@ public class DynamoDbJsonHandler {tableName, addRegions, removeRegions, updateRegions, region);}+ // Parsed and validated up front, same as the replica check above: updateTable and the+ // mutations that follow it have side effects (persisted throughput/GSI changes, stream+ // enable/disable), so an invalid SSEType must fail before any of them run rather than+ // after, or ValidationException would leave a partially-applied update behind.+ JsonNode sseSpec = request.path("SSESpecification");+ boolean sseSpecified = !sseSpec.isMissingNode();+ boolean sseEnabled = sseSpecified && sseSpec.path("Enabled").asBoolean(false);+ String sseType = sseEnabled ? sseSpec.path("SSEType").asText(SSE_TYPE_KMS) : null;+ if (sseEnabled) {+ validateSseType(sseType);+ }+TableDefinition table = dynamoDbService.updateTable(tableName, readCapacity, writeCapacity,gsiCreates, gsiDeletes, newAttrDefs, region);@@ -1419,15 +1455,36 @@ public class DynamoDbJsonHandler {}}- // Same flush as CreateTable: the stream mutations above land after updateTable's write,- // so without this a retargeted view type is rebuilt from the stale persisted value on- // restart and records resume the old image shape.+ if (sseSpecified) {+ if (sseEnabled) {+ table.setSseEnabled(true);+ table.setSseType(sseType);+ if (SSE_TYPE_KMS.equals(sseType)) {+ JsonNode kmsMasterKeyId = sseSpec.path("KMSMasterKeyId");+ table.setKmsMasterKeyArn(!kmsMasterKeyId.isMissingNode() && !kmsMasterKeyId.isNull()+ ? kmsMasterKeyId.asText()+ : defaultKmsMasterKeyArn(region));+ } else {+ table.setKmsMasterKeyArn(null);+ }+ } else {+ table.setSseEnabled(false);+ table.setSseType(null);+ table.setKmsMasterKeyArn(null);+ }+ }++ // Same flush as CreateTable: the stream and SSE mutations above land after updateTable's+ // write, so without this a retargeted view type or SSE change is rebuilt from the stale+ // persisted value on restart. Must run before applyReplicaUpdates below, which re-fetches+ // the table from storage for its own persist -- a persistent backend serializes on write+ // rather than holding the live object, so without this flush first, that re-fetch would+ // see neither the stream nor the SSE mutations above.dynamoDbService.persistTable(tableName, table, region);if (!addRegions.isEmpty() || !removeRegions.isEmpty() || !updateRegions.isEmpty()) {table = dynamoDbService.applyReplicaUpdates(tableName, addRegions, removeRegions, updateRegions, region);}-ObjectNode response = objectMapper.createObjectNode();response.set("TableDescription", tableToNode(table));return Response.ok(response).build();@@ -2243,10 +2300,13 @@ public class DynamoDbJsonHandler {if (table.isSseEnabled()) {ObjectNode sseDescription = objectMapper.createObjectNode();sseDescription.put("Status", "ENABLED");- sseDescription.put("SSEType", table.getSseType() != null ? table.getSseType() : SSE_TYPE_KMS);- sseDescription.put("KMSMasterKeyArn", table.getKmsMasterKeyArn() != null- ? table.getKmsMasterKeyArn()- : defaultKmsMasterKeyArn(AwsArnUtils.regionOrDefault(table.getTableArn(), "us-east-1")));+ String sseType = table.getSseType() != null ? table.getSseType() : SSE_TYPE_KMS;+ sseDescription.put("SSEType", sseType);+ if (SSE_TYPE_KMS.equals(sseType)) {+ sseDescription.put("KMSMasterKeyArn", table.getKmsMasterKeyArn() != null+ ? table.getKmsMasterKeyArn()+ : defaultKmsMasterKeyArn(AwsArnUtils.regionOrDefault(table.getTableArn(), "us-east-1")));+ }node.set("SSEDescription", sseDescription);}diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.javaindex 85d32ae01c..7d29933e35 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java@@ -163,6 +163,320 @@ class DynamoDbIntegrationTest {.statusCode(200);}+ @Test+ void createTableWithCustomKmsKeyRetainsItOnDescribe() {+ String customKeyArn = "arn:aws:kms:us-east-1:000000000000:key/custom-key-id";++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "CustomKmsKeyTable",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST",+ "SSESpecification": {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "%s"}+ }+ """.formatted(customKeyArn))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("TableDescription.SSEDescription.Status", equalTo("ENABLED"))+ .body("TableDescription.SSEDescription.SSEType", equalTo("KMS"))+ .body("TableDescription.SSEDescription.KMSMasterKeyArn", equalTo(customKeyArn));++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "CustomKmsKeyTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Table.SSEDescription.Status", equalTo("ENABLED"))+ .body("Table.SSEDescription.SSEType", equalTo("KMS"))+ .body("Table.SSEDescription.KMSMasterKeyArn", equalTo(customKeyArn));++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "CustomKmsKeyTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void updateTableCanEnableRotateAndDisableSse() {+ String firstKeyArn = "arn:aws:kms:us-east-1:000000000000:key/first-key-id";+ String rotatedKeyArn = "arn:aws:kms:us-east-1:000000000000:key/rotated-key-id";++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "UpdateSseTable",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST"+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("TableDescription.SSEDescription", nullValue());++ // Enable SSE with a customer-supplied KMS key via UpdateTable.+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "UpdateSseTable",+ "SSESpecification": {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "%s"}+ }+ """.formatted(firstKeyArn))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("TableDescription.SSEDescription.Status", equalTo("ENABLED"))+ .body("TableDescription.SSEDescription.SSEType", equalTo("KMS"))+ .body("TableDescription.SSEDescription.KMSMasterKeyArn", equalTo(firstKeyArn));++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "UpdateSseTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Table.SSEDescription.Status", equalTo("ENABLED"))+ .body("Table.SSEDescription.SSEType", equalTo("KMS"))+ .body("Table.SSEDescription.KMSMasterKeyArn", equalTo(firstKeyArn));++ // Rotate to a different customer-supplied KMS key via UpdateTable.+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "UpdateSseTable",+ "SSESpecification": {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "%s"}+ }+ """.formatted(rotatedKeyArn))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("TableDescription.SSEDescription.KMSMasterKeyArn", equalTo(rotatedKeyArn));++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "UpdateSseTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Table.SSEDescription.KMSMasterKeyArn", equalTo(rotatedKeyArn));++ // Disable SSE via UpdateTable.+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "UpdateSseTable",+ "SSESpecification": {"Enabled": false}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("TableDescription.SSEDescription", nullValue());++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "UpdateSseTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Table.SSEDescription", nullValue());++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "UpdateSseTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void createTableWithAes256SseOmitsKmsMasterKeyArn() {+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "Aes256SseTable",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST",+ "SSESpecification": {"Enabled": true, "SSEType": "AES256"}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("TableDescription.SSEDescription.Status", equalTo("ENABLED"))+ .body("TableDescription.SSEDescription.SSEType", equalTo("AES256"))+ .body("TableDescription.SSEDescription.KMSMasterKeyArn", nullValue());++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "Aes256SseTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Table.SSEDescription.Status", equalTo("ENABLED"))+ .body("Table.SSEDescription.SSEType", equalTo("AES256"))+ .body("Table.SSEDescription.KMSMasterKeyArn", nullValue());++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "Aes256SseTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void createTableWithInvalidSseTypeFailsValidation() {+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "InvalidSseTypeTable",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST",+ "SSESpecification": {"Enabled": true, "SSEType": "BOGUS"}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"));++ // SSEType is validated before CreateTable runs, so the table must not exist at all --+ // a real DynamoDB rejects the whole request rather than leaving a table behind.+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "InvalidSseTypeTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ResourceNotFoundException"));+ }++ @Test+ void updateTableWithInvalidSseTypeFailsValidation() {+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "UpdateInvalidSseTypeTable",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST"+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {+ "TableName": "UpdateInvalidSseTypeTable",+ "SSESpecification": {"Enabled": true, "SSEType": "BOGUS"}+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"));++ // SSEType is validated before UpdateTable applies anything, so the table must still+ // show no SSEDescription at all -- the failed request must not have partially applied.+ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "UpdateInvalidSseTypeTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Table.SSEDescription", nullValue());++ given()+ .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName": "UpdateInvalidSseTypeTable"}+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }+@Testvoid createTableWithGsiAndLsi() {given()diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.javaindex d687053b1f..3cd459a071 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java@@ -1,11 +1,13 @@package io.github.hectorvent.floci.services.dynamodb;+import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.storage.InMemoryStorage;+import io.github.hectorvent.floci.core.storage.PersistentStorage;import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;@@ -13,8 +15,11 @@ import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;import jakarta.ws.rs.core.Response;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;+import java.nio.file.Path;import java.util.List;+import java.util.Map;import static org.junit.jupiter.api.Assertions.*;@@ -352,6 +357,50 @@ class DynamoDbJsonHandlerTest {assertNull(reasons.get(0).get("Message"), "non failed item must not have a Message field");}+ @Test+ void sseSettingsSurvivePersistentStorageReload(@TempDir Path tempDir) throws Exception {+ Path tableFile = tempDir.resolve("dynamodb-tables.json");+ TypeReference<Map<String, TableDefinition>> typeReference = new TypeReference<>() {};++ DynamoDbService persistentService = new DynamoDbService(new PersistentStorage<>(tableFile, typeReference));+ DynamoDbJsonHandler persistentHandler = new DynamoDbJsonHandler(persistentService, null, null, mapper);++ ObjectNode createRequest = mapper.createObjectNode();+ createRequest.put("TableName", "PersistedSseTable");+ ObjectNode keySchemaElement = mapper.createObjectNode();+ keySchemaElement.put("AttributeName", "pk");+ keySchemaElement.put("KeyType", "HASH");+ createRequest.putArray("KeySchema").add(keySchemaElement);+ ObjectNode attrDef = mapper.createObjectNode();+ attrDef.put("AttributeName", "pk");+ attrDef.put("AttributeType", "S");+ createRequest.putArray("AttributeDefinitions").add(attrDef);+ createRequest.put("BillingMode", "PAY_PER_REQUEST");++ persistentHandler.handle("CreateTable", createRequest, "us-east-1");++ ObjectNode updateRequest = mapper.createObjectNode();+ updateRequest.put("TableName", "PersistedSseTable");+ ObjectNode sseSpec = updateRequest.putObject("SSESpecification");+ sseSpec.put("Enabled", true);+ sseSpec.put("SSEType", "KMS");+ sseSpec.put("KMSMasterKeyId", "arn:aws:kms:us-east-1:000000000000:key/reload-test-key");++ persistentHandler.handle("UpdateTable", updateRequest, "us-east-1");++ // Simulate a process restart: a brand new storage backend instance reading the same file,+ // independent of the in-memory table object mutated above.+ PersistentStorage<String, TableDefinition> reloadedStorage = new PersistentStorage<>(tableFile, typeReference);+ reloadedStorage.load();+ DynamoDbService reloadedService = new DynamoDbService(reloadedStorage);++ TableDefinition reloadedTable = reloadedService.describeTable("PersistedSseTable", "us-east-1");+ assertTrue(reloadedTable.isSseEnabled(), "SSE must survive a storage reload");+ assertEquals("KMS", reloadedTable.getSseType());+ assertEquals("arn:aws:kms:us-east-1:000000000000:key/reload-test-key",+ reloadedTable.getKmsMasterKeyArn());+ }+// PutResourcePolicy/GetResourcePolicy/DeleteResourcePolicy previously fell through to the// default 400 UnknownOperationException branch, which blocks the Terraform provider's// aws_dynamodb_resource_policy resource at apply time.