-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkLambdaPolicy.js
43 lines (34 loc) · 1.34 KB
/
checkLambdaPolicy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";
const AWS = require('aws-sdk');
const _ = require('lodash');
module.exports = (functionName, name, api_info, account, aws_config) => {
const lambda = new AWS.Lambda(aws_config);
// for dev permissions
const apiId = api_info.apiId || '*';
const apiResourceName = api_info.resourceName || functionName.toLowerCase();
const apiMethod = api_info.method || "POST";
return lambda.getPolicy({
FunctionName: functionName,
Qualifier: name
}).promise()
.catch(err => err.code === 'ResourceNotFoundException' ? Promise.resolve() : Promise.reject(err))
.then(res => {
let found = false;
if (res) {
const policy = JSON.parse(res.Policy);
found = !!_.find(policy.Statement, st => st.Effect === 'Allow' &&
st.Action === 'lambda:InvokeFunction' &&
st.Resource === `arn:aws:lambda:${account}:function:${functionName}:${name}` &&
st.Condition && st.Condition.ArnLike &&
st.Condition.ArnLike['AWS:SourceArn'] === `arn:aws:execute-api:${account}:${apiId}/*/${apiMethod}/${apiResourceName}`
);
}
if (found) console.log('Policy exists not updating');
else console.log('Policy not found, updating');
return Promise.resolve(found);
})
.catch(err => {
console.log('err:', err);
return Promise.reject(err);
});
};