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

* fix(dynamodb): reject an unknown BillingMode

CreateTable, UpdateTable and ImportTable accepted any BillingMode and
treated an unknown one as PROVISIONED. AWS rejects it with a
ValidationException before looking up the table. ImportTable reports the
member as tableCreationParameters.billingMode.

* fix(dynamodb): check an import's BillingMode before its ClientToken

On AWS an ImportTable retry with the same ClientToken and an unknown
BillingMode fails with the BillingMode ValidationException, not with
ImportConflictException. The check now runs before the token lookup.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-28 07:24:11 -04:00
1 parent bf47dd7193
commit b8b5fcd476
4 files changed
+117 -3

No files matched your search

@@ -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",
@@ -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);
}
}
@@ -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"))
@@ -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()