mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(guardduty): add GuardDuty detector and organization configuration emulation (#2346)
This commit is contained in:
1 parent
291a97473e
commit
6779aa8270
28 files changed
+2388
-9
No files matched your search
+314
@@ -0,0 +1,314 @@
|
||||
package io.github.hectorvent.floci.services.guardduty;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** Verifies the GuardDuty restJson1 detector lifecycle, organization readback, and isolation. */
|
||||
@QuarkusTest
|
||||
class GuardDutyControllerIntegrationTest {
|
||||
|
||||
private static final String EAST = "us-east-1";
|
||||
private static final String WEST = "us-west-2";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectorCreateGetUpdateDeleteLifecycle() {
|
||||
String authorization = auth("000000000201", EAST);
|
||||
String detectorId = createDetector(authorization, """
|
||||
{"enable":true,"findingPublishingFrequency":"SIX_HOURS","tags":{"env":"compat"}}
|
||||
""");
|
||||
|
||||
Response detector = given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("ENABLED"))
|
||||
.body("findingPublishingFrequency", equalTo("SIX_HOURS"))
|
||||
.body("serviceRole", notNullValue())
|
||||
.body("tags.env", equalTo("compat"))
|
||||
.extract().response();
|
||||
assertTrue(detector.path("createdAt").toString().endsWith("Z"));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"enable\":false,\"findingPublishingFrequency\":\"ONE_HOUR\"}")
|
||||
.when()
|
||||
.post("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("DISABLED"))
|
||||
.body("findingPublishingFrequency", equalTo("ONE_HOUR"));
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("detectorIds", equalTo(List.of(detectorId)));
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.delete("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void featureOrderIsPreservedOnReadBack() {
|
||||
String authorization = auth("000000000202", EAST);
|
||||
String detectorId = createDetector(authorization, """
|
||||
{"enable":true,"features":[
|
||||
{"name":"RUNTIME_MONITORING","status":"ENABLED","additionalConfiguration":[
|
||||
{"name":"ECS_FARGATE_AGENT_MANAGEMENT","status":"ENABLED"},
|
||||
{"name":"EC2_AGENT_MANAGEMENT","status":"ENABLED"},
|
||||
{"name":"EKS_ADDON_MANAGEMENT","status":"DISABLED"}
|
||||
]},
|
||||
{"name":"S3_DATA_EVENTS","status":"ENABLED"}
|
||||
]}
|
||||
""");
|
||||
|
||||
Response detector = given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().response();
|
||||
|
||||
assertEquals(List.of("RUNTIME_MONITORING", "S3_DATA_EVENTS"),
|
||||
detector.path("features.name"));
|
||||
assertEquals(
|
||||
List.of("ECS_FARGATE_AGENT_MANAGEMENT", "EC2_AGENT_MANAGEMENT", "EKS_ADDON_MANAGEMENT"),
|
||||
detector.path("features[0].additionalConfiguration.name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void organizationConfigurationLifecycle() {
|
||||
String authorization = auth("000000000203", EAST);
|
||||
String detectorId = createDetector(authorization, "{\"enable\":true}");
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector/" + detectorId + "/admin")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("autoEnable", equalTo(false))
|
||||
.body("memberAccountLimitReached", equalTo(false))
|
||||
.body("autoEnableOrganizationMembers", equalTo("NONE"));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("""
|
||||
{"autoEnableOrganizationMembers":"ALL","features":[
|
||||
{"name":"RUNTIME_MONITORING","autoEnable":"ALL","additionalConfiguration":[
|
||||
{"name":"ECS_FARGATE_AGENT_MANAGEMENT","autoEnable":"ALL"},
|
||||
{"name":"EC2_AGENT_MANAGEMENT","autoEnable":"ALL"},
|
||||
{"name":"EKS_ADDON_MANAGEMENT","autoEnable":"NONE"}
|
||||
]}
|
||||
]}
|
||||
""")
|
||||
.when()
|
||||
.post("/detector/" + detectorId + "/admin")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
Response configuration = given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/detector/" + detectorId + "/admin")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("autoEnable", equalTo(true))
|
||||
.body("autoEnableOrganizationMembers", equalTo("ALL"))
|
||||
.extract().response();
|
||||
assertEquals(
|
||||
List.of("ECS_FARGATE_AGENT_MANAGEMENT", "EC2_AGENT_MANAGEMENT", "EKS_ADDON_MANAGEMENT"),
|
||||
configuration.path("features[0].additionalConfiguration.name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void organizationAdminAccountLifecycle() {
|
||||
String authorization = auth("000000000204", EAST);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"adminAccountId\":\"111111111111\"}")
|
||||
.when()
|
||||
.post("/admin/enable")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/admin")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("adminAccounts[0].adminAccountId", equalTo("111111111111"))
|
||||
.body("adminAccounts[0].adminStatus", equalTo("ENABLED"));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"adminAccountId\":\"111111111111\"}")
|
||||
.when()
|
||||
.post("/admin/disable")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"adminAccountId\":\"111111111111\"}")
|
||||
.when()
|
||||
.post("/admin/disable")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(GuardDutyService.ADMIN_ALREADY_DISABLED_MESSAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectorsAreIsolatedByAccountAndRegion() {
|
||||
String eastAccountA = auth("000000000205", EAST);
|
||||
String eastAccountB = auth("000000000206", EAST);
|
||||
String westAccountA = auth("000000000205", WEST);
|
||||
String detectorId = createDetector(eastAccountA, "{\"enable\":true}");
|
||||
|
||||
given()
|
||||
.header("Authorization", eastAccountB)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE));
|
||||
|
||||
given()
|
||||
.header("Authorization", westAccountA)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE));
|
||||
|
||||
given()
|
||||
.header("Authorization", eastAccountA)
|
||||
.when()
|
||||
.get("/detector/" + detectorId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondCreateInSameAccountAndRegionIsRejected() {
|
||||
String authorization = auth("000000000207", EAST);
|
||||
createDetector(authorization, "{\"enable\":true}");
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"enable\":true}")
|
||||
.when()
|
||||
.post("/detector")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectorTagsAreServedThroughTheSharedTagsRoutes() {
|
||||
String authorization = auth("000000000208", EAST);
|
||||
String detectorId = createDetector(authorization, "{\"enable\":true,\"tags\":{\"env\":\"test\"}}");
|
||||
String arn = "arn:aws:guardduty:" + EAST + ":000000000208:detector/" + detectorId;
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("{\"tags\":{\"team\":\"security\"}}")
|
||||
.when()
|
||||
.post("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.get("/tags/" + arn)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("tags.env", equalTo("test"))
|
||||
.body("tags.team", equalTo("security"));
|
||||
|
||||
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("security"));
|
||||
}
|
||||
|
||||
private static String auth(String accountId, String region) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + accountId + "/20260215/" + region + "/guardduty/aws4_request";
|
||||
}
|
||||
|
||||
private static String createDetector(String authorization, String body) {
|
||||
return given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/detector")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("detectorId", notNullValue())
|
||||
.extract().path("detectorId");
|
||||
}
|
||||
}
|
||||
+373
@@ -0,0 +1,373 @@
|
||||
package io.github.hectorvent.floci.services.guardduty;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.AdminAccount;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.Detector;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.DetectorAdditionalConfiguration;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.DetectorFeature;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.OrganizationAdditionalConfiguration;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.OrganizationConfiguration;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.OrganizationFeature;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class GuardDutyServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final GuardDutyService service =
|
||||
new GuardDutyService(new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
|
||||
@Test
|
||||
void createDetectorAppliesDefaultsAndGeneratesIdentifiers() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
assertEquals(32, detector.getId().length());
|
||||
assertEquals("ENABLED", detector.getStatus());
|
||||
assertEquals("SIX_HOURS", detector.getFindingPublishingFrequency());
|
||||
assertEquals(
|
||||
"arn:aws:iam::" + ACCOUNT
|
||||
+ ":role/aws-service-role/guardduty.amazonaws.com/AWSServiceRoleForAmazonGuardDuty",
|
||||
detector.getServiceRole());
|
||||
assertEquals(detector.getCreatedAt(), detector.getUpdatedAt());
|
||||
assertNull(detector.getFeatures());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDetectorRejectsSecondDetectorInSameRegion() throws Exception {
|
||||
service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}")));
|
||||
|
||||
assertEquals("BadRequestException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(
|
||||
"The request is rejected because a detector already exists for the current account.",
|
||||
error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectorsAreRegionScoped() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
service.createDetector("us-west-2", ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class, () -> service.getDetector("us-west-2", detector.getId()));
|
||||
|
||||
assertEquals("BadRequestException", error.getErrorCode());
|
||||
assertEquals(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE, error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDetectorRejectsMissingDetectorWithProviderMatchedMessage() {
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class, () -> service.getDetector(REGION, "does-not-exist"));
|
||||
|
||||
assertEquals("BadRequestException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE, error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDetectorTogglesStatusAndFrequency() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
service.updateDetector(REGION, detector.getId(), request(
|
||||
"{\"enable\":false,\"findingPublishingFrequency\":\"ONE_HOUR\"}"));
|
||||
|
||||
Detector updated = service.getDetector(REGION, detector.getId());
|
||||
assertEquals("DISABLED", updated.getStatus());
|
||||
assertEquals("ONE_HOUR", updated.getFindingPublishingFrequency());
|
||||
}
|
||||
|
||||
@Test
|
||||
void featureAdditionalConfigurationPreservesSubmittedOrder() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("""
|
||||
{"enable":true,"features":[
|
||||
{"name":"RUNTIME_MONITORING","status":"ENABLED","additionalConfiguration":[
|
||||
{"name":"ECS_FARGATE_AGENT_MANAGEMENT","status":"ENABLED"},
|
||||
{"name":"EC2_AGENT_MANAGEMENT","status":"ENABLED"},
|
||||
{"name":"EKS_ADDON_MANAGEMENT","status":"DISABLED"}
|
||||
]},
|
||||
{"name":"S3_DATA_EVENTS","status":"ENABLED"}
|
||||
]}
|
||||
"""));
|
||||
|
||||
List<DetectorFeature> features = service.getDetector(REGION, detector.getId()).getFeatures();
|
||||
assertEquals(List.of("RUNTIME_MONITORING", "S3_DATA_EVENTS"),
|
||||
features.stream().map(DetectorFeature::getName).toList());
|
||||
assertEquals(
|
||||
List.of("ECS_FARGATE_AGENT_MANAGEMENT", "EC2_AGENT_MANAGEMENT", "EKS_ADDON_MANAGEMENT"),
|
||||
features.get(0).getAdditionalConfiguration().stream()
|
||||
.map(DetectorAdditionalConfiguration::getName)
|
||||
.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDetectorMergesFeaturesByNameAndAppendsNewOnes() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("""
|
||||
{"enable":true,"features":[
|
||||
{"name":"S3_DATA_EVENTS","status":"ENABLED"},
|
||||
{"name":"RDS_LOGIN_EVENTS","status":"ENABLED"}
|
||||
]}
|
||||
"""));
|
||||
|
||||
service.updateDetector(REGION, detector.getId(), request("""
|
||||
{"features":[
|
||||
{"name":"RDS_LOGIN_EVENTS","status":"DISABLED"},
|
||||
{"name":"LAMBDA_NETWORK_LOGS","status":"ENABLED"}
|
||||
]}
|
||||
"""));
|
||||
|
||||
List<DetectorFeature> features = service.getDetector(REGION, detector.getId()).getFeatures();
|
||||
assertEquals(List.of("S3_DATA_EVENTS", "RDS_LOGIN_EVENTS", "LAMBDA_NETWORK_LOGS"),
|
||||
features.stream().map(DetectorFeature::getName).toList());
|
||||
assertEquals("ENABLED", features.get(0).getStatus());
|
||||
assertEquals("DISABLED", features.get(1).getStatus());
|
||||
assertEquals("ENABLED", features.get(2).getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDetectorRejectsUnknownFeatureName() {
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.createDetector(REGION, ACCOUNT, request(
|
||||
"{\"enable\":true,\"features\":[{\"name\":\"NOT_A_FEATURE\",\"status\":\"ENABLED\"}]}")));
|
||||
|
||||
assertEquals("BadRequestException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDetectorRejectsInvalidFrequencyAndMissingEnable() {
|
||||
AwsException frequencyError = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.createDetector(REGION, ACCOUNT, request(
|
||||
"{\"enable\":true,\"findingPublishingFrequency\":\"NEVER\"}")));
|
||||
assertEquals("BadRequestException", frequencyError.getErrorCode());
|
||||
|
||||
AwsException enableError = assertThrows(
|
||||
AwsException.class, () -> service.createDetector(REGION, ACCOUNT, request("{}")));
|
||||
assertEquals("BadRequestException", enableError.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDetectorRemovesItAndRejectsSecondDelete() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
service.deleteDetector(REGION, detector.getId());
|
||||
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class, () -> service.deleteDetector(REGION, detector.getId()));
|
||||
assertEquals(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE, error.getMessage());
|
||||
assertTrue(service.listDetectorIds(REGION, null, null).items().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listDetectorIdsReturnsTheRegionalDetector() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
GuardDutyService.Page<String> page = service.listDetectorIds(REGION, null, null);
|
||||
|
||||
assertEquals(List.of(detector.getId()), page.items());
|
||||
assertNull(page.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeOrganizationConfigurationDefaultsToNone() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
OrganizationConfiguration configuration =
|
||||
service.describeOrganizationConfiguration(REGION, detector.getId());
|
||||
|
||||
assertEquals(false, configuration.getAutoEnable());
|
||||
assertEquals("NONE", configuration.getAutoEnableOrganizationMembers());
|
||||
assertTrue(configuration.getFeatures().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void organizationConfigurationEchoesMembersAndDerivesAutoEnable() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
|
||||
service.updateOrganizationConfiguration(REGION, detector.getId(), request(
|
||||
"{\"autoEnableOrganizationMembers\":\"ALL\"}"));
|
||||
|
||||
OrganizationConfiguration configuration =
|
||||
service.describeOrganizationConfiguration(REGION, detector.getId());
|
||||
assertEquals(true, configuration.getAutoEnable());
|
||||
assertEquals("ALL", configuration.getAutoEnableOrganizationMembers());
|
||||
}
|
||||
|
||||
@Test
|
||||
void organizationFeatureUpdatesMergeWithoutClobberingOtherFeatures() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
service.updateOrganizationConfiguration(REGION, detector.getId(), request(
|
||||
"{\"autoEnableOrganizationMembers\":\"ALL\"}"));
|
||||
|
||||
service.updateOrganizationConfiguration(REGION, detector.getId(), request("""
|
||||
{"features":[
|
||||
{"name":"RUNTIME_MONITORING","autoEnable":"ALL","additionalConfiguration":[
|
||||
{"name":"ECS_FARGATE_AGENT_MANAGEMENT","autoEnable":"ALL"},
|
||||
{"name":"EC2_AGENT_MANAGEMENT","autoEnable":"ALL"},
|
||||
{"name":"EKS_ADDON_MANAGEMENT","autoEnable":"NONE"}
|
||||
]}
|
||||
]}
|
||||
"""));
|
||||
service.updateOrganizationConfiguration(REGION, detector.getId(), request(
|
||||
"{\"features\":[{\"name\":\"S3_DATA_EVENTS\",\"autoEnable\":\"NEW\"}]}"));
|
||||
|
||||
OrganizationConfiguration configuration =
|
||||
service.describeOrganizationConfiguration(REGION, detector.getId());
|
||||
assertEquals("ALL", configuration.getAutoEnableOrganizationMembers());
|
||||
assertEquals(List.of("RUNTIME_MONITORING", "S3_DATA_EVENTS"),
|
||||
configuration.getFeatures().stream().map(OrganizationFeature::getName).toList());
|
||||
assertEquals(
|
||||
List.of("ECS_FARGATE_AGENT_MANAGEMENT", "EC2_AGENT_MANAGEMENT", "EKS_ADDON_MANAGEMENT"),
|
||||
configuration.getFeatures().get(0).getAdditionalConfiguration().stream()
|
||||
.map(OrganizationAdditionalConfiguration::getName)
|
||||
.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateOrganizationConfigurationRejectsMissingDetectorAndBadValues() throws Exception {
|
||||
AwsException notFound = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.updateOrganizationConfiguration(REGION, "missing", request(
|
||||
"{\"autoEnableOrganizationMembers\":\"ALL\"}")));
|
||||
assertEquals(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE, notFound.getMessage());
|
||||
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request("{\"enable\":true}"));
|
||||
AwsException badValue = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.updateOrganizationConfiguration(REGION, detector.getId(), request(
|
||||
"{\"autoEnableOrganizationMembers\":\"SOME\"}")));
|
||||
assertEquals("BadRequestException", badValue.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminAccountLifecycle() throws Exception {
|
||||
service.enableOrganizationAdminAccount(REGION, request("{\"adminAccountId\":\"111111111111\"}"));
|
||||
|
||||
GuardDutyService.Page<AdminAccount> accounts =
|
||||
service.listOrganizationAdminAccounts(REGION, null, null);
|
||||
assertEquals(1, accounts.items().size());
|
||||
assertEquals("111111111111", accounts.items().get(0).getAdminAccountId());
|
||||
assertEquals("ENABLED", accounts.items().get(0).getAdminStatus());
|
||||
|
||||
AwsException conflict = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.enableOrganizationAdminAccount(REGION, request(
|
||||
"{\"adminAccountId\":\"222222222222\"}")));
|
||||
assertEquals("BadRequestException", conflict.getErrorCode());
|
||||
|
||||
service.disableOrganizationAdminAccount(REGION, request("{\"adminAccountId\":\"111111111111\"}"));
|
||||
assertTrue(service.listOrganizationAdminAccounts(REGION, null, null).items().isEmpty());
|
||||
|
||||
AwsException alreadyDisabled = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.disableOrganizationAdminAccount(REGION, request(
|
||||
"{\"adminAccountId\":\"111111111111\"}")));
|
||||
assertEquals(GuardDutyService.ADMIN_ALREADY_DISABLED_MESSAGE, alreadyDisabled.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableOrganizationAdminAccountRejectsMalformedAccountId() {
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.enableOrganizationAdminAccount(REGION, request(
|
||||
"{\"adminAccountId\":\"not-an-account\"}")));
|
||||
|
||||
assertEquals("BadRequestException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagOperationsRoundTripThroughTheDetectorArn() throws Exception {
|
||||
Detector detector = service.createDetector(REGION, ACCOUNT, request(
|
||||
"{\"enable\":true,\"tags\":{\"env\":\"test\"}}"));
|
||||
String arn = "arn:aws:guardduty:" + REGION + ":" + ACCOUNT + ":detector/" + detector.getId();
|
||||
|
||||
service.tagResource(arn, Map.of("team", "security"));
|
||||
assertEquals(Map.of("env", "test", "team", "security"), service.listTags(arn));
|
||||
|
||||
service.untagResource(arn, List.of("env"));
|
||||
assertEquals(Map.of("team", "security"), service.listTags(arn));
|
||||
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.listTags("arn:aws:guardduty:" + REGION + ":" + ACCOUNT + ":detector/missing"));
|
||||
assertEquals(GuardDutyService.DETECTOR_NOT_FOUND_MESSAGE, error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectorSurvivesPersistentStorageReloadWithOrderIntact(@TempDir Path tempDir) throws Exception {
|
||||
Path detectorFile = tempDir.resolve("detectors.json");
|
||||
Path adminFile = tempDir.resolve("admins.json");
|
||||
GuardDutyService firstService = new GuardDutyService(
|
||||
loadedStore(detectorFile, new TypeReference<Map<String, Detector>>() {
|
||||
}),
|
||||
loadedStore(adminFile, new TypeReference<Map<String, AdminAccount>>() {
|
||||
}));
|
||||
Detector created = firstService.createDetector(REGION, ACCOUNT, request("""
|
||||
{"enable":true,"tags":{"env":"test"},"features":[
|
||||
{"name":"RUNTIME_MONITORING","status":"ENABLED","additionalConfiguration":[
|
||||
{"name":"ECS_FARGATE_AGENT_MANAGEMENT","status":"ENABLED"},
|
||||
{"name":"EC2_AGENT_MANAGEMENT","status":"ENABLED"},
|
||||
{"name":"EKS_ADDON_MANAGEMENT","status":"DISABLED"}
|
||||
]}
|
||||
]}
|
||||
"""));
|
||||
firstService.updateOrganizationConfiguration(REGION, created.getId(), request(
|
||||
"{\"autoEnableOrganizationMembers\":\"ALL\"}"));
|
||||
|
||||
GuardDutyService reloadedService = new GuardDutyService(
|
||||
loadedStore(detectorFile, new TypeReference<Map<String, Detector>>() {
|
||||
}),
|
||||
loadedStore(adminFile, new TypeReference<Map<String, AdminAccount>>() {
|
||||
}));
|
||||
Detector reloaded = reloadedService.getDetector(REGION, created.getId());
|
||||
|
||||
assertEquals(created.getId(), reloaded.getId());
|
||||
assertEquals(created.getCreatedAt(), reloaded.getCreatedAt());
|
||||
assertEquals("test", reloaded.getTags().get("env"));
|
||||
assertEquals(
|
||||
List.of("ECS_FARGATE_AGENT_MANAGEMENT", "EC2_AGENT_MANAGEMENT", "EKS_ADDON_MANAGEMENT"),
|
||||
reloaded.getFeatures().get(0).getAdditionalConfiguration().stream()
|
||||
.map(DetectorAdditionalConfiguration::getName)
|
||||
.toList());
|
||||
assertEquals("ALL",
|
||||
reloadedService.describeOrganizationConfiguration(REGION, created.getId())
|
||||
.getAutoEnableOrganizationMembers());
|
||||
}
|
||||
|
||||
private static <V> PersistentStorage<String, V> loadedStore(
|
||||
Path file, TypeReference<Map<String, V>> type) {
|
||||
PersistentStorage<String, V> store = new PersistentStorage<>(file, type);
|
||||
store.load();
|
||||
return store;
|
||||
}
|
||||
|
||||
private JsonNode request(String json) {
|
||||
try {
|
||||
return objectMapper.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user