From 5cc2f6af1abb2cf6253ccf62cec637d5353ea06b Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 18 Mar 2024 08:57:54 -0600 Subject: [PATCH] clean up nil client checks Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/artifact/image.go | 3 -- pkg/cmd/attestation/download/download.go | 5 ++- pkg/cmd/attestation/download/download_test.go | 11 +++---- pkg/cmd/attestation/download/metadata.go | 6 ++++ pkg/cmd/attestation/inspect/inspect_test.go | 9 ++---- pkg/cmd/attestation/verify/verify_test.go | 32 ++++++++----------- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/pkg/cmd/attestation/artifact/image.go b/pkg/cmd/attestation/artifact/image.go index b5b9b486e..839e493af 100644 --- a/pkg/cmd/attestation/artifact/image.go +++ b/pkg/cmd/attestation/artifact/image.go @@ -8,9 +8,6 @@ import ( ) func digestContainerImageArtifact(url string, client oci.Client) (*DigestedArtifact, error) { - if client == nil { - return nil, fmt.Errorf("missing OCI client") - } // try to parse the url as a valid registry reference named, err := reference.Parse(url) if err != nil { diff --git a/pkg/cmd/attestation/download/download.go b/pkg/cmd/attestation/download/download.go index fd887ce67..633723f27 100644 --- a/pkg/cmd/attestation/download/download.go +++ b/pkg/cmd/attestation/download/download.go @@ -81,6 +81,8 @@ func NewDownloadCmd(f *cmdutil.Factory, runF func(*Options) error) *cobra.Comman opts.OCIClient = oci.NewLiveClient() + opts.Store = NewLiveStore("") + if err := auth.IsHostSupported(); err != nil { return err } @@ -107,9 +109,6 @@ func NewDownloadCmd(f *cmdutil.Factory, runF func(*Options) error) *cobra.Comman } func runDownload(opts *Options) error { - if opts.APIClient == nil { - return fmt.Errorf("missing API client") - } artifact, err := artifact.NewDigestedArtifact(opts.OCIClient, opts.ArtifactPath, opts.DigestAlgorithm) if err != nil { return fmt.Errorf("failed to digest artifact: %w", err) diff --git a/pkg/cmd/attestation/download/download_test.go b/pkg/cmd/attestation/download/download_test.go index 6f0385114..78d41a138 100644 --- a/pkg/cmd/attestation/download/download_test.go +++ b/pkg/cmd/attestation/download/download_test.go @@ -167,6 +167,10 @@ func TestNewDownloadCmd(t *testing.T) { assert.Equal(t, tc.wants.Limit, opts.Limit) assert.Equal(t, tc.wants.Owner, opts.Owner) assert.Equal(t, tc.wants.Repo, opts.Repo) + assert.NotNil(t, opts.APIClient) + assert.NotNil(t, opts.OCIClient) + assert.NotNil(t, opts.Logger) + assert.NotNil(t, opts.Store) }) } } @@ -289,13 +293,6 @@ func TestRunDownload(t *testing.T) { require.ErrorContains(t, err, "failed to digest artifact") }) - t.Run("with missing OCI client", func(t *testing.T) { - customOpts := baseOpts - customOpts.ArtifactPath = "oci://ghcr.io/github/test" - customOpts.OCIClient = nil - require.Error(t, runDownload(&customOpts)) - }) - t.Run("with missing API client", func(t *testing.T) { customOpts := baseOpts customOpts.APIClient = nil diff --git a/pkg/cmd/attestation/download/metadata.go b/pkg/cmd/attestation/download/metadata.go index 4b2c0e9d1..ac2ffe1dc 100644 --- a/pkg/cmd/attestation/download/metadata.go +++ b/pkg/cmd/attestation/download/metadata.go @@ -62,3 +62,9 @@ func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp [ return metadataFilePath, nil } + +func NewLiveStore(outputPath string) *LiveStore { + return &LiveStore{ + outputPath: outputPath, + } +} diff --git a/pkg/cmd/attestation/inspect/inspect_test.go b/pkg/cmd/attestation/inspect/inspect_test.go index da9449635..139e34505 100644 --- a/pkg/cmd/attestation/inspect/inspect_test.go +++ b/pkg/cmd/attestation/inspect/inspect_test.go @@ -116,6 +116,8 @@ func TestNewInspectCmd(t *testing.T) { assert.Equal(t, tc.wants.BundlePath, opts.BundlePath) assert.Equal(t, tc.wants.DigestAlgorithm, opts.DigestAlgorithm) assert.Equal(t, tc.wants.JsonResult, opts.JsonResult) + assert.NotNil(t, opts.OCIClient) + assert.NotNil(t, opts.Logger) }) } } @@ -144,11 +146,4 @@ func TestRunInspect(t *testing.T) { customOpts.BundlePath = test.NormalizeRelativePath("../test/data/non-existent-sigstoreBundle.json") require.Error(t, runInspect(&customOpts)) }) - - t.Run("with missing OCI client", func(t *testing.T) { - customOpts := opts - customOpts.ArtifactPath = "oci://ghcr.io/github/test" - customOpts.OCIClient = nil - require.Error(t, runInspect(&customOpts)) - }) } diff --git a/pkg/cmd/attestation/verify/verify_test.go b/pkg/cmd/attestation/verify/verify_test.go index 5878b44a3..3de7e3e8f 100644 --- a/pkg/cmd/attestation/verify/verify_test.go +++ b/pkg/cmd/attestation/verify/verify_test.go @@ -50,8 +50,8 @@ func TestNewVerifyCmd(t *testing.T) { name: "Invalid digest-alg flag", cli: "../test/data/sigstore-js-2.1.0.tgz --bundle ../test/data/sigstore-js-2.1.0-bundle.json --digest-alg sha384 --owner sigstore", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", - BundlePath: "../test/data/sigstore-js-2.1.0-bundle.json", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), + BundlePath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0-bundle.json"), DigestAlgorithm: "sha384", Limit: 30, OIDCIssuer: GitHubOIDCIssuer, @@ -63,8 +63,8 @@ func TestNewVerifyCmd(t *testing.T) { name: "Use default digest-alg value", cli: "../test/data/sigstore-js-2.1.0.tgz --bundle ../test/data/sigstore-js-2.1.0-bundle.json --owner sigstore", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", - BundlePath: "../test/data/sigstore-js-2.1.0-bundle.json", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), + BundlePath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0-bundle.json"), DigestAlgorithm: "sha256", Limit: 30, OIDCIssuer: GitHubOIDCIssuer, @@ -77,8 +77,8 @@ func TestNewVerifyCmd(t *testing.T) { name: "Use custom digest-alg value", cli: "../test/data/sigstore-js-2.1.0.tgz --bundle ../test/data/sigstore-js-2.1.0-bundle.json --digest-alg sha512 --owner sigstore", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", - BundlePath: "../test/data/sigstore-js-2.1.0-bundle.json", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), + BundlePath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0-bundle.json"), DigestAlgorithm: "sha512", Limit: 30, OIDCIssuer: GitHubOIDCIssuer, @@ -91,7 +91,7 @@ func TestNewVerifyCmd(t *testing.T) { name: "Missing owner and repo flags", cli: "../test/data/sigstore-js-2.1.0.tgz", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), DigestAlgorithm: "sha256", OIDCIssuer: GitHubOIDCIssuer, Owner: "sigstore", @@ -104,7 +104,7 @@ func TestNewVerifyCmd(t *testing.T) { name: "Has both owner and repo flags", cli: "../test/data/sigstore-js-2.1.0.tgz --owner sigstore --repo sigstore/sigstore-js", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), DigestAlgorithm: "sha256", OIDCIssuer: GitHubOIDCIssuer, Owner: "sigstore", @@ -117,7 +117,7 @@ func TestNewVerifyCmd(t *testing.T) { name: "Uses default limit flag", cli: "../test/data/sigstore-js-2.1.0.tgz --owner sigstore", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), DigestAlgorithm: "sha256", Limit: 30, OIDCIssuer: GitHubOIDCIssuer, @@ -130,7 +130,7 @@ func TestNewVerifyCmd(t *testing.T) { name: "Uses custom limit flag", cli: "../test/data/sigstore-js-2.1.0.tgz --owner sigstore --limit 101", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), DigestAlgorithm: "sha256", OIDCIssuer: GitHubOIDCIssuer, Owner: "sigstore", @@ -143,7 +143,7 @@ func TestNewVerifyCmd(t *testing.T) { name: "Uses invalid limit flag", cli: "../test/data/sigstore-js-2.1.0.tgz --owner sigstore --limit 0", wants: Options{ - ArtifactPath: "../test/data/sigstore-js-2.1.0.tgz", + ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), DigestAlgorithm: "sha256", OIDCIssuer: GitHubOIDCIssuer, Owner: "sigstore", @@ -202,6 +202,9 @@ func TestNewVerifyCmd(t *testing.T) { assert.Equal(t, tc.wants.Repo, opts.Repo) assert.Equal(t, tc.wants.SAN, opts.SAN) assert.Equal(t, tc.wants.SANRegex, opts.SANRegex) + assert.NotNil(t, opts.APIClient) + assert.NotNil(t, opts.Logger) + assert.NotNil(t, opts.OCIClient) }) } } @@ -330,13 +333,6 @@ func TestRunVerify(t *testing.T) { require.Error(t, runVerify(&opts)) }) - t.Run("with missing OCI client", func(t *testing.T) { - customOpts := publicGoodOpts - customOpts.ArtifactPath = "oci://ghcr.io/github/test" - customOpts.OCIClient = nil - require.Error(t, runVerify(&customOpts)) - }) - t.Run("with missing API client", func(t *testing.T) { customOpts := publicGoodOpts customOpts.APIClient = nil