CloudTrail audit records
Floci emulates CloudTrail trail control, event selection, audit delivery, and event history. Service activity becomes CloudTrail-shaped data when an active trail selects it. Management events also enter a short-lived event-history store for LookupEvents, while selected records are buffered and written to the trail's S3 destination. The AWS-compatible control API exposes trail state and both audit views.
Audit system model
CloudTrail connects service activity to an audit destination through a trail. A trail defines what to record, where to deliver it, and whether recording is active. Management activity has a second path through event history, so clients can look up recent management events even when no trail selects them.
The following flow shows how activity reaches event history and trail delivery.
flowchart LR
Activity[Service activity] --> Record[CloudTrail event record]
Record --> History[Management event history]
Record --> Select[Active trail and selector match]
Select --> Buffer[Per-trail pending records]
Buffer --> Writer[Scheduled log delivery]
Writer --> Object[Gzipped JSON object in S3]
API[AWS-compatible CloudTrail API] --> TrailState[Trail configuration and status]
API --> HistoryView[LookupEvents view]
History --> HistoryView
TrailState --> Select
Trail lifecycle
A trail is the durable audit configuration for a name, ARN, home region, S3 destination, logging state, selector mode, tags, and delivery status. Trail configuration survives process restarts through the shared storage layer. Pending event records do not, so durable audit output begins only after a successful S3 delivery.
Trail names resolve within the request region. Trail ARNs can identify a trail across regions, but mutations still apply to the trail's home region. A multi-region trail can select activity from other regions in the same partition. Its delivery path uses the region where the event occurred.
References: src/…/CloudTrailService.java• L95-155 src/…/CloudTrailService.java• L175-229 src/…/CloudTrailService.java• L261-280 src/…/CloudTrailService.java• L828-860
Activity recording
The service accepts inputs for management events and S3 data events. It builds CloudTrail-shaped records with event time, source, action, region, request details, result details, request identity, and event classification. Failed S3 operations can be recorded with their error code and message, so an audit trail can show both successful and rejected activity.
Every management event enters account-and-region event history before trail selection. Only events selected by an active trail enter that trail's delivery buffer. S3 data events use trail selection and delivery, but they do not enter the management event-history view.
Identity data is resolved from the access key when IAM knows the key. Unknown or absent keys use a safe fallback identity. See IAM and authentication for the identity system that supplies this context.
References: src/…/CloudTrailService.java• L488-529 src/…/CloudTrailService.java• L553-579 src/…/CloudTrailService.java• L1038-1108 src/…/CloudTrailService.java• L1111-1146
Event selection
CloudTrail delivers activity only when at least one logging trail accepts it. Selection combines trail scope, logging state, region coverage, and one configured selector mode. A trail with no usable selector does not record S3 data events.
Selector modes
A trail uses either basic event selectors or advanced event selectors. The two modes are mutually exclusive. The same JSON shapes are parsed for the CloudTrail API and CloudFormation trail provisioning, which keeps those entry points consistent.
Basic selectors choose S3 object resources and can limit events to reads, writes, or both. Resource values can cover a bucket, a key prefix, or broad bucket and object patterns. Advanced selectors apply field conditions to the event category, resource type, and resource ARN. Each advanced selector requires all of its field conditions to match, while any matching selector can accept the event.
References: src/…/CloudTrailJsonHandler.java• L167-212 src/…/CloudTrailService.java• L828-835 src/…/CloudTrailService.java• L891-955
Management event scope
Management events use the basic selector's management flags when no advanced selectors are set. A selector can include management events, restrict them to write activity, and exclude named event sources. Advanced selectors can instead match management fields such as event category, source, name, read-only state, event type, or user identity ARN.
S3 data events use resource scope and read/write direction. Bucket-level operations and object-level operations have different resource types under advanced selection, so a selector for one does not silently include the other.
References: src/…/CloudTrailService.java• L863-889 src/…/CloudTrailService.java• L891-929 src/…/CloudTrailService.java• L986-1030
AWS-compatible audit API
The CloudTrail JSON handler exposes the CloudTrail_20131101 action family through the emulator's AWS-compatible request path. It translates request fields into trail state and returns AWS-shaped responses for trail configuration, selectors, tags, logging status, delivery status, and event history.
Trail and selector operations
Clients can create, describe, list, update, and delete trails. They can start or stop logging and read status values such as logging state, logging times, latest delivery time, and latest delivery error. Selector operations return the active basic or advanced form stored for the trail.
Trail creation and update also carry settings for the S3 destination, region behavior, organization status, log file validation, KMS key metadata, and CloudWatch Logs delivery metadata. The emulator uses S3 as the audit delivery destination; these extra settings are represented in trail responses where supported.
References: src/…/CloudTrailJsonHandler.java• L45-64 src/…/CloudTrailJsonHandler.java• L66-100 src/…/CloudTrailJsonHandler.java• L120-156 src/…/CloudTrailJsonHandler.java• L159-252
Tag operations
Trail tags are managed by trail ARN rather than by a regional name. Create-time tags and later tag changes share the same stored tag set. The API supports adding, removing, and listing tags, including tags whose value is absent. A trail can have at most 50 tags.
References: src/…/CloudTrailJsonHandler.java• L385-415 src/…/CloudTrailService.java• L356-393
Event history
LookupEvents reads recent management events from account-and-region event history. The history is in memory only. It retains up to 90 days and 10,000 events for each account and region, and it is cleared when the service resets.
Clients can filter by one supported lookup attribute and by an inclusive time range. Results are newest first and use continuation tokens that belong to the same region and filter. Each response returns at most 50 events, even when a larger MaxResults value is requested. Insight events are not recorded, so an insight-category lookup returns no events. Resource lookup attributes also return no matches because the recorded management events have no resource names.
Event history is separate from delivered trail objects. Clients that need S3 data events or the durable trail output must read the delivered CloudTrail objects from the trail's S3 destination.
References: src/…/CloudTrailEventHistory.java• L17-25 src/…/CloudTrailEventHistory.java• L40-69 src/…/CloudTrailJsonHandler.java• L255-314 src/…/CloudTrailJsonHandler.java• L316-345 src/…/CloudTrailService.java• L532-545
Log delivery
Matched records wait in an in-memory buffer for scheduled delivery. The writer groups at most 1,000 records into a batch, wraps them in a Records envelope, compresses the JSON with gzip, and writes the object under the AWS CloudTrail S3 key layout. A configured key prefix is placed before AWSLogs/<account>/CloudTrail/<event-region>/date.
The writer starts only when CloudTrail is enabled and runs at the configured interval. A manual flush is also available for deterministic local checks. Delivery status records the latest success or failure on the trail.
Delivery reliability
The pending buffer is limited to 1,024 records or about 4 MiB per trail. A failed write returns the batch to the front of the queue for retry. A successful write completes the batch and never retries it, which prevents duplicate log objects. Shutdown makes one best-effort final flush, but records still in memory can be lost when the process ends.
After a delivery write succeeds, the writer submits an S3 data event for the new log object. This preserves self-auditing when a trail selects its own destination bucket. Failure of that follow-up event does not undo or repeat the already successful S3 write.
References: src/…/CloudTrailService.java• L582-671 src/…/CloudTrailService.java• L698-817 src/…/CloudTrailLogWriter.java• L84-235
Change checklist
When these files or sections are changed, remember to consider these:
- Keep management-event and S3-data-event record fields aligned with the selector fields and lookup attributes that can inspect them.
- When selector JSON changes, update API parsing, stored selector mode, API responses, and the shared CloudFormation parsing path together.
- Keep trail home region, event region, and account identity distinct across matching, buffering, event history, and S3 object naming.
- Preserve the delivery boundary: failures before the S3 write requeue records, while failures after the write must not create duplicates.
- Keep event-history retention, ordering, filter behavior, and pagination tokens aligned between
CloudTrailEventHistoryandLookupEvents. - Keep
LookupEventsbehavior and the documented S3-delivery path aligned so clients do not assume that one reads the other. - When trail state fields change, preserve them across selector, tag, logging-status, and delivery-status updates.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Logging is stopped or no selector matches | Activity is not placed in a delivery buffer | Enable logging and configure a selector that covers the event and region |
| A basic and advanced selector are sent together | The selector update is rejected | Send one selector mode per trail |
| A bucket operation is evaluated against an object resource selector | The event is not selected | Use the matching S3 resource type in advanced selectors |
| A delivery write fails | The batch stays pending and delivery status records the error | The writer requeues the batch for a later flush |
| The process ends before buffered records reach S3 | Those records are lost despite a best-effort shutdown flush | Flush before shutdown when the audit output must be durable |
A client expects LookupEvents to include S3 data events or delivered trail objects | The client receives no such records | Read the trail's S3 objects for durable trail output |
| A lookup uses more than one attribute or a token with changed filters | The request is rejected | Send at most one supported attribute and reuse a token with the same lookup scope |
| A lookup asks for events older than 90 days or beyond the per-region event cap | The events are unavailable from event history | Use delivered trail objects for durable audit records |
| Trail delivery succeeds but self-event emission fails | The log object exists without its follow-up audit event | Treat self-emission as separate best-effort activity |
Key files
| File | Why to read it |
|---|---|
| src/…/CloudTrailService.java | Trail lifecycle, event creation, selector matching, event history access, buffering, and delivery state |
| src/…/CloudTrailEventHistory.java | In-memory management-event retention, lookup filters, ordering, and capacity limits |
| src/…/CloudTrailJsonHandler.java | AWS-compatible trail, selector, tag, status, and event-history actions |
| src/…/CloudTrailLogWriter.java | Scheduled batching, gzip delivery, retry behavior, and self-audit emission |
| src/…/CloudTrailEntry.java | Durable trail state, selector mode, tags, logging state, and delivery status |
| src/…/Trail.java | AWS-shaped trail identity, destination, region, and feature settings |
| src/…/EventSelector.java | Basic management and S3 data selection shape |
| src/…/AdvancedEventSelector.java | Advanced selector grouping and field conditions |