fix(identitystore): refuse an identity store to accounts outside its organization (#9346)

Identity Store checked only that an IdentityStoreId was well formed, so any
account could read and change the users, groups and memberships of any
store, the default account's IAM Identity Center store among them.

The IAM Identity Center User Guide says who may use an organization
instance's store: "Member accounts have access to Read actions on both the
sso-directory and identitystore namespaces", while adding, editing or
deleting users or groups is the management account's and a delegated
administrator's. SsoAdminService now answers how an account may use a store
from the instance that holds it: its own account, and a delegated
administrator for IAM Identity Center in an organization instance's
organization, in full; another account of that organization to read; any
other account not at all. Identity Store's handler refuses a store an
account cannot see as ResourceNotFoundException (ResourceType
IDENTITY_STORE) and a change from an account that may only read as
AccessDeniedException. A store no instance holds is used as before.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-24 10:40:42 -04:00
1 parent 0c4fa969e3
commit 49a59da5e6
6 files changed
+217 -21

No files matched your search

@@ -6,30 +6,43 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.RequestContext;
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.IdentityStoreAccess;
import io.floci.aws.services.ssoadmin.SsoAdminService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
@ApplicationScoped
public class IdentityStoreJsonHandler {
private static final String ENTERPRISE_EXTENSION = "aws:identitystore:enterprise";
/** The actions that change an identity store, which the Service Authorization Reference gives the Write level. */
private static final Pattern WRITE_ACTION = Pattern.compile("(Create|Update|Delete)[A-Z]");
private final IdentityStoreService service;
private final ObjectMapper mapper;
private final SsoAdminService ssoAdmin;
private final RequestContext requestContext;
@Inject
public IdentityStoreJsonHandler(IdentityStoreService service, ObjectMapper mapper) {
public IdentityStoreJsonHandler(IdentityStoreService service, ObjectMapper mapper, SsoAdminService ssoAdmin,
RequestContext requestContext) {
this.service = service;
this.mapper = mapper;
this.ssoAdmin = ssoAdmin;
this.requestContext = requestContext;
}
public Response handle(String action, JsonNode request) {
requireAccess(action, request);
return switch (action) {
case "CreateGroup" -> createGroup(request);
case "DeleteGroup" -> emptyAfter(() -> service.deleteGroup(request));
@@ -55,6 +68,26 @@ public class IdentityStoreJsonHandler {
};
}
/**
* Refuses a request on an identity store the caller may not use so ({@link SsoAdminService#identityStoreAccess}):
* one it cannot see as not found, and a change from an account that may only read the store as access denied.
*/
private void requireAccess(String action, JsonNode request) {
String storeId = request == null ? null : request.path("IdentityStoreId").asText(null);
if (storeId == null) {
return;
}
IdentityStoreAccess access = ssoAdmin.identityStoreAccess(storeId, requestContext.getAccountId());
if (access == IdentityStoreAccess.NONE) {
throw new AwsException("ResourceNotFoundException", "Identity store not found: " + storeId, 400,
Map.of("ResourceType", "IDENTITY_STORE", "ResourceId", storeId));
}
if (access == IdentityStoreAccess.READ && WRITE_ACTION.matcher(action).lookingAt()) {
throw new AwsException("AccessDeniedException",
"You do not have sufficient access to perform this action.", 400);
}
}
private Response createGroup(JsonNode request) {
Group group = service.createGroup(request);
ObjectNode out = mapper.createObjectNode();
@@ -0,0 +1,11 @@
package io.floci.aws.services.ssoadmin;
/** How an account may use an identity store ({@link SsoAdminService#identityStoreAccess}). */
public enum IdentityStoreAccess {
/** Every Identity Store action: the store's own account, or its organization's delegated administrator. */
FULL,
/** Only the read actions: another account of an organization instance's organization. */
READ,
/** None: the store is not one the account can see. */
NONE
}
@@ -10,6 +10,7 @@ import io.floci.aws.core.common.EpochSeconds;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.Resettable;
import io.floci.aws.core.common.ServicePrincipals;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.common.UuidShapes;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
@@ -54,6 +55,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -61,6 +63,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
@@ -71,6 +74,8 @@ import java.util.stream.Collectors;
public class SsoAdminService implements Resettable, StoredResourceArns {
private static final String BOOTSTRAP_INSTANCE = "instance/ssoins-7223b02a5d9f7c8e";
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");
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}");
@@ -136,6 +141,11 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
private final StorageBackend<String, InstanceUpdateState> instanceUpdateStates;
private final StorageBackend<String, String> instanceClientTokens;
private final StorageBackend<String, Boolean> instanceDeletionMarkers;
/**
* The account whose instance holds each identity store, as last found: each entry is checked against that
* account's instance before it is used, so one a reset or a restore outdates is found again by the scan.
*/
private final Map<String, String> identityStoreOwners = new ConcurrentHashMap<>();
private final StorageBackend<String, InstanceAccessControlAttributeConfiguration> accessControlAttributeConfigurations;
private final StorageBackend<String, TrustedTokenIssuer> trustedTokenIssuers;
private final StorageBackend<String, TrustedTokenIssuer> trustedTokenIssuerUpdateOverrides;
@@ -269,34 +279,78 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
public String getIdentityStoreId() { return IDENTITY_STORE_ID; }
public boolean hasIdentityStore(String identityStoreId) {
return instanceHoldingIdentityStore(identityStoreId, defaultAccountId).isPresent();
}
/**
* 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
* organization, in full; another account of that organization only to read ("Member accounts have access to
* Read actions on both the sso-directory and identitystore namespaces"); any other account not at all. A store
* no instance holds is not IAM Identity Center's, and any account uses it in full.
*/
public IdentityStoreAccess identityStoreAccess(String identityStoreId, String callerAccountId) {
Optional<SsoInstance> holding = instanceHoldingIdentityStore(identityStoreId, callerAccountId);
if (holding.isEmpty() || holding.get().ownerAccountId().equals(callerAccountId)) {
return IdentityStoreAccess.FULL;
}
SsoInstance instance = holding.get();
boolean member = !instance.accountInstance() && organizationsService
.findManagementAccountForResource(callerAccountId)
.filter(instance.ownerAccountId()::equals)
.isPresent();
if (!member) {
return IdentityStoreAccess.NONE;
}
boolean delegated = organizationsService.findAccount(callerAccountId)
.map(account -> account.getDelegatedServices().containsKey(SSO_SERVICE_PRINCIPAL))
.orElse(false);
return delegated ? IdentityStoreAccess.FULL : IdentityStoreAccess.READ;
}
/**
* 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.
*/
private Optional<SsoInstance> instanceHoldingIdentityStore(String identityStoreId, String callerAccountId) {
if (identityStoreId == null) {
return false;
return Optional.empty();
}
// Self-heals like listInstances(): storageFactory.clearAll() wipes `instances` right after
// clear() re-seeds it, so /state/reset otherwise leaves the bootstrap instance missing.
ensureBootstrapInstance(defaultAccountId, defaultRegion);
if (instances.scan(key -> true).stream()
.anyMatch(instance -> identityStoreId.equals(instance.identityStoreId()))) {
return true;
Set<String> accounts = new LinkedHashSet<>(List.of(callerAccountId, defaultAccountId));
Optional.ofNullable(identityStoreOwners.get(identityStoreId)).ifPresent(accounts::add);
for (String account : accounts) {
Optional<SsoInstance> own = AccountAwareStorageBackend.getInAccount(instances, account, account)
.filter(instance -> identityStoreId.equals(instance.identityStoreId()));
if (own.isPresent()) {
return own;
}
}
if (instances instanceof AccountAwareStorageBackend<?> accountAware) {
return accountAware.scanAllAccounts().stream()
.filter(SsoInstance.class::isInstance)
.map(SsoInstance.class::cast)
.anyMatch(instance -> identityStoreId.equals(instance.identityStoreId()));
}
return false;
identityStoreOwners.remove(identityStoreId);
List<SsoInstance> all = instances instanceof AccountAwareStorageBackend<SsoInstance> 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();
}
/**
* Seeds the account's bootstrap instance where that account keeps it, whichever account's request runs: an
* Identity Store request of another account heals the default account's instance, never a copy of its own.
*/
void ensureBootstrapInstance(String ownerAccountId, String region) {
if (instanceDeletionMarkers.get(ownerAccountId).orElse(false)) {
if (AccountAwareStorageBackend.getInAccount(instanceDeletionMarkers, ownerAccountId, ownerAccountId)
.orElse(false)) {
return;
}
Optional<SsoInstance> stored = instances.get(ownerAccountId);
Optional<SsoInstance> stored = AccountAwareStorageBackend.getInAccount(instances, ownerAccountId,
ownerAccountId);
if (stored.isEmpty()) {
instances.put(ownerAccountId, new SsoInstance(bootstrapInstanceArn, IDENTITY_STORE_ID,
"floci-identity-center", ownerAccountId, region, System.currentTimeMillis(), "ACTIVE",
null, false, new LinkedHashMap<>()));
AccountAwareStorageBackend.putInAccount(instances, ownerAccountId, ownerAccountId,
new SsoInstance(bootstrapInstanceArn, IDENTITY_STORE_ID, "floci-identity-center", ownerAccountId,
region, System.currentTimeMillis(), "ACTIVE", null, false, new LinkedHashMap<>()));
return;
}
// Before the seeded instance took its partition it was stored as arn:aws: in every
@@ -305,9 +359,10 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
SsoInstance instance = stored.get();
String expectedArn = globalArn("sso", instance.primaryRegion(), "", BOOTSTRAP_INSTANCE);
if (instance.instanceArn().endsWith(":" + BOOTSTRAP_INSTANCE) && !instance.instanceArn().equals(expectedArn)) {
instances.put(ownerAccountId, new SsoInstance(expectedArn, instance.identityStoreId(), instance.name(),
instance.ownerAccountId(), instance.primaryRegion(), instance.createdDateEpochMillis(),
instance.status(), instance.statusReason(), instance.accountInstance(), instance.tags()));
AccountAwareStorageBackend.putInAccount(instances, ownerAccountId, ownerAccountId,
new SsoInstance(expectedArn, instance.identityStoreId(), instance.name(),
instance.ownerAccountId(), instance.primaryRegion(), instance.createdDateEpochMillis(),
instance.status(), instance.statusReason(), instance.accountInstance(), instance.tags()));
}
}