Skip to content

Instantly share code, notes, and snippets.

@pburkholder
Last active March 21, 2022 12:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pburkholder/855141433cec44752cb8d01f731c9a17 to your computer and use it in GitHub Desktop.
Save pburkholder/855141433cec44752cb8d01f731c9a17 to your computer and use it in GitHub Desktop.
event_name="ConsoleLogin"
aws cloudtrail lookup-events --lookup-attributes \
AttributeKey=EventName,AttributeValue=$event_name --query \
'Events[*].{Ev:CloudTrailEvent,User:Username}' |
jq '.[]| "Username: " + .User, " " + (.Ev| fromjson | "EventTime: " + .eventTime, "SourceIP: " + .sourceIPAddress) '
for event_name in AuthorizeSecurityGroupEgress AuthorizeSecurityGroupIngress CreatePolicy \
CreateSecurityGroup DeleteTrail ModifyVpcAttribute PutUserPolicy PutRolePolicy \
RevokeSecurityGroupEgress RevokeSecurityGroupIngress UpdateTrail; do
printf "\n================= $event_name ===========\n"
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=$event_name | jq '.Events | length'
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=$event_name |
jq '[.Events[]| {Username: .Username, EventInfo: (.CloudTrailEvent | fromjson) }]'
done
@pburkholder
Copy link
Author

Here's one way of digging into the CreatePolicy events:

The aws cloudtrail call gets all the CreatePolicy events

  • the jq unpacks some of the strings in the original JSON that are themselves embedded JSON
    • the CloudTrailEvent string, which in turn embbeds
      • the requestParameters.policyDocument
  • the final jq just selects the events that have Username = terraform-provision
event_name="CreatePolicy"
aws cloudtrail lookup-events \
  --lookup-attributes AttributeKey=EventName,AttributeValue=$event_name |
    jq '[.Events[]| {Username: .Username, EventInfo: (.CloudTrailEvent | fromjson), PolicyDocument: (.CloudTrailEvent | fromjson | .requestParameters.policyDocument | fromjson) } ]'  | 
    jq '.[]| select(.Username=="terraform-provision")'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment