-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add action to get short-lived access token using OIDC #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21bf1c4
Add action to get short-lived access token using OIDC
Mrtenz be7b59c
Remove extra newline
Mrtenz 318a7b2
Fix format of `requested_permissions`
Mrtenz a79d250
Add OIDC audience
Mrtenz 8bfb594
Add option to set target repository
Mrtenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| name: Get Token | ||
| description: 'Get a short-lived access token to interact with the GitHub API. Note: Using this action requires the "id-token: write" permission to be set for the GitHub Actions workflow job.' | ||
|
|
||
| inputs: | ||
| token-exchange-url: | ||
| description: 'The URL to exchange the OIDC token for an access token.' | ||
| required: true | ||
| permissions: | ||
| description: 'The permissions to request for the access token, in the format of "scope: permission", separated by newlines.' | ||
| required: true | ||
| target-repository: | ||
| description: 'The repository to target for the access token. Defaults to the current repository.' | ||
| required: false | ||
| default: '${{ github.repository }}' | ||
|
|
||
| outputs: | ||
| token: | ||
| value: '${{ steps.access-token.outputs.token }}' | ||
| description: 'The short-lived access token obtained from the token exchange service.' | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Get OIDC token | ||
| id: oidc-token | ||
| uses: actions/github-script@v9 | ||
| with: | ||
| script: | | ||
| const token = await core.getIDToken('api://token-exchange-service'); | ||
| core.setSecret(token); | ||
| core.setOutput('token', token); | ||
|
|
||
| - name: Exchange OIDC token | ||
| id: access-token | ||
| uses: actions/github-script@v9 | ||
| env: | ||
| OIDC_TOKEN: ${{ steps.oidc-token.outputs.token }} | ||
| TOKEN_EXCHANGE_URL: ${{ inputs.token-exchange-url }} | ||
| REQUESTED_PERMISSIONS: ${{ inputs.permissions }} | ||
| TARGET_REPOSITORY: ${{ inputs.target-repository }} | ||
| with: | ||
| script: | | ||
| const { | ||
| OIDC_TOKEN, | ||
| TOKEN_EXCHANGE_URL, | ||
| REQUESTED_PERMISSIONS, | ||
| TARGET_REPOSITORY, | ||
| } = process.env; | ||
|
|
||
| // This assumes `REQUESTED_PERMISSIONS` is a newline-separated string | ||
| // of "scope: permission" pairs, e.g.: | ||
| // ``` | ||
| // contents: read | ||
| // issues: write | ||
| // ``` | ||
| const requestedPermissions = Object.fromEntries( | ||
| REQUESTED_PERMISSIONS | ||
| .split('\n') | ||
| .filter(line => line.trim() !== '') | ||
| .map(line => line.split(':').map(part => part.trim())) | ||
| ); | ||
|
|
||
| const response = await fetch(`${TOKEN_EXCHANGE_URL}/api/exchange/token`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify({ | ||
| oidcToken: OIDC_TOKEN, | ||
| targetRepo: TARGET_REPOSITORY, | ||
| requested_permissions: requestedPermissions, | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| return core.setFailed(`Token exchange failed: ${response.status} ${response.statusText} - ${errorText}`); | ||
| } | ||
|
|
||
| const { token, permissions, expires_at, } = await response.json(); | ||
| if (!token) { | ||
| return core.setFailed('Token exchange response did not contain an access token.'); | ||
| } | ||
|
|
||
| core.setSecret(token); | ||
| core.setOutput('token', token); | ||
|
|
||
| core.info(`Access token obtained successfully. Token expires at: "${expires_at}".`); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why save the token to
outputsin this step?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the OIDC token used to request the access token in the second step. It's referenced below:
In case you're confused about the
core.setSecretabove, that's actually just theadd-maskworkflow command and doesn't actually set a secret.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, thanks for the clarification.