clean up nil client checks

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-18 08:57:54 -06:00
parent 274af8b436
commit 5cc2f6af1a
6 changed files with 28 additions and 38 deletions

View file

@ -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 {

View file

@ -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)

View file

@ -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

View file

@ -62,3 +62,9 @@ func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp [
return metadataFilePath, nil
}
func NewLiveStore(outputPath string) *LiveStore {
return &LiveStore{
outputPath: outputPath,
}
}

View file

@ -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))
})
}

View file

@ -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