Skip to content

Commit 0efc25e

Browse files
author
Boyd, Zach
committed
Added support to append ${stageVariables.SERVERLESS_ALIAS} to authorizers of type REQUEST #96
1 parent dde216e commit 0efc25e

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

lib/stackops/apiGateway.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
222222

223223
// Audjust authorizer Uri and name (stage variables are not allowed in Uris here)
224224
_.forOwn(authorizers, (authorizer, name) => {
225-
if (_.get(authorizer, 'Properties.Type') === 'TOKEN') {
225+
const authorizerType = _.get(authorizer, 'Properties.Type');
226+
if (authorizerType === 'TOKEN' || authorizerType === 'REQUEST') {
226227
const uriParts = authorizer.Properties.AuthorizerUri['Fn::Join'][1];
227228
const funcIndex = _.findIndex(uriParts, part => _.has(part, 'Fn::GetAtt'));
228229

test/data/auth-stack.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,37 @@
289289
"Type": "TOKEN"
290290
}
291291
},
292+
"TestauthApiGatewayRequestAuthorizer": {
293+
"Type": "AWS::ApiGateway::Authorizer",
294+
"Properties": {
295+
"AuthorizerResultTtlInSeconds": 0,
296+
"IdentitySource": "method.request.header.Authorization",
297+
"Name": "testauthrequest",
298+
"RestApiId": {
299+
"Ref": "ApiGatewayRestApi"
300+
},
301+
"AuthorizerUri": {
302+
"Fn::Join": [
303+
"",
304+
[
305+
"arn:aws:apigateway:",
306+
{
307+
"Ref": "AWS::Region"
308+
},
309+
":lambda:path/2015-03-31/functions/",
310+
{
311+
"Fn::GetAtt": [
312+
"TestauthLambdaFunction",
313+
"Arn"
314+
]
315+
},
316+
"/invocations"
317+
]
318+
]
319+
},
320+
"Type": "REQUEST"
321+
}
322+
},
292323
"CognitoTestApiGatewayAuthorizer": {
293324
"Type": "AWS::ApiGateway::Authorizer",
294325
"Properties": {

test/stackops/apiGateway.test.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -622,35 +622,40 @@ describe('API Gateway', () => {
622622
});
623623

624624
it('should handle only Lambda authorizers', () => {
625+
const authorizeUriTemplate = {
626+
"Fn::Join": [
627+
"",
628+
[
629+
"arn:aws:apigateway:",
630+
{
631+
"Ref": "AWS::Region"
632+
},
633+
":lambda:path/2015-03-31/functions/",
634+
{
635+
"Fn::GetAtt": [
636+
"TestauthLambdaFunction",
637+
"Arn"
638+
]
639+
},
640+
":${stageVariables.SERVERLESS_ALIAS}",
641+
"/invocations"
642+
]
643+
]
644+
};
625645
const template = serverless.service.provider.compiledCloudFormationTemplate = stackTemplate;
626646
const cogAuth = _.cloneDeep(template.Resources.CognitoTestApiGatewayAuthorizer);
627647
cogAuth.Properties.Name += "-myAlias";
628648
serverless.service.provider.compiledCloudFormationAliasTemplate = aliasTemplate;
629649
return expect(awsAlias.aliasHandleApiGateway({}, [], {})).to.be.fulfilled
630650
.then(() => BbPromise.all([
631651
expect(template).to.not.have.a.nested.property('Resources.TestauthApiGatewayAuthorizer'),
652+
expect(template).to.not.have.a.nested.property('Resources.TestauthApiGatewayRequestAuthorizer'),
632653
expect(template).to.have.a.nested.property('Resources.TestauthApiGatewayAuthorizermyAlias')
633654
.that.has.a.nested.property("Properties.AuthorizerUri")
634-
.that.deep.equals({
635-
"Fn::Join": [
636-
"",
637-
[
638-
"arn:aws:apigateway:",
639-
{
640-
"Ref": "AWS::Region"
641-
},
642-
":lambda:path/2015-03-31/functions/",
643-
{
644-
"Fn::GetAtt": [
645-
"TestauthLambdaFunction",
646-
"Arn"
647-
]
648-
},
649-
":${stageVariables.SERVERLESS_ALIAS}",
650-
"/invocations"
651-
]
652-
]
653-
}),
655+
.that.deep.equals(authorizeUriTemplate),
656+
expect(template).to.have.a.nested.property('Resources.TestauthApiGatewayRequestAuthorizermyAlias')
657+
.that.has.a.nested.property("Properties.AuthorizerUri")
658+
.that.deep.equals(authorizeUriTemplate),
654659
expect(template).to.have.a.nested.property('Resources.CognitoTestApiGatewayAuthorizermyAlias')
655660
.that.deep.equals(cogAuth)
656661
]));

0 commit comments

Comments
 (0)