mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(dynamodb): resource policies and GSI-level OnDemandThroughput (#2976)
PutResourcePolicy/GetResourcePolicy/DeleteResourcePolicy previously fell through to the default 400 UnknownOperationException branch, and a GSI created with an explicit OnDemandThroughput never stored the value, so DescribeTable could not report it back. Both block the Terraform provider: aws_dynamodb_resource_policy fails at apply, and an on-demand GSI shows phantom drift on every plan. Policies live on TableDefinition with an opaque RevisionId honoring ExpectedRevisionId on Put/Delete (PolicyNotFoundException on mismatch or absence); the GSI parse/emit mirrors the existing table-level OnDemandThroughput handling.
This commit is contained in:
1 parent
6c9cc95d54
commit
17e02edad9
5 files changed
+284
No files matched your search
@@ -66,6 +66,9 @@ public class DynamoDbJsonHandler {
|
||||
case "TagResource" -> handleTagResource(request, region);
|
||||
case "UntagResource" -> handleUntagResource(request, region);
|
||||
case "ListTagsOfResource" -> handleListTagsOfResource(request, region);
|
||||
case "PutResourcePolicy" -> handlePutResourcePolicy(request, region);
|
||||
case "GetResourcePolicy" -> handleGetResourcePolicy(request, region);
|
||||
case "DeleteResourcePolicy" -> handleDeleteResourcePolicy(request, region);
|
||||
case "EnableKinesisStreamingDestination" -> handleEnableKinesisStreamingDestination(request, region);
|
||||
case "DisableKinesisStreamingDestination" -> handleDisableKinesisStreamingDestination(request, region);
|
||||
case "DescribeKinesisStreamingDestination" -> handleDescribeKinesisStreamingDestination(request, region);
|
||||
@@ -132,6 +135,15 @@ public class DynamoDbJsonHandler {
|
||||
gsi.getProvisionedThroughput().setReadCapacityUnits(gsiPt.path("ReadCapacityUnits").asLong(0));
|
||||
gsi.getProvisionedThroughput().setWriteCapacityUnits(gsiPt.path("WriteCapacityUnits").asLong(0));
|
||||
}
|
||||
JsonNode gsiOnDemand = gsiNode.path("OnDemandThroughput");
|
||||
if (gsiOnDemand.isObject()) {
|
||||
if (gsiOnDemand.has("MaxReadRequestUnits")) {
|
||||
gsi.setOnDemandMaxReadRequestUnits(gsiOnDemand.get("MaxReadRequestUnits").asInt());
|
||||
}
|
||||
if (gsiOnDemand.has("MaxWriteRequestUnits")) {
|
||||
gsi.setOnDemandMaxWriteRequestUnits(gsiOnDemand.get("MaxWriteRequestUnits").asInt());
|
||||
}
|
||||
}
|
||||
gsis.add(gsi);
|
||||
}
|
||||
}
|
||||
@@ -1295,6 +1307,15 @@ public class DynamoDbJsonHandler {
|
||||
newGsi.getProvisionedThroughput().setReadCapacityUnits(newGsiPt.path("ReadCapacityUnits").asLong(0));
|
||||
newGsi.getProvisionedThroughput().setWriteCapacityUnits(newGsiPt.path("WriteCapacityUnits").asLong(0));
|
||||
}
|
||||
JsonNode newGsiOnDemand = createNode.path("OnDemandThroughput");
|
||||
if (newGsiOnDemand.isObject()) {
|
||||
if (newGsiOnDemand.has("MaxReadRequestUnits")) {
|
||||
newGsi.setOnDemandMaxReadRequestUnits(newGsiOnDemand.get("MaxReadRequestUnits").asInt());
|
||||
}
|
||||
if (newGsiOnDemand.has("MaxWriteRequestUnits")) {
|
||||
newGsi.setOnDemandMaxWriteRequestUnits(newGsiOnDemand.get("MaxWriteRequestUnits").asInt());
|
||||
}
|
||||
}
|
||||
gsiCreates.add(newGsi);
|
||||
}
|
||||
JsonNode deleteNode = update.path("Delete");
|
||||
@@ -1693,6 +1714,58 @@ public class DynamoDbJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handlePutResourcePolicy(JsonNode request, String region) {
|
||||
String resourceArn = request.path("ResourceArn").asText();
|
||||
if (!isValidDynamoDbTableArn(resourceArn)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid ResourceArn: " + resourceArn, 400);
|
||||
}
|
||||
if (!request.hasNonNull("Policy")) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value null at 'policy' failed to satisfy constraint: "
|
||||
+ "Member must not be null", 400);
|
||||
}
|
||||
String policy = request.path("Policy").asText();
|
||||
String expectedRevisionId = request.has("ExpectedRevisionId")
|
||||
? request.get("ExpectedRevisionId").asText() : null;
|
||||
|
||||
String revisionId = dynamoDbService.putResourcePolicy(resourceArn, policy, expectedRevisionId, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("RevisionId", revisionId);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleGetResourcePolicy(JsonNode request, String region) {
|
||||
String resourceArn = request.path("ResourceArn").asText();
|
||||
if (!isValidDynamoDbTableArn(resourceArn)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid ResourceArn: " + resourceArn, 400);
|
||||
}
|
||||
DynamoDbService.ResourcePolicyResult result = dynamoDbService.getResourcePolicy(resourceArn, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("Policy", result.policy());
|
||||
response.put("RevisionId", result.revisionId());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteResourcePolicy(JsonNode request, String region) {
|
||||
String resourceArn = request.path("ResourceArn").asText();
|
||||
if (!isValidDynamoDbTableArn(resourceArn)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid ResourceArn: " + resourceArn, 400);
|
||||
}
|
||||
String expectedRevisionId = request.has("ExpectedRevisionId")
|
||||
? request.get("ExpectedRevisionId").asText() : null;
|
||||
|
||||
String revisionId = dynamoDbService.deleteResourcePolicy(resourceArn, expectedRevisionId, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("RevisionId", revisionId);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleEnableKinesisStreamingDestination(JsonNode request, String region) {
|
||||
String tableName = request.path("TableName").asText();
|
||||
String streamArn = request.path("StreamArn").asText();
|
||||
@@ -2133,6 +2206,18 @@ public class DynamoDbJsonHandler {
|
||||
gsiNode.put("IndexSizeBytes", gsi.getIndexSizeBytes());
|
||||
gsiNode.put("ItemCount", gsi.getItemCount());
|
||||
|
||||
if (gsi.getOnDemandMaxReadRequestUnits() != null
|
||||
|| gsi.getOnDemandMaxWriteRequestUnits() != null) {
|
||||
ObjectNode gsiOdt = objectMapper.createObjectNode();
|
||||
if (gsi.getOnDemandMaxReadRequestUnits() != null) {
|
||||
gsiOdt.put("MaxReadRequestUnits", gsi.getOnDemandMaxReadRequestUnits());
|
||||
}
|
||||
if (gsi.getOnDemandMaxWriteRequestUnits() != null) {
|
||||
gsiOdt.put("MaxWriteRequestUnits", gsi.getOnDemandMaxWriteRequestUnits());
|
||||
}
|
||||
gsiNode.set("OnDemandThroughput", gsiOdt);
|
||||
}
|
||||
|
||||
gsiArray.add(gsiNode);
|
||||
}
|
||||
node.set("GlobalSecondaryIndexes", gsiArray);
|
||||
|
||||
@@ -1613,6 +1613,54 @@ public class DynamoDbService implements ResourceProvider {
|
||||
return table.getTags() != null ? table.getTags() : Map.of();
|
||||
}
|
||||
|
||||
/** Result of a successful GetResourcePolicy call: the raw policy document and its revision id. */
|
||||
public record ResourcePolicyResult(String policy, String revisionId) {}
|
||||
|
||||
public String putResourcePolicy(String resourceArn, String policy, String expectedRevisionId, String region) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
if (expectedRevisionId != null
|
||||
&& !expectedRevisionId.equals(table.getResourcePolicyRevisionId())) {
|
||||
throw new AwsException("PolicyNotFoundException",
|
||||
"Policy with revision id " + expectedRevisionId + " does not exist for: " + resourceArn, 400);
|
||||
}
|
||||
String revisionId = UUID.randomUUID().toString();
|
||||
table.setResourcePolicy(policy);
|
||||
table.setResourcePolicyRevisionId(revisionId);
|
||||
String storageKey = regionKey(region, table.getTableName());
|
||||
tableStore.put(storageKey, table);
|
||||
LOG.debugv("Put resource policy: {0}", resourceArn);
|
||||
return revisionId;
|
||||
}
|
||||
|
||||
public ResourcePolicyResult getResourcePolicy(String resourceArn, String region) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
if (table.getResourcePolicy() == null) {
|
||||
throw new AwsException("PolicyNotFoundException",
|
||||
"No resource policy found for: " + resourceArn, 400);
|
||||
}
|
||||
return new ResourcePolicyResult(table.getResourcePolicy(), table.getResourcePolicyRevisionId());
|
||||
}
|
||||
|
||||
public String deleteResourcePolicy(String resourceArn, String expectedRevisionId, String region) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
if (table.getResourcePolicy() == null) {
|
||||
throw new AwsException("PolicyNotFoundException",
|
||||
"No resource policy found for: " + resourceArn, 400);
|
||||
}
|
||||
if (expectedRevisionId != null
|
||||
&& !expectedRevisionId.equals(table.getResourcePolicyRevisionId())) {
|
||||
throw new AwsException("PolicyNotFoundException",
|
||||
"Policy with revision id " + expectedRevisionId + " does not exist for: " + resourceArn, 400);
|
||||
}
|
||||
String revisionId = table.getResourcePolicyRevisionId();
|
||||
table.setResourcePolicy(null);
|
||||
table.setResourcePolicyRevisionId(null);
|
||||
String storageKey = regionKey(region, table.getTableName());
|
||||
tableStore.put(storageKey, table);
|
||||
LOG.debugv("Deleted resource policy: {0}", resourceArn);
|
||||
return revisionId;
|
||||
}
|
||||
|
||||
private TableDefinition findTableByArn(String arn, String region) {
|
||||
String prefix = region + "::";
|
||||
return tableStore.scan(k -> k.startsWith(prefix)).stream()
|
||||
|
||||
+8
@@ -19,6 +19,8 @@ public class GlobalSecondaryIndex {
|
||||
private long itemCount;
|
||||
private long indexSizeBytes;
|
||||
private List<String> nonKeyAttributes;
|
||||
private Integer onDemandMaxReadRequestUnits;
|
||||
private Integer onDemandMaxWriteRequestUnits;
|
||||
|
||||
public GlobalSecondaryIndex() {
|
||||
this.provisionedThroughput = new ProvisionedThroughput(0, 0);
|
||||
@@ -64,6 +66,12 @@ public class GlobalSecondaryIndex {
|
||||
public long getIndexSizeBytes() { return indexSizeBytes; }
|
||||
public void setIndexSizeBytes(long indexSizeBytes) { this.indexSizeBytes = indexSizeBytes; }
|
||||
|
||||
public Integer getOnDemandMaxReadRequestUnits() { return onDemandMaxReadRequestUnits; }
|
||||
public void setOnDemandMaxReadRequestUnits(Integer v) { this.onDemandMaxReadRequestUnits = v; }
|
||||
|
||||
public Integer getOnDemandMaxWriteRequestUnits() { return onDemandMaxWriteRequestUnits; }
|
||||
public void setOnDemandMaxWriteRequestUnits(Integer v) { this.onDemandMaxWriteRequestUnits = v; }
|
||||
|
||||
@JsonIgnore
|
||||
public String getPartitionKeyName() {
|
||||
return keySchema.stream()
|
||||
|
||||
@@ -51,6 +51,11 @@ public class TableDefinition {
|
||||
// Replica regions for a global table (single-process emulator backs them all with this table's
|
||||
// data; the list drives the DescribeTable Replicas/GlobalTableVersion projection).
|
||||
private List<String> replicaRegions;
|
||||
// Resource-based policy attached via PutResourcePolicy (JSON policy document text), and the
|
||||
// opaque revision id AWS hands back so callers can pass ExpectedRevisionId for optimistic
|
||||
// concurrency on subsequent Put/DeleteResourcePolicy calls. Null when no policy is attached.
|
||||
private String resourcePolicy;
|
||||
private String resourcePolicyRevisionId;
|
||||
|
||||
public TableDefinition() {
|
||||
this.keySchema = new ArrayList<>();
|
||||
@@ -207,6 +212,12 @@ public class TableDefinition {
|
||||
public Integer getOnDemandMaxWriteRequestUnits() { return onDemandMaxWriteRequestUnits; }
|
||||
public void setOnDemandMaxWriteRequestUnits(Integer v) { this.onDemandMaxWriteRequestUnits = v; }
|
||||
|
||||
public String getResourcePolicy() { return resourcePolicy; }
|
||||
public void setResourcePolicy(String resourcePolicy) { this.resourcePolicy = resourcePolicy; }
|
||||
|
||||
public String getResourcePolicyRevisionId() { return resourcePolicyRevisionId; }
|
||||
public void setResourcePolicyRevisionId(String resourcePolicyRevisionId) { this.resourcePolicyRevisionId = resourcePolicyRevisionId; }
|
||||
|
||||
@JsonIgnore
|
||||
public String getPartitionKeyName() {
|
||||
return keySchema.stream()
|
||||
|
||||
Reference in new issue
Block a user