Skip to content

Commit

Permalink
remove old fulcio root and fix fallback target code (#1738)
Browse files Browse the repository at this point in the history
Signed-off-by: Asra Ali <asraa@google.com>
  • Loading branch information
asraa committed Apr 11, 2022
1 parent c56181c commit f983706
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pkg/cosign/tuf/client.go
Expand Up @@ -28,6 +28,7 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -316,7 +317,7 @@ func (t *TUF) GetTargetsByMeta(usage UsageKind, fallbacks []string) ([]TargetFil
if scm.Sigstore.Usage == usage {
target, err := t.GetTarget(name)
if err != nil {
return nil, errors.Wrap(err, "error getting target")
return nil, errors.Wrap(err, "error getting target by usage")
}
matchedTargets = append(matchedTargets, TargetFile{Target: target, Status: scm.Sigstore.Status})
}
Expand All @@ -325,11 +326,15 @@ func (t *TUF) GetTargetsByMeta(usage UsageKind, fallbacks []string) ([]TargetFil
for _, fallback := range fallbacks {
target, err := t.GetTarget(fallback)
if err != nil {
return nil, errors.Wrap(err, "error getting target")
fmt.Fprintf(os.Stderr, "**Warning** Missing fallback target %s, skipping\n", fallback)
continue
}
matchedTargets = append(matchedTargets, TargetFile{Target: target, Status: Active})
}
}
if len(matchedTargets) == 0 {
return matchedTargets, fmt.Errorf("no matching targets by custom metadata, fallbacks not found: %s", strings.Join(fallbacks, ", "))
}
return matchedTargets, nil
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/cosign/tuf/client_test.go
Expand Up @@ -267,6 +267,17 @@ func TestGetTargetsByMeta(t *testing.T) {
if targets[0].Status != Active || targets[1].Status != Active {
t.Fatalf("target without custom metadata not active, got: %v and %v", targets[0].Status, targets[1].Status)
}
// Specify multiple fallbacks with no custom metadata.
targets, err = tufObj.GetTargetsByMeta(UnknownUsage, []string{"fooNoCustom.txt", "fooNoCustomOtherMissingTarget.txt"})
if err != nil {
t.Fatal(err)
}
if len(targets) != 1 {
t.Fatalf("expected one targets without custom metadata, got %d targets", len(targets))
}
if targets[0].Status != Active {
t.Fatalf("target without custom metadata not active, got: %v and %v", targets[0].Status, targets[1].Status)
}
// Fetch targets with custom metadata.
targets, err = tufObj.GetTargetsByMeta(Fulcio, []string{"fooNoCustom.txt"})
if err != nil {
Expand All @@ -291,7 +302,7 @@ func TestGetTargetsByMeta(t *testing.T) {
// Error when fetching target that does not exist.
_, err = tufObj.GetTargetsByMeta(UsageKind(UnknownStatus), []string{"unknown.txt"})
expectedErr := "file not found: unknown.txt"
if !strings.Contains(err.Error(), "file not found: unknown.txt") {
if !strings.Contains(err.Error(), "not found: unknown.txt") {
t.Fatalf("unexpected error fetching missing metadata, expected: %s, got: %s", expectedErr, err.Error())
}
}
Expand Down

0 comments on commit f983706

Please sign in to comment.