feat(detective): add organization member operations (#3190)

* feat(detective): add organization member operations

* fix(detective): align organization operations with AWS

* docs(detective): clarify member listing

* fix(detective): enforce organization administration contracts
This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-07 18:31:48 -04:00
1 parent 2869502e7c
commit 5864b013d2
18 files changed
+821 -2

No files matched your search

@@ -0,0 +1,87 @@
package io.github.hectorvent.floci.services.detective;
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
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.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class DetectiveIntegrationTest {
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=AKID/20260904/us-east-1/detective/aws4_request";
@Inject
OrganizationsService organizationsService;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void organizationGraphAndMemberLifecycleMatchesAwsContract() {
organizationsService.createOrganization("000000000000", "ALL");
String enableBody = post("/orgs/enableAdminAccount", "{\"AccountId\":\"000000000000\"}")
.statusCode(200).extract().asString();
assertTrue(enableBody.isEmpty());
String graphArn = post("/graphs/list", "{\"MaxResults\":200}")
.statusCode(200)
.body("GraphList", hasSize(1))
.extract().path("GraphList[0].Arn");
String updateBody = post("/orgs/updateOrganizationConfiguration",
"{\"GraphArn\":\"" + graphArn + "\",\"AutoEnable\":true}")
.statusCode(200).extract().asString();
assertTrue(updateBody.isEmpty());
post("/orgs/updateOrganizationConfiguration", "{\"GraphArn\":\"" + graphArn + "\"}")
.statusCode(200);
post("/orgs/describeOrganizationConfiguration", "{\"GraphArn\":\"" + graphArn + "\"}")
.statusCode(200).body("AutoEnable", equalTo(true));
post("/graph/members", "{\"GraphArn\":\"" + graphArn
+ "\",\"Accounts\":[{\"AccountId\":\"444444444444\"},{\"AccountId\":\"bad\"}]}")
.statusCode(400).body("__type", equalTo("ValidationException"));
post("/graph/members/list", "{\"GraphArn\":\"" + graphArn + "\"}")
.statusCode(200).body("MemberDetails", hasSize(0));
String create = "{\"GraphArn\":\"" + graphArn
+ "\",\"Accounts\":[{\"AccountId\":\"111111111111\"}]}";
post("/graph/members", create)
.statusCode(200)
.body("Members[0].Status", equalTo("ACCEPTED_BUT_DISABLED"))
.body("UnprocessedAccounts", hasSize(0));
post("/graph/members", create)
.statusCode(200)
.body("Members", hasSize(0))
.body("UnprocessedAccounts[0].AccountId", equalTo("111111111111"));
String monitoringBody = post("/graph/member/monitoringstate",
"{\"GraphArn\":\"" + graphArn + "\",\"AccountId\":\"111111111111\"}")
.statusCode(200).extract().asString();
assertTrue(monitoringBody.isEmpty());
post("/graph/members/list", "{\"GraphArn\":\"" + graphArn + "\",\"MaxResults\":200}")
.statusCode(200).body("MemberDetails[0].Status", equalTo("ENABLED"));
}
@Test
void invalidAdminAccountIdReturnsValidationError() {
post("/orgs/enableAdminAccount", "{\"AccountId\":\"bad\"}")
.statusCode(400).body("__type", equalTo("ValidationException"));
}
private static io.restassured.response.ValidatableResponse post(String path, String body) {
return given()
.contentType("application/json")
.header("Authorization", AUTH)
.body(body)
.post(path)
.then();
}
}
@@ -0,0 +1,130 @@
package io.github.hectorvent.floci.services.detective;
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.detective.model.DetectiveState;
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
import io.github.hectorvent.floci.services.organizations.model.Organization;
import io.github.hectorvent.floci.services.organizations.model.OrganizationAccount;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
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 DetectiveServiceTest {
private static final String REGION = "us-east-1";
private static final String MANAGEMENT_ACCOUNT = "222222222222";
private static final String ADMIN_ACCOUNT = MANAGEMENT_ACCOUNT;
private static final String MEMBER_ACCOUNT = "333333333333";
private RegionResolver regionResolver;
private OrganizationsService organizationsService;
private DetectiveService service;
@BeforeEach
void setUp() {
regionResolver = mock(RegionResolver.class);
organizationsService = mock(OrganizationsService.class);
when(regionResolver.getAccountId()).thenReturn(MANAGEMENT_ACCOUNT);
Organization organization = new Organization();
organization.setMasterAccountId(MANAGEMENT_ACCOUNT);
OrganizationAccount management = new OrganizationAccount();
management.setId(MANAGEMENT_ACCOUNT);
OrganizationAccount member = new OrganizationAccount();
member.setId(MEMBER_ACCOUNT);
when(organizationsService.describeOrganization(MANAGEMENT_ACCOUNT)).thenReturn(organization);
when(organizationsService.listAccounts(MANAGEMENT_ACCOUNT)).thenReturn(java.util.List.of(management, member));
service = new DetectiveService(
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT), regionResolver, organizationsService);
}
@Test
void delegationCreatesGraphForAdministratorAccount() {
service.enableAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
when(regionResolver.getAccountId()).thenReturn(ADMIN_ACCOUNT);
assertTrue(service.requireGraph(REGION).isGraph());
assertEquals(ADMIN_ACCOUNT, service.requireGraph(REGION).getAdminAccountId());
}
@Test
void organizationMemberDoesNotRequireEmailAddress() {
service.enableAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
when(regionResolver.getAccountId()).thenReturn(ADMIN_ACCOUNT);
String graphArn = service.graphArn(REGION);
var member = service.createMember(REGION, graphArn, MEMBER_ACCOUNT, null);
assertEquals(MEMBER_ACCOUNT, member.getAccountId());
assertNull(member.getEmailAddress());
assertEquals("ACCEPTED_BUT_DISABLED", member.getStatus());
}
@Test
void omittedAutoEnableLeavesConfigurationUnchanged() {
service.enableAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
when(regionResolver.getAccountId()).thenReturn(ADMIN_ACCOUNT);
String graphArn = service.graphArn(REGION);
service.updateOrganizationConfiguration(REGION, graphArn, true);
service.updateOrganizationConfiguration(REGION, graphArn, null);
assertTrue(service.requireGraph(REGION).isAutoEnable());
}
@Test
void startMonitoringRequiresAcceptedButDisabledMember() {
service.enableAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
when(regionResolver.getAccountId()).thenReturn(ADMIN_ACCOUNT);
String graphArn = service.graphArn(REGION);
service.createMember(REGION, graphArn, MEMBER_ACCOUNT, null);
assertEquals("ENABLED", service.startMonitoring(REGION, MEMBER_ACCOUNT, graphArn).getStatus());
AwsException error = assertThrows(AwsException.class,
() -> service.startMonitoring(REGION, MEMBER_ACCOUNT, graphArn));
assertEquals("ConflictException", error.getErrorCode());
}
@Test
void nonManagementAccountCannotDesignateAdministrator() {
String memberCaller = MEMBER_ACCOUNT;
Organization organization = new Organization();
organization.setMasterAccountId(MANAGEMENT_ACCOUNT);
when(organizationsService.describeOrganization(memberCaller)).thenReturn(organization);
AwsException error = assertThrows(AwsException.class,
() -> service.enableAdmin(REGION, memberCaller, MANAGEMENT_ACCOUNT));
assertEquals("AccessDeniedException", error.getErrorCode());
assertNull(service.state(REGION).getAdminAccountId());
}
@Test
void administratorMustBelongToOrganization() {
AwsException error = assertThrows(AwsException.class,
() -> service.enableAdmin(REGION, MANAGEMENT_ACCOUNT, "999999999999"));
assertEquals("ValidationException", error.getErrorCode());
assertNull(service.state(REGION).getAdminAccountId());
}
@Test
void clearRemovesAllDetectiveState() {
service.enableAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
assertEquals(ADMIN_ACCOUNT, service.state(REGION).getAdminAccountId());
service.clear();
assertNull(service.state(REGION).getAdminAccountId());
assertFalse(service.state(REGION).isGraph());
}
}