fix(ssoadmin): provision to all accounts only those the check covered (#7463)

Under IAM enforcement, a ProvisionPermissionSet for
ALL_PROVISIONED_ACCOUNTS is checked on each account the permission set
is provisioned to, which the permission check finds through
SsoAdminService.through. The handler then found those accounts again
and provisioned to each. Another request could provision the permission
set to one more account in between, with an account assignment of its
own, and a permission set change could leave that account
LATEST_PERMISSION_SET_NOT_PROVISIONED: this request then provisioned it
again, changing the account's state with no policy checked against it.

The accounts are now found once for the request and kept, in a
request-scoped SsoAdminRequestContext, as PaymentCryptographyRequestContext
keeps the key an alias named: the check and the provisioning use the
same accounts, and an account provisioned to since is left as the other
request left it. Each request finds them afresh, so the next one is
checked on it.

SsoAdminServiceTest replays the race: the check finds one account,
another account is assigned and a policy attached, and the provisioning
leaves the second account not provisioned. The IAM integration test
assigns a third account after a request was let through on two, and the
same grant no longer covers the next request. iam.md says so.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-03 23:18:56 -06:00
1 parent deb639a6f1
commit 27057e2c00
5 files changed
+108 -10

No files matched your search

+3 -1
View File
@@ -1732,7 +1732,9 @@ gives. IAM Identity Center's `CreateAccountAssignment`, `DeleteAccountAssignment
`ProvisionPermissionSet` are checked on the account their `TargetId` names, `account/<id>`, beside the
instance and the permission set, and a `ProvisionPermissionSet` for `ALL_PROVISIONED_ACCOUNTS` on each
account the permission set is provisioned to. AWS does not document which accounts such a request is
authorized on, so checking each one it provisions is Floci's reading. A permission set, an application or a trusted token issuer
authorized on, so checking each one it provisions is Floci's reading, and it provisions to the accounts it was
checked on: one another request provisions the permission set to while it runs is left as that request
left it. A permission set, an application or a trusted token issuer
a create makes has the id of the instance its `InstanceArn` names, `permissionSet/<instance id>/*`.
Cost Explorer's `GetAnomalyMonitors` and `GetAnomalySubscriptions` are checked on each ARN their
`MonitorArnList` or `SubscriptionArnList` gives, and Timestream for InfluxDB's `RebootDbCluster` on the
@@ -0,0 +1,36 @@
package io.github.hectorvent.floci.services.ssoadmin;
import jakarta.enterprise.context.RequestScoped;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* What this request found, kept for the request.
*
* <p>A {@code ProvisionPermissionSet} for {@code ALL_PROVISIONED_ACCOUNTS} provisions a permission set to every
* account it is provisioned to, and the permission check finds those accounts and checks each one. Another request
* may provision the permission set to one more account while this one is running, with an assignment of its own,
* and the request must provision to the accounts it was checked on: an account found again afterwards could be one
* no policy was checked against.
*/
@RequestScoped
public class SsoAdminRequestContext {
/** The accounts a permission set was provisioned to, by the permission set's ARN. */
private final Map<String, List<String>> provisionedAccounts = new HashMap<>();
/**
* The accounts the permission set was provisioned to when this request found them, and null when this request
* has not looked for them yet.
*/
List<String> provisionedAccounts(String permissionSetArn) {
return provisionedAccounts.get(permissionSetArn);
}
/** Remembers the accounts the permission set is provisioned to, so that the request provisions to those. */
void pinProvisionedAccounts(String permissionSetArn, List<String> accountIds) {
provisionedAccounts.put(permissionSetArn, List.copyOf(accountIds));
}
}
@@ -139,6 +139,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
private final StorageBackend<String, String> trustedTokenIssuerClientTokens;
private final IdentityStoreService identityStoreService;
private final OrganizationsService organizationsService;
private final SsoAdminRequestContext requestContext;
private final String defaultAccountId;
private final String defaultRegion;
/** The seeded instance lives in the deployment's home region, so its ARN takes that partition. */
@@ -146,7 +147,8 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
@Inject
public SsoAdminService(StorageFactory storageFactory, IdentityStoreService identityStoreService,
OrganizationsService organizationsService, EmulatorConfig config) {
OrganizationsService organizationsService, SsoAdminRequestContext requestContext,
EmulatorConfig config) {
this(
storageFactory.create("ssoadmin", "ssoadmin-permission-sets.json", new TypeReference<Map<String, PermissionSet>>() {}),
storageFactory.create("ssoadmin", "ssoadmin-assignments.json", new TypeReference<Map<String, Assignment>>() {}),
@@ -175,6 +177,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
storageFactory.create("ssoadmin", "ssoadmin-trusted-token-issuer-client-tokens.json", new TypeReference<Map<String, String>>() {}),
identityStoreService,
organizationsService,
requestContext,
config.defaultAccountId(),
config.defaultRegion());
}
@@ -206,6 +209,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
StorageBackend<String, String> trustedTokenIssuerClientTokens,
IdentityStoreService identityStoreService,
OrganizationsService organizationsService,
SsoAdminRequestContext requestContext,
String defaultAccountId,
String defaultRegion) {
this.permissionSets = permissionSets;
@@ -235,6 +239,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
this.trustedTokenIssuerClientTokens = trustedTokenIssuerClientTokens;
this.identityStoreService = identityStoreService;
this.organizationsService = organizationsService;
this.requestContext = requestContext;
this.defaultAccountId = defaultAccountId;
this.defaultRegion = defaultRegion;
this.bootstrapInstanceArn = globalArn("sso", defaultRegion, "", BOOTSTRAP_INSTANCE);
@@ -1887,13 +1892,25 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
return operation;
}
/** The accounts the permission set is provisioned to. */
/**
* The accounts the permission set is provisioned to, as this request found them.
*
* <p>They are found once for the request, and kept: the permission check of a {@code ProvisionPermissionSet} for
* {@code ALL_PROVISIONED_ACCOUNTS} finds them and checks each one, and the request must provision to those.
* Another request may provision the permission set to one more account in between, and an account found again
* afterwards could be one no policy was checked against.
*/
private List<String> provisionedAccounts(String permissionSetArn) {
return permissionSetProvisionings.scan(key -> true).stream()
.filter(provisioning -> permissionSetArn.equals(provisioning.permissionSetArn()))
.map(PermissionSetProvisioning::accountId)
.distinct()
.toList();
List<String> accountIds = requestContext.provisionedAccounts(permissionSetArn);
if (accountIds == null) {
accountIds = permissionSetProvisionings.scan(key -> true).stream()
.filter(provisioning -> permissionSetArn.equals(provisioning.permissionSetArn()))
.map(PermissionSetProvisioning::accountId)
.distinct()
.toList();
requestContext.pinProvisionedAccounts(permissionSetArn, accountIds);
}
return accountIds;
}
@Override
@@ -1908,7 +1925,8 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
/**
* The accounts a permission set is provisioned to, in its partition, which a {@code ProvisionPermissionSet} for
* {@code ALL_PROVISIONED_ACCOUNTS} provisions it to again, as {@link #provisionPermissionSet} does.
* {@code ALL_PROVISIONED_ACCOUNTS} provisions it to again, as {@link #provisionPermissionSet} does: the same
* ones, found once for the request.
*/
@Override
public List<String> through(String member, String value, String region) {
@@ -1510,7 +1510,8 @@ class IamRequestResourceEnforcementIntegrationTest {
/**
* An IAM Identity Center permission set provisioned to all the accounts it is provisioned to, with no
* {@code TargetId}, or a null one, is checked on each of those accounts, so a grant on one does not reach the
* others, a deny on one holds, and a grant on all of them lets it through.
* others, a deny on one holds, and a grant on all of them lets it through. Each request finds the accounts
* afresh, so one provisioned to since is checked too.
*/
@Test
void aPermissionSetProvisionedToAllItsAccountsIsCheckedOnEachOfThem() {
@@ -1544,6 +1545,10 @@ class IamRequestResourceEnforcementIntegrationTest {
+ allowOn("sso:ProvisionPermissionSet", "arn:aws:sso:::account/222222222222"));
ssoAdmin(user, "ProvisionPermissionSet", provision + "\"TargetType\":\"ALL_PROVISIONED_ACCOUNTS\","
+ "\"TargetId\":null}").statusCode(200);
String third = assignment(instance, permissionSet, "333333333333");
adminJson("sso", "SWBExternalService.CreateAccountAssignment").body(third).post("/").then().statusCode(200);
cleanup.register(() -> adminJson("sso", "SWBExternalService.DeleteAccountAssignment").body(third).post("/"));
refusedJson(ssoAdmin(user, "ProvisionPermissionSet", all), "sso:ProvisionPermissionSet");
}
/**
@@ -90,6 +90,7 @@ class SsoAdminServiceTest {
new InMemoryStorage<String, String>(),
identityStoreService,
organizationsService,
new SsoAdminRequestContext(),
ACCOUNT_ID,
"us-east-1");
service.ensureBootstrapInstance(ACCOUNT_ID, "us-east-1");
@@ -1587,6 +1588,41 @@ class SsoAdminServiceTest {
() -> accountInstanceService.listAssignmentsForPrincipal(accountInstanceRequest, ACCOUNT_ID));
}
/**
* A {@code ProvisionPermissionSet} for {@code ALL_PROVISIONED_ACCOUNTS} provisions the permission set to the
* accounts its permission check found it provisioned to, not to one another request provisioned it to in between:
* a permission set change then leaves that account not provisioned, and this request was not checked on it.
*/
@Test
void allProvisionedAccountsAreTheOnesThePermissionCheckFound() {
PermissionSet permissionSet = createPermissionSet("PinnedAccounts");
service.createAssignment(assignmentRequest(permissionSet.arn()));
assertEquals(List.of("arn:aws:sso:::account/" + ACCOUNT_ID),
service.through("PermissionSetArn", permissionSet.arn(), "us-east-1"));
ObjectNode other = assignmentRequest(permissionSet.arn());
other.put("TargetId", "222222222222");
service.createAssignment(other);
service.attachPolicy(service.getInstanceArn(), permissionSet.arn(), "arn:aws:iam::aws:policy/ReadOnlyAccess");
ObjectNode all = mapper.createObjectNode();
all.put("InstanceArn", service.getInstanceArn());
all.put("PermissionSetArn", permissionSet.arn());
all.put("TargetType", "ALL_PROVISIONED_ACCOUNTS");
service.provisionPermissionSet(all);
assertEquals(List.of(ACCOUNT_ID), accountsProvisioned(permissionSet.arn(), "LATEST_PERMISSION_SET_PROVISIONED"));
assertEquals(List.of("222222222222"),
accountsProvisioned(permissionSet.arn(), "LATEST_PERMISSION_SET_NOT_PROVISIONED"));
}
private List<String> accountsProvisioned(String permissionSetArn, String status) {
ObjectNode request = mapper.createObjectNode();
request.put("InstanceArn", service.getInstanceArn());
request.put("PermissionSetArn", permissionSetArn);
request.put("ProvisioningStatus", status);
return service.listAccountsForProvisionedPermissionSet(request).items();
}
@Test
void provisionPermissionSetSupportsSingleAndAllProvisionedAccounts() {
PermissionSet permissionSet = createPermissionSet("ProvisionAdmins");
@@ -1921,6 +1957,7 @@ class SsoAdminServiceTest {
new InMemoryStorage<String, String>(),
identityStoreService,
organizationsService,
new SsoAdminRequestContext(),
"999999999999",
defaultRegion);
}