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): undeployable due to invalid mapping #18922

Merged
merged 3 commits into from Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -291,7 +291,7 @@
{
"Ref": "AWS::Region"
},
"1_0_98_0_x86_64"
"1x0x98x0xx86x64"
]
}
],
Expand Down Expand Up @@ -364,7 +364,7 @@
{
"Ref": "AWS::Region"
},
"1_0_119_0_x86_64"
"1x0x119x0xx86x64"
]
}
],
Expand Down Expand Up @@ -457,92 +457,92 @@
"Mappings": {
"CloudwatchlambdainsightsversionMap": {
"af-south-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:af-south-1:012438385374:layer:LambdaInsightsExtension:8",
"1_0_119_0_x86_64": "arn:aws:lambda:af-south-1:012438385374:layer:LambdaInsightsExtension:9"
"1x0x98x0xx86x64": "arn:aws:lambda:af-south-1:012438385374:layer:LambdaInsightsExtension:8",
"1x0x119x0xx86x64": "arn:aws:lambda:af-south-1:012438385374:layer:LambdaInsightsExtension:9"
},
"ap-east-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:ap-east-1:519774774795:layer:LambdaInsightsExtension:8",
"1_0_119_0_x86_64": "arn:aws:lambda:ap-east-1:519774774795:layer:LambdaInsightsExtension:9"
"1x0x98x0xx86x64": "arn:aws:lambda:ap-east-1:519774774795:layer:LambdaInsightsExtension:8",
"1x0x119x0xx86x64": "arn:aws:lambda:ap-east-1:519774774795:layer:LambdaInsightsExtension:9"
},
"ap-northeast-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:ap-northeast-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:ap-northeast-1:580247275435:layer:LambdaInsightsExtension:23"
"1x0x98x0xx86x64": "arn:aws:lambda:ap-northeast-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:ap-northeast-1:580247275435:layer:LambdaInsightsExtension:23"
},
"ap-northeast-2": {
"1_0_98_0_x86_64": "arn:aws:lambda:ap-northeast-2:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:ap-northeast-2:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:ap-northeast-2:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:ap-northeast-2:580247275435:layer:LambdaInsightsExtension:16"
},
"ap-south-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension:16"
},
"ap-southeast-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:ap-southeast-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:ap-southeast-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:ap-southeast-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:ap-southeast-1:580247275435:layer:LambdaInsightsExtension:16"
},
"ap-southeast-2": {
"1_0_98_0_x86_64": "arn:aws:lambda:ap-southeast-2:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:ap-southeast-2:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:ap-southeast-2:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:ap-southeast-2:580247275435:layer:LambdaInsightsExtension:16"
},
"ca-central-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:ca-central-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:ca-central-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:ca-central-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:ca-central-1:580247275435:layer:LambdaInsightsExtension:16"
},
"cn-north-1": {
"1_0_98_0_x86_64": "arn:aws-cn:lambda:cn-north-1:488211338238:layer:LambdaInsightsExtension:8",
"1_0_119_0_x86_64": "arn:aws-cn:lambda:cn-north-1:488211338238:layer:LambdaInsightsExtension:9"
"1x0x98x0xx86x64": "arn:aws-cn:lambda:cn-north-1:488211338238:layer:LambdaInsightsExtension:8",
"1x0x119x0xx86x64": "arn:aws-cn:lambda:cn-north-1:488211338238:layer:LambdaInsightsExtension:9"
},
"cn-northwest-1": {
"1_0_98_0_x86_64": "arn:aws-cn:lambda:cn-northwest-1:488211338238:layer:LambdaInsightsExtension:8",
"1_0_119_0_x86_64": "arn:aws-cn:lambda:cn-northwest-1:488211338238:layer:LambdaInsightsExtension:9"
"1x0x98x0xx86x64": "arn:aws-cn:lambda:cn-northwest-1:488211338238:layer:LambdaInsightsExtension:8",
"1x0x119x0xx86x64": "arn:aws-cn:lambda:cn-northwest-1:488211338238:layer:LambdaInsightsExtension:9"
},
"eu-central-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:eu-central-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:eu-central-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:eu-central-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:eu-central-1:580247275435:layer:LambdaInsightsExtension:16"
},
"eu-north-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:eu-north-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:eu-north-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:eu-north-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:eu-north-1:580247275435:layer:LambdaInsightsExtension:16"
},
"eu-south-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:eu-south-1:339249233099:layer:LambdaInsightsExtension:8",
"1_0_119_0_x86_64": "arn:aws:lambda:eu-south-1:339249233099:layer:LambdaInsightsExtension:9"
"1x0x98x0xx86x64": "arn:aws:lambda:eu-south-1:339249233099:layer:LambdaInsightsExtension:8",
"1x0x119x0xx86x64": "arn:aws:lambda:eu-south-1:339249233099:layer:LambdaInsightsExtension:9"
},
"eu-west-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:eu-west-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:eu-west-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:eu-west-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:eu-west-1:580247275435:layer:LambdaInsightsExtension:16"
},
"eu-west-2": {
"1_0_98_0_x86_64": "arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:16"
},
"eu-west-3": {
"1_0_98_0_x86_64": "arn:aws:lambda:eu-west-3:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:eu-west-3:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:eu-west-3:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:eu-west-3:580247275435:layer:LambdaInsightsExtension:16"
},
"me-south-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:me-south-1:285320876703:layer:LambdaInsightsExtension:8",
"1_0_119_0_x86_64": "arn:aws:lambda:me-south-1:285320876703:layer:LambdaInsightsExtension:9"
"1x0x98x0xx86x64": "arn:aws:lambda:me-south-1:285320876703:layer:LambdaInsightsExtension:8",
"1x0x119x0xx86x64": "arn:aws:lambda:me-south-1:285320876703:layer:LambdaInsightsExtension:9"
},
"sa-east-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:sa-east-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:sa-east-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:sa-east-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:sa-east-1:580247275435:layer:LambdaInsightsExtension:16"
},
"us-east-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:16"
},
"us-east-2": {
"1_0_98_0_x86_64": "arn:aws:lambda:us-east-2:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:us-east-2:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:us-east-2:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:us-east-2:580247275435:layer:LambdaInsightsExtension:16"
},
"us-west-1": {
"1_0_98_0_x86_64": "arn:aws:lambda:us-west-1:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:us-west-1:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:us-west-1:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:us-west-1:580247275435:layer:LambdaInsightsExtension:16"
},
"us-west-2": {
"1_0_98_0_x86_64": "arn:aws:lambda:us-west-2:580247275435:layer:LambdaInsightsExtension:14",
"1_0_119_0_x86_64": "arn:aws:lambda:us-west-2:580247275435:layer:LambdaInsightsExtension:16"
"1x0x98x0xx86x64": "arn:aws:lambda:us-west-2:580247275435:layer:LambdaInsightsExtension:14",
"1x0x119x0xx86x64": "arn:aws:lambda:us-west-2:580247275435:layer:LambdaInsightsExtension:16"
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/@aws-cdk/aws-lambda/test/lambda-insights.test.ts
Expand Up @@ -97,7 +97,7 @@ describe('lambda-insights', () => {
{
Ref: 'AWS::Region',
},
'1_0_98_0_x86_64',
'1x0x98x0xx86x64',
],
}],
});
Expand All @@ -117,20 +117,20 @@ describe('lambda-insights', () => {
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
FunctionName: 'MyLambda1',
Layers: [{
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1_0_98_0_x86_64'],
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1x0x98x0xx86x64'],
}],
});

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
FunctionName: 'MyLambda2',
Layers: [{
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1_0_98_0_x86_64'],
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1x0x98x0xx86x64'],
}],
});

