mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): give resource-tag conditions the tags of the table a request names (#8623)
The generated request resources give DynamoDB's table, index and stream types aws:ResourceTag/ keys, but DynamoDB had no StoredResourceArns, so a condition on a table's tags saw none. DynamoDbStoredResourceArns now answers from the selected backend: the native one reads the table stored under the ARN in the account and Region it names, a pre-multi-account one of the default account in place, matched on the whole ARN; the DynamoDB Local one reads the tags Floci records for it while DynamoDB Local still has the table. An index ARN gets its table's tags when the table holds that index; a stream ARN gets none. Native tag writes, CreateTable's included, now replace the map whole under one lock, keeping tags already put, and the field is volatile.
This commit is contained in:
1 parent
9de6cd74b8
commit
3d6f59c064
14 files changed
+272
-21
No files matched your search
@@ -33,10 +33,10 @@ import java.util.Optional;
|
||||
* network connector; Glue, for the tags of a resource it tags; Step Functions, for the tags of a state machine, its
|
||||
* versions and executions, and an activity; CodeBuild, for the project a build belongs to and the tags of a project or
|
||||
* a report group; Athena, for the workgroup and the data catalog a request naming none uses and the tags of a
|
||||
* workgroup or a data catalog; Redshift Serverless, for the tags of a namespace, a workgroup or a snapshot); looked up
|
||||
* lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
* workgroup or a data catalog; Redshift Serverless, for the tags of a namespace, a workgroup or a snapshot; DynamoDB,
|
||||
* for the tags of a table and its indexes); looked up lazily by {@code ResourceArnBuilder} and
|
||||
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
|
||||
* ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -138,6 +138,9 @@ public class DynamoDbService {
|
||||
private final Object capacityLock = new Object();
|
||||
// Held from a resource policy revision check until the policy it allowed is stored.
|
||||
private final Object resourcePolicyLock = new Object();
|
||||
// Held while a table's tags are copied, changed and put in place of the map it held, which is never changed. The
|
||||
// copy is put one entry at a time, as the map always was, so ListTagsOfResource keeps the order it listed.
|
||||
private final Object tagLock = new Object();
|
||||
// ClientRequestToken idempotency for TransactWriteItems. AWS retains tokens for
|
||||
// ~10 minutes; floci uses the same window. The cache entry stores a hash of the
|
||||
// request body so a replay with the same token but different parameters can be
|
||||
@@ -3427,24 +3430,46 @@ public class DynamoDbService {
|
||||
// --- Tag Operations ---
|
||||
|
||||
public void tagResource(String resourceArn, Map<String, String> tags, String region) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
if (table.getTags() == null) {
|
||||
table.setTags(new HashMap<>());
|
||||
synchronized (tagLock) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
Map<String, String> updated = new HashMap<>();
|
||||
if (table.getTags() != null) {
|
||||
table.getTags().forEach(updated::put);
|
||||
}
|
||||
tags.forEach(updated::put);
|
||||
table.setTags(updated);
|
||||
String storageKey = regionKey(region, table.getTableName());
|
||||
tableStore.put(storageKey, table);
|
||||
}
|
||||
table.getTags().putAll(tags);
|
||||
String storageKey = regionKey(region, table.getTableName());
|
||||
tableStore.put(storageKey, table);
|
||||
LOG.debugv("Tagged resource: {0}", resourceArn);
|
||||
}
|
||||
|
||||
public void untagResource(String resourceArn, List<String> tagKeys, String region) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
if (table.getTags() != null) {
|
||||
for (String key : tagKeys) {
|
||||
table.getTags().remove(key);
|
||||
/**
|
||||
* Puts CreateTable's tags on the table it has just stored, under the lock TagResource takes, keeping a tag a
|
||||
* TagResource already put on it in the meantime, as that came later.
|
||||
*/
|
||||
void putCreationTags(TableDefinition table, Map<String, String> tags) {
|
||||
synchronized (tagLock) {
|
||||
Map<String, String> created = new HashMap<>();
|
||||
tags.forEach(created::put);
|
||||
if (table.getTags() != null) {
|
||||
table.getTags().forEach(created::put);
|
||||
}
|
||||
table.setTags(created);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagResource(String resourceArn, List<String> tagKeys, String region) {
|
||||
synchronized (tagLock) {
|
||||
TableDefinition table = findTableByArn(resourceArn, region);
|
||||
if (table.getTags() != null) {
|
||||
Map<String, String> updated = new HashMap<>();
|
||||
table.getTags().forEach(updated::put);
|
||||
tagKeys.forEach(updated::remove);
|
||||
table.setTags(updated);
|
||||
String storageKey = regionKey(region, table.getTableName());
|
||||
tableStore.put(storageKey, table);
|
||||
}
|
||||
String storageKey = regionKey(region, table.getTableName());
|
||||
tableStore.put(storageKey, table);
|
||||
}
|
||||
LOG.debugv("Untagged resource: {0}", resourceArn);
|
||||
}
|
||||
@@ -3454,6 +3479,33 @@ public class DynamoDbService {
|
||||
return table.getTags() != null ? table.getTags() : Map.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the table {@code arn} names, or of the table whose index it names, as an index inherits its table's
|
||||
* tags: the table stored under the ARN's name in the account and Region it names, a pre-multi-account one of the
|
||||
* default account read in place, matched on the whole ARN and, for an index, holding it. Empty for any other ARN,
|
||||
* a stream's included, whose tags are its own and which Floci holds none of.
|
||||
*/
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "dynamodb")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String[] parts = parsed.resource().split("/", -1);
|
||||
boolean index = parts.length == 4 && "index".equals(parts[2]);
|
||||
if (!"table".equals(parts[0]) || parts.length != 2 && !index) {
|
||||
return Map.of();
|
||||
}
|
||||
TableDefinition table = AccountAwareStorageBackend.readInAccount(tableStore, parsed.accountId(),
|
||||
regionKey(parsed.region(), parts[1])).orElse(null);
|
||||
if (table == null || table.getTableArn() == null
|
||||
|| !arn.equals(index ? table.getTableArn() + "/index/" + parts[3] : table.getTableArn())
|
||||
|| index && !table.holdsIndex(parts[3])) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<String, String> tags = table.getTags();
|
||||
return tags == null ? Map.of() : tags;
|
||||
}
|
||||
|
||||
/** Result of a successful GetResourcePolicy call: the raw policy document and its revision id. */
|
||||
public record ResourcePolicyResult(String policy, String revisionId) {}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.floci.aws.services.dynamodb;
|
||||
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.dynamodb.backend.DynamoDbTableAccess;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks DynamoDB of the resources a request names: the tags of the table an ARN names, or of the table whose
|
||||
* index it names, from the selected backend.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class DynamoDbStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final DynamoDbTableAccess tables;
|
||||
|
||||
@Inject
|
||||
public DynamoDbStoredResourceArns(DynamoDbTableAccess tables) {
|
||||
this.tables = tables;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "dynamodb";
|
||||
}
|
||||
|
||||
/** DynamoDB mints no id into an ARN a request names by its name, so there is nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
return tables.storedTags(arn);
|
||||
}
|
||||
}
|
||||
@@ -155,7 +155,7 @@ public class NativeDynamoDbTableService {
|
||||
|
||||
applyOnDemandThroughput(table, settings.onDemandThroughput());
|
||||
|
||||
request.tags().forEach(table.getTags()::put);
|
||||
dynamoDbService.putCreationTags(table, request.tags());
|
||||
|
||||
if (Boolean.TRUE.equals(settings.streamEnabled())) {
|
||||
enableStream(table, settings.streamViewType(), region);
|
||||
|
||||
@@ -543,6 +543,36 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags Floci records for the table {@code arn} names in the account and Region it names, matched on the whole
|
||||
* ARN, or for the table whose index it names, when DynamoDB Local describes that table, and for an index as
|
||||
* holding it, as ListTagsOfResource asks it; none for a table DynamoDB Local no longer has.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "dynamodb")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String[] parts = parsed.resource().split("/", -1);
|
||||
boolean index = parts.length == 4 && "index".equals(parts[2]);
|
||||
if (!"table".equals(parts[0]) || parts.length != 2 && !index) {
|
||||
return Map.of();
|
||||
}
|
||||
String tableArn = AwsArnUtils.Arn.of("dynamodb", parsed.region(), parsed.accountId(), "table/" + parts[1])
|
||||
.toString();
|
||||
if (!arn.equals(index ? tableArn + "/index/" + parts[3] : tableArn)) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<String, String> held = AccountAwareStorageBackend.readInAccount(tags, parsed.accountId(),
|
||||
key(parsed.region(), parts[1])).orElse(Map.of());
|
||||
if (held.isEmpty() || !findTable(new Scope(parsed.accountId(), parsed.region()), parts[1])
|
||||
.map(table -> !index || table.holdsIndex(parts[3])).orElse(false)) {
|
||||
return Map.of();
|
||||
}
|
||||
return held;
|
||||
}
|
||||
|
||||
private String existingTableKey(Scope scope, String resourceArn) {
|
||||
if (!AwsArnUtils.looksLikeArn(resourceArn)) {
|
||||
throw new AwsException("ValidationException", "Invalid TableArn", 400);
|
||||
|
||||
@@ -61,6 +61,13 @@ public interface DynamoDbTableAccess {
|
||||
|
||||
void untagResource(Scope scope, String resourceArn, List<String> tagKeys);
|
||||
|
||||
/**
|
||||
* The tags held for the table {@code arn} names, or for the table whose index it names, as an index inherits its
|
||||
* table's tags, in the account and Region the ARN names, read without changing anything; empty for an ARN naming
|
||||
* no table or index held, or any other resource.
|
||||
*/
|
||||
Map<String, String> storedTags(String arn);
|
||||
|
||||
TableDefinition applyReplicaUpdates(Scope scope, String tableName, List<String> addRegions,
|
||||
List<String> removeRegions);
|
||||
|
||||
|
||||
@@ -264,6 +264,11 @@ public class NativeDynamoDbBackend implements DynamoDbBackend {
|
||||
() -> dynamoDbService.untagResource(resourceArn, tagKeys, scope.region()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
return dynamoDbService.storedTags(arn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDefinition applyReplicaUpdates(Scope scope, String tableName, List<String> addRegions,
|
||||
List<String> removeRegions) {
|
||||
|
||||
@@ -31,7 +31,7 @@ public class TableDefinition {
|
||||
private long warmReadUnitsPerSecond;
|
||||
private long warmWriteUnitsPerSecond;
|
||||
private String tableArn;
|
||||
private Map<String, String> tags;
|
||||
private volatile Map<String, String> tags;
|
||||
private List<GlobalSecondaryIndex> globalSecondaryIndexes;
|
||||
private List<LocalSecondaryIndex> localSecondaryIndexes;
|
||||
private List<VectorIndex> vectorIndexes;
|
||||
@@ -345,6 +345,12 @@ public class TableDefinition {
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** Whether the table has a global secondary, local secondary or vector index of this name. */
|
||||
public boolean holdsIndex(String indexName) {
|
||||
return findGsi(indexName).isPresent() || findLsi(indexName).isPresent()
|
||||
|| findVectorIndex(indexName).isPresent();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public Optional<VectorIndex> findVectorIndex(String indexName) {
|
||||
if (vectorIndexes == null || vectorIndexes.isEmpty() || indexName == null) {
|
||||
|
||||
Reference in new issue
Block a user