Skip to content

Commit 03db1fb

Browse files
authored
Merge pull request #1604 from smallstep/mariano/fix-1601
Fix isFilename to detect files by existence check
2 parents 23b67fc + dd664ad commit 03db1fb

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

internal/cryptoutil/cryptoutil.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ func IsKMS(rawuri string) bool {
3737
return true
3838
}
3939

40-
func isFilename(kmsURI, name string) bool {
41-
return kmsURI == "" && !IsKMS(name)
40+
func isFilename(name string) bool {
41+
_, err := os.Stat(name)
42+
return err == nil
4243
}
4344

4445
// Attestor is the interface implemented by step-kms-plugin using the key, sign,
@@ -49,7 +50,7 @@ type Attestor interface {
4950
}
5051

5152
func PublicKey(kmsURI, name string, opts ...pemutil.Options) (crypto.PublicKey, error) {
52-
if isFilename(kmsURI, name) {
53+
if isFilename(name) {
5354
s, err := pemutil.Read(name, opts...)
5455
if err != nil {
5556
return nil, err
@@ -71,7 +72,7 @@ func PublicKey(kmsURI, name string, opts ...pemutil.Options) (crypto.PublicKey,
7172
// CreateSigner reads a key from a file with a given name or creates a signer
7273
// with the given kms and name uri.
7374
func CreateSigner(kmsURI, name string, opts ...pemutil.Options) (crypto.Signer, error) {
74-
if isFilename(kmsURI, name) {
75+
if isFilename(name) {
7576
s, err := pemutil.Read(name, opts...)
7677
if err != nil {
7778
return nil, err
@@ -87,7 +88,7 @@ func CreateSigner(kmsURI, name string, opts ...pemutil.Options) (crypto.Signer,
8788

8889
// LoadCertificate returns a x509.Certificate from a kms or file
8990
func LoadCertificate(kmsURI, certPath string) ([]*x509.Certificate, error) {
90-
if isFilename(kmsURI, certPath) {
91+
if isFilename(certPath) {
9192
s, err := pemutil.ReadCertificateBundle(certPath)
9293
if err != nil {
9394
return nil, fmt.Errorf("file %s does not contain a valid certificate: %w", certPath, err)
@@ -123,7 +124,7 @@ func LoadCertificate(kmsURI, certPath string) ([]*x509.Certificate, error) {
123124

124125
// LoadJSONWebKey returns a jose.JSONWebKey from a KMS or a file.
125126
func LoadJSONWebKey(kmsURI, name string, opts ...jose.Option) (*jose.JSONWebKey, error) {
126-
if isFilename(kmsURI, name) {
127+
if isFilename(name) {
127128
return jose.ReadKey(name, opts...)
128129
}
129130

0 commit comments

Comments
 (0)