forked from Enterprise-CMCS/serverless-iam-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (86 loc) · 2.57 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use strict";
const _ = require("lodash");
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
"before:deploy:deploy": this.iamPathAndPermissionsBoundaryHelp.bind(this),
};
}
iamPathAndPermissionsBoundaryHelp() {
addRoleForApiLogging.call(this);
addProperties.call(this);
}
}
function addRoleForApiLogging() {
if (apiLoggingEnabled.call(this)) {
const iamPath = iamPathSpecified.call(this);
const iamPermissionBoundary = iamPermissionsBoundarySpecified.call(this);
var cloudwatchRoleForApiLogging = {
Type: "AWS::IAM::Role",
Properties: {
AssumeRolePolicyDocument: {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: {
Service: "apigateway.amazonaws.com",
},
Action: "sts:AssumeRole",
},
],
},
ManagedPolicyArns: [
"arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs",
],
},
};
const template =
this.serverless.service.provider.compiledCloudFormationTemplate;
template.Resources.CloudWatchRoleForApiGatewayLogging =
cloudwatchRoleForApiLogging;
template.Resources.CustomApiGatewayAccountCloudWatchRole.Properties.RoleArn =
{
"Fn::GetAtt": ["CloudWatchRoleForApiGatewayLogging", "Arn"],
};
}
}
function addProperties() {
const iamPath = iamPathSpecified.call(this);
if (iamPath) {
setPropertyForTypes.call(this, ["AWS::IAM::Role"], "Path", iamPath);
}
const iamPermissionBoundary = iamPermissionsBoundarySpecified.call(this);
if (iamPermissionBoundary) {
setPropertyForTypes.call(
this,
["AWS::IAM::Role"],
"PermissionsBoundary",
iamPermissionBoundary
);
}
}
function setPropertyForTypes(types, property, value) {
const template =
this.serverless.service.provider.compiledCloudFormationTemplate;
Object.keys(template.Resources).forEach(function (key) {
if (types.includes(template.Resources[key]["Type"])) {
template.Resources[key]["Properties"][property] = value;
}
});
}
function apiLoggingEnabled() {
return _.get(this.serverless, "service.provider.logs.restApi");
}
function iamPathSpecified() {
return _.get(this.serverless, "service.provider.iam.role.path");
}
function iamPermissionsBoundarySpecified() {
return _.get(
this.serverless,
"service.provider.iam.role.permissionsBoundary"
);
}
module.exports = ServerlessPlugin;