feat(securityhub): add organization configuration operations (#3161)

* feat(securityhub): add organization configuration operations

* fix(securityhub): complete organization configuration contracts

* fix(securityhub): harden organization contracts

* test(securityhub): log tolerated organization state

* test(securityhub): clean organization fixture state

* test(securityhub): isolate organization cleanup failures

* fix(securityhub): return modeled invalid access error
This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-08 19:40:01 -05:00
1 parent fd3c4d9ab6
commit 9d4e71f8a2
21 files changed
+1765 -14

No files matched your search

@@ -22,10 +22,10 @@ class UnknownServiceScopeGuardDisabledIntegrationTest {
@Test
void unsupportedScopeFallsThroughWhenRejectionDisabled() {
given()
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260707/us-east-1/securityhub"
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260707/us-east-1/account"
+ "/aws4_request, SignedHeaders=host;x-amz-date, Signature=deadbeef")
.when()
.get("/accounts")
.get("/guard-disabled-no-such-bucket")
.then()
// Back to the old behaviour: S3's path-style catch-all answers for the bucket
// named "accounts", instead of the guard's UnknownOperationException.
@@ -38,17 +38,6 @@ class UnknownServiceScopeGuardIntegrationTest {
.body("__type", equalTo("UnknownOperationException"));
}
@Test
void securityhubScopedRestRequestGetsUnknownOperation() {
given()
.header("Authorization", authorization("securityhub"))
.when()
.get("/accounts")
.then()
.statusCode(404)
.body("__type", equalTo("UnknownOperationException"));
}
@Test
void s3ScopedRequestKeepsS3Behavior() {
given()
@@ -0,0 +1,165 @@
package io.github.hectorvent.floci.services.securityhub;
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
import jakarta.inject.Inject;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
@QuarkusTest
class SecurityHubOrganizationIntegrationTest {
@Inject
OrganizationsService organizationsService;
@Test
void organizationAndConfigurationPolicyLifecycle() {
String management = "920000000001";
String administrator = "920000000002";
String member = "920000000003";
createOrganization(management, administrator, member);
given().header("Authorization", auth(management)).get("/organization/admin").then().statusCode(200)
.body("AdminAccounts", hasSize(0));
given().contentType("application/json").header("Authorization", auth(management))
.body("{\"AdminAccountId\":\"" + administrator + "\"}").post("/organization/admin/enable")
.then().statusCode(200)
.body("AdminAccountId", equalTo(administrator))
.body("Feature", equalTo("SecurityHub"));
given().header("Authorization", auth(administrator)).get("/accounts").then().statusCode(200)
.body("HubArn", notNullValue());
String aggregatorArn = given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"RegionLinkingMode\":\"ALL_REGIONS\"}").post("/findingAggregator/create")
.then().statusCode(200).extract().path("FindingAggregatorArn");
given().header("Authorization", auth(administrator)).get("/findingAggregator/get/" + aggregatorArn)
.then().statusCode(200).body("RegionLinkingMode", equalTo("ALL_REGIONS"));
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"OrganizationConfiguration\":{\"ConfigurationType\":\"CENTRAL\"},"
+ "\"AutoEnable\":false,\"AutoEnableStandards\":\"NONE\"}")
.post("/organization/configuration").then().statusCode(200);
given().header("Authorization", auth(administrator)).get("/organization/configuration").then().statusCode(200)
.body("OrganizationConfiguration.ConfigurationType", equalTo("CENTRAL"));
String policyId = given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Name\":\"security-policy\",\"ConfigurationPolicy\":{"
+ "\"SecurityHub\":{\"ServiceEnabled\":true}}}")
.post("/configurationPolicy/create").then().statusCode(200).extract().path("Id");
given().header("Authorization", auth(administrator)).get("/configurationPolicy/get/" + policyId)
.then().statusCode(200).body("Id", equalTo(policyId));
String association = "{\"ConfigurationPolicyIdentifier\":\"" + policyId
+ "\",\"Target\":{\"AccountId\":\"" + member + "\"}}";
given().contentType("application/json").header("Authorization", auth(administrator)).body(association)
.post("/configurationPolicyAssociation/associate").then().statusCode(200);
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Target\":{\"AccountId\":\"" + member + "\"}}")
.post("/configurationPolicyAssociation/get").then().statusCode(200)
.body("AssociationStatus", equalTo("PENDING"));
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Target\":{\"AccountId\":\"" + member + "\"}}")
.post("/configurationPolicyAssociation/get").then().statusCode(200)
.body("AssociationStatus", equalTo("SUCCESS"));
given().header("Authorization", auth(member)).get("/accounts").then().statusCode(200);
}
@Test
void organizationBoundariesAreEnforced() {
String management = "920000000011";
String administrator = "920000000012";
String outsider = "930000000013";
createOrganization(management, administrator);
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"AdminAccountId\":\"" + administrator + "\"}").post("/organization/admin/enable")
.then().statusCode(403).body("__type", equalTo("AccessDeniedException"));
given().header("Authorization", auth(administrator)).get("/organization/admin")
.then().statusCode(401).body("__type", equalTo("InvalidAccessException"));
given().contentType("application/json").header("Authorization", auth(management))
.body("{\"AdminAccountId\":\"" + outsider + "\"}").post("/organization/admin/enable")
.then().statusCode(400).body("__type", equalTo("InvalidInputException"));
given().contentType("application/json").header("Authorization", auth(management))
.body("{\"AdminAccountId\":\"" + administrator + "\"}").post("/organization/admin/enable")
.then().statusCode(200);
String policyId = given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Name\":\"bounded-policy\",\"ConfigurationPolicy\":{"
+ "\"SecurityHub\":{\"ServiceEnabled\":true}}}")
.post("/configurationPolicy/create").then().statusCode(200).extract().path("Id");
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"ConfigurationPolicyIdentifier\":\"" + policyId
+ "\",\"Target\":{\"AccountId\":\"" + outsider + "\"}}")
.post("/configurationPolicyAssociation/associate").then().statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
void malformedPoliciesAndInvalidUpdatesAreRejected() {
String management = "920000000021";
String administrator = "920000000022";
createOrganization(management, administrator);
designateAdministrator(management, administrator);
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Name\":\"bad-policy\",\"ConfigurationPolicy\":{"
+ "\"SecurityHub\":{\"ServiceEnabled\":\"yes\"}}}")
.post("/configurationPolicy/create").then().statusCode(400)
.body("__type", equalTo("InvalidInputException"));
String policyId = given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Name\":\"valid-policy\",\"ConfigurationPolicy\":{"
+ "\"SecurityHub\":{\"ServiceEnabled\":true}}}")
.post("/configurationPolicy/create").then().statusCode(200).extract().path("Id");
String longName = "a".repeat(129);
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"Name\":\"" + longName + "\"}")
.patch("/configurationPolicy/" + policyId).then().statusCode(400)
.body("__type", equalTo("InvalidInputException"));
}
@Test
void findingAggregatorValidatesRegionElementsButAllowsEmptyExclusionList() {
String management = "920000000031";
String administrator = "920000000032";
createOrganization(management, administrator);
designateAdministrator(management, administrator);
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"RegionLinkingMode\":\"ALL_REGIONS_EXCEPT_SPECIFIED\",\"Regions\":[123]}")
.post("/findingAggregator/create").then().statusCode(400)
.body("__type", equalTo("InvalidInputException"));
given().contentType("application/json").header("Authorization", auth(administrator))
.body("{\"RegionLinkingMode\":\"ALL_REGIONS_EXCEPT_SPECIFIED\"}")
.post("/findingAggregator/create").then().statusCode(200)
.body("RegionLinkingMode", equalTo("ALL_REGIONS_EXCEPT_SPECIFIED"));
}
@Test
void invalidAdminAccountIdReturnsValidationException() {
given().contentType("application/json").header("Authorization", auth("920000000041"))
.body("{\"AdminAccountId\":\"bad\"}").post("/organization/admin/enable")
.then().statusCode(400).body("__type", equalTo("InvalidInputException"));
}
private void createOrganization(String managementAccountId, String... members) {
organizationsService.createOrganization(managementAccountId, "ALL");
for (String member : members) {
var handshake = organizationsService.inviteAccountToOrganization(
managementAccountId, member, "ACCOUNT", null);
organizationsService.acceptHandshake(member, handshake.getId());
}
}
private void designateAdministrator(String managementAccountId, String administratorAccountId) {
given().contentType("application/json").header("Authorization", auth(managementAccountId))
.body("{\"AdminAccountId\":\"" + administratorAccountId + "\"}")
.post("/organization/admin/enable").then().statusCode(200);
}
private static String auth(String accountId) {
return "AWS4-HMAC-SHA256 Credential=" + accountId
+ "/20260101/us-east-1/securityhub/aws4_request";
}
}
@@ -0,0 +1,76 @@
package io.github.hectorvent.floci.services.securityhub;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
import io.github.hectorvent.floci.services.securityhub.model.SecurityHubState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class SecurityHubServiceTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT_ID = "222222222222";
private final ObjectMapper objectMapper = new ObjectMapper();
private SecurityHubService service;
@BeforeEach
void setUp() {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
service = new SecurityHubService(AccountAwareStorageBackend.inMemory(ACCOUNT_ID), regionResolver,
mock(OrganizationsService.class));
}
@Test
void enableSecurityHubPersistsConfigurationAndHubTags() throws Exception {
service.enableSecurityHub(REGION, objectMapper.readTree("""
{
"ControlFindingGenerator": "STANDARD_CONTROL",
"Tags": {"env": "test"}
}
"""));
SecurityHubState state = service.state(REGION);
assertTrue(state.isEnabled());
assertEquals("STANDARD_CONTROL", state.getControlFindingGenerator());
assertEquals(Map.of("env", "test"), service.tagsForResource(REGION, service.hubArn(REGION)));
}
@Test
void invalidReservedTagPrefixIsRejected() throws Exception {
AwsException error = assertThrows(AwsException.class, () -> service.enableSecurityHub(REGION,
objectMapper.readTree("{\"Tags\":{\"aws:owner\":\"test\"}}")));
assertEquals("InvalidInputException", error.getErrorCode());
}
@Test
void invalidAdministratorFeatureIsRejected() {
AwsException error = assertThrows(AwsException.class,
() -> service.enableOrganizationAdminAccount(REGION, "111111111111", "Other"));
assertEquals("InvalidInputException", error.getErrorCode());
}
@Test
void clearRemovesState() throws Exception {
service.enableSecurityHub(REGION, objectMapper.readTree("{}"));
assertTrue(service.state(REGION).isEnabled());
service.clear();
assertFalse(service.state(REGION).isEnabled());
}
}