Sub topic 9
Allowing the workflow to request a token
This chapter covers the permissions block in a GitHub Actions workflow. It controls whether a job can request an OIDC token from GitHub. It assumes you know the basics of workflows (jobs, steps, triggers) and have read the earlier chapters on the token claims and the IAM trust policy.
Why the token is not available by default
GitHub does not hand an OIDC token to every job. A job can request one only if the workflow grants the id-token permission with the value write. Without it, the runner has no token endpoint to call, and any step that tries to fetch a token fails.
This is a deliberate safeguard. Jobs that never talk to a cloud provider have no reason to be able to mint identity tokens, so the capability is opt-in.
The name is easy to misread. id-token: write does not let the job write anything to your repository or to AWS. It only lets the job request a signed JSON Web Token (JWT) that describes the running workflow. The token is useless to AWS until the role trust policy accepts it.
The GITHUB_TOKEN and the permissions block
Every job also receives a GITHUB_TOKEN. This is a separate, repository-scoped credential that GitHub's API accepts. The permissions key sets what that token can do, and it also controls the id-token permission.
The permissions are:
| Permission | Purpose | Typical value |
|---|---|---|
| `id-token` | Lets the job request an OIDC token | `write` |
| `contents` | Reads or writes repository files and commits | `read` |
| `pull-requests` | Reads or writes pull requests and comments | not needed for deploys |
id-token accepts only write or none. There is no read value for it.
Setting the permissions
Here is the smallest block that works for a deployment job:
permissions:
id-token: write
contents: readYou need contents: read because of an easy-to-miss side effect. When you set any permission explicitly, every permission you leave out is set to none. If you list only id-token: write, then actions/checkout can no longer read your repository, and the checkout step fails. Adding contents: read restores that access.
The rule is: once you write a permissions block, you must list everything the job needs.
Workflow level or job level
You can place permissions at the top of the workflow, where it applies to all jobs. You can also place it inside a single job, where it applies only to that job and replaces the workflow-level setting for it.
Job-level placement is better for least privilege. Only the job that deploys gets the ability to request a token:
name: build-and-deploy
on:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Tests run here with no cloud access"
deploy:
needs: test
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- run: echo "Only this job can request an OIDC token"The test job inherits contents: read from the workflow level. The deploy job defines its own block, so it receives both permissions. If a step in the test job tried to request a token, it would fail. That is the behavior you want: a compromised test dependency cannot obtain AWS credentials.
What the runner provides when the permission is set
When id-token: write is active, GitHub adds two environment variables to the job:
ACTIONS_ID_TOKEN_REQUEST_URL, the URL the job calls to ask for a token.ACTIONS_ID_TOKEN_REQUEST_TOKEN, a bearer credential used to authenticate that request.
You normally never touch these. The aws-actions/configure-aws-credentials action, covered in the next chapter, reads them and does the exchange for you. You can still confirm they exist with a quick diagnostic step:
- name: Check token endpoint is available
run: |
if [ -z "$ACTIONS_ID_TOKEN_REQUEST_URL" ]; then
echo "id-token permission is missing for this job"
exit 1
fi
echo "Token endpoint is available"Do not print the value of ACTIONS_ID_TOKEN_REQUEST_TOKEN in logs. Anyone who sees it during the job could request tokens as that job.
Common mistakes
- Missing the block entirely. The default permissions for
GITHUB_TOKENdo not includeid-token. The workflow runs, but the credentials step fails with an error such asCredentials could not be loadedor a message that the OIDC token request variables are not set. - Forgetting
contents: read. Checkout fails with a repository not found error, even though the repository exists. - Setting the permission at the wrong level. A job-level
permissionsblock replaces the workflow-level one. It does not merge with it. If the job block lacksid-token: write, the workflow-level value does not apply. - Running from a forked pull request. For pull requests from forks, GitHub does not grant
id-token: write, even if the workflow requests it. This protects your AWS account from code written by outside contributors. Deployments should run onpushor on trusted events, not onpull_requestfrom forks.
Where this fits in the flow
The workflow permission is the first gate in the chain. The following steps all have to succeed for a deployment to work:
- The job has
id-token: write, so GitHub can issue a token. - The token carries claims such as
iss,aud, andsub. - The IAM OIDC provider recognizes the issuer.
- The role trust policy accepts the
audandsubvalues. - AWS STS returns temporary credentials.
Failing at step 1 gives a different error from failing at step 4. A missing permission produces a failure before any request reaches AWS, so you will see nothing in CloudTrail. A trust policy mismatch does reach AWS and is recorded there.
Exercise
A deploy job uses actions/checkout followed by aws-actions/configure-aws-credentials. You add permissions: id-token: write to the job and nothing else. The OIDC step now works, but checkout fails. Why, and how do you fix it?
Show answer
Once a permissions block lists any permission, every permission not listed is set to none. Listing only id-token: write removes the contents read access that checkout needs. Fix it by also adding contents: read to the same block.
Exercise
Your workflow has permissions: id-token: write at the top level and a build job with its own block containing only contents: read. Can a step in the build job request an OIDC token? Why does this matter for security?
Show answer
No. A job-level permissions block replaces the workflow-level one, so build has no id-token permission. This is useful for least privilege: jobs that only build or test cannot obtain AWS credentials, so a compromised dependency in those jobs cannot assume the deployment role.