mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(inspector2): add organization configuration operations (#3162)
* feat(inspector2): add organization configuration operations * fix(inspector2): complete organization contracts * fix(inspector2): scope organization account state * test(inspector2): log tolerated organization state * test(inspector2): isolate organization cleanup
This commit is contained in:
1 parent
e313ab5b1d
commit
4944764fa3
17 files changed
+1137
-1
No files matched your search
+169
@@ -0,0 +1,169 @@
|
||||
package io.github.hectorvent.floci.services.inspector2;
|
||||
|
||||
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
@QuarkusTest
|
||||
class Inspector2IntegrationTest {
|
||||
|
||||
@Inject
|
||||
OrganizationsService organizationsService;
|
||||
|
||||
@Test
|
||||
void delegatedAdminEnablementAndOrganizationConfiguration() {
|
||||
String management = "940000000001";
|
||||
String administrator = "940000000002";
|
||||
createOrganization(management, administrator);
|
||||
|
||||
given().contentType("application/json").header("Authorization", auth(management)).body("{}")
|
||||
.post("/delegatedadminaccounts/list").then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", auth(management))
|
||||
.body("{\"delegatedAdminAccountId\":\"" + administrator + "\"}")
|
||||
.post("/delegatedadminaccounts/enable").then().statusCode(200)
|
||||
.body("delegatedAdminAccountId", equalTo(administrator));
|
||||
given().contentType("application/json").header("Authorization", auth(administrator))
|
||||
.body("{\"autoEnable\":{\"ec2\":true,\"ecr\":true,\"lambda\":true,"
|
||||
+ "\"lambdaCode\":true,\"codeRepository\":true}}")
|
||||
.post("/organizationconfiguration/update").then().statusCode(200)
|
||||
.body("autoEnable.codeRepository", equalTo(true));
|
||||
given().contentType("application/json").header("Authorization", auth(administrator)).body("{}")
|
||||
.post("/organizationconfiguration/describe").then().statusCode(200)
|
||||
.body("autoEnable.ec2", equalTo(true))
|
||||
.body("autoEnable.ecr", equalTo(true))
|
||||
.body("autoEnable.lambda", equalTo(true))
|
||||
.body("autoEnable.lambdaCode", equalTo(true))
|
||||
.body("autoEnable.codeRepository", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void managementAuthorizationAndMembershipAreEnforced() {
|
||||
String management = "940000000011";
|
||||
String administrator = "940000000012";
|
||||
String member = "940000000013";
|
||||
String outsider = "950000000014";
|
||||
createOrganization(management, administrator, member);
|
||||
|
||||
given().contentType("application/json").header("Authorization", auth(member))
|
||||
.body("{\"delegatedAdminAccountId\":\"" + administrator + "\"}")
|
||||
.post("/delegatedadminaccounts/enable").then().statusCode(403)
|
||||
.body("__type", equalTo("AccessDeniedException"));
|
||||
given().contentType("application/json").header("Authorization", auth(management))
|
||||
.body("{\"delegatedAdminAccountId\":\"" + outsider + "\"}")
|
||||
.post("/delegatedadminaccounts/enable").then().statusCode(404)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void delegatedAdminManagesMemberPerResourceState() {
|
||||
String management = "940000000021";
|
||||
String administrator = "940000000022";
|
||||
String member = "940000000023";
|
||||
createOrganization(management, administrator, member);
|
||||
designateAdministrator(management, administrator);
|
||||
|
||||
given().contentType("application/json").header("Authorization", auth(administrator))
|
||||
.body("{\"accountIds\":[\"" + member + "\"],\"resourceTypes\":[\"EC2\"]}")
|
||||
.post("/enable").then().statusCode(200)
|
||||
.body("accounts[0].accountId", equalTo(member))
|
||||
.body("accounts[0].resourceStatus.ec2", equalTo("ENABLING"))
|
||||
.body("accounts[0].resourceStatus.ecr", equalTo("DISABLED"));
|
||||
|
||||
String body = "{\"accountIds\":[\"" + member + "\"]}";
|
||||
given().contentType("application/json").header("Authorization", auth(administrator)).body(body)
|
||||
.post("/status/batch/get").then().statusCode(200)
|
||||
.body("accounts", hasSize(1))
|
||||
.body("accounts[0].accountId", equalTo(member))
|
||||
.body("accounts[0].state.status", equalTo("ENABLING"))
|
||||
.body("accounts[0].resourceState.ec2.status", equalTo("ENABLING"))
|
||||
.body("accounts[0].resourceState.ecr.status", equalTo("DISABLED"));
|
||||
given().contentType("application/json").header("Authorization", auth(administrator)).body(body)
|
||||
.post("/status/batch/get").then().statusCode(200)
|
||||
.body("accounts[0].state.status", equalTo("ENABLED"))
|
||||
.body("accounts[0].resourceState.ec2.status", equalTo("ENABLED"))
|
||||
.body("accounts[0].resourceState.ecr.status", equalTo("DISABLED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void memberCanEnableItselfWithOmittedAccountIds() {
|
||||
String management = "940000000031";
|
||||
String member = "940000000032";
|
||||
createOrganization(management, member);
|
||||
|
||||
given().contentType("application/json").header("Authorization", auth(member))
|
||||
.body("{\"resourceTypes\":[\"ECR\"]}")
|
||||
.post("/enable").then().statusCode(200)
|
||||
.body("accounts[0].accountId", equalTo(member))
|
||||
.body("accounts[0].resourceStatus.ecr", equalTo("ENABLING"));
|
||||
given().contentType("application/json").header("Authorization", auth(member)).body("{}")
|
||||
.post("/status/batch/get").then().statusCode(200)
|
||||
.body("accounts", hasSize(1))
|
||||
.body("accounts[0].accountId", equalTo(member));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedOrganizationUpdateDoesNotPartiallyMutateState() {
|
||||
String management = "940000000041";
|
||||
String administrator = "940000000042";
|
||||
createOrganization(management, administrator);
|
||||
designateAdministrator(management, administrator);
|
||||
|
||||
given().contentType("application/json").header("Authorization", auth(administrator))
|
||||
.body("{\"autoEnable\":{\"ec2\":false,\"ecr\":false}}")
|
||||
.post("/organizationconfiguration/update").then().statusCode(200);
|
||||
given().contentType("application/json").header("Authorization", auth(administrator))
|
||||
.body("{\"autoEnable\":{\"ec2\":true}}")
|
||||
.post("/organizationconfiguration/update").then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
given().contentType("application/json").header("Authorization", auth(administrator)).body("{}")
|
||||
.post("/organizationconfiguration/describe").then().statusCode(200)
|
||||
.body("autoEnable.ec2", equalTo(false))
|
||||
.body("autoEnable.ecr", equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void delegatedAdminCanBeDisabledByManagementAccount() {
|
||||
String management = "940000000051";
|
||||
String administrator = "940000000052";
|
||||
createOrganization(management, administrator);
|
||||
designateAdministrator(management, administrator);
|
||||
given().contentType("application/json").header("Authorization", auth(management))
|
||||
.body("{\"delegatedAdminAccountId\":\"" + administrator + "\"}")
|
||||
.post("/delegatedadminaccounts/disable").then().statusCode(200)
|
||||
.body("delegatedAdminAccountId", equalTo(administrator));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidAccountIdReturnsValidationException() {
|
||||
String management = "940000000061";
|
||||
organizationsService.createOrganization(management, "ALL");
|
||||
given().contentType("application/json").header("Authorization", auth(management))
|
||||
.body("{\"delegatedAdminAccountId\":\"bad\"}")
|
||||
.post("/delegatedadminaccounts/enable").then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
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("{\"delegatedAdminAccountId\":\"" + administratorAccountId + "\"}")
|
||||
.post("/delegatedadminaccounts/enable").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String auth(String accountId) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + accountId + "/20260101/us-east-1/inspector2/aws4_request";
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package io.github.hectorvent.floci.services.inspector2;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.services.inspector2.model.InspectorState;
|
||||
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
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 Inspector2ServiceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String MANAGEMENT_ACCOUNT = "222222222222";
|
||||
private static final String ADMIN_ACCOUNT = "111111111111";
|
||||
private static final String MEMBER_ACCOUNT = "333333333333";
|
||||
private static final String OUTSIDE_ACCOUNT = "444444444444";
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private Inspector2Service service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
OrganizationsService organizations = mock(OrganizationsService.class);
|
||||
when(organizations.findManagementAccountForResource(MANAGEMENT_ACCOUNT))
|
||||
.thenReturn(Optional.of(MANAGEMENT_ACCOUNT));
|
||||
when(organizations.findManagementAccountForResource(ADMIN_ACCOUNT))
|
||||
.thenReturn(Optional.of(MANAGEMENT_ACCOUNT));
|
||||
when(organizations.findManagementAccountForResource(MEMBER_ACCOUNT))
|
||||
.thenReturn(Optional.of(MANAGEMENT_ACCOUNT));
|
||||
when(organizations.findManagementAccountForResource(OUTSIDE_ACCOUNT))
|
||||
.thenReturn(Optional.of(OUTSIDE_ACCOUNT));
|
||||
service = new Inspector2Service(
|
||||
AccountAwareStorageBackend.<InspectorState>inMemory(MANAGEMENT_ACCOUNT), organizations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyManagementAccountCanDesignateDelegatedAdministrator() {
|
||||
AwsException denied = assertThrows(AwsException.class,
|
||||
() -> service.enableDelegatedAdmin(REGION, MEMBER_ACCOUNT, ADMIN_ACCOUNT));
|
||||
assertEquals("AccessDeniedException", denied.getErrorCode());
|
||||
|
||||
AwsException outsider = assertThrows(AwsException.class,
|
||||
() -> service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, OUTSIDE_ACCOUNT));
|
||||
assertEquals("ResourceNotFoundException", outsider.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void delegatedAdministratorOwnsOrganizationConfiguration() throws Exception {
|
||||
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
|
||||
|
||||
InspectorState state = service.updateOrganizationConfiguration(REGION, ADMIN_ACCOUNT,
|
||||
objectMapper.readTree("{\"autoEnable\":{\"ec2\":true,\"ecr\":true,\"codeRepository\":true}}"));
|
||||
|
||||
assertTrue(state.isAutoEnableEc2());
|
||||
assertTrue(state.isAutoEnableEcr());
|
||||
assertTrue(state.isAutoEnableCodeRepository());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedOrganizationConfigurationUpdateIsAtomic() throws Exception {
|
||||
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
|
||||
service.updateOrganizationConfiguration(REGION, ADMIN_ACCOUNT,
|
||||
objectMapper.readTree("{\"autoEnable\":{\"ec2\":false,\"ecr\":false}}"));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.updateOrganizationConfiguration(REGION, ADMIN_ACCOUNT,
|
||||
objectMapper.readTree("{\"autoEnable\":{\"ec2\":true}}")));
|
||||
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
InspectorState current = service.organizationConfiguration(REGION, ADMIN_ACCOUNT);
|
||||
assertFalse(current.isAutoEnableEc2());
|
||||
assertFalse(current.isAutoEnableEcr());
|
||||
}
|
||||
|
||||
@Test
|
||||
void delegatedAdministratorEnablesMemberAndOnlyRequestedResourceTypes() throws Exception {
|
||||
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
|
||||
|
||||
service.enable(REGION, ADMIN_ACCOUNT, objectMapper.readTree(
|
||||
"{\"accountIds\":[\"" + MEMBER_ACCOUNT + "\"],\"resourceTypes\":[\"EC2\"]}"));
|
||||
|
||||
InspectorState first = service.accountStatus(REGION, ADMIN_ACCOUNT, MEMBER_ACCOUNT);
|
||||
assertEquals("ENABLING", first.getStatus());
|
||||
assertEquals("ENABLING", first.getEc2Status());
|
||||
assertEquals("DISABLED", first.getEcrStatus());
|
||||
assertEquals("DISABLED", first.getLambdaStatus());
|
||||
InspectorState converged = service.accountStatus(REGION, ADMIN_ACCOUNT, MEMBER_ACCOUNT);
|
||||
assertEquals("ENABLED", converged.getStatus());
|
||||
assertEquals("ENABLED", converged.getEc2Status());
|
||||
assertEquals("DISABLED", converged.getEcrStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void memberMayEnableItselfButCannotManageAnotherAccount() throws Exception {
|
||||
service.enable(REGION, MEMBER_ACCOUNT, objectMapper.readTree(
|
||||
"{\"resourceTypes\":[\"ECR\"]}"));
|
||||
assertEquals("ENABLING", service.accountStatus(REGION, MEMBER_ACCOUNT, MEMBER_ACCOUNT).getEcrStatus());
|
||||
|
||||
AwsException denied = assertThrows(AwsException.class,
|
||||
() -> service.enable(REGION, MEMBER_ACCOUNT, objectMapper.readTree(
|
||||
"{\"accountIds\":[\"" + ADMIN_ACCOUNT + "\"],\"resourceTypes\":[\"EC2\"]}")));
|
||||
assertEquals("AccessDeniedException", denied.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void managementAccountCannotUpdateOrganizationConfiguration() throws Exception {
|
||||
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.updateOrganizationConfiguration(REGION, MANAGEMENT_ACCOUNT,
|
||||
objectMapper.readTree("{\"autoEnable\":{\"ec2\":true,\"ecr\":true}}")));
|
||||
|
||||
assertEquals("AccessDeniedException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disableRequiresManagementAndCurrentAdministrator() {
|
||||
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
|
||||
|
||||
AwsException denied = assertThrows(AwsException.class,
|
||||
() -> service.disableDelegatedAdmin(REGION, ADMIN_ACCOUNT, ADMIN_ACCOUNT));
|
||||
assertEquals("AccessDeniedException", denied.getErrorCode());
|
||||
|
||||
AwsException missing = assertThrows(AwsException.class,
|
||||
() -> service.disableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, MEMBER_ACCOUNT));
|
||||
assertEquals("ResourceNotFoundException", missing.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearRemovesState() {
|
||||
service.enableDelegatedAdmin(REGION, MANAGEMENT_ACCOUNT, ADMIN_ACCOUNT);
|
||||
service.clear();
|
||||
|
||||
assertNull(service.state(REGION).getAdminAccountId());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user