From 4ac0c61cf28159e2a0b2ac3e37ffd420560bfdd9 Mon Sep 17 00:00:00 2001 From: Vishal Choudhary Date: Sat, 9 Sep 2023 23:13:48 +0530 Subject: [PATCH 1/4] fix: add func to generate `entity not found` error Signed-off-by: Vishal Choudhary --- cmd/cosign/cli/sign/sign.go | 2 +- pkg/oci/remote/remote.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/cosign/cli/sign/sign.go b/cmd/cosign/cli/sign/sign.go index ba286981d24..847c42041e0 100644 --- a/cmd/cosign/cli/sign/sign.go +++ b/cmd/cosign/cli/sign/sign.go @@ -179,7 +179,7 @@ func SignCmd(ro *options.RootOptions, ko options.KeyOpts, signOpts options.SignO if digest, ok := ref.(name.Digest); ok && !signOpts.Recursive { se, err := ociremote.SignedEntity(ref, opts...) - if err == ociremote.ErrEntityNotFound { + if ociremote.IsEntityNotFoundError(err) { se = ociremote.SignedUnknown(digest) } else if err != nil { return fmt.Errorf("accessing image: %w", err) diff --git a/pkg/oci/remote/remote.go b/pkg/oci/remote/remote.go index fab0a76588d..192ef345c6a 100644 --- a/pkg/oci/remote/remote.go +++ b/pkg/oci/remote/remote.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "net/http" + "strings" "github.com/google/go-containerregistry/pkg/name" v1 "github.com/google/go-containerregistry/pkg/v1" @@ -39,9 +40,17 @@ var ( // ErrEntityNotFound is the error that SignedEntity returns when the // provided ref does not exist. - ErrEntityNotFound = errors.New("entity not found in registry") + ErrEntityNotFound = "entity not found in registry" ) +func NewEntityNotFoundError(err error) error { + return fmt.Errorf("%s error: %v", ErrEntityNotFound, err) +} + +func IsEntityNotFoundError(err error) bool { + return strings.Contains(err.Error(), ErrEntityNotFound) +} + // SignedEntity provides access to a remote reference, and its signatures. // The SignedEntity will be one of SignedImage or SignedImageIndex. func SignedEntity(ref name.Reference, options ...Option) (oci.SignedEntity, error) { @@ -50,7 +59,7 @@ func SignedEntity(ref name.Reference, options ...Option) (oci.SignedEntity, erro got, err := remoteGet(ref, o.ROpt...) var te *transport.Error if errors.As(err, &te) && te.StatusCode == http.StatusNotFound { - return nil, ErrEntityNotFound + return nil, NewEntityNotFoundError(err) } else if err != nil { return nil, err } From 8fd56c119684de86b9abf2e82de6c80d650d607b Mon Sep 17 00:00:00 2001 From: Vishal Choudhary Date: Sun, 10 Sep 2023 13:14:07 +0530 Subject: [PATCH 2/4] fix: update format specifier in fmt.Errorf Signed-off-by: Vishal Choudhary --- pkg/oci/remote/remote.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/oci/remote/remote.go b/pkg/oci/remote/remote.go index 192ef345c6a..623044df5d3 100644 --- a/pkg/oci/remote/remote.go +++ b/pkg/oci/remote/remote.go @@ -44,7 +44,7 @@ var ( ) func NewEntityNotFoundError(err error) error { - return fmt.Errorf("%s error: %v", ErrEntityNotFound, err) + return fmt.Errorf("%s error: %w", ErrEntityNotFound, err) } func IsEntityNotFoundError(err error) bool { From 3a9098ed13b771c124899e97ed91ed6d04c8671e Mon Sep 17 00:00:00 2001 From: Vishal Choudhary Date: Sun, 10 Sep 2023 21:26:14 +0530 Subject: [PATCH 3/4] feat: make ErrEntityNotFound error message less common Signed-off-by: Vishal Choudhary --- pkg/oci/remote/remote.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/oci/remote/remote.go b/pkg/oci/remote/remote.go index 623044df5d3..02f0756f980 100644 --- a/pkg/oci/remote/remote.go +++ b/pkg/oci/remote/remote.go @@ -40,7 +40,7 @@ var ( // ErrEntityNotFound is the error that SignedEntity returns when the // provided ref does not exist. - ErrEntityNotFound = "entity not found in registry" + ErrEntityNotFound = "cosign remoteGet: entity not found in registry" ) func NewEntityNotFoundError(err error) error { From 207124cf4750e6fd389ffc035e371c64b2360e78 Mon Sep 17 00:00:00 2001 From: Vishal Choudhary Date: Sun, 10 Sep 2023 22:12:06 +0530 Subject: [PATCH 4/4] feat: make EntityNotFoundError a first-class error Signed-off-by: Vishal Choudhary --- cmd/cosign/cli/sign/sign.go | 2 +- pkg/oci/remote/remote.go | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/cosign/cli/sign/sign.go b/cmd/cosign/cli/sign/sign.go index 847c42041e0..53c0fd3ccff 100644 --- a/cmd/cosign/cli/sign/sign.go +++ b/cmd/cosign/cli/sign/sign.go @@ -179,7 +179,7 @@ func SignCmd(ro *options.RootOptions, ko options.KeyOpts, signOpts options.SignO if digest, ok := ref.(name.Digest); ok && !signOpts.Recursive { se, err := ociremote.SignedEntity(ref, opts...) - if ociremote.IsEntityNotFoundError(err) { + if _, isEntityNotFoundErr := err.(*ociremote.EntityNotFoundError); isEntityNotFoundErr { se = ociremote.SignedUnknown(digest) } else if err != nil { return fmt.Errorf("accessing image: %w", err) diff --git a/pkg/oci/remote/remote.go b/pkg/oci/remote/remote.go index 02f0756f980..d279d38d26b 100644 --- a/pkg/oci/remote/remote.go +++ b/pkg/oci/remote/remote.go @@ -20,7 +20,6 @@ import ( "fmt" "io" "net/http" - "strings" "github.com/google/go-containerregistry/pkg/name" v1 "github.com/google/go-containerregistry/pkg/v1" @@ -37,18 +36,22 @@ var ( remoteIndex = remote.Index remoteGet = remote.Get remoteWrite = remote.Write - - // ErrEntityNotFound is the error that SignedEntity returns when the - // provided ref does not exist. - ErrEntityNotFound = "cosign remoteGet: entity not found in registry" ) -func NewEntityNotFoundError(err error) error { - return fmt.Errorf("%s error: %w", ErrEntityNotFound, err) +// EntityNotFoundError is the error that SignedEntity returns when the +// provided ref does not exist. +type EntityNotFoundError struct { + baseErr error +} + +func (e *EntityNotFoundError) Error() string { + return fmt.Sprintf("entity not found in registry, error: %v", e.baseErr) } -func IsEntityNotFoundError(err error) bool { - return strings.Contains(err.Error(), ErrEntityNotFound) +func NewEntityNotFoundError(err error) error { + return &EntityNotFoundError{ + baseErr: err, + } } // SignedEntity provides access to a remote reference, and its signatures.