From f4f8957261619c77f46fddfa2b3a1eb35cf9e19c Mon Sep 17 00:00:00 2001 From: Sachin Shekhar Date: Sat, 9 May 2020 06:06:48 +0530 Subject: [PATCH] tweaks and fixes related to api key creation --- .../@aws-cdk/aws-appsync/lib/graphqlapi.ts | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts index 67108d554cde9..0332e2f73ef1b 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts @@ -58,11 +58,11 @@ export interface AuthorizationMode { */ readonly userPoolConfig?: UserPoolConfig; /** - * If authorizationType is `AuthorizationType.API_KEY`, this option can be confogured. + * If authorizationType is `AuthorizationType.API_KEY`, this option can be configured. If AuthorizationType.API_KEY` is in `additionalAuthorizationModes`, this option is required. */ readonly apiKeyConfig?: ApiKeyConfig; /** - * If authorizationType is `AuthorizationType.OIDC`, this option can be configured. + * If authorizationType is `AuthorizationType.OIDC`, this option is required. */ readonly openIdConnectConfig?: OpenIdConnectConfig; } @@ -417,6 +417,15 @@ export class GraphQLApi extends Construct { 'Missing User Pool Configuration inside an additional authorization mode' ); } + + if ( + authorizationMode.authorizationType === AuthorizationType.API_KEY && + !authorizationMode.apiKeyConfig + ) { + throw new Error( + 'Missing API Key Configuration inside an additional authorization mode' + ); + } } ); } @@ -468,16 +477,8 @@ export class GraphQLApi extends Construct { ): CfnGraphQLApi.AdditionalAuthenticationProviderProperty[] { return authModes.reduce< CfnGraphQLApi.AdditionalAuthenticationProviderProperty[] - >((acc, authMode) => { - if (authMode.authorizationType === AuthorizationType.API_KEY) { - const apiKeyConfig: ApiKeyConfig = authMode.apiKeyConfig || { - name: 'DefaultAPIKey', - description: 'Default API Key created by CDK', - }; - - this.createAPIKey(apiKeyConfig); - } - return [ + >( + (acc, authMode) => [ ...acc, { authenticationType: authMode.authorizationType, @@ -490,8 +491,9 @@ export class GraphQLApi extends Construct { ? this.formatOpenIdConnectConfig(authMode.openIdConnectConfig!) : undefined, }, - ]; - }, []); + ], + [] + ); } /**