fix(organizations): accept documented management policy types (#6305)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-22 21:16:29 -04:00
1 parent 133ad743ae
commit 201ce60b61
5 files changed
+158 -5

No files matched your search

+4
View File
@@ -33,6 +33,10 @@ call `LeaveOrganization`. An account in no organization gets
## Behaviour notes
- Management policy types include `INSPECTOR_POLICY`, `UPGRADE_ROLLOUT_POLICY`, `BEDROCK_POLICY`,
`S3_POLICY`, `NETWORK_SECURITY_DIRECTOR_POLICY`, and `GUARDDUTY_POLICY`. They use the existing
enable, create, attach, list, effective-policy, detach, delete, and disable flows. Policy content
is not validated against a per-type schema, and these policies do not configure the backing AWS services.
- 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.
+6
View File
@@ -330,6 +330,12 @@
</dependency>
<!-- Testing -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>organizations</artifactId>
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssm</artifactId>
@@ -85,7 +85,13 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
"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",
"GUARDDUTY_POLICY");
private static final Set<String> POLICY_TYPE_SET = Set.copyOf(POLICY_TYPES);
@@ -94,9 +100,8 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
* 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}.
* <p>This is floci's rendering of the model's {@code EffectivePolicyType} shape. Ordered for
* the same reason as {@link #POLICY_TYPES}.
*/
private static final List<String> EFFECTIVE_POLICY_TYPES = List.of(
"TAG_POLICY",
@@ -109,7 +114,8 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization
"UPGRADE_ROLLOUT_POLICY",
"BEDROCK_POLICY",
"S3_POLICY",
"NETWORK_SECURITY_DIRECTOR_POLICY");
"NETWORK_SECURITY_DIRECTOR_POLICY",
"GUARDDUTY_POLICY");
private static final Set<String> EFFECTIVE_POLICY_TYPE_SET = Set.copyOf(EFFECTIVE_POLICY_TYPES);
@@ -0,0 +1,97 @@
package io.github.hectorvent.floci.services.organizations;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.organizations.OrganizationsClient;
import software.amazon.awssdk.services.organizations.model.EffectivePolicy;
import software.amazon.awssdk.services.organizations.model.OrganizationsException;
import software.amazon.awssdk.services.organizations.model.Policy;
import java.net.URI;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class OrganizationsPolicyTypesIntegrationTest {
@ParameterizedTest
@CsvSource({"INSPECTOR_POLICY,741623700001", "UPGRADE_ROLLOUT_POLICY,741623700002",
"BEDROCK_POLICY,741623700003", "S3_POLICY,741623700004",
"NETWORK_SECURITY_DIRECTOR_POLICY,741623700005", "GUARDDUTY_POLICY,741623700006"})
void sdkSupportsTheManagementPolicyLifecycle(String type, String account) {
try (OrganizationsClient client = client(account)) {
client.createOrganization(request -> request.featureSet("ALL"));
try {
String root = client.listRoots().roots().getFirst().id();
client.enablePolicyType(request -> request.rootId(root).policyType(type));
assertTrue(client.listRoots().roots().getFirst().policyTypes().stream()
.anyMatch(entry -> type.equals(entry.typeAsString()) && "ENABLED".equals(entry.statusAsString())));
Policy policy = client.createPolicy(request -> request.name("Policy").description("description")
.type(type).content("{}")).policy();
try {
assertEquals(type, policy.policySummary().typeAsString());
assertEquals(List.of(policy.policySummary()), client.listPolicies(request -> request.filter(type)).policies());
String id = policy.policySummary().id();
client.attachPolicy(request -> request.policyId(id).targetId(root));
try {
assertEquals(List.of(policy.policySummary()), client.listPoliciesForTarget(request -> request
.targetId(root).filter(type)).policies());
EffectivePolicy effective = client.describeEffectivePolicy(request -> request
.policyType(type).targetId(account)).effectivePolicy();
assertEquals(type, effective.policyTypeAsString());
assertEquals(account, effective.targetId());
assertEquals("{}", effective.policyContent());
assertTrue(client.listAccountsWithInvalidEffectivePolicy(request -> request.policyType(type))
.accounts().isEmpty());
} finally {
client.detachPolicy(request -> request.policyId(id).targetId(root));
}
assertTrue(client.listPoliciesForTarget(request -> request.targetId(root).filter(type)).policies().isEmpty());
} finally {
client.deletePolicy(request -> request.policyId(policy.policySummary().id()));
}
assertTrue(client.listPolicies(request -> request.filter(type)).policies().isEmpty());
client.disablePolicyType(request -> request.rootId(root).policyType(type));
assertTrue(client.listRoots().roots().getFirst().policyTypes().stream()
.noneMatch(entry -> type.equals(entry.typeAsString())));
} finally {
client.deleteOrganization();
}
}
}
@Test
void sdkReportsTheMissingGuardDutyEffectivePolicy() {
String account = "741623700007";
try (OrganizationsClient client = client(account)) {
client.createOrganization(request -> request.featureSet("ALL"));
try {
OrganizationsException missing = assertThrows(OrganizationsException.class,
() -> client.describeEffectivePolicy(request -> request.policyType("GUARDDUTY_POLICY")));
assertEquals("EffectivePolicyNotFoundException", missing.awsErrorDetails().errorCode());
assertEquals(400, missing.statusCode());
} finally {
client.deleteOrganization();
}
}
}
private static OrganizationsClient client(String account) {
return OrganizationsClient.builder()
.endpointOverride(URI.create("http://localhost:" + RestAssured.port))
.httpClientBuilder(UrlConnectionHttpClient.builder())
.region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test")))
.build();
}
}
@@ -12,6 +12,8 @@ import io.github.hectorvent.floci.services.organizations.model.OrganizationPolic
import io.github.hectorvent.floci.services.organizations.model.OrganizationalUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
@@ -77,6 +79,44 @@ class OrganizationsServiceTest {
assertTrue(status.getAccountId().matches("\\d{12}"), status.getAccountId());
}
@ParameterizedTest
@ValueSource(strings = {"INSPECTOR_POLICY", "UPGRADE_ROLLOUT_POLICY", "BEDROCK_POLICY",
"S3_POLICY", "NETWORK_SECURITY_DIRECTOR_POLICY", "GUARDDUTY_POLICY"})
void managementPolicyTypesSupportTheExistingLifecycle(String type) {
Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
String rootId = organization.getRoot().getId();
AwsException disabled = assertThrows(AwsException.class, () -> service.createPolicy(
MANAGEMENT_ACCOUNT, "{}", "description", "Policy", type, null));
assertEquals("PolicyTypeNotEnabledException", disabled.getErrorCode());
service.enablePolicyType(MANAGEMENT_ACCOUNT, rootId, type);
OrganizationPolicy policy = service.createPolicy(MANAGEMENT_ACCOUNT, "{}", "description", "Policy", type, null);
assertEquals(type, policy.getType());
assertEquals(List.of(policy), service.listPolicies(MANAGEMENT_ACCOUNT, type));
service.attachPolicy(MANAGEMENT_ACCOUNT, policy.getId(), rootId);
assertEquals(List.of(policy), service.listPoliciesForTarget(MANAGEMENT_ACCOUNT, rootId, type));
OrganizationsService.EffectivePolicy effective = service.describeEffectivePolicy(
MANAGEMENT_ACCOUNT, type, MANAGEMENT_ACCOUNT);
assertEquals(type, effective.policyType());
assertEquals("{}", effective.policyContent());
assertEquals(MANAGEMENT_ACCOUNT, effective.targetId());
assertTrue(service.listAccountsWithInvalidEffectivePolicy(MANAGEMENT_ACCOUNT, type).isEmpty());
service.detachPolicy(MANAGEMENT_ACCOUNT, policy.getId(), rootId);
assertTrue(service.listPoliciesForTarget(MANAGEMENT_ACCOUNT, rootId, type).isEmpty());
service.deletePolicy(MANAGEMENT_ACCOUNT, policy.getId());
assertTrue(service.listPolicies(MANAGEMENT_ACCOUNT, type).isEmpty());
service.disablePolicyType(MANAGEMENT_ACCOUNT, rootId, type);
assertTrue(organization.getRoot().getPolicyTypes().stream().noneMatch(entry -> type.equals(entry.getType())));
}
@Test
void guardDutyWithNoAttachedPolicyReportsTheMissingEffectivePolicy() {
service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
AwsException missing = assertThrows(AwsException.class, () -> service.describeEffectivePolicy(
MANAGEMENT_ACCOUNT, "GUARDDUTY_POLICY", MANAGEMENT_ACCOUNT));
assertEquals("EffectivePolicyNotFoundException", missing.getErrorCode());
}
@Test
void organizationsArnsTakeTheRequestsPartition() {
OrganizationsService china = new OrganizationsService(