Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for referenced custom authorizer lambdas #102

Merged
merged 6 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/stackops/apiGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,14 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
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'));
const isExternalRefAuthorizer = _.every(uriParts, part => !_.startsWith(part, 'arn:aws:lambda'));
if (isExternalRefAuthorizer) {
const funcIndex = _.findIndex(uriParts, part =>
_.has(part, 'Fn::GetAtt') || _.startsWith(part, 'arn:aws:lambda'));

// Use the SERVERLESS_ALIAS stage variable to determine the called function alias
uriParts.splice(funcIndex + 1, 0, ':${stageVariables.SERVERLESS_ALIAS}');
// Use the SERVERLESS_ALIAS stage variable to determine the called function alias
uriParts.splice(funcIndex + 1, 0, ':${stageVariables.SERVERLESS_ALIAS}');
}
}

authorizer.Properties.Name = `${authorizer.Properties.Name}-${this._alias}`;
Expand Down Expand Up @@ -265,9 +269,12 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
const functionName = _.replace(name, /LambdaPermissionApiGateway$/, '');
const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName));
const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName));
const isExternalRef = _.startsWith(permission.Properties.FunctionName, 'arn:aws:lambda');

// Adjust references and alias permissions
permission.Properties.FunctionName = { Ref: aliasName };
if (!isExternalRef) {
permission.Properties.FunctionName = { Ref: aliasName };
}
if (permission.Properties.SourceArn) {
// Authorizers do not set the SourceArn property
permission.Properties.SourceArn = {
Expand All @@ -285,9 +292,13 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
]
};
}

// Add dependency on function version
permission.DependsOn = [ versionName, aliasName ];
if (!isExternalRef) {
permission.DependsOn = [ versionName, aliasName ];
} else {
permission.DependsOn = _.compact([ versionName, aliasName ]);
}

delete stageStack.Resources[name];
});
Expand Down
Loading