@@ -219,18 +219,24 @@ type keyExporter interface {
219
219
ExportPublicKey (ctx context.Context , id string ) ([]byte , error )
220
220
}
221
221
222
- // ExportMissingPublicKeys will export any possibly missing public keys to the
222
+ // UpdateExportedPublicKeys will export any possibly missing public keys to the
223
223
// stores .public-keys directory.
224
- func (s * Store ) ExportMissingPublicKeys (ctx context.Context , rs []string ) (bool , error ) {
224
+ func (s * Store ) UpdateExportedPublicKeys (ctx context.Context , rs []string ) (bool , error ) {
225
225
exp , ok := s .crypto .(keyExporter )
226
226
if ! ok {
227
227
debug .Log ("not exporting public keys for %T" , s .crypto )
228
228
229
229
return false , nil
230
230
}
231
231
232
- var failed , exported bool
232
+ recipients := make ( map [ string ] bool , len ( rs ))
233
233
for _ , r := range rs {
234
+ recipients [r ] = true
235
+ }
236
+
237
+ // add any missing keys
238
+ var failed , exported bool
239
+ for r := range recipients {
234
240
if r == "" {
235
241
continue
236
242
}
@@ -258,13 +264,43 @@ func (s *Store) ExportMissingPublicKeys(ctx context.Context, rs []string) (bool,
258
264
259
265
continue
260
266
}
267
+ }
268
+
269
+ // remove any extra key files
270
+ keys , err := s .storage .List (ctx , keyDir )
271
+ if err != nil {
272
+ failed = true
273
+
274
+ out .Errorf (ctx , "Failed to list keys: %s" , err )
275
+ }
276
+
277
+ debug .Log ("Checking %q for extra keys that need to be removed" , keys )
278
+ for _ , key := range keys {
279
+ key := strings .TrimPrefix (key , keyDir + string (filepath .Separator ))
280
+ if ! recipients [key ] {
281
+ if err := s .storage .Delete (ctx , filepath .Join (keyDir , key )); err != nil {
282
+ out .Errorf (ctx , "Failed to remove extra key %q: %s" , key , err )
283
+
284
+ continue
285
+ }
286
+
287
+ if err := s .storage .Add (ctx , filepath .Join (keyDir , key )); err != nil {
288
+ out .Errorf (ctx , "Failed to mark extra key for removal %q: %s" , key , err )
289
+
290
+ continue
291
+ }
292
+
293
+ // to ensure the commit
294
+ exported = true
295
+ debug .Log ("Removed extra key %s" , key )
296
+ }
297
+ }
261
298
262
- if err := s .storage .Commit (ctx , fmt .Sprintf ("Exported Public Keys %s" , r )); err != nil && ! errors .Is (err , store .ErrGitNothingToCommit ) {
299
+ if exported {
300
+ if err := s .storage .Commit (ctx , fmt .Sprintf ("Updated exported Public Keys" )); err != nil && ! errors .Is (err , store .ErrGitNothingToCommit ) {
263
301
failed = true
264
302
265
303
out .Errorf (ctx , "Failed to git commit: %s" , err )
266
-
267
- continue
268
304
}
269
305
}
270
306
@@ -302,9 +338,12 @@ func (s *Store) saveRecipients(ctx context.Context, rs []string, msg string) err
302
338
303
339
// save all recipients public keys to the repo
304
340
if ctxutil .IsExportKeys (ctx ) {
305
- if _ , err := s .ExportMissingPublicKeys (ctx , rs ); err != nil {
341
+ debug .Log ("updating exported keys" )
342
+ if _ , err := s .UpdateExportedPublicKeys (ctx , rs ); err != nil {
306
343
out .Errorf (ctx , "Failed to export missing public keys: %s" , err )
307
344
}
345
+ } else {
346
+ debug .Log ("updating exported keys not requested" )
308
347
}
309
348
310
349
// push to remote repo
0 commit comments