From c6f6895523d528157324e7d8bc5d5b038fb603a4 Mon Sep 17 00:00:00 2001 From: Frank Spitulski Date: Wed, 17 Jun 2020 13:18:04 -0700 Subject: [PATCH 1/2] feat(api-gateway): support v1 authorizer --- lib/CfTemplateGenerators/ApiGateway.js | 9 ++++++++- serverless-plugin-canary-deployments.js | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/CfTemplateGenerators/ApiGateway.js b/lib/CfTemplateGenerators/ApiGateway.js index f5b02be..4d2309f 100644 --- a/lib/CfTemplateGenerators/ApiGateway.js +++ b/lib/CfTemplateGenerators/ApiGateway.js @@ -42,10 +42,17 @@ function replaceV2AuthorizerUriWithAlias (apiGatewayMethod, functionAlias) { return newMethod } +function replaceAuthorizerUriWithAlias (apiGatewayMethod, functionAlias) { + const aliasUri = buildUriForAlias(functionAlias) + const newMethod = _.set('Properties.AuthorizerUri', aliasUri, apiGatewayMethod) + return newMethod +} + const ApiGateway = { replaceV2IntegrationUriWithAlias, replaceMethodUriWithAlias, - replaceV2AuthorizerUriWithAlias + replaceV2AuthorizerUriWithAlias, + replaceAuthorizerUriWithAlias } module.exports = ApiGateway diff --git a/serverless-plugin-canary-deployments.js b/serverless-plugin-canary-deployments.js index d206203..2254278 100644 --- a/serverless-plugin-canary-deployments.js +++ b/serverless-plugin-canary-deployments.js @@ -142,6 +142,7 @@ class ServerlessCanaryDeployments { const replaceAliasStrategy = { 'AWS::Lambda::EventSourceMapping': CfGenerators.lambda.replaceEventMappingFunctionWithAlias, 'AWS::ApiGateway::Method': CfGenerators.apiGateway.replaceMethodUriWithAlias, + 'AWS::ApiGateway::Authorizer': CfGenerators.apiGateway.replaceAuthorizerUriWithAlias, 'AWS::ApiGatewayV2::Integration': CfGenerators.apiGateway.replaceV2IntegrationUriWithAlias, 'AWS::ApiGatewayV2::Authorizer': CfGenerators.apiGateway.replaceV2AuthorizerUriWithAlias, 'AWS::SNS::Topic': CfGenerators.sns.replaceTopicSubscriptionFunctionWithAlias, @@ -162,6 +163,7 @@ class ServerlessCanaryDeployments { getEventsFor (functionName) { const apiGatewayMethods = this.getApiGatewayMethodsFor(functionName) + const apiGatewayAuthorizers = this.getApiGatewayAuthorizersFor(functionName) const apiGatewayV2Methods = this.getApiGatewayV2MethodsFor(functionName) const apiGatewayV2Authorizers = this.getApiGatewayV2AuthorizersFor(functionName) const eventSourceMappings = this.getEventSourceMappingsFor(functionName) @@ -174,6 +176,7 @@ class ServerlessCanaryDeployments { return Object.assign( {}, apiGatewayMethods, + apiGatewayAuthorizers, apiGatewayV2Methods, apiGatewayV2Authorizers, eventSourceMappings, @@ -214,6 +217,20 @@ class ServerlessCanaryDeployments { return getMethodsForFunction(this.compiledTpl.Resources) } + getApiGatewayAuthorizersFor (functionName) { + const isApiGMethod = _.matchesProperty('Type', 'AWS::ApiGateway::Authorizer') + const isMethodForFunction = _.pipe( + _.prop('Properties.AuthorizerUri'), + flattenObject, + _.includes(functionName) + ) + const getMethodsForFunction = _.pipe( + _.pickBy(isApiGMethod), + _.pickBy(isMethodForFunction) + ) + return getMethodsForFunction(this.compiledTpl.Resources) + } + getApiGatewayV2AuthorizersFor (functionName) { const isApiGMethod = _.matchesProperty('Type', 'AWS::ApiGatewayV2::Authorizer') const isMethodForFunction = _.pipe( From 3816b172b7d020c30ade5549ef626b18d93ba3ff Mon Sep 17 00:00:00 2001 From: Frank Spitulski Date: Wed, 12 Aug 2020 16:15:18 -0700 Subject: [PATCH 2/2] add authorizer into example --- example/serverless.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/example/serverless.yml b/example/serverless.yml index fbf53b9..7a33c46 100644 --- a/example/serverless.yml +++ b/example/serverless.yml @@ -33,7 +33,10 @@ functions: - websocket: route: $default authorizer: authorize - - http: GET hello + - http: + path: hello + method: GET + authorizer: authorize - stream: type: dynamodb arn: