mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
Add CloudTrail trail lifecycle support (#1256)
* feat(cloudtrail): add trail lifecycle operations * test(cloudtrail): cover provisioning integration * test(cloudtrail): add SDK compatibility coverage * fix(cloudtrail): harden trail lifecycle state
This commit is contained in:
1 parent
8c2d18b1d0
commit
9a244a6964
13 files changed
+998
-1
No files matched your search
@@ -364,10 +364,16 @@ public interface EmulatorConfig {
|
||||
CurServiceConfig cur();
|
||||
BcmDataExportsServiceConfig bcmDataExports();
|
||||
ConfigServiceConfig configservice();
|
||||
CloudTrailServiceConfig cloudtrail();
|
||||
CloudFrontServiceConfig cloudfront();
|
||||
AppSyncServiceConfig appsync();
|
||||
}
|
||||
|
||||
interface CloudTrailServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface TransferServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.glue.GlueJsonHandler;
|
||||
import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingJsonHandler;
|
||||
import io.github.hectorvent.floci.services.bcmdataexports.BcmDataExportsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.ce.CostExplorerJsonHandler;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailJsonHandler;
|
||||
import io.github.hectorvent.floci.services.configservice.ConfigServiceJsonHandler;
|
||||
import io.github.hectorvent.floci.services.cur.CurJsonHandler;
|
||||
import io.github.hectorvent.floci.services.pricing.PricingJsonHandler;
|
||||
@@ -80,6 +81,7 @@ public class AwsJson11Controller {
|
||||
private final CurJsonHandler curJsonHandler;
|
||||
private final BcmDataExportsJsonHandler bcmDataExportsJsonHandler;
|
||||
private final ConfigServiceJsonHandler configServiceJsonHandler;
|
||||
private final CloudTrailJsonHandler cloudTrailJsonHandler;
|
||||
|
||||
@Inject
|
||||
public AwsJson11Controller(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
|
||||
@@ -106,7 +108,8 @@ public class AwsJson11Controller {
|
||||
CostExplorerJsonHandler costExplorerJsonHandler,
|
||||
CurJsonHandler curJsonHandler,
|
||||
BcmDataExportsJsonHandler bcmDataExportsJsonHandler,
|
||||
ConfigServiceJsonHandler configServiceJsonHandler) {
|
||||
ConfigServiceJsonHandler configServiceJsonHandler,
|
||||
CloudTrailJsonHandler cloudTrailJsonHandler) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.catalog = catalog;
|
||||
this.regionResolver = regionResolver;
|
||||
@@ -137,6 +140,7 @@ public class AwsJson11Controller {
|
||||
this.curJsonHandler = curJsonHandler;
|
||||
this.bcmDataExportsJsonHandler = bcmDataExportsJsonHandler;
|
||||
this.configServiceJsonHandler = configServiceJsonHandler;
|
||||
this.cloudTrailJsonHandler = cloudTrailJsonHandler;
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -192,6 +196,7 @@ public class AwsJson11Controller {
|
||||
case "cur" -> curJsonHandler.handle(action, request, region);
|
||||
case "bcm-data-exports" -> bcmDataExportsJsonHandler.handle(action, request, region);
|
||||
case "config" -> configServiceJsonHandler.handle(action, request, region);
|
||||
case "cloudtrail" -> cloudTrailJsonHandler.handle(action, request, region);
|
||||
default -> null;
|
||||
};
|
||||
// catalog.matchTarget is protocol-agnostic: a JSON 1.0 target
|
||||
|
||||
@@ -234,6 +234,11 @@ public class ResolvedServiceCatalog {
|
||||
null, null, 5000L, null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("StarlingDoveService."), Set.of("config"), Set.of(), Set.of()),
|
||||
descriptor("cloudtrail", "cloudtrail", config.services().cloudtrail().enabled(), true,
|
||||
"cloudtrail", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101."),
|
||||
Set.of("cloudtrail"), Set.of(), Set.of()),
|
||||
descriptor("autoscaling", "autoscaling", config.services().autoscaling().enabled(), true,
|
||||
"autoscaling", config.storage().mode(), 5000L, AwsNamespaces.AUTOSCALING, ServiceProtocol.QUERY,
|
||||
protocols(ServiceProtocol.QUERY),
|
||||
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
package io.github.hectorvent.floci.services.cloudtrail;
|
||||
|
||||
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.services.cloudtrail.model.CloudTrailTrail;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CloudTrailJsonHandler {
|
||||
private final CloudTrailService service;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@Inject
|
||||
public CloudTrailJsonHandler(CloudTrailService service, ObjectMapper mapper) {
|
||||
this.service = service;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
return switch (action) {
|
||||
case "CreateTrail" -> createTrail(request, region);
|
||||
case "UpdateTrail" -> updateTrail(request, region);
|
||||
case "DescribeTrails" -> describeTrails(request, region);
|
||||
case "StartLogging" -> startLogging(request, region);
|
||||
case "StopLogging" -> stopLogging(request, region);
|
||||
case "DeleteTrail" -> deleteTrail(request, region);
|
||||
case "GetTrailStatus" -> getTrailStatus(request, region);
|
||||
case "PutEventSelectors" -> putEventSelectors(request, region);
|
||||
default -> throw new AwsException("UnsupportedOperation",
|
||||
"Operation " + action + " is not supported.", 400);
|
||||
};
|
||||
}
|
||||
|
||||
private Response createTrail(JsonNode request, String region) {
|
||||
CloudTrailTrail trail = service.createTrail(
|
||||
region,
|
||||
request.path("Name").asText(null),
|
||||
request.path("S3BucketName").asText(null),
|
||||
request.path("IncludeGlobalServiceEvents").asBoolean(false),
|
||||
request.path("IsMultiRegionTrail").asBoolean(false),
|
||||
request.path("IsOrganizationTrail").asBoolean(false),
|
||||
parseTags(request.path("TagsList")));
|
||||
return Response.ok(trailNode(trail)).build();
|
||||
}
|
||||
|
||||
private Response updateTrail(JsonNode request, String region) {
|
||||
CloudTrailTrail trail = service.updateTrail(
|
||||
region,
|
||||
request.path("Name").asText(null),
|
||||
request.path("S3BucketName").asText(null),
|
||||
optionalBoolean(request, "IncludeGlobalServiceEvents"),
|
||||
optionalBoolean(request, "IsMultiRegionTrail"));
|
||||
return Response.ok(trailNode(trail)).build();
|
||||
}
|
||||
|
||||
private Response describeTrails(JsonNode request, String region) {
|
||||
List<String> names = stringList(request.path("trailNameList"));
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
ArrayNode trails = response.putArray("trailList");
|
||||
for (CloudTrailTrail trail : service.describeTrails(region, names)) {
|
||||
trails.add(trailNode(trail));
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response startLogging(JsonNode request, String region) {
|
||||
service.startLogging(region, request.path("Name").asText(null));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response stopLogging(JsonNode request, String region) {
|
||||
service.stopLogging(region, request.path("Name").asText(null));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response deleteTrail(JsonNode request, String region) {
|
||||
service.deleteTrail(region, request.path("Name").asText(null));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response getTrailStatus(JsonNode request, String region) {
|
||||
CloudTrailTrail trail = service.requireTrail(region, request.path("Name").asText(null));
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
response.put("IsLogging", trail.isLogging());
|
||||
if (trail.getUpdated() != null) {
|
||||
response.put("LatestDeliveryTime", trail.getUpdated().toEpochMilli() / 1000.0);
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response putEventSelectors(JsonNode request, String region) {
|
||||
service.putEventSelectors(region, request.path("TrailName").asText(null));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private ObjectNode trailNode(CloudTrailTrail trail) {
|
||||
ObjectNode node = mapper.createObjectNode();
|
||||
node.put("Name", trail.getName());
|
||||
node.put("TrailARN", trail.getTrailArn());
|
||||
node.put("S3BucketName", trail.getS3BucketName());
|
||||
node.put("IncludeGlobalServiceEvents", trail.isIncludeGlobalServiceEvents());
|
||||
node.put("IsMultiRegionTrail", trail.isMultiRegionTrail());
|
||||
node.put("IsOrganizationTrail", trail.isOrganizationTrail());
|
||||
node.put("HomeRegion", trail.getHomeRegion());
|
||||
if (trail.getCreated() != null) {
|
||||
node.put("CreationDate", trail.getCreated().toEpochMilli() / 1000.0);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static Map<String, String> parseTags(JsonNode tagsNode) {
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
if (tagsNode != null && tagsNode.isArray()) {
|
||||
for (JsonNode tag : tagsNode) {
|
||||
String key = tag.path("Key").asText(null);
|
||||
if (key != null) {
|
||||
tags.put(key, tag.path("Value").asText(""));
|
||||
}
|
||||
}
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private static List<String> stringList(JsonNode node) {
|
||||
if (node == null || !node.isArray()) {
|
||||
return List.of();
|
||||
}
|
||||
java.util.ArrayList<String> values = new java.util.ArrayList<>();
|
||||
node.forEach(value -> values.add(value.asText()));
|
||||
return values;
|
||||
}
|
||||
|
||||
private static Boolean optionalBoolean(JsonNode node, String fieldName) {
|
||||
return node != null && node.has(fieldName) ? node.path(fieldName).asBoolean() : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package io.github.hectorvent.floci.services.cloudtrail;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.model.CloudTrailTrail;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CloudTrailService {
|
||||
private final StorageBackend<String, CloudTrailTrail> trailStore;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public CloudTrailService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
this.trailStore = storageFactory.create("cloudtrail", "cloudtrail-trails.json",
|
||||
new TypeReference<Map<String, CloudTrailTrail>>() {});
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
public CloudTrailTrail createTrail(String region, String name, String s3BucketName,
|
||||
boolean includeGlobalServiceEvents, boolean multiRegionTrail,
|
||||
boolean organizationTrail, Map<String, String> tags) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("InvalidTrailNameException", "Trail name is required", 400);
|
||||
}
|
||||
if (trailStore.get(key(region, name)).isPresent()) {
|
||||
throw new AwsException("TrailAlreadyExistsException", "Trail " + name + " already exists", 400);
|
||||
}
|
||||
|
||||
CloudTrailTrail trail = new CloudTrailTrail();
|
||||
Instant now = Instant.now();
|
||||
trail.setName(name);
|
||||
trail.setTrailArn(trailArn(region, name));
|
||||
trail.setS3BucketName(s3BucketName);
|
||||
trail.setIncludeGlobalServiceEvents(includeGlobalServiceEvents);
|
||||
trail.setMultiRegionTrail(multiRegionTrail);
|
||||
trail.setOrganizationTrail(organizationTrail);
|
||||
trail.setHomeRegion(region);
|
||||
trail.setCreated(now);
|
||||
trail.setUpdated(now);
|
||||
trail.setTags(tags == null ? Map.of() : tags);
|
||||
trailStore.put(key(region, name), trail);
|
||||
return trail;
|
||||
}
|
||||
|
||||
public CloudTrailTrail updateTrail(String region, String nameOrArn, String s3BucketName,
|
||||
Boolean includeGlobalServiceEvents, Boolean multiRegionTrail) {
|
||||
CloudTrailTrail trail = requireTrail(region, nameOrArn);
|
||||
if (s3BucketName != null && !s3BucketName.isBlank()) {
|
||||
trail.setS3BucketName(s3BucketName);
|
||||
}
|
||||
if (includeGlobalServiceEvents != null) {
|
||||
trail.setIncludeGlobalServiceEvents(includeGlobalServiceEvents);
|
||||
}
|
||||
if (multiRegionTrail != null) {
|
||||
trail.setMultiRegionTrail(multiRegionTrail);
|
||||
}
|
||||
trail.setUpdated(Instant.now());
|
||||
trailStore.put(key(trail.getHomeRegion(), trail.getName()), trail);
|
||||
return trail;
|
||||
}
|
||||
|
||||
public List<CloudTrailTrail> describeTrails(String region, List<String> namesOrArns) {
|
||||
if (namesOrArns == null || namesOrArns.isEmpty()) {
|
||||
return trailStore.scan(key -> key.startsWith(region + ":"));
|
||||
}
|
||||
return namesOrArns.stream()
|
||||
.map(nameOrArn -> findTrail(region, nameOrArn))
|
||||
.flatMap(java.util.Optional::stream)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public CloudTrailTrail requireTrail(String region, String nameOrArn) {
|
||||
return findTrail(region, nameOrArn)
|
||||
.orElseThrow(() -> new AwsException("TrailNotFoundException",
|
||||
"Trail " + nameOrArn + " does not exist", 400));
|
||||
}
|
||||
|
||||
public void startLogging(String region, String nameOrArn) {
|
||||
CloudTrailTrail trail = requireTrail(region, nameOrArn);
|
||||
trail.setLogging(true);
|
||||
trail.setUpdated(Instant.now());
|
||||
trailStore.put(key(trail.getHomeRegion(), trail.getName()), trail);
|
||||
}
|
||||
|
||||
public void stopLogging(String region, String nameOrArn) {
|
||||
CloudTrailTrail trail = requireTrail(region, nameOrArn);
|
||||
trail.setLogging(false);
|
||||
trail.setUpdated(Instant.now());
|
||||
trailStore.put(key(trail.getHomeRegion(), trail.getName()), trail);
|
||||
}
|
||||
|
||||
public void deleteTrail(String region, String nameOrArn) {
|
||||
CloudTrailTrail trail = requireTrail(region, nameOrArn);
|
||||
trailStore.delete(key(trail.getHomeRegion(), trail.getName()));
|
||||
}
|
||||
|
||||
public void putEventSelectors(String region, String nameOrArn) {
|
||||
requireTrail(region, nameOrArn);
|
||||
}
|
||||
|
||||
private java.util.Optional<CloudTrailTrail> findTrail(String region, String nameOrArn) {
|
||||
String name = nameFromNameOrArn(nameOrArn);
|
||||
if (name == null || name.isBlank()) {
|
||||
return java.util.Optional.empty();
|
||||
}
|
||||
java.util.Optional<CloudTrailTrail> sameRegion = trailStore.get(key(region, name));
|
||||
if (sameRegion.isPresent()) {
|
||||
return sameRegion;
|
||||
}
|
||||
Predicate<String> candidateKey = nameOrArn.startsWith("arn:")
|
||||
? ignored -> true
|
||||
: key -> key.endsWith(":" + name);
|
||||
return trailStore.scan(candidateKey).stream()
|
||||
.filter(trail -> name.equals(trail.getName()) || nameOrArn.equals(trail.getTrailArn()))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private String trailArn(String region, String name) {
|
||||
return regionResolver.buildArn("cloudtrail", region, "trail/" + name);
|
||||
}
|
||||
|
||||
private static String key(String region, String name) {
|
||||
return region + ":" + name;
|
||||
}
|
||||
|
||||
private static String nameFromNameOrArn(String nameOrArn) {
|
||||
if (nameOrArn == null) {
|
||||
return null;
|
||||
}
|
||||
int slash = nameOrArn.lastIndexOf('/');
|
||||
return slash >= 0 ? nameOrArn.substring(slash + 1) : nameOrArn;
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package io.github.hectorvent.floci.services.cloudtrail.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class CloudTrailTrail {
|
||||
private String name;
|
||||
private String trailArn;
|
||||
private String s3BucketName;
|
||||
private boolean includeGlobalServiceEvents;
|
||||
private boolean multiRegionTrail;
|
||||
private boolean organizationTrail;
|
||||
private boolean logging;
|
||||
private String homeRegion;
|
||||
private Instant created;
|
||||
private Instant updated;
|
||||
private Map<String, String> tags;
|
||||
|
||||
public CloudTrailTrail() {
|
||||
this.tags = new HashMap<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTrailArn() {
|
||||
return trailArn;
|
||||
}
|
||||
|
||||
public void setTrailArn(String trailArn) {
|
||||
this.trailArn = trailArn;
|
||||
}
|
||||
|
||||
public String getS3BucketName() {
|
||||
return s3BucketName;
|
||||
}
|
||||
|
||||
public void setS3BucketName(String s3BucketName) {
|
||||
this.s3BucketName = s3BucketName;
|
||||
}
|
||||
|
||||
public boolean isIncludeGlobalServiceEvents() {
|
||||
return includeGlobalServiceEvents;
|
||||
}
|
||||
|
||||
public void setIncludeGlobalServiceEvents(boolean includeGlobalServiceEvents) {
|
||||
this.includeGlobalServiceEvents = includeGlobalServiceEvents;
|
||||
}
|
||||
|
||||
public boolean isMultiRegionTrail() {
|
||||
return multiRegionTrail;
|
||||
}
|
||||
|
||||
public void setMultiRegionTrail(boolean multiRegionTrail) {
|
||||
this.multiRegionTrail = multiRegionTrail;
|
||||
}
|
||||
|
||||
public boolean isOrganizationTrail() {
|
||||
return organizationTrail;
|
||||
}
|
||||
|
||||
public void setOrganizationTrail(boolean organizationTrail) {
|
||||
this.organizationTrail = organizationTrail;
|
||||
}
|
||||
|
||||
public boolean isLogging() {
|
||||
return logging;
|
||||
}
|
||||
|
||||
public void setLogging(boolean logging) {
|
||||
this.logging = logging;
|
||||
}
|
||||
|
||||
public String getHomeRegion() {
|
||||
return homeRegion;
|
||||
}
|
||||
|
||||
public void setHomeRegion(String homeRegion) {
|
||||
this.homeRegion = homeRegion;
|
||||
}
|
||||
|
||||
public Instant getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Instant created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public Instant getUpdated() {
|
||||
return updated;
|
||||
}
|
||||
|
||||
public void setUpdated(Instant updated) {
|
||||
this.updated = updated;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new HashMap<>() : new HashMap<>(tags);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user