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

feat(core): add validations for export name in CfnOutput #28575

Merged
merged 8 commits into from
Jan 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ describe('CDK Include', () => {
const output = cfnTemplate.getOutput('Output1');
output.value = 'a mutated value';
output.description = undefined;
output.exportName = 'an export';
output.exportName = 'an-export';
output.condition = new core.CfnCondition(stack, 'MyCondition', {
expression: core.Fn.conditionIf('AlwaysFalseCond', core.Aws.NO_VALUE, true),
});
Expand Down Expand Up @@ -755,7 +755,7 @@ describe('CDK Include', () => {
"Output1": {
"Value": "a mutated value",
"Export": {
"Name": "an export",
"Name": "an-export",
},
"Condition": "MyCondition",
},
Expand Down
15 changes: 12 additions & 3 deletions packages/aws-cdk-lib/core/lib/cfn-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,19 @@ export class CfnOutput extends CfnElement {
}

private validateOutput(): string[] {
if (this._exportName && !Token.isUnresolved(this._exportName) && this._exportName.length > 255) {
return [`Export name cannot exceed 255 characters (got ${this._exportName.length} characters)`];
const errors: string[] = [];
if (this._exportName !== undefined && !Token.isUnresolved(this._exportName)) {
if (this._exportName.length === 0) {
errors.push('Export name cannot be empty');
}
if (this._exportName.length > 255) {
errors.push(`Export name cannot exceed 255 characters (got ${this._exportName.length} characters)`);
}
if (!/^[A-Za-z0-9-:]*$/.test(this._exportName)) {
errors.push(`Export name must only include alphanumeric characters, colons, or hyphens (got '${this._exportName}')`);
}
}
return [];
return errors;
}
}

Expand Down
39 changes: 38 additions & 1 deletion packages/aws-cdk-lib/core/test/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,44 @@ describe('output', () => {
const errors = output.node.validate();

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

test('Verify zero length of export name', () => {
const output = new CfnOutput(stack, 'SomeOutput', { value: 'x', exportName: '' });
const errors = output.node.validate();

expect(errors).toEqual([
expect.stringContaining('Export name cannot be empty'),
]);
});

test('throw if export name has invalid strings (space)', () => {
const output = new CfnOutput(stack, 'SomeOutput', { value: 'x', exportName: 'SOME INVALID EXPORT NAME' });
const errors = output.node.validate();

expect(errors).toEqual([
expect.stringContaining('Export name must only include alphanumeric characters, colons, or hyphens (got \'SOME INVALID EXPORT NAME\''),
]);
});

test('throw if export name has invalid strings (underscore)', () => {
const output = new CfnOutput(stack, 'SomeOutput', { value: 'x', exportName: 'SOME_INVALID_EXPORT_NAME' });
const errors = output.node.validate();

expect(errors).toEqual([
expect.stringContaining('Export name must only include alphanumeric characters, colons, or hyphens (got \'SOME_INVALID_EXPORT_NAME\''),
]);
});

test('throw if export name exceeds maximum length and has invalid strings', () => {
const output = new CfnOutput(stack, 'SomeOutput', { value: 'x', exportName: ' '.repeat(260) });
const errors = output.node.validate();

expect(errors).toEqual([
expect.stringContaining('Export name cannot exceed 255 characters (got 260 characters)'),
expect.stringContaining(`Export name must only include alphanumeric characters, colons, or hyphens (got '${' '.repeat(260)}'`),
]);
});
});