Skip to content

Commit

Permalink
fix(core): autogenerated exports do not account for stack name length
Browse files Browse the repository at this point in the history
Autogenerated IDs are limited to 248 characters. However, for exports
these are combined with a stack name, which may make the combination
exceed the maximum length of 255 characters.

Account for the stack name length while generating these IDs.

Fixes #9733.
  • Loading branch information
rix0rrr committed Dec 7, 2020
1 parent 60875a5 commit 211671e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/core/lib/cfn-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,18 @@ export class CfnOutput extends CfnElement {
},
};
}

protected validate(): string[] {
if (this._exportName && !Token.isUnresolved(this._exportName) && this._exportName.length > 255) {
return [`Export name cannot exceed 255 characters (got ${this._exportName.length} characters)`];
}
return [];
}
}

import { CfnCondition } from './cfn-condition';
import { Fn } from './cfn-fn';
import { Lazy } from './lazy';
import { Stack } from './stack';
import { Token } from './token';

5 changes: 3 additions & 2 deletions packages/@aws-cdk/core/lib/private/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ function generateExportName(stackExports: Construct, id: string) {
id,
];
const prefix = stack.stackName ? stack.stackName + ':' : '';
const exportName = prefix + makeUniqueId(components);
return exportName;
const localPart = makeUniqueId(components);
const maxLength = 255;
return prefix + localPart.slice(Math.max(0, localPart.length - maxLength + prefix.length));
}

// ------------------------------------------------------------------------------------------------
Expand Down
14 changes: 13 additions & 1 deletion packages/@aws-cdk/core/test/output.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nodeunitShim, Test } from 'nodeunit-shim';
import { App, CfnOutput, CfnResource, Stack } from '../lib';
import { App, CfnOutput, CfnResource, ConstructNode, Stack, ValidationError } from '../lib';
import { toCloudFormation } from './util';

let app: App;
Expand Down Expand Up @@ -113,4 +113,16 @@ nodeunitShim({

test.done();
},

'Verify maximum length of export name'(test: Test) {
new CfnOutput(stack, 'SomeOutput', { value: 'x', exportName: 'x'.repeat(260) });

const errors = ConstructNode.validate(stack.node).map((v: ValidationError) => v.message);

expect(errors).toEqual([
expect.stringContaining('Export name cannot exceed 255 characters'),
]);

test.done();
},
});
33 changes: 33 additions & 0 deletions packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,39 @@ nodeunitShim({
test.done();
},

'Cross-stack export names account for stack name lengths'(test: Test) {
// GIVEN
const app = new App();
const stack1 = new Stack(app, 'Stack1', {
stackName: 'SoThisCouldPotentiallyBeAVeryLongStackName',
});
let scope: Construct = stack1;

// WHEN - deeply nested
for (let i = 0; i < 50; i++) {
scope = new Construct(scope, `ChildConstruct${i}`);
}

const resource1 = new CfnResource(scope, 'Resource', { type: 'BLA' });
const stack2 = new Stack(app, 'Stack2');

// WHEN - used in another resource
new CfnResource(stack2, 'SomeResource', {
type: 'AWS::Some::Resource',
properties: {
someProperty: new Intrinsic(resource1.ref),
},
});

// THEN
const assembly = app.synth();
const template1 = assembly.getStackByName(stack1.stackName).template;

const theOutput = template1.Outputs[Object.keys(template1.Outputs)[0]];
expect(theOutput.Export.Name.length).toEqual(255);
test.done();
},

'Cross-stack reference export names are relative to the stack (when the flag is set)'(test: Test) {
// GIVEN
const app = new App({
Expand Down

0 comments on commit 211671e

Please sign in to comment.