Skip to content

Commit

Permalink
Fix shadowed trustedroot (#178)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
codysoyland committed Nov 18, 2024
1 parent c4af6b7 commit cb5e546
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions pkg/tuf/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,9 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
}

var (
mu sync.RWMutex
singletonRootError error
timestamp time.Time
trustedRoot *root.TrustedRoot
mu sync.RWMutex
timestamp time.Time
trustedRoot *root.TrustedRoot
)

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

tufClient, err := tuf.NewFromEnv(context.Background())
if err != nil {
singletonRootError = fmt.Errorf("initializing tuf: %w", err)
return nil, singletonRootError
return nil, fmt.Errorf("initializing tuf: %w", err)
}
// TODO: add support for custom trusted root path
targetBytes, err := tufClient.GetTarget("trusted_root.json")
if err != nil {
singletonRootError = fmt.Errorf("error getting targets: %w", err)
return nil, singletonRootError
return nil, fmt.Errorf("error getting targets: %w", err)
}
trustedRoot, err := root.NewTrustedRootFromJSON(targetBytes)
trustedRoot, err = root.NewTrustedRootFromJSON(targetBytes)
if err != nil {
singletonRootError = fmt.Errorf("error creating trusted root: %w", err)
return nil, singletonRootError
return nil, fmt.Errorf("error creating trusted root: %w", err)
}

timestamp = now
Expand Down

0 comments on commit cb5e546

Please sign in to comment.