diff --git a/docs/services/iam.md b/docs/services/iam.md index be1de5c920..c9ddea455e 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3199,8 +3199,9 @@ floci populates: workgroups and snapshots, DynamoDB for its tables and their indexes (the table's tags), ECR for its repositories, Redshift for its clusters, snapshots, parameter groups, subnet groups, snapshot copy grants, snapshot schedules, event subscriptions and integrations, SSM for its parameters, documents and associations, - EMR for its clusters, and CloudFront for every resource it tags, by its ARN. Tag keys differing only in case are one - condition key holding every value. + EMR for its clusters, CloudFront for every resource it tags, by its ARN, and IAM Identity Center for its instances, + permission sets, applications and trusted token issuers. 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 60b2059d9e..0aec8c3ceb 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -23,10 +23,11 @@ import java.util.Optional; * connections and API destinations; Application Auto Scaling, for its scalable targets; Redshift Data, for the * Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment Cryptography, for * the key an alias names; 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; Resource Explorer, for its - * default view; OAM and RAM, for the sinks, invitations and shares of other accounts they act on; 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; + * 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; 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 * or a change set; S3 Tables, for the table a request names by its table bucket, namespace and name, and the tags of a * table bucket or a table; Lambda, for the layer version a layer version ARN selects, the image of a MicroVM and the diff --git a/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java b/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java index 71972b4581..a3fe5dafb1 100644 --- a/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java +++ b/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java @@ -1963,6 +1963,31 @@ public class SsoAdminService implements Resettable, StoredResourceArns { return Optional.empty(); } + /** + * The tags held on the instance, permission set, application or trusted token issuer {@code arn} names, as + * ListTagsForResource reads them: the tags a tagging call left on it, else the resource's own; read in place where + * the handlers find these resources, by their whole ARN. Empty for an ARN naming nothing stored. + */ + @Override + public Map storedTags(String arn) { + Map tags; + try { + TaggableResource resource = resolveTaggableResource(arn); + tags = resourceTagOverrides.get(arn).orElseGet(resource::tags); + } catch (AwsException notStored) { + return Map.of(); + } + Map held = new LinkedHashMap<>(); + if (tags != null) { + tags.forEach((key, value) -> { + if (key != null && value != null) { + held.put(key, value); + } + }); + } + return held; + } + private void ensurePermissionSetProvisioned(String permissionSetArn, String accountId) { String key = permissionSetArn + "::" + accountId; PermissionSet permissionSet = permissionSetForPortal(permissionSetArn); diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java index f8df5b560c..5b085ce6b3 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java @@ -1809,6 +1809,34 @@ class IamRequestResourceEnforcementIntegrationTest { .body(containsString("InstanceGroups must contain at least one instance group.")); } + /** + * An {@code aws:ResourceTag} condition reads the tags IAM Identity Center holds on the permission set a request + * names, so an allow on one team's tag lets the caller describe that team's permission set and not another's. + */ + @Test + void anIdentityCenterResourceTagConditionReadsTheTagsOfThePermissionSet() { + String instance = ssoInstance(); + String tagged = taggedPermissionSet(instance, "data"); + String other = taggedPermissionSet(instance, "web"); + + User user = user("res-sso-tags"); + user.policy(allowOn("sso:DescribePermissionSet", instance) + ",{\"Effect\":\"Allow\"," + + "\"Action\":\"sso:DescribePermissionSet\",\"Resource\":\"*\"," + + "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}"); + String describe = "{\"InstanceArn\":\"" + instance + "\",\"PermissionSetArn\":\""; + ssoAdmin(user, "DescribePermissionSet", describe + tagged + "\"}").statusCode(200); + refusedJson(ssoAdmin(user, "DescribePermissionSet", describe + other + "\"}"), "sso:DescribePermissionSet"); + } + + /** A permission set of the instance, tagged with the team by an administrator's TagResource. */ + private String taggedPermissionSet(String instance, String team) { + String permissionSet = permissionSet(instance); + adminJson("sso", "SWBExternalService.TagResource").body("{\"InstanceArn\":\"" + instance + + "\",\"ResourceArn\":\"" + permissionSet + "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"" + team + + "\"}]}").post("/").then().statusCode(200); + return permissionSet; + } + /** * An IAM Identity Center account assignment is checked on the account its {@code TargetId} names beside the * instance and the permission set, so a grant scoped to one account does not reach another, and a deny on an diff --git a/src/test/java/io/floci/aws/services/ssoadmin/SsoAdminServiceTest.java b/src/test/java/io/floci/aws/services/ssoadmin/SsoAdminServiceTest.java index d69815f44d..aaebfb7010 100644 --- a/src/test/java/io/floci/aws/services/ssoadmin/SsoAdminServiceTest.java +++ b/src/test/java/io/floci/aws/services/ssoadmin/SsoAdminServiceTest.java @@ -1334,6 +1334,31 @@ class SsoAdminServiceTest { assertError("ServiceQuotaExceededException", () -> service.addRegion(overQuota)); } + /** + * IAM's tag lookup reads a resource's tags as ListTagsForResource does: its own until a tagging call changes them, + * then the changed set; nothing for an ARN naming no stored resource or one that is not an Identity Center ARN. + */ + @Test + void storedTagsAreTheTagsListTagsForResourceGives() { + ObjectNode create = mapper.createObjectNode(); + create.put("InstanceArn", service.getInstanceArn()); + create.put("Name", "StoredTagAdmins"); + create.putArray("Tags").addObject().put("Key", "team").put("Value", "data"); + PermissionSet permissionSet = service.createPermissionSet(create); + + assertEquals(Map.of("team", "data"), service.storedTags(permissionSet.arn())); + + ObjectNode tag = mapper.createObjectNode(); + tag.put("ResourceArn", permissionSet.arn()); + tag.putArray("Tags").addObject().put("Key", "env").put("Value", "prod"); + service.tagResource(tag); + + assertEquals(Map.of("team", "data", "env", "prod"), service.storedTags(permissionSet.arn())); + String unknown = permissionSet.arn().substring(0, permissionSet.arn().length() - 16) + "0000000000000000"; + assertEquals(Map.of(), service.storedTags(unknown)); + assertEquals(Map.of(), service.storedTags("arn:aws:s3:::bucket")); + } + @Test void listTagsForResourceReturnsPermissionSetTagsAndValidatesContext() { ObjectNode create = mapper.createObjectNode();