mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(controltower): add enabled control operations (#3056)
* feat(controltower): add enabled control operations * fix(controltower): harden baseline state and pagination * fix(controltower): register persisted control model
This commit is contained in:
1 parent
615056999e
commit
b199894e19
12 files changed
+817
-75
No files matched your search
+91
@@ -0,0 +1,91 @@
|
||||
package io.github.hectorvent.floci.services.controltower;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class ControlTowerControlControllerIntegrationTest {
|
||||
private static final String TARGET = "arn:aws:organizations::000000000000:ou/o-example/ou-example-12345678";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); }
|
||||
|
||||
@Test
|
||||
void enableListGetUpdateResetAndPoll() {
|
||||
String control = "arn:aws:controlcatalog:::control/7mo7a2h2ebsq71l8k6uzr96ou";
|
||||
String enabledArn = post("/enable-control", "{\"controlIdentifier\":\"" + control
|
||||
+ "\",\"targetIdentifier\":\"" + TARGET + "\",\"tags\":{\"managed_by\":\"cloud-launchpad\"}}")
|
||||
.then().statusCode(200).body("operationIdentifier", notNullValue()).extract().path("arn");
|
||||
|
||||
post("/list-enabled-controls", "{\"targetIdentifier\":\"" + TARGET + "\"}")
|
||||
.then().statusCode(200).body("enabledControls", hasSize(1))
|
||||
.body("enabledControls[0].controlIdentifier", equalTo(control));
|
||||
post("/get-enabled-control", "{\"enabledControlIdentifier\":\"" + enabledArn + "\"}")
|
||||
.then().statusCode(200).body("enabledControlDetails.statusSummary.status", equalTo("SUCCEEDED"));
|
||||
|
||||
String update = post("/update-enabled-control", "{\"enabledControlIdentifier\":\"" + enabledArn
|
||||
+ "\",\"parameters\":[{\"key\":\"ExemptedPrincipalArns\",\"value\":[\"arn:aws:iam::000000000000:role/Admin\"]}]}")
|
||||
.then().statusCode(200).extract().path("operationIdentifier");
|
||||
post("/get-control-operation", "{\"operationIdentifier\":\"" + update + "\"}")
|
||||
.then().statusCode(200).body("controlOperation.operationType", equalTo("UPDATE_ENABLED_CONTROL"))
|
||||
.body("controlOperation.status", equalTo("SUCCEEDED"));
|
||||
|
||||
String reset = post("/reset-enabled-control", "{\"enabledControlIdentifier\":\"" + enabledArn + "\"}")
|
||||
.then().statusCode(200).extract().path("operationIdentifier");
|
||||
post("/get-control-operation", "{\"operationIdentifier\":\"" + reset + "\"}")
|
||||
.then().statusCode(200).body("controlOperation.operationType", equalTo("RESET_ENABLED_CONTROL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateEnableAndMissingResourcesReturnAwsErrors() {
|
||||
String control = "arn:aws:controltower:us-east-1::control/AWS-GR_RDS_STORAGE_ENCRYPTED";
|
||||
String body = "{\"controlIdentifier\":\"" + control + "\",\"targetIdentifier\":\"" + TARGET + "\"}";
|
||||
post("/enable-control", body).then().statusCode(200);
|
||||
post("/enable-control", body).then().statusCode(409).body("__type", containsString("ConflictException"));
|
||||
post("/get-enabled-control", "{\"enabledControlIdentifier\":\"arn:aws:controltower:us-east-1:000000000000:enabledcontrol/missing\"}")
|
||||
.then().statusCode(404).body("__type", containsString("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateRequiresDifferentParametersAndResetRejectsScp() {
|
||||
String control = "arn:aws:controltower:us-east-1::control/AWS-GR_RESTRICT_ROOT_USER";
|
||||
String enabledArn = post("/enable-control", "{\"controlIdentifier\":\"" + control
|
||||
+ "\",\"targetIdentifier\":\"" + TARGET + "\",\"parameters\":[{\"key\":\"ExemptAssumeRoot\",\"value\":false}]}")
|
||||
.then().statusCode(200).extract().path("arn");
|
||||
post("/update-enabled-control", "{\"enabledControlIdentifier\":\"" + enabledArn
|
||||
+ "\",\"parameters\":[{\"key\":\"ExemptAssumeRoot\",\"value\":false}]}")
|
||||
.then().statusCode(400).body("__type", containsString("ValidationException"));
|
||||
post("/reset-enabled-control", "{\"enabledControlIdentifier\":\"" + enabledArn + "\"}")
|
||||
.then().statusCode(400).body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEnabledControlsRejectsNegativeNextToken() {
|
||||
post("/list-enabled-controls", "{\"nextToken\":\"-1\"}")
|
||||
.then().statusCode(400).body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validationRejectsMalformedIdentifiersAndOperationIds() {
|
||||
post("/enable-control", "{\"controlIdentifier\":\"bad\",\"targetIdentifier\":\"" + TARGET + "\"}")
|
||||
.then().statusCode(400).body("__type", containsString("ValidationException"));
|
||||
post("/get-control-operation", "{\"operationIdentifier\":\"bad\"}")
|
||||
.then().statusCode(400).body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
private static io.restassured.response.Response post(String path, String body) {
|
||||
return given().contentType("application/json").header("Authorization", auth()).body(body).post(path);
|
||||
}
|
||||
|
||||
private static String auth() {
|
||||
return "AWS4-HMAC-SHA256 Credential=000000000000/20260904/us-east-1/controltower/aws4_request";
|
||||
}
|
||||
}
|
||||
+18
-4
@@ -1,6 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.controltower;
|
||||
|
||||
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.OrganizationalUnit;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import jakarta.inject.Inject;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
@@ -23,6 +27,9 @@ class ControlTowerControllerIntegrationTest {
|
||||
|
||||
private static final String EAST = "us-east-1";
|
||||
|
||||
@Inject
|
||||
OrganizationsService organizationsService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
@@ -263,7 +270,7 @@ class ControlTowerControllerIntegrationTest {
|
||||
.filter(e -> identityCenterBaselineArn.equals(e.get("baselineIdentifier")))
|
||||
.findFirst().orElseThrow().get("arn").toString();
|
||||
|
||||
String ouArn = "arn:aws:organizations::000000000204:ou/o-floci0001/ou-abcd-33333333";
|
||||
String ouArn = createOuArn("000000000204", "register-ou");
|
||||
String enableBody = """
|
||||
{"baselineIdentifier":"%s","baselineVersion":"5.0","targetIdentifier":"%s",
|
||||
"parameters":[{"key":"IdentityCenterEnabledBaselineArn","value":"%s"}]}
|
||||
@@ -321,7 +328,7 @@ class ControlTowerControllerIntegrationTest {
|
||||
String baselineArn = ((List<Map<String, Object>>) baselines.path("baselines")).stream()
|
||||
.filter(b -> "AWSControlTowerBaseline".equals(b.get("name")))
|
||||
.findFirst().orElseThrow().get("arn").toString();
|
||||
String targetArn = "arn:aws:organizations::000000000207:ou/o-floci0001/ou-update-00000001";
|
||||
String targetArn = createOuArn("000000000207", "update-ou");
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
@@ -349,7 +356,7 @@ class ControlTowerControllerIntegrationTest {
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"enabledBaselineIdentifier\":\"" + enabledArn
|
||||
+ "\",\"baselineVersion\":\"6.0\",\"parameters\":[{\"key\":\"Example\",\"value\":{\"enabled\":true}}]}")
|
||||
+ "\",\"baselineVersion\":\"4.0\",\"parameters\":[{\"key\":\"Example\",\"value\":{\"enabled\":true}}]}")
|
||||
.when()
|
||||
.post("/update-enabled-baseline")
|
||||
.then()
|
||||
@@ -376,7 +383,7 @@ class ControlTowerControllerIntegrationTest {
|
||||
.post("/get-enabled-baseline")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("enabledBaselineDetails.baselineVersion", equalTo("6.0"))
|
||||
.body("enabledBaselineDetails.baselineVersion", equalTo("4.0"))
|
||||
.body("enabledBaselineDetails.parameters[0].key", equalTo("Example"))
|
||||
.body("enabledBaselineDetails.parameters[0].value.enabled", equalTo(true));
|
||||
}
|
||||
@@ -426,6 +433,13 @@ class ControlTowerControllerIntegrationTest {
|
||||
.body(containsString("controltower"));
|
||||
}
|
||||
|
||||
private String createOuArn(String accountId, String name) {
|
||||
Organization organization = organizationsService.createOrganization(accountId, "ALL");
|
||||
OrganizationalUnit unit = organizationsService.createOrganizationalUnit(
|
||||
accountId, organization.getRoot().getId(), name, Map.of());
|
||||
return unit.getArn();
|
||||
}
|
||||
|
||||
private static String listLandingZonesArn(String authorization) {
|
||||
Response response = given()
|
||||
.header("Authorization", authorization)
|
||||
|
||||
+60
-9
@@ -206,7 +206,9 @@ class ControlTowerServiceTest {
|
||||
// Another account, and another region of the same account, must not see it at all.
|
||||
assertTrue(operationIdentifiers("000000000102", REGION).isEmpty());
|
||||
assertTrue(operationIdentifiers(ACCOUNT, "us-west-2").isEmpty());
|
||||
assertEquals("UPDATE", service.getOperationType("000000000102", REGION, opId));
|
||||
AwsException foreign = assertThrows(AwsException.class,
|
||||
() -> service.getOperationType("000000000102", REGION, opId));
|
||||
assertEquals("ResourceNotFoundException", foreign.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -312,7 +314,7 @@ class ControlTowerServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOperationTypeReportsUpdateForIssuedAndUnknownIds() throws Exception {
|
||||
void getOperationTypeReportsUpdateForIssuedAndRejectsUnknownIds() throws Exception {
|
||||
JsonNode request = objectMapper.readTree("""
|
||||
{"version":"4.0","landingZoneIdentifier":"%s",
|
||||
"manifest":{"securityRoles":{"enabled":true},"accessManagement":{"enabled":true}}}
|
||||
@@ -320,7 +322,12 @@ class ControlTowerServiceTest {
|
||||
String opId = service.updateLandingZone(ACCOUNT, REGION, request);
|
||||
|
||||
assertEquals("UPDATE", service.getOperationType(ACCOUNT, REGION, opId));
|
||||
assertNotNull(service.getOperationType(ACCOUNT, REGION, "never-issued"));
|
||||
AwsException invalid = assertThrows(AwsException.class,
|
||||
() -> service.getOperationType(ACCOUNT, REGION, "never-issued"));
|
||||
assertEquals("ValidationException", invalid.getErrorCode());
|
||||
AwsException unknown = assertThrows(AwsException.class,
|
||||
() -> service.getOperationType(ACCOUNT, REGION, "11111111-1111-4111-8111-111111111111"));
|
||||
assertEquals("ResourceNotFoundException", unknown.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -421,11 +428,11 @@ class ControlTowerServiceTest {
|
||||
assertEquals("5.0", stored.getBaselineVersion());
|
||||
assertEquals("SUCCEEDED", stored.getStatus());
|
||||
|
||||
assertEquals("BASELINE_ENABLED", service.getBaselineOperationType(ACCOUNT, REGION, result.operationIdentifier()));
|
||||
assertEquals("ENABLE_BASELINE", service.getBaselineOperationType(ACCOUNT, REGION, result.operationIdentifier()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableBaselineTwiceForSameTargetReplacesNotDuplicates() throws Exception {
|
||||
void enableBaselineTwiceForSameTargetReturnsConflict() throws Exception {
|
||||
String ctBaselineArn = service.listBaselines(REGION).stream()
|
||||
.filter(b -> "AWSControlTowerBaseline".equals(b.get("name").asText()))
|
||||
.findFirst().orElseThrow().get("arn").asText();
|
||||
@@ -435,7 +442,10 @@ class ControlTowerServiceTest {
|
||||
{"baselineIdentifier":"%s","baselineVersion":"5.0","targetIdentifier":"%s"}
|
||||
""".formatted(ctBaselineArn, ouArn));
|
||||
service.enableBaseline(ACCOUNT, REGION, request);
|
||||
service.enableBaseline(ACCOUNT, REGION, request);
|
||||
AwsException duplicate = assertThrows(AwsException.class,
|
||||
() -> service.enableBaseline(ACCOUNT, REGION, request));
|
||||
assertEquals("ConflictException", duplicate.getErrorCode());
|
||||
assertEquals(409, duplicate.getHttpStatus());
|
||||
|
||||
long matching = service.listEnabledBaselines(ACCOUNT, REGION).stream()
|
||||
.filter(e -> ouArn.equals(e.getTargetIdentifier()))
|
||||
@@ -543,6 +553,47 @@ class ControlTowerServiceTest {
|
||||
assertNull(second.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyEnabledBaselineKeysMigrateWithoutDuplicates() throws Exception {
|
||||
InMemoryStorage<String, EnabledBaseline> baselines = new InMemoryStorage<>();
|
||||
String baselineArn = "arn:aws:controltower:us-east-1::baseline/17BSJV3IGJ2QSGA2";
|
||||
String targetArn = "arn:aws:organizations::000000000101:ou/o-floci0001/ou-legacy-00000001";
|
||||
String enabledArn = "arn:aws:controltower:us-east-1:000000000101:enabledbaseline/legacy000001";
|
||||
EnabledBaseline legacy = new EnabledBaseline(
|
||||
enabledArn, baselineArn, "5.0", targetArn, "SUCCEEDED", null);
|
||||
baselines.put(REGION + "::" + targetArn, legacy);
|
||||
ControlTowerService migrated = new ControlTowerService(new InMemoryStorage<>(), baselines);
|
||||
|
||||
long matching = migrated.listEnabledBaselines(ACCOUNT, REGION).stream()
|
||||
.filter(entry -> targetArn.equals(entry.getTargetIdentifier()))
|
||||
.count();
|
||||
assertEquals(1, matching);
|
||||
assertTrue(baselines.get(REGION + "::" + targetArn).isEmpty());
|
||||
assertTrue(baselines.get(REGION + "::" + targetArn + "::" + baselineArn).isPresent());
|
||||
|
||||
JsonNode duplicateRequest = objectMapper.readTree("""
|
||||
{"baselineIdentifier":"%s","baselineVersion":"5.0","targetIdentifier":"%s"}
|
||||
""".formatted(baselineArn, targetArn));
|
||||
AwsException duplicate = assertThrows(AwsException.class,
|
||||
() -> migrated.enableBaseline(ACCOUNT, REGION, duplicateRequest));
|
||||
assertEquals("ConflictException", duplicate.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void negativeNextTokensReturnValidationException() throws Exception {
|
||||
AwsException baselines = assertThrows(AwsException.class,
|
||||
() -> service.listEnabledBaselines(ACCOUNT, REGION,
|
||||
objectMapper.readTree("{\"nextToken\":\"-1\"}")));
|
||||
assertEquals("ValidationException", baselines.getErrorCode());
|
||||
assertEquals(400, baselines.getHttpStatus());
|
||||
|
||||
AwsException operations = assertThrows(AwsException.class,
|
||||
() -> service.listLandingZoneOperations(ACCOUNT, REGION,
|
||||
objectMapper.readTree("{\"nextToken\":\"-1\"}")));
|
||||
assertEquals("ValidationException", operations.getErrorCode());
|
||||
assertEquals(400, operations.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resetEnabledBaselineReturnsOperationIdentifierForValidBaseline() throws Exception {
|
||||
String baselineArn = service.listBaselines(REGION).stream()
|
||||
@@ -562,7 +613,7 @@ class ControlTowerServiceTest {
|
||||
String opId = service.resetEnabledBaseline(ACCOUNT, REGION, enabled.getArn());
|
||||
assertNotNull(opId);
|
||||
assertFalse(opId.isBlank());
|
||||
assertEquals("BASELINE_RESET", service.getBaselineOperationType(ACCOUNT, REGION, opId));
|
||||
assertEquals("RESET_ENABLED_BASELINE", service.getBaselineOperationType(ACCOUNT, REGION, opId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -608,13 +659,13 @@ class ControlTowerServiceTest {
|
||||
.findFirst().orElseThrow();
|
||||
|
||||
JsonNode updateRequest = objectMapper.readTree("""
|
||||
{"enabledBaselineIdentifier":"%s","baselineVersion":"6.0",
|
||||
{"enabledBaselineIdentifier":"%s","baselineVersion":"4.0",
|
||||
"parameters":[{"key":"Example","value":{"enabled":true}}]}
|
||||
""".formatted(enabled.getArn()));
|
||||
String opId = service.updateEnabledBaseline(ACCOUNT, REGION, updateRequest);
|
||||
|
||||
EnabledBaseline updated = service.getEnabledBaseline(ACCOUNT, REGION, enabled.getArn());
|
||||
assertEquals("6.0", updated.getBaselineVersion());
|
||||
assertEquals("4.0", updated.getBaselineVersion());
|
||||
assertEquals(true, updated.getParameters().path(0).path("value").path("enabled").asBoolean());
|
||||
assertEquals("UPDATE_ENABLED_BASELINE", service.getBaselineOperationType(ACCOUNT, REGION, opId));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user