-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
166 lines (138 loc) · 4.64 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
const aws = require("aws-sdk");
const _ = require("lodash");
class ServerlessKmsGrants {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.kms = null;
this.commands = {
createKmsGrant: {
usage: "Creates a KMS grant for a lambda",
lifecycleEvents: ["createGrant"],
options: {},
},
revokeKmsGrant: {
usage: "Revokes a KMS grant for a lambda",
lifecycleEvents: ["revokeGrant"],
options: {},
},
};
this.createGrant = this.createGrant.bind(this);
this.revokeGrant = this.revokeGrant.bind(this);
this.getLambdaRole = this.getLambdaRole.bind(this);
this.getRoleArn = this.getRoleArn.bind(this);
this.findGrant = this.findGrant.bind(this);
this.hooks = {
"createKmsGrant:createGrant": this.createGrant,
"revokeKmsGrant:revokeGrant": this.revokeGrant,
"after:deploy:deploy": this.createGrant,
"before:remove:remove": this.revokeGrant,
};
}
async createGrant() {
if (!this.kms) {
this.kms = new aws.KMS({ region: this.serverless.service.provider.region });
}
const grants = _.get(this.serverless.service, "custom.kmsGrants");
if (!grants) {
// Nothing to do - no grants/role key pairs
this.serverless.cli.log("Exiting, nothing to create grants for");
return;
}
for (var i = 0; i < grants.length; i++) {
const [keyArn, roleArn, grantID] = await this.findGrant(grants[i]);
if (grantID === null) {
this.serverless.cli.log("Creating KMS grant for " + roleArn);
await this.kms
.createGrant({
KeyId: keyArn,
GranteePrincipal: roleArn,
Operations: ["Encrypt", "Decrypt"],
})
.promise();
} else {
this.serverless.cli.log("KMS grant already exists for " + roleArn);
}
}
}
async revokeGrant() {
if (!this.kms) {
this.kms = new aws.KMS({ region: this.serverless.service.provider.region });
}
const grants = _.get(this.serverless.service, "custom.kmsGrants");
if (!grants) {
// Nothing to do - no grants/role key pairs
this.serverless.cli.log("Exiting, nothing to revoke grants for");
return;
}
for (var i = 0; i < grants.length; i++) {
const [keyArn, roleArn, grantID] = await this.findGrant(grants[i]);
if (grantID !== null) {
this.serverless.cli.log("Revoking KMS grant for " + roleArn);
await this.kms
.revokeGrant({ KeyId: keyArn, GrantId: grantID })
.promise();
} else {
this.serverless.cli.log("No KMS grant found for " + roleArn + ".");
}
}
}
getLambdaRole() {
const service = _.get(this.serverless.service, "service");
if (!service) {
throw new Error("Service is undefined in serverless.yaml");
}
const stage = _.get(this.serverless.service, "provider.stage");
if (!stage) {
throw new Error("Stage is undefined in serverless.yaml");
}
const region = _.get(this.serverless.service, "provider.region");
if (!region) {
throw new Error("Region is undefined in serverless.yaml");
}
const lambdaRole = `${service}-${stage}-${region}-lambdaRole`;
return lambdaRole;
}
async getRoleArn(roleName) {
let role = roleName || this.getLambdaRole();
const iam = new aws.IAM({
region: this.serverless.service.provider.region,
});
const roleData = await iam.getRole({ RoleName: role }).promise();
const roleArn = roleData.Role.Arn;
return roleArn;
}
async findGrant(grant) {
const keyId = _.get(grant, "kmsKeyId");
if (!keyId) {
throw new Error("No kms key id given.");
}
let roleArn = _.get(grant, "roleArn");
let roleName = _.get(grant, "roleName");
if (roleName) {
roleArn = await this.getRoleArn(roleName);
}
if (!roleArn) {
this.serverless.cli.log(
"Neither 'roleArn' or 'roleName' defined for grant " +
keyId +
", using default format for role name: <service>-<stage>-<region>-lambdaRole",
);
roleArn = await this.getRoleArn();
}
const keyData = await this.kms.describeKey({ KeyId: keyId }).promise();
const keyArn = keyData.KeyMetadata.Arn;
const { Grants: grantsArray } = await this.kms
.listGrants({ KeyId: keyArn })
.promise();
for (let i = 0; i < grantsArray.length; i++) {
if (grantsArray[i].GranteePrincipal === roleArn) {
const grantID = grantsArray[i].GrantId;
return [keyArn, roleArn, grantID];
}
}
return [keyArn, roleArn, null];
}
}
module.exports = ServerlessKmsGrants;