Skip to content

Commit 4db97ca

Browse files
authored
Fix shadowed trustedroot (#178)
* Fix shadowed variable bug This code caused the singleton `trustedRoot` to be returned as nil on subsequent calls. The singleton was shadowed when the variable was redeclared in the `if` block. Signed-off-by: Cody Soyland <[email protected]> * Remove unused singleton `singletonRootError` was never returned without being overwritten, so it was essentially unused. I think it's wise to always retry the TUF call on future invocations in case of network errors. Signed-off-by: Cody Soyland <[email protected]> --------- Signed-off-by: Cody Soyland <[email protected]>
1 parent 4d0a2d6 commit 4db97ca

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

pkg/tuf/repo.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,9 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
293293
}
294294

295295
var (
296-
mu sync.RWMutex
297-
singletonRootError error
298-
timestamp time.Time
299-
trustedRoot *root.TrustedRoot
296+
mu sync.RWMutex
297+
timestamp time.Time
298+
trustedRoot *root.TrustedRoot
300299
)
301300

302301
// GetTrustedRoot returns the trusted root for the TUF repository.
@@ -311,19 +310,16 @@ func GetTrustedRoot(ctx context.Context) (*root.TrustedRoot, error) {
311310

312311
tufClient, err := tuf.NewFromEnv(context.Background())
313312
if err != nil {
314-
singletonRootError = fmt.Errorf("initializing tuf: %w", err)
315-
return nil, singletonRootError
313+
return nil, fmt.Errorf("initializing tuf: %w", err)
316314
}
317315
// TODO: add support for custom trusted root path
318316
targetBytes, err := tufClient.GetTarget("trusted_root.json")
319317
if err != nil {
320-
singletonRootError = fmt.Errorf("error getting targets: %w", err)
321-
return nil, singletonRootError
318+
return nil, fmt.Errorf("error getting targets: %w", err)
322319
}
323-
trustedRoot, err := root.NewTrustedRootFromJSON(targetBytes)
320+
trustedRoot, err = root.NewTrustedRootFromJSON(targetBytes)
324321
if err != nil {
325-
singletonRootError = fmt.Errorf("error creating trusted root: %w", err)
326-
return nil, singletonRootError
322+
return nil, fmt.Errorf("error creating trusted root: %w", err)
327323
}
328324

329325
timestamp = now

0 commit comments

Comments
 (0)