Skip to content

Commit

Permalink
return global error
Browse files Browse the repository at this point in the history
Signed-off-by: Asra Ali <[email protected]>
  • Loading branch information
asraa committed Jun 6, 2022
1 parent 77de032 commit 8719e02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
36 changes: 21 additions & 15 deletions pkg/cosign/tuf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
// subsequent invocations of initializeTUF
singletonTUF *TUF
singletonTUFOnce = new(sync.Once)
singletonTUFErr error
)

var GetRemoteRoot = func() string {
Expand Down Expand Up @@ -233,43 +234,45 @@ func GetRootStatus(ctx context.Context) (*RootStatus, error) {
// targets in a targets/ subfolder.
// * forceUpdate: indicates checking the remote for an update, even when the local
// timestamp.json is up to date.
func initializeTUF(ctx context.Context, mirror string, root []byte, embedded fs.FS, forceUpdate bool) *TUF {
func initializeTUF(ctx context.Context, mirror string, root []byte, embedded fs.FS, forceUpdate bool) (*TUF, error) {
singletonTUFOnce.Do(func() {
t := &TUF{
mirror: mirror,
embedded: embedded,
}

t.targets = newFileImpl()
var err error
t.local, err = newLocalStore()
if err != nil {
panic(err)
t.local, singletonTUFErr = newLocalStore()
if singletonTUFErr != nil {
return
}

t.remote, err = remoteFromMirror(ctx, t.mirror)
if err != nil {
panic(err)
t.remote, singletonTUFErr = remoteFromMirror(ctx, t.mirror)
if singletonTUFErr != nil {
return
}

t.client = client.NewClient(t.local, t.remote)

trustedMeta, err := t.local.GetMeta()
if err != nil {
panic(fmt.Errorf("getting trusted meta: %w", err))
singletonTUFErr = fmt.Errorf("getting trusted meta: %w", err)
return
}

// If the caller does not supply a root, then either use the root in the local store
// or default to the embedded one.
if root == nil {
root, err = getRoot(trustedMeta, t.embedded)
if err != nil {
panic(fmt.Errorf("getting trusted root: %w", err))
singletonTUFErr = fmt.Errorf("getting trusted root: %w", err)
return
}
}

if err := t.client.InitLocal(root); err != nil {
panic(fmt.Errorf("unable to initialize client, local cache may be corrupt: %w", err))
singletonTUFErr = fmt.Errorf("unable to initialize client, local cache may be corrupt: %w", err)
return
}

// We may already have an up-to-date local store! Check to see if it needs to be updated.
Expand All @@ -282,13 +285,14 @@ func initializeTUF(ctx context.Context, mirror string, root []byte, embedded fs.

// Update if local is not populated or out of date.
if err := t.updateMetadataAndDownloadTargets(); err != nil {
panic(fmt.Errorf("updating local metadata and targets: %w", err))
singletonTUFErr = fmt.Errorf("updating local metadata and targets: %w", err)
return
}

// We're golden so stash the TUF object for later use
singletonTUF = t
})
return singletonTUF
return singletonTUF, singletonTUFErr
}

func NewFromEnv(ctx context.Context) (*TUF, error) {
Expand All @@ -303,12 +307,14 @@ func NewFromEnv(ctx context.Context) (*TUF, error) {
}

// Initializes a new TUF object from the local cache or defaults.
return initializeTUF(ctx, mirror, nil, GetEmbedded(), false), nil
return initializeTUF(ctx, mirror, nil, GetEmbedded(), false)
}

func Initialize(ctx context.Context, mirror string, root []byte) error {
// Initialize the client. Force an update with remote.
_ = initializeTUF(ctx, mirror, root, GetEmbedded(), true)
if _, err := initializeTUF(ctx, mirror, root, GetEmbedded(), true); err != nil {
return err
}

// Store the remote for later if we are caching.
if !noCache() {
Expand Down
16 changes: 4 additions & 12 deletions pkg/cosign/tuf/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,10 @@ func TestCustomRoot(t *testing.T) {
return true
}

// Force a panic error that the remote metadata is expired.
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("NewFromEnv with expired remote metadata should have panicked!")
}
}()
// This should cause a panic
if _, err = NewFromEnv(ctx); err == nil {
t.Errorf("expected expired timestamp from the remote")
}
}()
// This should cause an error that remote metadata is expired.
if _, err = NewFromEnv(ctx); err == nil {
t.Errorf("expected expired timestamp from the remote")
}

// Let internal TUF verification succeed normally now.
verify.IsExpired = oldIsExpired
Expand Down

0 comments on commit 8719e02

Please sign in to comment.