Skip to content

Commit d3d137f

Browse files
committed
fix: Change role duration behavior
When a session token provided, use 1hr as role duration Otherwise, use the max duration of GitHub action (6hr)
1 parent 5a4b8f0 commit d3d137f

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const assert = require('assert');
44
const fs = require('fs');
55
const path = require('path');
66

7-
// The max time that a GitHub action is allowed to run is 6 hours.
8-
// That seems like a reasonable default to use if no role duration is defined.
7+
// When a session token provided, use 1hr as role duration
8+
// Otherwise, use the max duration of GitHub action (6hr)
99
const MAX_ACTION_RUNTIME = 6 * 3600;
10+
const SESSION_ROLE_DURATION = 3600;
1011
const DEFAULT_ROLE_DURATION_FOR_OIDC_ROLES = 3600;
1112
const USER_AGENT = 'configure-aws-credentials-for-github-actions';
1213
const MAX_TAG_VALUE_LENGTH = 256;
@@ -85,7 +86,7 @@ async function assumeRole(params) {
8586
}
8687

8788
let assumeFunction = sts.assumeRole.bind(sts);
88-
89+
8990
// These are customizations needed for the GH OIDC Provider
9091
if(isDefined(webIdentityToken)) {
9192
delete assumeRoleRequest.Tags;
@@ -110,8 +111,8 @@ async function assumeRole(params) {
110111
} catch(error) {
111112
throw new Error(`Web identity token file could not be read: ${error.message}`);
112113
}
113-
114-
}
114+
115+
}
115116

116117
return assumeFunction(assumeRoleRequest)
117118
.promise()
@@ -270,7 +271,9 @@ async function run() {
270271
const maskAccountId = core.getInput('mask-aws-account-id', { required: false });
271272
const roleToAssume = core.getInput('role-to-assume', {required: false});
272273
const roleExternalId = core.getInput('role-external-id', { required: false });
273-
let roleDurationSeconds = core.getInput('role-duration-seconds', {required: false}) || MAX_ACTION_RUNTIME;
274+
let roleDurationSeconds = core.getInput('role-duration-seconds', {required: false})
275+
|| (sessionToken && SESSION_ROLE_DURATION)
276+
|| MAX_ACTION_RUNTIME;
274277
const roleSessionName = core.getInput('role-session-name', { required: false }) || ROLE_SESSION_NAME;
275278
const roleSkipSessionTaggingInput = core.getInput('role-skip-session-tagging', { required: false })|| 'false';
276279
const roleSkipSessionTagging = roleSkipSessionTaggingInput.toLowerCase() === 'true';
@@ -304,7 +307,7 @@ async function run() {
304307

305308
exportCredentials({accessKeyId, secretAccessKey, sessionToken});
306309
}
307-
310+
308311
// Attempt to load credentials from the GitHub OIDC provider.
309312
// If a user provides an IAM Role Arn and DOESN'T provide an Access Key Id
310313
// The only way to assume the role is via GitHub's OIDC provider.

index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,28 @@ describe('Configure AWS Credentials', () => {
527527
})
528528
});
529529

530+
test('role assumption session token provided, no role assumption duration provided', async () => {
531+
core.getInput = jest
532+
.fn()
533+
.mockImplementation(mockGetInput({...ASSUME_ROLE_INPUTS, 'aws-session-token': FAKE_SESSION_TOKEN}));
534+
535+
await run();
536+
expect(mockStsAssumeRole).toHaveBeenCalledWith({
537+
RoleArn: ROLE_ARN,
538+
RoleSessionName: 'GitHubActions',
539+
DurationSeconds: 3600,
540+
Tags: [
541+
{Key: 'GitHub', Value: 'Actions'},
542+
{Key: 'Repository', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REPOSITORY},
543+
{Key: 'Workflow', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_WORKFLOW},
544+
{Key: 'Action', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_ACTION},
545+
{Key: 'Actor', Value: GITHUB_ACTOR_SANITIZED},
546+
{Key: 'Commit', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_SHA},
547+
{Key: 'Branch', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REF},
548+
]
549+
})
550+
});
551+
530552
test('role name provided instead of ARN', async () => {
531553
core.getInput = jest
532554
.fn()

0 commit comments

Comments
 (0)