diff --git a/docs/services/iam.md b/docs/services/iam.md index 41e487bd69..4b5d8d3dd8 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3203,8 +3203,8 @@ floci populates: permission sets, applications and trusted token issuers, OAM for its sinks and links, RAM for its resource shares, Payment Cryptography for its keys, Resource Explorer for its indexes and views, Application Auto Scaling for its scalable targets, ECS for its clusters, task definitions, services, capacity providers, tasks and container - instances, ACM for its certificates, Firehose for its delivery streams, and Cognito for its user pools. Tag keys - differing only in case are one condition key holding every value. + instances, ACM for its certificates, Firehose for its delivery streams, Cognito for its user pools, and OpenSearch + for its domains. Tag keys differing only in case are one condition key holding every value. - EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and `ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the key names, an ARN for a key the Service Reference types as one. `resourceKeys` in diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java index 6649fc8c50..efaad4ece8 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -45,9 +45,9 @@ import java.util.Optional; * key value store a name names and the tags of any resource it tags; ECS, for the revision a task definition * reference resolves to, its default cluster and the tags of a cluster, a task definition, a service, a capacity * provider, a task or a container instance; ACM, for the tags of a certificate; Firehose, for the tags of a delivery - * stream; Cognito, for the tags of a user pool); looked up lazily by {@code ResourceArnBuilder} and - * {@code IamConditionContextResolver} through {@code Instance} for the namespace a request or an - * ARN is of, so IAM never depends on that service directly.

+ * stream; Cognito, for the tags of a user pool; OpenSearch, for the tags of a domain); looked up lazily by + * {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance} for + * the namespace a request or an ARN is of, so IAM never depends on that service directly.

* *

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 diff --git a/src/main/java/io/floci/aws/services/opensearch/OpenSearchService.java b/src/main/java/io/floci/aws/services/opensearch/OpenSearchService.java index e5c656888d..d7f7d24ff3 100644 --- a/src/main/java/io/floci/aws/services/opensearch/OpenSearchService.java +++ b/src/main/java/io/floci/aws/services/opensearch/OpenSearchService.java @@ -6,6 +6,7 @@ import io.floci.aws.core.common.AwsArnUtils; import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.AwsRegions; import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.core.common.StoredResourceArns; import io.floci.aws.core.common.docker.ContainerStorageHelper; import io.floci.aws.core.resource.ExplorerResource; import io.floci.aws.core.resource.ResourceProvider; @@ -33,6 +34,7 @@ import java.security.SecureRandom; import java.time.Instant; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -50,7 +52,7 @@ import java.util.concurrent.TimeUnit; * region from the domain's ARN, which has always carried it. */ @ApplicationScoped -public class OpenSearchService implements ResourceProvider { +public class OpenSearchService implements ResourceProvider, StoredResourceArns { private static final Logger LOG = Logger.getLogger(OpenSearchService.class); @@ -352,9 +354,12 @@ public class OpenSearchService implements ResourceProvider { return domain; } - public void addTags(String arn, Map tags) { + /** Gives the domain a new map, its tags with {@code tags} added, one tag call at a time. */ + public synchronized void addTags(String arn, Map tags) { Domain domain = findByArn(arn); - domain.getTags().putAll(tags); + Map merged = new HashMap<>(domain.getTags()); + merged.putAll(tags); + domain.setTags(merged); putDomain(domain); } @@ -362,12 +367,55 @@ public class OpenSearchService implements ResourceProvider { return findByArn(arn).getTags(); } - public void removeTags(String arn, List tagKeys) { + /** Gives the domain a new map, its tags without {@code tagKeys}, one tag call at a time. */ + public synchronized void removeTags(String arn, List tagKeys) { Domain domain = findByArn(arn); - tagKeys.forEach(domain.getTags()::remove); + Map kept = new HashMap<>(domain.getTags()); + tagKeys.forEach(kept::remove); + domain.setTags(kept); putDomain(domain); } + @Override + public String namespace() { + return "es"; + } + + /** A request names a domain by its name, which its ARN carries whole, so there is nothing to find. */ + @Override + public Optional find(String type, List names, String region) { + return Optional.empty(); + } + + /** + * The tags of the domain whose ARN is {@code arn}, in the request's account, where the tag handlers read: an ARN of + * another account names none. Read in place under the key the ARN gives, or the name a domain was stored under + * before its key named the Region, a record from before storage was account-aware included, as {@link #findByArn} + * finds it, and matched on the whole ARN. A tag call stores a new map, so the read is of one whole set. + */ + @Override + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "es")) { + return Map.of(); + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + String account = parsed.accountId(); + if (!account.equals(regionResolver.getAccountId()) || !parsed.resource().startsWith("domain/")) { + return Map.of(); + } + String domainName = parsed.resource().substring("domain/".length()); + Map tags = new LinkedHashMap<>(); + AccountAwareStorageBackend.readInAccount(domainStore, account, storageKey(parsed.region(), domainName)) + .or(() -> AccountAwareStorageBackend.readInAccount(domainStore, account, domainName)) + .filter(domain -> arn.equals(domain.getArn())) + .ifPresent(domain -> domain.getTags().forEach((key, value) -> { + if (key != null && value != null) { + tags.put(key, value); + } + })); + return tags; + } + public Domain upgradeDomain(String domainName, String targetVersion, String region) { Domain domain = describeDomain(domainName, region); if (targetVersion == null || targetVersion.isBlank()) { diff --git a/src/main/java/io/floci/aws/services/opensearch/model/Domain.java b/src/main/java/io/floci/aws/services/opensearch/model/Domain.java index b8361d135a..3712b7287f 100644 --- a/src/main/java/io/floci/aws/services/opensearch/model/Domain.java +++ b/src/main/java/io/floci/aws/services/opensearch/model/Domain.java @@ -81,7 +81,7 @@ public class Domain { private String endpoint = ""; @JsonProperty("Tags") - private Map tags = new HashMap<>(); + private volatile Map tags = new HashMap<>(); @JsonProperty("ContainerId") private String containerId; diff --git a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java index e64969597b..4c76fea248 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -813,6 +813,35 @@ class IamRestRouteEnforcementIntegrationTest { as(user, "es").get("/2021-01-01/opensearch/domain/other-" + domain).then().statusCode(409); } + /** + * An {@code aws:ResourceTag} condition reads the tags OpenSearch holds on the domain a request names in its path, + * as a tag call leaves them, so an allow on one team's tag lets the caller describe that team's domain and not + * another's. + */ + @Test + void anOpenSearchResourceTagConditionReadsTheTagsOfTheDomain() { + String tagged = openSearchDomain("web"); + String other = openSearchDomain("web"); + adminOf("es").body("{\"ARN\":\"arn:aws:es:us-east-1:" + ACCOUNT + ":domain/" + tagged + + "\",\"TagList\":[{\"Key\":\"team\",\"Value\":\"data\"}]}").post("/2021-01-01/tags").then() + .statusCode(200); + String user = user("route-domain-tags"); + + policy(user, onTeam("Allow", "es:DescribeDomain", "data")); + request(user, "es", "GET", "/2021-01-01/opensearch/domain/" + tagged, null).statusCode(200); + refused(request(user, "es", "GET", "/2021-01-01/opensearch/domain/" + other, null), "es:DescribeDomain"); + } + + /** The name of an OpenSearch domain an administrator creates tagged with the team, deleted after the test. */ + private String openSearchDomain(String team) { + String name = "route-tags-" + suffix(); + adminOf("es").body("{\"DomainName\":\"" + name + "\",\"EngineVersion\":\"OpenSearch_2.11\",\"TagList\":" + + "[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}").post("/2021-01-01/opensearch/domain").then() + .statusCode(200); + cleanup.register(() -> adminOf("es").delete("/2021-01-01/opensearch/domain/" + name)); + return name; + } + /** * An Access Analyzer request naming its analyzer, in its path or its body, is checked on that analyzer, the ARN * Access Analyzer gives it: a caller with no grant is refused, a grant scoped to the analyzer lets diff --git a/src/test/java/io/floci/aws/services/opensearch/OpenSearchServiceTest.java b/src/test/java/io/floci/aws/services/opensearch/OpenSearchServiceTest.java index 5bfaa3f646..d9af68fe76 100644 --- a/src/test/java/io/floci/aws/services/opensearch/OpenSearchServiceTest.java +++ b/src/test/java/io/floci/aws/services/opensearch/OpenSearchServiceTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; @@ -215,6 +216,37 @@ class OpenSearchServiceTest { assertEquals(EU_WEST_1, service.describeDomain("dual-region", EU_WEST_1).getRegion()); } + /** + * IAM's tag lookup reads the tags of the domain whose ARN it is given, as tag calls leave them, a tag call giving + * the domain a new map rather than changing the one a reader holds, and those of one stored under its name from + * before keys named the Region, in place, without migrating it; nothing for an ARN of another account or Region, or + * of no domain. + */ + @Test + void storedTagsAreTheTagsOfTheDomainTheArnNamesAndTaggingPutsANewMap() { + when(domainManager.tryStartDomain(any())).thenReturn(false); + String arn = service.createDomain("tagged", "OpenSearch_2.11", null, null, Map.of("env", "dev"), US_EAST_1) + .getArn(); + Map held = service.listTags(arn); + service.addTags(arn, Map.of("team", "data")); + service.removeTags(arn, List.of("env")); + String legacyArn = AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/legacy").toString(); + Domain legacy = new Domain(); + legacy.setDomainName("legacy"); + legacy.setArn(legacyArn); + legacy.setTags(Map.of("tier", "web")); + domainStore.putForAccount(ACCOUNT, "legacy", legacy); + + assertEquals(Map.of("env", "dev"), held); + assertEquals(Map.of("team", "data"), service.storedTags(arn)); + assertEquals(Map.of("tier", "web"), service.storedTags(legacyArn)); + assertTrue(domainStore.getForAccount(ACCOUNT, "legacy").isPresent()); + assertEquals(Map.of(), service.storedTags(arn.replace(":" + ACCOUNT + ":", ":111122223333:"))); + assertEquals(Map.of(), service.storedTags(arn.replace(":" + US_EAST_1 + ":", ":" + EU_WEST_1 + ":"))); + assertEquals(Map.of(), service.storedTags(arn + "0")); + assertEquals(Map.of(), service.storedTags("arn:aws:s3:::tagged")); + } + @Test void sameNameInTheSameRegionStillConflicts() { when(domainManager.tryStartDomain(any())).thenReturn(false);