@@ -185,7 +185,6 @@ func (c *keyController) checkAndCreateKeys(ctx context.Context, syncContext fact
185
185
newKeyRequired bool
186
186
newKeyID uint64
187
187
latestKeyID uint64
188
- ok bool
189
188
reasons []string
190
189
)
191
190
@@ -196,17 +195,12 @@ func (c *keyController) checkAndCreateKeys(ctx context.Context, syncContext fact
196
195
var commonReason * string
197
196
for gr , grKeys := range desiredEncryptionState {
198
197
// if kmsKeyID in GR ReadKey is not the same as current kmsKeyID, needed is true.
199
- ks , needed := needsNewKeyWithInternalReason (grKeys , currentKeyState .Mode , currentKeyState .KMSKeyID , currentKeyState .ExternalReason , encryptedGRs )
198
+ ks , needed := needsNewKeyWithInternalReason (grKeys , currentKeyState .Mode , currentKeyState .KMSPluginHash , currentKeyState .ExternalReason , encryptedGRs )
200
199
if ! needed {
201
200
continue
202
201
}
203
202
204
- if ks .Mode != state .KMS {
205
- latestKeyID , ok = state .NameToKeyID (ks .Key .Name )
206
- if ! ok {
207
- latestKeyID = 0
208
- }
209
- }
203
+ latestKeyID = ks .Generation
210
204
211
205
if commonReason == nil {
212
206
commonReason = & ks .InternalReason
@@ -216,9 +210,8 @@ func (c *keyController) checkAndCreateKeys(ctx context.Context, syncContext fact
216
210
217
211
newKeyRequired = true
218
212
219
- // tracking the newKeyID is only required for non-KMS
220
213
nextKeyID := latestKeyID + 1
221
- if ks . Mode != state . KMS && newKeyID < nextKeyID {
214
+ if newKeyID < nextKeyID {
222
215
newKeyID = nextKeyID
223
216
}
224
217
@@ -236,7 +229,7 @@ func (c *keyController) checkAndCreateKeys(ctx context.Context, syncContext fact
236
229
237
230
var keySecret * corev1.Secret
238
231
if currentKeyState .Mode == state .KMS {
239
- keySecret , err = c .generateKMSKeySecret (currentKeyState .KMSConfig , currentKeyState .InternalReason , currentKeyState .ExternalReason )
232
+ keySecret , err = c .generateKMSKeySecret (newKeyID , currentKeyState .KMSConfig , currentKeyState .InternalReason , currentKeyState .ExternalReason )
240
233
} else {
241
234
keySecret , err = c .generateLocalKeySecret (newKeyID , currentKeyState .Mode , currentKeyState .InternalReason , currentKeyState .ExternalReason )
242
235
}
@@ -270,16 +263,19 @@ func (c *keyController) validateExistingSecret(ctx context.Context, keySecret *c
270
263
return fmt .Errorf ("secret %s/%s is invalid, new keys cannot be created for encryption target" , keySecret .Namespace , keySecret .Name )
271
264
}
272
265
273
- if ks .Mode == state .KMS && ks .KMSKeyID != "" {
266
+ if ks .Generation == 0 {
267
+ return fmt .Errorf ("secret %s/%s is invalid, key generation id cannot be zero" , keySecret .Namespace , keySecret .Name )
268
+ }
269
+
270
+ if ks .Mode == state .KMS && ks .KMSPluginHash != "" {
274
271
return nil
275
272
}
276
273
277
- if ks .Mode == state .KMS && ks .KMSKeyID == "" {
274
+ if ks .Mode == state .KMS && ks .KMSPluginHash == "" {
278
275
// kmsKeyID is mandatory in case of KMS
279
- return fmt .Errorf ("secret %s/%s is invalid, new KMS keys cannot be created for encryption target" , keySecret .Namespace , keySecret .Name )
276
+ return fmt .Errorf ("secret %s/%s is invalid, new KMS config keys cannot be created for encryption target" , keySecret .Namespace , keySecret .Name )
280
277
}
281
278
282
- // checks for local aes (non-KMS) keys only
283
279
actualKeyID , ok := state .NameToKeyID (actualKeySecret .Name )
284
280
if ! ok || actualKeyID != keyID {
285
281
// TODO we can just get stuck in degraded here ...
@@ -292,6 +288,7 @@ func (c *keyController) validateExistingSecret(ctx context.Context, keySecret *c
292
288
func (c * keyController ) generateLocalKeySecret (keyID uint64 , currentMode state.Mode , internalReason , externalReason string ) (* corev1.Secret , error ) {
293
289
bs := crypto .ModeToNewKeyFunc [currentMode ]()
294
290
ks := state.KeyState {
291
+ Generation : keyID ,
295
292
Key : apiserverv1.Key {
296
293
Name : fmt .Sprintf ("%d" , keyID ),
297
294
Secret : base64 .StdEncoding .EncodeToString (bs ),
@@ -303,19 +300,20 @@ func (c *keyController) generateLocalKeySecret(keyID uint64, currentMode state.M
303
300
return secrets .FromKeyState (c .instanceName , ks )
304
301
}
305
302
306
- func (c * keyController ) generateKMSKeySecret (kmsConfig * configv1.KMSConfig , internalReason , externalReason string ) (* corev1.Secret , error ) {
303
+ func (c * keyController ) generateKMSKeySecret (keyID uint64 , kmsConfig * configv1.KMSConfig , internalReason , externalReason string ) (* corev1.Secret , error ) {
307
304
kmsConfig = kmsConfig .DeepCopy ()
308
305
309
- kmsKeyID , err := encryptionconfig .HashKMSConfig (* kmsConfig )
306
+ kmsPluginHash , err := encryptionconfig .HashKMSConfig (* kmsConfig )
310
307
if err != nil {
311
308
return nil , err
312
309
}
313
310
314
311
ks := state.KeyState {
312
+ Generation : keyID ,
315
313
Mode : state .KMS ,
316
314
InternalReason : internalReason ,
317
315
ExternalReason : externalReason ,
318
- KMSKeyID : kmsKeyID ,
316
+ KMSPluginHash : kmsPluginHash ,
319
317
KMSConfig : kmsConfig ,
320
318
}
321
319
return secrets .FromKeyState (c .instanceName , ks )
@@ -344,16 +342,17 @@ func (c *keyController) getCurrentEncryptionModeWithExternalReason(ctx context.C
344
342
case state .KMS :
345
343
kmsConfig := apiServer .Spec .Encryption .KMS .DeepCopy ()
346
344
347
- kmsKeyID , err := encryptionconfig .HashKMSConfig (* kmsConfig )
345
+ kmsPluginHash , err := encryptionconfig .HashKMSConfig (* kmsConfig )
348
346
if err != nil {
349
- return state.KeyState {}, fmt .Errorf ("encryption mode configured: %s, but provided kms config could not generate required kms key id %v" , currentMode , err )
347
+ return state.KeyState {}, fmt .Errorf ("encryption mode configured: %s, but provided kms config could not generate required kms plugin hash %v" , currentMode , err )
350
348
}
351
349
352
350
ks := state.KeyState {
353
351
Mode : state .KMS ,
354
- KMSKeyID : kmsKeyID ,
355
- KMSConfig : kmsConfig ,
356
352
ExternalReason : reason ,
353
+
354
+ KMSPluginHash : kmsPluginHash ,
355
+ KMSConfig : kmsConfig ,
357
356
}
358
357
return ks , nil
359
358
case "" : // unspecified means use the default (which can change over time)
@@ -365,15 +364,16 @@ func (c *keyController) getCurrentEncryptionModeWithExternalReason(ctx context.C
365
364
366
365
// needsNewKeyWithInternalReason checks whether a new key must be created for the given resource. If true, it also returns the latest
367
366
// used key ID and a reason string.
368
- func needsNewKeyWithInternalReason (grKeys state.GroupResourceState , currentMode state.Mode , optionalCurrentKMSKeyID string , externalReason string , encryptedGRs []schema.GroupResource ) (state.KeyState , bool ) {
367
+ func needsNewKeyWithInternalReason (grKeys state.GroupResourceState , currentMode state.Mode , optionalCurrentKMSHash string , externalReason string , encryptedGRs []schema.GroupResource ) (state.KeyState , bool ) {
369
368
// we always need to have some encryption keys unless we are turned off
370
369
if len (grKeys .ReadKeys ) == 0 {
371
370
return state.KeyState {InternalReason : "key-does-not-exist" }, currentMode != state .Identity
372
371
}
373
372
374
373
latestKey := grKeys .ReadKeys [0 ]
375
- latestKeyID , ok := state .NameToKeyID (latestKey .Key .Name )
376
- if ! ok {
374
+ latestKeyID := latestKey .Generation
375
+
376
+ if latestKeyID == 0 {
377
377
latestKey .InternalReason = fmt .Sprintf ("key-secret-%d-is-invalid" , latestKeyID )
378
378
return latestKey , true
379
379
}
@@ -411,8 +411,8 @@ func needsNewKeyWithInternalReason(grKeys state.GroupResourceState, currentMode
411
411
return state.KeyState {}, false
412
412
}
413
413
414
- // if the hash of the kms config (kmsKeyID) has updated, we need a new KMS backing secret
415
- if currentMode == state .KMS && latestKey .KMSKeyID != optionalCurrentKMSKeyID {
414
+ // if the hash of the kms config has updated, we need a new KMS backing secret
415
+ if currentMode == state .KMS && latestKey .KMSPluginHash != optionalCurrentKMSHash {
416
416
latestKey .InternalReason = "kms-config-changed"
417
417
return latestKey , true
418
418
}
0 commit comments