Skip to content

Commit 2ccac33

Browse files
author
Frank Schmid
committed
Fixed detection of Principal for Serverless 1.27
1 parent ac7d9f9 commit 2ccac33

File tree

5 files changed

+23
-4035
lines changed

5 files changed

+23
-4035
lines changed

lib/stackops/apiGateway.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
204204
const apiLambdaPermissions =
205205
_.assign({},
206206
_.pickBy(_.pickBy(stageStack.Resources, [ 'Type', 'AWS::Lambda::Permission' ]),
207-
['Properties.Principal', 'apigateway.amazonaws.com']));
207+
permission => utils.hasPermissionPrincipal(permission, 'apigateway')));
208208

209209
const apiMethods = _.assign({}, _.pickBy(stageStack.Resources, [ 'Type', 'AWS::ApiGateway::Method' ]));
210210
const authorizers = _.assign({}, _.pickBy(stageStack.Resources, [ 'Type', 'AWS::ApiGateway::Authorizer' ]));
@@ -292,7 +292,9 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
292292
'Fn::Join': [
293293
'',
294294
[
295-
'arn:aws:execute-api:',
295+
'arn:',
296+
{ Ref: 'AWS::Partition' },
297+
':execute-api:',
296298
{ Ref: 'AWS::Region' },
297299
':',
298300
{ Ref: 'AWS::AccountId' },

lib/stackops/cwEvents.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
1616
const cwEventLambdaPermissions =
1717
_.assign({},
1818
_.pickBy(_.pickBy(stageStack.Resources, [ 'Type', 'AWS::Lambda::Permission' ]),
19-
['Properties.Principal', 'events.amazonaws.com']));
19+
permission => utils.hasPermissionPrincipal(permission, 'events')));
2020

2121
_.forOwn(cwEvents, (cwEvent, name) => {
2222
// Reference alias as FunctionName

lib/stackops/snsEvents.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
6767
const snsLambdaPermissions =
6868
_.assign({},
6969
_.pickBy(_.pickBy(stageStack.Resources, [ 'Type', 'AWS::Lambda::Permission' ]),
70-
[ 'Properties.Principal', 'sns.amazonaws.com' ]));
70+
permission => utils.hasPermissionPrincipal(permission, 'sns')));
7171

7272
// Adjust permission to reference the function aliases
7373
_.forOwn(snsLambdaPermissions, (permission, name) => {

lib/utils.js

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ class Utils {
8787
}, alias);
8888
}
8989

90+
/**
91+
* Checks if a CF resource permission targets the given service as Principal.
92+
* @param {string} service
93+
*/
94+
static hasPermissionPrincipal(permission, service) {
95+
const principal = _.get(permission, 'Properties.Principal');
96+
if (_.isString(principal)) {
97+
return _.startsWith(principal, service);
98+
} else if (_.isPlainObject(principal)) {
99+
const join = principal['Fn::Join'];
100+
if (join) {
101+
return _.some(join[1], joinPart => _.isString(joinPart) && _.startsWith(joinPart, service));
102+
}
103+
}
104+
return false;
105+
}
106+
90107
}
91108

92109
module.exports = Utils;

0 commit comments

Comments
 (0)