Skip to content

Commit baf85d8

Browse files
fix: Make tagging optional (#92)
* fix:making role session tagging optional * test:improve test coverage Add test to cover error thrown if access key exists and no secret key provided * docs: Update README.md Add details about skipping session tagging during role assumption Co-authored-by: KeifferCulbreth <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent b3a87c1 commit baf85d8

File tree

4 files changed

+99
-11
lines changed

4 files changed

+99
-11
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ The session will have the name "GitHubActions" and be tagged with the following
157157

158158
_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 charcters, the characters will be replaced with an '*'._
159159

160+
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:
161+
162+
```yaml
163+
uses: aws-actions/configure-aws-credentials@v1
164+
with:
165+
role-skip-session-tagging: true
166+
```
167+
160168
## Self-Hosted Runners
161169

162170
If you run your GitHub Actions in a [self-hosted runner](https://help.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners) that already has access to AWS credentials, such as an EC2 instance, then you do not need to provide IAM user access key credentials to this action.

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ inputs:
4343
role-external-id:
4444
description: 'The external ID of the role to assume'
4545
required: false
46+
role-skip-session-tagging:
47+
description: 'Skip session tagging during role assumption'
48+
required: false
4649
outputs:
4750
aws-account-id:
4851
description: 'The AWS account ID for the provided credentials'

index.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function assumeRole(params) {
2222
roleDurationSeconds,
2323
roleSessionName,
2424
region,
25+
roleSkipSessionTagging
2526
} = params;
2627
assert(
2728
[sourceAccountId, roleToAssume, roleDurationSeconds, roleSessionName, region].every(isDefined),
@@ -41,20 +42,23 @@ async function assumeRole(params) {
4142
// Supports only 'aws' partition. Customers in other partitions ('aws-cn') will need to provide full ARN
4243
roleArn = `arn:aws:iam::${sourceAccountId}:role/${roleArn}`;
4344
}
45+
const tagArray = [
46+
{Key: 'GitHub', Value: 'Actions'},
47+
{Key: 'Repository', Value: GITHUB_REPOSITORY},
48+
{Key: 'Workflow', Value: sanitizeGithubWorkflowName(GITHUB_WORKFLOW)},
49+
{Key: 'Action', Value: GITHUB_ACTION},
50+
{Key: 'Actor', Value: sanitizeGithubActor(GITHUB_ACTOR)},
51+
{Key: 'Branch', Value: GITHUB_REF},
52+
{Key: 'Commit', Value: GITHUB_SHA},
53+
];
54+
55+
const roleSessionTags = roleSkipSessionTagging ? undefined : tagArray;
4456

4557
const assumeRoleRequest = {
4658
RoleArn: roleArn,
4759
RoleSessionName: roleSessionName,
4860
DurationSeconds: roleDurationSeconds,
49-
Tags: [
50-
{Key: 'GitHub', Value: 'Actions'},
51-
{Key: 'Repository', Value: GITHUB_REPOSITORY},
52-
{Key: 'Workflow', Value: sanitizeGithubWorkflowName(GITHUB_WORKFLOW)},
53-
{Key: 'Action', Value: GITHUB_ACTION},
54-
{Key: 'Actor', Value: sanitizeGithubActor(GITHUB_ACTOR)},
55-
{Key: 'Branch', Value: GITHUB_REF},
56-
{Key: 'Commit', Value: GITHUB_SHA},
57-
]
61+
Tags: roleSessionTags
5862
};
5963

6064
if (roleExternalId) {
@@ -196,7 +200,8 @@ async function run() {
196200
const roleExternalId = core.getInput('role-external-id', { required: false });
197201
const roleDurationSeconds = core.getInput('role-duration-seconds', {required: false}) || MAX_ACTION_RUNTIME;
198202
const roleSessionName = core.getInput('role-session-name', { required: false }) || ROLE_SESSION_NAME;
199-
203+
const roleSkipSessionTagging = core.getInput('role-skip-session-tagging', { required: false });
204+
200205
if (!region.match(REGION_REGEX)) {
201206
throw new Error(`Region is not valid: ${region}`);
202207
}
@@ -233,7 +238,8 @@ async function run() {
233238
roleToAssume,
234239
roleExternalId,
235240
roleDurationSeconds,
236-
roleSessionName
241+
roleSessionName,
242+
roleSkipSessionTagging
237243
});
238244
exportCredentials(roleCredentials);
239245
await validateCredentials(roleCredentials.accessKeyId);

index.test.js

+71
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@ describe('Configure AWS Credentials', () => {
276276
expect(core.setFailed).toHaveBeenCalledWith('Region is not valid: $AWS_REGION');
277277
});
278278

279+
test('throws error if access key id exists but missing secret access key', async () => {
280+
process.env.SHOW_STACK_TRACE = 'false';
281+
const inputsWIthoutSecretKey = {...ASSUME_ROLE_INPUTS}
282+
inputsWIthoutSecretKey["aws-secret-access-key"] = undefined
283+
core.getInput = jest
284+
.fn()
285+
.mockImplementation(mockGetInput(inputsWIthoutSecretKey));
286+
287+
await run();
288+
expect(core.setFailed).toHaveBeenCalledWith("'aws-secret-access-key' must be provided if 'aws-access-key-id' is provided");
289+
290+
});
291+
279292
test('can opt out of masking account ID', async () => {
280293
const mockInputs = {...CREDS_INPUTS, 'aws-region': 'us-east-1', 'mask-aws-account-id': 'false'};
281294
core.getInput = jest
@@ -523,4 +536,62 @@ describe('Configure AWS Credentials', () => {
523536
})
524537
});
525538

539+
test('skip tagging provided as true', async () => {
540+
core.getInput = jest
541+
.fn()
542+
.mockImplementation(mockGetInput({...ASSUME_ROLE_INPUTS, 'role-skip-session-tagging': true}));
543+
544+
await run();
545+
expect(mockStsAssumeRole).toHaveBeenCalledWith({
546+
RoleArn: ROLE_ARN,
547+
RoleSessionName: 'GitHubActions',
548+
DurationSeconds: 21600,
549+
Tags: undefined
550+
})
551+
});
552+
553+
test('skip tagging provided as false', async () => {
554+
core.getInput = jest
555+
.fn()
556+
.mockImplementation(mockGetInput({...ASSUME_ROLE_INPUTS, 'role-skip-session-tagging': false}));
557+
558+
await run();
559+
expect(mockStsAssumeRole).toHaveBeenCalledWith({
560+
RoleArn: ROLE_ARN,
561+
RoleSessionName: 'GitHubActions',
562+
DurationSeconds: 21600,
563+
Tags: [
564+
{Key: 'GitHub', Value: 'Actions'},
565+
{Key: 'Repository', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REPOSITORY},
566+
{Key: 'Workflow', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_WORKFLOW},
567+
{Key: 'Action', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_ACTION},
568+
{Key: 'Actor', Value: GITHUB_ACTOR_SANITIZED},
569+
{Key: 'Branch', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REF},
570+
{Key: 'Commit', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_SHA},
571+
]
572+
})
573+
});
574+
575+
test('skip tagging not provided', async () => {
576+
core.getInput = jest
577+
.fn()
578+
.mockImplementation(mockGetInput({...ASSUME_ROLE_INPUTS}));
579+
580+
await run();
581+
expect(mockStsAssumeRole).toHaveBeenCalledWith({
582+
RoleArn: ROLE_ARN,
583+
RoleSessionName: 'GitHubActions',
584+
DurationSeconds: 21600,
585+
Tags: [
586+
{Key: 'GitHub', Value: 'Actions'},
587+
{Key: 'Repository', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REPOSITORY},
588+
{Key: 'Workflow', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_WORKFLOW},
589+
{Key: 'Action', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_ACTION},
590+
{Key: 'Actor', Value: GITHUB_ACTOR_SANITIZED},
591+
{Key: 'Branch', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REF},
592+
{Key: 'Commit', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_SHA},
593+
]
594+
})
595+
});
596+
526597
});

0 commit comments

Comments
 (0)