Skip to content

Commit

Permalink
feat(core): add validations for export name in CfnOutput (#28575)
Browse files Browse the repository at this point in the history
Closes #28563.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k committed Jan 23, 2024
1 parent baf2bfb commit 513d9fb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
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)}'`),
]);
});
});

0 comments on commit 513d9fb

Please sign in to comment.