Skip to content

Commit abc8ff3

Browse files
committed
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 563c485 commit abc8ff3

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
@@ -299,10 +299,9 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
299299
}
300300

301301
var (
302-
mu sync.RWMutex
303-
singletonRootError error
304-
timestamp time.Time
305-
trustedRoot *root.TrustedRoot
302+
mu sync.RWMutex
303+
timestamp time.Time
304+
trustedRoot *root.TrustedRoot
306305
)
307306

308307
// GetTrustedRoot returns the trusted root for the TUF repository.
@@ -317,19 +316,16 @@ func GetTrustedRoot(ctx context.Context) (*root.TrustedRoot, error) {
317316

318317
tufClient, err := tuf.NewFromEnv(context.Background())
319318
if err != nil {
320-
singletonRootError = fmt.Errorf("initializing tuf: %w", err)
321-
return nil, singletonRootError
319+
return nil, fmt.Errorf("initializing tuf: %w", err)
322320
}
323321
// TODO: add support for custom trusted root path
324322
targetBytes, err := tufClient.GetTarget("trusted_root.json")
325323
if err != nil {
326-
singletonRootError = fmt.Errorf("error getting targets: %w", err)
327-
return nil, singletonRootError
324+
return nil, fmt.Errorf("error getting targets: %w", err)
328325
}
329-
trustedRoot, err := root.NewTrustedRootFromJSON(targetBytes)
326+
trustedRoot, err = root.NewTrustedRootFromJSON(targetBytes)
330327
if err != nil {
331-
singletonRootError = fmt.Errorf("error creating trusted root: %w", err)
332-
return nil, singletonRootError
328+
return nil, fmt.Errorf("error creating trusted root: %w", err)
333329
}
334330

335331
timestamp = now

0 commit comments

Comments
 (0)