@@ -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.
2628func 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.
3646type Attestor interface {
@@ -39,7 +49,7 @@ type Attestor interface {
3949}
4050
4151func 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.
6373func 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
8389func 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.
119125func 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