Skip to content

Commit d11e8e5

Browse files
authored
Merge pull request #9 from rschick/outputSupport
Add support for CloudFormation Output ARN to be allowed access to lam…
2 parents 9ce6da8 + f9fc82c commit d11e8e5

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ provider:
1818
allowAccess: # can be defined as a single value or an array
1919
- 111111111111 # principal as accountId
2020
- 'arn:aws:iam::222222222222:root' # principal as ARN
21+
- Fn::Import: cloudformation-output-arn # principal as CloudFormation Output Value ARN
2122

2223
functions:
2324
function1:

add-permissions.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ module.exports = class AwsAddLambdaAccountPermissions {
3939
const functionLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
4040

4141
functionAllowAccess.reduce((previousResourceName, principal) => {
42-
principal = principal.toString();
43-
const principalName = principal.replace(/\b\w/g, l => l.toUpperCase()).replace(/[_\W]+/g, "");
42+
let principalString;
43+
const fnName = principal instanceof Object ? Object.keys(principal).find(k => k.indexOf('Fn::') >= 0) : undefined;
44+
if (fnName) {
45+
principalString = principal[fnName].toString();
46+
}
47+
else {
48+
principal = principal.toString();
49+
principalString = principal;
50+
}
51+
const principalName = principalString.replace(/\b\w/g, l => l.toUpperCase()).replace(/[_\W]+/g, "");
4452
const resourceName = `${functionLogicalId}PermitInvokeFrom${principalName}`;
4553
const resource = {
4654
Type: 'AWS::Lambda::Permission',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-plugin-lambda-account-access",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"engines": {
55
"node": ">=4.0"
66
},

test/add-permissions-tests.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,31 @@ describe('serverless-plugin-lambda-account-access', function() {
347347
});
348348
});
349349

350+
it('should support principal to be an ARN Output from CloudFormation', function() {
351+
const instance = createTestInstance({
352+
functions: {
353+
function1: {}
354+
}
355+
});
356+
357+
instance.addPoliciesForFunctions([{'Fn::ImportValue':'output-role-arn'}]);
358+
359+
expect(instance)
360+
.to.have.deep.property('serverless.service.resources.Resources')
361+
.that.deep.equals({
362+
'Function1LambdaFunctionPermitInvokeFromOutputRoleArn': {
363+
'Type': 'AWS::Lambda::Permission',
364+
'Properties': {
365+
'Action': 'lambda:InvokeFunction',
366+
'FunctionName': {
367+
'Fn::GetAtt': [ 'Function1LambdaFunction', 'Arn' ],
368+
},
369+
'Principal': {'Fn::ImportValue':'output-role-arn'}
370+
}
371+
}
372+
});
373+
});
374+
350375
it('should support local allowAccess to be a single value', function() {
351376
const instance = createTestInstance({
352377
functions: {

0 commit comments

Comments
 (0)