Skip to content

Commit

Permalink
fix(core): allow empty string components in parseArn (#5875)
Browse files Browse the repository at this point in the history
fixes #5808

BREAKING CHANGE: Arn.parseArn now returns empty string for nullable Arn
components. Users who were depending on an undefined value will now
receive the falsy empty string.
  • Loading branch information
melchii authored and mergify[bot] committed Jan 20, 2020
1 parent ad2b714 commit 5ed5eb4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/@aws-cdk/core/lib/arn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ export class Arn {
resourceName += rest.join(':');
}

// "|| undefined" will cause empty strings to be treated as "undefined"
// "|| undefined" will cause empty strings to be treated as "undefined".
// Optional ARN attributes (e.g. region, account) should return as empty string
// if they are provided as such.
return filterUndefined({
service: service || undefined,
resource: resource || undefined ,
resource: resource || undefined,
partition: partition || undefined,
region: region || undefined,
account: account || undefined,
region,
account,
resourceName,
sep
});
Expand Down
23 changes: 23 additions & 0 deletions packages/@aws-cdk/core/test/test.arn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,33 @@ export = {
partition: 'aws',
service: 'apigateway',
region: 'us-east-1',
account: '',
resource: 'a123456789012bc3de45678901f23a45',
sep: ':',
resourceName: '/test/mydemoresource/*'
},
'arn:aws-cn:cloud9::123456789012:environment:81e900317347585a0601e04c8d52eaEX': {
partition: 'aws-cn',
service: 'cloud9',
region: '',
account: '123456789012',
resource: 'environment',
resourceName: '81e900317347585a0601e04c8d52eaEX',
sep: ':'
},
'arn::cognito-sync:::identitypool/us-east-1:1a1a1a1a-ffff-1111-9999-12345678:bla': {
service: 'cognito-sync',
region: '',
account: '',
resource: 'identitypool',
resourceName: 'us-east-1:1a1a1a1a-ffff-1111-9999-12345678:bla',
sep: '/'
},
'arn:aws:s3:::my_corporate_bucket': {
partition: 'aws',
service: 's3',
region: '',
account: '',
resource: 'my_corporate_bucket'
}
};
Expand Down Expand Up @@ -202,6 +208,23 @@ export = {
// tslint:disable-next-line:max-line-length
test.deepEqual(stack.resolve(parsed.resourceName), { 'Fn::Select': [ 1, { 'Fn::Split': [ '/', { 'Fn::Select': [ 5, { 'Fn::Split': [ ':', theToken ]} ]} ]} ]});

test.done();
},

'returns empty string ARN components'(test: Test) {
const stack = new Stack();
const arn = 'arn:aws:iam::123456789012:role/abc123';
const expected: ArnComponents = {
partition: 'aws',
service: 'iam',
region: '',
account: '123456789012',
resource: 'role',
resourceName: 'abc123',
sep: '/'
};

test.deepEqual(stack.parseArn(arn), expected, arn);
test.done();
}
},
Expand Down

0 comments on commit 5ed5eb4

Please sign in to comment.