Skip to content

Commit

Permalink
fix(aws-appsync): Strip unsupported characters from Lambda DataSource (
Browse files Browse the repository at this point in the history
…#18765)

Closes #16169

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Dzhuneyt authored Feb 4, 2022
1 parent a1d94b3 commit bb8d6f6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-appsync/lib/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export abstract class BaseDataSource extends CoreConstruct {
if (extended.type !== 'NONE') {
this.serviceRole = props.serviceRole || new Role(this, 'ServiceRole', { assumedBy: new ServicePrincipal('appsync') });
}
const name = props.name ?? id;
// Replace unsupported characters from DataSource name. The only allowed pattern is: {[_A-Za-z][_0-9A-Za-z]*}
const name = (props.name ?? id).replace(/[\W]+/g, '');
this.ds = new CfnDataSource(this, 'Resource', {
apiId: props.api.apiId,
name: name,
Expand Down
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,42 @@ describe('Lambda Data Source configuration', () => {
});
});

test('appsync sanitized datasource name from unsupported characters', () => {
const badCharacters = [...'!@#$%^&*()+-=[]{}\\|;:\'",<>?/'];

badCharacters.forEach((badCharacter) => {
// WHEN
const newStack = new cdk.Stack();
const graphqlapi = new appsync.GraphqlApi(newStack, 'baseApi', {
name: 'api',
schema: appsync.Schema.fromAsset(path.join(__dirname, 'appsync.test.graphql')),
});
const dummyFunction = new lambda.Function(newStack, 'func', {
code: lambda.Code.fromAsset(path.join(__dirname, 'verify/iam-query')),
handler: 'iam-query.handler',
runtime: lambda.Runtime.NODEJS_12_X,
});
graphqlapi.addLambdaDataSource(`data-${badCharacter}-source`, dummyFunction);

// THEN
Template.fromStack(newStack).hasResourceProperties('AWS::AppSync::DataSource', {
Type: 'AWS_LAMBDA',
Name: 'datasource',
});
});
});

test('appsync leaves underscore untouched in datasource name', () => {
// WHEN
api.addLambdaDataSource('data_source', func);

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::DataSource', {
Type: 'AWS_LAMBDA',
Name: 'data_source',
});
});

test('appsync errors when creating multiple lambda data sources with no configuration', () => {
// THEN
expect(() => {
Expand Down

0 comments on commit bb8d6f6

Please sign in to comment.