mode();
@@ -610,6 +618,7 @@ public interface EmulatorConfig {
IotDataServiceConfig iotdata();
CloudHsmV2ServiceConfig cloudhsmv2();
RumServiceConfig rum();
+ GuardDutyServiceConfig guardduty();
}
interface IotServiceConfig {
@@ -643,6 +652,11 @@ public interface EmulatorConfig {
boolean enabled();
}
+ interface GuardDutyServiceConfig {
+ @WithDefault("true")
+ boolean enabled();
+ }
+
interface LightsailServiceConfig {
@WithDefault("true")
boolean enabled();
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
index 9025f34749..56f788177d 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
@@ -28,6 +28,7 @@ import io.github.hectorvent.floci.services.route53.Route53Controller;
import io.github.hectorvent.floci.services.ses.SesController;
import io.github.hectorvent.floci.services.appsync.AppSyncController;
import io.github.hectorvent.floci.services.rdsdata.RdsDataController;
+import io.github.hectorvent.floci.services.guardduty.GuardDutyController;
import io.github.hectorvent.floci.services.rum.RumController;
import io.github.hectorvent.floci.services.s3vectors.S3VectorsController;
import io.github.hectorvent.floci.services.s3tables.S3TablesController;
@@ -461,7 +462,13 @@ public class ResolvedServiceCatalog {
"rum", storageMode(config.storage().services().rum().mode(), config.storage().mode()),
config.storage().services().rum().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON),
- Set.of(), Set.of("rum"), Set.of(), Set.of(RumController.class))
+ Set.of(), Set.of("rum"), Set.of(), Set.of(RumController.class)),
+ descriptor("guardduty", "guardduty", config.services().guardduty().enabled(), true,
+ "guardduty",
+ storageMode(config.storage().services().guardduty().mode(), config.storage().mode()),
+ config.storage().services().guardduty().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
+ protocols(ServiceProtocol.REST_JSON),
+ Set.of(), Set.of("guardduty"), Set.of(), Set.of(GuardDutyController.class))
));
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java b/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java
new file mode 100644
index 0000000000..d02d118c72
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyController.java
@@ -0,0 +1,202 @@
+package io.github.hectorvent.floci.services.guardduty;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+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.DetectorFeature;
+import io.github.hectorvent.floci.services.guardduty.model.OrganizationConfiguration;
+import io.github.hectorvent.floci.services.guardduty.model.OrganizationFeature;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.Context;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+
+/**
+ * GuardDuty (Smithy restJson1) — detector lifecycle and organization configuration.
+ *
+ * The literal {@code /detector} and {@code /admin} paths take JAX-RS precedence over S3's
+ * {@code /{bucket}} and {@code /{bucket}/{key}} template routes, so these routes win with no
+ * extra routing wiring. Tag operations ({@code /tags/{arn}}) are served by
+ * {@code SharedTagsController} via {@link GuardDutyTagHandler}.
+ *
+ *
GuardDuty reports every client error as {@code BadRequestException} (HTTP 400); the
+ * Terraform AWS provider matches specific message texts to detect missing resources, so those
+ * messages must not change.
+ */
+@Path("/")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+public class GuardDutyController {
+
+ private final GuardDutyService service;
+ private final ObjectMapper objectMapper;
+ private final RegionResolver regionResolver;
+
+ @Inject
+ public GuardDutyController(
+ GuardDutyService service, ObjectMapper objectMapper, RegionResolver regionResolver) {
+ this.service = service;
+ this.objectMapper = objectMapper;
+ this.regionResolver = regionResolver;
+ }
+
+ private JsonNode parse(String body) {
+ if (body == null || body.isBlank()) {
+ return objectMapper.createObjectNode();
+ }
+ try {
+ JsonNode request = objectMapper.readTree(body);
+ if (request == null || !request.isObject()) {
+ throw new AwsException("BadRequestException", "Request body must be a JSON object.", 400);
+ }
+ return request;
+ } catch (AwsException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new AwsException("BadRequestException", "Request body is not valid JSON.", 400);
+ }
+ }
+
+ @POST
+ @Path("/detector")
+ public Response createDetector(@Context HttpHeaders headers, String body) {
+ Detector detector = service.createDetector(
+ regionResolver.resolveRegion(headers), regionResolver.getAccountId(), parse(body));
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("detectorId", detector.getId());
+ return Response.ok(response).build();
+ }
+
+ @GET
+ @Path("/detector")
+ public Response listDetectors(
+ @Context HttpHeaders headers,
+ @QueryParam("maxResults") String maxResults,
+ @QueryParam("nextToken") String nextToken) {
+ GuardDutyService.Page page = service.listDetectorIds(
+ regionResolver.resolveRegion(headers), maxResults, nextToken);
+ ObjectNode response = objectMapper.createObjectNode();
+ ArrayNode ids = response.putArray("detectorIds");
+ page.items().forEach(ids::add);
+ if (page.nextToken() != null) {
+ response.put("nextToken", page.nextToken());
+ }
+ return Response.ok(response).build();
+ }
+
+ @GET
+ @Path("/detector/{detectorId}")
+ public Response getDetector(@Context HttpHeaders headers, @PathParam("detectorId") String detectorId) {
+ Detector detector = service.getDetector(regionResolver.resolveRegion(headers), detectorId);
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("createdAt", detector.getCreatedAt());
+ if (detector.getFindingPublishingFrequency() != null) {
+ response.put("findingPublishingFrequency", detector.getFindingPublishingFrequency());
+ }
+ response.put("serviceRole", detector.getServiceRole());
+ response.put("status", detector.getStatus());
+ response.put("updatedAt", detector.getUpdatedAt());
+ if (detector.getTags() != null && !detector.getTags().isEmpty()) {
+ ObjectNode tags = response.putObject("tags");
+ detector.getTags().forEach(tags::put);
+ }
+ if (detector.getFeatures() != null) {
+ ArrayNode features = response.putArray("features");
+ for (DetectorFeature feature : detector.getFeatures()) {
+ features.add(objectMapper.valueToTree(feature));
+ }
+ }
+ return Response.ok(response).build();
+ }
+
+ @POST
+ @Path("/detector/{detectorId}")
+ public Response updateDetector(
+ @Context HttpHeaders headers, @PathParam("detectorId") String detectorId, String body) {
+ service.updateDetector(regionResolver.resolveRegion(headers), detectorId, parse(body));
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
+ @DELETE
+ @Path("/detector/{detectorId}")
+ public Response deleteDetector(
+ @Context HttpHeaders headers, @PathParam("detectorId") String detectorId) {
+ service.deleteDetector(regionResolver.resolveRegion(headers), detectorId);
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
+ @GET
+ @Path("/detector/{detectorId}/admin")
+ public Response describeOrganizationConfiguration(
+ @Context HttpHeaders headers, @PathParam("detectorId") String detectorId) {
+ OrganizationConfiguration configuration = service.describeOrganizationConfiguration(
+ regionResolver.resolveRegion(headers), detectorId);
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("autoEnable", Boolean.TRUE.equals(configuration.getAutoEnable()));
+ response.put("memberAccountLimitReached", false);
+ response.put("autoEnableOrganizationMembers", configuration.getAutoEnableOrganizationMembers());
+ ArrayNode features = response.putArray("features");
+ if (configuration.getFeatures() != null) {
+ for (OrganizationFeature feature : configuration.getFeatures()) {
+ features.add(objectMapper.valueToTree(feature));
+ }
+ }
+ return Response.ok(response).build();
+ }
+
+ @POST
+ @Path("/detector/{detectorId}/admin")
+ public Response updateOrganizationConfiguration(
+ @Context HttpHeaders headers, @PathParam("detectorId") String detectorId, String body) {
+ service.updateOrganizationConfiguration(
+ regionResolver.resolveRegion(headers), detectorId, parse(body));
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
+ @POST
+ @Path("/admin/enable")
+ public Response enableOrganizationAdminAccount(@Context HttpHeaders headers, String body) {
+ service.enableOrganizationAdminAccount(regionResolver.resolveRegion(headers), parse(body));
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
+ @POST
+ @Path("/admin/disable")
+ public Response disableOrganizationAdminAccount(@Context HttpHeaders headers, String body) {
+ service.disableOrganizationAdminAccount(regionResolver.resolveRegion(headers), parse(body));
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
+ @GET
+ @Path("/admin")
+ public Response listOrganizationAdminAccounts(
+ @Context HttpHeaders headers,
+ @QueryParam("maxResults") String maxResults,
+ @QueryParam("nextToken") String nextToken) {
+ GuardDutyService.Page page = service.listOrganizationAdminAccounts(
+ regionResolver.resolveRegion(headers), maxResults, nextToken);
+ ObjectNode response = objectMapper.createObjectNode();
+ ArrayNode accounts = response.putArray("adminAccounts");
+ for (AdminAccount account : page.items()) {
+ accounts.add(objectMapper.valueToTree(account));
+ }
+ if (page.nextToken() != null) {
+ response.put("nextToken", page.nextToken());
+ }
+ return Response.ok(response).build();
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyService.java b/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyService.java
new file mode 100644
index 0000000000..784f3b553e
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/guardduty/GuardDutyService.java
@@ -0,0 +1,549 @@
+package io.github.hectorvent.floci.services.guardduty;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.storage.StorageBackend;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
+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 jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.regex.Pattern;
+
+/**
+ * GuardDuty detector lifecycle and organization configuration backed by the configured
+ * Floci storage mode.
+ *
+ * Organization semantics are readback-only: Floci has no Organizations service, so
+ * {@code adminAccountId} membership is not validated, delegated-administrator permissions are
+ * not enforced, and member accounts are not fanned out. Organization configuration is stored
+ * per calling account and echoed back as submitted.
+ */
+@ApplicationScoped
+public class GuardDutyService {
+
+ /**
+ * The Terraform AWS provider matches this exact message to translate a
+ * {@code BadRequestException} into resource removal from state.
+ */
+ static final String DETECTOR_NOT_FOUND_MESSAGE =
+ "The request is rejected because the input detectorId is not owned by the current account.";
+
+ /** Also matched verbatim by the Terraform AWS provider on delegated-admin delete. */
+ static final String ADMIN_ALREADY_DISABLED_MESSAGE =
+ "The request failed because the delegated administrator account has already been disabled "
+ + "and/or GuardDuty protection has been disabled.";
+
+ private static final int DEFAULT_MAX_RESULTS = 50;
+ private static final int MAX_RESULTS = 50;
+ private static final String TOKEN_PREFIX = "guardduty:v1:";
+ private static final Pattern ACCOUNT_ID_PATTERN = Pattern.compile("[0-9]{12}");
+ private static final Set FINDING_PUBLISHING_FREQUENCIES =
+ Set.of("FIFTEEN_MINUTES", "ONE_HOUR", "SIX_HOURS");
+ private static final Set FEATURE_STATUSES = Set.of("ENABLED", "DISABLED");
+ private static final Set ORG_AUTO_ENABLE_VALUES = Set.of("NEW", "NONE", "ALL");
+ private static final Set DETECTOR_FEATURE_NAMES = Set.of(
+ "S3_DATA_EVENTS", "EKS_AUDIT_LOGS", "EBS_MALWARE_PROTECTION", "RDS_LOGIN_EVENTS",
+ "LAMBDA_NETWORK_LOGS", "EKS_RUNTIME_MONITORING", "RUNTIME_MONITORING",
+ "AI_PROTECTION", "AI_ANALYST");
+ private static final Set ORG_FEATURE_NAMES = Set.of(
+ "S3_DATA_EVENTS", "EKS_AUDIT_LOGS", "EBS_MALWARE_PROTECTION", "RDS_LOGIN_EVENTS",
+ "LAMBDA_NETWORK_LOGS", "EKS_RUNTIME_MONITORING", "RUNTIME_MONITORING", "AI_PROTECTION");
+ private static final Set ADDITIONAL_CONFIGURATION_NAMES =
+ Set.of("EKS_ADDON_MANAGEMENT", "ECS_FARGATE_AGENT_MANAGEMENT", "EC2_AGENT_MANAGEMENT");
+
+ private final StorageBackend detectorStore;
+ private final StorageBackend adminAccountStore;
+
+ @Inject
+ public GuardDutyService(StorageFactory storageFactory) {
+ this(storageFactory.create(
+ "guardduty",
+ "guardduty-detectors.json",
+ new TypeReference