Skip to content

Commit b8c74de

Browse files
Richard H Boyddependabot[bot]mergify[bot]GitHub Actions
authored
feat: added OIDC (#262)
* feat: OIDC provider (with PR comments) * chore: Bump jest from 27.2.1 to 27.2.2 (#267) Bumps [jest](https://github.com/facebook/jest) from 27.2.1 to 27.2.2. - [Release notes](https://github.com/facebook/jest/releases) - [Changelog](https://github.com/facebook/jest/blob/main/CHANGELOG.md) - [Commits](jestjs/jest@v27.2.1...v27.2.2) --- updated-dependencies: - dependency-name: jest dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore: Bump ansi-regex from 5.0.0 to 5.0.1 (#269) Bumps [ansi-regex](https://github.com/chalk/ansi-regex) from 5.0.0 to 5.0.1. - [Release notes](https://github.com/chalk/ansi-regex/releases) - [Commits](chalk/ansi-regex@v5.0.0...v5.0.1) --- updated-dependencies: - dependency-name: ansi-regex dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore: Bump aws-sdk from 2.991.0 to 2.996.0 (#268) Bumps [aws-sdk](https://github.com/aws/aws-sdk-js) from 2.991.0 to 2.996.0. - [Release notes](https://github.com/aws/aws-sdk-js/releases) - [Changelog](https://github.com/aws/aws-sdk-js/blob/master/CHANGELOG.md) - [Commits](aws/aws-sdk-js@v2.991.0...v2.996.0) --- updated-dependencies: - dependency-name: aws-sdk dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> * chore: Update dist * feat: OIDC provider (with PR comments) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: GitHub Actions <runner@fv-az209-487.sst5i0nymnhu5a1lxus1lxbvub.xx.internal.cloudapp.net>
1 parent ef6880f commit b8c74de

File tree

6 files changed

+4352
-172
lines changed

6 files changed

+4352
-172
lines changed

README.md

+81-57
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Configure AWS credential and region environment variables for use in other GitHu
99
- [Usage](#usage)
1010
- [Credentials](#credentials)
1111
- [Assuming a Role](#assuming-a-role)
12-
+ [Permissions for assuming a role](#permissions-for-assuming-a-role)
1312
+ [Session tagging](#session-tagging)
13+
+ [Sample IAM Role Permissions](#sample-iam-role-cloudformation-template)
1414
- [Self-Hosted Runners](#self-hosted-runners)
1515
- [License Summary](#license-summary)
1616
- [Security Disclosures](#security-disclosures)
@@ -25,9 +25,7 @@ Add the following step to your workflow:
2525
- name: Configure AWS Credentials
2626
uses: aws-actions/configure-aws-credentials@v1
2727
with:
28-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
29-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
30-
# aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }} # if you have/need it
28+
role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role
3129
aws-region: us-east-2
3230
```
3331
@@ -47,8 +45,7 @@ jobs:
4745
- name: Configure AWS credentials from Test account
4846
uses: aws-actions/configure-aws-credentials@v1
4947
with:
50-
aws-access-key-id: ${{ secrets.TEST_AWS_ACCESS_KEY_ID }}
51-
aws-secret-access-key: ${{ secrets.TEST_AWS_SECRET_ACCESS_KEY }}
48+
role-to-assume: arn:aws:iam::111111111111:role/my-github-actions-role-test
5249
aws-region: us-east-1
5350

5451
- name: Copy files to the test website with the AWS CLI
@@ -58,8 +55,7 @@ jobs:
5855
- name: Configure AWS credentials from Production account
5956
uses: aws-actions/configure-aws-credentials@v1
6057
with:
61-
aws-access-key-id: ${{ secrets.PROD_AWS_ACCESS_KEY_ID }}
62-
aws-secret-access-key: ${{ secrets.PROD_AWS_SECRET_ACCESS_KEY }}
58+
role-to-assume: arn:aws:iam::222222222222:role/my-github-actions-role-prod
6359
aws-region: us-west-2
6460

6561
- name: Copy files to the production website with the AWS CLI
@@ -72,19 +68,39 @@ See [action.yml](action.yml) for the full documentation for this action's inputs
7268
## Credentials
7369
7470
We recommend following [Amazon IAM best practices](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html) for the AWS credentials used in GitHub Actions workflows, including:
75-
* Do not store credentials in your repository's code. You may use [GitHub Actions secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets) to store credentials and redact credentials from GitHub Actions workflow logs.
76-
* [Create an individual IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#create-iam-users) with an access key for use in GitHub Actions workflows, preferably one per repository. Do not use the AWS account root user access key.
71+
* Do not store credentials in your repository's code.
7772
* [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege) to the credentials used in GitHub Actions workflows. Grant only the permissions required to perform the actions in your GitHub Actions workflows.
78-
* [Rotate the credentials](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#rotate-credentials) used in GitHub Actions workflows regularly.
7973
* [Monitor the activity](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#keep-a-log) of the credentials used in GitHub Actions workflows.
8074
8175
## Assuming a Role
82-
If you would like to use the static credentials you provide to this action to assume a role, you can do so by specifying the role ARN in `role-to-assume`.
83-
The role credentials will then be configured in the Actions environment instead of the static credentials you have provided.
84-
The default session duration is 6 hours, but if you would like to adjust this you can pass a duration to `role-duration-seconds`.
76+
We recommend using GitHub's OIDC provider to get short-lived credentials needed for your actions.
77+
Specifying `role-to-assume` without providing an `aws-access-key-id` or a `web-identity-token-file` will signal to the action that you wish to use the OIDC provider.
78+
The default session duration is 1 hour when using the OIDC provider to directly assume an IAM Role.
79+
The default session duration is 6 hours when using an IAM User to assume an IAM Role (by providing an `aws-access-key-id`, `aws-secret-access-key`, and a `role-to-assume`) .
80+
If you would like to adjust this you can pass a duration to `role-duration-seconds`, but the duration cannot exceed the maximum that was defined when the IAM Role was created.
8581
The default session name is GitHubActions, and you can modify it by specifying the desired name in `role-session-name`.
8682

87-
Example:
83+
The following table describes which identity is used based on which values are supplied to the Action:
84+
85+
| **Identity Used** | `aws-access-key-id` | `role-to-assume` | `web-identity-token-file` |
86+
|------------------------------------------------------------------|---------------------|------------------|---------------------------|
87+
| [✅ Recommended] Assume Role directly using GitHub OIDC provider | | ✔ | |
88+
| IAM User | ✔ | | |
89+
| Assume Role using IAM User credentials | ✔ | ✔ | |
90+
| Assume Role using WebIdentity Token File credentials | | ✔ | ✔ |
91+
92+
### Examples
93+
94+
```yaml
95+
- name: Configure AWS Credentials
96+
uses: aws-actions/configure-aws-credentials@v1
97+
with:
98+
aws-region: us-east-2
99+
role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role
100+
role-session-name: MySessionName
101+
```
102+
In this example, the Action will load the OIDC token from the GitHub-provided environment variable and use it to assume the role `arn:aws:iam::123456789100:role/my-github-actions-role` with the session name `MySessionName`.
103+
88104
```yaml
89105
- name: Configure AWS Credentials
90106
uses: aws-actions/configure-aws-credentials@v1
@@ -99,48 +115,52 @@ Example:
99115
```
100116
In this example, the secret `AWS_ROLE_TO_ASSUME` contains a string like `arn:aws:iam::123456789100:role/my-github-actions-role`. To assume a role in the same account as the static credentials, you can simply specify the role name, like `role-to-assume: my-github-actions-role`.
101117

102-
### Permissions for assuming a role
103-
104-
In order to assume a role, the IAM user for the static credentials must have the following permissions:
105-
```json
106-
{
107-
"Version": "2012-10-17",
108-
"Statement": [
109-
{
110-
"Action": [
111-
"sts:AssumeRole",
112-
"sts:TagSession"
113-
],
114-
"Resource": "arn:aws:iam::123456789012:role/my-github-actions-role",
115-
"Effect": "Allow"
116-
}
117-
]
118-
}
118+
### Sample IAM Role CloudFormation Template
119+
```yaml
120+
Parameters:
121+
GitHubOrg:
122+
Type: String
123+
RepositoryName:
124+
Type: String
125+
OIDCProviderArn:
126+
Description: Arn for the GitHub OIDC Provider.
127+
Default: ""
128+
Type: String
129+
130+
Conditions:
131+
CreateOIDCProvider: !Equals
132+
- !Ref OIDCProviderArn
133+
- ""
134+
135+
Resources:
136+
Role:
137+
Type: AWS::IAM::Role
138+
Properties:
139+
RoleName: ExampleGithubRole
140+
AssumeRolePolicyDocument:
141+
Statement:
142+
- Effect: Allow
143+
Action: sts:AssumeRoleWithWebIdentity
144+
Principal:
145+
Federated: !Ref GithubOidc
146+
Condition:
147+
StringLike:
148+
vstoken.actions.githubusercontent.com:sub: !Sub repo:${GitHubOrg}/${RepositoryName}:*
149+
150+
GithubOidc:
151+
Type: AWS::IAM::OIDCProvider
152+
Condition: CreateOIDCProvider
153+
Properties:
154+
Url: https://vstoken.actions.githubusercontent.com
155+
ClientIdList: [sigstore]
156+
ThumbprintList: [a031c46782e6e6c662c2c87c76da9aa62ccabd8e]
157+
158+
Outputs:
159+
Role:
160+
Value: !GetAtt Role.Arn
119161
```
120162

121-
The role's trust policy must allow the IAM user to assume the role:
122-
```json
123-
{
124-
"Version": "2012-10-17",
125-
"Statement": [
126-
{
127-
"Sid": "AllowIamUserAssumeRole",
128-
"Effect": "Allow",
129-
"Action": "sts:AssumeRole",
130-
"Principal": {"AWS": "arn:aws:iam::123456789012:user/my-github-actions-user"},
131-
"Condition": {
132-
"StringEquals": {"sts:ExternalId": "Example987"}
133-
}
134-
},
135-
{
136-
"Sid": "AllowPassSessionTags",
137-
"Effect": "Allow",
138-
"Action": "sts:TagSession",
139-
"Principal": {"AWS": "arn:aws:iam::123456789012:user/my-github-actions-user"}
140-
}
141-
]
142-
}
143-
```
163+
The GitHub OIDC Provider only needs to be created once per account (i.e. multiple IAM Roles that can be assumed by the GitHub's OIDC can share a single OIDC Provider)
144164

145165
### Session tagging
146166
The session will have the name "GitHubActions" and be tagged with the following tags:
@@ -158,7 +178,10 @@ The session will have the name "GitHubActions" and be tagged with the following
158178

159179
_Note: all tag values must conform to [the requirements](https://docs.aws.amazon.com/STS/latest/APIReference/API_Tag.html). Particularly, `GITHUB_WORKFLOW` will be truncated if it's too long. If `GITHUB_ACTOR` or `GITHUB_WORKFLOW` contain invalid characters, the characters will be replaced with an '*'._
160180

161-
The action will use session tagging by default during role assumption. You can skip this session tagging by providing `role-skip-session-tagging` as true in the action's inputs:
181+
The action will use session tagging by default during role assumption.
182+
Note that for WebIdentity role assumption, the session tags have to be included in the encoded WebIdentity token.
183+
This means that Tags can only be supplied by the OIDC provider and not set during the AssumeRoleWithWebIdentity API call within the Action.
184+
You can skip this session tagging by providing `role-skip-session-tagging` as true in the action's inputs:
162185

163186
```yaml
164187
uses: aws-actions/configure-aws-credentials@v1
@@ -189,7 +212,8 @@ with:
189212
```
190213
In this case, your runner's credentials must have permissions to assume the role.
191214

192-
You can also assume a role using a web identity token file, such as if using [Amazon EKS IRSA](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html). Pods running in EKS worker nodes that do not run as root can use this file to assume a role with a web identity.
215+
You can also assume a role using a web identity token file, such as if using [Amazon EKS IRSA](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html).
216+
Pods running in EKS worker nodes that do not run as root can use this file to assume a role with a web identity.
193217

194218
You can configure your workflow as follows in order to use this file:
195219
```yaml

dist/index.js

+4,133-97
Large diffs are not rendered by default.

index.js

+75-17
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ const aws = require('aws-sdk');
33
const assert = require('assert');
44
const fs = require('fs');
55
const path = require('path');
6+
const axios = require('axios');
67

78
// The max time that a GitHub action is allowed to run is 6 hours.
89
// That seems like a reasonable default to use if no role duration is defined.
910
const MAX_ACTION_RUNTIME = 6 * 3600;
11+
const DEFAULT_ROLE_DURATION_FOR_OIDC_ROLES = 3600;
1012
const USER_AGENT = 'configure-aws-credentials-for-github-actions';
1113
const MAX_TAG_VALUE_LENGTH = 256;
1214
const SANITIZATION_CHARACTER = '_';
@@ -25,10 +27,11 @@ async function assumeRole(params) {
2527
roleSessionName,
2628
region,
2729
roleSkipSessionTagging,
28-
webIdentityTokenFile
30+
webIdentityTokenFile,
31+
webIdentityToken
2932
} = params;
3033
assert(
31-
[sourceAccountId, roleToAssume, roleDurationSeconds, roleSessionName, region].every(isDefined),
34+
[roleToAssume, roleDurationSeconds, roleSessionName, region].every(isDefined),
3235
"Missing required input when assuming a Role."
3336
);
3437

@@ -43,6 +46,10 @@ async function assumeRole(params) {
4346
let roleArn = roleToAssume;
4447
if (!roleArn.startsWith('arn:aws')) {
4548
// Supports only 'aws' partition. Customers in other partitions ('aws-cn') will need to provide full ARN
49+
assert(
50+
isDefined(sourceAccountId),
51+
"Source Account ID is needed if the Role Name is provided and not the Role Arn."
52+
);
4653
roleArn = `arn:aws:iam::${sourceAccountId}:role/${roleArn}`;
4754
}
4855

@@ -79,9 +86,15 @@ async function assumeRole(params) {
7986
}
8087

8188
let assumeFunction = sts.assumeRole.bind(sts);
89+
90+
// These are customizations needed for the GH OIDC Provider
91+
if(isDefined(webIdentityToken)) {
92+
delete assumeRoleRequest.Tags;
8293

83-
if(isDefined(webIdentityTokenFile)) {
84-
core.debug("webIdentityTokenFile provided. Will call sts:AssumeRoleWithWebIdentity and take session tags from token contents.")
94+
assumeRoleRequest.WebIdentityToken = webIdentityToken;
95+
assumeFunction = sts.assumeRoleWithWebIdentity.bind(sts);
96+
} else if(isDefined(webIdentityTokenFile)) {
97+
core.debug("webIdentityTokenFile provided. Will call sts:AssumeRoleWithWebIdentity and take session tags from token contents.");
8598
delete assumeRoleRequest.Tags;
8699

87100
const webIdentityTokenFilePath = path.isAbsolute(webIdentityTokenFile) ?
@@ -172,6 +185,21 @@ async function exportAccountId(maskAccountId, region) {
172185
return accountId;
173186
}
174187

188+
async function getWebIdentityToken() {
189+
const isDefined = i => !!i;
190+
const {ACTIONS_ID_TOKEN_REQUEST_URL, ACTIONS_ID_TOKEN_REQUEST_TOKEN} = process.env;
191+
192+
assert(
193+
[ACTIONS_ID_TOKEN_REQUEST_URL, ACTIONS_ID_TOKEN_REQUEST_TOKEN].every(isDefined),
194+
'Missing required environment value. Are you running in GitHub Actions?'
195+
);
196+
const { data } = await axios.get(`${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=sigstore`, {
197+
headers: {"Authorization": `bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}`}
198+
}
199+
);
200+
return data.value;
201+
}
202+
175203
function loadCredentials() {
176204
// Force the SDK to re-resolve credentials with the default provider chain.
177205
//
@@ -234,18 +262,28 @@ async function run() {
234262
const maskAccountId = core.getInput('mask-aws-account-id', { required: false });
235263
const roleToAssume = core.getInput('role-to-assume', {required: false});
236264
const roleExternalId = core.getInput('role-external-id', { required: false });
237-
const roleDurationSeconds = core.getInput('role-duration-seconds', {required: false}) || MAX_ACTION_RUNTIME;
265+
let roleDurationSeconds = core.getInput('role-duration-seconds', {required: false}) || MAX_ACTION_RUNTIME;
238266
const roleSessionName = core.getInput('role-session-name', { required: false }) || ROLE_SESSION_NAME;
239267
const roleSkipSessionTaggingInput = core.getInput('role-skip-session-tagging', { required: false })|| 'false';
240268
const roleSkipSessionTagging = roleSkipSessionTaggingInput.toLowerCase() === 'true';
241-
const webIdentityTokenFile = core.getInput('web-identity-token-file', { required: false })
269+
const webIdentityTokenFile = core.getInput('web-identity-token-file', { required: false });
242270

243271
if (!region.match(REGION_REGEX)) {
244272
throw new Error(`Region is not valid: ${region}`);
245273
}
246274

247275
exportRegion(region);
248276

277+
// This wraps the logic for deciding if we should rely on the GH OIDC provider since we may need to reference
278+
// the decision in a few differennt places. Consolidating it here makes the logic clearer elsewhere.
279+
const useGitHubOIDCProvider = () => {
280+
// The assumption here is that self-hosted runners won't be populating the `ACTIONS_ID_TOKEN_REQUEST_TOKEN`
281+
// environment variable and they won't be providing a web idenity token file or access key either.
282+
// V2 of the action might relax this a bit and create an explicit precedence for these so that customers
283+
// can provide as much info as they want and we will follow the established credential loading precedence.
284+
return roleToAssume && process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN && !accessKeyId && !webIdentityTokenFile
285+
}
286+
249287
// Always export the source credentials and account ID.
250288
// The STS client for calling AssumeRole pulls creds from the environment.
251289
// Plus, in the assume role case, if the AssumeRole call fails, we want
@@ -258,15 +296,26 @@ async function run() {
258296

259297
exportCredentials({accessKeyId, secretAccessKey, sessionToken});
260298
}
261-
262-
// Regardless of whether any source credentials were provided as inputs,
263-
// validate that the SDK can actually pick up credentials. This validates
264-
// cases where this action is on a self-hosted runner that doesn't have credentials
265-
// configured correctly, and cases where the user intended to provide input
266-
// credentials but the secrets inputs resolved to empty strings.
267-
await validateCredentials(accessKeyId);
268-
269-
const sourceAccountId = await exportAccountId(maskAccountId, region);
299+
300+
// Attempt to load credentials from the GitHub OIDC provider.
301+
// If a user provides an IAM Role Arn and DOESN'T provide an Access Key Id
302+
// The only way to assume the role is via GitHub's OIDC provider.
303+
let sourceAccountId;
304+
let webIdentityToken;
305+
if(useGitHubOIDCProvider()) {
306+
webIdentityToken = await getWebIdentityToken();
307+
roleDurationSeconds = core.getInput('role-duration-seconds', {required: false}) || DEFAULT_ROLE_DURATION_FOR_OIDC_ROLES;
308+
// We don't validate the credentials here because we don't have them yet when using OIDC.
309+
} else {
310+
// Regardless of whether any source credentials were provided as inputs,
311+
// validate that the SDK can actually pick up credentials. This validates
312+
// cases where this action is on a self-hosted runner that doesn't have credentials
313+
// configured correctly, and cases where the user intended to provide input
314+
// credentials but the secrets inputs resolved to empty strings.
315+
await validateCredentials(accessKeyId);
316+
317+
sourceAccountId = await exportAccountId(maskAccountId, region);
318+
}
270319

271320
// Get role credentials if configured to do so
272321
if (roleToAssume) {
@@ -278,10 +327,19 @@ async function run() {
278327
roleDurationSeconds,
279328
roleSessionName,
280329
roleSkipSessionTagging,
281-
webIdentityTokenFile
330+
webIdentityTokenFile,
331+
webIdentityToken
282332
});
283333
exportCredentials(roleCredentials);
284-
await validateCredentials(roleCredentials.accessKeyId);
334+
// I don't know a good workaround for this. I'm not sure why we're validating the credentials
335+
// so frequently inside the action. The approach I've taken here is that if the GH OIDC token
336+
// isn't set, then we're in a self-hosted runner and we need to validate the credentials for
337+
// some mysterious reason that wasn't explained by whoever wrote this aciton.
338+
//
339+
// It's gross but it works so ... ¯\_(ツ)_/¯
340+
if (!process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN) {
341+
await validateCredentials(roleCredentials.accessKeyId);
342+
}
285343
await exportAccountId(maskAccountId, region);
286344
}
287345
}

0 commit comments

Comments
 (0)