From 18128b432aaeb48196def008ea97510c03d27abc Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Wed, 30 Sep 2026 17:12:38 -0500 Subject: [PATCH] fix(organizations): start a new root with no policy types enabled (#4756) CreateOrganization (and EnableAllFeatures) seeded the root with SERVICE_CONTROL_POLICY already ENABLED, so the first EnablePolicyType a caller issues was rejected with PolicyTypeAlreadyEnabledException. That is exactly what Terraform's aws_organizations_organization does with enabled_policy_types right after CreateOrganization, so the common "create the org and turn on SCPs" configuration failed at its first resource. On AWS a new root has an empty PolicyTypes list: the provider's acceptance test for a plain organization asserts roots.0.policy_types is empty, and its enabled_policy_types test enables SCPs on a fresh organization successfully. DescribeOrganization's AvailablePolicyTypes used to read the root's list, which is what the seed was standing in for. It is now reported separately and, as AWS documents for the deprecated field, lists only SERVICE_CONTROL_POLICY for an ALL organization. Tests that relied on the seed now call EnablePolicyType first, as a real caller must. The CloudFormation test creates the stack without its SCP, enables the type, then adds the SCP by UpdateStack, because no CloudFormation resource enables a policy type. --- .../com/floci/test/OrganizationsTest.java | 8 ++++-- docs/services/organizations.md | 6 +++++ .../OrganizationsJsonHandler.java | 6 ++++- .../organizations/OrganizationsService.java | 9 +++---- .../OrganizationsCfnIntegrationTest.java | 27 +++++++++++++++++-- ...ementLeaveOrganizationIntegrationTest.java | 7 +++-- ...rganizationsFeatureSetIntegrationTest.java | 8 ++++++ .../OrganizationsIntegrationTest.java | 27 ++++++++++++++++--- .../OrganizationsServiceTest.java | 12 +++++++-- 9 files changed, 92 insertions(+), 18 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/OrganizationsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/OrganizationsTest.java index 7c405fc9f8..5284b4729e 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/OrganizationsTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/OrganizationsTest.java @@ -71,11 +71,15 @@ public class OrganizationsTest { try { assertThat(client.describeOrganization().organization().id()).isEqualTo(organization.id()); - // 2. The root exists and carries the AWS-managed FullAWSAccess SCP. + // 2. The root exists with no policy types enabled, and carries the AWS-managed + // FullAWSAccess SCP. SCPs have to be enabled on it before one can be created. Root root = client.listRoots().roots().get(0); assertThat(root.id()).matches("r-[a-z0-9]{4}"); assertThat(root.name()).isEqualTo("Root"); - assertThat(root.policyTypes()).anyMatch(p -> p.type() == PolicyType.SERVICE_CONTROL_POLICY); + assertThat(root.policyTypes()).isEmpty(); + client.enablePolicyType(r -> r.rootId(root.id()).policyType(PolicyType.SERVICE_CONTROL_POLICY)); + assertThat(client.listRoots().roots().get(0).policyTypes()) + .anyMatch(p -> p.type() == PolicyType.SERVICE_CONTROL_POLICY); assertThat(client.listPoliciesForTarget(r -> r.targetId(root.id()) .filter(PolicyType.SERVICE_CONTROL_POLICY)).policies()) diff --git a/docs/services/organizations.md b/docs/services/organizations.md index 5f2ba689c6..e1eabdecec 100644 --- a/docs/services/organizations.md +++ b/docs/services/organizations.md @@ -36,6 +36,12 @@ call `LeaveOrganization`. An account in no organization gets - The AWS-managed `p-FullAWSAccess` SCP is created with the organization and attached to the root, every new OU and every new account. Detaching the last service control policy from a target is rejected with `ConstraintViolationException`, as on AWS. +- A new root has no policy types enabled, whatever the feature set; `ListRoots` reports an + empty `PolicyTypes` until `EnablePolicyType` is called, and `CreatePolicy` / `AttachPolicy` + for a type that is not enabled fail with `PolicyTypeNotEnabledException`. SCPs only apply to + member accounts once `SERVICE_CONTROL_POLICY` is enabled. The deprecated + `AvailablePolicyTypes` on `DescribeOrganization` lists only `SERVICE_CONTROL_POLICY`, for an + `ALL` organization, as on AWS. - A `CONSOLIDATED_BILLING` organization has no available policy types. `EnableAllFeatures` promotes it to `ALL`; with no member accounts the handshake completes immediately. With member accounts it stays `REQUESTED` until one of them calls `AcceptHandshake` — AWS requires *every* diff --git a/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java index 3dade4de55..826f37d229 100644 --- a/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java @@ -630,8 +630,12 @@ public class OrganizationsJsonHandler { node.put("MasterAccountArn", organization.getMasterAccountArn()); node.put("MasterAccountId", organization.getMasterAccountId()); node.put("MasterAccountEmail", organization.getMasterAccountEmail()); + // Deprecated in AWS and distinct from the root's enabled types (ListRoots): it reports SCPs as + // available for an all-features organization and never includes any other policy type. ArrayNode policyTypes = node.putArray("AvailablePolicyTypes"); - organization.getRoot().getPolicyTypes().forEach(summary -> policyTypes.add(policyTypeNode(summary))); + if ("ALL".equals(organization.getFeatureSet())) { + policyTypes.add(policyTypeNode(new PolicyTypeSummary("SERVICE_CONTROL_POLICY", "ENABLED"))); + } return node; } diff --git a/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsService.java b/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsService.java index ae00c55391..a7570d01b8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsService.java @@ -274,9 +274,9 @@ public class OrganizationsService implements ScpProvider { root.setId(rootId); root.setArn(arn(callerAccountId, "root/" + organizationId + "/" + rootId)); root.setName("Root"); - if (FEATURE_SET_ALL.equals(resolvedFeatureSet)) { - root.getPolicyTypes().add(new PolicyTypeSummary(SERVICE_CONTROL_POLICY, "ENABLED")); - } + // A new root has no policy types enabled, whatever the feature set: callers enable SCPs with + // EnablePolicyType, and Terraform's enabled_policy_types does exactly that right after + // CreateOrganization, so a pre-enabled root fails it with PolicyTypeAlreadyEnabledException. organization.setRoot(root); organizations.putForAccount(callerAccountId, organizationId, organization); @@ -1727,9 +1727,6 @@ public class OrganizationsService implements ScpProvider { private void applyEnableAllFeatures(Organization organization) { organization.setFeatureSet(FEATURE_SET_ALL); - if (findPolicyType(organization.getRoot(), SERVICE_CONTROL_POLICY).isEmpty()) { - organization.getRoot().getPolicyTypes().add(new PolicyTypeSummary(SERVICE_CONTROL_POLICY, "ENABLED")); - } organizations.putForAccount(organization.getMasterAccountId(), organization.getId(), organization); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/OrganizationsCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/OrganizationsCfnIntegrationTest.java index 7c6785a893..683bbc9bdd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/OrganizationsCfnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/OrganizationsCfnIntegrationTest.java @@ -1,5 +1,7 @@ package io.github.hectorvent.floci.services.cloudformation; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; @@ -11,6 +13,7 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestMethodOrder; import java.time.Duration; +import java.util.List; import java.util.concurrent.TimeUnit; import static io.restassured.RestAssured.given; @@ -178,12 +181,32 @@ class OrganizationsCfnIntegrationTest { "DescribeStacksResponse.DescribeStacksResult.Stacks.member.StackStatus"))); } + /** + * {@link #TEMPLATE} without the SCP. A new root has no policy types enabled and CloudFormation + * has no resource that enables one, so a template cannot create an organization and an SCP in + * the same operation: the SCP arrives in an update once EnablePolicyType has run. + */ + private static String templateWithoutScp() throws Exception { + ObjectNode template = (ObjectNode) new ObjectMapper().readTree(TEMPLATE); + ((ObjectNode) template.get("Resources")).remove("Scp"); + ((ObjectNode) template.get("Outputs")).remove(List.of("PolicyId", "PolicyArn", "PolicyAwsManaged")); + return template.toString(); + } + @Test @Order(1) - void createStackProvisionsEveryOrganizationsResourceType() { - cloudFormation("CreateStack", "StackName", STACK_NAME, "TemplateBody", TEMPLATE) + void createStackProvisionsEveryOrganizationsResourceType() throws Exception { + cloudFormation("CreateStack", "StackName", STACK_NAME, "TemplateBody", templateWithoutScp()) .then().statusCode(200); awaitStackStatus("CREATE_COMPLETE"); + + organizations("EnablePolicyType", "{\"RootId\":\"" + output(describeStacks(), "RootId") + + "\",\"PolicyType\":\"SERVICE_CONTROL_POLICY\"}") + .then().statusCode(200); + + cloudFormation("UpdateStack", "StackName", STACK_NAME, "TemplateBody", TEMPLATE) + .then().statusCode(200); + awaitStackStatus("UPDATE_COMPLETE"); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java index 900eb1c7b2..2509797f8b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java @@ -45,9 +45,12 @@ class ScpEnforcementLeaveOrganizationIntegrationTest { // --- management sets up the organization ----------------------------------------------- org(MGMT, "CreateOrganization", "{\"FeatureSet\":\"ALL\"}").then().statusCode(200); - // FeatureSet=ALL already enables the SERVICE_CONTROL_POLICY type on the root and seeds - // FullAWSAccess, so no explicit EnablePolicyType is needed for SCPs to participate. + // A new root has no policy types enabled; SCPs only participate once EnablePolicyType + // turns them on. FullAWSAccess is already attached, so enabling them denies nothing alone. String rootId = JsonPath.from(org(MGMT, "ListRoots", "{}").asString()).getString("Roots[0].Id"); + org(MGMT, "EnablePolicyType", + "{\"RootId\":\"" + rootId + "\",\"PolicyType\":\"SERVICE_CONTROL_POLICY\"}") + .then().statusCode(200); String policyId = JsonPath.from(org(MGMT, "CreatePolicy", "{\"Name\":\"deny-leave-" + suffix + "\",\"Type\":\"SERVICE_CONTROL_POLICY\"," diff --git a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsFeatureSetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsFeatureSetIntegrationTest.java index 87f0d3e1af..f1deb46043 100644 --- a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsFeatureSetIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsFeatureSetIntegrationTest.java @@ -95,6 +95,14 @@ class OrganizationsFeatureSetIntegrationTest { .statusCode(200) .body("Organization.FeatureSet", equalTo("ALL")) .body("Organization.AvailablePolicyTypes.Type", hasItem("SERVICE_CONTROL_POLICY")); + + // Upgrading makes policy types available; it does not enable any of them on the root. + organizations("ListRoots", "{}") + .when() + .post("/") + .then() + .statusCode(200) + .body("Roots[0].PolicyTypes", empty()); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsIntegrationTest.java index 06148c7115..3e9f48842b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsIntegrationTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.TestMethodOrder; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItems; @@ -108,7 +109,9 @@ class OrganizationsIntegrationTest { @Test @Order(4) - void listRootsReturnsRootWithServiceControlPolicyEnabled() { + void listRootsReturnsRootWithNoPolicyTypesEnabled() { + // AvailablePolicyTypes above lists SCPs for an all-features organization, but that is not + // the same as the root having them enabled: a new root starts with none. rootId = organizations("ListRoots", "{}") .when() .post("/") @@ -118,13 +121,31 @@ class OrganizationsIntegrationTest { .body("Roots[0].Id", matchesPattern("r-[a-z0-9]{4}")) .body("Roots[0].Name", equalTo("Root")) .body("Roots[0].Arn", startsWith("arn:aws:organizations::000000000000:root/" + organizationId)) - .body("Roots[0].PolicyTypes.Type", hasItem("SERVICE_CONTROL_POLICY")) - .body("Roots[0].PolicyTypes.Status", hasItem("ENABLED")) + .body("Roots[0].PolicyTypes", empty()) .extract().jsonPath().getString("Roots[0].Id"); } @Test @Order(5) + void serviceControlPoliciesCanBeEnabledOnTheNewRoot() { + organizations("EnablePolicyType", "{\"RootId\":\"" + rootId + "\",\"PolicyType\":\"SERVICE_CONTROL_POLICY\"}") + .when() + .post("/") + .then() + .statusCode(200) + .body("Root.PolicyTypes.Type", contains("SERVICE_CONTROL_POLICY")); + + organizations("ListRoots", "{}") + .when() + .post("/") + .then() + .statusCode(200) + .body("Roots[0].PolicyTypes.Type", contains("SERVICE_CONTROL_POLICY")) + .body("Roots[0].PolicyTypes.Status", contains("ENABLED")); + } + + @Test + @Order(6) void fullAwsAccessIsAttachedToTheRoot() { organizations("ListPoliciesForTarget", "{\"TargetId\":\"" + rootId + "\",\"Filter\":\"SERVICE_CONTROL_POLICY\"}") diff --git a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsServiceTest.java index 616308c234..a62292f548 100644 --- a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsServiceTest.java @@ -63,6 +63,7 @@ class OrganizationsServiceTest { service.createOrganizationalUnit(MANAGEMENT_ACCOUNT, organization.getRoot().getId(), "Unit", null); assertTrue(unit.getId().matches("ou-" + rootSuffix + "-[a-z0-9]{8}"), unit.getId()); + service.enablePolicyType(MANAGEMENT_ACCOUNT, organization.getRoot().getId(), "SERVICE_CONTROL_POLICY"); OrganizationPolicy policy = service.createPolicy(MANAGEMENT_ACCOUNT, "{}", null, "Policy", "SERVICE_CONTROL_POLICY", null); assertTrue(policy.getId().matches("p-[a-z0-9]{8}"), policy.getId()); @@ -216,6 +217,7 @@ class OrganizationsServiceTest { Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); String rootId = organization.getRoot().getId(); OrganizationalUnit unit = service.createOrganizationalUnit(MANAGEMENT_ACCOUNT, rootId, "Unit", null); + service.enablePolicyType(MANAGEMENT_ACCOUNT, rootId, "SERVICE_CONTROL_POLICY"); OrganizationPolicy policy = service.createPolicy(MANAGEMENT_ACCOUNT, "{}", null, "Policy", "SERVICE_CONTROL_POLICY", null); @@ -293,6 +295,7 @@ class OrganizationsServiceTest { String member = service.createAccount(MANAGEMENT_ACCOUNT, "member@example.com", "Member", null, false) .getAccountId(); service.moveAccount(MANAGEMENT_ACCOUNT, member, rootId, ouId); + service.enablePolicyType(MANAGEMENT_ACCOUNT, rootId, "SERVICE_CONTROL_POLICY"); // FullAWSAccess sits on root, OU, and account: three levels. List> levels = service.effectiveScpLevels(member); @@ -309,7 +312,8 @@ class OrganizationsServiceTest { @Test void theManagementAccountIsExemptFromScps() { - service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); + Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); + service.enablePolicyType(MANAGEMENT_ACCOUNT, organization.getRoot().getId(), "SERVICE_CONTROL_POLICY"); String member = service.createAccount(MANAGEMENT_ACCOUNT, "member@example.com", "Member", null, false) .getAccountId(); @@ -326,10 +330,14 @@ class OrganizationsServiceTest { } @Test - void disablingTheScpPolicyTypeOnTheRootRemovesTheCeiling() { + void scpsBindOnlyWhileTheirPolicyTypeIsEnabledOnTheRoot() { Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); String member = service.createAccount(MANAGEMENT_ACCOUNT, "member@example.com", "Member", null, false) .getAccountId(); + // A new root has no policy types enabled, so there is no ceiling until SCPs are turned on. + assertNull(service.effectiveScpLevels(member)); + + service.enablePolicyType(MANAGEMENT_ACCOUNT, organization.getRoot().getId(), "SERVICE_CONTROL_POLICY"); assertNotNull(service.effectiveScpLevels(member)); service.disablePolicyType(MANAGEMENT_ACCOUNT, organization.getRoot().getId(), "SERVICE_CONTROL_POLICY");