diff --git a/docs/services/iam.md b/docs/services/iam.md index 0f3d599c0f..becced166c 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3201,7 +3201,8 @@ floci populates: snapshot schedules, event subscriptions and integrations, SSM for its parameters, documents and associations, EMR for its clusters, CloudFront for every resource it tags, by its ARN, IAM Identity Center for its instances, permission sets, applications and trusted token issuers, OAM for its sinks and links, RAM for its resource shares, - and Payment Cryptography for its keys. Tag keys differing only in case are one condition key holding every value. + Payment Cryptography for its keys, and Resource Explorer for its indexes and views. 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 c27d2683dc..92ab38ae78 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -25,9 +25,9 @@ import java.util.Optional; * the key an alias names and the tags of a key; EMR, for the clusters of its instance groups and a tag call's * resource and the tags of a cluster; IAM Identity Center, for the accounts its permission sets are provisioned to * and the tags of an instance, - * a permission set, an application or a trusted token issuer; Resource Explorer, for its default view; OAM and RAM, - * for the sinks, invitations and shares of other accounts they act on, OAM for the tags of a sink or a link, and - * RAM for the tags of a resource share; + * a permission set, an application or a trusted token issuer; Resource Explorer, for its default view and the tags + * of an index or a view; OAM and RAM, for the sinks, invitations and shares of other accounts they act on, OAM for + * the tags of a sink or a link, and RAM for the tags of a resource share; * Secrets Manager, for the secret a name or an ARN names, random suffix included, and its tags; EventBridge * Scheduler, for its default schedule group and its groups' tags; * CloudFormation, for the stack or the stack set a name names, whose id it minted, and the tags of a stack, a stack set diff --git a/src/main/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2Service.java b/src/main/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2Service.java index b9e7b4ca90..a04162871e 100644 --- a/src/main/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2Service.java +++ b/src/main/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2Service.java @@ -11,6 +11,7 @@ import io.floci.aws.core.common.StoredResourceArns; import io.floci.aws.core.resource.ExplorerResource; import io.floci.aws.core.resource.ResourceProvider; import io.floci.aws.core.resource.SupportedResourceType; +import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.InMemoryStorage; import io.floci.aws.core.storage.StorageBackend; import io.floci.aws.core.storage.StorageFactory; @@ -494,6 +495,35 @@ public class ResourceExplorer2Service implements StoredResourceArns { return Optional.empty(); } + /** + * The tags held on the index or view stored under {@code arn}, in the account it names, read in place: both are + * kept by their whole ARN, so the ARN is matched whole, and each is an immutable record that tagging replaces. + * Empty for an ARN naming neither. + */ + @Override + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "resource-explorer-2")) { + return Map.of(); + } + String account = AwsArnUtils.parse(arn).accountId(); + if (account.isEmpty()) { + return Map.of(); + } + Map tags = AccountAwareStorageBackend.readInAccount(indexStore, account, arn) + .map(Taggable::tags) + .or(() -> AccountAwareStorageBackend.readInAccount(viewStore, account, arn).map(Taggable::tags)) + .orElse(null); + Map held = new LinkedHashMap<>(); + if (tags != null) { + tags.forEach((key, value) -> { + if (key != null && value != null) { + held.put(key, value); + } + }); + } + return held; + } + /** * The Region's default view, which {@code Search} and {@code ListResources} use for a request naming no view, read * without writing anything, since IAM asks before it decides: the one stored, or, where such a request first @@ -1014,7 +1044,8 @@ public class ResourceExplorer2Service implements StoredResourceArns { mutateTags(arn, current -> tagKeys.forEach(current::remove)); } - private void mutateTags(String arn, Consumer> mutation) { + /** One tag change at a time, so two on one resource each keep the other's tags. */ + private synchronized void mutateTags(String arn, Consumer> mutation) { StoredTaggable stored = resolveTaggable(arn); Map updated = stored.entity().tags() != null ? new HashMap<>(stored.entity().tags()) : new HashMap<>(); 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 e0adbab681..b9cd2d02f4 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -1145,6 +1145,32 @@ class IamRestRouteEnforcementIntegrationTest { return sink; } + /** + * An {@code aws:ResourceTag} condition reads the tags Resource Explorer holds on the view a request names, so an + * allow on one team's tag lets the caller read that team's view and not another's. + */ + @Test + void aResourceExplorerResourceTagConditionReadsTheTagsOfTheView() { + String tagged = taggedView("data"); + String other = taggedView("web"); + String user = user("route-view-tags"); + + policy(user, onTeam("Allow", "resource-explorer-2:GetView", "data")); + request(user, "resource-explorer-2", "POST", "/GetView", "{\"ViewArn\":\"" + tagged + "\"}").statusCode(200); + refused(request(user, "resource-explorer-2", "POST", "/GetView", "{\"ViewArn\":\"" + other + "\"}"), + "resource-explorer-2:GetView"); + } + + /** The ARN of a Resource Explorer view an administrator creates tagged with the team, deleted after the test. */ + private String taggedView(String team) { + String view = adminOf("resource-explorer-2").body("{\"ViewName\":\"route-view-" + suffix() + + "\",\"Tags\":{\"team\":\"" + team + "\"}}").post("/CreateView") + .then().statusCode(200).extract().path("View.ViewArn"); + cleanup.register(() -> adminOf("resource-explorer-2").body("{\"ViewArn\":\"" + view + "\"}") + .post("/DeleteView")); + return view; + } + @Test void oamIsCheckedOnTheArnItsIdentifiersHold() { String sink = oamSink("{}"); diff --git a/src/test/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2DefaultViewLookupTest.java b/src/test/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2DefaultViewLookupTest.java index 968dbde7d6..972cc960d7 100644 --- a/src/test/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2DefaultViewLookupTest.java +++ b/src/test/java/io/floci/aws/services/resourceexplorer2/ResourceExplorer2DefaultViewLookupTest.java @@ -47,6 +47,22 @@ class ResourceExplorer2DefaultViewLookupTest { new ObjectMapper(), indexStore, viewStore, defaultViewStore, new InMemoryStorage<>(), new ResourceExplorer2RequestContext()); + /** + * IAM's tag lookup reads the tags held on the view the whole ARN names, as tagging leaves them, read in place; + * nothing for an ARN naming no stored view or index. + */ + @Test + void storedTagsAreTheTagsOfTheViewTheArnNames() { + String view = service.createView(REGION, "tagged", null, null, List.of(), Map.of("team", "data")).viewArn(); + service.tagResource(view, Map.of("env", "prod")); + service.untagResource(view, List.of("team")); + + assertEquals(Map.of("env", "prod"), service.storedTags(view)); + assertEquals(Map.of(), service.storedTags(VIEWS + "absent/1")); + assertEquals(Map.of(), service.storedTags("arn:aws:s3:::bucket")); + assertTrue(indexStore.scan(key -> true).isEmpty()); + } + @Test void theDefaultViewIsReadWithoutProvisioningTheBaseline() { // No index yet: a request naming no view would provision the baseline, whose view's id is not minted yet.