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