-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Prevent volatile physical name generation (#2984)
The PhysicalName generation strategy for cross-account/region use-cases could generate names that are subject to collisions/sniping when the account and region were not specified; and those names would also have changed if a stack went from re-locatable to account/region specific, even if the target environment would not have changed. The generator will now throw errors in case the account ID or region is blank or a Token.
- Loading branch information
1 parent
f954128
commit af2680c
Showing
4 changed files
with
118 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/@aws-cdk/core/test/private/test.physical-name-generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import nodeunit = require('nodeunit'); | ||
import { App, Aws, Resource, Stack } from '../../lib'; | ||
import { generatePhysicalName } from '../../lib/private/physical-name-generator'; | ||
|
||
export = nodeunit.testCase({ | ||
'generates correct physical names'(test: nodeunit.Test) { | ||
const app = new App(); | ||
const stack = new Stack(app, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } }); | ||
|
||
const testResourceA = new TestResource(stack, 'A'); | ||
const testResourceB = new TestResource(testResourceA, 'B'); | ||
|
||
test.equal(generatePhysicalName(testResourceA), 'teststackteststackaa164c141d59b37c1b663'); | ||
test.equal(generatePhysicalName(testResourceB), 'teststackteststackab27595cd34d8188283a1f'); | ||
|
||
test.done(); | ||
}, | ||
|
||
'generates different names in different accounts'(test: nodeunit.Test) { | ||
const appA = new App(); | ||
const stackA = new Stack(appA, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } }); | ||
const resourceA = new TestResource(stackA, 'Resource'); | ||
|
||
const appB = new App(); | ||
const stackB = new Stack(appB, 'TestStack', { env: { account: '012345678913', region: 'bermuda-triangle-1' } }); | ||
const resourceB = new TestResource(stackB, 'Resource'); | ||
|
||
test.notEqual(generatePhysicalName(resourceA), generatePhysicalName(resourceB)); | ||
|
||
test.done(); | ||
}, | ||
|
||
'generates different names in different regions'(test: nodeunit.Test) { | ||
const appA = new App(); | ||
const stackA = new Stack(appA, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } }); | ||
const resourceA = new TestResource(stackA, 'Resource'); | ||
|
||
const appB = new App(); | ||
const stackB = new Stack(appB, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-2' } }); | ||
const resourceB = new TestResource(stackB, 'Resource'); | ||
|
||
test.notEqual(generatePhysicalName(resourceA), generatePhysicalName(resourceB)); | ||
|
||
test.done(); | ||
}, | ||
|
||
'fails when the region is an unresolved token'(test: nodeunit.Test) { | ||
const app = new App(); | ||
const stack = new Stack(app, 'TestStack', { env: { account: '012345678912', region: Aws.REGION } }); | ||
const testResource = new TestResource(stack, 'A'); | ||
|
||
test.throws(() => generatePhysicalName(testResource), | ||
/Cannot generate a physical name for TestStack\/A, because the region is un-resolved or missing/); | ||
|
||
test.done(); | ||
}, | ||
|
||
'fails when the region is not provided'(test: nodeunit.Test) { | ||
const app = new App(); | ||
const stack = new Stack(app, 'TestStack', { env: { account: '012345678912' } }); | ||
const testResource = new TestResource(stack, 'A'); | ||
|
||
test.throws(() => generatePhysicalName(testResource), | ||
/Cannot generate a physical name for TestStack\/A, because the region is un-resolved or missing/); | ||
|
||
test.done(); | ||
}, | ||
|
||
'fails when the account is an unresolved token'(test: nodeunit.Test) { | ||
const app = new App(); | ||
const stack = new Stack(app, 'TestStack', { env: { account: Aws.ACCOUNT_ID, region: 'bermuda-triangle-1' } }); | ||
const testResource = new TestResource(stack, 'A'); | ||
|
||
test.throws(() => generatePhysicalName(testResource), | ||
/Cannot generate a physical name for TestStack\/A, because the account is un-resolved or missing/); | ||
|
||
test.done(); | ||
}, | ||
|
||
'fails when the account is not provided'(test: nodeunit.Test) { | ||
const app = new App(); | ||
const stack = new Stack(app, 'TestStack', { env: { region: 'bermuda-triangle-1' } }); | ||
const testResource = new TestResource(stack, 'A'); | ||
|
||
test.throws(() => generatePhysicalName(testResource), | ||
/Cannot generate a physical name for TestStack\/A, because the account is un-resolved or missing/); | ||
|
||
test.done(); | ||
}, | ||
}); | ||
|
||
class TestResource extends Resource {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters