Skip to content

Commit

Permalink
clean up nil client checks
Browse files Browse the repository at this point in the history
Signed-off-by: Meredith Lancaster <[email protected]>
  • Loading branch information
malancas committed Mar 18, 2024
1 parent 274af8b commit 5cc2f6a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
3 changes: 0 additions & 3 deletions pkg/cmd/attestation/artifact/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/attestation/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions pkg/cmd/attestation/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/cmd/attestation/download/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp [

return metadataFilePath, nil
}

func NewLiveStore(outputPath string) *LiveStore {
return &LiveStore{
outputPath: outputPath,
}
}
9 changes: 2 additions & 7 deletions pkg/cmd/attestation/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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))
})
}
32 changes: 14 additions & 18 deletions pkg/cmd/attestation/verify/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5cc2f6a

Please sign in to comment.