Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Prevent volatile physical name generation #2984

Merged
merged 7 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/@aws-cdk/cdk/lib/private/physical-name-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export function generatePhysicalName(resource: IResource): string {
const stackPart = new PrefixNamePart(stack.stackName, 25);
const idPart = new SuffixNamePart(resource.node.uniqueId, 24);

let region: string | undefined = stack.region;
if (Token.isUnresolved(region)) {
region = undefined;
const region: string = stack.region;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically stack.region/account will never be undefined but that’s ok

if (Token.isUnresolved(region) || !region) {
throw new Error(`Cannot generate a physical name for ${resource.node.path}, because the region is un-resolved or missing`);
}

let account: string | undefined = stack.account;
if (Token.isUnresolved(account)) {
account = undefined;
const account: string = stack.account;
if (Token.isUnresolved(account) || !account) {
throw new Error(`Cannot generate a physical name for ${resource.node.path}, because the account is un-resolved or missing`);
}

const parts = [stackPart, idPart]
Expand All @@ -25,8 +25,8 @@ export function generatePhysicalName(resource: IResource): string {
const sha256 = crypto.createHash('sha256')
.update(stackPart.bareStr)
.update(idPart.bareStr)
.update(region || '')
.update(account || '');
.update(region)
.update(account);
const hash = sha256.digest('hex').slice(0, hashLength);

const ret = [...parts, hash].join('');
Expand Down
14 changes: 12 additions & 2 deletions packages/@aws-cdk/cdk/test/test.cross-environment-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export = {
const stack1 = new Stack(app, 'Stack1', {
env: {
account: '123456789012',
region: 'bermuda-triangle-1337',
},
});
const myResource = new MyResource(stack1, 'MyResource', PhysicalName.of('PhysicalName'));

const stack2 = new Stack(app, 'Stack2', {
env: {
account: '234567890123',
region: 'bermuda-triangle-42',
},
});

Expand Down Expand Up @@ -56,11 +58,13 @@ export = {
const stack1 = new Stack(app, 'Stack1', {
env: {
account: '123456789012',
region: 'bermuda-triangle-1337',
},
});
const stack2 = new Stack(app, 'Stack2', {
env: {
account: '234567890123',
region: 'bermuda-triangle-42',
},
});

Expand Down Expand Up @@ -88,13 +92,15 @@ export = {
const stack1 = new Stack(app, 'Stack1', {
env: {
account: '123456789012',
region: 'bermuda-triangle-1337',
},
});
const myResource = new MyResource(stack1, 'MyResource', PhysicalName.auto({ crossEnvironment: true }));

const stack2 = new Stack(app, 'Stack2', {
env: {
account: '234567890123',
region: 'bermuda-triangle-42',
},
});

Expand All @@ -115,7 +121,7 @@ export = {
{
Ref: 'AWS::Partition',
},
':myservice:::my-resource/stack1stack1myresourcec54ced43dab875fcfa49',
':myservice:::my-resource/stack1stack1myresourcec54ced43683ebf9a3c4c',
],
],
},
Expand All @@ -132,11 +138,13 @@ export = {
const stack1 = new Stack(app, 'Stack1', {
env: {
account: '123456789012',
region: 'bermuda-triangle-1337',
},
});
const stack2 = new Stack(app, 'Stack2', {
env: {
account: '234567890123',
region: 'bermuda-triangle-42',
},
});

Expand All @@ -150,7 +158,7 @@ export = {
test.deepEqual(toCloudFormation(stack2), {
Outputs: {
Output: {
Value: 'stack1stack1myresourcec54ced43dab875fcfa49',
Value: 'stack1stack1myresourcec54ced43683ebf9a3c4c',
},
},
});
Expand All @@ -165,11 +173,13 @@ export = {
const stack1 = new Stack(app, 'Stack1', {
env: {
account: '123456789012',
region: 'bermuda-triangle-1337',
},
});
const stack2 = new Stack(app, 'Stack2', {
env: {
account: '234567890123',
region: 'bermuda-triangle-42',
},
});

Expand Down