@@ -327,7 +327,42 @@ func (s *Store) UpdateExportedPublicKeys(ctx context.Context, rs []string) (bool
327
327
}
328
328
329
329
// add any missing keys
330
+ failed , exported := s .addMissingKeys (ctx , exp , recipients )
331
+
332
+ // remove any extra key files
333
+ // TODO(GH-2620): Temporarily disabled by default until we fix the
334
+ // key cleanup.
335
+ if config .Bool (ctx , "recipients.remove-extra-keys" ) {
336
+ f , e := s .removeExtraKeys (ctx , recipients )
337
+ failed = failed || f
338
+ exported = exported || e
339
+ }
340
+
341
+ if exported && ctxutil .IsGitCommit (ctx ) {
342
+ if err := s .storage .Commit (ctx , "Updated exported Public Keys" ); err != nil {
343
+ switch {
344
+ case errors .Is (err , store .ErrGitNothingToCommit ):
345
+ debug .Log ("nothing to commit: %s" , err )
346
+ case errors .Is (err , store .ErrGitNotInit ):
347
+ debug .Log ("git not initialized: %s" , err )
348
+ default :
349
+ failed = true
350
+
351
+ out .Errorf (ctx , "Failed to git commit: %s" , err )
352
+ }
353
+ }
354
+ }
355
+
356
+ if failed {
357
+ return exported , fmt .Errorf ("some keys failed" )
358
+ }
359
+
360
+ return exported , nil
361
+ }
362
+
363
+ func (s * Store ) addMissingKeys (ctx context.Context , exp keyExporter , recipients map [string ]bool ) (bool , bool ) {
330
364
var failed , exported bool
365
+
331
366
for r := range recipients {
332
367
if r == "" {
333
368
continue
@@ -358,7 +393,12 @@ func (s *Store) UpdateExportedPublicKeys(ctx context.Context, rs []string) (bool
358
393
}
359
394
}
360
395
361
- // remove any extra key files
396
+ return failed , exported
397
+ }
398
+
399
+ func (s * Store ) removeExtraKeys (ctx context.Context , recipients map [string ]bool ) (bool , bool ) {
400
+ var failed , exported bool
401
+
362
402
keys , err := s .storage .List (ctx , keyDir )
363
403
if err != nil {
364
404
failed = true
@@ -397,26 +437,7 @@ func (s *Store) UpdateExportedPublicKeys(ctx context.Context, rs []string) (bool
397
437
debug .Log ("Removed extra key %s" , key )
398
438
}
399
439
400
- if exported && ctxutil .IsGitCommit (ctx ) {
401
- if err := s .storage .Commit (ctx , "Updated exported Public Keys" ); err != nil {
402
- switch {
403
- case errors .Is (err , store .ErrGitNothingToCommit ):
404
- debug .Log ("nothing to commit: %s" , err )
405
- case errors .Is (err , store .ErrGitNotInit ):
406
- debug .Log ("git not initialized: %s" , err )
407
- default :
408
- failed = true
409
-
410
- out .Errorf (ctx , "Failed to git commit: %s" , err )
411
- }
412
- }
413
- }
414
-
415
- if failed {
416
- return exported , fmt .Errorf ("some keys failed" )
417
- }
418
-
419
- return exported , nil
440
+ return failed , exported
420
441
}
421
442
422
443
type recipientMarshaler interface {
0 commit comments