Sub topic 1
Why Long-Lived Access Keys Are a Problem in CI
A continuous integration (CI) job that deploys to AWS needs credentials. The most common first solution is an IAM user with an access key, stored as a GitHub secret. It works, and that is why it spreads. This chapter explains what that setup costs you, so the rest of the tutorial has a clear problem to solve.
Prerequisites
This chapter assumes you already have:
- Basic GitHub Actions knowledge: workflows, jobs, steps, triggers, and repository secrets.
- Basic AWS IAM knowledge: users, roles, and policies.
- Enough AWS CLI experience to run a command and read JSON or table output.
You do not need to know OIDC yet. It is introduced in the next chapter.
The pattern most teams start with
You create an IAM user, attach a policy, generate an access key, and paste the two values into repository secrets. The workflow reads them as environment variables:
name: deploy-with-static-keys
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: us-east-1
steps:
- uses: actions/checkout@v4
- run: aws s3 sync ./site s3://my-example-bucket --deleteThe pair is a long-term credential. Its ID starts with AKIA, while temporary credentials issued by AWS Security Token Service (STS) start with ASIA. An IAM user's key has no expiry date. It stays valid until someone deactivates or deletes it.
Where the keys can leak
A secret stored in GitHub is encrypted at rest, but it is decrypted and handed to every job that references it. Each place where a job can read or print it is a place it can escape.
- Workflow logs. GitHub masks the exact secret string in logs. If a script transforms the value, for example by base64-encoding it or splitting it across lines, the masking may not match and the value appears in plain text.
- Third-party actions. An action you run in the same step or job can read the environment variables. If its repository is compromised, or you pin a mutable tag like
@v1, a new release can send your keys elsewhere. - Build dependencies. A malicious package installed during a build runs as the same process user and can read the same environment.
- Pull request workflows. Workflows triggered by
pull_request_targetrun with secrets available. If they check out and execute code from the pull request, an outside contributor can run code that reads the keys. - People and copies. Someone with write access can edit a workflow to print the value. Someone who created the key may have also saved it in a password manager, a shell history file, or a local
~/.aws/credentialsfile.
When any of these happens, the key keeps working after the incident is over. The attacker can use it from their own machine, at any hour, until you notice.
Why "long-lived" is the core problem
Several properties combine to make static keys risky.
- No expiry. A leaked temporary credential becomes useless after at most a few hours. A leaked access key stays useful for months or years.
- No context. AWS cannot tell whether a request came from your
mainbranch, a fork, or an attacker's laptop. The key is a bearer secret: whoever holds it is you. - Wide blast radius. One IAM user is often shared by many repositories and environments. A leak in a low-risk test repository then exposes production permissions.
- Rotation is manual. A user can have at most two active keys, so rotation means create, update the secret, test, deactivate, delete. Under deadline pressure this gets skipped, and keys age.
- Weak attribution. CloudTrail records the IAM user name for every call. It does not say which repository, workflow run, or commit made the call.
Common mistakes that make it worse
- Over-broad policies. Teams attach
AdministratorAccessto the CI user "to make it work" and never narrow it. A leak then means full account access. - One user for everything. A single
github-ciuser shared across repositories means you cannot revoke access for one project without breaking all of them. - Keys that nobody owns. The employee who created the key leaves, and the key keeps working because nobody knows it exists.
- Rotation without cleanup. A new key is added but the old one stays active "just in case".
- Treating masking as protection. Log masking hides accidental prints. It does not stop code that is running in the job from reading the value.
Finding static keys in your own account
Before changing anything, look at what exists. The first command lists the keys for one user, and the second shows when a given key was last used. Replace the user name and key ID with your own values.
aws iam list-access-keys \
--user-name github-ci \
--query 'AccessKeyMetadata[].[AccessKeyId,Status,CreateDate]' \
--output table
aws iam get-access-key-last-used \
--access-key-id AKIAEXAMPLEKEYID123To review every user at once, generate the credential report and decode it. The report is a CSV file that includes the age, last use, and last rotation date of each user's keys.
aws iam generate-credential-report
aws iam get-credential-report \
--query Content --output text | base64 --decode > credential-report.csvThe generate-credential-report call can take a few seconds. If get-credential-report returns an error saying the report is not ready, wait and run it again.
Look for keys that are older than your rotation policy, keys that were never used, and users whose names suggest they belong to CI. Each one is a candidate for replacement.
What the alternative looks like
The fix is not to rotate faster. It is to stop storing a secret at all. With OpenID Connect (OIDC) federation, the workflow proves its identity to AWS using a short-lived signed token that GitHub issues for each job. AWS checks the token against a trust policy and returns temporary credentials.
| Property | Static access key | OIDC with an IAM role |
|---|---|---|
| Stored in GitHub | Yes, as secrets | No secret stored |
| Lifetime | Until manually revoked | Short session, expires automatically |
| Scope control | By IAM policy only | By policy and by token claims such as repository and branch |
| Rotation | Manual | Not needed |
| CloudTrail attribution | IAM user name | Role session with workflow context |
The remaining chapters build this up piece by piece: the token, the identity provider, the trust policy, and the workflow.
Exercise
A teammate says, "Our static key is safe because GitHub masks secrets in logs and encrypts them at rest." Give two reasons this does not remove the risk.
Show answer
Masking only hides the exact string in logs and does not stop code in the job from reading the environment variable, including a third-party action or a malicious dependency. Encryption at rest protects storage, but the secret is decrypted and given to every job that references it. Also, the key never expires, so a copy taken from any of those places stays usable until someone manually revokes it.
Exercise
Which command would you run to see when a specific access key was last used, and what would a result of "never used" suggest?
Show answer
Run aws iam get-access-key-last-used --access-key-id <key-id>. A result showing the key was never used suggests the key is unneeded and can be deactivated, which removes risk without affecting any workflow.