Refactor the initialization of KeyManagers.

This commit is contained in:
Mariano Cano 2020-05-11 18:47:22 -07:00
parent c02fe77998
commit 63e36ecd7a
7 changed files with 81 additions and 62 deletions

View file

@ -2,30 +2,18 @@ package kms
import (
"context"
"crypto"
"crypto/x509"
"strings"
"github.com/pkg/errors"
"github.com/smallstep/certificates/kms/apiv1"
"github.com/smallstep/certificates/kms/cloudkms"
"github.com/smallstep/certificates/kms/softkms"
"github.com/smallstep/certificates/kms/yubikey"
)
// KeyManager is the interface implemented by all the KMS.
type KeyManager interface {
GetPublicKey(req *apiv1.GetPublicKeyRequest) (crypto.PublicKey, error)
CreateKey(req *apiv1.CreateKeyRequest) (*apiv1.CreateKeyResponse, error)
CreateSigner(req *apiv1.CreateSignerRequest) (crypto.Signer, error)
Close() error
}
type KeyManager = apiv1.KeyManager
// CertificateManager is the interface implemented by the KMS that can load and store x509.Certificates.
type CertificateManager interface {
LoadCerticate(req *apiv1.LoadCertificateRequest) (*x509.Certificate, error)
StoreCertificate(req *apiv1.StoreCertificateRequest) error
}
// CertificateManager is the interface implemented by the KMS that can load and
// store x509.Certificates.
type CertificateManager = apiv1.CertificateManager
// New initializes a new KMS from the given type.
func New(ctx context.Context, opts apiv1.Options) (KeyManager, error) {
@ -33,14 +21,14 @@ func New(ctx context.Context, opts apiv1.Options) (KeyManager, error) {
return nil, err
}
switch apiv1.Type(strings.ToLower(opts.Type)) {
case apiv1.DefaultKMS, apiv1.SoftKMS:
return softkms.New(ctx, opts)
case apiv1.CloudKMS:
return cloudkms.New(ctx, opts)
case apiv1.YubiKey:
return yubikey.New(ctx, opts)
default:
t := apiv1.Type(strings.ToLower(opts.Type))
if t == apiv1.DefaultKMS {
t = apiv1.SoftKMS
}
fn, ok := apiv1.LoadKeyManagerNewFunc(t)
if !ok {
return nil, errors.Errorf("unsupported kms type '%s'", opts.Type)
}
return fn(ctx, opts)
}