Skip to content

Commit e6b08fe

Browse files
committed
Use KMS URIs without --kms flag
This command allows use use KMS URIs without using the `--kms` flag. Those commands using cryptoutils package will read a key from a KMS if the name is not a file and is one of the supported KMS types.
1 parent 8aacf25 commit e6b08fe

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

command/ca/rekey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func rekeyCertificateAction(ctx *cli.Context) error {
239239
// For now, if the --kms flag is given, do not allow to generate a new key
240240
// and write it on disk. We can't use the daemon mode because we
241241
// cannot generate new keys.
242-
if kmsURI != "" {
242+
if kmsURI != "" || cryptoutil.IsKMS(keyFile) {
243243
switch {
244244
case givenPrivate == "":
245245
return errs.RequiredWithFlag(ctx, "kms", "private-key")

internal/cryptoutil/cryptoutil.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14+
"os"
1415
"os/exec"
1516
"strconv"
1617
"strings"
@@ -22,15 +23,24 @@ import (
2223
"go.step.sm/crypto/pemutil"
2324
)
2425

25-
// IsKMS returns true if the given uri is a KMS URI.
26+
// IsKMS returns true if the given uri is a KMS URI. It will return false if a
27+
// file exists with the same name, even if the path matches a KMS uri pattern.
2628
func IsKMS(rawuri string) bool {
29+
if _, err := os.Stat(rawuri); err == nil {
30+
return false
31+
}
32+
2733
typ, err := kms.TypeOf(rawuri)
2834
if err != nil || typ == apiv1.DefaultKMS {
2935
return false
3036
}
3137
return true
3238
}
3339

40+
func isFilename(kmsURI, name string) bool {
41+
return kmsURI == "" && !IsKMS(name)
42+
}
43+
3444
// Attestor is the interface implemented by step-kms-plugin using the key, sign,
3545
// and attest commands.
3646
type Attestor interface {
@@ -39,7 +49,7 @@ type Attestor interface {
3949
}
4050

4151
func PublicKey(kmsURI, name string, opts ...pemutil.Options) (crypto.PublicKey, error) {
42-
if kmsURI == "" {
52+
if isFilename(kmsURI, name) {
4353
s, err := pemutil.Read(name, opts...)
4454
if err != nil {
4555
return nil, err
@@ -61,7 +71,7 @@ func PublicKey(kmsURI, name string, opts ...pemutil.Options) (crypto.PublicKey,
6171
// CreateSigner reads a key from a file with a given name or creates a signer
6272
// with the given kms and name uri.
6373
func CreateSigner(kmsURI, name string, opts ...pemutil.Options) (crypto.Signer, error) {
64-
if kmsURI == "" || isSoftKMS(kmsURI) {
74+
if isFilename(kmsURI, name) {
6575
s, err := pemutil.Read(name, opts...)
6676
if err != nil {
6777
return nil, err
@@ -75,13 +85,9 @@ func CreateSigner(kmsURI, name string, opts ...pemutil.Options) (crypto.Signer,
7585
return newKMSSigner(kmsURI, name)
7686
}
7787

78-
func isSoftKMS(kmsURI string) bool {
79-
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(kmsURI)), "softkms")
80-
}
81-
8288
// LoadCertificate returns a x509.Certificate from a kms or file
8389
func LoadCertificate(kmsURI, certPath string) ([]*x509.Certificate, error) {
84-
if kmsURI == "" {
90+
if isFilename(kmsURI, certPath) {
8591
s, err := pemutil.ReadCertificateBundle(certPath)
8692
if err != nil {
8793
return nil, fmt.Errorf("file %s does not contain a valid certificate: %w", certPath, err)
@@ -117,7 +123,7 @@ func LoadCertificate(kmsURI, certPath string) ([]*x509.Certificate, error) {
117123

118124
// LoadJSONWebKey returns a jose.JSONWebKey from a KMS or a file.
119125
func LoadJSONWebKey(kmsURI, name string, opts ...jose.Option) (*jose.JSONWebKey, error) {
120-
if kmsURI == "" {
126+
if isFilename(kmsURI, name) {
121127
return jose.ReadKey(name, opts...)
122128
}
123129

0 commit comments

Comments
 (0)