Skip to content

Commit 96c6f7e

Browse files
authored
feat: infer role ARN if given role name (#35)
1 parent 4731257 commit 96c6f7e

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Diff for: index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ async function assumeRole(params) {
4242
accessKeyId, secretAccessKey, sessionToken, region, endpoint, customUserAgent: USER_AGENT
4343
});
4444

45+
let roleArn = roleToAssume;
46+
if (!roleArn.startsWith('arn:aws')) {
47+
const identity = await sts.getCallerIdentity().promise();
48+
const accountId = identity.Account;
49+
// Supports only 'aws' partition. Customers in other partitions ('aws-cn') will need to provide full ARN
50+
roleArn = `arn:aws:iam::${accountId}:role/${roleArn}`;
51+
}
52+
4553
const assumeRoleRequest = {
46-
RoleArn: roleToAssume,
54+
RoleArn: roleArn,
4755
RoleSessionName: roleSessionName,
4856
DurationSeconds: roleDurationSeconds,
4957
Tags: [

Diff for: index.test.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const FAKE_STS_SESSION_TOKEN = 'STS-AWS-SESSION-TOKEN';
1414
const FAKE_REGION = 'fake-region-1';
1515
const FAKE_ACCOUNT_ID = '123456789012';
1616
const ROLE_NAME = 'MY-ROLE';
17+
const ROLE_ARN = 'arn:aws:iam::123456789012:role/MY-ROLE';
1718
const ENVIRONMENT_VARIABLE_OVERRIDES = {
1819
SHOW_STACK_TRACE: 'true',
1920
GITHUB_REPOSITORY: 'MY-REPOSITORY-NAME',
@@ -40,7 +41,7 @@ const DEFAULT_INPUTS = {
4041
'aws-region': FAKE_REGION,
4142
'mask-aws-account-id': 'TRUE'
4243
};
43-
const ASSUME_ROLE_INPUTS = {...REQUIRED_INPUTS, 'role-to-assume': ROLE_NAME, 'aws-region': FAKE_REGION};
44+
const ASSUME_ROLE_INPUTS = {...REQUIRED_INPUTS, 'role-to-assume': ROLE_ARN, 'aws-region': FAKE_REGION};
4445

4546
const mockStsCallerIdentity = jest.fn();
4647
const mockStsAssumeRole = jest.fn();
@@ -201,7 +202,7 @@ describe('Configure AWS Credentials', () => {
201202

202203
await run();
203204
expect(mockStsAssumeRole).toHaveBeenCalledWith({
204-
RoleArn: ROLE_NAME,
205+
RoleArn: ROLE_ARN,
205206
RoleSessionName: 'GitHubActions',
206207
DurationSeconds: 6 * 3600,
207208
Tags: [
@@ -223,7 +224,7 @@ describe('Configure AWS Credentials', () => {
223224

224225
await run();
225226
expect(mockStsAssumeRole).toHaveBeenCalledWith({
226-
RoleArn: ROLE_NAME,
227+
RoleArn: ROLE_ARN,
227228
RoleSessionName: 'GitHubActions',
228229
DurationSeconds: 5,
229230
Tags: [
@@ -245,7 +246,7 @@ describe('Configure AWS Credentials', () => {
245246

246247
await run();
247248
expect(mockStsAssumeRole).toHaveBeenCalledWith({
248-
RoleArn: ROLE_NAME,
249+
RoleArn: ROLE_ARN,
249250
RoleSessionName: 'MySessionName',
250251
DurationSeconds: 6 * 3600,
251252
Tags: [
@@ -260,14 +261,36 @@ describe('Configure AWS Credentials', () => {
260261
})
261262
});
262263

264+
test('role name provided instead of ARN', async () => {
265+
core.getInput = jest
266+
.fn()
267+
.mockImplementation(mockGetInput({...REQUIRED_INPUTS, 'role-to-assume': ROLE_NAME, 'aws-region': FAKE_REGION}));
268+
269+
await run();
270+
expect(mockStsAssumeRole).toHaveBeenCalledWith({
271+
RoleArn: ROLE_ARN,
272+
RoleSessionName: 'GitHubActions',
273+
DurationSeconds: 6 * 3600,
274+
Tags: [
275+
{Key: 'GitHub', Value: 'Actions'},
276+
{Key: 'Repository', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REPOSITORY},
277+
{Key: 'Workflow', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_WORKFLOW},
278+
{Key: 'Action', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_ACTION},
279+
{Key: 'Actor', Value: GITHUB_ACTOR_SANITIZED},
280+
{Key: 'Branch', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_REF},
281+
{Key: 'Commit', Value: ENVIRONMENT_VARIABLE_OVERRIDES.GITHUB_SHA},
282+
]
283+
})
284+
});
285+
263286
test('role external ID provided', async () => {
264287
core.getInput = jest
265288
.fn()
266289
.mockImplementation(mockGetInput({...ASSUME_ROLE_INPUTS, 'role-external-id': 'abcdef'}));
267290

268291
await run();
269292
expect(mockStsAssumeRole).toHaveBeenCalledWith({
270-
RoleArn: ROLE_NAME,
293+
RoleArn: ROLE_ARN,
271294
RoleSessionName: 'GitHubActions',
272295
DurationSeconds: 6 * 3600,
273296
Tags: [
@@ -294,7 +317,7 @@ describe('Configure AWS Credentials', () => {
294317

295318
await run();
296319
expect(mockStsAssumeRole).toHaveBeenCalledWith({
297-
RoleArn: ROLE_NAME,
320+
RoleArn: ROLE_ARN,
298321
RoleSessionName: 'GitHubActions',
299322
DurationSeconds: 6 * 3600,
300323
Tags: [

0 commit comments

Comments
 (0)