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

feat: adds NonExistentTag Exit Code to Error #2766

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions cmd/cosign/errors/exit_code_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
// exitCodeLookup contains a map of errorTypes and their associated exitCodes.
var exitCodeLookup = map[string]int{
verificationError.ErrNoMatchingSignaturesType: NoMatchingSignature,
verificationError.ErrImageTagNotFoundType: NonExistentTag,
verificationError.ErrNoSignaturesFoundType: ImageWithoutSignature,
}

func LookupExitCodeForErrorType(errorType string) int {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cosign/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ var (
// NoMatchingSignatures
ErrNoMatchingSignaturesType = "NoMatchingSignatures"
ErrNoMatchingSignaturesMessage = "no matching signatures"

// NonExistingTagType
ErrImageTagNotFoundType = "ImageTagNotFound"
ErrImageTagNotFoundMessage = "image tag not found"

// NoSignaturesFound
ErrNoSignaturesFoundType = "NoSignaturesFound"
ErrNoSignaturesFoundMessage = "no signatures found for image"
)

// VerificationError is the type of Go error that is used by cosign to surface
Expand Down
13 changes: 13 additions & 0 deletions pkg/cosign/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,12 @@ func VerifyImageSignatures(ctx context.Context, signedImgRef name.Reference, co
// entity that minimizes registry requests when supplied with a digest input
digest, err := ociremote.ResolveDigest(signedImgRef, co.RegistryClientOpts...)
if err != nil {
if strings.Contains(err.Error(), "MANIFEST_UNKNOWN") {
return nil, false, &VerificationError{
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 think best practice for wrapping errors is to store the inner error inside the struct and format the message inside the Error function.

See: https://go.dev/blog/go1.13-errors

errorType: ErrImageTagNotFoundType,
message: fmt.Sprintf("%s: %v", ErrImageTagNotFoundMessage, err),
}
}
return nil, false, err
}
h, err := v1.NewHash(digest.Identifier())
Expand Down Expand Up @@ -567,6 +573,13 @@ func verifySignatures(ctx context.Context, sigs oci.Signatures, h v1.Hash, co *C
return nil, false, err
}

if len(sl) == 0 {
return nil, false, &VerificationError{
errorType: ErrNoSignaturesFoundType,
message: ErrNoSignaturesFoundMessage,
}
}

validationErrs := []string{}

for _, sig := range sl {
Expand Down