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

Fixing issue 3743 #3744

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 19 additions & 4 deletions pkg/cosign/tsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ func GetTufTargets(ctx context.Context, usage tuf.UsageKind, names []string) ([]
return buffer.Bytes(), nil
}

func isTufTargetExist(ctx context.Context, name string) (bool, error) {
tufClient, err := tuf.NewFromEnv(ctx)
if err != nil {
return false, fmt.Errorf("error creating TUF client: %w", err)
}
_, err = tufClient.GetTarget(name)
if err != nil {
return false, nil
}
return true, nil
}

// GetTSACerts retrieves trusted TSA certificates from the embedded or cached
// TUF root. If expired, makes a network call to retrieve the updated targets.
// By default, the certificates come from TUF, but you can override this for test
Expand All @@ -68,7 +80,7 @@ func GetTSACerts(ctx context.Context, certChainPath string, fn GetTargetStub) (*

var raw []byte
var err error

var isExist bool
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, i'd just call this exists

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@haydentherapper Thanks for the comment. Done

switch {
case altTSACert != "":
raw, err = os.ReadFile(altTSACert)
Expand All @@ -78,8 +90,11 @@ func GetTSACerts(ctx context.Context, certChainPath string, fn GetTargetStub) (*
certNames := []string{tsaLeafCertStr, tsaRootCertStr}
for i := 0; ; i++ {
intermediateCertStr := fmt.Sprintf(tsaIntermediateCertStrPattern, i)
_, err := fn(ctx, tuf.TSA, []string{intermediateCertStr})
isExist, err = isTufTargetExist(ctx, intermediateCertStr)
if err != nil {
return nil, fmt.Errorf("error fetching TSA certificates: %w", err)
}
if !isExist {
break
}
certNames = append(certNames, intermediateCertStr)
Expand All @@ -99,8 +114,8 @@ func GetTSACerts(ctx context.Context, certChainPath string, fn GetTargetStub) (*
return nil, fmt.Errorf("error splitting TSA certificates: %w", err)
}

if len(leaves) > 1 {
return nil, fmt.Errorf("TSA certificate chain must contain at most one leaf certificate")
if len(leaves) != 1 {
return nil, fmt.Errorf("TSA certificate chain must contain exactly one leaf certificate")
}

if len(roots) == 0 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/cosign/tsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ func TestGetTSACertsFromTUF(t *testing.T) {
require.NotNil(t, tsaCerts.RootCert)
require.Len(t, tsaCerts.RootCert, 1)
}

func TestGetTSACertsFromLocalTUF(t *testing.T) {
_, err := GetTSACerts(context.Background(), "", GetTufTargets)
if err != nil {
t.Fatalf("Failed to get TSA certs from TUF: %v", err)
}
}
Loading