Skip to content

Commit

Permalink
Merge pull request #97 from zachboyd/authorizer-request-fix
Browse files Browse the repository at this point in the history
Added support to append ${stageVariables.SERVERLESS_ALIAS} to authorizers of type REQUEST #96
  • Loading branch information
HyperBrain committed Jan 26, 2018
2 parents 87b12a3 + 0efc25e commit 89de546
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/stackops/apiGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
31 changes: 31 additions & 0 deletions test/data/auth-stack.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
45 changes: 25 additions & 20 deletions test/stackops/apiGateway.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,35 +622,40 @@ 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";
serverless.service.provider.compiledCloudFormationAliasTemplate = aliasTemplate;
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)
]));
Expand Down

0 comments on commit 89de546

Please sign in to comment.