From 0efc25e1eadfb4ebc38cce84fab47fd129655095 Mon Sep 17 00:00:00 2001 From: "Boyd, Zach" Date: Thu, 25 Jan 2018 07:59:43 -0600 Subject: [PATCH] Added support to append ${stageVariables.SERVERLESS_ALIAS} to authorizers of type REQUEST #96 --- lib/stackops/apiGateway.js | 3 ++- test/data/auth-stack.json | 31 ++++++++++++++++++++++ test/stackops/apiGateway.test.js | 45 ++++++++++++++++++-------------- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/lib/stackops/apiGateway.js b/lib/stackops/apiGateway.js index 37b7918..8d7f218 100644 --- a/lib/stackops/apiGateway.js +++ b/lib/stackops/apiGateway.js @@ -222,7 +222,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac // Audjust authorizer Uri and name (stage variables are not allowed in Uris here) _.forOwn(authorizers, (authorizer, name) => { - if (_.get(authorizer, 'Properties.Type') === 'TOKEN') { + const authorizerType = _.get(authorizer, 'Properties.Type'); + if (authorizerType === 'TOKEN' || authorizerType === 'REQUEST') { const uriParts = authorizer.Properties.AuthorizerUri['Fn::Join'][1]; const funcIndex = _.findIndex(uriParts, part => _.has(part, 'Fn::GetAtt')); diff --git a/test/data/auth-stack.json b/test/data/auth-stack.json index 994543d..af00237 100644 --- a/test/data/auth-stack.json +++ b/test/data/auth-stack.json @@ -289,6 +289,37 @@ "Type": "TOKEN" } }, + "TestauthApiGatewayRequestAuthorizer": { + "Type": "AWS::ApiGateway::Authorizer", + "Properties": { + "AuthorizerResultTtlInSeconds": 0, + "IdentitySource": "method.request.header.Authorization", + "Name": "testauthrequest", + "RestApiId": { + "Ref": "ApiGatewayRestApi" + }, + "AuthorizerUri": { + "Fn::Join": [ + "", + [ + "arn:aws:apigateway:", + { + "Ref": "AWS::Region" + }, + ":lambda:path/2015-03-31/functions/", + { + "Fn::GetAtt": [ + "TestauthLambdaFunction", + "Arn" + ] + }, + "/invocations" + ] + ] + }, + "Type": "REQUEST" + } + }, "CognitoTestApiGatewayAuthorizer": { "Type": "AWS::ApiGateway::Authorizer", "Properties": { diff --git a/test/stackops/apiGateway.test.js b/test/stackops/apiGateway.test.js index b824471..8efee40 100644 --- a/test/stackops/apiGateway.test.js +++ b/test/stackops/apiGateway.test.js @@ -622,6 +622,26 @@ describe('API Gateway', () => { }); it('should handle only Lambda authorizers', () => { + const authorizeUriTemplate = { + "Fn::Join": [ + "", + [ + "arn:aws:apigateway:", + { + "Ref": "AWS::Region" + }, + ":lambda:path/2015-03-31/functions/", + { + "Fn::GetAtt": [ + "TestauthLambdaFunction", + "Arn" + ] + }, + ":${stageVariables.SERVERLESS_ALIAS}", + "/invocations" + ] + ] + }; const template = serverless.service.provider.compiledCloudFormationTemplate = stackTemplate; const cogAuth = _.cloneDeep(template.Resources.CognitoTestApiGatewayAuthorizer); cogAuth.Properties.Name += "-myAlias"; @@ -629,28 +649,13 @@ describe('API Gateway', () => { return expect(awsAlias.aliasHandleApiGateway({}, [], {})).to.be.fulfilled .then(() => BbPromise.all([ expect(template).to.not.have.a.nested.property('Resources.TestauthApiGatewayAuthorizer'), + expect(template).to.not.have.a.nested.property('Resources.TestauthApiGatewayRequestAuthorizer'), expect(template).to.have.a.nested.property('Resources.TestauthApiGatewayAuthorizermyAlias') .that.has.a.nested.property("Properties.AuthorizerUri") - .that.deep.equals({ - "Fn::Join": [ - "", - [ - "arn:aws:apigateway:", - { - "Ref": "AWS::Region" - }, - ":lambda:path/2015-03-31/functions/", - { - "Fn::GetAtt": [ - "TestauthLambdaFunction", - "Arn" - ] - }, - ":${stageVariables.SERVERLESS_ALIAS}", - "/invocations" - ] - ] - }), + .that.deep.equals(authorizeUriTemplate), + expect(template).to.have.a.nested.property('Resources.TestauthApiGatewayRequestAuthorizermyAlias') + .that.has.a.nested.property("Properties.AuthorizerUri") + .that.deep.equals(authorizeUriTemplate), expect(template).to.have.a.nested.property('Resources.CognitoTestApiGatewayAuthorizermyAlias') .that.deep.equals(cogAuth) ]));