Sub topic 13
Auditing key use with CloudTrail
This chapter assumes you can use the AWS CLI and that you know the Encrypt, Decrypt and GenerateDataKey calls from earlier chapters. It also assumes you know what encryption context is. Here you will see who used a key, when, and for what.
What KMS records
AWS CloudTrail is the AWS service that records API calls. Every AWS KMS API call is logged as a management event. This covers data operations such as Encrypt, Decrypt and GenerateDataKey. It also covers administrative operations such as CreateGrant, DisableKey and ScheduleKeyDeletion.
Each record shows the caller identity, the time, the source IP address and the request parameters. It also shows the key ARN and any error code. Plaintext and ciphertext are never logged.
The last 90 days of management events appear in CloudTrail Event history for each Region, with no setup. For longer retention, create a trail that delivers events to an S3 bucket. Create a trail in each Region where you use the key, or make it a multi-Region trail.
Searching Event history
The following command lists recent KMS events:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventSource,AttributeValue=kms.amazonaws.com \
--max-results 20 \
--query "Events[].[EventTime,EventName,Username]" \
--output tableTo narrow the search to one operation, use AttributeKey=EventName,AttributeValue=Decrypt. Only one lookup attribute is allowed per call. Filter further on the CloudTrailEvent JSON string in each result.
Reading a Decrypt event
A trimmed Decrypt record looks like this:
{
"eventSource": "kms.amazonaws.com",
"eventName": "Decrypt",
"userIdentity": {
"type": "AssumedRole",
"arn": "arn:aws:sts::111122223333:assumed-role/app-role/session1"
},
"requestParameters": {
"encryptionContext": { "purpose": "demo" },
"keyId": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
},
"resources": [
{ "type": "AWS::KMS::Key",
"ARN": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" }
]
}Check these fields first:
userIdentitynames the role or user. When an AWS service calls KMS for you,invokedBynames the service, such ass3.amazonaws.com.requestParameters.encryptionContextshows why the call was made. S3 setsaws:s3:arnto the object or bucket ARN, so you can tie a decrypt to an object.errorCode, such asAccessDeniedExceptionorDisabledException, marks failed attempts. A burst of failures can mean a misconfigured policy or a probing caller.
Gotchas
- A trail can be set to exclude KMS events. If you use that setting, KMS activity disappears from the trail, so check it.
- Services can generate many
Decryptcalls. S3 Bucket Keys cut this volume and the cost of storing the logs. - Event history cannot be searched past 90 days. Use a trail with S3 and Athena, or CloudTrail Lake, for older records.
- For alerts on
ScheduleKeyDeletionorDisableKey, send trail events to CloudWatch Logs or EventBridge and create a rule.
Exercise
A security reviewer asks which role called Decrypt on your key last week, and whether the call was for one S3 object. Which two fields in the CloudTrail event answer this? What do you need if the question is about events from four months ago?
Show answer
The role comes from userIdentity (the arn field). The object comes from requestParameters.encryptionContext, which S3 fills with aws:s3:arn. Event history holds only 90 days, so you need a trail that delivers events to S3, or CloudTrail Lake. The trail must have been created before the events happened.