Skip to content

Commit

Permalink
Merge pull request #162 from serverless-heaven/fix-for-naming-issue
Browse files Browse the repository at this point in the history
Fix for functionnames where functionname A startwith functionname B
  • Loading branch information
Enase authored May 10, 2019
2 parents b4c83c2 + 3b922db commit dd1dbba
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 24 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/node_modules
/coverage
/.nyc_output

# Jetbrains IDEs
.idea/
Expand Down
14 changes: 7 additions & 7 deletions lib/stackops/apiGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
this.options.verbose && this._serverless.cli.log('Configuring stage');
const stageResource = internal.createStageResource.call(this, `${stackName}-ApiGatewayRestApi`, deploymentName);
aliasResources.push({ ApiGatewayStage: stageResource });

const baseMapping = _.assign({}, _.pickBy(stageStack.Resources, ['Type', 'AWS::ApiGateway::BasePathMapping']));
if (!_.isEmpty(baseMapping)) {
const baseMappingName = _.keys(baseMapping)[0];
Expand All @@ -198,7 +198,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
aliasResources.push(baseMapping);
delete stageStack.Resources[baseMappingName];
}
}
}

// Fetch lambda permissions, methods and resources. These have to be updated later to allow the aliased functions.
const apiLambdaPermissions =
Expand Down Expand Up @@ -278,9 +278,9 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
// Adjust permission to reference the function aliases
_.forOwn(apiLambdaPermissions, (permission, name) => {
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 = isExternalRefAuthorizerPredicate(permission.Properties.FunctionName);
const versionName = utils.getFunctionVersionName(versions, functionName);
const aliasName = utils.getAliasVersionName(aliases, functionName);
const isExternalRef = isExternalRefAuthorizerPredicate(permission.Properties.FunctionName);

// Adjust references and alias permissions
if (!isExternalRef) {
Expand All @@ -305,9 +305,9 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
]
};
}

// Add dependency on function version
if (!isExternalRef) {
if (!isExternalRef) {
permission.DependsOn = [ versionName, aliasName ];
} else {
permission.DependsOn = _.compact([ versionName, aliasName ]);
Expand Down
12 changes: 6 additions & 6 deletions lib/stackops/snsEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
_.forOwn(lambdaSubscriptions, subscription => {
const functionNameRef = utils.findAllReferences(_.get(subscription, 'Endpoint'));
const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, '');
const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName));
const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName));
const versionName = utils.getFunctionVersionName(versions, functionName);
const aliasName = utils.getAliasVersionName(aliases, functionName);

subscription.Endpoint = { Ref: aliasName };

Expand All @@ -54,8 +54,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac

const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint'));
const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, '');
const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName));
const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName));
const versionName = utils.getFunctionVersionName(versions, functionName);
const aliasName = utils.getAliasVersionName(aliases, functionName);

subscription.Properties.Endpoint = { Ref: aliasName };
subscription.DependsOn = [ versionName, aliasName ];
Expand All @@ -72,8 +72,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
// Adjust permission to reference the function aliases
_.forOwn(snsLambdaPermissions, (permission, name) => {
const functionName = _.replace(name, /LambdaPermission.*$/, '');
const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName));
const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName));
const versionName = utils.getFunctionVersionName(versions, functionName);
const aliasName = utils.getAliasVersionName(aliases, functionName);

// Adjust references and alias permissions
permission.Properties.FunctionName = { Ref: aliasName };
Expand Down
20 changes: 19 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class Utils {

/**
* Checks if a CF resource permission targets the given service as Principal.
* @param {string} service
* @param {Object} permission
* @param {string} service
*/
static hasPermissionPrincipal(permission, service) {
const principal = _.get(permission, 'Properties.Principal');
Expand All @@ -104,6 +105,23 @@ class Utils {
return false;
}

/**
* @param {object} versions
* @param {string} functionName
* @returns {string}
*/
static getFunctionVersionName(versions, functionName) {
return _.find(_.keys(versions), version => version === `${functionName}LambdaVersion`);
}

/**
* @param {object} aliases
* @param {string} functionName
* @returns {string}
*/
static getAliasVersionName(aliases, functionName) {
return _.find(_.keys(aliases), alias => alias === `${functionName}Alias`);
}
}

module.exports = Utils;
64 changes: 64 additions & 0 deletions test/data/alias-stack-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
"RetentionInDays": 7
}
},
"Testfct1WithSuffixAlias": {
"Type": "AWS::Lambda::Alias",
"Properties": {
"Description": "Echo function echoes alias",
"FunctionName": {
"Fn::ImportValue": "sls-test-project-dev-Testfct1WithSuffix-LambdaFunctionArn"
},
"FunctionVersion": {
"Fn::GetAtt": [
"Testfct1WithSuffixLambdaVersionWh5jTkiTR67V05RPWQIlzPI25WiPbdHDYNgbtAMneU",
"Version"
]
},
"Name": "myAlias"
},
"DependsOn": [
"Testfct1WithSuffixLambdaVersionWh5jTkiTR67V05RPWQIlzPI25WiPbdHDYNgbtAMneU"
]
},
"Testfct1Alias": {
"Type": "AWS::Lambda::Alias",
"Properties": {
Expand Down Expand Up @@ -47,6 +66,17 @@
"WarmUpPluginLambdaVersionWh5jTkiTR67V05RPWQIlzPI25WiPbdHDYNgbtAMneU"
]
},
"Testfct1WithSuffixLambdaVersionWh5jTkiTR67V05RPWQIlzPI25WiPbdHDYNgbtAMneU": {
"Type": "AWS::Lambda::Version",
"DeletionPolicy": "Delete",
"Properties": {
"FunctionName": {
"Fn::ImportValue": "sls-test-project-dev-Testfct1WithSuffix-LambdaFunctionArn"
},
"CodeSha256": "Wh5jTkiTR67+V05RPWQIlzPI25WiPbdHDYNgbtAMneU=",
"Description": "Echo function echoes alias"
}
},
"Testfct1LambdaVersionWh5jTkiTR67V05RPWQIlzPI25WiPbdHDYNgbtAMneU": {
"Type": "AWS::Lambda::Version",
"DeletionPolicy": "Delete",
Expand Down Expand Up @@ -97,6 +127,40 @@
"ApiGatewayDeployment1494367071211"
]
},
"Testfct1WithSuffixLambdaPermissionApiGateway": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"FunctionName": {
"Ref": "Testfct1WithSuffixAlias"
},
"Action": "lambda:InvokeFunction",
"Principal": "apigateway.amazonaws.com",
"SourceArn": {
"Fn::Join": [
"",
[
"arn:aws:execute-api:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":",
{
"Fn::ImportValue": "sls-test-project-dev-ApiGatewayRestApi"
},
"/*/*"
]
]
}
},
"DependsOn": [
"Testfct1WithSuffixLambdaVersionWh5jTkiTR67V05RPWQIlzPI25WiPbdHDYNgbtAMneU",
"Testfct1WithSuffixAlias"
]
},
"Testfct1LambdaPermissionApiGateway": {
"Type": "AWS::Lambda::Permission",
"Properties": {
Expand Down
Loading

0 comments on commit dd1dbba

Please sign in to comment.