Light, fluffy, and always free - The AWS Local Emulator alternative
feat(organizations): add ListAccountsWithInvalidEffectivePolicy (#2628)
Sam Fakhreddine18521edf287f
diff --git a/docs/services/organizations.md b/docs/services/organizations.mdindex 70725faf16..d499f4c296 100644--- a/docs/services/organizations.md+++ b/docs/services/organizations.md@@ -74,6 +74,7 @@ call `LeaveOrganization`. An account in no organization gets| `DescribeAccount` | Returns information about the specified member account. || `ListAccounts` | Lists every account in the organization. || `ListAccountsForParent` | Lists the accounts directly under the specified root or OU. |+| `ListAccountsWithInvalidEffectivePolicy` | Always returns an empty list; effective-policy validation is not modeled. || `MoveAccount` | Moves an account from one root or OU to another. || `RemoveAccountFromOrganization` | Removes a member account from the organization. || `LeaveOrganization` | Removes the calling member account from its organization. |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.javaindex fd4ab3926e..53f3a48bb1 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@@ -81,6 +81,8 @@ public class OrganizationsJsonHandler {case "DescribeAccount" -> describeAccount(request, callerAccountId);case "ListAccounts" -> listAccounts(request, callerAccountId);case "ListAccountsForParent" -> listAccountsForParent(request, callerAccountId);+ case "ListAccountsWithInvalidEffectivePolicy" ->+ listAccountsWithInvalidEffectivePolicy(request, callerAccountId);case "MoveAccount" -> moveAccount(request, callerAccountId);case "RemoveAccountFromOrganization" -> removeAccountFromOrganization(request, callerAccountId);case "LeaveOrganization" -> leaveOrganization(callerAccountId);@@ -280,6 +282,29 @@ public class OrganizationsJsonHandler {return accountsResponse(page(all, OrganizationAccount::getId, request));}+ /**+ * Effective-policy validation is not modeled in Floci; there is no way to evaluate+ * which accounts have invalid effective policies. Return an explicit empty list to+ * match the wire contract without fabricating data — but only once the required+ * PolicyType has been checked against the enum, so a bogus type is not answered with+ * a reassuring "no invalid accounts".+ */+ private Response listAccountsWithInvalidEffectivePolicy(JsonNode request, String caller) {+ String policyType = text(request, "PolicyType");+ List<OrganizationAccount> accounts =+ service.listAccountsWithInvalidEffectivePolicy(caller, policyType);+ PaginatedResult<OrganizationAccount> page = page(accounts, OrganizationAccount::getId, request);+ ObjectNode response = objectMapper.createObjectNode();+ ArrayNode accountsNode = response.putArray("Accounts");+ page.items().forEach(account -> accountsNode.add(accountNode(account)));+ putNextToken(response, page.nextToken());+ // PolicyType is the only field this operation's response can carry any+ // information in - Accounts is always empty by design (see javadoc above) - so+ // echoing the validated request value back is required, not cosmetic.+ response.put("PolicyType", policyType);+ return Response.ok(response).build();+ }+private Response accountsResponse(PaginatedResult<OrganizationAccount> page) {ObjectNode response = objectMapper.createObjectNode();ArrayNode accounts = response.putArray("Accounts");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.javaindex 93e641d1bc..2d9feb33cf 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@@ -88,6 +88,10 @@ public class OrganizationsService {* {@code DescribeEffectivePolicy} is defined only for the inheritable policy types. AWS* rejects the two access-control types outright — they are evaluated as a deny-by-intersection* chain rather than merged into a single effective document, so there is nothing to return.+ *+ * <p>This is floci's rendering of the model's {@code EffectivePolicyType} shape; the last five+ * entries were added to the shape after it was first written here. Ordered for the same reason+ * as {@link #POLICY_TYPES}.*/private static final List<String> EFFECTIVE_POLICY_TYPES = List.of("TAG_POLICY",@@ -95,7 +99,12 @@ public class OrganizationsService {"AISERVICES_OPT_OUT_POLICY","CHATBOT_POLICY","DECLARATIVE_POLICY_EC2",- "SECURITYHUB_POLICY");+ "SECURITYHUB_POLICY",+ "INSPECTOR_POLICY",+ "UPGRADE_ROLLOUT_POLICY",+ "BEDROCK_POLICY",+ "S3_POLICY",+ "NETWORK_SECURITY_DIRECTOR_POLICY");private static final Set<String> EFFECTIVE_POLICY_TYPE_SET = Set.copyOf(EFFECTIVE_POLICY_TYPES);@@ -748,15 +757,49 @@ public class OrganizationsService {return root;}+ /**+ * Rejects anything outside the {@code EffectivePolicyType} enum, which both+ * DescribeEffectivePolicy and ListAccountsWithInvalidEffectivePolicy draw their required+ * PolicyType from. Without it the latter answers 200 with an empty account list for a+ * policy type that does not exist, which reads as "nothing is broken".+ */+ private void validateEffectivePolicyType(String policyType) {+ if (policyType == null || !EFFECTIVE_POLICY_TYPE_SET.contains(policyType)) {+ throw invalidInput("PolicyType must be one of " + String.join(", ", EFFECTIVE_POLICY_TYPES) + ".");+ }+ }++ /**+ * Floci does not evaluate effective policies, so no account can be reported as carrying an+ * invalid one. The operation still has to validate its required PolicyType before it can+ * honestly answer "none".+ *+ * <p>The model restricts this to the management account or a delegated administrator, but+ * names no service that delegation must be scoped to and {@code EffectivePolicyType} maps to+ * none — there is nothing to check a delegated admin's registration against, because+ * delegated administration of Organizations' own operations is granted through the+ * organization's resource-based delegation policy ({@code PutResourcePolicy}), not through+ * {@code RegisterDelegatedAdministrator}'s per-service map. Floci exposes the resource-policy+ * CRUD but nothing reads it for authorization yet, so the concept genuinely isn't modelled+ * here — if it ever is, the resource policy is the hook. Management-only is stricter than the+ * model text, matching how every other operation carrying this same boilerplate phrase is+ * gated here ({@link #attachPolicy}, {@link #enablePolicyType},+ * {@link #registerDelegatedAdministrator}, {@link #listDelegatedAdministrators}).+ */+ public List<OrganizationAccount> listAccountsWithInvalidEffectivePolicy(String callerAccountId,+ String policyType) {+ requireManagementAccount(callerAccountId);+ validateEffectivePolicyType(policyType);+ return List.of();+ }+/*** Merges every policy of {@code policyType} down the inheritance chain root → OU(s) → target,* with the closest ancestor taking precedence on conflicting keys.*/public EffectivePolicy describeEffectivePolicy(String callerAccountId, String policyType, String targetId) {Organization organization = requireOrganizationForCaller(callerAccountId);- if (policyType == null || !EFFECTIVE_POLICY_TYPE_SET.contains(policyType)) {- throw invalidInput("PolicyType must be one of " + String.join(", ", EFFECTIVE_POLICY_TYPES) + ".");- }+ validateEffectivePolicyType(policyType);String effectiveTarget = targetId == null || targetId.isEmpty() ? callerAccountId : targetId;requireTarget(organization, effectiveTarget);diff --git a/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsInvalidEffectivePolicyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsInvalidEffectivePolicyIntegrationTest.javanew file mode 100644index 0000000000..96937b7f9a--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/organizations/OrganizationsInvalidEffectivePolicyIntegrationTest.java@@ -0,0 +1,191 @@+package io.github.hectorvent.floci.services.organizations;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestInstance;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.empty;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.matchesPattern;++/**+ * ListAccountsWithInvalidEffectivePolicy takes a required PolicyType drawn from the+ * EffectivePolicyType enum. Uses its own management account so it never contends with the+ * other Organizations integration tests inside the shared Quarkus instance.+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+@TestInstance(TestInstance.Lifecycle.PER_CLASS)+class OrganizationsInvalidEffectivePolicyIntegrationTest {++ private static final String CONTENT_TYPE = "application/x-amz-json-1.1";+ private static final String TARGET_PREFIX = "AWSOrganizationsV20161128.";+ private static final String MANAGEMENT_ACCOUNT = "666666666666";++ private String memberAccountId;++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ private RequestSpecification organizations(String action, String body) {+ return organizations(MANAGEMENT_ACCOUNT, action, body);+ }++ private RequestSpecification organizations(String accountId, String action, String body) {+ return given()+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + accountId+ + "/20260822/us-east-1/organizations/aws4_request, SignedHeaders=host, Signature=abc")+ .header("X-Amz-Target", TARGET_PREFIX + action)+ .contentType(CONTENT_TYPE)+ .body(body);+ }++ @Test+ @Order(1)+ void createOrganization() {+ organizations("CreateOrganization", "{\"FeatureSet\":\"ALL\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(2)+ void listAccountsWithInvalidEffectivePolicyAcceptsAModelledPolicyType() {+ organizations("ListAccountsWithInvalidEffectivePolicy", "{\"PolicyType\":\"TAG_POLICY\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Accounts", empty())+ // PolicyType is echoed back per the model's response shape - with Accounts+ // always empty by design, it's the only field this response can inform on.+ .body("PolicyType", equalTo("TAG_POLICY"));+ }++ /**+ * The enum gained these after floci first rendered it; AWS accepts them, so the+ * check must follow the current model rather than the older shorter list.+ */+ @Test+ @Order(3)+ void listAccountsWithInvalidEffectivePolicyAcceptsTheNewerPolicyTypes() {+ for (String policyType : new String[] {+ "INSPECTOR_POLICY", "UPGRADE_ROLLOUT_POLICY", "BEDROCK_POLICY",+ "S3_POLICY", "NETWORK_SECURITY_DIRECTOR_POLICY"}) {+ organizations("ListAccountsWithInvalidEffectivePolicy",+ "{\"PolicyType\":\"" + policyType + "\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Accounts", empty());+ }+ }++ @Test+ @Order(4)+ void listAccountsWithInvalidEffectivePolicyRejectsAnUnmodelledPolicyType() {+ organizations("ListAccountsWithInvalidEffectivePolicy", "{\"PolicyType\":\"NOT_A_POLICY\"}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidInputException"));+ }++ /**+ * SERVICE_CONTROL_POLICY is a valid policy type but not an effective-policy type, so it+ * must be rejected here the same way DescribeEffectivePolicy rejects it.+ */+ @Test+ @Order(5)+ void listAccountsWithInvalidEffectivePolicyRejectsAnAccessControlPolicyType() {+ organizations("ListAccountsWithInvalidEffectivePolicy",+ "{\"PolicyType\":\"SERVICE_CONTROL_POLICY\"}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidInputException"));+ }++ @Test+ @Order(6)+ void listAccountsWithInvalidEffectivePolicyRequiresPolicyType() {+ organizations("ListAccountsWithInvalidEffectivePolicy", "{}")+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidInputException"));+ }++ @Test+ @Order(7)+ void createMemberAccount() {+ var response = organizations("CreateAccount", "{\"Email\":\"[email protected]\",\"AccountName\":\"Member\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("CreateAccountStatus.AccountId", matchesPattern("\\d{12}"))+ .extract().jsonPath();++ memberAccountId = response.getString("CreateAccountStatus.AccountId");+ }++ /**+ * AWS restricts this operation to the management account or a delegated administrator — unlike+ * DescribeEffectivePolicy, which "you can call ... from any account in a organization" per the+ * botocore model. An ordinary member must not get a reassuring empty list.+ */+ @Test+ @Order(8)+ void listAccountsWithInvalidEffectivePolicyRejectsAnOrdinaryMemberAccount() {+ organizations(memberAccountId, "ListAccountsWithInvalidEffectivePolicy", "{\"PolicyType\":\"TAG_POLICY\"}")+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .body("__type", equalTo("AccessDeniedException"));+ }++ @Test+ @Order(9)+ void registerMemberAsDelegatedAdministrator() {+ organizations(MANAGEMENT_ACCOUNT, "RegisterDelegatedAdministrator",+ "{\"AccountId\":\"" + memberAccountId + "\",\"ServicePrincipal\":\"config.amazonaws.com\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ /**+ * The model names no service that a delegated administrator must be scoped to for this+ * operation, and EffectivePolicyType maps to none — there is nothing to check a registration+ * against. Rather than accept delegation for an unrelated service (config.amazonaws.com here),+ * this stays management-only, matching every other operation gated on this boilerplate phrase.+ */+ @Test+ @Order(10)+ void listAccountsWithInvalidEffectivePolicyRejectsADelegatedAdministratorForAnUnrelatedService() {+ organizations(memberAccountId, "ListAccountsWithInvalidEffectivePolicy", "{\"PolicyType\":\"TAG_POLICY\"}")+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .body("__type", equalTo("AccessDeniedException"));+ }+}