@@ -293,29 +293,45 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
293
293
}
294
294
295
295
var (
296
- once sync.Once
297
- trustedRoot * root.TrustedRoot
296
+ mu sync.RWMutex
298
297
singletonRootError error
298
+ timestamp time.Time
299
+ trustedRoot * root.TrustedRoot
299
300
)
300
301
301
302
// GetTrustedRoot returns the trusted root for the TUF repository.
302
303
func GetTrustedRoot () (* root.TrustedRoot , error ) {
303
- once .Do (func () {
304
+ now := time .Now ().UTC ()
305
+ // check if timestamp has never been or if the current time is more
306
+ // than 24 hours after the current value of timestamp
307
+ if timestamp .IsZero () || now .After (timestamp .Add (24 * time .Hour )) {
308
+ mu .Lock ()
309
+ defer mu .Unlock ()
310
+
304
311
tufClient , err := tuf .NewFromEnv (context .Background ())
305
312
if err != nil {
306
313
singletonRootError = fmt .Errorf ("initializing tuf: %w" , err )
307
- return
314
+ return nil , singletonRootError
308
315
}
309
316
// TODO: add support for custom trusted root path
310
317
targetBytes , err := tufClient .GetTarget ("trusted_root.json" )
311
318
if err != nil {
312
319
singletonRootError = fmt .Errorf ("error getting targets: %w" , err )
313
- return
320
+ return nil , singletonRootError
314
321
}
315
- trustedRoot , singletonRootError = root .NewTrustedRootFromJSON (targetBytes )
316
- })
317
- if singletonRootError != nil {
318
- return nil , singletonRootError
322
+ trustedRoot , err := root .NewTrustedRootFromJSON (targetBytes )
323
+ if err != nil {
324
+ singletonRootError = fmt .Errorf ("error creating trusted root: %w" , err )
325
+ return nil , singletonRootError
326
+ }
327
+
328
+ timestamp = now
329
+
330
+ return trustedRoot , nil
319
331
}
332
+
333
+ mu .RLock ()
334
+ defer mu .RUnlock ()
335
+
320
336
return trustedRoot , nil
321
337
}
0 commit comments