mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(identitystore): check an identity store in the account that holds it (#9363)
Under IAM enforcement an Identity Store request was checked on arn:aws:identitystore::<caller>:identitystore/<id>, whatever account's IAM Identity Center instance holds the store, so another account of its organization reading it was checked on a store of its own account, and a grant there reached it. Every ARN names the account owning its resource. Identity Store's lookup now answers the store's ARN in the account whose instance holds it (SsoAdminService.identityStoreOwner), which the reader already asks a service for for a resource filled from one member, so the request is checked on the store where it is. A store no instance holds keeps the caller's account, as before. No generator or table change. The lookup a store's owner takes remembers a store no instance holds, so a request naming one again does not look through every account's instances again; the record empties whenever an instance is written and on reset, as only those make a store held that was not, and starts over past 10,000 stores.
This commit is contained in:
1 parent
93460ae2d9
commit
bb579d5fe4
6 files changed
+121
-12
No files matched your search
+22
-7
@@ -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<String> find(String type, List<String> 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<String, String> 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<String> unheldIdentityStores = ConcurrentHashMap.newKeySet();
|
||||
private final StorageBackend<String, InstanceAccessControlAttributeConfiguration> accessControlAttributeConfigurations;
|
||||
private final StorageBackend<String, TrustedTokenIssuer> trustedTokenIssuers;
|
||||
private final StorageBackend<String, TrustedTokenIssuer> 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<String> 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<SsoInstance> 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<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();
|
||||
Optional<SsoInstance> 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();
|
||||
|
||||
Reference in new issue
Block a user