-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathkmsKeyRotation.spec.js
More file actions
280 lines (264 loc) · 8.62 KB
/
kmsKeyRotation.spec.js
File metadata and controls
280 lines (264 loc) · 8.62 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
var expect = require('chai').expect;
const kmsKeyRotation = require('./kmsKeyRotation');
const listKeys = {
KeyId: "60c4f21b-e271-4e97-86ae-6403618a9467",
KeyArn: "arn:aws:kms:us-east-1:111122223333:key/60c4f21b-e271-4e97-86ae-6403618a9467"
};
const describeKey = [
{
KeyMetadata: {
AWSAccountId: "111122223333",
Arn: "arn:aws:kms:us-east-1:111122223333:key/60c4f21b-e271-4e97-86ae-6403618a9467",
CreationDate: "2020-08-26T16:55:28+00:00",
CustomerMasterKeySpec: "SYMMETRIC_DEFAULT",
Description: "",
Enabled: true,
EncryptionAlgorithms: [
"SYMMETRIC_DEFAULT"
],
KeyId: "60c4f21b-e271-4e97-86ae-6403618a9467",
KeyManager: "CUSTOMER",
KeyState: "Enabled",
KeyUsage: "ENCRYPT_DECRYPT",
MultiRegion: false,
Origin: "AWS_KMS"
}
},
{
KeyMetadata: {
AWSAccountId: "111122223333",
Arn: "arn:aws:kms:us-east-1:111122223333:key/60c4f21b-e271-4e97-86ae-6403618a9467",
CreationDate: "2020-08-26T16:55:28+00:00",
CustomerMasterKeySpec: "SYMMETRIC_DEFAULT",
Description: "",
Enabled: true,
EncryptionAlgorithms: [
"SYMMETRIC_DEFAULT"
],
KeyId: "60c4f21b-e271-4e97-86ae-6403618a9467",
KeyManager: "CUSTOMER",
KeyState: "Enabled",
KeyUsage: "ENCRYPT_DECRYPT",
MultiRegion: false,
Origin: "AWS_KMS"
}
},
{
KeyMetadata: {
AWSAccountId: "111122223333",
Arn: "arn:aws:kms:us-east-1:111122223333:key/60c4f21b-e271-4e97-86ae-6403618a9467",
CreationDate: "2020-08-26T16:55:28+00:00",
CustomerMasterKeySpec: "HMAC_512",
Description: "",
Enabled: true,
KeyId: "60c4f21b-e271-4e97-86ae-6403618a9467",
KeyManager: "CUSTOMER",
KeyState: "Enabled",
KeyUsage: "ENCRYPT_DECRYPT",
MultiRegion: false,
Origin: "AWS_KMS",
KeySpec: "HMAC_512",
}
},
{
KeyMetadata: {
AWSAccountId: "111122223333",
Arn: "arn:aws:kms:us-east-1:111122223333:key/60c4f21b-e271-4e97-86ae-6403618a9467",
CreationDate: "2020-08-26T16:55:28+00:00",
CustomerMasterKeySpec: "SYMMETRIC_DEFAULT",
Description: "",
Enabled: true,
EncryptionAlgorithms: [
"SYMMETRIC_DEFAULT"
],
KeyId: "60c4f21b-e271-4e97-86ae-6403618a9467",
KeyManager: "AWS",
KeyState: "PendingDeletion",
KeyUsage: "ENCRYPT_DECRYPT",
MultiRegion: false,
Origin: "AWS_KMS"
}
}
]
const keyPolicy = [
{
"Version" : "2012-10-17",
"Id" : "key-default-1",
"Statement" : [
{
"Sid" : "Enable IAM User Permissions",
"Effect" : "Allow",
"Principal" : {
"AWS" : "arn:aws:iam::111122223333:root"
},
"Action" : "kms:*",
"Resource" : "*"
}
]
},
{
"Version" : "2012-10-17",
"Id" : "aqua-cspm",
"Statement" : [
{
"Sid" : "Enable IAM User Permissions",
"Effect" : "Allow",
"Principal" : {
"AWS" : "arn:aws:iam::111122223333:root"
},
"Action" : "kms:*",
"Resource" : "*"
}
]
}
]
const keyRotationStatus = [
{
KeyRotationEnabled: true
},
{
KeyRotationEnabled: false
}
]
const createCache = (keys, describeKey, keyPolicy, keyRotation) => {
var keyId = (keys && keys.length) ? keys[0].KeyId : null;
return {
kms:{
listKeys: {
'us-east-1': {
data: keys
}
},
describeKey: {
'us-east-1': {
[keyId]: {
data: describeKey
}
}
},
getKeyPolicy: {
'us-east-1': {
[keyId]: {
data: keyPolicy
}
}
},
getKeyRotationStatus: {
'us-east-1': {
[keyId]: {
data: keyRotation
}
},
}
},
};
};
const createErrorCache = () => {
return {
kms: {
listKeys: {
'us-east-1': {
err: 'Unable to list keys '
}
},
describeKey: {
'us-east-1': {
['id']: {
err: 'Unable to describe key'
}
}
},
getKeyPolicy: {
'us-east-1': {
['id']: {
err: 'Unable to get key policy'
}
}
},
getKeyRotationStatus: {
'us-east-1': {
['id']: {
err: 'Unable to get key rotation status'
}
}
}
}
};
};
const createNullCache = () => {
return {
kms: {
listKeys: {
'us-east-1': null
},
describeKey: {
'us-east-1': null
},
getKeyPolicy: {
'us-east-1': null
},
getKeyRotationStatus: {
'us-east-1': null
}
}
};
};
describe('kmsKeyRotation', function () {
describe('run', function () {
it('should PASS if KMS has encryption greater than awskms and has rotation enabled', function (done) {
const cache = createCache([listKeys], describeKey[1], keyPolicy[0], keyRotationStatus[0]);
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
done();
});
});
it('should PASS if Key Rotation is not available for KMS Key', function (done) {
const cache = createCache([listKeys], describeKey[2], keyPolicy[0], keyRotationStatus[0]);
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
done();
});
});
it('should FAIL if KMS has encryption greater than awskms and has rotation disabled', function (done) {
const cache = createCache([listKeys], describeKey[1], keyPolicy[0], keyRotationStatus[1]);
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(2);
done();
});
});
it('should PASS if no KMS keys found', function (done) {
const cache = createCache([], {}, {}, {});
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
done();
});
});
it('should UNKNOWN if unable to list KMS keys', function (done) {
const cache = createErrorCache();
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(3);
done();
});
});
it('should pass if KMS encryption level is lower than or equal to awskms', function (done) {
const cache = createCache([listKeys], describeKey[3], keyPolicy[0], keyRotationStatus[1]);
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
done();
});
});
it('should not return any result if list keys response is not found', function (done) {
const cache = createNullCache();
kmsKeyRotation.run(cache, {}, (err, results) => {
expect(results.length).to.equal(0);
done();
});
});
});
});