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

@@ -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(