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): autogenerated exports do not account for stack name length #11909

Merged
merged 2 commits into from
Dec 7, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

50 seems a bit excessive; by my math, 17 or so would more than do it. :)

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