diff --git a/docs/services/identitystore.md b/docs/services/identitystore.md index e6f51f2684..7a64dac4ac 100644 --- a/docs/services/identitystore.md +++ b/docs/services/identitystore.md @@ -44,7 +44,7 @@ Deleting a user or group removes its related local group memberships so subseque With IAM enforcement on, each action is checked on the resources the AWS Service Authorization Reference lists for it, read from the request: the identity store its `IdentityStoreId` names, `arn:aws:identitystore:::identitystore/`, and the group, user or membership its `GroupId`, `UserId`, `MemberId.UserId` or `MembershipId` names, `arn:aws:identitystore:::group/` with no Region or account. The list actions are also checked on every group, user or membership (`arn:aws:identitystore:::user/*`), as the reference gives them. A type the reference marks an action as requiring that the request does not name is the one Identity Store finds from what it holds: `DescribeGroupMembership` and `DeleteGroupMembership` are also checked on the group and the user of the membership their `MembershipId` names, `GetGroupMembershipId` on the membership of its group and member, and `GetGroupId` and `GetUserId` on the group or user their `AlternateIdentifier` names; a lookup that finds nothing is checked on every resource of its type (`arn:aws:identitystore:::user/*`). -An identity store an IAM Identity Center instance holds belongs to that instance's account, as the IAM Identity Center User Guide describes: that account, and a delegated administrator for IAM Identity Center in an organization instance's organization, use it in full; another account of that organization can only read it ("Member accounts have access to Read actions on both the sso-directory and identitystore namespaces"), and a `Create`, `Update` or `Delete` action from it is refused with `AccessDeniedException`; any other account gets `ResourceNotFoundException` with `ResourceType` `IDENTITY_STORE`. A store no instance holds can be used by any account. +An identity store an IAM Identity Center instance holds belongs to that instance's account, as the IAM Identity Center User Guide describes: that account, and a delegated administrator for IAM Identity Center in an organization instance's organization, use it in full; another account of that organization can only read it ("Member accounts have access to Read actions on both the sso-directory and identitystore namespaces"), and a `Create`, `Update` or `Delete` action from it is refused with `AccessDeniedException`; any other account gets `ResourceNotFoundException` with `ResourceType` `IDENTITY_STORE`. A store no instance holds can be used by any account. Under IAM enforcement a store an instance holds is checked as `arn:aws:identitystore:::identitystore/` in that instance's account, the account its ARN names, so another account's grant on a store of its own account does not reach it. ## IAM Identity Center SCIM diff --git a/src/main/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArns.java b/src/main/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArns.java index 2a9f1db780..eb312d5f49 100644 --- a/src/main/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArns.java +++ b/src/main/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArns.java @@ -6,6 +6,7 @@ import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.RequestResources; import io.floci.aws.core.common.StoredResourceArns; +import io.floci.aws.services.ssoadmin.SsoAdminService; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -14,10 +15,11 @@ import java.util.Optional; import java.util.stream.Stream; /** - * What IAM asks Identity Store of the resources a request names: the group and the user of the membership a - * {@code MembershipId} names, and the group, user or membership a lookup's request names by other members (an - * alternate identifier, or a group and a member), which the Service Authorization Reference requires those actions - * on. Each is found as its handler finds it. + * What IAM asks Identity Store of the resources a request names: the identity store's ARN in the account whose IAM + * Identity Center instance holds it, the group and the user of the membership a {@code MembershipId} names, and the + * group, user or membership a lookup's request names by other members (an alternate identifier, or a group and a + * member), which the Service Authorization Reference requires those actions on. Each is found as its handler finds + * it. */ @ApplicationScoped public class IdentityStoreStoredResourceArns implements StoredResourceArns { @@ -25,11 +27,14 @@ public class IdentityStoreStoredResourceArns implements StoredResourceArns { private static final ObjectMapper MAPPER = new ObjectMapper(); private final IdentityStoreService identityStore; + private final SsoAdminService ssoAdmin; private final RegionResolver regions; @Inject - public IdentityStoreStoredResourceArns(IdentityStoreService identityStore, RegionResolver regions) { + public IdentityStoreStoredResourceArns(IdentityStoreService identityStore, SsoAdminService ssoAdmin, + RegionResolver regions) { this.identityStore = identityStore; + this.ssoAdmin = ssoAdmin; this.regions = regions; } @@ -38,10 +43,20 @@ public class IdentityStoreStoredResourceArns implements StoredResourceArns { return "identitystore"; } - /** Identity Store finds its resources by the members a lookup names them by, never by one name alone. */ + /** + * An identity store's ARN in the account whose IAM Identity Center instance holds it, as every ARN names the + * account owning its resource, so another account of its organization reading it is checked on the store where it + * is; none for a store no instance holds, which stays in the caller's account. Identity Store finds its other + * resources by the members a lookup names them by, never by one name alone. + */ @Override public Optional find(String type, List names, String region) { - return Optional.empty(); + if (!"Identitystore".equals(type) || names.size() != 1 || names.getFirst() == null) { + return Optional.empty(); + } + String storeId = names.getFirst(); + return ssoAdmin.identityStoreOwner(storeId).map(owner -> RequestResources.idArn(namespace(), type, storeId, + regions.partitionForRegion(region), region, owner)); } /** 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 ed492f2a06..78f0a0e106 100644 --- a/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java +++ b/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java @@ -76,6 +76,8 @@ public class SsoAdminService implements Resettable, StoredResourceArns { private static final String IDENTITY_STORE_ID = "d-9067f2a3c1"; /** The service principal an organization registers a delegated administrator for IAM Identity Center under. */ private static final String SSO_SERVICE_PRINCIPAL = ServicePrincipals.of("sso"); + /** How many identity stores no instance holds are remembered before the record starts over. */ + private static final int UNHELD_LIMIT = 10_000; private static final Pattern INSTANCE_ARN_PATTERN = Pattern.compile("arn:" + AwsArnUtils.PARTITION_REGEX + ":sso:::instance/(?:sso)?ins-[a-zA-Z0-9-.]{16}"); private static final Pattern PERMISSION_SET_NAME = Pattern.compile("[\\w+=,.@-]+"); private static final Pattern PERMISSION_SET_ARN = Pattern.compile("arn:" + AwsArnUtils.PARTITION_REGEX + ":sso:::permissionSet/(?:sso)?ins-[a-zA-Z0-9-.]{16}/ps-[a-zA-Z0-9-./]{16}"); @@ -146,6 +148,12 @@ public class SsoAdminService implements Resettable, StoredResourceArns { * account's instance before it is used, so one a reset or a restore outdates is found again by the scan. */ private final Map identityStoreOwners = new ConcurrentHashMap<>(); + /** + * The identity stores the last look through every instance found none holding, up to {@code UNHELD_LIMIT}, so a + * request naming one again is answered without another; emptied whenever an instance is written and on reset, as + * only those make a store held that was not. + */ + private final Set unheldIdentityStores = ConcurrentHashMap.newKeySet(); private final StorageBackend accessControlAttributeConfigurations; private final StorageBackend trustedTokenIssuers; private final StorageBackend trustedTokenIssuerUpdateOverrides; @@ -282,6 +290,11 @@ public class SsoAdminService implements Resettable, StoredResourceArns { return instanceHoldingIdentityStore(identityStoreId, defaultAccountId).isPresent(); } + /** The account whose IAM Identity Center instance holds the identity store; empty for a store no instance holds. */ + public Optional identityStoreOwner(String identityStoreId) { + return instanceHoldingIdentityStore(identityStoreId, defaultAccountId).map(SsoInstance::ownerAccountId); + } + /** * How an account may use an identity store, as the IAM Identity Center User Guide gives it: the account its * instance belongs to, and a delegated administrator for IAM Identity Center in an organization instance's @@ -311,7 +324,8 @@ public class SsoAdminService implements Resettable, StoredResourceArns { /** * The instance holding the identity store, read where its account keeps its one instance: the caller's, the * default account's, or the one last found holding it ({@code identityStoreOwners}). Only a store none of those - * holds is looked for among every account's instances, which records each instance's store for the next request. + * holds is looked for among every account's instances, which records each instance's store for the next request, + * and a store none holds as such ({@code unheldIdentityStores}). */ private Optional instanceHoldingIdentityStore(String identityStoreId, String callerAccountId) { if (identityStoreId == null) { @@ -329,11 +343,22 @@ public class SsoAdminService implements Resettable, StoredResourceArns { return own; } } + if (unheldIdentityStores.contains(identityStoreId)) { + return Optional.empty(); + } identityStoreOwners.remove(identityStoreId); List all = instances instanceof AccountAwareStorageBackend accountAware ? accountAware.scanAllAccounts() : instances.scan(key -> true); all.forEach(instance -> identityStoreOwners.put(instance.identityStoreId(), instance.ownerAccountId())); - return all.stream().filter(instance -> identityStoreId.equals(instance.identityStoreId())).findFirst(); + Optional holding = all.stream() + .filter(instance -> identityStoreId.equals(instance.identityStoreId())).findFirst(); + if (holding.isEmpty()) { + if (unheldIdentityStores.size() >= UNHELD_LIMIT) { + unheldIdentityStores.clear(); + } + unheldIdentityStores.add(identityStoreId); + } + return holding; } /** @@ -351,6 +376,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns { AccountAwareStorageBackend.putInAccount(instances, ownerAccountId, ownerAccountId, new SsoInstance(bootstrapInstanceArn, IDENTITY_STORE_ID, "floci-identity-center", ownerAccountId, region, System.currentTimeMillis(), "ACTIVE", null, false, new LinkedHashMap<>())); + unheldIdentityStores.clear(); return; } // Before the seeded instance took its partition it was stored as arn:aws: in every @@ -758,6 +784,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns { SsoInstance instance = new SsoInstance(instanceArn, identityStoreId, name, callerAccountId, region, System.currentTimeMillis(), "ACTIVE", null, true, tags); instances.put(callerAccountId, instance); + unheldIdentityStores.clear(); instanceDeletionMarkers.delete(callerAccountId); instanceClientTokens.put(tokenKey, instanceArn); return instance; @@ -2730,6 +2757,8 @@ public class SsoAdminService implements Resettable, StoredResourceArns { @Override public void clear() { + identityStoreOwners.clear(); + unheldIdentityStores.clear(); permissionSets.clear(); assignments.clear(); assignmentOperations.clear(); diff --git a/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java index 0199c42d8a..6bcbc1a4f7 100644 --- a/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java +++ b/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java @@ -1996,6 +1996,32 @@ class ResourceArnBuilderTest { builder.buildResources("identitystore", "identitystore:ListUsers", ctx, "us-east-1", ACCOUNT, "aws")); } + /** + * An identity store is checked in the account whose instance holds it, which the service answers for the store an + * {@code IdentityStoreId} names, not in the caller's. + */ + @Test + void identityStoreChecksTheStoreInTheAccountItsServiceGives() { + ResourceArnBuilder reading = reading(new StoredResourceArns() { + @Override + public String namespace() { + return "identitystore"; + } + + @Override + public Optional find(String type, List names, String region) { + return "Identitystore".equals(type) + ? Optional.of("arn:aws:identitystore::111122223333:identitystore/" + names.getFirst()) + : Optional.empty(); + } + }); + setJsonBody("{\"IdentityStoreId\":\"d-1234567890\",\"UserId\":\"1234567890-u1\"}"); + assertEquals(List.of("arn:aws:identitystore::111122223333:identitystore/d-1234567890", + "arn:aws:identitystore:::user/1234567890-u1"), + reading.buildResources("identitystore", "identitystore:DescribeUser", ctx, "us-east-1", ACCOUNT, + "aws")); + } + /** * A required type an Identity Store request does not name is asked of the service: a membership's group and user * through its id, and a lookup's user by the members of the one way its alternate identifier is given, with their diff --git a/src/test/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArnsTest.java b/src/test/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArnsTest.java index 5da741ad09..2aa24197b7 100644 --- a/src/test/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArnsTest.java +++ b/src/test/java/io/floci/aws/services/identitystore/IdentityStoreStoredResourceArnsTest.java @@ -7,8 +7,10 @@ import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.services.identitystore.model.Group; import io.floci.aws.services.identitystore.model.Membership; import io.floci.aws.services.identitystore.model.User; +import io.floci.aws.services.ssoadmin.SsoAdminService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import java.util.List; import java.util.Optional; @@ -27,6 +29,7 @@ class IdentityStoreStoredResourceArnsTest { private final ObjectMapper mapper = new ObjectMapper(); private IdentityStoreService service; + private SsoAdminService ssoAdmin; private IdentityStoreStoredResourceArns stored; private Group group; private User user; @@ -37,7 +40,8 @@ class IdentityStoreStoredResourceArnsTest { service = new IdentityStoreService(AccountAwareStorageBackend.inMemory(GLOBAL_PARTITION), AccountAwareStorageBackend.inMemory(GLOBAL_PARTITION), AccountAwareStorageBackend.inMemory(GLOBAL_PARTITION), mapper); - stored = new IdentityStoreStoredResourceArns(service, new RegionResolver(REGION, GLOBAL_PARTITION)); + ssoAdmin = Mockito.mock(SsoAdminService.class); + stored = new IdentityStoreStoredResourceArns(service, ssoAdmin, new RegionResolver(REGION, GLOBAL_PARTITION)); group = service.createGroup(mapper.createObjectNode().put("IdentityStoreId", STORE) .put("DisplayName", "Admins")); user = service.createUser(mapper.createObjectNode().put("IdentityStoreId", STORE).put("UserName", "alice") @@ -73,6 +77,16 @@ class IdentityStoreStoredResourceArnsTest { List.of(STORE, "displayName", "Admins"), REGION)); } + @Test + void anIdentityStoreIsTheOneInTheAccountWhoseInstanceHoldsIt() { + Mockito.when(ssoAdmin.identityStoreOwner(STORE)).thenReturn(Optional.of("111122223333")); + assertEquals(Optional.of("arn:aws:identitystore::111122223333:identitystore/" + STORE), + stored.find("Identitystore", List.of(STORE), REGION)); + // A store no instance holds is not answered, so it stays in the caller's account. + Mockito.when(ssoAdmin.identityStoreOwner("d-0000000000")).thenReturn(Optional.empty()); + assertEquals(Optional.empty(), stored.find("Identitystore", List.of("d-0000000000"), REGION)); + } + @Test void aLookupFindingNothingFindsNone() { assertEquals(Optional.empty(), stored.findByMembers("User", List.of("IdentityStoreId", 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 fb70862c52..b58ed2f1d5 100644 --- a/src/test/java/io/floci/aws/services/ssoadmin/SsoAdminServiceTest.java +++ b/src/test/java/io/floci/aws/services/ssoadmin/SsoAdminServiceTest.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.function.Predicate; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -61,6 +62,7 @@ class SsoAdminServiceTest { private InMemoryStorage applicationAuthenticationMethods; private InMemoryStorage applicationGrants; private InMemoryStorage instances; + private int instanceScans; @BeforeEach void setUp() { @@ -70,7 +72,13 @@ class SsoAdminServiceTest { applicationAccessScopes = new InMemoryStorage<>(); applicationAuthenticationMethods = new InMemoryStorage<>(); applicationGrants = new InMemoryStorage<>(); - instances = new InMemoryStorage<>(); + instances = new InMemoryStorage<>() { + @Override + public List scan(Predicate keyFilter) { + instanceScans++; + return super.scan(keyFilter); + } + }; service = new SsoAdminService( new InMemoryStorage(), new InMemoryStorage(), @@ -2079,6 +2087,8 @@ class SsoAdminServiceTest { assertEquals(IdentityStoreAccess.NONE, service.identityStoreAccess(store, "410987654321")); // A store no instance holds is not IAM Identity Center's: any account uses it. assertEquals(IdentityStoreAccess.FULL, service.identityStoreAccess("d-1234567890", "410987654321")); + assertEquals(Optional.of(ACCOUNT_ID), service.identityStoreOwner(store)); + assertEquals(Optional.empty(), service.identityStoreOwner("d-1234567890")); } @Test @@ -2095,6 +2105,21 @@ class SsoAdminServiceTest { assertEquals(IdentityStoreAccess.FULL, service.identityStoreAccess("d-5555555555", ACCOUNT_ID)); } + @Test + void aStoreNoInstanceHoldsIsLookedForOnceUntilAnInstanceIsWritten() { + int before = instanceScans; + assertEquals(IdentityStoreAccess.FULL, service.identityStoreAccess("d-0000000000", "410987654321")); + assertEquals(IdentityStoreAccess.FULL, service.identityStoreAccess("d-0000000000", "410987654321")); + assertEquals(Optional.empty(), service.identityStoreOwner("d-0000000000")); + assertEquals(before + 1, instanceScans); + // A reset forgets it, so a store an instance comes to hold is found. + service.clear(); + String owner = "510987654321"; + instances.put(owner, new SsoInstance("arn:aws:sso:::instance/ssoins-2222222222222222", "d-0000000000", + "account-instance", owner, "us-east-1", 0L, "ACTIVE", null, true, new LinkedHashMap<>())); + assertEquals(Optional.of(owner), service.identityStoreOwner("d-0000000000")); + } + private static OrganizationAccount account(String id) { OrganizationAccount account = new OrganizationAccount(); account.setId(id);