mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
feat(organizations): add ListAccountsWithInvalidEffectivePolicy (#2628)
* feat(organizations): add ListAccountsWithInvalidEffectivePolicy Effective-policy validation is not modeled, so the operation returns an explicit empty Accounts list rather than fabricating data. Documented as a limitation in docs/services/organizations.md. * fix(organizations): validate PolicyType on ListAccountsWithInvalidEffectivePolicy The operation takes a required PolicyType drawn from the EffectivePolicyType enum, and the handler ignored it entirely: any string — NOT_A_POLICY, SERVICE_CONTROL_POLICY — was answered 200 with an empty Accounts list, which reads as "no account has an invalid effective policy" when AWS would have rejected the request outright. Route the operation through the service so it validates PolicyType the way DescribeEffectivePolicy does, and lift that check into a shared helper. EFFECTIVE_POLICY_TYPES is floci's rendering of the EffectivePolicyType shape and had drifted five values behind the model (INSPECTOR_POLICY, UPGRADE_ROLLOUT_POLICY, BEDROCK_POLICY, S3_POLICY, NETWORK_SECURITY_DIRECTOR_POLICY); widened so the new validation enforces the enum rather than a subset of it. * fix(organizations): restrict ListAccountsWithInvalidEffectivePolicy to management/delegated admin An ordinary member account could call this operation and get HTTP 200 with an empty Accounts list, indistinguishable from "everything checked out fine". The botocore model restricts it to the management account or a member account registered as a delegated administrator - unlike DescribeEffectivePolicy, which any account in the organization may call. Add requireManagementOrDelegatedAdmin alongside the existing requireOrganizationForCaller/requireManagementAccount pair and route this operation through it. * fix(organizations): tighten ListAccountsWithInvalidEffectivePolicy to management-only requireManagementOrDelegatedAdmin (added in the prior commit) let a member delegated as admin for any unrelated service - config.amazonaws.com in the new test - through, since it only checked whether the account's delegatedServices map was non-empty rather than which service. The model names no service this operation's delegation should be scoped to, and EffectivePolicyType maps to none, so there is nothing to check a registration against. Collapse to requireManagementAccount instead, matching every other operation gated on this same boilerplate phrase in this file (attachPolicy, enablePolicyType, registerDelegatedAdministrator, listDelegatedAdministrators) - stricter than the model text, consistently so. Removes the now-unused requireManagementOrDelegatedAdmin helper. * fix(organizations): echo PolicyType on ListAccountsWithInvalidEffectivePolicy Accounts is always empty by design here, so PolicyType was the only field the response could carry any information in, and it was being dropped by the shared accountsResponse() helper (built for the two-member ListAccounts shape). Builds the response inline instead, echoing the validated PolicyType back per the modeled response shape. Also closes a stray blank line above requireManagementAccount's javadoc, and expands the delegated-administrator note on why management-only is correct today.
This commit is contained in:
1 parent
d60bd6ead2
commit
18521edf28
4 files changed
+264
-4
No files matched your search
@@ -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. |
|
||||
|
||||
+25
@@ -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");
|
||||
|
||||
+47
-4
@@ -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);
|
||||
|
||||
|
||||
+191
@@ -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\":\"member@example.com\",\"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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user