-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathbootstrap.test.ts
More file actions
411 lines (404 loc) · 14.6 KB
/
bootstrap.test.ts
File metadata and controls
411 lines (404 loc) · 14.6 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { cloneDeep, merge } from 'lodash'
import { pki } from 'node-forge'
import stubs from 'src/test-stubs'
import {
bootstrap,
bootstrapSops,
copyBasicFiles,
createCustomCA,
getKmsValues,
getStoredClusterSecrets,
handleFileEntry,
processValues,
} from './bootstrap'
const { terminal } = stubs
jest.mock('src/common/envalid', () => ({
env: {
VALUES_INPUT: 'testValues.yaml',
ENV_DIR: '/test',
},
}))
describe('Bootstrapping values', () => {
const values = {
apps: { 'cert-manager': { issuer: 'custom-ca' } },
cluster: { name: 'bla', provider: 'dida' },
}
const users = [{ id: 'user1', initialPassword: 'existing-password' }, { id: 'user2' }]
const secrets = { secret: 'true', deep: { nested: 'secret' } }
let deps
beforeEach(() => {
deps = {
$: jest.fn().mockReturnValue({
nothrow: jest.fn().mockReturnValue({
quiet: jest.fn(),
}),
}),
bootstrapSops: jest.fn(),
copyBasicFiles: jest.fn(),
copyFile: jest.fn(),
createCustomCA: jest.fn(),
ensureManifestDirectories: jest.fn(),
handleFileEntry: jest.fn(),
decrypt: jest.fn(),
encrypt: jest.fn(),
existsSync: jest.fn(),
genSops: jest.fn(),
getDeploymentState: jest.fn().mockReturnValue({}),
getImageTagFromValues: jest.fn(),
getK8sSecret: jest.fn(),
hfValues: jest.fn(),
isCli: true,
migrate: jest.fn(),
nothrow: jest.fn(),
pathExists: jest.fn(),
processValues: jest.fn(),
terminal,
writeValues: jest.fn(),
}
})
it('should call relevant sub routines', async () => {
deps.processValues.mockReturnValue(values)
deps.hfValues.mockReturnValue(values)
await bootstrap(deps)
expect(deps.copyBasicFiles).toHaveBeenCalled()
expect(deps.bootstrapSops).toHaveBeenCalled()
})
it('should copy only skeleton files to env dir if it is empty or nonexisting', async () => {
deps.processValues.mockReturnValue(undefined)
await bootstrap(deps)
expect(deps.hfValues).toHaveBeenCalledTimes(0)
})
it('should get stored cluster secrets if those exist', async () => {
deps.getK8sSecret.mockReturnValue({ 'otomi-generated-passwords': secrets })
const res = await getStoredClusterSecrets(deps)
expect(res).toEqual(secrets)
})
it('should not get stored cluster secrets if those do not exist', async () => {
deps.getK8sSecret.mockReturnValue(undefined)
const res = await getStoredClusterSecrets(deps)
expect(res).toEqual(undefined)
})
describe('getKmsValues', () => {
let kmsValuesDeps: any
const ageKeys = { publicKey: 'agePublicKey', privateKey: 'agePrivateKey' }
const values = { someKey: 'someValue' }
beforeEach(() => {
kmsValuesDeps = {
generateAgeKeys: jest.fn().mockResolvedValue(ageKeys),
hfValues: jest.fn(),
}
})
it('should not get kms values if those do not exist', async () => {
kmsValuesDeps.hfValues.mockReturnValue(values)
const res = await getKmsValues(kmsValuesDeps)
expect(res).toBeUndefined()
})
it('should get kms values if those exist', async () => {
const deps = {
generateAgeKeys: jest.fn(),
}
const values = {
kms: {
sops: {
provider: 'azure',
},
},
}
deps.generateAgeKeys.mockResolvedValue({ publicKey: 'key1', privateKey: 'key2' })
const res = await getKmsValues(values, deps)
expect(res).toEqual({
kms: {
sops: {
provider: 'azure',
},
},
})
})
it('should generate and return new age keys if provider is age and keys are missing', async () => {
const deps = {
generateAgeKeys: jest.fn(),
}
const values = {
kms: {
sops: {
provider: 'age',
},
},
}
deps.generateAgeKeys.mockResolvedValue({ publicKey: 'key1', privateKey: 'key2' })
const res = await getKmsValues(values, deps)
expect(res).toEqual({
kms: {
sops: {
provider: 'age',
age: { publicKey: 'key1', privateKey: 'key2' },
},
},
})
})
describe('Copying basic files', () => {
const deps = {
copy: jest.fn(),
copyFile: jest.fn(),
copySchema: jest.fn(),
mkdir: jest.fn(),
pathExists: jest.fn(),
terminal,
}
it('should not throw any exception', async () => {
const res = await copyBasicFiles(deps)
expect(res).toBe(undefined)
})
})
describe('Creating folders and files for workload', () => {
const values = {
values: {
image: {
repository: 'linode/apl-nodejs-helloworld',
tag: 'v1.5.1',
},
},
}
const workload = {
files: {
'env/teams/workloads/demo/values.yaml': JSON.stringify(values),
},
}
const deps = {
loadYaml: jest.fn().mockReturnValue(workload),
mkdir: jest.fn(),
terminal,
writeFile: jest.fn(),
}
it('should create folders and files based on file entry in yaml', async () => {
await handleFileEntry(deps)
expect(deps.mkdir).toHaveBeenCalledWith('/test/env/teams/workloads/demo', { recursive: true })
expect(deps.writeFile).toHaveBeenCalledWith(
'/test/env/teams/workloads/demo/values.yaml',
JSON.stringify(values),
)
})
})
describe('Generating sops related files', () => {
const settings = {
kms: {
sops: {
provider: 'aws',
aws: {
keys: 'key1,key2',
},
},
},
}
const deps = {
copyFile: jest.fn(),
decrypt: jest.fn(),
encrypt: jest.fn(),
gucci: jest.fn().mockReturnValue('ok'),
hfValues: jest.fn(),
loadYaml: jest.fn().mockReturnValue(settings),
pathExists: jest.fn(),
readFile: jest.fn(),
getKmsSettings: jest.fn(),
terminal,
writeFile: jest.fn(),
createUpdateGenericSecret: jest.fn(),
}
it('should create files on first run and en/de-crypt', async () => {
deps.pathExists.mockReturnValue(false)
deps.getKmsSettings.mockReturnValue({
kms: {
sops: {
provider: 'age',
age: { publicKey: 'key1', privateKey: 'key2' },
},
},
})
await bootstrapSops(undefined, deps)
expect(deps.encrypt).toHaveBeenCalled()
expect(deps.decrypt).toHaveBeenCalled()
})
it('should just create files on next runs', async () => {
deps.pathExists.mockReturnValue(true)
deps.hfValues.mockReturnValue(settings)
deps.decrypt = jest.fn()
deps.encrypt = jest.fn()
const res = await bootstrapSops(undefined, deps)
expect(res).toBe(undefined)
expect(deps.encrypt).not.toHaveBeenCalled()
expect(deps.decrypt).not.toHaveBeenCalled()
})
})
describe('Checking for a custom CA', () => {
const deps = {
pki: {
rsa: {
generateKeyPair: jest.fn().mockReturnValue({
publicKey: { n: {}, e: {} },
privateKey: { d: {}, p: {}, q: {} },
}),
},
createCertificate: jest.fn().mockReturnValue({
publicKey: {},
serialNumber: '01',
validity: {},
sign: jest.fn(),
setSubject: jest.fn(),
setIssuer: jest.fn(),
setExtensions: jest.fn(),
}),
certificateToPem: jest.fn(),
privateKeyToPem: jest.fn(),
} as unknown as typeof pki,
writeValues: jest.fn(),
terminal,
}
deps.pki.certificateToPem = jest.fn().mockReturnValue('certpem')
deps.pki.privateKeyToPem = jest.fn().mockReturnValue('keypem')
it('should create a new key pair when none exist', () => {
const res = createCustomCA(deps)
expect(res).toMatchObject({
apps: {
'cert-manager': {
customRootCA: 'certpem',
customRootCAKey: 'keypem',
},
},
})
})
})
describe('processing values', () => {
const generatedSecrets = { gen: 'x' }
const generatedPassword = 'generated-password'
const usersWithPasswords = [
{ id: 'user1', initialPassword: 'existing-password' },
{ id: 'user2', initialPassword: generatedPassword },
]
const ca = { a: 'cert' }
const mergedSecretsWithCa = merge(cloneDeep(secrets), cloneDeep(ca))
const mergedSecretsWithGen = merge(cloneDeep(secrets), cloneDeep(generatedSecrets))
const mergedSecretsWithGenAndCa = merge(cloneDeep(mergedSecretsWithGen), cloneDeep(ca))
let deps
beforeEach(() => {
deps = {
createCustomCA: jest.fn().mockReturnValue(ca),
createK8sSecret: jest.fn(),
decrypt: jest.fn(),
existsSync: jest.fn(),
generateSecrets: jest.fn().mockReturnValue(generatedSecrets),
getStoredClusterSecrets: jest.fn().mockReturnValue(secrets),
getKmsValues: jest.fn().mockReturnValue({}),
hfValues: jest.fn().mockReturnValue(values),
loadYaml: jest.fn(),
terminal,
validateValues: jest.fn().mockReturnValue(true),
writeValues: jest.fn(),
getUsers: jest.fn().mockReturnValue(usersWithPasswords),
generatePassword: jest.fn().mockReturnValue(generatedPassword),
addInitialPasswords: jest.fn().mockReturnValue(usersWithPasswords),
addPlatformAdmin: jest.fn().mockReturnValue(usersWithPasswords),
pathExists: jest.fn().mockReturnValue(true),
}
})
describe('Creating CA', () => {
it('should ask to create a CA if issuer is custom-ca', async () => {
await processValues(deps)
expect(deps.createCustomCA).toHaveBeenCalledTimes(1)
})
})
describe('processing app values', () => {
it('should not retrieve values from env dir', async () => {
await processValues(deps)
expect(deps.hfValues).toHaveBeenCalledTimes(0)
})
it('should generate secrets by taking values and previously generated secrets as input', async () => {
deps.loadYaml.mockReturnValue(values)
await processValues(deps)
expect(deps.generateSecrets).toHaveBeenCalledWith(merge(cloneDeep(secrets), cloneDeep(values)))
expect(deps.createK8sSecret).toHaveBeenCalledTimes(1)
})
it('should overwrite a stored secret with one that was provided in values', async () => {
const newSecret = { secret: 'new' }
const valuesWithSecrets = merge(cloneDeep(values), newSecret)
const allSecrets = merge(cloneDeep(mergedSecretsWithCa), newSecret)
deps.loadYaml.mockReturnValue(valuesWithSecrets)
deps.getStoredClusterSecrets.mockReturnValue(secrets)
deps.generateSecrets.mockReturnValue(allSecrets)
await processValues(deps)
expect(deps.createK8sSecret).toHaveBeenCalledWith('otomi-generated-passwords', 'otomi', allSecrets)
expect(deps.createK8sSecret).toHaveBeenCalledTimes(1)
})
it('should create a custom ca if issuer is custom-ca or undefined and no CA yet exists', async () => {
deps.loadYaml.mockReturnValue({ apps: { 'cert-manager': { issuer: 'custom-ca' } } })
await processValues(deps)
expect(deps.createCustomCA).toHaveBeenCalled()
})
it('should not re-create a custom ca if issuer is custom-ca or undefined and a CA already exists', async () => {
deps.loadYaml.mockReturnValue({
apps: { 'cert-manager': { issuer: 'custom-ca', customRootCA: 'certpem', customRootCAKey: 'keypem' } },
})
await processValues(deps)
expect(deps.createCustomCA).not.toHaveBeenCalled()
})
it('should only store secrets', async () => {
deps.getStoredClusterSecrets.mockReturnValue(secrets)
deps.generateSecrets.mockReturnValue(generatedSecrets)
deps.createCustomCA.mockReturnValue(ca)
await processValues(deps)
expect(deps.createK8sSecret).toHaveBeenCalledWith(
'otomi-generated-passwords',
'otomi',
mergedSecretsWithGenAndCa,
)
})
it('should not overwrite stored secrets', async () => {
deps.loadYaml.mockReturnValue({})
deps.getStoredClusterSecrets.mockReturnValue(generatedSecrets)
deps.createCustomCA.mockReturnValue({})
deps.generateSecrets.mockReturnValue(generatedSecrets)
await processValues(deps)
expect(deps.generateSecrets).toHaveBeenCalledWith(generatedSecrets)
expect(deps.createK8sSecret).toHaveBeenCalledWith('otomi-generated-passwords', 'otomi', generatedSecrets)
})
it('should only write and return original values', async () => {
deps.loadYaml.mockReturnValue({
cluster: { name: 'bla', provider: 'dida' },
})
deps.createCustomCA.mockReturnValue({ a: 'cert' })
deps.getStoredClusterSecrets.mockReturnValue({
users: [{ id: 'user1', initialPassword: 'existing-password' }, { id: 'user2' }],
})
deps.generateSecrets.mockReturnValue({ gen: 'x' })
deps.createCustomCA.mockReturnValue(ca)
const res = await processValues(deps)
expect(deps.writeValues).toHaveBeenNthCalledWith(1, {
cluster: { name: 'bla', provider: 'dida' },
a: 'cert',
gen: 'x',
users: [
{ id: 'user1', initialPassword: 'existing-password' },
{ id: 'user2', initialPassword: 'generated-password' },
],
})
expect(res).toEqual({
cluster: { name: 'bla', provider: 'dida' },
users: [{ id: 'user1', initialPassword: 'existing-password' }, { id: 'user2' }],
})
})
it('should merge original with generated values and write them to env dir', async () => {
const writtenValues = merge(
cloneDeep(values),
cloneDeep(mergedSecretsWithGenAndCa),
cloneDeep({ users: usersWithPasswords }),
)
deps.loadYaml.mockReturnValue({ ...values, users })
deps.getStoredClusterSecrets.mockReturnValue(secrets)
deps.generateSecrets.mockReturnValue(generatedSecrets)
deps.getUsers.mockReturnValue(usersWithPasswords)
await processValues(deps)
expect(deps.writeValues).toHaveBeenNthCalledWith(1, writtenValues)
})
})
})
})
})