Sub topic 13
Auditing and monitoring secret access
This chapter assumes you can read a GitHub Actions workflow and know what an AWS IAM role is. You should also have seen platform secret stores and an external secret manager in earlier chapters. Here you will learn how to see who used a secret, so that misuse shows up as a signal instead of a surprise.
Why audit secret access
Storing secrets safely does not tell you whether someone is using them. A leaked token looks like a normal request unless you can compare it with what you expect. Auditing gives you the record. Monitoring turns that record into alerts. Both feed the incident response chapter that follows.
An audit trail should answer four questions:
- Who or what accessed the secret?
- Which secret or role was it?
- When did it happen?
- From where, such as which workflow, repository, branch, or IP address?
Where the records live
Each layer of your setup keeps its own log. You need to know which log answers which question.
| Source | What it records | Typical question it answers |
|---|---|---|
| CI platform audit log | Secrets created, updated, or deleted, and permission changes | Who changed this secret? |
| Workflow run history | Which workflow, branch, and commit ran | Which job could have read the secret? |
| Secret manager log | Each read of a secret value | Which identity fetched it? |
| Cloud provider log (for example AWS CloudTrail) | Role assumptions and API calls | Was the role used outside a pipeline? |
A CI platform does not log every time a job reads a stored secret. It logs changes to the secret, so reads must be traced through run history. The log of the system that uses the credential, such as your cloud provider, is the stronger source.
Auditing OIDC role assumptions
With OIDC federation, each role assumption is an event you can inspect. The trust policy limits which sub claim may assume the role. The log shows which subject actually did.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRoleWithWebIdentity \
--max-results 20 \
--output json \
| jq -r '.Events[].CloudTrailEvent | fromjson
| [.eventTime, .userIdentity.userName, .sourceIPAddress] | @tsv'Each line shows the time, the token subject (for example repo:my-org/my-repo:environment:production), and the source IP address. Subjects you do not recognize are the first thing to look for.
What to alert on
Alert on events that break the normal pattern, not on every access. Useful rules include:
- A role assumed with a
subclaim that matches no known repository and environment. - A secret read with no workflow run at the same time. This suggests the credential is being used outside the pipeline.
- Access from an IP address range your runners never use. This only works for self-hosted runners or other known ranges, because hosted runner addresses change.
- A secret created, changed, or deleted outside a change window, or by an unexpected user.
- A sudden rise in failed access attempts.
Start with two or three rules you will actually act on. Many noisy alerts get ignored.
Common mistakes
- Enabling logs but never reading them. Review alert rules on a schedule and test that they fire.
- Keeping logs only in the system being attacked. Copy them out.
- Short retention. Leaks are often found weeks later, so keep logs long enough to reconstruct that period.
- Assuming masked job logs are an audit trail. Masking hides values and does not record who used them.
Exercise
A production deploy role is trusted only for the subject repo:my-org/my-repo:environment:production. Your cloud log shows a successful role assumption with the subject repo:my-org/my-repo:ref:refs/heads/feature-x. What does this tell you, and what should you check first?
Show answer
The trust policy should have rejected that subject, so either the policy condition is missing or too loose (for example, a wildcard on sub), or a different role or policy allowed it. Check the role's trust policy conditions first, then review the workflow run tied to that branch to see what the credentials were used for.