mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(controltower): serve tags for landing zones and enabled controls (#7403)
This commit is contained in:
1 parent
648d363102
commit
ac0662ae9d
11 files changed
+410
-31
No files matched your search
+20
@@ -11,6 +11,7 @@ import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class ControlTowerControlControllerIntegrationTest {
|
||||
@@ -45,6 +46,25 @@ class ControlTowerControlControllerIntegrationTest {
|
||||
.then().statusCode(200).body("controlOperation.operationType", equalTo("RESET_ENABLED_CONTROL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void enabledControlTagsCanBeListedAddedAndRemoved() {
|
||||
String control = "arn:aws:controltower:us-east-1::control/AWS-GR_EBS_OPTIMIZED_INSTANCE";
|
||||
String target = "arn:aws:organizations::000000000000:ou/o-example/ou-example-tagged01";
|
||||
String enabledArn = post("/enable-control", "{\"controlIdentifier\":\"" + control
|
||||
+ "\",\"targetIdentifier\":\"" + target + "\",\"tags\":{\"env\":\"test\"}}")
|
||||
.then().statusCode(200).extract().path("arn");
|
||||
|
||||
given().header("Authorization", auth()).get("/tags/" + enabledArn)
|
||||
.then().statusCode(200).body("tags.env", equalTo("test"));
|
||||
given().contentType("application/json").header("Authorization", auth())
|
||||
.body("{\"tags\":{\"team\":\"platform\"}}").post("/tags/" + enabledArn)
|
||||
.then().statusCode(204);
|
||||
given().header("Authorization", auth()).queryParam("tagKeys", "env").delete("/tags/" + enabledArn)
|
||||
.then().statusCode(204);
|
||||
given().header("Authorization", auth()).get("/tags/" + enabledArn)
|
||||
.then().statusCode(200).body("tags.team", equalTo("platform")).body("tags.env", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateEnableAndMissingResourcesReturnAwsErrors() {
|
||||
String control = "arn:aws:controltower:us-east-1::control/AWS-GR_RDS_STORAGE_ENCRYPTED";
|
||||
|
||||
+104
@@ -16,6 +16,7 @@ import java.util.Map;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -72,6 +73,109 @@ class ControlTowerControllerIntegrationTest {
|
||||
.body("operationIdentifier", containsString("-"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void landingZoneTagsCanBeListedAddedAndRemoved() {
|
||||
String authorization = auth("000000000298", "eu-central-1");
|
||||
String arn = given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"version\":\"4.0\",\"tags\":{\"env\":\"test\"},"
|
||||
+ "\"manifest\":{\"accessManagement\":{\"enabled\":true}}}")
|
||||
.when()
|
||||
.post("/create-landingzone")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("arn");
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("tags.env", equalTo("test"));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"tags\":{\"team\":\"platform\"}}")
|
||||
.when()
|
||||
.post("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.queryParam("tagKeys", "env")
|
||||
.when()
|
||||
.delete("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("tags.team", equalTo("platform"))
|
||||
.body("tags.env", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void landingZoneTagsForAnUnknownLandingZoneReturnResourceNotFound() {
|
||||
String authorization = auth("000000000297", "eu-west-1");
|
||||
String arn = listLandingZonesArn(authorization);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/tags/" + arn.substring(0, arn.lastIndexOf('/') + 1) + "UNKNOWNLZ")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", containsString("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void landingZoneTagsForAnotherAccountAreNotFoundAndSeedNothing() {
|
||||
String caller = auth("000000000296", EAST);
|
||||
String other = listLandingZonesArn(auth("000000000295", EAST));
|
||||
|
||||
given()
|
||||
.header("Authorization", caller)
|
||||
.when()
|
||||
.get("/tags/" + other)
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", containsString("ResourceNotFoundException"));
|
||||
|
||||
assertTrue(listLandingZonesArn(caller).contains(":000000000296:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void landingZoneTagRequestsWithMalformedTagsAreRejected() {
|
||||
String authorization = auth("000000000294", EAST);
|
||||
String arn = listLandingZonesArn(authorization);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"tags\":{\"env\":123}}")
|
||||
.when()
|
||||
.post("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.delete("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLandingZoneAcceptsLandingZoneIdLikeTheTerraformProvider() {
|
||||
String authorization = auth("000000000250", EAST);
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package io.github.hectorvent.floci.services.controltower;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class ControlTowerTagsTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void fromRequest_absentTags_returnsEmpty() {
|
||||
assertEquals(Map.of(), ControlTowerTags.fromRequest(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromRequest_stringValues_returnsThem() throws Exception {
|
||||
assertEquals(Map.of("env", "test"), ControlTowerTags.fromRequest(mapper.readTree("{\"env\":\"test\"}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromRequest_nonStringValue_isRejected() throws Exception {
|
||||
JsonNode tags = mapper.readTree("{\"env\":123}");
|
||||
assertValidation(() -> ControlTowerTags.fromRequest(tags));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_moreThan200Entries_isRejected() {
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
for (int i = 0; i <= 200; i++) {
|
||||
tags.put("key" + i, "value");
|
||||
}
|
||||
assertValidation(() -> ControlTowerTags.validate(tags));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_blankOrLongKeyOrLongValue_isRejected() {
|
||||
assertValidation(() -> ControlTowerTags.validate(Map.of(" ", "value")));
|
||||
assertValidation(() -> ControlTowerTags.validate(Map.of("k".repeat(129), "value")));
|
||||
assertValidation(() -> ControlTowerTags.validate(Map.of("key", "v".repeat(257))));
|
||||
}
|
||||
|
||||
private static void assertValidation(Runnable call) {
|
||||
AwsException e = assertThrows(AwsException.class, call::run);
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
+24
@@ -263,6 +263,30 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
eks().get("/tags/" + cluster).then().statusCode(200).body("tags.team", equalTo("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void controlTowerTaggingIsChecked() {
|
||||
// Control Tower requests name their resources in the path, which RequestResources does not read yet,
|
||||
// so its tagging is checked on *.
|
||||
String user = user("route-controltower-tags");
|
||||
String landingZone = "arn:aws:controltower:us-east-1:" + ACCOUNT + ":landingzone/ROUTE" + suffix();
|
||||
|
||||
refused(as(user, "controltower").get("/tags/" + landingZone).then(), "controltower:ListTagsForResource");
|
||||
refused(as(user, "controltower").body("{\"tags\":{\"team\":\"a\"}}").post("/tags/" + landingZone).then(),
|
||||
"controltower:TagResource");
|
||||
refused(as(user, "controltower").queryParam("tagKeys", "team").delete("/tags/" + landingZone).then(),
|
||||
"controltower:UntagResource");
|
||||
// The shared controller also serves the bare /tags path, naming the ARN in the query or the body.
|
||||
refused(as(user, "controltower").queryParam("resourceArn", landingZone).get("/tags").then(),
|
||||
"controltower:ListTagsForResource");
|
||||
refused(as(user, "controltower").body("{\"resourceArn\":\"" + landingZone + "\",\"tags\":{\"team\":\"a\"}}")
|
||||
.post("/tags").then(), "controltower:TagResource");
|
||||
refused(as(user, "controltower").queryParam("resourceArn", landingZone).queryParam("tagKeys", "team")
|
||||
.delete("/tags").then(), "controltower:UntagResource");
|
||||
|
||||
policy(user, allow("controltower:ListTagsForResource", "*"));
|
||||
as(user, "controltower").get("/tags/" + landingZone).then().statusCode(not(403));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTrailingSlashDoesNotLetARouteThroughUnchecked() {
|
||||
// The router matches /clusters/ as /clusters, so the request is checked as the route it reaches.
|
||||
|
||||
Reference in new issue
Block a user