Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 60096f2

Browse files
committed
Add func to retrieve TrustedRoot from TUF
Signed-off-by: Cody Soyland <[email protected]> Sync TUF cache used for sigstore bundle verification (#166) * sync tuf cache used for sigstore bundle verification Signed-off-by: Meredith Lancaster <[email protected]> * remove singleton err Signed-off-by: Meredith Lancaster <[email protected]> * start adding lock Signed-off-by: Meredith Lancaster <[email protected]> * Use RWMutex Signed-off-by: Meredith Lancaster <[email protected]> * pr feedback Signed-off-by: Meredith Lancaster <[email protected]> --------- Signed-off-by: Meredith Lancaster <[email protected]> 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]> Update go.mod Signed-off-by: Cody Soyland <[email protected]>
1 parent 7845832 commit 60096f2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require (
6464
github.com/go-jose/go-jose/v4 v4.0.5
6565
github.com/sigstore/protobuf-specs v0.4.1
6666
github.com/sigstore/scaffolding v0.7.22
67+
github.com/sigstore/sigstore-go v0.7.1
6768
github.com/sigstore/sigstore/pkg/signature/kms/aws v1.9.3
6869
github.com/sigstore/sigstore/pkg/signature/kms/azure v1.9.3
6970
github.com/sigstore/sigstore/pkg/signature/kms/gcp v1.9.3
@@ -228,7 +229,6 @@ require (
228229
github.com/sassoftware/relic v7.2.1+incompatible // indirect
229230
github.com/secure-systems-lab/go-securesystemslib v0.9.0 // indirect
230231
github.com/shibumi/go-pathspec v1.3.0 // indirect
231-
github.com/sigstore/sigstore-go v0.7.1 // indirect
232232
github.com/sigstore/timestamp-authority v1.2.5 // indirect
233233
github.com/sirupsen/logrus v1.9.3 // indirect
234234
github.com/sourcegraph/conc v0.3.0 // indirect

pkg/tuf/repo.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import (
2828
"path/filepath"
2929
"runtime"
3030
"strings"
31+
"sync"
3132
"testing/fstest"
3233
"time"
3334

35+
"github.com/sigstore/sigstore-go/pkg/root"
36+
"github.com/sigstore/sigstore/pkg/tuf"
3437
"github.com/theupdateframework/go-tuf/client"
3538
"sigs.k8s.io/release-utils/version"
3639
)
@@ -294,3 +297,43 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
294297
}
295298
return tufClient, nil
296299
}
300+
301+
var (
302+
mu sync.RWMutex
303+
timestamp time.Time
304+
trustedRoot *root.TrustedRoot
305+
)
306+
307+
// GetTrustedRoot returns the trusted root for the TUF repository.
308+
func GetTrustedRoot() (*root.TrustedRoot, error) {
309+
now := time.Now().UTC()
310+
// check if timestamp has never been or if the current time is more
311+
// than 24 hours after the current value of timestamp
312+
if timestamp.IsZero() || now.After(timestamp.Add(24*time.Hour)) {
313+
mu.Lock()
314+
defer mu.Unlock()
315+
316+
tufClient, err := tuf.NewFromEnv(context.Background())
317+
if err != nil {
318+
return nil, fmt.Errorf("initializing tuf: %w", err)
319+
}
320+
// TODO: add support for custom trusted root path
321+
targetBytes, err := tufClient.GetTarget("trusted_root.json")
322+
if err != nil {
323+
return nil, fmt.Errorf("error getting targets: %w", err)
324+
}
325+
trustedRoot, err = root.NewTrustedRootFromJSON(targetBytes)
326+
if err != nil {
327+
return nil, fmt.Errorf("error creating trusted root: %w", err)
328+
}
329+
330+
timestamp = now
331+
332+
return trustedRoot, nil
333+
}
334+
335+
mu.RLock()
336+
defer mu.RUnlock()
337+
338+
return trustedRoot, nil
339+
}

0 commit comments

Comments
 (0)