Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync TUF cache used for sigstore bundle verification #166

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions pkg/tuf/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,29 +293,43 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
}

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

// GetTrustedRoot returns the trusted root for the TUF repository.
func GetTrustedRoot() (*root.TrustedRoot, error) {
once.Do(func() {
now := time.Now().UTC()
if timestamp.IsZero() || timestamp.Before(now.Add(-24*time.Hour)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Before(now.Add(-24*Hour) doesn't quite roll off the tongue. What about,

is now 24hrs after the last time we checked?
now.After(timestamp.Add(24*time.Hour))

(assuming I didn't mess up the math, time math is notoriously tricky)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems easier to understand to me

mu.Lock()
defer mu.Unlock()

tufClient, err := tuf.NewFromEnv(context.Background())
if err != nil {
singletonRootError = fmt.Errorf("initializing tuf: %w", err)
return
return nil, singletonRootError
}
// 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
return nil, singletonRootError
}
trustedRoot, singletonRootError = root.NewTrustedRootFromJSON(targetBytes)
})
if singletonRootError != nil {
return nil, singletonRootError
trustedRoot, err := root.NewTrustedRootFromJSON(targetBytes)
if err != nil {
singletonRootError = fmt.Errorf("error creating trusted root: %w", err)
return nil, singletonRootError
}

timestamp = now

return trustedRoot, nil
}

mu.RLock()
defer mu.RUnlock()

return trustedRoot, nil
}
Loading