Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): reject an unknown BillingMode (#4554)

TATSUNO “Taz” Yasuhirob8b5fcd4766b
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
index 620b16aaba..49be5401cb 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java
@@ -119,6 +119,7 @@ public class DynamoDbService {
private static final int MIN_VECTOR_INDEX_NAME_LENGTH = 3;
// The order AWS prints the enum constraint in, which is neither alphabetical nor the order
// the API reference lists.
+ private static final Set<String> VALID_BILLING_MODES = Set.of("PROVISIONED", "PAY_PER_REQUEST");
private static final List<String> VECTOR_DISTANCE_FUNCTIONS = List.of(
DynamoDbVectorScoring.DOT_PRODUCT,
DynamoDbVectorScoring.COSINE,
@@ -4792,6 +4793,16 @@ public class DynamoDbService {
* leaves nothing behind. Returns the import already started with the same ClientToken,
* or null when this is a new import.
*/
+ static void validateBillingMode(JsonNode request, String memberPrefix) {
+ JsonNode billingMode = request.path("BillingMode");
+ if (billingMode.isTextual() && !VALID_BILLING_MODES.contains(billingMode.asText())) {
+ throw new AwsException("ValidationException",
+ "1 validation error detected: Value '" + billingMode.asText() + "' at '" + memberPrefix
+ + "billingMode' failed to satisfy constraint: "
+ + "Member must satisfy enum value set: [PROVISIONED, PAY_PER_REQUEST]", 400);
+ }
+ }
+
public ImportTableDescription validateImportRequest(JsonNode request) {
if (request.path("S3BucketSource").path("S3Bucket").asText("").isBlank()) {
throw new AwsException("ValidationException", "S3BucketSource.S3Bucket is required", 400);
@@ -4816,6 +4827,7 @@ public class DynamoDbService {
+ "' at 'inputCompressionType' failed to satisfy constraint: Member must satisfy enum value set: [GZIP, ZSTD, NONE]", 400);
}
DynamoDbTableNames.requireShortName(request.path("TableCreationParameters").path("TableName").asText(null));
+ validateBillingMode(request.path("TableCreationParameters"), "tableCreationParameters.");
var clientToken = request.path("ClientToken").asText(null);
if (clientToken != null && clientToken.isBlank()) {
throw new AwsException("ValidationException",
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java
index f4f31d1d1e..c6dc7f9c0f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java
@@ -123,7 +123,7 @@ public class NativeDynamoDbJsonHandler {
}
private Response handleCreateTable(JsonNode request, String region) {
- var table = createTableFromSpec(request, region, "ACTIVE");
+ var table = createTableFromSpec(request, "", region, "ACTIVE");
var response = objectMapper.createObjectNode();
response.set("TableDescription", tableToNode(table));
@@ -131,11 +131,13 @@ public class NativeDynamoDbJsonHandler {
}
/** Shared by CreateTable and ImportTable, whose TableCreationParameters use the same keys. */
- private TableDefinition createTableFromSpec(JsonNode request, String region, String initialStatus) {
+ private TableDefinition createTableFromSpec(JsonNode request, String memberPrefix, String region,
+ String initialStatus) {
// Distinguish missing (undefined) from empty ("") for TableName
String tableNameRaw = (request.has("TableName") && !request.path("TableName").isNull())
? request.path("TableName").asText() : null;
String tableName = DynamoDbTableNames.requireShortName(tableNameRaw);
+ DynamoDbService.validateBillingMode(request, memberPrefix);
List<KeySchemaElement> keySchema = new ArrayList<>();
request.path("KeySchema").forEach(ks ->
@@ -1619,6 +1621,7 @@ public class NativeDynamoDbJsonHandler {
private Response handleUpdateTable(JsonNode request, String region) {
String tableName = request.path("TableName").asText();
+ DynamoDbService.validateBillingMode(request, "");
JsonNode replicaUpdates = request.path("ReplicaUpdates");
List<String> addRegions = new ArrayList<>();
List<String> removeRegions = new ArrayList<>();
@@ -2892,7 +2895,8 @@ public class NativeDynamoDbJsonHandler {
return existing;
}
return dynamoDbService.startImport(request,
- createTableFromSpec(request.path("TableCreationParameters"), region, "CREATING"), region);
+ createTableFromSpec(request.path("TableCreationParameters"), "tableCreationParameters.",
+ region, "CREATING"), region);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbImportIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbImportIntegrationTest.java
index 8f0333e77c..d527c2f0f6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbImportIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbImportIntegrationTest.java
@@ -350,6 +350,51 @@ class DynamoDbImportIntegrationTest {
.body("__type", containsString("ResourceNotFoundException"));
}
+ @Test
+ void importTable_invalidBillingMode_returnsValidationException() {
+ String request = importRequest("ImportBadBilling", BUCKET_NAME, "plain/", "DYNAMODB_JSON", "NONE")
+ .replace("\"PAY_PER_REQUEST\"", "\"INVALID_MODE\"");
+ dynamo("ImportTable", request)
+ .then()
+ .statusCode(400)
+ .body("__type", containsString("ValidationException"))
+ .body("message", equalTo("1 validation error detected: Value 'INVALID_MODE' at "
+ + "'tableCreationParameters.billingMode' failed to satisfy constraint: "
+ + "Member must satisfy enum value set: [PROVISIONED, PAY_PER_REQUEST]"));
+
+ dynamo("DescribeTable", "{\"TableName\": \"ImportBadBilling\"}")
+ .then()
+ .statusCode(400)
+ .body("__type", containsString("ResourceNotFoundException"));
+ }
+
+ @Test
+ void importTable_invalidBillingModeOnARetry_isAValidationErrorNotAConflict() {
+ putObject("token-billing/data.json", "{\"Item\":{\"pk\":{\"S\":\"one\"}}}\n");
+ String body = """
+ {
+ "ClientToken": "import-token-billing",
+ "S3BucketSource": {"S3Bucket": "%s", "S3KeyPrefix": "token-billing/"},
+ "InputFormat": "DYNAMODB_JSON",
+ "TableCreationParameters": {
+ "TableName": "ImportTokenBilling",
+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ }
+ """.formatted(BUCKET_NAME);
+ dynamo("ImportTable", body).then().statusCode(200);
+
+ dynamo("ImportTable", body.replace("\"PAY_PER_REQUEST\"", "\"INVALID_MODE\""))
+ .then()
+ .statusCode(400)
+ .body("__type", containsString("ValidationException"))
+ .body("message", equalTo("1 validation error detected: Value 'INVALID_MODE' at "
+ + "'tableCreationParameters.billingMode' failed to satisfy constraint: "
+ + "Member must satisfy enum value set: [PROVISIONED, PAY_PER_REQUEST]"));
+ }
+
@Test
void importTable_zstdCompression_returnsValidationException() {
dynamo("ImportTable", importRequest("ImportZstd", BUCKET_NAME, "plain/", "DYNAMODB_JSON", "ZSTD"))
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.java
index 969b214146..d8f3345fa4 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
@@ -477,6 +477,59 @@ class DynamoDbIntegrationTest {
.statusCode(200);
}
+ @Test
+ void createTableWithInvalidBillingModeFailsValidation() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {
+ "TableName": "InvalidBillingModeTable",
+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
+ "BillingMode": "INVALID_MODE"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: Value 'INVALID_MODE' at 'billingMode' "
+ + "failed to satisfy constraint: Member must satisfy enum value set: "
+ + "[PROVISIONED, PAY_PER_REQUEST]"));
+
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "InvalidBillingModeTable"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ResourceNotFoundException"));
+ }
+
+ @Test
+ void updateTableWithInvalidBillingModeFailsValidationBeforeTheTableLookup() {
+ given()
+ .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")
+ .contentType(DYNAMODB_CONTENT_TYPE)
+ .body("""
+ {"TableName": "NoSuchBillingModeTable", "BillingMode": "pay_per_request"}
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("1 validation error detected: Value 'pay_per_request' at 'billingMode' "
+ + "failed to satisfy constraint: Member must satisfy enum value set: "
+ + "[PROVISIONED, PAY_PER_REQUEST]"));
+ }
+
@Test
void createTableWithGsiAndLsi() {
given()