Skip to content

Commit 022cdb1

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 (sigstore#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 (sigstore#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 1e0a8a4 commit 022cdb1

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require (
6464
github.com/go-jose/go-jose/v4 v4.0.4
6565
github.com/sigstore/protobuf-specs v0.3.2
6666
github.com/sigstore/scaffolding v0.7.11
67+
github.com/sigstore/sigstore-go v0.6.2
6768
github.com/sigstore/sigstore/pkg/signature/kms/aws v1.8.10
6869
github.com/sigstore/sigstore/pkg/signature/kms/azure v1.8.10
6970
github.com/sigstore/sigstore/pkg/signature/kms/gcp v1.8.10
@@ -231,7 +232,6 @@ require (
231232
github.com/sassoftware/relic v7.2.1+incompatible // indirect
232233
github.com/secure-systems-lab/go-securesystemslib v0.8.0 // indirect
233234
github.com/shibumi/go-pathspec v1.3.0 // indirect
234-
github.com/sigstore/sigstore-go v0.6.2 // indirect
235235
github.com/sigstore/timestamp-authority v1.2.2 // indirect
236236
github.com/sirupsen/logrus v1.9.3 // indirect
237237
github.com/sourcegraph/conc v0.3.0 // indirect

pkg/tuf/repo.go

+43
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)