Sub topic 16
Auditing access with CloudTrail
This chapter assumes you can read CloudTrail's JSON events and know the IAM and KMS setup from the earlier chapters. It shows how to find out who read, changed or deleted a parameter or secret.
What CloudTrail records
AWS CloudTrail logs API calls made to your account. Parameter Store, Secrets Manager and KMS calls are all recorded as management events, so you get them without extra configuration.
| Service | Events worth watching |
|---|---|
| Parameter Store | `GetParameter`, `GetParameters`, `GetParametersByPath`, `PutParameter`, `DeleteParameter` |
| Secrets Manager | `GetSecretValue`, `PutSecretValue`, `RotateSecret`, `DeleteSecret` |
| KMS | `Decrypt`, `GenerateDataKey` |
One read of a secret usually produces two events: a Secrets Manager GetSecretValue and a KMS Decrypt. The KMS event carries an encryption context that names the secret or parameter, so you can trace key use back to a specific value.
CloudTrail records who called and what they asked for. It does not store the secret value. Do not rely on it to recover a lost value.
Searching recent events
Event history keeps 90 days of management events. Search it with lookup-events:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=GetSecretValue \
--max-results 20 \
--query 'Events[].{time:EventTime,user:Username,id:EventId}'To see the caller and the secret for each event, parse the embedded JSON:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=GetSecretValue \
--query 'Events[].CloudTrailEvent' --output text \
| jq -r 'select(.userIdentity.arn != null)
| [.eventTime, .userIdentity.arn, .requestParameters.secretId] | @tsv'lookup-events allows only one lookup attribute per call and is rate limited. Use it for investigation, not for continuous monitoring.
Keeping logs longer
Event history expires after 90 days and cannot be queried across a whole organization. Create a trail that delivers to an S3 bucket. For organizations, create an organization trail from the management account. Query the logs with Athena, or send the trail to CloudWatch Logs for metric filters.
A trail logs management events by default, which covers every call in the table above. Check that your trail does not exclude KMS events. Frequent KMS calls can make the log large.
Alerting on risky actions
Searching after the fact is slower than being told. Use an Amazon EventBridge rule on events such as DeleteSecret, PutSecretValue or reads from an unexpected principal. This pattern matches deletions:
{
"source": ["aws.secretsmanager"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventName": ["DeleteSecret", "PutResourcePolicy"]
}
}Send the match to an SNS topic or a Lambda function.
Common errors
- No events found. Event history is per Region. Search the Region where the secret lives.
- Reads missing for a parameter. Parameter Store events appear under the
ssm.amazonaws.comevent source. Searching for Secrets Manager names will not find them. - Cannot see who used a key. Read the
Decryptevent, not only the service event. It shows the role and the encryption context. - Assumed-role callers look anonymous. The
userIdentity.arnfor an assumed role includes the session name. Set meaningful session names so you can tell workloads apart.
Exercise
An auditor asks who read the secret myapp/prod/db 120 days ago. Event history shows nothing. What is the cause, and which two event names would you search for in a trail's logs?
Show answer
Event history keeps only 90 days, so the events are gone unless a trail was delivering to S3 at the time. In the trail logs, search for the Secrets Manager GetSecretValue event, filtered on the secret ID, and for the KMS Decrypt event whose encryption context names the secret.