Light, fluffy, and always free - The AWS Local Emulator alternative
fix: DynamoDB table creation compatibility with Terraform AWS provider v6 (#89)
Kurt McAlpine7b87bf2c1fa8
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.javaindex 08ce9c2714..cf34f05dfb 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java@@ -195,8 +195,9 @@ public class AwsJsonController {/*** Handles AWS smithy-rpc-v2-cbor protocol requests.- * Go SDK CloudWatch v1.55.1+ sends to POST /service/{serviceId}/operation/{op}+ * AWS SDK v2 sends to POST /service/{sdkId}/operation/{op}* with Content-Type: application/cbor and no X-Amz-Target header.+ * Supported services: DynamoDB, SQS, SNS, StepFunctions, CloudWatch.*/@POST@Path("service/{serviceId}/operation/{operation}")@@ -208,11 +209,7 @@ public class AwsJsonController {@Context HttpHeaders httpHeaders,byte[] body) {- if (!CLOUDWATCH_SERVICE_ID.equals(serviceId)) {- return Response.status(404).build();- }-- LOG.debugv("CloudWatch RPC v2 CBOR action: {0}", operation);+ LOG.debugv("Smithy RPC v2 CBOR: service={0}, operation={1}", serviceId, operation);try {JsonNode request = (body != null && body.length > 0)@@ -220,28 +217,21 @@ public class AwsJsonController {: objectMapper.createObjectNode();String region = regionResolver.resolveRegion(httpHeaders);- Response delegated = cloudWatchMetricsJsonHandler.handle(operation, request, region);- byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());+ Response delegated = dispatchCbor(serviceId, operation, request, region);+ if (delegated == null) {+ return Response.status(404).build();+ }+ byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());return Response.status(delegated.getStatus()).header("Smithy-Protocol", "rpc-v2-cbor").type("application/cbor").entity(cborBytes).build();} catch (AwsException e) {- try {- byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(- new AwsErrorResponse(e.getErrorCode(), e.getMessage()));- return Response.status(e.getHttpStatus())- .header("Smithy-Protocol", "rpc-v2-cbor")- .type("application/cbor")- .entity(errBytes)- .build();- } catch (Exception ex) {- return Response.status(e.getHttpStatus()).build();- }+ return cborErrorResponse(e, "Smithy-Protocol");} catch (Exception e) {- LOG.error("Error processing CloudWatch CBOR request", e);+ LOG.error("Error processing Smithy CBOR request: " + serviceId + "." + operation, e);return Response.status(500).build();}}@@ -258,12 +248,37 @@ public class AwsJsonController {@Context HttpHeaders httpHeaders,byte[] body) {- if (target == null || !target.startsWith(CLOUDWATCH_TARGET_PREFIX)) {+ if (target == null) {return null;}- String action = target.substring(CLOUDWATCH_TARGET_PREFIX.length());- LOG.debugv("CloudWatch CBOR action: {0}", action);+ String prefix;+ String serviceName;++ if (target.startsWith(DYNAMODB_STREAMS_TARGET_PREFIX)) {+ prefix = DYNAMODB_STREAMS_TARGET_PREFIX;+ serviceName = "DynamoDBStreams";+ } else if (target.startsWith(DYNAMODB_TARGET_PREFIX)) {+ prefix = DYNAMODB_TARGET_PREFIX;+ serviceName = "DynamoDB";+ } else if (target.startsWith(SQS_TARGET_PREFIX)) {+ prefix = SQS_TARGET_PREFIX;+ serviceName = "SQS";+ } else if (target.startsWith(SNS_TARGET_PREFIX)) {+ prefix = SNS_TARGET_PREFIX;+ serviceName = "SNS";+ } else if (target.startsWith(STEPFUNCTIONS_TARGET_PREFIX)) {+ prefix = STEPFUNCTIONS_TARGET_PREFIX;+ serviceName = "StepFunctions";+ } else if (target.startsWith(CLOUDWATCH_TARGET_PREFIX)) {+ prefix = CLOUDWATCH_TARGET_PREFIX;+ serviceName = "CloudWatch";+ } else {+ return null;+ }++ String action = target.substring(prefix.length());+ LOG.debugv("{0} CBOR action: {1}", serviceName, action);try {JsonNode request = (body != null && body.length > 0)@@ -271,29 +286,57 @@ public class AwsJsonController {: objectMapper.createObjectNode();String region = regionResolver.resolveRegion(httpHeaders);- Response delegated = cloudWatchMetricsJsonHandler.handle(action, request, region);- byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());+ Response delegated = switch (serviceName) {+ case "DynamoDB" -> dynamoDbJsonHandler.handle(action, request, region);+ case "DynamoDBStreams" -> dynamoDbStreamsJsonHandler.handle(action, request, region);+ case "SQS" -> sqsJsonHandler.handle(action, request, region);+ case "SNS" -> snsJsonHandler.handle(action, request, region);+ case "StepFunctions" -> sfnJsonHandler.handle(action, request, region);+ case "CloudWatch" -> cloudWatchMetricsJsonHandler.handle(action, request, region);+ default -> null;+ };+ if (delegated == null) {+ return null;+ }+ byte[] cborBytes = nodeToSmithyCbor((JsonNode) delegated.getEntity());return Response.status(delegated.getStatus()).header("smithy-protocol", "rpc-v2-cbor").type("application/cbor").entity(cborBytes).build();} catch (AwsException e) {- try {- byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(- new AwsErrorResponse(e.getErrorCode(), e.getMessage()));- return Response.status(e.getHttpStatus())- .header("smithy-protocol", "rpc-v2-cbor")- .type("application/cbor")- .entity(errBytes)- .build();- } catch (Exception ex) {- return Response.status(e.getHttpStatus()).build();- }+ return cborErrorResponse(e, "smithy-protocol");} catch (Exception e) {- LOG.error("Error processing CloudWatch CBOR request", e);+ LOG.error("Error processing CBOR request: " + serviceName + "." + action, e);return Response.status(500).build();}}++ /** Dispatches a CBOR request to the appropriate service handler by SDK service ID. */+ private Response dispatchCbor(String serviceId, String operation, JsonNode request, String region) throws Exception {+ return switch (serviceId) {+ case "DynamoDB" -> dynamoDbJsonHandler.handle(operation, request, region);+ case "DynamoDB Streams" -> dynamoDbStreamsJsonHandler.handle(operation, request, region);+ case "SQS" -> sqsJsonHandler.handle(operation, request, region);+ case "SNS" -> snsJsonHandler.handle(operation, request, region);+ case "SFN" -> sfnJsonHandler.handle(operation, request, region);+ case CLOUDWATCH_SERVICE_ID -> cloudWatchMetricsJsonHandler.handle(operation, request, region);+ default -> null;+ };+ }++ private Response cborErrorResponse(AwsException e, String protocolHeader) {+ try {+ byte[] errBytes = CBOR_MAPPER.writeValueAsBytes(+ new AwsErrorResponse(e.getErrorCode(), e.getMessage()));+ return Response.status(e.getHttpStatus())+ .header(protocolHeader, "rpc-v2-cbor")+ .type("application/cbor")+ .entity(errBytes)+ .build();+ } catch (Exception ex) {+ return Response.status(e.getHttpStatus()).build();+ }+ }}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 1c91913336..2af73357a8 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@@ -114,9 +114,28 @@ public class DynamoDbJsonHandler {}}+ String billingMode = request.has("BillingMode")+ ? request.get("BillingMode").asText() : null;+TableDefinition table = dynamoDbService.createTable(tableName, keySchema, attrDefs,readCapacity, writeCapacity, gsis, lsis, region);+ if ("PAY_PER_REQUEST".equals(billingMode)) {+ table.setBillingMode("PAY_PER_REQUEST");+ table.getProvisionedThroughput().setReadCapacityUnits(0L);+ table.getProvisionedThroughput().setWriteCapacityUnits(0L);+ } else {+ table.setBillingMode("PROVISIONED");+ }++ // Store tags from CreateTable request+ JsonNode tagsNode = request.path("Tags");+ if (tagsNode.isArray()) {+ for (JsonNode tag : tagsNode) {+ table.getTags().put(tag.path("Key").asText(), tag.path("Value").asText());+ }+ }+JsonNode streamSpec = request.path("StreamSpecification");if (!streamSpec.isMissingNode() && streamSpec.path("StreamEnabled").asBoolean(false)) {String viewType = streamSpec.path("StreamViewType").asText("NEW_AND_OLD_IMAGES");@@ -375,6 +394,16 @@ public class DynamoDbJsonHandler {TableDefinition table = dynamoDbService.updateTable(tableName, readCapacity, writeCapacity, region);+ String billingMode = request.has("BillingMode")+ ? request.get("BillingMode").asText() : null;+ if (billingMode != null) {+ table.setBillingMode(billingMode);+ if ("PAY_PER_REQUEST".equals(billingMode)) {+ table.getProvisionedThroughput().setReadCapacityUnits(0L);+ table.getProvisionedThroughput().setWriteCapacityUnits(0L);+ }+ }+JsonNode streamSpec = request.path("StreamSpecification");if (!streamSpec.isMissingNode()) {boolean streamEnabled = streamSpec.path("StreamEnabled").asBoolean(false);@@ -525,6 +554,21 @@ public class DynamoDbJsonHandler {node.put("CreationDateTime", table.getCreationDateTime().getEpochSecond());node.put("ItemCount", table.getItemCount());node.put("TableSizeBytes", table.getTableSizeBytes());+ node.put("DeletionProtectionEnabled", false);++ if ("PAY_PER_REQUEST".equals(table.getBillingMode())) {+ ObjectNode billing = objectMapper.createObjectNode();+ billing.put("BillingMode", "PAY_PER_REQUEST");+ billing.put("LastUpdateToPayPerRequestDateTime",+ table.getCreationDateTime().getEpochSecond());+ node.set("BillingModeSummary", billing);+ }++ ObjectNode warmThroughput = objectMapper.createObjectNode();+ warmThroughput.put("Status", "ACTIVE");+ warmThroughput.put("ReadUnitsPerSecond", 0);+ warmThroughput.put("WriteUnitsPerSecond", 0);+ node.set("WarmThroughput", warmThroughput);ArrayNode keySchemaArray = objectMapper.createArrayNode();for (var ks : table.getKeySchema()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.javaindex 8e360488a2..6adeb0c63b 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/TableDefinition.java@@ -29,6 +29,7 @@ public class TableDefinition {private Map<String, String> tags;private List<GlobalSecondaryIndex> globalSecondaryIndexes;private List<LocalSecondaryIndex> localSecondaryIndexes;+ private String billingMode; // "PROVISIONED" or "PAY_PER_REQUEST"private String ttlAttributeName;private boolean ttlEnabled;private boolean streamEnabled;@@ -107,6 +108,9 @@ public class TableDefinition {this.localSecondaryIndexes = localSecondaryIndexes != null ? localSecondaryIndexes : new ArrayList<>();}+ public String getBillingMode() { return billingMode; }+ public void setBillingMode(String billingMode) { this.billingMode = billingMode; }+public String getTtlAttributeName() { return ttlAttributeName; }public void setTtlAttributeName(String ttlAttributeName) { this.ttlAttributeName = ttlAttributeName; }diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.javanew file mode 100644index 0000000000..1203c8d45c--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.java@@ -0,0 +1,176 @@+package io.github.hectorvent.floci.services.dynamodb;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.dataformat.cbor.CBORFactory;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.RestAssured;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.*;++/**+ * Tests the smithy-rpc-v2-cbor protocol for DynamoDB.+ * AWS SDK v2 sends DynamoDB requests as CBOR to /service/DynamoDB/operation/{op}.+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class DynamoDbCborIntegrationTest {++ private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());+ private static final ObjectMapper JSON_MAPPER = new ObjectMapper();++ @Test+ @Order(1)+ void createTableViaCbor() throws Exception {+ JsonNode request = JSON_MAPPER.readTree("""+ {+ "TableName": "CborTable",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"}+ ],+ "AttributeDefinitions": [+ {"AttributeName": "pk", "AttributeType": "S"}+ ],+ "ProvisionedThroughput": {+ "ReadCapacityUnits": 5,+ "WriteCapacityUnits": 5+ }+ }+ """);++ byte[] cborBody = CBOR_MAPPER.writeValueAsBytes(request);++ byte[] responseBytes = given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .body(cborBody)+ .when()+ .post("/service/DynamoDB/operation/CreateTable")+ .then()+ .statusCode(200)+ .contentType("application/cbor")+ .extract().asByteArray();++ JsonNode response = CBOR_MAPPER.readTree(responseBytes);+ assertThat(response.path("TableDescription").path("TableName").asText(), equalTo("CborTable"));+ assertThat(response.path("TableDescription").path("TableStatus").asText(), equalTo("ACTIVE"));+ }++ @Test+ @Order(2)+ void describeTableViaCbor() throws Exception {+ JsonNode request = JSON_MAPPER.readTree("""+ {"TableName": "CborTable"}+ """);++ byte[] cborBody = CBOR_MAPPER.writeValueAsBytes(request);++ byte[] responseBytes = given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .body(cborBody)+ .when()+ .post("/service/DynamoDB/operation/DescribeTable")+ .then()+ .statusCode(200)+ .contentType("application/cbor")+ .extract().asByteArray();++ JsonNode response = CBOR_MAPPER.readTree(responseBytes);+ assertThat(response.path("Table").path("TableName").asText(), equalTo("CborTable"));+ assertThat(response.path("Table").path("TableStatus").asText(), equalTo("ACTIVE"));+ }++ @Test+ @Order(3)+ void describeNonExistentTableViaCbor() throws Exception {+ JsonNode request = JSON_MAPPER.readTree("""+ {"TableName": "NoSuchTable"}+ """);++ byte[] cborBody = CBOR_MAPPER.writeValueAsBytes(request);++ byte[] responseBytes = given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .body(cborBody)+ .when()+ .post("/service/DynamoDB/operation/DescribeTable")+ .then()+ .statusCode(400)+ .extract().asByteArray();++ JsonNode response = CBOR_MAPPER.readTree(responseBytes);+ assertThat(response.path("__type").asText(), equalTo("ResourceNotFoundException"));+ }++ @Test+ @Order(4)+ void putAndGetItemViaCbor() throws Exception {+ JsonNode putRequest = JSON_MAPPER.readTree("""+ {+ "TableName": "CborTable",+ "Item": {+ "pk": {"S": "cbor-1"},+ "data": {"S": "hello cbor"}+ }+ }+ """);++ given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .body(CBOR_MAPPER.writeValueAsBytes(putRequest))+ .when()+ .post("/service/DynamoDB/operation/PutItem")+ .then()+ .statusCode(200);++ JsonNode getRequest = JSON_MAPPER.readTree("""+ {+ "TableName": "CborTable",+ "Key": {"pk": {"S": "cbor-1"}}+ }+ """);++ byte[] responseBytes = given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .body(CBOR_MAPPER.writeValueAsBytes(getRequest))+ .when()+ .post("/service/DynamoDB/operation/GetItem")+ .then()+ .statusCode(200)+ .extract().asByteArray();++ JsonNode response = CBOR_MAPPER.readTree(responseBytes);+ assertThat(response.path("Item").path("data").path("S").asText(), equalTo("hello cbor"));+ }++ @Test+ @Order(5)+ void deleteTableViaCbor() throws Exception {+ JsonNode request = JSON_MAPPER.readTree("""+ {"TableName": "CborTable"}+ """);++ byte[] responseBytes = given()+ .contentType("application/cbor")+ .accept("application/cbor")+ .body(CBOR_MAPPER.writeValueAsBytes(request))+ .when()+ .post("/service/DynamoDB/operation/DeleteTable")+ .then()+ .statusCode(200)+ .extract().asByteArray();++ JsonNode response = CBOR_MAPPER.readTree(responseBytes);+ assertThat(response.path("TableDescription").path("TableStatus").asText(), equalTo("DELETING"));+ }+}