diff --git a/dist/index.js b/dist/index.js index 9d11c31..f2f84d1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -166,6 +166,8 @@ module.exports = AWS.MediaLive; const core = __webpack_require__(6470); const aws = __webpack_require__(9350); const assert = __webpack_require__(2357); +const fs = __webpack_require__(5747); +const path = __webpack_require__(5622); // The max time that a GitHub action is allowed to run is 6 hours. // That seems like a reasonable default to use if no role duration is defined. @@ -187,7 +189,8 @@ async function assumeRole(params) { roleDurationSeconds, roleSessionName, region, - roleSkipSessionTagging + roleSkipSessionTagging, + webIdentityTokenFile } = params; assert( [sourceAccountId, roleToAssume, roleDurationSeconds, roleSessionName, region].every(isDefined), @@ -207,6 +210,7 @@ async function assumeRole(params) { // Supports only 'aws' partition. Customers in other partitions ('aws-cn') will need to provide full ARN roleArn = `arn:aws:iam::${sourceAccountId}:role/${roleArn}`; } + const tagArray = [ {Key: 'GitHub', Value: 'Actions'}, {Key: 'Repository', Value: GITHUB_REPOSITORY}, @@ -239,15 +243,38 @@ async function assumeRole(params) { assumeRoleRequest.ExternalId = roleExternalId; } - return sts.assumeRole(assumeRoleRequest) - .promise() - .then(function (data) { - return { - accessKeyId: data.Credentials.AccessKeyId, - secretAccessKey: data.Credentials.SecretAccessKey, - sessionToken: data.Credentials.SessionToken, - }; - }); + let assumeFunction = sts.assumeRole.bind(sts); + + if(isDefined(webIdentityTokenFile)) { + core.debug("webIdentityTokenFile provided. Will call sts:AssumeRoleWithWebIdentity and take session tags from token contents.") + delete assumeRoleRequest.Tags; + + const webIdentityTokenFilePath = path.isAbsolute(webIdentityTokenFile) ? + webIdentityTokenFile : + path.join(process.env.GITHUB_WORKSPACE, webIdentityTokenFile); + + if (!fs.existsSync(webIdentityTokenFilePath)) { + throw new Error(`Web identity token file does not exist: ${webIdentityTokenFilePath}`); + } + + try { + assumeRoleRequest.WebIdentityToken = await fs.promises.readFile(webIdentityTokenFilePath, 'utf8'); + assumeFunction = sts.assumeRoleWithWebIdentity.bind(sts); + } catch(error) { + throw new Error(`Web identity token file could not be read: ${error.message}`); + } + + } + + return assumeFunction(assumeRoleRequest) + .promise() + .then(function (data) { + return { + accessKeyId: data.Credentials.AccessKeyId, + secretAccessKey: data.Credentials.SecretAccessKey, + sessionToken: data.Credentials.SessionToken, + }; + }); } function sanitizeGithubActor(actor) { @@ -376,6 +403,7 @@ async function run() { const roleSessionName = core.getInput('role-session-name', { required: false }) || ROLE_SESSION_NAME; const roleSkipSessionTaggingInput = core.getInput('role-skip-session-tagging', { required: false })|| 'false'; const roleSkipSessionTagging = roleSkipSessionTaggingInput.toLowerCase() === 'true'; + const webIdentityTokenFile = core.getInput('web-identity-token-file', { required: false }) if (!region.match(REGION_REGEX)) { throw new Error(`Region is not valid: ${region}`); @@ -414,7 +442,8 @@ async function run() { roleExternalId, roleDurationSeconds, roleSessionName, - roleSkipSessionTagging + roleSkipSessionTagging, + webIdentityTokenFile }); exportCredentials(roleCredentials); await validateCredentials(roleCredentials.accessKeyId);