Template.fromStack(stack).hasMapping('CloudwatchlambdainsightsversionMap', {
'af-south-1': {
'1_0_98_0_x86_64': 'arn:aws:lambda:af-south-1:012438385374:layer:LambdaInsightsExtension:8',
'1x0x98x0xx86x64': 'arn:aws:lambda:af-south-1:012438385374:layer:LambdaInsightsExtension:8',
},
});

Expand Down Expand Up @@ -218,21 +218,21 @@ describe('lambda-insights', () => {
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
FunctionName: 'MyLambda1',
Layers: [{
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1_0_119_0_x86_64'],
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1x0x119x0xx86x64'],
}],
});

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
FunctionName: 'MyLambda2',
Layers: [{
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1_0_119_0_arm64'],
'Fn::FindInMap': ['CloudwatchlambdainsightsversionMap', { Ref: 'AWS::Region' }, '1x0x119x0xarm64'],
}],
});

Template.fromStack(stack).hasMapping('CloudwatchlambdainsightsversionMap', {
'ap-south-1': {
'1_0_119_0_x86_64': 'arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension:16',
'1_0_119_0_arm64': 'arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension-Arm64:1',
'1x0x119x0xx86x64': 'arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension:16',
'1x0x119x0xarm64': 'arn:aws:lambda:ap-south-1:580247275435:layer:LambdaInsightsExtension-Arm64:1',
},
});

