mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): honor customer-supplied KMS key for table SSE (#1923)
* fix(dynamodb): honor customer-supplied KMS key for table SSE
CreateTable discarded SSESpecification.KMSMasterKeyId and always stored
the default AWS-managed key ARN, so CreateTable/DescribeTable responses
never reflected a customer-managed key. It also hardcoded SSEType to
KMS, ignoring AES256. UpdateTable had no SSESpecification handling at
all, so a table's encryption could not be enabled, rotated to a new
key, or disabled after creation.
* fix(dynamodb): persist post-create/update SSE state and validate SSEType
Address PR review feedback: CreateTable/UpdateTable mutated the table's
SSE fields after the service layer's own persistence call, so a
persistent/WAL storage backend would silently drop the encryption
settings (along with other post-create/update fields) on reload. Both
handlers now re-persist the fully mutated table before responding.
SSEType was also accepted verbatim with no enum check, letting an
invalid value be stored and echoed back in SSEDescription. Both
handlers now validate it against {KMS, AES256}.
Adds a reload test proving SSE settings survive a fresh
PersistentStorage instance reading the same file, an AES256 coverage
test, and validation-failure tests for both CreateTable and
UpdateTable.
* fix(dynamodb): validate SSEType before mutating, not after
Closes the remaining review items on this PR:
- Resolved the merge conflict with #1811 (global table v2 replicas) in
handleUpdateTable. Also found and fixed a duplicate: main had
independently added its own persistTable(...) flush for the same
stream-persistence reason this PR added one for SSE, so the two
collapse into one call.
- Moved SSEType validation ahead of createTable/updateTable in both
handlers, mirroring the existing validateReplicaUpdates-before-
updateTable pattern already used in handleUpdateTable. Previously an
invalid SSEType threw only after the table (and possibly its stream)
were already created/mutated, leaving real side effects behind a
ValidationException -- real DynamoDB rejects the whole request first.
- The merge resolution deliberately does NOT follow the literal
"replica block first" suggestion from review: applyReplicaUpdates
re-fetches the table from storage for its own persist, and a
persistent backend does not hold the live object (confirmed by
reading both applyReplicaUpdates itself and the comment already on
main's persistTable call). Running it before the stream/SSE
mutations are flushed would silently drop them, not just SSE's.
Kept main's original persistTable-then-applyReplicaUpdates ordering
instead, with the SSE block slotted in before the flush, matching
the same stream-then-SSE-then-persist shape CreateTable already uses.
- Added DescribeTable assertions to both existing invalid-SSEType
tests confirming no side effects survive the failed request (no
table for CreateTable, no SSEDescription change for UpdateTable) --
meaningful now that validation actually runs first.
AES256 test coverage (createTableWithAes256SseOmitsKmsMasterKeyArn)
was already present as of the second commit on this branch; no
further gap there.
---------
Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
3 files changed
+435
-12
No files matched your search
+314
@@ -163,6 +163,320 @@ class DynamoDbIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableWithCustomKmsKeyRetainsItOnDescribe() {
|
||||
String customKeyArn = "arn:aws:kms:us-east-1:000000000000:key/custom-key-id";
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "CustomKmsKeyTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST",
|
||||
"SSESpecification": {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "%s"}
|
||||
}
|
||||
""".formatted(customKeyArn))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TableDescription.SSEDescription.Status", equalTo("ENABLED"))
|
||||
.body("TableDescription.SSEDescription.SSEType", equalTo("KMS"))
|
||||
.body("TableDescription.SSEDescription.KMSMasterKeyArn", equalTo(customKeyArn));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "CustomKmsKeyTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Table.SSEDescription.Status", equalTo("ENABLED"))
|
||||
.body("Table.SSEDescription.SSEType", equalTo("KMS"))
|
||||
.body("Table.SSEDescription.KMSMasterKeyArn", equalTo(customKeyArn));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "CustomKmsKeyTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTableCanEnableRotateAndDisableSse() {
|
||||
String firstKeyArn = "arn:aws:kms:us-east-1:000000000000:key/first-key-id";
|
||||
String rotatedKeyArn = "arn:aws:kms:us-east-1:000000000000:key/rotated-key-id";
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "UpdateSseTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TableDescription.SSEDescription", nullValue());
|
||||
|
||||
// Enable SSE with a customer-supplied KMS key via UpdateTable.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "UpdateSseTable",
|
||||
"SSESpecification": {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "%s"}
|
||||
}
|
||||
""".formatted(firstKeyArn))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TableDescription.SSEDescription.Status", equalTo("ENABLED"))
|
||||
.body("TableDescription.SSEDescription.SSEType", equalTo("KMS"))
|
||||
.body("TableDescription.SSEDescription.KMSMasterKeyArn", equalTo(firstKeyArn));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "UpdateSseTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Table.SSEDescription.Status", equalTo("ENABLED"))
|
||||
.body("Table.SSEDescription.SSEType", equalTo("KMS"))
|
||||
.body("Table.SSEDescription.KMSMasterKeyArn", equalTo(firstKeyArn));
|
||||
|
||||
// Rotate to a different customer-supplied KMS key via UpdateTable.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "UpdateSseTable",
|
||||
"SSESpecification": {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "%s"}
|
||||
}
|
||||
""".formatted(rotatedKeyArn))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TableDescription.SSEDescription.KMSMasterKeyArn", equalTo(rotatedKeyArn));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "UpdateSseTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Table.SSEDescription.KMSMasterKeyArn", equalTo(rotatedKeyArn));
|
||||
|
||||
// Disable SSE via UpdateTable.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "UpdateSseTable",
|
||||
"SSESpecification": {"Enabled": false}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TableDescription.SSEDescription", nullValue());
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "UpdateSseTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Table.SSEDescription", nullValue());
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "UpdateSseTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableWithAes256SseOmitsKmsMasterKeyArn() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "Aes256SseTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST",
|
||||
"SSESpecification": {"Enabled": true, "SSEType": "AES256"}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TableDescription.SSEDescription.Status", equalTo("ENABLED"))
|
||||
.body("TableDescription.SSEDescription.SSEType", equalTo("AES256"))
|
||||
.body("TableDescription.SSEDescription.KMSMasterKeyArn", nullValue());
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "Aes256SseTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Table.SSEDescription.Status", equalTo("ENABLED"))
|
||||
.body("Table.SSEDescription.SSEType", equalTo("AES256"))
|
||||
.body("Table.SSEDescription.KMSMasterKeyArn", nullValue());
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "Aes256SseTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableWithInvalidSseTypeFailsValidation() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "InvalidSseTypeTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST",
|
||||
"SSESpecification": {"Enabled": true, "SSEType": "BOGUS"}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
|
||||
// SSEType is validated before CreateTable runs, so the table must not exist at all --
|
||||
// a real DynamoDB rejects the whole request rather than leaving a table behind.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "InvalidSseTypeTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTableWithInvalidSseTypeFailsValidation() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "UpdateInvalidSseTypeTable",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "UpdateInvalidSseTypeTable",
|
||||
"SSESpecification": {"Enabled": true, "SSEType": "BOGUS"}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
|
||||
// SSEType is validated before UpdateTable applies anything, so the table must still
|
||||
// show no SSEDescription at all -- the failed request must not have partially applied.
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "UpdateInvalidSseTypeTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Table.SSEDescription", nullValue());
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "UpdateInvalidSseTypeTable"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableWithGsiAndLsi() {
|
||||
given()
|
||||
|
||||
+49
@@ -1,11 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
@@ -13,8 +15,11 @@ import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -352,6 +357,50 @@ class DynamoDbJsonHandlerTest {
|
||||
assertNull(reasons.get(0).get("Message"), "non failed item must not have a Message field");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sseSettingsSurvivePersistentStorageReload(@TempDir Path tempDir) throws Exception {
|
||||
Path tableFile = tempDir.resolve("dynamodb-tables.json");
|
||||
TypeReference<Map<String, TableDefinition>> typeReference = new TypeReference<>() {};
|
||||
|
||||
DynamoDbService persistentService = new DynamoDbService(new PersistentStorage<>(tableFile, typeReference));
|
||||
DynamoDbJsonHandler persistentHandler = new DynamoDbJsonHandler(persistentService, null, null, mapper);
|
||||
|
||||
ObjectNode createRequest = mapper.createObjectNode();
|
||||
createRequest.put("TableName", "PersistedSseTable");
|
||||
ObjectNode keySchemaElement = mapper.createObjectNode();
|
||||
keySchemaElement.put("AttributeName", "pk");
|
||||
keySchemaElement.put("KeyType", "HASH");
|
||||
createRequest.putArray("KeySchema").add(keySchemaElement);
|
||||
ObjectNode attrDef = mapper.createObjectNode();
|
||||
attrDef.put("AttributeName", "pk");
|
||||
attrDef.put("AttributeType", "S");
|
||||
createRequest.putArray("AttributeDefinitions").add(attrDef);
|
||||
createRequest.put("BillingMode", "PAY_PER_REQUEST");
|
||||
|
||||
persistentHandler.handle("CreateTable", createRequest, "us-east-1");
|
||||
|
||||
ObjectNode updateRequest = mapper.createObjectNode();
|
||||
updateRequest.put("TableName", "PersistedSseTable");
|
||||
ObjectNode sseSpec = updateRequest.putObject("SSESpecification");
|
||||
sseSpec.put("Enabled", true);
|
||||
sseSpec.put("SSEType", "KMS");
|
||||
sseSpec.put("KMSMasterKeyId", "arn:aws:kms:us-east-1:000000000000:key/reload-test-key");
|
||||
|
||||
persistentHandler.handle("UpdateTable", updateRequest, "us-east-1");
|
||||
|
||||
// Simulate a process restart: a brand new storage backend instance reading the same file,
|
||||
// independent of the in-memory table object mutated above.
|
||||
PersistentStorage<String, TableDefinition> reloadedStorage = new PersistentStorage<>(tableFile, typeReference);
|
||||
reloadedStorage.load();
|
||||
DynamoDbService reloadedService = new DynamoDbService(reloadedStorage);
|
||||
|
||||
TableDefinition reloadedTable = reloadedService.describeTable("PersistedSseTable", "us-east-1");
|
||||
assertTrue(reloadedTable.isSseEnabled(), "SSE must survive a storage reload");
|
||||
assertEquals("KMS", reloadedTable.getSseType());
|
||||
assertEquals("arn:aws:kms:us-east-1:000000000000:key/reload-test-key",
|
||||
reloadedTable.getKmsMasterKeyArn());
|
||||
}
|
||||
|
||||
// PutResourcePolicy/GetResourcePolicy/DeleteResourcePolicy previously fell through to the
|
||||
// default 400 UnknownOperationException branch, which blocks the Terraform provider's
|
||||
// aws_dynamodb_resource_policy resource at apply time.
|
||||
|
||||
Reference in new issue
Block a user