fix(organizations): preconfigure the account access role in a new account (#6444)

* fix(organizations): preconfigure the account access role in a new account

CreateAccount was returning a member account with no OrganizationAccountAccessRole, so sts:AssumeRole into it failed on a role that does not exist. The role AWS preconfigures is now created through IAM's own creation, in the new account's namespace: it trusts the management account and carries AdministratorAccess. CreateAccount's RoleName is honored, defaulted to OrganizationAccountAccessRole, and validated with the pattern the reference publishes, including the AWSServiceRoleFor refusal. Invited accounts do not get the role, which is AWS behavior. IAM owns the how behind an interface, the way ScpProvider already decouples IAM's reads of organization policy.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* fix(organizations): answer RoleName errors with their reasons and provision the role as the new account

CreateAccount's InvalidInputException for a bad RoleName now carries the
reference's Reason, INVALID_PATTERN for a pattern violation and
INVALID_ROLE_NAME for the reserved prefix, asserted at the service, the
wire and the SDK levels. The provisioner itself now runs createRole and
attachRolePolicy inside RequestScopes.callAs(memberAccountId), so
validation, ARN minting and storage stay IAM's own and the hand-built
role with its account-aware storage branch is gone. EntityAlreadyExists
is the one error caught, falling back to the role that is there, and
the integration test builds its requests and auth through the
OrganizationsRequests and EnforcementFixtures helpers.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-25 07:31:36 -04:00
1 parent 35b76d2d94
commit 9242c7003a
10 files changed
+560 -3

No files matched your search

@@ -11,6 +11,8 @@ import software.amazon.awssdk.services.organizations.model.CreateAccountState;
import software.amazon.awssdk.services.organizations.model.CreateAccountStatus;
import software.amazon.awssdk.services.organizations.model.Handshake;
import software.amazon.awssdk.services.organizations.model.HandshakeState;
import software.amazon.awssdk.services.organizations.model.InvalidInputException;
import software.amazon.awssdk.services.organizations.model.InvalidInputExceptionReason;
import software.amazon.awssdk.services.organizations.model.Organization;
import software.amazon.awssdk.services.organizations.model.OrganizationFeatureSet;
import software.amazon.awssdk.services.organizations.model.OrganizationalUnit;
@@ -20,6 +22,8 @@ import software.amazon.awssdk.services.organizations.model.PolicyInUseException;
import software.amazon.awssdk.services.organizations.model.PolicyType;
import software.amazon.awssdk.services.organizations.model.Root;
import software.amazon.awssdk.services.organizations.model.TargetType;
import software.amazon.awssdk.services.sts.StsClient;
import software.amazon.awssdk.services.sts.model.Credentials;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -101,6 +105,38 @@ public class OrganizationsTest {
assertThat(client.describeCreateAccountStatus(r -> r.createAccountRequestId(status.id()))
.createAccountStatus().accountId()).isEqualTo(memberAccountId);
// 4b. The account access role AWS preconfigures in the new account: the management
// account can assume it right away, and a RoleName renames it.
try (StsClient sts = TestFixtures.stsClient()) {
Credentials accessRoleCredentials = sts.assumeRole(r -> r
.roleArn(TestFixtures.globalArn("iam", memberAccountId,
"role/OrganizationAccountAccessRole"))
.roleSessionName("compat-access-role"))
.credentials();
assertThat(accessRoleCredentials.accessKeyId()).startsWith("ASIA");
assertThat(accessRoleCredentials.sessionToken()).isNotBlank();
}
CreateAccountStatus customRoleAccount = client.createAccount(r -> r
.email("compat-role@example.com").accountName("CompatRole")
.roleName("CompatAccessRole")).createAccountStatus();
assertThat(customRoleAccount.state()).isEqualTo(CreateAccountState.SUCCEEDED);
try (StsClient sts = TestFixtures.stsClient()) {
assertThat(sts.assumeRole(r -> r
.roleArn(TestFixtures.globalArn("iam", customRoleAccount.accountId(),
"role/CompatAccessRole"))
.roleSessionName("compat-access-role"))
.credentials().accessKeyId()).startsWith("ASIA");
}
// 4c. The RoleName the reference refuses, with the Reason member an SDK caller reads.
assertThatThrownBy(() -> client.createAccount(r -> r
.email("compat-bad@example.com").accountName("CompatBad")
.roleName("AWSServiceRoleForOrganizations")))
.isInstanceOfSatisfying(InvalidInputException.class, error ->
assertThat(error.reason())
.isEqualTo(InvalidInputExceptionReason.INVALID_ROLE_NAME));
Account member = client.describeAccount(r -> r.accountId(memberAccountId)).account();
assertThat(member.email()).isEqualTo("compat-dev@example.com");
assertThat(member.name()).isEqualTo("CompatDev");
@@ -242,6 +278,7 @@ public class OrganizationsTest {
// 11. Tear the organization down.
client.removeAccountFromOrganization(r -> r.accountId(memberAccountId));
client.removeAccountFromOrganization(r -> r.accountId(customRoleAccount.accountId()));
client.deleteOrganizationalUnit(r -> r.organizationalUnitId(production.id()));
client.deleteOrganizationalUnit(r -> r.organizationalUnitId(workloads.id()));
} finally {