Expand Down
16 changes: 15 additions & 1 deletion packages/@aws-cdk/core/lib/cfn-mapping.ts
Expand Up @@ -43,14 +43,16 @@ export class CfnMapping extends CfnRefElement {

constructor(scope: Construct, id: string, props: CfnMappingProps = {}) {
super(scope, id);
this.mapping = props.mapping ?? { };
this.mapping = props.mapping ? this.validateMapping(props.mapping) : {};
this.lazy = props.lazy;
}

/**
* Sets a value in the map based on the two keys.
*/
public setValue(key1: string, key2: string, value: any) {
this.validateAlphanumeric(key2);

if (!(key1 in this.mapping)) {
this.mapping[key1] = { };
}
Expand Down Expand Up @@ -108,4 +110,16 @@ export class CfnMapping extends CfnRefElement {
}
this.lazyInformed = true;
}

private validateMapping(mapping: Mapping): Mapping {
Object.keys(mapping).forEach((m) => Object.keys(mapping[m]).forEach(this.validateAlphanumeric));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this matters other than for testing but -- the complexity of this function looks to be O(nm). Can't think of a better way to do it but I'm curious if this is prohibitive (I feel like its ok).

return mapping;
}

private validateAlphanumeric(value: any) {
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html
if (value.match(/[^a-zA-Z0-9]/g)) {
throw new Error(`Attribute name '${value}' must contain only alphanumeric characters.`);
}
}
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/private/region-lookup.ts
Expand Up @@ -31,7 +31,7 @@ export function deployTimeLookup(stack: Stack, factName: string, lookupMap: Reco
: [factName, 'value'] as const;

const mapId = `${ucfirst(factClass)}Map`;
const factKey = factParam.replace(/[^a-zA-Z0-9]/g, '_');
const factKey = factParam.replace(/[^a-zA-Z0-9]/g, 'x');

let mapping = stack.node.tryFindChild(mapId) as CfnMapping | undefined;
if (!mapping) {
Expand Down
42 changes: 24 additions & 18 deletions packages/@aws-cdk/core/test/mappings.test.ts
Expand Up @@ -58,35 +58,32 @@ describe('mappings', () => {
},
},
});


});

test('allow using unresolved tokens in find-in-map', () => {
const stack = new Stack();

const mapping = new CfnMapping(stack, 'mapping', {
mapping: {
instanceCount: {
'us-east-1': 12,
'us-east-1': {
instanceCount: 12,
},
},
});

const v1 = mapping.findInMap('instanceCount', Aws.REGION);
const v2 = Fn.findInMap(mapping.logicalId, 'instanceCount', Aws.REGION);
const v1 = mapping.findInMap(Aws.REGION, 'instanceCount');
const v2 = Fn.findInMap(mapping.logicalId, Aws.REGION, 'instanceCount');

const expected = { 'Fn::FindInMap': ['mapping', 'instanceCount', { Ref: 'AWS::Region' }] };
const expected = { 'Fn::FindInMap': ['mapping', { Ref: 'AWS::Region' }, 'instanceCount'] };
expect(stack.resolve(v1)).toEqual(expected);
expect(stack.resolve(v2)).toEqual(expected);
expect(toCloudFormation(stack).Mappings).toEqual({
mapping: {
instanceCount: {
'us-east-1': 12,
'us-east-1': {
instanceCount: 12,
},
},
});

});

test('no validation if first key is token and second is a static string', () => {
Expand Down Expand Up @@ -114,34 +111,43 @@ describe('mappings', () => {
},
},
});

});

test('validate first key if it is a string and second is a token', () => {
// GIVEN
const stack = new Stack();
const mapping = new CfnMapping(stack, 'mapping', {
mapping: {
size: {
'us-east-1': 12,
'us-east-1': {
size: 12,
},
},
});

// WHEN
const v = mapping.findInMap('size', Aws.REGION);
const v = mapping.findInMap(Aws.REGION, 'size');

// THEN
expect(() => mapping.findInMap('not-found', Aws.REGION)).toThrow(/Mapping doesn't contain top-level key 'not-found'/);
expect(stack.resolve(v)).toEqual({ 'Fn::FindInMap': ['mapping', 'size', { Ref: 'AWS::Region' }] });
expect(() => mapping.findInMap('not-found', 'size')).toThrow(/Mapping doesn't contain top-level key 'not-found'/);
expect(stack.resolve(v)).toEqual({ 'Fn::FindInMap': ['mapping', { Ref: 'AWS::Region' }, 'size'] });
expect(toCloudFormation(stack).Mappings).toEqual({
mapping: {
size: {
'us-east-1': 12,
'us-east-1': {
size: 12,
},
},
});
});

test('throws if mapping attribute name not alphanumeric', () => {
const stack = new Stack();
expect(() => new CfnMapping(stack, 'mapping', {
mapping: {
size: {
'us-east-1': 12,
},
},
})).toThrowError(/Attribute name 'us-east-1' must contain only alphanumeric characters./);
});
});

Expand Down