mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(guardduty): support publishing destinations (#6016)
* feat(guardduty): support publishing destinations
Add CreatePublishingDestination, DescribePublishingDestination,
UpdatePublishingDestination, DeletePublishingDestination and
ListPublishingDestinations under /detector/{detectorId}/publishingDestination.
Previously these returned UnknownOperationException, which broke
Terraform's aws_guardduty_publishing_destination.
Destinations are stored per detector through StorageFactory, so they follow
the detector's account and region scoping, survive a persistent-storage
reload, and report PUBLISHING on create (the state Terraform's create waiter
targets). A missing destination returns BadRequestException with the message
the Terraform provider matches for not-found. Deleting a detector removes its
destinations.
clientToken makes create idempotent and is capped at the model's 64
characters, measured in code points as the model's length constraint is.
destinationArn stays optional on create: the GuardDuty model marks
DestinationProperties.DestinationArn and KmsKeyArn as not required
(botocore guardduty/2017-11-28 service-2.json).
The service gains a five-store constructor; the existing three- and
four-store (malware protection plan) constructors delegate to it.
Closes #4983
* docs(guardduty): update the service matrix operation count
* test(guardduty): cover the publishing destination lifecycle through the SDK
Create, describe, update, list and delete an S3 publishing destination
with the AWS SDK v2 GuardDuty client, asserting the shapes as the SDK
reads them, including the required publishingFailureStartTimestamp.
This commit is contained in:
1 parent
28799ed352
commit
fb390968d5
8 files changed
+654
-1
No files matched your search
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.guardduty.model.MalwareProtectionPlan
|
||||
import io.github.hectorvent.floci.services.guardduty.model.MemberAccount;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.OrganizationConfiguration;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.OrganizationFeature;
|
||||
import io.github.hectorvent.floci.services.guardduty.model.PublishingDestination;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
@@ -181,6 +182,80 @@ public class GuardDutyController {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/detector/{detectorId}/publishingDestination")
|
||||
public Response createPublishingDestination(
|
||||
@Context HttpHeaders headers, @PathParam("detectorId") String detectorId, String body) {
|
||||
PublishingDestination destination = service.createPublishingDestination(
|
||||
regionResolver.resolveRegion(headers), detectorId, parse(body));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("destinationId", destination.destinationId());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/detector/{detectorId}/publishingDestination")
|
||||
public Response listPublishingDestinations(
|
||||
@Context HttpHeaders headers, @PathParam("detectorId") String detectorId,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
GuardDutyService.Page<PublishingDestination> page = service.listPublishingDestinations(
|
||||
regionResolver.resolveRegion(headers), detectorId, maxResults, nextToken);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode destinations = response.putArray("destinations");
|
||||
for (PublishingDestination destination : page.items()) {
|
||||
ObjectNode node = destinations.addObject();
|
||||
node.put("destinationId", destination.destinationId());
|
||||
node.put("destinationType", destination.destinationType());
|
||||
node.put("status", destination.status());
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/detector/{detectorId}/publishingDestination/{destinationId}")
|
||||
public Response describePublishingDestination(
|
||||
@Context HttpHeaders headers, @PathParam("detectorId") String detectorId,
|
||||
@PathParam("destinationId") String destinationId) {
|
||||
PublishingDestination destination = service.describePublishingDestination(
|
||||
regionResolver.resolveRegion(headers), detectorId, destinationId);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("destinationId", destination.destinationId());
|
||||
response.put("destinationType", destination.destinationType());
|
||||
response.put("status", destination.status());
|
||||
response.put("publishingFailureStartTimestamp", 0L);
|
||||
ObjectNode properties = response.putObject("destinationProperties");
|
||||
if (destination.destinationArn() != null) {
|
||||
properties.put("destinationArn", destination.destinationArn());
|
||||
}
|
||||
if (destination.kmsKeyArn() != null) {
|
||||
properties.put("kmsKeyArn", destination.kmsKeyArn());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/detector/{detectorId}/publishingDestination/{destinationId}")
|
||||
public Response updatePublishingDestination(
|
||||
@Context HttpHeaders headers, @PathParam("detectorId") String detectorId,
|
||||
@PathParam("destinationId") String destinationId, String body) {
|
||||
service.updatePublishingDestination(
|
||||
regionResolver.resolveRegion(headers), detectorId, destinationId, parse(body));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/detector/{detectorId}/publishingDestination/{destinationId}")
|
||||
public Response deletePublishingDestination(
|
||||
@Context HttpHeaders headers, @PathParam("detectorId") String detectorId,
|
||||
@PathParam("destinationId") String destinationId) {
|
||||
service.deletePublishingDestination(regionResolver.resolveRegion(headers), detectorId, destinationId);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/detector/{detectorId}/admin")
|
||||
public Response describeOrganizationConfiguration(
|
||||
|
||||
@@ -20,6 +20,7 @@ import io.github.hectorvent.floci.services.guardduty.model.MemberAccount;
|
||||
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 io.github.hectorvent.floci.services.guardduty.model.PublishingDestination;
|
||||
import io.github.hectorvent.floci.services.iam.ServiceLinkedRoles;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -96,6 +97,7 @@ public class GuardDutyService {
|
||||
private final StorageBackend<String, AdminAccount> adminAccountStore;
|
||||
private final StorageBackend<String, MemberAccount> memberStore;
|
||||
private final StorageBackend<String, MalwareProtectionPlan> malwareProtectionPlanStore;
|
||||
private final StorageBackend<String, PublishingDestination> destinationStore;
|
||||
|
||||
@Inject
|
||||
public GuardDutyService(StorageFactory storageFactory) {
|
||||
@@ -118,6 +120,11 @@ public class GuardDutyService {
|
||||
"guardduty",
|
||||
"guardduty-malware-protection-plans.json",
|
||||
new TypeReference<Map<String, MalwareProtectionPlan>>() {
|
||||
}),
|
||||
storageFactory.create(
|
||||
"guardduty",
|
||||
"guardduty-publishing-destinations.json",
|
||||
new TypeReference<Map<String, PublishingDestination>>() {
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -134,10 +141,20 @@ public class GuardDutyService {
|
||||
StorageBackend<String, AdminAccount> adminAccountStore,
|
||||
StorageBackend<String, MemberAccount> memberStore,
|
||||
StorageBackend<String, MalwareProtectionPlan> malwareProtectionPlanStore) {
|
||||
this(detectorStore, adminAccountStore, memberStore, malwareProtectionPlanStore, new InMemoryStorage<>());
|
||||
}
|
||||
|
||||
GuardDutyService(
|
||||
StorageBackend<String, Detector> detectorStore,
|
||||
StorageBackend<String, AdminAccount> adminAccountStore,
|
||||
StorageBackend<String, MemberAccount> memberStore,
|
||||
StorageBackend<String, MalwareProtectionPlan> malwareProtectionPlanStore,
|
||||
StorageBackend<String, PublishingDestination> destinationStore) {
|
||||
this.detectorStore = detectorStore;
|
||||
this.adminAccountStore = adminAccountStore;
|
||||
this.memberStore = memberStore;
|
||||
this.malwareProtectionPlanStore = malwareProtectionPlanStore;
|
||||
this.destinationStore = destinationStore;
|
||||
}
|
||||
|
||||
public synchronized Detector createDetector(String region, String accountId, JsonNode request) {
|
||||
@@ -194,6 +211,9 @@ public class GuardDutyService {
|
||||
throw detectorNotFound();
|
||||
}
|
||||
detectorStore.delete(key);
|
||||
String prefix = destinationKey(region, detectorId, "");
|
||||
destinationStore.scan(k -> k.startsWith(prefix)).forEach(d ->
|
||||
destinationStore.delete(destinationKey(region, detectorId, d.destinationId())));
|
||||
}
|
||||
|
||||
public Page<String> listDetectorIds(String region, String accountId, String maxResultsValue, String nextToken) {
|
||||
@@ -337,6 +357,100 @@ public class GuardDutyService {
|
||||
return listMembers(region, detectorId, null, null, null).items();
|
||||
}
|
||||
|
||||
public synchronized PublishingDestination createPublishingDestination(
|
||||
String region, String detectorId, JsonNode request) {
|
||||
getDetector(region, detectorId);
|
||||
if (!"S3".equals(requireText(request, "destinationType"))) {
|
||||
throw badRequest("destinationType must be S3.");
|
||||
}
|
||||
JsonNode properties = request.get("destinationProperties");
|
||||
requireObject(properties, "destinationProperties");
|
||||
String clientToken = optionalText(request, "clientToken");
|
||||
if (clientToken != null && clientToken.codePointCount(0, clientToken.length()) > 64) {
|
||||
// botocore's ClientToken shape: string, min 0, max 64.
|
||||
throw badRequest("clientToken must be at most 64 characters.");
|
||||
}
|
||||
if (clientToken != null) {
|
||||
// Idempotent replay: a retried create returns the destination the token already made.
|
||||
String prefix = destinationKey(region, detectorId, "");
|
||||
for (PublishingDestination existing : destinationStore.scan(k -> k.startsWith(prefix))) {
|
||||
if (clientToken.equals(existing.clientToken())) {
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
}
|
||||
PublishingDestination destination = new PublishingDestination(
|
||||
UUID.randomUUID().toString().replace("-", ""),
|
||||
"S3",
|
||||
optionalText(properties, "destinationArn"),
|
||||
optionalText(properties, "kmsKeyArn"),
|
||||
"PUBLISHING",
|
||||
clientToken);
|
||||
destinationStore.put(destinationKey(region, detectorId, destination.destinationId()), destination);
|
||||
return destination;
|
||||
}
|
||||
|
||||
public PublishingDestination describePublishingDestination(
|
||||
String region, String detectorId, String destinationId) {
|
||||
getDetector(region, detectorId);
|
||||
return destinationStore.get(destinationKey(region, detectorId, destinationId))
|
||||
.orElseThrow(GuardDutyService::destinationNotFound);
|
||||
}
|
||||
|
||||
public synchronized void updatePublishingDestination(
|
||||
String region, String detectorId, String destinationId, JsonNode request) {
|
||||
PublishingDestination current = describePublishingDestination(region, detectorId, destinationId);
|
||||
JsonNode properties = request.get("destinationProperties");
|
||||
if (properties == null) {
|
||||
return;
|
||||
}
|
||||
requireObject(properties, "destinationProperties");
|
||||
destinationStore.put(destinationKey(region, detectorId, destinationId), new PublishingDestination(
|
||||
destinationId,
|
||||
current.destinationType(),
|
||||
properties.has("destinationArn") ? optionalText(properties, "destinationArn") : current.destinationArn(),
|
||||
properties.has("kmsKeyArn") ? optionalText(properties, "kmsKeyArn") : current.kmsKeyArn(),
|
||||
current.status(),
|
||||
current.clientToken()));
|
||||
}
|
||||
|
||||
public synchronized void deletePublishingDestination(String region, String detectorId, String destinationId) {
|
||||
describePublishingDestination(region, detectorId, destinationId);
|
||||
destinationStore.delete(destinationKey(region, detectorId, destinationId));
|
||||
}
|
||||
|
||||
public Page<PublishingDestination> listPublishingDestinations(
|
||||
String region, String detectorId, String maxResults, String nextToken) {
|
||||
getDetector(region, detectorId);
|
||||
int limit = parseMaxResults(maxResults);
|
||||
String prefix = destinationKey(region, detectorId, "");
|
||||
List<PublishingDestination> destinations = destinationStore.scan(key -> key.startsWith(prefix)).stream()
|
||||
.sorted(Comparator.comparing(PublishingDestination::destinationId)).toList();
|
||||
int offset = decodeOffset(nextToken, destinations.size());
|
||||
int end = Math.min(destinations.size(), offset + limit);
|
||||
return new Page<>(destinations.subList(offset, end), end < destinations.size() ? encodeOffset(end) : null);
|
||||
}
|
||||
|
||||
private static String destinationKey(String region, String detectorId, String destinationId) {
|
||||
return region + "::" + detectorId + "::" + destinationId;
|
||||
}
|
||||
|
||||
private static String optionalText(JsonNode parent, String field) {
|
||||
JsonNode value = parent.get(field);
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isTextual()) {
|
||||
throw badRequest(field + " must be a string.");
|
||||
}
|
||||
return value.textValue();
|
||||
}
|
||||
|
||||
/** Matched verbatim by the Terraform AWS provider to detect a deleted publishing destination. */
|
||||
private static AwsException destinationNotFound() {
|
||||
return badRequest("The request is rejected because the one or more input parameters have invalid values.");
|
||||
}
|
||||
|
||||
private static boolean isAssociatedRelationship(String status) {
|
||||
return "Enabled".equals(status) || "Invited".equals(status) || "EmailVerificationInProgress".equals(status);
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.guardduty.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public record PublishingDestination(String destinationId, String destinationType, String destinationArn,
|
||||
String kmsKeyArn, String status, String clientToken) {}
|
||||
Reference in new issue
Block a user