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

@@ -0,0 +1,141 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.testing.AwsTestClients;
import io.floci.aws.testing.CfnStackWaits;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
import software.amazon.awssdk.services.dynamodb.model.SSEDescription;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@code SSESpecification} on {@code AWS::DynamoDB::Table} and {@code AWS::DynamoDB::GlobalTable}: "If enabled (true), server-side
* encryption type is set to KMS and an AWS managed key is used" (AWS::DynamoDB::Table
* SSESpecification), so DescribeTable reports the {@code SSEDescription} that CreateTable with the
* same setting gives.
*/
@QuarkusTest
class CloudFormationDynamoDbSseSpecificationIntegrationTest {
private static final String TEMPLATE = """
{
"Resources": {
"Orders": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"TableName": "%s",
"BillingMode": "PAY_PER_REQUEST",
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"SSESpecification": %s
}
}
}
}
""";
private static final String GLOBAL_TEMPLATE = """
{
"Resources": {
"Orders": {
"Type": "AWS::DynamoDB::GlobalTable",
"Properties": {
"TableName": "%s",
"BillingMode": "PAY_PER_REQUEST",
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"Replicas": [{"Region": "us-east-1"}],
"SSESpecification": {"SSEEnabled": true, "SSEType": "KMS"}
}
}
}
}
""";
private CloudFormationClient cfn;
private DynamoDbClient dynamoDb;
private String stackName;
private String tableName;
@BeforeEach
void setUp() {
cfn = AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1);
dynamoDb = AwsTestClients.create(DynamoDbClient.builder(), "test", Region.US_EAST_1);
String suffix = Long.toString(System.nanoTime(), 36);
stackName = "cfn-ddb-sse-" + suffix;
tableName = "sse-table-" + suffix;
}
@AfterEach
void cleanUp() {
try {
CfnStackWaits.deleteStack(cfn, stackName);
} finally {
cfn.close();
dynamoDb.close();
}
}
@Test
void sseEnabledEncryptsTheTableWithTheAwsManagedKey() {
CfnStackWaits.createStack(cfn, stackName, TEMPLATE.formatted(tableName, "{\"SSEEnabled\": true}"),
"CREATE_COMPLETE");
SSEDescription sse = dynamoDb.describeTable(r -> r.tableName(tableName)).table().sseDescription();
assertEquals("ENABLED", sse.statusAsString());
assertEquals("KMS", sse.sseTypeAsString());
assertTrue(sse.kmsMasterKeyArn().endsWith(":key/aws-managed-dynamodb"), sse.kmsMasterKeyArn());
}
@Test
void kmsMasterKeyIdIsTheKeyTheTableReports() {
String key = "arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
CfnStackWaits.createStack(cfn, stackName, TEMPLATE.formatted(tableName,
"{\"SSEEnabled\": true, \"SSEType\": \"KMS\", \"KMSMasterKeyId\": \"" + key + "\"}"),
"CREATE_COMPLETE");
SSEDescription sse = dynamoDb.describeTable(r -> r.tableName(tableName)).table().sseDescription();
assertEquals(key, sse.kmsMasterKeyArn());
}
@Test
void globalTableSseEnabledEncryptsTheTableWithTheAwsManagedKey() {
CfnStackWaits.createStack(cfn, stackName, GLOBAL_TEMPLATE.formatted(tableName), "CREATE_COMPLETE");
SSEDescription sse = dynamoDb.describeTable(r -> r.tableName(tableName)).table().sseDescription();
assertEquals("ENABLED", sse.statusAsString());
assertEquals("KMS", sse.sseTypeAsString());
assertTrue(sse.kmsMasterKeyArn().endsWith(":key/aws-managed-dynamodb"), sse.kmsMasterKeyArn());
}
@Test
void unknownSseTypeFailsTheStackAndLeavesNoTable() {
CfnStackWaits.createStack(cfn, stackName, TEMPLATE.formatted(tableName,
"{\"SSEEnabled\": true, \"SSEType\": \"DES\"}"), "ROLLBACK_COMPLETE");
String reason = CfnStackWaits.failedResourceReason(stackName);
assertTrue(reason.contains("Value 'DES' at 'sSESpecification.sSEType' failed to satisfy constraint"), reason);
assertThrows(ResourceNotFoundException.class, () -> dynamoDb.describeTable(r -> r.tableName(tableName)));
}
@Test
void emptyKmsMasterKeyIdFailsTheStackAndLeavesNoTable() {
CfnStackWaits.createStack(cfn, stackName, TEMPLATE.formatted(tableName,
"{\"SSEEnabled\": true, \"KMSMasterKeyId\": \"\"}"), "ROLLBACK_COMPLETE");
String reason = CfnStackWaits.failedResourceReason(stackName);
assertTrue(reason.contains("Value '' at 'sSESpecification.kMSMasterKeyId' failed to satisfy constraint"), reason);
assertThrows(ResourceNotFoundException.class, () -> dynamoDb.describeTable(r -> r.tableName(tableName)));
}
}
@@ -293,6 +293,61 @@ class DynamoDbLocalBackendIntegrationTest {
.body("TimeToLiveDescription.AttributeName", equalTo("expiresAt"));
}
@Test
void aStackDeclaringEncryptionCreatesItsTableOnLocal() {
String table = tableName("sse");
String stack = "local-it-sse-" + UUID.randomUUID();
String template = """
{
"Resources": {
"Orders": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"SSESpecification": {"SSEEnabled": true}
}
}
}
}
""".formatted(table);
cloudFormation(A_EAST, "CreateStack", stack, template);
created.add(new CreatedTable(A_EAST, table));
CfnStackWaits.awaitStatus(stack, "CREATE_COMPLETE", A_EAST.authorization("cloudformation"));
dynamoDb(A_EAST, "DescribeTable", "{\"TableName\": \"" + table + "\"}").statusCode(200);
}
@Test
void aStackDeclaringAnUnknownSseTypeRollsBackOnLocal() {
String table = tableName("sse-des");
String stack = "local-it-sse-des-" + UUID.randomUUID();
String template = """
{
"Resources": {
"Orders": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"SSESpecification": {"SSEEnabled": true, "SSEType": "DES"}
}
}
}
}
""".formatted(table);
cloudFormation(A_EAST, "CreateStack", stack, template);
CfnStackWaits.awaitStatus(stack, "ROLLBACK_COMPLETE", A_EAST.authorization("cloudformation"));
dynamoDb(A_EAST, "DescribeTable", "{\"TableName\": \"" + table + "\"}").statusCode(400);
}
@Test
void resetIsRefused() {
given().when().post("/_floci/state/reset").then().statusCode(409);
@@ -105,6 +105,12 @@ public final class RecordingDynamoDbBackend implements DynamoDbItemAccess, Dynam
return record("updateThroughput", scope);
}
@Override
public TableDefinition enableServerSideEncryption(Scope scope, String tableName, String sseType,
String kmsMasterKeyId) {
return record("enableServerSideEncryption", scope);
}
@Override
public TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,