fix(cloudformation): apply SSESpecification to a DynamoDB table on create (#9379)

This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-24 16:41:13 -04:00
1 parent 4a9ae4a569
commit bc6a2279d4
12 files changed
+271 -11

No files matched your search

@@ -185,6 +185,7 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
boolean plainTable = TABLE.equals(r.getResourceType());
ThroughputSpec tableThroughput = plainTable ? declaredThroughput(props, "#", ctx) : NO_THROUGHPUT;
Map<String, ThroughputSpec> indexThroughput = new LinkedHashMap<>();
JsonNode sseSpec = props != null ? engine.resolveNode(props.get("SSESpecification")) : null;
List<KeySchemaElement> keySchema = new ArrayList<>();
List<AttributeDefinition> attrDefs = new ArrayList<>();
@@ -308,6 +309,12 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
}
try {
// CreateTable takes SSESpecification itself; applied right after it, a refused SSEType
// still leaves no table, as the failure below deletes the one this attempt created.
if (created && sseSpec != null && "true".equalsIgnoreCase(ctx.resolveOptional(sseSpec, "SSEEnabled"))) {
table = dynamoDb.tables().enableServerSideEncryption(scope, tableName,
ctx.resolveOptional(sseSpec, "SSEType"), ctx.resolveOptional(sseSpec, "KMSMasterKeyId"));
}
// TTL is read when the template declares it, and on an in-place update for the rollback
// snapshot, so UpdateTimeToLive is called only when something differs. A rename of an
// enabled TTL attribute fails here, and the snapshot is taken here, before anything changes.
@@ -2601,13 +2601,8 @@ public class NativeDynamoDbJsonHandler {
addIndexViolations(request.path("GlobalSecondaryIndexes"), "globalSecondaryIndexes", true, violations);
addOlderEnumError(request, "BillingMode", "billingMode", DynamoDbService.BILLING_MODES, violations);
JsonNode sse = request.path("SSESpecification");
String kmsMasterKeyId = textOrNull(sse, "KMSMasterKeyId");
if (kmsMasterKeyId != null) {
addIfPresent(violations, SmithyValidationErrors.lengthOutOfRange(
"sSESpecification.kMSMasterKeyId", kmsMasterKeyId, 1, 2048));
}
addOlderEnumError(sse, "SSEType", "sSESpecification.sSEType", NativeDynamoDbTableService.SSE_TYPES,
violations);
violations.addAll(NativeDynamoDbTableService.sseSpecificationViolations(textOrNull(sse, "SSEType"),
textOrNull(sse, "KMSMasterKeyId")));
addKeySchemaViolations(request.path("KeySchema"), "keySchema", violations);
violations.addAll(throughputViolations(request.path("ProvisionedThroughput"), "provisionedThroughput"));
JsonNode stream = request.path("StreamSpecification");
@@ -435,6 +435,16 @@ public class NativeDynamoDbTableService {
warm(change.table().warm())), region);
}
/** The UpdateTable that turns server-side encryption on, with the checks of the API. */
public TableDefinition enableServerSideEncryption(String tableName, String sseType, String kmsMasterKeyId,
String region) {
requireValidSseSpecification(sseType, kmsMasterKeyId);
TableSettings settings = new TableSettings(null, null, null, null, null, null, true, sseType,
kmsMasterKeyId);
return updateTable(new UpdateTableRequest(tableName, null, List.of(), List.of(), List.of(), List.of(),
List.of(), List.of(), List.of(), List.of(), List.of(), settings, null), region);
}
private static Throughput throughput(CapacityUnits capacity) {
return capacity == null ? null : new Throughput(capacity.readCapacityUnits(), capacity.writeCapacityUnits());
}
@@ -849,6 +859,30 @@ public class NativeDynamoDbTableService {
return kmsMasterKeyId != null ? kmsMasterKeyId : defaultKmsMasterKeyArn(region);
}
/** CreateTable's checks on the members of {@code SSESpecification}, in the order DynamoDB reports them. */
static List<String> sseSpecificationViolations(String sseType, String kmsMasterKeyId) {
List<String> violations = new ArrayList<>();
if (kmsMasterKeyId != null) {
String keyViolation = SmithyValidationErrors.lengthOutOfRange("sSESpecification.kMSMasterKeyId",
kmsMasterKeyId, 1, 2048);
if (keyViolation != null) {
violations.add(keyViolation);
}
}
if (sseType != null && !SSE_TYPES.contains(sseType)) {
violations.add(SmithyValidationErrors.notInEnum(sseType, "sSESpecification.sSEType", SSE_TYPES));
}
return violations;
}
/** Refuses an {@code SSESpecification} that CreateTable would refuse, whichever engine keeps the table. */
public static void requireValidSseSpecification(String sseType, String kmsMasterKeyId) {
List<String> violations = sseSpecificationViolations(sseType, kmsMasterKeyId);
if (!violations.isEmpty()) {
throw SmithyValidationErrors.validationException(violations);
}
}
private static void validateSseType(String sseType) {
if (!SSE_TYPES.contains(sseType)) {
throw SmithyValidationErrors.validationException(
@@ -19,6 +19,7 @@ import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.dynamodb.DynamoDbTableNames;
import io.floci.aws.services.dynamodb.DynamoDbTtlIdentity;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService;
import io.floci.aws.services.dynamodb.model.AttributeDefinition;
import io.floci.aws.services.dynamodb.model.GlobalSecondaryIndex;
import io.floci.aws.services.dynamodb.model.KeySchemaElement;
@@ -461,6 +462,15 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
return table(call(scope, "UpdateTable", body).path("TableDescription"));
}
// ponytail: Local keeps no encryption setting and answers an UpdateTable that only sets one with
// "Nothing to update", so the setting gets CreateTable's checks and the table is left as it is.
@Override
public TableDefinition enableServerSideEncryption(Scope scope, String tableName, String sseType,
String kmsMasterKeyId) {
NativeDynamoDbTableService.requireValidSseSpecification(sseType, kmsMasterKeyId);
return describeTable(scope, tableName);
}
private ObjectNode capacity(CapacityUnits capacity) {
return aws.createObjectNode()
.put("ReadCapacityUnits", capacity.readCapacityUnits())
@@ -52,6 +52,12 @@ public interface DynamoDbTableAccess {
/** One UpdateTable that changes the capacity and warm throughput of the table and of the named indexes. */
TableDefinition updateThroughput(Scope scope, String tableName, ThroughputChange change);
/**
* Turns server-side encryption on as CreateTable's {@code SSESpecification} does, with its checks.
* A null {@code sseType} means KMS, and a null {@code kmsMasterKeyId} the AWS managed key.
*/
TableDefinition enableServerSideEncryption(Scope scope, String tableName, String sseType, String kmsMasterKeyId);
TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,
List<GlobalSecondaryIndex> globalSecondaryIndexes,
@@ -197,6 +197,13 @@ public class NativeDynamoDbBackend implements DynamoDbBackend {
() -> tableService.updateThroughput(tableName, change, scope.region()));
}
@Override
public TableDefinition enableServerSideEncryption(Scope scope, String tableName, String sseType,
String kmsMasterKeyId) {
return RequestScopes.callAs(scope.accountId(),
() -> tableService.enableServerSideEncryption(tableName, sseType, kmsMasterKeyId, scope.region()));
}
@Override
public TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,