Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(cloudtrail): in-process CloudTrail with S3 data event emission (#1460)

Guilherme Lima024d74e5c4e9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9d9a6d117d..22afde83e0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Added
+
+- **cloudtrail:** new in-process service. Supports `CreateTrail`, `DescribeTrails`, `DeleteTrail`, `UpdateTrail`, `PutEventSelectors`, `GetEventSelectors`, `StartLogging`, `StopLogging`, `GetTrailStatus`, `LookupEvents`. S3 data events on hooked ops (`PutObject`, `GetObject`, `HeadObject`, `DeleteObject`, `ListObjects`, `GetObjectAcl`) are buffered per-trail and flushed as gzipped JSON log files into the destination bucket at AWS-shaped key paths (`AWSLogs/${account}/CloudTrail/${region}/yyyy/MM/dd/${file}.json.gz`) at a configurable cadence. IAM-enforcement denials emit matching `AccessDenied` records.
+- **iam:** seeded `AWSCloudTrail_FullAccess` and `AWSCloudTrail_ReadOnlyAccess` AWS managed policies.
+- **iam:** distinguish `s3:GetObjectAcl` / `s3:PutObjectAcl` / `s3:Get|Put|DeleteObjectTagging` from base S3 ops via `?acl` / `?tagging` query parameters when resolving an action for IAM enforcement.
+
## [1.5.33] - 2026-07-15
### Added
@@ -423,7 +429,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- bump `actions/checkout` from 4 to 6.0.2 ([#1159](https://github.com/floci-io/floci/pull/1159))
- bump `aws-actions/configure-aws-credentials` from 4 to 6.1.2 ([#1158](https://github.com/floci-io/floci/pull/1158))
- bump the actions-minor-patch group with 5 updates ([#1157](https://github.com/floci-io/floci/pull/1157))
-
## [1.5.21] - 2026-05-31
### Added
diff --git a/README.md b/README.md
index 38ee422f79..5ca65a1194 100644
--- a/README.md
+++ b/README.md
@@ -190,7 +190,7 @@ flowchart LR
Router["HTTP Router\nJAX-RS / Vert.x"]
subgraph Stateless ["Stateless Services"]
- A["SSM · SQS · SNS\nIAM · STS · KMS\nSecrets Manager · SES\nCognito · Kinesis\nEventBridge · Scheduler · AppConfig\nCloudWatch · Step Functions\nCloudFormation · ACM · Config\nAPI Gateway · AppSync · ELB v2 · Auto Scaling\nElastic Beanstalk · CodeDeploy · CodePipeline · Backup · Bedrock Runtime · Route53 · Transfer"]
+ A["SSM · SQS · SNS\nIAM · STS · KMS\nSecrets Manager · SES\nCognito · Kinesis\nEventBridge · Scheduler · AppConfig\nCloudWatch · Step Functions\nCloudFormation · ACM · Config · CloudTrail\nAPI Gateway · AppSync · ELB v2 · Auto Scaling\nElastic Beanstalk · CodeDeploy · CodePipeline · Backup · Bedrock Runtime · Route53 · Transfer"]
end
subgraph Stateful ["Stateful Services"]
@@ -292,7 +292,7 @@ For operation-level compatibility, see the [Services Overview](https://floci.io/
| Elastic Beanstalk | In-process | Applications, application versions, environments, configuration templates, platform and solution stack metadata |
| AWS Backup | In-process | Vaults, backup plans, selections, simulated job lifecycle, recovery points |
| AWS Config | In-process | Config rules, configuration recorders, delivery channels, conformance packs, tagging |
-| CloudTrail | In-process | Trail lifecycle, event selectors, logging status; management API only |
+| CloudTrail | In-process | Trails, event selectors (S3 data events with bucket/prefix matching), `StartLogging`/`StopLogging`, scheduled gzipped log file emission to the destination bucket at AWS-shaped key paths, IAM-deny path emits `AccessDenied` records |
| CloudFront | In-process | Distributions, origins, cache behaviors, invalidations, tagging |
| WAF v2 | In-process | Web ACLs, IP sets, regex pattern sets, rule groups, logging configs, resource associations, tagging (REGIONAL and CLOUDFRONT scopes) |
| Route53 | In-process | Hosted zones, SOA and NS records, resource record sets, change tracking, tagging |
diff --git a/docs/services/cloudtrail.md b/docs/services/cloudtrail.md
index 8b0279c94f..d36a8d6129 100644
--- a/docs/services/cloudtrail.md
+++ b/docs/services/cloudtrail.md
@@ -1,10 +1,15 @@
# CloudTrail
-**Protocol:** JSON 1.1
-**Endpoint:** `http://localhost:4566/`
-**Target prefix:** `X-Amz-Target: com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101.*`
+**Protocol:** JSON 1.1 (`X-Amz-Target: com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101.*`)
+**Endpoint:** `POST http://localhost:4566/`
-Floci emulates the AWS CloudTrail management API. Trails, their event selectors, and logging status are persisted in Floci's storage backend so you can create and manage trails and validate IaC locally. Floci does not record live API activity into trails.
+Floci emulates a CloudTrail trail's full life cycle and emits S3 data
+events to the trail's destination bucket as gzipped JSON log files at the
+AWS-shaped key path:
+
+```
+${prefix}AWSLogs/${accountId}/CloudTrail/${region}/yyyy/MM/dd/${accountId}_CloudTrail_${region}_${YYYYMMDDTHHMMZ}_${rand12}.json.gz
+```
## Supported Actions
@@ -12,29 +17,77 @@ Floci emulates the AWS CloudTrail management API. Trails, their event selectors,
| Action | Description |
| --- | --- |
| `CreateTrail` | Creates a trail and returns its ARN |
-| `UpdateTrail` | Updates the settings of an existing trail |
| `DescribeTrails` | Returns the settings for one or more trails |
+| `DeleteTrail` | Deletes a trail |
+| `UpdateTrail` | Updates the settings of an existing trail |
+| `PutEventSelectors` | Configures the event selectors for a trail |
+| `GetEventSelectors` | - |
| `StartLogging` | Starts logging for a trail |
| `StopLogging` | Stops logging for a trail |
-| `DeleteTrail` | Deletes a trail |
| `GetTrailStatus` | Returns the logging status of a trail |
-| `PutEventSelectors` | Configures the event selectors for a trail |
| `LookupEvents` | - |
<!-- floci:actions:end -->
+Selector matching honors `ReadWriteType` (`All`, `ReadOnly`, `WriteOnly`)
+and `AWS::S3::Object` data resource ARNs in any of these forms:
+
+- `arn:aws:s3:::` — every bucket
+- `arn:aws:s3:::bucket/` — every key in the bucket
+- `arn:aws:s3:::bucket/prefix` — keys with the given prefix
+- `arn:aws:s3:::bucket/prefix/key` — exact key match
+
+## S3 Hook Coverage
+
+Floci emits a CloudTrail data event whenever any of these S3 ops runs
+against a bucket matched by an active trail's selectors:
+
+- `PutObject`, `GetObject`, `HeadObject`, `DeleteObject`, `ListObjects`,
+ `GetObjectAcl`
+
+Both success and `AwsException` paths emit; the latter populates
+`errorCode` / `errorMessage` (`NoSuchKey`, `NoSuchBucketPolicy`, etc.).
+
+If `floci.iam.enforcement-enabled` is set, IAM-deny responses also emit
+records with `errorCode: "AccessDenied"` for the same op set.
+
+## Configuration
+
+| Variable | Default | Description |
+|---|---|---|
+| `FLOCI_SERVICES_CLOUDTRAIL_ENABLED` | `true` | Enable or disable the service |
+| `FLOCI_SERVICES_CLOUDTRAIL_FLUSH_INTERVAL_SECONDS` | `60` | How often the writer flushes buffered records into the destination bucket. Real CloudTrail delivers with ~5 minute latency; the local default is shorter for fast feedback loops. |
+
## Example
```bash
export AWS_ENDPOINT_URL=http://localhost:4566
+export AWS_DEFAULT_REGION=us-east-1
+export AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test
-# A trail needs a destination S3 bucket
-aws s3 mb s3://my-trail-bucket
+aws s3 mb s3://my-source
+aws s3 mb s3://my-trail-logs
aws cloudtrail create-trail \
- --name demo-trail \
- --s3-bucket-name my-trail-bucket
+ --name my-trail \
+ --s3-bucket-name my-trail-logs
+
+aws cloudtrail put-event-selectors \
+ --trail-name my-trail \
+ --event-selectors '[{
+ "ReadWriteType": "All",
+ "IncludeManagementEvents": false,
+ "DataResources": [{
+ "Type": "AWS::S3::Object",
+ "Values": ["arn:aws:s3:::my-source/"]
+ }]
+ }]'
+
+aws cloudtrail start-logging --name my-trail
+
+# Drive some traffic
+echo hello | aws s3 cp - s3://my-source/greeting.txt
+aws s3 cp s3://my-source/greeting.txt -
-aws cloudtrail start-logging --name demo-trail
-aws cloudtrail get-trail-status --name demo-trail
-aws cloudtrail describe-trails
+# After ~60s, log files appear
+aws s3 ls s3://my-trail-logs/AWSLogs/ --recursive
```
diff --git a/docs/services/index.md b/docs/services/index.md
index b71a744f0d..9c7e8efac0 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -72,7 +72,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [Route53](route53.md) | `/2013-04-01/hostedzone/*`, `/2013-04-01/healthcheck/*`, `/2013-04-01/change/*` | REST XML | 17 |
| [Cloud Map](cloudmap.md) | `POST /` + `X-Amz-Target: Route53AutoNaming_v20170314.*` | JSON 1.1 | 22 |
| [AWS Config](config.md) | `POST /` + `X-Amz-Target: StarlingDoveService.*` | JSON 1.1 | 20 |
-| [CloudTrail](cloudtrail.md) | `POST /` + `X-Amz-Target: com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101.*` | JSON 1.1 | 8 |
+| [CloudTrail](cloudtrail.md) | `POST /` + `X-Amz-Target: com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101.*` | JSON 1.1 | 9 |
| [Textract](textract.md) | `POST /` + `X-Amz-Target: Textract.*` | JSON 1.1 | 6 |
| [Transcribe](transcribe.md) | `POST /` + `X-Amz-Target: Transcribe.*` | JSON 1.1 | 8 |
| [Pricing](pricing.md) | `POST /` + `X-Amz-Target: AWSPriceListService.*` | JSON 1.1 | 5 |
diff --git a/mkdocs.yml b/mkdocs.yml
index 5f9e27ddc6..cf2900692d 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -122,6 +122,7 @@ nav:
- CodeDeploy: services/codedeploy.md
- CodePipeline: services/codepipeline.md
- AWS Backup: services/backup.md
+ - CloudTrail: services/cloudtrail.md
- CloudFront: services/cloudfront.md
- Route53: services/route53.md
- AWS Cloud Map: services/cloudmap.md
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index a90e0468b6..07266f31f7 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -266,6 +266,7 @@ public interface EmulatorConfig {
TranscribeStorageConfig transcribe();
TaggingStorageConfig tagging();
ElasticBeanstalkStorageConfig elasticbeanstalk();
+ CloudTrailStorageConfig cloudtrail();
}
interface SsmStorageConfig {
@@ -467,6 +468,13 @@ public interface EmulatorConfig {
long flushIntervalMs();
}
+ interface CloudTrailStorageConfig {
+ Optional<String> mode();
+
+ @WithDefault("5000")
+ long flushIntervalMs();
+ }
+
interface CodeDeployStorageConfig {
Optional<String> mode();
@@ -592,11 +600,6 @@ public interface EmulatorConfig {
boolean enabled();
}
- interface CloudTrailServiceConfig {
- @WithDefault("true")
- boolean enabled();
- }
-
interface LightsailServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -606,7 +609,6 @@ public interface EmulatorConfig {
@WithDefault("true")
boolean enabled();
}
-
interface S3VectorsServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -647,6 +649,17 @@ public interface EmulatorConfig {
boolean enabled();
}
+ interface CloudTrailServiceConfig {
+ @WithDefault("true")
+ boolean enabled();
+
+ /** How often the writer flushes pending records into the destination
+ * bucket. Real AWS delivers data events with ~5-minute lag; the
+ * default here is 60s so dev/CI feedback loops stay fast. */
+ @WithDefault("60")
+ int flushIntervalSeconds();
+ }
+
interface AutoScalingServiceConfig {
@WithDefault("true")
boolean enabled();
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
index 9a0fdf8dae..5932a71baf 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
@@ -1,12 +1,14 @@
package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
import io.github.hectorvent.floci.services.iam.model.CallerContext;
+import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
@@ -55,6 +57,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
private final ResourceArnBuilder arnBuilder;
private final RequestContext requestContext;
private final IamConditionContextResolver conditionContextResolver;
+ private final CloudTrailService cloudTrailService;
+ private final CurrentVertxRequest currentVertxRequest;
@Inject
public IamEnforcementFilter(EmulatorConfig config,
@@ -64,7 +68,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
IamActionRegistry actionRegistry,
ResourceArnBuilder arnBuilder,
RequestContext requestContext,
- IamConditionContextResolver conditionContextResolver) {
+ IamConditionContextResolver conditionContextResolver,
+ CloudTrailService cloudTrailService,
+ CurrentVertxRequest currentVertxRequest) {
this.config = config;
this.accountResolver = accountResolver;
this.iamService = iamService;
@@ -73,6 +79,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
this.arnBuilder = arnBuilder;
this.requestContext = requestContext;
this.conditionContextResolver = conditionContextResolver;
+ this.cloudTrailService = cloudTrailService;
+ this.currentVertxRequest = currentVertxRequest;
}
@Override
@@ -116,10 +124,115 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
Decision decision = evaluator.evaluate(caller, null, action, resource, conditionContext);
if (decision == Decision.DENY) {
LOG.infov("IAM enforcement DENY: akid={0} action={1} resource={2}", akid, action, resource);
+ String denyMessage = "User: arn:aws:iam::" + accountId
+ + ":user/" + akid + " is not authorized to perform: " + action
+ + " on resource: \"" + resource + "\""
+ + " because no identity-based policy allows the " + action + " action";
+ emitS3DenialIfApplicable(akid, action, resource, ctx, region, denyMessage);
ctx.abortWith(accessDeniedResponse(action, credentialScope, ctx.getMediaType()));
}
}
+ /**
+ * Best-effort CloudTrail emission for S3 access denials. Without this hook,
+ * denied requests get aborted before {@code S3Controller}'s try/catch sees
+ * them, so denials would never appear in CloudTrail logs — leaving a major
+ * gap vs. real AWS for downstream audit ingestion. Failures here never
+ * propagate (the deny response is the load-bearing behavior).
+ */
+ private void emitS3DenialIfApplicable(String akid, String action, String resource,
+ ContainerRequestContext ctx, String region,
+ String denyMessage) {
+ try {
+ if (action == null || !action.startsWith("s3:")) {
+ return;
+ }
+ String eventName = mapS3ActionToEventName(action, ctx.getMethod());
+ if (eventName == null) {
+ return;
+ }
+ String[] bk = parseS3Resource(resource);
+ String bucket = bk[0];
+ String key = bk[1];
+
+ String userAgent = null;
+ String sourceIp = null;
+ try {
+ var rc = currentVertxRequest.getCurrent();
+ if (rc != null) {
+ var req = rc.request();
+ if (req != null) {
+ userAgent = req.getHeader("User-Agent");
+ String fwd = req.getHeader("X-Forwarded-For");
+ if (fwd != null && !fwd.isBlank()) {
+ int comma = fwd.indexOf(',');
+ sourceIp = (comma > 0 ? fwd.substring(0, comma) : fwd).trim();
+ } else if (req.remoteAddress() != null) {
+ sourceIp = req.remoteAddress().host();
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.tracev(e, "CloudTrail: could not extract request context for IAM denial {0} on {1}", action, resource);
+ }
+
+ cloudTrailService.emitS3DataEvent(CloudTrailService.S3EventInput.builder()
+ .region(region)
+ .eventName(eventName)
+ .bucketName(bucket)
+ .key(key)
+ .accessKeyId(akid)
+ .sourceIp(sourceIp)
+ .userAgent(userAgent)
+ .errorCode("AccessDenied")
+ .errorMessage(denyMessage)
+ .eventTimeMillis(System.currentTimeMillis())
+ .build());
+ } catch (RuntimeException e) {
+ LOG.tracev(e, "CloudTrail denial emission failed for {0} on {1}", action, resource);
+ }
+ }
+
+ // Package-private for unit testing.
+ static String mapS3ActionToEventName(String action, String httpMethod) {
+ if (action == null) return null;
+ // Action set sourced from IamActionRegistry — see that file for any
+ // additions. HEAD on an object is bucketed under s3:GetObject by the
+ // registry, so we distinguish via httpMethod.
+ return switch (action) {
+ case "s3:GetObject" -> "HEAD".equalsIgnoreCase(httpMethod) ? "HeadObject" : "GetObject";
+ case "s3:PutObject" -> "PutObject";
+ case "s3:DeleteObject" -> "DeleteObject";
+ case "s3:ListBucket" -> "ListObjects";
+ case "s3:ListAllMyBuckets" -> "ListBuckets";
+ case "s3:GetObjectAcl" -> "GetObjectAcl";
+ case "s3:PutObjectAcl" -> "PutObjectAcl";
+ case "s3:GetObjectTagging" -> "GetObjectTagging";
+ case "s3:PutObjectTagging" -> "PutObjectTagging";
+ case "s3:DeleteObjectTagging" -> "DeleteObjectTagging";
+ default -> null;
+ };
+ }
+
+ /** Returns [bucket, key] (key may be null if the resource is a bucket-level ARN). */
+ // Package-private for unit testing.
+ static String[] parseS3Resource(String resource) {
+ if (resource == null || !resource.startsWith("arn:aws:s3:::")) {
+ return new String[] { null, null };
+ }
+ String tail = resource.substring("arn:aws:s3:::".length());
+ if (tail.isEmpty() || "*".equals(tail)) {
+ return new String[] { null, null };
+ }
+ int slash = tail.indexOf('/');
+ if (slash < 0) {
+ return new String[] { tail, null };
+ }
+ String bucket = tail.substring(0, slash);
+ String key = tail.substring(slash + 1);
+ return new String[] { bucket, key.isEmpty() ? null : key };
+ }
+
private String extractCredentialScope(String auth) {
Matcher m = SERVICE_PATTERN.matcher(auth);
return m.find() ? m.group(1) : null;
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 7a8b7c00d7..afe8873f44 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
@@ -285,9 +285,11 @@ public class ResolvedServiceCatalog {
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,
+ "cloudtrail", storageMode(config.storage().services().cloudtrail().mode(), config.storage().mode()),
+ config.storage().services().cloudtrail().flushIntervalMs(), null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
- Set.of("com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101."),
+ Set.of("CloudTrail_20131101.",
+ "com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101."),
Set.of("cloudtrail"), Set.of(), Set.of()),
descriptor("lightsail", "lightsail", config.services().lightsail().enabled(), true,
"lightsail", storageMode(config.storage().services().lightsail().mode(), config.storage().mode()),
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailEntry.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailEntry.java
new file mode 100644
index 0000000000..20fd0d45ef
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailEntry.java
@@ -0,0 +1,41 @@
+package io.github.hectorvent.floci.services.cloudtrail;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.github.hectorvent.floci.services.cloudtrail.model.EventSelector;
+import io.github.hectorvent.floci.services.cloudtrail.model.Trail;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.util.List;
+
+@RegisterForReflection
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@JsonIgnoreProperties(ignoreUnknown = true)
+public record CloudTrailEntry(
+ Trail trail,
+ List<EventSelector> selectors,
+ boolean logging,
+ Long startLoggingTime,
+ Long stopLoggingTime) {
+
+ public CloudTrailEntry withTrail(Trail updated) {
+ return new CloudTrailEntry(updated, selectors, logging, startLoggingTime, stopLoggingTime);
+ }
+
+ public CloudTrailEntry withSelectors(List<EventSelector> updated, boolean hasCustomSelectors) {
+ Trail updatedTrail = new Trail(
+ trail.name(), trail.trailArn(), trail.s3BucketName(), trail.s3KeyPrefix(),
+ trail.snsTopicArn(), trail.includeGlobalServiceEvents(), trail.isMultiRegionTrail(),
+ trail.homeRegion(), trail.logFileValidationEnabled(), hasCustomSelectors,
+ trail.hasInsightSelectors(), trail.isOrganizationTrail());
+ return new CloudTrailEntry(updatedTrail, updated, logging, startLoggingTime, stopLoggingTime);
+ }
+
+ public CloudTrailEntry startLogging(long time) {
+ return new CloudTrailEntry(trail, selectors, true, time, stopLoggingTime);
+ }
+
+ public CloudTrailEntry stopLogging(long time) {
+ return new CloudTrailEntry(trail, selectors, false, startLoggingTime, time);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java
index 746639493c..472c4770cb 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java
@@ -2,20 +2,21 @@ 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 io.github.hectorvent.floci.services.cloudtrail.model.DataResource;
+import io.github.hectorvent.floci.services.cloudtrail.model.EventSelector;
+import io.github.hectorvent.floci.services.cloudtrail.model.Trail;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
-import java.util.HashMap;
+import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
@ApplicationScoped
public class CloudTrailJsonHandler {
+
private final CloudTrailService service;
private final ObjectMapper mapper;
@@ -25,128 +26,195 @@ public class CloudTrailJsonHandler {
this.mapper = mapper;
}
- public Response handle(String action, JsonNode request, String region) {
+ public Response handle(String action, JsonNode request, String region) throws Exception {
return switch (action) {
case "CreateTrail" -> createTrail(request, region);
- case "UpdateTrail" -> updateTrail(request, region);
case "DescribeTrails" -> describeTrails(request, region);
+ case "DeleteTrail" -> deleteTrail(request, region);
+ case "UpdateTrail" -> updateTrail(request, region);
+ case "PutEventSelectors" -> putEventSelectors(request, region);
+ case "GetEventSelectors" -> getEventSelectors(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);
case "LookupEvents" -> lookupEvents(request, region);
- default -> throw new AwsException("UnsupportedOperation",
- "Operation " + action + " is not supported.", 400);
+ default -> throw new AwsException(
+ "InvalidAction", "Could not find operation " + action, 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 createTrail(JsonNode req, String region) {
+ String name = req.path("Name").asText(null);
+ String s3BucketName = req.path("S3BucketName").asText(null);
+ String s3KeyPrefix = req.has("S3KeyPrefix") ? req.path("S3KeyPrefix").asText(null) : null;
+ String snsTopicArn = req.has("SnsTopicARN") ? req.path("SnsTopicARN").asText(null)
+ : req.has("SnsTopicName") ? req.path("SnsTopicName").asText(null) : null;
+ boolean includeGlobal = req.path("IncludeGlobalServiceEvents").asBoolean(true);
+ boolean isMultiRegion = req.path("IsMultiRegionTrail").asBoolean(false);
+ boolean enableLogFileValidation = req.path("EnableLogFileValidation").asBoolean(false);
+ boolean isOrganizationTrail = req.path("IsOrganizationTrail").asBoolean(false);
+
+ Trail trail = service.createTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicArn,
+ includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail);
+
+ ObjectNode resp = mapper.createObjectNode();
+ resp.put("Name", trail.name());
+ resp.put("S3BucketName", trail.s3BucketName());
+ if (trail.s3KeyPrefix() != null) resp.put("S3KeyPrefix", trail.s3KeyPrefix());
+ if (trail.snsTopicArn() != null) {
+ resp.put("SnsTopicARN", trail.snsTopicArn());
+ resp.put("SnsTopicName", trail.snsTopicArn());
+ }
+ resp.put("IncludeGlobalServiceEvents", trail.includeGlobalServiceEvents());
+ resp.put("IsMultiRegionTrail", trail.isMultiRegionTrail());
+ resp.put("TrailARN", trail.trailArn());
+ resp.put("LogFileValidationEnabled", trail.logFileValidationEnabled());
+ resp.put("IsOrganizationTrail", trail.isOrganizationTrail());
+ return Response.ok(resp).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 deleteTrail(JsonNode req, String region) {
+ String name = req.path("Name").asText(null);
+ service.deleteTrail(region, name);
+ return Response.ok(mapper.createObjectNode()).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));
+ private Response updateTrail(JsonNode req, String region) {
+ String name = req.path("Name").asText(null);
+ String s3BucketName = req.has("S3BucketName") ? req.path("S3BucketName").asText(null) : null;
+ String s3KeyPrefix = req.has("S3KeyPrefix") ? req.path("S3KeyPrefix").asText(null) : null;
+ String snsTopicName = req.has("SnsTopicARN") ? req.path("SnsTopicARN").asText(null)
+ : req.has("SnsTopicName") ? req.path("SnsTopicName").asText(null) : null;
+ Boolean includeGlobal = req.has("IncludeGlobalServiceEvents")
+ ? req.path("IncludeGlobalServiceEvents").asBoolean() : null;
+ Boolean isMultiRegion = req.has("IsMultiRegionTrail")
+ ? req.path("IsMultiRegionTrail").asBoolean() : null;
+ Boolean enableLogFileValidation = req.has("EnableLogFileValidation")
+ ? req.path("EnableLogFileValidation").asBoolean() : null;
+ Boolean isOrganizationTrail = req.has("IsOrganizationTrail")
+ ? req.path("IsOrganizationTrail").asBoolean() : null;
+
+ Trail trail = service.updateTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicName,
+ includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail);
+
+ ObjectNode resp = mapper.createObjectNode();
+ resp.put("Name", trail.name());
+ resp.put("S3BucketName", trail.s3BucketName());
+ if (trail.s3KeyPrefix() != null) resp.put("S3KeyPrefix", trail.s3KeyPrefix());
+ if (trail.snsTopicArn() != null) {
+ resp.put("SnsTopicARN", trail.snsTopicArn());
+ resp.put("SnsTopicName", trail.snsTopicArn());
}
- return Response.ok(response).build();
+ resp.put("IncludeGlobalServiceEvents", trail.includeGlobalServiceEvents());
+ resp.put("IsMultiRegionTrail", trail.isMultiRegionTrail());
+ resp.put("TrailARN", trail.trailArn());
+ resp.put("LogFileValidationEnabled", trail.logFileValidationEnabled());
+ resp.put("IsOrganizationTrail", trail.isOrganizationTrail());
+ return Response.ok(resp).build();
}
- private Response startLogging(JsonNode request, String region) {
- service.startLogging(region, request.path("Name").asText(null));
- return Response.ok(mapper.createObjectNode()).build();
+ private Response describeTrails(JsonNode req, String region) {
+ List<String> nameList = extractStringList(req, "trailNameList");
+ List<Trail> trails = service.describeTrails(region, nameList);
+ ObjectNode resp = mapper.createObjectNode();
+ resp.set("trailList", mapper.valueToTree(trails));
+ return Response.ok(resp).build();
}
- private Response stopLogging(JsonNode request, String region) {
- service.stopLogging(region, request.path("Name").asText(null));
- return Response.ok(mapper.createObjectNode()).build();
- }
+ private Response putEventSelectors(JsonNode req, String region) {
+ String trailName = req.path("TrailName").asText(null);
+ List<EventSelector> selectors = parseEventSelectors(req.path("EventSelectors"));
+ List<EventSelector> stored = service.putEventSelectors(region, trailName, selectors);
- private Response deleteTrail(JsonNode request, String region) {
- service.deleteTrail(region, request.path("Name").asText(null));
- return Response.ok(mapper.createObjectNode()).build();
+ ObjectNode resp = mapper.createObjectNode();
+ Trail trail = firstTrail(service.describeTrails(region, List.of(trailName)));
+ if (trail != null) {
+ resp.put("TrailARN", trail.trailArn());
+ }
+ resp.set("EventSelectors", mapper.valueToTree(stored));
+ return Response.ok(resp).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);
+ private Response getEventSelectors(JsonNode req, String region) {
+ String trailName = req.path("TrailName").asText(null);
+ List<EventSelector> selectors = service.getEventSelectors(region, trailName);
+
+ ObjectNode resp = mapper.createObjectNode();
+ Trail trail = firstTrail(service.describeTrails(region, List.of(trailName)));
+ if (trail != null) {
+ resp.put("TrailARN", trail.trailArn());
}
- return Response.ok(response).build();
+ resp.set("EventSelectors", mapper.valueToTree(selectors));
+ return Response.ok(resp).build();
}
- private Response putEventSelectors(JsonNode request, String region) {
- service.putEventSelectors(region, request.path("TrailName").asText(null));
+ private Response startLogging(JsonNode req, String region) {
+ String trailName = req.path("Name").asText(null);
+ service.startLogging(region, trailName);
return Response.ok(mapper.createObjectNode()).build();
}
- private Response lookupEvents(JsonNode request, String region) {
- ObjectNode response = mapper.createObjectNode();
- response.putArray("Events");
- return Response.ok(response).build();
+ private Response stopLogging(JsonNode req, String region) {
+ String trailName = req.path("Name").asText(null);
+ service.stopLogging(region, trailName);
+ 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);
+ private Response getTrailStatus(JsonNode req, String region) {
+ String trailName = req.path("Name").asText(null);
+ CloudTrailService.TrailStatus status = service.getTrailStatus(region, trailName);
+
+ ObjectNode resp = mapper.createObjectNode();
+ resp.put("IsLogging", status.logging());
+ if (status.startLoggingTime() != null) {
+ resp.put("StartLoggingTime", status.startLoggingTime() / 1000.0);
+ resp.put("LatestDeliveryTime", status.startLoggingTime() / 1000.0);
}
- return node;
+ if (status.stopLoggingTime() != null) {
+ resp.put("StopLoggingTime", status.stopLoggingTime() / 1000.0);
+ }
+ return Response.ok(resp).build();
+ }
+
+ private Response lookupEvents(JsonNode req, String region) {
+ ObjectNode resp = mapper.createObjectNode();
+ resp.putArray("Events");
+ return Response.ok(resp).build();
}
- 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(""));
+ // --- Helpers ---
+
+ private List<EventSelector> parseEventSelectors(JsonNode selectorsNode) {
+ List<EventSelector> result = new ArrayList<>();
+ if (selectorsNode == null || !selectorsNode.isArray()) return result;
+ for (JsonNode sel : selectorsNode) {
+ String readWriteType = sel.has("ReadWriteType") ? sel.path("ReadWriteType").asText() : "All";
+ Boolean includeManagement = sel.has("IncludeManagementEvents")
+ ? sel.path("IncludeManagementEvents").asBoolean() : null;
+ List<String> excludeManagement = extractStringList(sel, "ExcludeManagementEventSources");
+ List<DataResource> dataResources = new ArrayList<>();
+ if (sel.has("DataResources")) {
+ for (JsonNode dr : sel.path("DataResources")) {
+ String type = dr.path("Type").asText(null);
+ List<String> values = extractStringList(dr, "Values");
+ dataResources.add(new DataResource(type, values));
}
}
+ result.add(new EventSelector(readWriteType, includeManagement, dataResources,
+ excludeManagement.isEmpty() ? null : excludeManagement));
}
- return tags;
+ return result;
}
- 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 Trail firstTrail(List<Trail> trails) {
+ return trails.isEmpty() ? null : trails.get(0);
}
- private static Boolean optionalBoolean(JsonNode node, String fieldName) {
- return node != null && node.has(fieldName) ? node.path(fieldName).asBoolean() : null;
+ private List<String> extractStringList(JsonNode req, String fieldName) {
+ List<String> result = new ArrayList<>();
+ if (req != null && req.has(fieldName)) {
+ req.path(fieldName).forEach(n -> result.add(n.asText()));
+ }
+ return result;
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailLogWriter.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailLogWriter.java
new file mode 100644
index 0000000000..33a1d06ce7
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailLogWriter.java
@@ -0,0 +1,195 @@
+package io.github.hectorvent.floci.services.cloudtrail;
+
+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.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.services.cloudtrail.model.Trail;
+import io.github.hectorvent.floci.services.s3.S3Service;
+import io.quarkus.runtime.ShutdownEvent;
+import io.quarkus.runtime.Startup;
+import jakarta.annotation.PostConstruct;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.security.SecureRandom;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.zip.GZIPOutputStream;
+
+/**
+ * Periodically drains queued CloudTrail records from {@link CloudTrailService}
+ * and writes them as gzipped JSON log files into each trail's destination
+ * S3 bucket, using AWS's documented key layout:
+ *
+ * <pre>{@code
+ * ${prefix}AWSLogs/${accountId}/CloudTrail/${region}/yyyy/MM/dd/
+ * ${accountId}_CloudTrail_${region}_${YYYYMMDDTHHMMZ}_${rand12}.json.gz
+ * }</pre>
+ *
+ * Real CloudTrail delivers logs with ~5 minute latency. The flush cadence
+ * here is configurable so local dev / CI loops stay fast.
+ */
+@Startup
+@ApplicationScoped
+public class CloudTrailLogWriter {
+
+ private static final Logger LOG = Logger.getLogger(CloudTrailLogWriter.class);
+
+ private static final DateTimeFormatter PATH_DATE = DateTimeFormatter.ofPattern("yyyy/MM/dd");
+ private static final DateTimeFormatter FILE_TS = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmm'Z'");
+ private static final String FILENAME_RAND_ALPHABET =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+ private final EmulatorConfig config;
+ private final CloudTrailService cloudTrailService;
+ private final S3Service s3Service;
+ private final RegionResolver regionResolver;
+ private final ObjectMapper mapper;
+ private final SecureRandom rng = new SecureRandom();
+
+ private ScheduledExecutorService executor;
+
+ @Inject
+ public CloudTrailLogWriter(EmulatorConfig config,
+ CloudTrailService cloudTrailService,
+ S3Service s3Service,
+ RegionResolver regionResolver,
+ ObjectMapper mapper) {
+ this.config = config;
+ this.cloudTrailService = cloudTrailService;
+ this.s3Service = s3Service;
+ this.regionResolver = regionResolver;
+ this.mapper = mapper;
+ }
+
+ @PostConstruct
+ void start() {
+ if (!config.services().cloudtrail().enabled()) {
+ return;
+ }
+ int interval = Math.max(1, config.services().cloudtrail().flushIntervalSeconds());
+ executor = Executors.newSingleThreadScheduledExecutor(r -> {
+ Thread t = new Thread(r, "cloudtrail-log-writer");
+ t.setDaemon(true);
+ return t;
+ });
+ executor.scheduleAtFixedRate(this::flushAll, interval, interval, TimeUnit.SECONDS);
+ LOG.infov("CloudTrail log writer started (flushIntervalSeconds={0})", interval);
+ }
+
+ void stop(@Observes ShutdownEvent event) {
+ if (executor != null) {
+ // Best-effort final flush so shutdown doesn't lose buffered events.
+ try { flushAll(); } catch (Exception e) { LOG.warnv(e, "CloudTrail final flush on shutdown failed — some records may be lost"); }
+ executor.shutdownNow();
+ executor = null;
+ }
+ }
+
+ /**
+ * Force an immediate flush. Useful for tests and for deterministic CLI
+ * verification.
+ */
+ public void flushNow() {
+ flushAll();
+ }
+
+ private void flushAll() {
+ try {
+ for (CloudTrailService.TrailKey key : cloudTrailService.trailsWithPendingRecords()) {
+ try {
+ flushTrail(key);
+ } catch (RuntimeException e) {
+ LOG.warnv(e, "CloudTrail log flush failed for trail {0} in {1}",
+ key.trailName(), key.region());
+ }
+ }
+ } catch (RuntimeException outer) {
+ LOG.errorv(outer, "CloudTrail log writer iteration failed");
+ }
+ }
+
+ private void flushTrail(CloudTrailService.TrailKey key) {
+ Trail trail = cloudTrailService.getTrail(key.region(), key.trailName());
+ if (trail == null) {
+ // Trail was deleted while records were pending — drop them.
+ cloudTrailService.drainPendingRecords(key);
+ return;
+ }
+
+ List<ObjectNode> records = cloudTrailService.drainPendingRecords(key);
+ if (records.isEmpty()) {
+ return;
+ }
+
+ try {
+ byte[] payload = serializeAndGzip(records);
+ String accountId = regionResolver.getAccountId();
+ // Use the event region for the S3 delivery path so multi-region trail
+ // events from us-west-2 land under CloudTrail/us-west-2, not the trail's home region.
+ String objectKey = buildObjectKey(trail, accountId, key.eventRegion());
+ s3Service.putObject(trail.s3BucketName(), objectKey, payload,
+ "application/x-gzip", Map.of());
+ LOG.debugv("CloudTrail wrote {0} records to s3://{1}/{2}",
+ records.size(), trail.s3BucketName(), objectKey);
+ } catch (RuntimeException e) {
+ // Re-queue so records survive the failed flush and are retried next cycle.
+ cloudTrailService.requeueRecords(key, records);
+ LOG.warnv(e, "CloudTrail flush failed for trail {0} ({1} records re-queued)",
+ key.trailName(), records.size());
+ throw e;
+ }
+ }
+
+ private byte[] serializeAndGzip(List<ObjectNode> records) {
+ ObjectNode envelope = mapper.createObjectNode();
+ ArrayNode arr = envelope.putArray("Records");
+ for (ObjectNode r : records) {
+ arr.add(r);
+ }
+ try {
+ byte[] json = mapper.writeValueAsBytes(envelope);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(Math.max(64, json.length / 4));
+ try (GZIPOutputStream gz = new GZIPOutputStream(baos)) {
+ gz.write(json);
+ }
+ return baos.toByteArray();
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to serialize CloudTrail records", e);
+ }
+ }
+
+ private String buildObjectKey(Trail trail, String accountId, String region) {
+ LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
+ String prefix = trail.s3KeyPrefix() == null || trail.s3KeyPrefix().isEmpty()
+ ? "" : trail.s3KeyPrefix() + (trail.s3KeyPrefix().endsWith("/") ? "" : "/");
+ String datePath = PATH_DATE.format(now);
+ String fileTs = FILE_TS.format(now);
+ String rand = randomFilenameSuffix(16);
+ return prefix
+ + "AWSLogs/" + accountId
+ + "/CloudTrail/" + region
+ + "/" + datePath + "/"
+ + accountId + "_CloudTrail_" + region + "_" + fileTs + "_" + rand + ".json.gz";
+ }
+
+ private String randomFilenameSuffix(int len) {
+ StringBuilder sb = new StringBuilder(len);
+ for (int i = 0; i < len; i++) {
+ sb.append(FILENAME_RAND_ALPHABET.charAt(rng.nextInt(FILENAME_RAND_ALPHABET.length())));
+ }
+ return sb.toString();
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java
index 105aee20e4..fbd40f1856 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java
@@ -1,143 +1,562 @@
package io.github.hectorvent.floci.services.cloudtrail;
import com.fasterxml.jackson.core.type.TypeReference;
+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.AwsArnUtils;
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 io.github.hectorvent.floci.services.cloudtrail.model.DataResource;
+import io.github.hectorvent.floci.services.cloudtrail.model.EventSelector;
+import io.github.hectorvent.floci.services.cloudtrail.model.Trail;
+import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.iam.model.AccessKey;
+import io.github.hectorvent.floci.services.iam.model.IamUser;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
import java.time.Instant;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import java.util.function.Predicate;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
@ApplicationScoped
public class CloudTrailService {
- private final StorageBackend<String, CloudTrailTrail> trailStore;
+
+ private static final Logger LOG = Logger.getLogger(CloudTrailService.class);
+
+ private static final String EVENT_VERSION = "1.11";
+ private static final String S3_EVENT_SOURCE = "s3.amazonaws.com";
+
+ private final StorageBackend<String, CloudTrailEntry> store;
private final RegionResolver regionResolver;
+ private final IamService iamService;
+ private final ObjectMapper mapper;
+
+ /** Per-trail pending record buffers — ephemeral, never persisted. */
+ private final ConcurrentHashMap<TrailKey, ConcurrentLinkedQueue<ObjectNode>> pendingRecordsByTrail =
+ new ConcurrentHashMap<>();
@Inject
- public CloudTrailService(StorageFactory storageFactory, RegionResolver regionResolver) {
- this.trailStore = storageFactory.create("cloudtrail", "cloudtrail-trails.json",
- new TypeReference<Map<String, CloudTrailTrail>>() {});
+ public CloudTrailService(StorageFactory storageFactory, RegionResolver regionResolver,
+ IamService iamService, ObjectMapper mapper) {
+ this.store = storageFactory.create("cloudtrail", "cloudtrail-trails.json",
+ new TypeReference<Map<String, CloudTrailEntry>>() {});
this.regionResolver = regionResolver;
+ this.iamService = iamService;
+ this.mapper = mapper;
}
- 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);
+ // --- Control plane ---
+
+ public Trail createTrail(String region, String name, String s3BucketName, String s3KeyPrefix,
+ String snsTopicArn, boolean includeGlobalServiceEvents,
+ boolean isMultiRegionTrail, boolean enableLogFileValidation,
+ boolean isOrganizationTrail) {
+ validateTrailName(name);
+ if (s3BucketName == null || s3BucketName.isEmpty()) {
+ throw new AwsException("S3BucketDoesNotExistException", "S3 bucket name is required.", 400);
+ }
+ String key = regionKey(region, name);
+ if (store.get(key).isPresent()) {
+ throw new AwsException("TrailAlreadyExistsException",
+ "Trail " + name + " already exists.", 400);
+ }
+ String arn = AwsArnUtils.Arn.of("cloudtrail", region, regionResolver.getAccountId(),
+ "trail/" + name).toString();
+ Trail trail = new Trail(
+ name, arn, s3BucketName, s3KeyPrefix, snsTopicArn,
+ includeGlobalServiceEvents, isMultiRegionTrail, region,
+ enableLogFileValidation, false, false, isOrganizationTrail);
+ store.put(key, new CloudTrailEntry(trail, List.of(), false, null, null));
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);
+ public void deleteTrail(String region, String trailNameOrArn) {
+ Trail trail = findTrailOrThrow(region, trailNameOrArn);
+ store.delete(regionKey(trail.homeRegion(), trail.name()));
+ pendingRecordsByTrail.keySet().removeIf(k -> k.trailName().equals(trail.name()));
+ }
+
+ public Trail updateTrail(String region, String trailNameOrArn,
+ String s3BucketName, String s3KeyPrefix, String snsTopicArn,
+ Boolean includeGlobalServiceEvents, Boolean isMultiRegionTrail,
+ Boolean enableLogFileValidation, Boolean isOrganizationTrail) {
+ Trail existing = findTrailOrThrow(region, trailNameOrArn);
+ Trail updated = new Trail(
+ existing.name(),
+ existing.trailArn(),
+ s3BucketName != null ? s3BucketName : existing.s3BucketName(),
+ s3KeyPrefix != null ? s3KeyPrefix : existing.s3KeyPrefix(),
+ snsTopicArn != null ? snsTopicArn : existing.snsTopicArn(),
+ includeGlobalServiceEvents != null ? includeGlobalServiceEvents : existing.includeGlobalServiceEvents(),
+ isMultiRegionTrail != null ? isMultiRegionTrail : existing.isMultiRegionTrail(),
+ existing.homeRegion(),
+ enableLogFileValidation != null ? enableLogFileValidation : existing.logFileValidationEnabled(),
+ existing.hasCustomEventSelectors(),
+ existing.hasInsightSelectors(),
+ isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail());
+ String key = regionKey(existing.homeRegion(), existing.name());
+ store.get(key).ifPresent(entry -> store.put(key, entry.withTrail(updated)));
+ return updated;
+ }
+
+ public List<Trail> describeTrails(String region, List<String> trailNameOrArnList) {
+ if (trailNameOrArnList == null || trailNameOrArnList.isEmpty()) {
+ List<Trail> results = new ArrayList<>();
+ for (String k : store.keys()) {
+ String trailRegion = regionFromKey(k);
+ CloudTrailEntry entry = store.get(k).orElse(null);
+ if (entry == null) continue;
+ Trail t = entry.trail();
+ if (trailRegion.equals(region) || t.isMultiRegionTrail()) {
+ results.add(t);
+ }
+ }
+ return results;
}
- if (includeGlobalServiceEvents != null) {
- trail.setIncludeGlobalServiceEvents(includeGlobalServiceEvents);
+ List<Trail> results = new ArrayList<>();
+ for (String nameOrArn : trailNameOrArnList) {
+ Trail t = findTrail(region, nameOrArn);
+ if (t != null) results.add(t);
}
- if (multiRegionTrail != null) {
- trail.setMultiRegionTrail(multiRegionTrail);
+ return results;
+ }
+
+ public List<EventSelector> putEventSelectors(String region, String trailNameOrArn, List<EventSelector> selectors) {
+ Trail trail = findTrailOrThrow(region, trailNameOrArn);
+ List<EventSelector> normalized = selectors == null ? List.of() : List.copyOf(selectors);
+ String key = regionKey(trail.homeRegion(), trail.name());
+ store.get(key).ifPresent(entry -> store.put(key, entry.withSelectors(normalized, true)));
+ return normalized;
+ }
+
+ public List<EventSelector> getEventSelectors(String region, String trailNameOrArn) {
+ Trail trail = findTrailOrThrow(region, trailNameOrArn);
+ return store.get(regionKey(trail.homeRegion(), trail.name()))
+ .map(e -> e.selectors() != null ? e.selectors() : List.<EventSelector>of())
+ .orElse(List.of());
+ }
+
+ public void startLogging(String region, String trailNameOrArn) {
+ Trail trail = findTrailOrThrow(region, trailNameOrArn);
+ String key = regionKey(trail.homeRegion(), trail.name());
+ store.get(key).ifPresent(entry -> store.put(key, entry.startLogging(System.currentTimeMillis())));
+ }
+
+ public void stopLogging(String region, String trailNameOrArn) {
+ Trail trail = findTrailOrThrow(region, trailNameOrArn);
+ String key = regionKey(trail.homeRegion(), trail.name());
+ store.get(key).ifPresent(entry -> store.put(key, entry.stopLogging(System.currentTimeMillis())));
+ }
+
+ public TrailStatus getTrailStatus(String region, String trailNameOrArn) {
+ Trail trail = findTrailOrThrow(region, trailNameOrArn);
+ return store.get(regionKey(trail.homeRegion(), trail.name()))
+ .map(e -> new TrailStatus(e.logging(), e.startLoggingTime(), e.stopLoggingTime()))
+ .orElse(new TrailStatus(false, null, null));
+ }
+
+ // --- Data plane: called by S3 (and other services) when an op happens ---
+
+ public void emitS3DataEvent(S3EventInput in) {
+ try {
+ String region = in.region() != null ? in.region() : regionResolver.getDefaultRegion();
+ List<MatchedTrail> matched = trailsMatching(region, in);
+ if (matched.isEmpty()) {
+ return;
+ }
+ ObjectNode record = buildS3Record(in);
+ for (MatchedTrail mt : matched) {
+ ObjectNode copy = record.deepCopy();
+ copy.put("recipientAccountId", regionResolver.getAccountId());
+ queueFor(new TrailKey(mt.region(), mt.trail().name(), region)).add(copy);
+ LOG.tracev("Emitted CloudTrail event {0} for trail {1}", in.eventName(), mt.trail().name());
+ }
+ } catch (Exception e) {
+ // Never let emission take down an S3 op.
+ LOG.warnv(e, "Failed to emit CloudTrail event for {0} {1}/{2}",
+ in.eventName(), in.bucketName(), in.key());
+ }
+ }
+
+ public void requeueRecords(TrailKey key, List<ObjectNode> records) {
+ if (!records.isEmpty()) {
+ queueFor(key).addAll(records);
}
- 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 + ":"));
+ public List<ObjectNode> drainPendingRecords(TrailKey key) {
+ ConcurrentLinkedQueue<ObjectNode> q = pendingRecordsByTrail.get(key);
+ if (q == null) return List.of();
+ List<ObjectNode> drained = new ArrayList<>();
+ ObjectNode r;
+ while ((r = q.poll()) != null) {
+ drained.add(r);
}
- return namesOrArns.stream()
- .map(nameOrArn -> findTrail(region, nameOrArn))
- .flatMap(java.util.Optional::stream)
- .toList();
+ return drained;
}
- public CloudTrailTrail requireTrail(String region, String nameOrArn) {
- return findTrail(region, nameOrArn)
- .orElseThrow(() -> new AwsException("TrailNotFoundException",
- "Trail " + nameOrArn + " does not exist", 400));
+ public List<TrailKey> trailsWithPendingRecords() {
+ List<TrailKey> result = new ArrayList<>();
+ for (Map.Entry<TrailKey, ConcurrentLinkedQueue<ObjectNode>> e : pendingRecordsByTrail.entrySet()) {
+ if (!e.getValue().isEmpty()) {
+ result.add(e.getKey());
+ }
+ }
+ return result;
}
- 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 Trail getTrail(String region, String trailName) {
+ return store.get(regionKey(region, trailName))
+ .map(CloudTrailEntry::trail)
+ .orElse(null);
}
- 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);
+ private ConcurrentLinkedQueue<ObjectNode> queueFor(TrailKey key) {
+ return pendingRecordsByTrail.computeIfAbsent(key, k -> new ConcurrentLinkedQueue<>());
}
- public void deleteTrail(String region, String nameOrArn) {
- CloudTrailTrail trail = requireTrail(region, nameOrArn);
- trailStore.delete(key(trail.getHomeRegion(), trail.getName()));
+ /**
+ * Identifies a pending-records queue.
+ * {@code region} is the trail's home region (used for trail store lookups).
+ * {@code eventRegion} is the region where the event occurred (used for the S3 delivery path).
+ * For single-region trails these are the same; for multi-region trails they differ.
+ */
+ public record TrailKey(String region, String trailName, String eventRegion) {}
+
+ // --- Helpers ---
+
+ private List<MatchedTrail> trailsMatching(String region, S3EventInput in) {
+ List<MatchedTrail> result = new ArrayList<>();
+ for (String k : store.keys()) {
+ String trailRegion = regionFromKey(k);
+ boolean sameRegion = trailRegion.equals(region);
+ CloudTrailEntry entry = store.get(k).orElse(null);
+ if (entry == null) continue;
+ Trail trail = entry.trail();
+ if (!sameRegion && !trail.isMultiRegionTrail()) continue;
+ if (!entry.logging()) continue;
+ List<EventSelector> selectors = entry.selectors() != null ? entry.selectors() : List.of();
+ if (matchesAnySelector(selectors, in)) {
+ result.add(new MatchedTrail(trail, trailRegion));
+ }
+ }
+ return result;
}
- public void putEventSelectors(String region, String nameOrArn) {
- requireTrail(region, nameOrArn);
+ private boolean matchesAnySelector(List<EventSelector> selectors, S3EventInput in) {
+ if (selectors.isEmpty()) {
+ return false;
+ }
+ boolean isRead = isReadOnlyEvent(in.eventName());
+ for (EventSelector sel : selectors) {
+ String rwt = sel.readWriteType() == null ? "All" : sel.readWriteType();
+ if ("ReadOnly".equalsIgnoreCase(rwt) && !isRead) continue;
+ if ("WriteOnly".equalsIgnoreCase(rwt) && isRead) continue;
+
+ List<DataResource> dataResources = sel.dataResources();
+ if (dataResources == null || dataResources.isEmpty()) {
+ continue;
+ }
+ for (DataResource dr : dataResources) {
+ if (!"AWS::S3::Object".equals(dr.type())) continue;
+ if (dr.values() == null) continue;
+ for (String v : dr.values()) {
+ if (matchesS3DataResourceArn(v, in.bucketName(), in.key())) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
}
- private java.util.Optional<CloudTrailTrail> findTrail(String region, String nameOrArn) {
- String name = nameFromNameOrArn(nameOrArn);
- if (name == null || name.isBlank()) {
- return java.util.Optional.empty();
+ // Package-private for unit testing.
+ static boolean matchesS3DataResourceArn(String configured, String bucketName, String key) {
+ if (configured == null) return false;
+ // "arn:aws:s3" (bare, no ":::") is shorthand for all buckets + all objects.
+ if (configured.equals("arn:aws:s3")) return true;
+ // Forms accepted:
+ // arn:aws:s3::: → all buckets, all keys
+ // arn:aws:s3:::* → all buckets (wildcard)
+ // arn:aws:s3:::bucket/ → all keys in bucket
+ // arn:aws:s3:::bucket/prefix → keys with the given prefix in bucket
+ // arn:aws:s3:::*/* → all objects (wildcard bucket + any key)
+ String prefix = "arn:aws:s3:::";
+ if (!configured.startsWith(prefix)) return false;
+ String tail = configured.substring(prefix.length());
+ if (tail.isEmpty() || tail.equals("/")) {
+ return true;
+ }
+ int slash = tail.indexOf('/');
+ if (slash < 0) {
+ return tail.equals("*") || tail.equals(bucketName);
}
- java.util.Optional<CloudTrailTrail> sameRegion = trailStore.get(key(region, name));
- if (sameRegion.isPresent()) {
- return sameRegion;
+ String configBucket = tail.substring(0, slash);
+ if (!configBucket.equals("*") && !configBucket.equals(bucketName)) return false;
+ String configKeyPart = tail.substring(slash + 1);
+ if (configKeyPart.isEmpty()) {
+ return true;
}
- 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();
+ if (configKeyPart.equals("*") || configKeyPart.equals("*/*")) {
+ return key != null;
+ }
+ if (key == null) return false;
+ return key.startsWith(configKeyPart);
}
- private String trailArn(String region, String name) {
- return regionResolver.buildArn("cloudtrail", region, "trail/" + name);
+ // Package-private for unit testing.
+ static boolean isReadOnlyEvent(String eventName) {
+ if (eventName == null) return true;
+ return switch (eventName) {
+ case "GetObject", "HeadObject", "ListObjects", "ListObjectsV2",
+ "GetObjectAcl", "GetObjectTagging", "ListMultipartUploads" -> true;
+ default -> false;
+ };
}
- private static String key(String region, String name) {
- return region + ":" + name;
+ private ObjectNode buildS3Record(S3EventInput in) {
+ ObjectNode record = mapper.createObjectNode();
+ record.put("eventVersion", EVENT_VERSION);
+ record.set("userIdentity", buildUserIdentity(in.accessKeyId()));
+ record.put("eventTime", DateTimeFormatter.ISO_INSTANT.format(
+ Instant.ofEpochMilli((in.eventTimeMillis() == 0L
+ ? System.currentTimeMillis() : in.eventTimeMillis()))
+ .truncatedTo(java.time.temporal.ChronoUnit.SECONDS)));
+ record.put("eventSource", S3_EVENT_SOURCE);
+ record.put("eventName", in.eventName());
+ record.put("awsRegion", in.region());
+ record.put("sourceIPAddress", in.sourceIp() == null ? "127.0.0.1" : in.sourceIp());
+ record.put("userAgent", in.userAgent() == null ? "" : in.userAgent());
+
+ if (in.errorCode() != null) {
+ record.put("errorCode", in.errorCode());
+ if (in.errorMessage() != null) {
+ record.put("errorMessage", in.errorMessage());
+ }
+ }
+
+ ObjectNode reqParams = mapper.createObjectNode();
+ if (in.bucketName() != null) reqParams.put("bucketName", in.bucketName());
+ reqParams.put("Host", in.bucketName() == null
+ ? "s3.amazonaws.com"
+ : in.bucketName() + ".s3.amazonaws.com");
+ if (in.key() != null) reqParams.put("key", in.key());
+ record.set("requestParameters", reqParams);
+ record.set("responseElements", mapper.nullNode());
+
+ ObjectNode addl = mapper.createObjectNode();
+ addl.put("SignatureVersion", "SigV4");
+ addl.put("CipherSuite", "TLS_AES_128_GCM_SHA256");
+ addl.put("bytesTransferredIn", in.bytesIn());
+ addl.put("AuthenticationMethod", "AuthHeader");
+ addl.put("bytesTransferredOut", in.bytesOut());
+ record.set("additionalEventData", addl);
+
+ record.put("requestID", UUID.randomUUID().toString().replace("-", "").substring(0, 16).toUpperCase());
+ record.put("eventID", UUID.randomUUID().toString());
+ record.put("readOnly", isReadOnlyEvent(in.eventName()));
+
+ if (in.bucketName() != null) {
+ ArrayNode resources = mapper.createArrayNode();
+ ObjectNode bucketRes = mapper.createObjectNode();
+ bucketRes.put("accountId", regionResolver.getAccountId());
+ bucketRes.put("type", "AWS::S3::Bucket");
+ bucketRes.put("ARN", "arn:aws:s3:::" + in.bucketName());
+ resources.add(bucketRes);
+ if (in.key() != null) {
+ ObjectNode objRes = mapper.createObjectNode();
+ objRes.put("type", "AWS::S3::Object");
+ objRes.put("ARN", "arn:aws:s3:::" + in.bucketName() + "/" + in.key());
+ resources.add(objRes);
+ }
+ record.set("resources", resources);
+ }
+
+ record.put("eventType", "AwsApiCall");
+ record.put("managementEvent", false);
+ record.put("eventCategory", "Data");
+
+ ObjectNode tls = mapper.createObjectNode();
+ tls.put("tlsVersion", "TLSv1.3");
+ tls.put("cipherSuite", "TLS_AES_128_GCM_SHA256");
+ tls.put("clientProvidedHostHeader", in.bucketName() == null
+ ? "s3.amazonaws.com"
+ : in.bucketName() + ".s3.amazonaws.com");
+ record.set("tlsDetails", tls);
+
+ return record;
}
- private static String nameFromNameOrArn(String nameOrArn) {
- if (nameOrArn == null) {
+ private ObjectNode buildUserIdentity(String accessKeyId) {
+ ObjectNode identity = mapper.createObjectNode();
+ String accountId = regionResolver.getAccountId();
+
+ if (accessKeyId == null || "test".equals(accessKeyId)) {
+ identity.put("type", "IAMUser");
+ identity.put("principalId", "AIDA" + repeat('A', 17));
+ identity.put("arn", "arn:aws:iam::" + accountId + ":root");
+ identity.put("accountId", accountId);
+ identity.put("accessKeyId", accessKeyId == null ? "" : accessKeyId);
+ identity.put("userName", "root");
+ return identity;
+ }
+
+ AccessKey key = iamService.findAccessKey(accessKeyId).orElse(null);
+ if (key != null) {
+ IamUser user = iamService.findUser(key.getUserName()).orElse(null);
+ if (user != null) {
+ identity.put("type", "IAMUser");
+ identity.put("principalId", user.getUserId());
+ identity.put("arn", user.getArn());
+ identity.put("accountId", accountId);
+ identity.put("accessKeyId", accessKeyId);
+ identity.put("userName", user.getUserName());
+ return identity;
+ }
+ }
+
+ identity.put("type", "IAMUser");
+ identity.put("principalId", "AIDA" + repeat('A', 17));
+ identity.put("arn", "arn:aws:iam::" + accountId + ":user/anonymous");
+ identity.put("accountId", accountId);
+ identity.put("accessKeyId", accessKeyId);
+ identity.put("userName", "anonymous");
+ return identity;
+ }
+
+ private static String repeat(char c, int n) {
+ char[] arr = new char[n];
+ java.util.Arrays.fill(arr, c);
+ return new String(arr);
+ }
+
+ private Trail findTrail(String region, String nameOrArn) {
+ // ARN → cross-region scan is valid (callers use ARN to target another Region)
+ if (nameOrArn != null && nameOrArn.startsWith("arn:")) {
+ for (String k : store.keys()) {
+ CloudTrailEntry entry = store.get(k).orElse(null);
+ if (entry == null) continue;
+ if (nameOrArn.equals(entry.trail().trailArn())) {
+ return entry.trail();
+ }
+ }
return null;
}
- int slash = nameOrArn.lastIndexOf('/');
- return slash >= 0 ? nameOrArn.substring(slash + 1) : nameOrArn;
+ // Name → region-scoped only (AWS resolves a name only in the current Region)
+ return store.get(regionKey(region, nameOrArn))
+ .map(CloudTrailEntry::trail)
+ .orElse(null);
+ }
+
+ private Trail findTrailOrThrow(String region, String nameOrArn) {
+ Trail t = findTrail(region, nameOrArn);
+ if (t == null) {
+ throw new AwsException("TrailNotFoundException",
+ "Unknown trail: " + nameOrArn, 400);
+ }
+ return t;
+ }
+
+ private static void validateTrailName(String name) {
+ if (name == null || name.isEmpty()) {
+ throw new AwsException("InvalidTrailNameException", "Trail name is required.", 400);
+ }
+ if (name.length() < 3) {
+ throw new AwsException("InvalidTrailNameException",
+ "Trail name too short. Minimum allowed length: 3 characters.", 400);
+ }
+ if (name.length() > 128) {
+ throw new AwsException("InvalidTrailNameException",
+ "Trail name too long. Maximum allowed length: 128 characters.", 400);
+ }
+ if (!Character.isLetterOrDigit(name.charAt(0))) {
+ throw new AwsException("InvalidTrailNameException",
+ "Trail name must starts with a letter or number.", 400);
+ }
+ if (!Character.isLetterOrDigit(name.charAt(name.length() - 1))) {
+ throw new AwsException("InvalidTrailNameException",
+ "Trail name must end with a letter or number.", 400);
+ }
+ for (char c : name.toCharArray()) {
+ if (!Character.isLetterOrDigit(c) && c != '.' && c != '_' && c != '-') {
+ throw new AwsException("InvalidTrailNameException",
+ "Trail name must only contain letters, numbers, periods, underscores, and hyphens.", 400);
+ }
+ }
+ }
+
+ private static String regionKey(String region, String name) {
+ return region + ":" + name;
+ }
+
+ private static String regionFromKey(String key) {
+ int colon = key.indexOf(':');
+ return colon < 0 ? key : key.substring(0, colon);
+ }
+
+ public record TrailStatus(boolean logging, Long startLoggingTime, Long stopLoggingTime) {}
+
+ private record MatchedTrail(Trail trail, String region) {}
+
+ /** Input describing a single S3 op for emission. Use the builder for clarity. */
+ public record S3EventInput(
+ String region,
+ String eventName,
+ String bucketName,
+ String key,
+ String accessKeyId,
+ String sourceIp,
+ String userAgent,
+ long bytesIn,
+ long bytesOut,
+ String errorCode,
+ String errorMessage,
+ long eventTimeMillis) {
+
+ public static Builder builder() { return new Builder(); }
+
+ public static final class Builder {
+ private String region;
+ private String eventName;
+ private String bucketName;
+ private String key;
+ private String accessKeyId;
+ private String sourceIp;
+ private String userAgent;
+ private long bytesIn;
+ private long bytesOut;
+ private String errorCode;
+ private String errorMessage;
+ private long eventTimeMillis;
+
+ public Builder region(String v) { this.region = v; return this; }
+ public Builder eventName(String v) { this.eventName = v; return this; }
+ public Builder bucketName(String v) { this.bucketName = v; return this; }
+ public Builder key(String v) { this.key = v; return this; }
+ public Builder accessKeyId(String v) { this.accessKeyId = v; return this; }
+ public Builder sourceIp(String v) { this.sourceIp = v; return this; }
+ public Builder userAgent(String v) { this.userAgent = v; return this; }
+ public Builder bytesIn(long v) { this.bytesIn = v; return this; }
+ public Builder bytesOut(long v) { this.bytesOut = v; return this; }
+ public Builder errorCode(String v) { this.errorCode = v; return this; }
+ public Builder errorMessage(String v) { this.errorMessage = v; return this; }
+ public Builder eventTimeMillis(long v) { this.eventTimeMillis = v; return this; }
+
+ public S3EventInput build() {
+ return new S3EventInput(region, eventName, bucketName, key, accessKeyId,
+ sourceIp, userAgent, bytesIn, bytesOut,
+ errorCode, errorMessage, eventTimeMillis);
+ }
+ }
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/DataResource.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/DataResource.java
new file mode 100644
index 0000000000..cf332c4755
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/DataResource.java
@@ -0,0 +1,16 @@
+package io.github.hectorvent.floci.services.cloudtrail.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.util.List;
+
+@RegisterForReflection
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@JsonIgnoreProperties(ignoreUnknown = true)
+public record DataResource(
+ @JsonProperty("Type") String type,
+ @JsonProperty("Values") List<String> values) {
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/EventSelector.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/EventSelector.java
new file mode 100644
index 0000000000..7cbcdb5ac6
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/EventSelector.java
@@ -0,0 +1,18 @@
+package io.github.hectorvent.floci.services.cloudtrail.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.util.List;
+
+@RegisterForReflection
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@JsonIgnoreProperties(ignoreUnknown = true)
+public record EventSelector(
+ @JsonProperty("ReadWriteType") String readWriteType,
+ @JsonProperty("IncludeManagementEvents") Boolean includeManagementEvents,
+ @JsonProperty("DataResources") List<DataResource> dataResources,
+ @JsonProperty("ExcludeManagementEventSources") List<String> excludeManagementEventSources) {
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/Trail.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/Trail.java
new file mode 100644
index 0000000000..c63c2915c5
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/model/Trail.java
@@ -0,0 +1,24 @@
+package io.github.hectorvent.floci.services.cloudtrail.model;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@JsonIgnoreProperties(ignoreUnknown = true)
+public record Trail(
+ @JsonProperty("Name") String name,
+ @JsonProperty("TrailARN") String trailArn,
+ @JsonProperty("S3BucketName") String s3BucketName,
+ @JsonProperty("S3KeyPrefix") String s3KeyPrefix,
+ @JsonProperty("SnsTopicARN") String snsTopicArn,
+ @JsonProperty("IncludeGlobalServiceEvents") boolean includeGlobalServiceEvents,
+ @JsonProperty("IsMultiRegionTrail") boolean isMultiRegionTrail,
+ @JsonProperty("HomeRegion") String homeRegion,
+ @JsonProperty("LogFileValidationEnabled") boolean logFileValidationEnabled,
+ @JsonProperty("HasCustomEventSelectors") boolean hasCustomEventSelectors,
+ @JsonProperty("HasInsightSelectors") boolean hasInsightSelectors,
+ @JsonProperty("IsOrganizationTrail") boolean isOrganizationTrail) {
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java b/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java
index 1492e252eb..2a5bb9fa51 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java
@@ -62,6 +62,10 @@ final class AwsManagedPolicies {
"Allows write permissions to the AWS X-Ray daemon."),
new ManagedPolicyDef("AmazonElasticFileSystemClientFullAccess", "/",
"Provides root client access to an Amazon EFS file system."),
+ new ManagedPolicyDef("AWSCloudTrail_FullAccess", "/",
+ "Provides full access to AWS CloudTrail."),
+ new ManagedPolicyDef("AWSCloudTrail_ReadOnlyAccess", "/",
+ "Provides read-only access to AWS CloudTrail."),
// Lambda execution role policies
new ManagedPolicyDef("AWSLambdaBasicExecutionRole", "/service-role/",
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
index ad0f1f45b5..c002f7ca89 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
@@ -134,6 +134,18 @@ public class IamActionRegistry {
String path = ctx.getUriInfo().getPath();
if (!path.startsWith("/")) path = "/" + path;
+ // S3 sub-resource override: the URL path alone doesn't distinguish
+ // s3:GetObjectAcl / s3:PutObjectAcl from s3:GetObject / s3:PutObject
+ // — only the {@code ?acl} query parameter does. Without this hook,
+ // an actor with s3:GetObject permission could read ACLs that their
+ // policy intends to forbid.
+ if ("s3".equals(credentialScope)) {
+ String s3SubAction = resolveS3SubResourceAction(method, ctx);
+ if (s3SubAction != null) {
+ return s3SubAction;
+ }
+ }
+
for (ActionRule rule : RULES) {
if (rule.service().equals(credentialScope)
&& rule.method().equals(method)
@@ -146,6 +158,57 @@ public class IamActionRegistry {
return null;
}
+ /**
+ * Resolves S3 sub-resource ops (ACL, tagging, retention, etc.) that
+ * cannot be distinguished from the parent op by HTTP method + path alone.
+ * Returns null when no sub-resource is present so the caller falls back
+ * to the standard rule table.
+ */
+ private static String resolveS3SubResourceAction(String method, ContainerRequestContext ctx) {
+ var params = ctx.getUriInfo().getQueryParameters();
+ boolean acl = params.containsKey("acl");
+ boolean tagging = params.containsKey("tagging");
+ if (!acl && !tagging) {
+ return null;
+ }
+ // /{bucket}?acl → bucket-level; /{bucket}/{key}?acl → object-level
+ String path = ctx.getUriInfo().getPath();
+ // Strip leading slash, then check whether there is a key segment after the bucket.
+ // A trailing slash is a valid key character, so /bucket/folder/?acl is an object
+ // request — we cannot use endsWith("/") to infer bucket-level.
+ String stripped = path.startsWith("/") ? path.substring(1) : path;
+ int firstSlash = stripped.indexOf('/');
+ boolean isBucketLevel = firstSlash < 0 || firstSlash == stripped.length() - 1;
+ if (acl) {
+ if (isBucketLevel) {
+ return switch (method) {
+ case "GET" -> "s3:GetBucketAcl";
+ case "PUT" -> "s3:PutBucketAcl";
+ default -> null;
+ };
+ }
+ return switch (method) {
+ case "GET" -> "s3:GetObjectAcl";
+ case "PUT" -> "s3:PutObjectAcl";
+ default -> null;
+ };
+ }
+ if (isBucketLevel) {
+ return switch (method) {
+ case "GET" -> "s3:GetBucketTagging";
+ case "PUT" -> "s3:PutBucketTagging";
+ case "DELETE" -> "s3:DeleteBucketTagging";
+ default -> null;
+ };
+ }
+ return switch (method) {
+ case "GET" -> "s3:GetObjectTagging";
+ case "PUT" -> "s3:PutObjectTagging";
+ case "DELETE" -> "s3:DeleteObjectTagging";
+ default -> null;
+ };
+ }
+
/**
* Reads {@code Action} from a {@code application/x-www-form-urlencoded}
* request body and restores the entity stream so downstream consumers
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java
index 345fae5347..0b51d59f07 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java
@@ -965,6 +965,14 @@ public class IamService implements SessionAccountLookup {
return findSessionAnyAccount(accessKeyId).map(SessionCredential::getSecretAccessKey);
}
+ public Optional<AccessKey> findAccessKey(String accessKeyId) {
+ return accessKeys.get(accessKeyId);
+ }
+
+ public Optional<IamUser> findUser(String userName) {
+ return users.get(userName);
+ }
+
// =========================================================================
// IAM Enforcement — session tracking and policy collection
// =========================================================================
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
index 33e191038e..5926a83c88 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
@@ -2,12 +2,15 @@ package io.github.hectorvent.floci.services.s3;
import static io.github.hectorvent.floci.services.s3.S3RequestParser.hasQueryParam;
+import io.github.hectorvent.floci.core.common.AccountResolver;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.common.RequestContext;
+import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
import io.github.hectorvent.floci.services.sns.SnsQueryHandler;
import io.github.hectorvent.floci.services.s3.model.Bucket;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesParts;
@@ -93,19 +96,76 @@ public class S3Controller {
private final SnsQueryHandler snsQueryHandler;
private final io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest;
private final io.github.hectorvent.floci.services.floci.ui.UiPages uiPages;
+ private final CloudTrailService cloudTrailService;
+ private final AccountResolver accountResolver;
+ private final RequestContext requestContext;
@Inject
public S3Controller(S3Service s3Service, S3SelectService s3SelectService,
RegionResolver regionResolver,
SnsQueryHandler snsQueryHandler,
io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest,
- io.github.hectorvent.floci.services.floci.ui.UiPages uiPages) {
+ io.github.hectorvent.floci.services.floci.ui.UiPages uiPages,
+ CloudTrailService cloudTrailService,
+ AccountResolver accountResolver,
+ RequestContext requestContext) {
this.s3Service = s3Service;
this.s3SelectService = s3SelectService;
this.regionResolver = regionResolver;
this.snsQueryHandler = snsQueryHandler;
this.currentVertxRequest = currentVertxRequest;
this.uiPages = uiPages;
+ this.cloudTrailService = cloudTrailService;
+ this.accountResolver = accountResolver;
+ this.requestContext = requestContext;
+ }
+
+ private void emitCloudTrailEvent(String eventName, String bucket, String key,
+ long bytesIn, long bytesOut,
+ String errorCode, String errorMessage) {
+ try {
+ String authHeader = null;
+ String userAgent = null;
+ String sourceIp = null;
+ try {
+ var ctx = currentVertxRequest.getCurrent();
+ if (ctx != null) {
+ var req = ctx.request();
+ if (req != null) {
+ authHeader = req.getHeader("Authorization");
+ userAgent = req.getHeader("User-Agent");
+ String fwd = req.getHeader("X-Forwarded-For");
+ if (fwd != null && !fwd.isBlank()) {
+ int comma = fwd.indexOf(',');
+ sourceIp = (comma > 0 ? fwd.substring(0, comma) : fwd).trim();
+ } else if (req.remoteAddress() != null) {
+ sourceIp = req.remoteAddress().host();
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.tracev(e, "CloudTrail: could not extract request context for S3 event {0}/{1}", bucket, key);
+ }
+ String akid = accountResolver.extractAccessKeyId(authHeader);
+ String region = requestContext.getRegion() != null
+ ? requestContext.getRegion() : regionResolver.getDefaultRegion();
+ cloudTrailService.emitS3DataEvent(CloudTrailService.S3EventInput.builder()
+ .region(region)
+ .eventName(eventName)
+ .bucketName(bucket)
+ .key(key)
+ .accessKeyId(akid)
+ .sourceIp(sourceIp)
+ .userAgent(userAgent)
+ .bytesIn(bytesIn)
+ .bytesOut(bytesOut)
+ .errorCode(errorCode)
+ .errorMessage(errorMessage)
+ .eventTimeMillis(System.currentTimeMillis())
+ .build());
+ } catch (Exception e) {
+ LOG.tracev(e, "CloudTrail event emission failed for {0} {1}/{2}", eventName, bucket, key);
+ }
}
// --- Bucket operations ---
@@ -516,8 +576,11 @@ public class S3Controller {
}
}
xml.end("ListBucketResult");
- return Response.ok(xml.build()).build();
+ String body = xml.build();
+ emitCloudTrailEvent("ListObjects", bucket, null, 0L, body.length(), null, null);
+ return Response.ok(body).build();
} catch (AwsException e) {
+ emitCloudTrailEvent("ListObjects", bucket, null, 0L, 0L, e.getErrorCode(), e.getMessage());
return xmlErrorResponse(e);
}
}
@@ -634,8 +697,10 @@ public class S3Controller {
resp.header("x-amz-version-id", obj.getVersionId());
}
appendPutObjectResponseHeaders(resp, obj);
+ emitCloudTrailEvent("PutObject", bucket, key, data == null ? 0 : data.length, 0L, null, null);
return resp.build();
} catch (AwsException e) {
+ emitCloudTrailEvent("PutObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
return xmlErrorResponse(e);
}
}
@@ -690,7 +755,15 @@ public class S3Controller {
}
if (hasQueryParam(uriInfo, "acl")) {
s3Service.authorizeObjectRead(bucket, key, versionId, "s3:GetObjectAcl", authorization);
- return Response.ok(s3Service.getObjectAcl(bucket, key, versionId)).build();
+ try {
+ String aclXml = s3Service.getObjectAcl(bucket, key, versionId);
+ emitCloudTrailEvent("GetObjectAcl", bucket, key, 0L,
+ aclXml == null ? 0L : aclXml.length(), null, null);
+ return Response.ok(aclXml).build();
+ } catch (AwsException e) {
+ emitCloudTrailEvent("GetObjectAcl", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
+ return xmlErrorResponse(e);
+ }
}
if (hasQueryParam(uriInfo, "attributes")) {
s3Service.authorizeObjectRead(bucket, key, versionId, "s3:GetObjectAttributes", authorization);
@@ -728,8 +801,10 @@ public class S3Controller {
return handleRangeRequest(bucket, key, versionId, obj, rangeHeader, overrides, includeChecksum);
}
+ emitCloudTrailEvent("GetObject", bucket, key, 0L, obj.getSize(), null, null);
return fullObjectResponse(bucket, key, versionId, obj, overrides, includeChecksum);
} catch (AwsException e) {
+ emitCloudTrailEvent("GetObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
if (isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders)) {
try {
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
@@ -913,8 +988,10 @@ public class S3Controller {
}
boolean includeChecksum = "ENABLED".equalsIgnoreCase(checksumMode);
appendObjectHeaders(resp, obj, overrides, includeChecksum);
+ emitCloudTrailEvent("HeadObject", bucket, key, 0L, obj.getSize(), null, null);
return resp.build();
} catch (AwsException e) {
+ emitCloudTrailEvent("HeadObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
return xmlErrorResponse(e);
}
}
@@ -1020,8 +1097,10 @@ public class S3Controller {
}
resp.header("x-amz-version-id", result.getVersionId());
}
+ emitCloudTrailEvent("DeleteObject", bucket, key, 0L, 0L, null, null);
return resp.build();
} catch (AwsException e) {
+ emitCloudTrailEvent("DeleteObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
return xmlErrorResponse(e);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java
index 7f8433d766..d245ae6486 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
import io.github.hectorvent.floci.services.iam.model.CallerContext;
import jakarta.ws.rs.core.MediaType;
@@ -98,7 +99,8 @@ class IamEnforcementFilterTest {
IamEnforcementFilter filter = new IamEnforcementFilter(
config, accountResolver, iamService, evaluator, actionRegistry, arnBuilder,
- requestContext, conditionContextResolver);
+ requestContext, conditionContextResolver,
+ mock(CloudTrailService.class), mock(io.quarkus.vertx.http.runtime.CurrentVertxRequest.class));
filter.filter(containerRequest);
@@ -132,7 +134,8 @@ class IamEnforcementFilterTest {
IamEnforcementFilter filter = new IamEnforcementFilter(
config, accountResolver, iamService, evaluator, actionRegistry, arnBuilder,
- requestContext, conditionContextResolver);
+ requestContext, conditionContextResolver,
+ mock(CloudTrailService.class), mock(io.quarkus.vertx.http.runtime.CurrentVertxRequest.class));
filter.filter(containerRequest);
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java
new file mode 100644
index 0000000000..160c39b77b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java
@@ -0,0 +1,128 @@
+package io.github.hectorvent.floci.core.common;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Unit tests for the static helpers that drive CloudTrail emission on the
+ * IAM-deny path. Full HTTP-level coverage (filter → emit → log file) lives in
+ * the SDK compatibility suite where SigV4 signing is available.
+ */
+class IamEnforcementFilterUnitTest {
+
+ // --- mapS3ActionToEventName ---
+
+ @Test
+ void getObject_overGet_emitsAsGetObject() {
+ assertEquals("GetObject",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "GET"));
+ }
+
+ @Test
+ void getObject_overHead_emitsAsHeadObject() {
+ assertEquals("HeadObject",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "HEAD"));
+ }
+
+ @Test
+ void putObject_emitsAsPutObject() {
+ assertEquals("PutObject",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "PUT"));
+ }
+
+ @Test
+ void deleteObject_emitsAsDeleteObject() {
+ assertEquals("DeleteObject",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObject", "DELETE"));
+ }
+
+ @Test
+ void listBucket_emitsAsListObjects() {
+ assertEquals("ListObjects",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:ListBucket", "GET"));
+ }
+
+ @Test
+ void listAllMyBuckets_emitsAsListBuckets() {
+ assertEquals("ListBuckets",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:ListAllMyBuckets", "GET"));
+ }
+
+ @Test
+ void getObjectAcl_emitsAsGetObjectAcl() {
+ assertEquals("GetObjectAcl",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAcl", "GET"));
+ }
+
+ @Test
+ void putObjectAcl_emitsAsPutObjectAcl() {
+ assertEquals("PutObjectAcl",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectAcl", "PUT"));
+ }
+
+ @Test
+ void objectTagging_emitsCorrectEventNames() {
+ assertEquals("GetObjectTagging",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectTagging", "GET"));
+ assertEquals("PutObjectTagging",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectTagging", "PUT"));
+ assertEquals("DeleteObjectTagging",
+ IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectTagging", "DELETE"));
+ }
+
+ @Test
+ void unknownAction_returnsNull() {
+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetBucketAcl", "GET"));
+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("dynamodb:Query", "POST"));
+ assertNull(IamEnforcementFilter.mapS3ActionToEventName(null, "GET"));
+ }
+
+ // --- parseS3Resource ---
+
+ @Test
+ void parseObjectArn_splitsBucketAndKey() {
+ assertArrayEquals(new String[] { "audit-source", "documents/hello.txt" },
+ IamEnforcementFilter.parseS3Resource("arn:aws:s3:::audit-source/documents/hello.txt"));
+ }
+
+ @Test
+ void parseBucketArn_returnsBucketWithNullKey() {
+ assertArrayEquals(new String[] { "audit-source", null },
+ IamEnforcementFilter.parseS3Resource("arn:aws:s3:::audit-source"));
+ }
+
+ @Test
+ void parseBucketArnWithTrailingSlash_returnsBucketWithNullKey() {
+ assertArrayEquals(new String[] { "audit-source", null },
+ IamEnforcementFilter.parseS3Resource("arn:aws:s3:::audit-source/"));
+ }
+
+ @Test
+ void parseKeyWithSlashes_keepsFullPath() {
+ assertArrayEquals(new String[] { "b", "a/b/c.txt" },
+ IamEnforcementFilter.parseS3Resource("arn:aws:s3:::b/a/b/c.txt"));
+ }
+
+ @Test
+ void parseStarArn_returnsNulls() {
+ assertArrayEquals(new String[] { null, null },
+ IamEnforcementFilter.parseS3Resource("arn:aws:s3:::*"));
+ }
+
+ @Test
+ void parseEmptyArn_returnsNulls() {
+ assertArrayEquals(new String[] { null, null },
+ IamEnforcementFilter.parseS3Resource("arn:aws:s3:::"));
+ }
+
+ @Test
+ void parseNonS3Arn_returnsNulls() {
+ assertArrayEquals(new String[] { null, null },
+ IamEnforcementFilter.parseS3Resource("arn:aws:lambda:us-east-1:000000000000:function:foo"));
+ assertArrayEquals(new String[] { null, null },
+ IamEnforcementFilter.parseS3Resource(null));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailIntegrationTest.java
index 10c5396e92..d9d5b2709f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailIntegrationTest.java
@@ -1,21 +1,38 @@
package io.github.hectorvent.floci.services.cloudtrail;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
+import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import java.io.ByteArrayInputStream;
+import java.util.UUID;
+import java.util.zip.GZIPInputStream;
+
import static io.restassured.RestAssured.given;
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.startsWith;
-
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * End-to-end smoke test for the CloudTrail control plane + S3 hook + log
+ * writer pipeline. Sets up a trail, exercises the five hooked S3 ops, forces
+ * a flush, and verifies that a gzipped CloudTrail log file lands in the
+ * destination bucket with the right record shape.
+ */
@QuarkusTest
class CloudTrailIntegrationTest {
- private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
- private static final String TARGET_PREFIX = "com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101.";
+
+ private static final String CT_TARGET = "CloudTrail_20131101.";
+ private static final String JSON11 = "application/x-amz-json-1.1";
+
+ @Inject
+ CloudTrailLogWriter writer;
@BeforeAll
static void configureRestAssured() {
@@ -23,414 +40,304 @@ class CloudTrailIntegrationTest {
}
@Test
- void trailLifecycleRoundTripsThroughJsonHandler() {
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "DescribeTrails")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "trailNameList": ["sample-audit"]
- }
- """)
- .when()
- .post("/")
- .then()
+ void s3DataEventsLandAsGzippedLogFilesInDestinationBucket() throws Exception {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String sourceBucket = "audit-source-" + suffix;
+ String destBucket = "audit-logs-" + suffix;
+ String trailName = "audit-trail-" + suffix;
+
+ // 1. Buckets
+ createBucket(sourceBucket);
+ createBucket(destBucket);
+
+ // 2. CreateTrail
+ invokeCloudTrail("CreateTrail", String.format("""
+ {
+ "Name": "%s",
+ "S3BucketName": "%s",
+ "IsMultiRegionTrail": true,
+ "IncludeGlobalServiceEvents": true,
+ "EnableLogFileValidation": true
+ }
+ """, trailName, destBucket))
+ .then()
.statusCode(200)
- .body("trailList", hasSize(0));
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "CreateTrail")
- .contentType(CONTENT_TYPE)
- .body("""
+ .body(containsString("\"TrailARN\""));
+
+ // 3. PutEventSelectors — match all S3 objects in the source bucket
+ invokeCloudTrail("PutEventSelectors", String.format("""
+ {
+ "TrailName": "%s",
+ "EventSelectors": [
+ {
+ "ReadWriteType": "All",
+ "IncludeManagementEvents": false,
+ "DataResources": [
{
- "Name": "sample-audit",
- "S3BucketName": "sample-audit-bucket",
- "IncludeGlobalServiceEvents": true,
- "IsMultiRegionTrail": true,
- "IsOrganizationTrail": false,
- "TagsList": [
- {"Key": "example:component", "Value": "audit"}
- ]
+ "Type": "AWS::S3::Object",
+ "Values": ["arn:aws:s3:::%s/"]
}
- """)
- .when()
- .post("/")
- .then()
+ ]
+ }
+ ]
+ }
+ """, trailName, sourceBucket))
+ .then()
.statusCode(200)
- .body("Name", equalTo("sample-audit"))
- .body("TrailARN", startsWith("arn:aws:cloudtrail:"))
- .body("S3BucketName", equalTo("sample-audit-bucket"));
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "PutEventSelectors")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "TrailName": "sample-audit",
- "AdvancedEventSelectors": []
- }
- """)
- .when()
- .post("/")
- .then()
- .statusCode(200);
+ .body(containsString("\"EventSelectors\""));
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "StartLogging")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "sample-audit"
- }
- """)
- .when()
- .post("/")
- .then()
+ // 4. StartLogging
+ invokeCloudTrail("StartLogging",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then()
.statusCode(200);
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "GetTrailStatus")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "sample-audit"
- }
- """)
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .body("IsLogging", equalTo(true))
- .body("LatestDeliveryTime", notNullValue());
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "UpdateTrail")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "sample-audit",
- "S3BucketName": "sample-audit-bucket-2"
- }
- """)
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .body("S3BucketName", equalTo("sample-audit-bucket-2"))
- .body("IncludeGlobalServiceEvents", equalTo(true))
- .body("IsMultiRegionTrail", equalTo(true));
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "DescribeTrails")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "trailNameList": ["sample-audit"]
- }
- """)
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .body("trailList", hasSize(1))
- .body("trailList[0].Name", equalTo("sample-audit"))
- .body("trailList[0].S3BucketName", equalTo("sample-audit-bucket-2"))
- .body("trailList[0].IncludeGlobalServiceEvents", equalTo(true))
- .body("trailList[0].IsMultiRegionTrail", equalTo(true));
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "StopLogging")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "sample-audit"
- }
- """)
- .when()
- .post("/")
- .then()
- .statusCode(200);
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "DeleteTrail")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "sample-audit"
- }
- """)
- .when()
- .post("/")
- .then()
- .statusCode(200);
+ // 5. Drive S3 traffic against the source bucket — covers all five
+ // hooked ops plus a denial path (NoSuchKey on a missing GetObject).
+ putObject(sourceBucket, "documents/hello.txt", "hello-world");
+ getObject(sourceBucket, "documents/hello.txt", 200);
+ headObject(sourceBucket, "documents/hello.txt", 200);
+ listObjects(sourceBucket);
+ getObject(sourceBucket, "documents/missing.txt", 404);
+ deleteObject(sourceBucket, "documents/hello.txt");
+
+ // 6. Force the writer to flush — bypasses the scheduled cadence.
+ writer.flushNow();
+
+ // 7. List the destination bucket and find the log file.
+ String listingXml = given()
+ .when().get("/" + destBucket + "?list-type=2")
+ .then().statusCode(200)
+ .extract().asString();
+
+ // Expect AWS-shaped key: AWSLogs/<account>/CloudTrail/<region>/yyyy/MM/dd/<file>.json.gz
+ assertTrue(listingXml.contains("AWSLogs/"),
+ "Destination bucket missing AWSLogs prefix; got:\n" + listingXml);
+ assertTrue(listingXml.contains("/CloudTrail/"),
+ "Destination bucket missing CloudTrail segment; got:\n" + listingXml);
+ assertTrue(listingXml.contains(".json.gz"),
+ "Destination bucket missing .json.gz file; got:\n" + listingXml);
+
+ String logKey = extractFirstKey(listingXml);
+ assertNotNull(logKey, "Could not parse first <Key> from listing:\n" + listingXml);
+
+ // 8. Download the log file and decompress.
+ byte[] gz = given()
+ .when().get("/" + destBucket + "/" + logKey)
+ .then().statusCode(200)
+ .extract().asByteArray();
+
+ byte[] json;
+ try (GZIPInputStream gzin = new GZIPInputStream(new ByteArrayInputStream(gz))) {
+ json = gzin.readAllBytes();
+ }
+
+ // 9. Verify record shape.
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode envelope = mapper.readTree(json);
+ JsonNode records = envelope.get("Records");
+ assertNotNull(records, "Missing Records[] in log file:\n" + new String(json));
+ assertTrue(records.isArray(), "Records is not an array");
+ assertTrue(records.size() >= 5,
+ "Expected at least 5 records (Put/Get/Head/List/Delete + denial), got " + records.size()
+ + ":\n" + new String(json));
+
+ boolean sawPut = false, sawGet = false, sawDelete = false, sawNoSuchKey = false;
+ for (JsonNode rec : records) {
+ assertEquals("1.11", rec.path("eventVersion").asText(),
+ "Bad eventVersion: " + rec);
+ assertEquals("s3.amazonaws.com", rec.path("eventSource").asText(),
+ "Bad eventSource: " + rec);
+ assertEquals("Data", rec.path("eventCategory").asText(),
+ "Bad eventCategory: " + rec);
+ assertEquals("AwsApiCall", rec.path("eventType").asText(),
+ "Bad eventType: " + rec);
+ assertNotNull(rec.path("eventID").asText(null), "Missing eventID");
+ assertNotNull(rec.path("requestID").asText(null), "Missing requestID");
+ assertEquals("IAMUser", rec.path("userIdentity").path("type").asText(),
+ "Bad userIdentity.type: " + rec);
+
+ String name = rec.path("eventName").asText();
+ if ("PutObject".equals(name)) sawPut = true;
+ if ("GetObject".equals(name) && rec.path("errorCode").isMissingNode()) sawGet = true;
+ if ("DeleteObject".equals(name)) sawDelete = true;
+ if ("NoSuchKey".equals(rec.path("errorCode").asText(null))) sawNoSuchKey = true;
+ }
+ assertTrue(sawPut, "Expected a PutObject record");
+ assertTrue(sawGet, "Expected a successful GetObject record");
+ assertTrue(sawDelete, "Expected a DeleteObject record");
+ assertTrue(sawNoSuchKey, "Expected a GetObject NoSuchKey record");
}
@Test
void lookupEventsReturnsEmptyEventPage() {
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "LookupEvents")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "LookupAttributes": [
- {"AttributeKey": "EventName", "AttributeValue": "CreateBucket"}
- ],
- "MaxResults": 10
- }
- """)
- .when()
- .post("/")
+ invokeCloudTrail("LookupEvents", """
+ {
+ "LookupAttributes": [
+ {"AttributeKey": "EventName", "AttributeValue": "CreateBucket"}
+ ],
+ "MaxResults": 10
+ }
+ """)
.then()
- .statusCode(200)
- .body("Events", hasSize(0));
+ .statusCode(200)
+ .body("Events", hasSize(0));
}
@Test
- void trailProvisioningLifecycleWorksWithBucketNotificationsAndQueuePolicy() {
- String bucket = "cloudtrail-audit-feed-bucket";
- String bucket2 = "cloudtrail-audit-feed-bucket-2";
- String queueName = "cloudtrail-audit-feed-events";
- String queueUrl = createQueue(queueName);
- String queueArn = getQueueArn(queueUrl);
- String bucketArn = "arn:aws:s3:::" + bucket;
- String trailName = "cloudtrail-audit-feed";
-
- createBucket(bucket);
- putBucketPolicy(bucket, trailName);
- putBucketNotification(bucket, queueArn);
- setQueuePolicy(queueUrl, bucketArn, queueArn);
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "CreateTrail")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "%s",
- "S3BucketName": "%s",
- "IncludeGlobalServiceEvents": true,
- "IsMultiRegionTrail": false,
- "IsOrganizationTrail": false,
- "TagsList": [
- {"Key": "example:component", "Value": "cloudAudit"}
- ]
- }
- """.formatted(trailName, bucket))
- .when()
- .post("/")
- .then()
+ void describeTrailsRoundTripsCreatedTrail() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String trailName = "rt-" + suffix;
+ String destBucket = "rt-logs-" + suffix;
+
+ createBucket(destBucket);
+ invokeCloudTrail("CreateTrail", String.format("""
+ {"Name":"%s","S3BucketName":"%s"}
+ """, trailName, destBucket))
+ .then().statusCode(200);
+
+ invokeCloudTrail("DescribeTrails",
+ String.format("{\"trailNameList\":[\"%s\"]}", trailName))
+ .then()
.statusCode(200)
- .body("Name", equalTo(trailName))
- .body("TrailARN", startsWith("arn:aws:cloudtrail:"))
- .body("S3BucketName", equalTo(bucket));
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "PutEventSelectors")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "TrailName": "%s",
- "AdvancedEventSelectors": [
- {
- "Name": "Management events",
- "FieldSelectors": [
- {"Field": "eventCategory", "Equals": ["Management"]}
- ]
- }
- ]
- }
- """.formatted(trailName))
- .when()
- .post("/")
- .then()
- .statusCode(200);
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "StartLogging")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "%s"
- }
- """.formatted(trailName))
- .when()
- .post("/")
- .then()
- .statusCode(200);
-
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "GetTrailStatus")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "%s"
- }
- """.formatted(trailName))
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .body("IsLogging", equalTo(true));
-
- given()
- .when()
- .get("/" + bucket + "?notification")
- .then()
- .statusCode(200)
- .body(containsString("<Queue>" + queueArn + "</Queue>"))
- .body(containsString("<Event>s3:ObjectCreated:*</Event>"));
+ .body(containsString("\"" + trailName + "\""))
+ .body(containsString(destBucket));
+ }
- getQueuePolicy(queueUrl)
- .statusCode(200)
- .body(containsString(bucketArn));
+ @Test
+ void getTrailStatusReflectsStartStopLogging() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String trailName = "ss-" + suffix;
+ String destBucket = "ss-logs-" + suffix;
+
+ createBucket(destBucket);
+ invokeCloudTrail("CreateTrail", String.format("""
+ {"Name":"%s","S3BucketName":"%s"}
+ """, trailName, destBucket))
+ .then().statusCode(200);
+
+ invokeCloudTrail("GetTrailStatus",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then().statusCode(200).body(containsString("\"IsLogging\":false"));
+
+ invokeCloudTrail("StartLogging",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then().statusCode(200);
+
+ invokeCloudTrail("GetTrailStatus",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then().statusCode(200).body(containsString("\"IsLogging\":true"));
+
+ invokeCloudTrail("StopLogging",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then().statusCode(200);
+
+ invokeCloudTrail("GetTrailStatus",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then().statusCode(200).body(containsString("\"IsLogging\":false"));
+ }
- createBucket(bucket2);
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "UpdateTrail")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "%s",
- "S3BucketName": "%s",
- "IncludeGlobalServiceEvents": true,
- "IsMultiRegionTrail": false
- }
- """.formatted(trailName, bucket2))
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .body("S3BucketName", equalTo(bucket2));
+ @Test
+ void deleteTrailRemovesItFromDescribeTrails() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String trailName = "del-" + suffix;
+ String destBucket = "del-logs-" + suffix;
+
+ createBucket(destBucket);
+ invokeCloudTrail("CreateTrail", String.format("""
+ {"Name":"%s","S3BucketName":"%s"}
+ """, trailName, destBucket))
+ .then().statusCode(200);
+
+ invokeCloudTrail("DescribeTrails",
+ String.format("{\"trailNameList\":[\"%s\"]}", trailName))
+ .then().statusCode(200).body(containsString(trailName));
+
+ invokeCloudTrail("DeleteTrail",
+ String.format("{\"Name\":\"%s\"}", trailName))
+ .then().statusCode(200);
+
+ // After deletion, DescribeTrails with the same name yields an empty list
+ String body = invokeCloudTrail("DescribeTrails",
+ String.format("{\"trailNameList\":[\"%s\"]}", trailName))
+ .then().statusCode(200).extract().asString();
+ assertTrue(body.contains("\"trailList\":[]"),
+ "Expected empty trailList after delete, got: " + body);
+ }
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "DescribeTrails")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "trailNameList": ["%s"]
- }
- """.formatted(trailName))
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .body("trailList", hasSize(1))
- .body("trailList[0].S3BucketName", equalTo(bucket2));
+ @Test
+ void updateTrailReplacesS3BucketName() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String trailName = "upd-" + suffix;
+ String oldBucket = "upd-old-" + suffix;
+ String newBucket = "upd-new-" + suffix;
+
+ createBucket(oldBucket);
+ createBucket(newBucket);
+ invokeCloudTrail("CreateTrail", String.format("""
+ {"Name":"%s","S3BucketName":"%s"}
+ """, trailName, oldBucket))
+ .then().statusCode(200);
+
+ invokeCloudTrail("UpdateTrail", String.format("""
+ {"Name":"%s","S3BucketName":"%s","IsMultiRegionTrail":true}
+ """, trailName, newBucket))
+ .then().statusCode(200)
+ .body(containsString("\"S3BucketName\":\"" + newBucket + "\""))
+ .body(containsString("\"IsMultiRegionTrail\":true"));
+
+ invokeCloudTrail("DescribeTrails",
+ String.format("{\"trailNameList\":[\"%s\"]}", trailName))
+ .then().statusCode(200)
+ .body(containsString(newBucket));
+ }
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "StopLogging")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "%s"
- }
- """.formatted(trailName))
- .when()
- .post("/")
- .then()
- .statusCode(200);
+ // --- Helpers ---
- given()
- .header("X-Amz-Target", TARGET_PREFIX + "DeleteTrail")
- .contentType(CONTENT_TYPE)
- .body("""
- {
- "Name": "%s"
- }
- """.formatted(trailName))
- .when()
- .post("/")
- .then()
- .statusCode(200);
+ private static io.restassured.response.Response invokeCloudTrail(String action, String body) {
+ return given()
+ .header("X-Amz-Target", CT_TARGET + action)
+ .contentType(JSON11)
+ .body(body)
+ .when().post("/");
}
- private static void createBucket(String bucket) {
- given()
- .when()
- .put("/" + bucket)
- .then()
- .statusCode(200);
+ private static void createBucket(String name) {
+ given().when().put("/" + name).then().statusCode(200);
}
- private static String createQueue(String queueName) {
- return given()
- .contentType("application/x-www-form-urlencoded")
- .formParam("Action", "CreateQueue")
- .formParam("QueueName", queueName)
- .formParam("Attribute.1.Name", "VisibilityTimeout")
- .formParam("Attribute.1.Value", "30")
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
+ private static void putObject(String bucket, String key, String body) {
+ given().body(body)
+ .when().put("/" + bucket + "/" + key)
+ .then().statusCode(200);
}
- private static String getQueueArn(String queueUrl) {
- return given()
- .contentType("application/x-www-form-urlencoded")
- .formParam("Action", "GetQueueAttributes")
- .formParam("QueueUrl", queueUrl)
- .formParam("AttributeName.1", "QueueArn")
- .when()
- .post("/")
- .then()
- .statusCode(200)
- .extract().xmlPath().getString("GetQueueAttributesResponse.GetQueueAttributesResult.Attribute.Value");
+ private static void getObject(String bucket, String key, int expectedStatus) {
+ given().when().get("/" + bucket + "/" + key)
+ .then().statusCode(expectedStatus);
}
- private static io.restassured.response.ValidatableResponse getQueuePolicy(String queueUrl) {
- return given()
- .contentType("application/x-www-form-urlencoded")
- .formParam("Action", "GetQueueAttributes")
- .formParam("QueueUrl", queueUrl)
- .formParam("AttributeName.1", "Policy")
- .when()
- .post("/")
- .then();
+ private static void headObject(String bucket, String key, int expectedStatus) {
+ given().when().head("/" + bucket + "/" + key)
+ .then().statusCode(expectedStatus);
}
- private static void setQueuePolicy(String queueUrl, String bucketArn, String queueArn) {
- given()
- .contentType("application/x-www-form-urlencoded")
- .formParam("Action", "SetQueueAttributes")
- .formParam("QueueUrl", queueUrl)
- .formParam("Attribute.1.Name", "Policy")
- .formParam("Attribute.1.Value", """
- {"Version":"2012-10-17","Statement":[{"Sid":"AllowS3Notifications","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":"sqs:SendMessage","Resource":"%s","Condition":{"ArnEquals":{"aws:SourceArn":"%s"}}}]}
- """.formatted(queueArn, bucketArn))
- .when()
- .post("/")
- .then()
- .statusCode(200);
+ private static void listObjects(String bucket) {
+ given().when().get("/" + bucket + "?list-type=2")
+ .then().statusCode(200);
}
- private static void putBucketPolicy(String bucket, String trailName) {
- given()
- .contentType("application/json")
- .body("""
- {"Version":"2012-10-17","Statement":[{"Sid":"AllowCloudTrail","Effect":"Allow","Principal":{"Service":"cloudtrail.amazonaws.com"},"Action":"s3:PutObject","Resource":"arn:aws:s3:::%s/AWSLogs/*","Condition":{"StringEquals":{"aws:SourceArn":"arn:aws:cloudtrail:us-east-1:000000000000:trail/%s"}}}]}
- """.formatted(bucket, trailName))
- .when()
- .put("/" + bucket + "?policy")
- .then()
- .statusCode(200);
+ private static void deleteObject(String bucket, String key) {
+ given().when().delete("/" + bucket + "/" + key)
+ .then().statusCode(204);
}
- private static void putBucketNotification(String bucket, String queueArn) {
- given()
- .contentType("application/xml")
- .body("""
- <NotificationConfiguration>
- <QueueConfiguration>
- <Id>cloudtrail-to-sqs</Id>
- <Queue>%s</Queue>
- <Event>s3:ObjectCreated:*</Event>
- </QueueConfiguration>
- </NotificationConfiguration>
- """.formatted(queueArn))
- .when()
- .put("/" + bucket + "?notification")
- .then()
- .statusCode(200);
+ /** Extract the first <Key>...</Key> from an S3 ListObjectsV2 XML response. */
+ private static String extractFirstKey(String xml) {
+ int open = xml.indexOf("<Key>");
+ if (open < 0) return null;
+ int close = xml.indexOf("</Key>", open);
+ if (close < 0) return null;
+ return xml.substring(open + "<Key>".length(), close);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailNameValidationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailNameValidationTest.java
new file mode 100644
index 0000000000..97a5db1092
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailNameValidationTest.java
@@ -0,0 +1,93 @@
+package io.github.hectorvent.floci.services.cloudtrail;
+
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+
+@QuarkusTest
+class CloudTrailNameValidationTest {
+
+ private static final String CT_TARGET = "CloudTrail_20131101.";
+ private static final String JSON11 = "application/x-amz-json-1.1";
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void tooShortName_rejectsWithInvalidTrailNameException() {
+ given()
+ .header("X-Amz-Target", CT_TARGET + "CreateTrail")
+ .contentType(JSON11)
+ .body("{\"Name\":\"ab\",\"S3BucketName\":\"some-bucket\"}")
+ .when().post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidTrailNameException"));
+ }
+
+ @Test
+ void singleCharName_rejectsWithInvalidTrailNameException() {
+ given()
+ .header("X-Amz-Target", CT_TARGET + "CreateTrail")
+ .contentType(JSON11)
+ .body("{\"Name\":\"a\",\"S3BucketName\":\"some-bucket\"}")
+ .when().post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidTrailNameException"));
+ }
+
+ @Test
+ void nameStartingWithHyphen_rejectsWithInvalidTrailNameException() {
+ given()
+ .header("X-Amz-Target", CT_TARGET + "CreateTrail")
+ .contentType(JSON11)
+ .body("{\"Name\":\"-trail\",\"S3BucketName\":\"some-bucket\"}")
+ .when().post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidTrailNameException"));
+ }
+
+ @Test
+ void nameEndingWithHyphen_rejectsWithInvalidTrailNameException() {
+ given()
+ .header("X-Amz-Target", CT_TARGET + "CreateTrail")
+ .contentType(JSON11)
+ .body("{\"Name\":\"trail-\",\"S3BucketName\":\"some-bucket\"}")
+ .when().post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidTrailNameException"));
+ }
+
+ @Test
+ void nameWithInvalidChar_rejectsWithInvalidTrailNameException() {
+ given()
+ .header("X-Amz-Target", CT_TARGET + "CreateTrail")
+ .contentType(JSON11)
+ .body("{\"Name\":\"my trail!\",\"S3BucketName\":\"some-bucket\"}")
+ .when().post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidTrailNameException"));
+ }
+
+ @Test
+ void validName_isAccepted() {
+ given()
+ .header("X-Amz-Target", CT_TARGET + "CreateTrail")
+ .contentType(JSON11)
+ .body("{\"Name\":\"my-trail_1.0\",\"S3BucketName\":\"some-bucket\"}")
+ .when().post("/")
+ .then()
+ .statusCode(200);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.java
new file mode 100644
index 0000000000..b3e2750657
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.java
@@ -0,0 +1,160 @@
+package io.github.hectorvent.floci.services.cloudtrail;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Unit tests for {@link CloudTrailService}'s S3 data-resource matcher and
+ * read/write classifier — the two predicates that gate every emit decision.
+ * Full pipeline coverage lives in {@code CloudTrailIntegrationTest}.
+ */
+class CloudTrailSelectorMatchingTest {
+
+ // --- matchesS3DataResourceArn ---
+
+ @Test
+ void allBucketsArn_matchesEverything() {
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::", "any-bucket", "any/key.txt"));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::", "any-bucket", null));
+ }
+
+ @Test
+ void allBucketsArnWithSlash_matchesEverything() {
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::/", "any-bucket", "any/key.txt"));
+ }
+
+ @Test
+ void bucketArnNoSlash_matchesAnyOpOnThatBucket() {
+ // Bucket-only ARNs are permissive: the selector ARN doesn't carry the
+ // request's action, so we can't distinguish bucket-level ops from
+ // object-level ones here. We match on bucket name and let event
+ // selectors carry the read/write filter via ReadWriteType. Most real
+ // workflows use `arn:aws:s3:::bucket/` (with slash) to capture every
+ // object event explicitly.
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::audit-source", "audit-source", null));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::audit-source", "audit-source", "k.txt"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::audit-source", "other-bucket", null));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::audit-source", "other-bucket", "k.txt"));
+ }
+
+ @Test
+ void bucketArnWithTrailingSlash_matchesAnyKey() {
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/", "audit-source", "documents/a.txt"));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/", "audit-source", "k.txt"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/", "other-bucket", "k.txt"));
+ }
+
+ @Test
+ void prefixArn_matchesKeysUnderPrefix() {
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/", "audit-source", "documents/a.txt"));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/", "audit-source", "documents/nested/a.txt"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/", "audit-source", "exports/a.txt"));
+ }
+
+ @Test
+ void prefixArnNoTrailingSlash_isPrefixMatchOnKey() {
+ // arn:aws:s3:::bucket/prefix matches keys that start with "prefix"
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents", "audit-source", "documents/a.txt"));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents", "audit-source", "documents-v2/a.txt"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents", "audit-source", "exports/a.txt"));
+ }
+
+ @Test
+ void prefixArn_doesNotMatchOtherBucket() {
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/", "other-bucket", "documents/a.txt"));
+ }
+
+ @Test
+ void exactKeyArn_matchesOnlyThatKey() {
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/a.txt", "audit-source", "documents/a.txt"));
+ // The current implementation uses startsWith, so "documents/a.txt2" also
+ // matches — that's deliberate (mirrors AWS's prefix semantics for object ARNs)
+ assertTrue(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/a.txt", "audit-source", "documents/a.txt2"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/a.txt", "audit-source", "documents/b.txt"));
+ }
+
+ @Test
+ void bareS3Arn_matchesEverything() {
+ // "arn:aws:s3" without ":::" is the AWS Console / CLI "all objects" shorthand
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3", "any-bucket", "any/key.txt"));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3", "any-bucket", null));
+ }
+
+ @Test
+ void wildcardBucketArn_matchesAnyBucket() {
+ // arn:aws:s3:::* matches all buckets (moto-style)
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::*", "any-bucket", null));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::*", "another-bucket", "k.txt"));
+ }
+
+ @Test
+ void wildcardObjectArn_matchesAnyNonNullKey() {
+ // arn:aws:s3:::*/* matches all objects across all buckets (value moto uses in its tests)
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::*/*", "any-bucket", "k.txt"));
+ assertTrue(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::*/*", "another-bucket", "a/b/c.json"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn("arn:aws:s3:::*/*", "any-bucket", null));
+ }
+
+ @Test
+ void nonS3Arn_neverMatches() {
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:lambda:us-east-1:000:function:foo", "audit-source", "k.txt"));
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(null, "audit-source", "k.txt"));
+ }
+
+ @Test
+ void prefixArn_nullKey_doesNotMatchKeyOps() {
+ // A key-level selector cannot match a bucket-level op (which has key=null)
+ assertFalse(CloudTrailService.matchesS3DataResourceArn(
+ "arn:aws:s3:::audit-source/documents/", "audit-source", null));
+ }
+
+ // --- isReadOnlyEvent ---
+
+ @Test
+ void readOps_classifiedReadOnly() {
+ assertTrue(CloudTrailService.isReadOnlyEvent("GetObject"));
+ assertTrue(CloudTrailService.isReadOnlyEvent("HeadObject"));
+ assertTrue(CloudTrailService.isReadOnlyEvent("ListObjects"));
+ assertTrue(CloudTrailService.isReadOnlyEvent("ListObjectsV2"));
+ assertTrue(CloudTrailService.isReadOnlyEvent("GetObjectAcl"));
+ assertTrue(CloudTrailService.isReadOnlyEvent("GetObjectTagging"));
+ assertTrue(CloudTrailService.isReadOnlyEvent("ListMultipartUploads"));
+ }
+
+ @Test
+ void writeOps_classifiedNotReadOnly() {
+ assertFalse(CloudTrailService.isReadOnlyEvent("PutObject"));
+ assertFalse(CloudTrailService.isReadOnlyEvent("DeleteObject"));
+ assertFalse(CloudTrailService.isReadOnlyEvent("PutObjectAcl"));
+ }
+
+ @Test
+ void unknownEvent_defaultsToWrite() {
+ // Conservative default: unknown ops are treated as writes so a
+ // ReadOnly selector won't accidentally swallow them.
+ assertFalse(CloudTrailService.isReadOnlyEvent("SomeFutureOperation"));
+ }
+
+ @Test
+ void nullEvent_classifiedReadOnly() {
+ // Defensive — buildS3Record uses this for the "readOnly" field, which
+ // should default to safe-no-write semantics if the eventName is missing.
+ assertTrue(CloudTrailService.isReadOnlyEvent(null));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
index aa15dfc446..11086cd0a8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
@@ -121,6 +121,32 @@ class IamActionRegistryTest {
assertNull(registry.resolve("kms", ctx));
}
+ @Test
+ void s3AclOnTrailingSlashKeyIsObjectLevel() {
+ // /bucket/folder/?acl — trailing slash is a valid key character, so this
+ // must be s3:GetObjectAcl, not s3:GetBucketAcl.
+ MultivaluedMap<String, String> acl = new MultivaluedHashMap<>();
+ acl.add("acl", "");
+ ContainerRequestContext ctx = mockCtx("GET", "/bucket/folder/", acl, null, "");
+ assertEquals("s3:GetObjectAcl", registry.resolve("s3", ctx));
+ }
+
+ @Test
+ void s3TaggingOnTrailingSlashKeyIsObjectLevel() {
+ MultivaluedMap<String, String> tagging = new MultivaluedHashMap<>();
+ tagging.add("tagging", "");
+ ContainerRequestContext ctx = mockCtx("GET", "/bucket/folder/", tagging, null, "");
+ assertEquals("s3:GetObjectTagging", registry.resolve("s3", ctx));
+ }
+
+ @Test
+ void s3AclOnBucketRootIsStillBucketLevel() {
+ MultivaluedMap<String, String> acl = new MultivaluedHashMap<>();
+ acl.add("acl", "");
+ ContainerRequestContext ctx = mockCtx("GET", "/bucket/", acl, null, "");
+ assertEquals("s3:GetBucketAcl", registry.resolve("s3", ctx));
+ }
+
// -------------------------------------------------------------------------
private static ContainerRequestContext mockCtx(String method, String path,