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

Clarify error when KMS provider fails to load #2220

Merged
merged 1 commit into from
Sep 4, 2022
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
10 changes: 9 additions & 1 deletion pkg/blob/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ import (
"strings"
)

type UnrecognizedSchemeError struct {
Scheme string
}

func (e *UnrecognizedSchemeError) Error() string {
return fmt.Sprintf("loading URL: unrecognized scheme: %s", e.Scheme)
}

func LoadFileOrURL(fileRef string) ([]byte, error) {
var raw []byte
var err error
Expand Down Expand Up @@ -51,7 +59,7 @@ func LoadFileOrURL(fileRef string) ([]byte, error) {
}
raw = []byte(value)
default:
return nil, fmt.Errorf("loading URL: unrecognized scheme: %s", scheme)
return nil, &UnrecognizedSchemeError{Scheme: scheme}
}
} else {
raw, err = os.ReadFile(filepath.Clean(fileRef))
Expand Down
12 changes: 11 additions & 1 deletion pkg/signature/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@ func LoadPublicKey(ctx context.Context, keyRef string) (verifier signature.Verif
// verifier using the provided hash algorithm
func VerifierForKeyRef(ctx context.Context, keyRef string, hashAlgorithm crypto.Hash) (verifier signature.Verifier, err error) {
// The key could be plaintext, in a file, at a URL, or in KMS.
if kmsKey, err := kms.Get(ctx, keyRef, hashAlgorithm); err == nil {
var perr *kms.ProviderNotFoundError
kmsKey, err := kms.Get(ctx, keyRef, hashAlgorithm)
switch {
case err == nil:
// KMS specified
return kmsKey, nil
case errors.As(err, &perr):
// We can ignore ProviderNotFoundError; that just means the keyRef
// didn't match any of the KMS schemes.
default:
// But other errors indicate something more insidious; pass those
// through.
return nil, err
}

raw, err := blob.LoadFileOrURL(keyRef)
Expand Down
27 changes: 27 additions & 0 deletions pkg/signature/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ package signature

import (
"context"
"crypto"
"errors"
"os"
"testing"

"github.com/sigstore/cosign/pkg/blob"
"github.com/sigstore/cosign/pkg/cosign"
sigsignature "github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/kms"
)

func generateKeyFile(t *testing.T, tmpDir string, pf cosign.PassFunc) (privFile, pubFile string) {
Expand Down Expand Up @@ -134,6 +139,28 @@ func TestSignerVerifierFromEnvVar(t *testing.T) {
}
}

func TestVerifierForKeyRefError(t *testing.T) {
kms.AddProvider("errorkms://", func(ctx context.Context, _ string, hf crypto.Hash, _ ...sigsignature.RPCOption) (kms.SignerVerifier, error) {
return nil, errors.New("bad")
})
var uerr *blob.UnrecognizedSchemeError

ctx := context.Background()
_, err := PublicKeyFromKeyRef(ctx, "errorkms://bad")
if err == nil {
t.Fatalf("PublicKeyFromKeyRef didn't return any error")
} else if errors.As(err, &uerr) {
t.Fatalf("PublicKeyFromKeyRef returned UnrecognizedSchemeError: %v", err)
}

_, err = PublicKeyFromKeyRef(ctx, "badscheme://bad")
if err == nil {
t.Fatalf("PublicKeyFromKeyRef didn't return any error")
} else if !errors.As(err, &uerr) {
t.Fatalf("PublicKeyFromKeyRef didn't return UnrecognizedSchemeError: %v", err)
}
}

func pass(s string) cosign.PassFunc {
return func(_ bool) ([]byte, error) {
return []byte(s), nil
Expand Down