Skip to content

Commit 570061d

Browse files
fix: get kms values & age key generation order (#1768)
Co-authored-by: jeho <[email protected]>
1 parent 94fdabd commit 570061d

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed

src/cmd/bootstrap.test.ts

+57-46
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,6 @@ describe('Bootstrapping values', () => {
2424
}
2525
const users = [{ id: 'user1', initialPassword: 'existing-password' }, { id: 'user2' }]
2626
const secrets = { secret: 'true', deep: { nested: 'secret' } }
27-
const ageKeys = { publicKey: 'agePublicKey', privateKey: 'agePrivateKey' }
28-
const kmsValues = {
29-
kms: {
30-
sops: {
31-
provider: 'azure',
32-
azure: {
33-
keys: 'key1,key2',
34-
},
35-
},
36-
},
37-
}
38-
const kmsValuesWithAgeProvider = {
39-
kms: {
40-
sops: {
41-
provider: 'age',
42-
},
43-
},
44-
}
45-
const kmsValuesWithAgeFull = {
46-
kms: {
47-
sops: {
48-
provider: 'age',
49-
age: {
50-
publicKey: 'publicKey',
51-
privateKey: 'privateKey',
52-
},
53-
},
54-
},
55-
}
5627
let deps
5728
beforeEach(() => {
5829
deps = {
@@ -103,24 +74,64 @@ describe('Bootstrapping values', () => {
10374
const res = await getStoredClusterSecrets(deps)
10475
expect(res).toEqual(undefined)
10576
})
106-
it('should not get kms values if those do not exist', async () => {
107-
const res = await getKmsValues(values)
108-
expect(res).toEqual(undefined)
109-
})
110-
it('should get kms values if those exist', async () => {
111-
const res = await getKmsValues({ ...values, ...kmsValues })
112-
expect(res).toEqual(kmsValues)
113-
})
114-
it('should generate and return new age keys if provider is age and keys are missing', async () => {
115-
const kmsValuesDeps = {
116-
generateAgeKeys: jest.fn().mockResolvedValue(ageKeys),
77+
describe('getKmsValues', () => {
78+
let kmsValuesDeps: any
79+
const ageKeys = { publicKey: 'agePublicKey', privateKey: 'agePrivateKey' }
80+
const values = { someKey: 'someValue' }
81+
const kmsValues = {
82+
kms: {
83+
sops: {
84+
provider: 'azure',
85+
azure: {
86+
keys: 'key1,key2',
87+
},
88+
},
89+
},
11790
}
118-
const res = await getKmsValues({ ...values, ...kmsValuesWithAgeProvider }, kmsValuesDeps)
119-
expect(res).toEqual({ kms: { sops: { provider: 'age', age: ageKeys } } })
120-
})
121-
it('should get kms values if age has public and private key', async () => {
122-
const res = await getKmsValues({ ...values, ...kmsValuesWithAgeFull })
123-
expect(res).toEqual(kmsValuesWithAgeFull)
91+
const kmsValuesWithAgeProvider = {
92+
kms: {
93+
sops: {
94+
provider: 'age',
95+
},
96+
},
97+
}
98+
const kmsValuesWithAgeFull = {
99+
kms: {
100+
sops: {
101+
provider: 'age',
102+
age: {
103+
publicKey: 'publicKey',
104+
privateKey: 'privateKey',
105+
},
106+
},
107+
},
108+
}
109+
beforeEach(() => {
110+
kmsValuesDeps = {
111+
generateAgeKeys: jest.fn().mockResolvedValue(ageKeys),
112+
hfValues: jest.fn(),
113+
}
114+
})
115+
it('should not get kms values if those do not exist', async () => {
116+
kmsValuesDeps.hfValues.mockReturnValue(values)
117+
const res = await getKmsValues(kmsValuesDeps)
118+
expect(res).toBeUndefined()
119+
})
120+
it('should get kms values if those exist', async () => {
121+
kmsValuesDeps.hfValues.mockReturnValue({ ...values, ...kmsValues })
122+
const res = await getKmsValues(kmsValuesDeps)
123+
expect(res).toEqual(kmsValues)
124+
})
125+
it('should generate and return new age keys if provider is age and keys are missing', async () => {
126+
kmsValuesDeps.hfValues.mockReturnValue({ ...values, ...kmsValuesWithAgeProvider })
127+
const res = await getKmsValues(kmsValuesDeps)
128+
expect(res).toEqual({ kms: { sops: { provider: 'age', age: ageKeys } } })
129+
})
130+
it('should get kms values if age has public and private key', async () => {
131+
kmsValuesDeps.hfValues.mockReturnValue({ ...values, ...kmsValuesWithAgeFull })
132+
const res = await getKmsValues(kmsValuesDeps)
133+
expect(res).toEqual(kmsValuesWithAgeFull)
134+
})
124135
})
125136
it('should set k8sContext and owner if needed', async () => {
126137
deps.processValues.mockReturnValue(values)

src/cmd/bootstrap.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ export const generateAgeKeys = async (deps = { $, terminal }) => {
183183
}
184184
}
185185

186-
export const getKmsValues = async (originalValues: any, deps = { generateAgeKeys }) => {
187-
const kms = originalValues?.kms
186+
export const getKmsValues = async (deps = { generateAgeKeys, hfValues }) => {
187+
const values = (await deps.hfValues({ defaultValues: true })) as Record<string, any>
188+
const kms = values?.kms
188189
if (!kms) return undefined
189190
const provider = kms?.sops?.provider
190191
if (!provider) return {}
@@ -260,13 +261,11 @@ export const processValues = async (
260261
const { ENV_DIR, VALUES_INPUT } = env
261262
let originalInput: Record<string, any> | undefined
262263
let storedSecrets: Record<string, any> | undefined
263-
let kmsValues: Record<string, any> | undefined
264264
if (deps.isChart) {
265265
d.log(`Loading app values from ${VALUES_INPUT}`)
266266
const originalValues = (await deps.loadYaml(VALUES_INPUT)) as Record<string, any>
267267
storedSecrets = (await deps.getStoredClusterSecrets()) || {}
268-
kmsValues = (await deps.getKmsValues(originalValues)) || {}
269-
originalInput = merge(cloneDeep(storedSecrets || {}), cloneDeep(originalValues), cloneDeep(kmsValues))
268+
originalInput = merge(cloneDeep(storedSecrets || {}), cloneDeep(originalValues))
270269
await deps.writeValues(originalInput)
271270
} else {
272271
d.log(`Loading repo values from ${ENV_DIR}`)
@@ -287,8 +286,15 @@ export const processValues = async (
287286
} else {
288287
caSecrets = deps.createCustomCA()
289288
}
289+
// get any kms values & generate age keys if needed
290+
const kmsValues = (await deps.getKmsValues()) || {}
290291
// merge existing secrets over newly generated ones to keep them
291-
const allSecrets = merge(cloneDeep(caSecrets), cloneDeep(storedSecrets), cloneDeep(generatedSecrets))
292+
const allSecrets = merge(
293+
cloneDeep(caSecrets),
294+
cloneDeep(storedSecrets),
295+
cloneDeep(generatedSecrets),
296+
cloneDeep(kmsValues),
297+
)
292298
// generate initial passwords for users if they don't have one
293299
const users = get(originalInput, 'users', [])
294300
for (const user of users) {

0 commit comments

Comments
 (0)