Skip to content

Commit

Permalink
feat: adds exit codes for verify errors
Browse files Browse the repository at this point in the history
- adds the exit code to when cosign throws an error due to a user trying to verify an image tag that doesn't exist.
- adds functionality with associated exit code for when there are no signatures found for an image

Signed-off-by: ChrisJBurns <29541485+ChrisJBurns@users.noreply.github.com>
  • Loading branch information
ChrisJBurns committed Mar 12, 2023
1 parent f61cdf0 commit 1977cc8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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{
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

0 comments on commit 1977cc8

Please sign in to comment.