Skip to content

Commit 7012e70

Browse files
authored
Merge pull request #732 from smallstep/mariano/softkms-fullpath
Fix open files with a full path in softkms
2 parents 5d5ec69 + 29c8162 commit 7012e70

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

kms/softkms/softkms.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ func filename(s string) string {
195195
if f := u.Get("path"); f != "" {
196196
return f
197197
}
198-
return u.Opaque
198+
switch {
199+
case u.Path != "":
200+
return u.Path
201+
default:
202+
return u.Opaque
203+
}
199204
}
200205
return s
201206
}

kms/softkms/softkms_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import (
1212
"encoding/pem"
1313
"fmt"
1414
"os"
15+
"path/filepath"
1516
"reflect"
1617
"testing"
1718

19+
"github.com/stretchr/testify/assert"
20+
1821
"go.step.sm/crypto/kms/apiv1"
1922
"go.step.sm/crypto/pemutil"
2023
"go.step.sm/crypto/x25519"
@@ -247,6 +250,12 @@ func TestSoftKMS_GetPublicKey(t *testing.T) {
247250
if err != nil {
248251
t.Fatal(err)
249252
}
253+
254+
fullPath := filepath.Join(t.TempDir(), "pub.pem")
255+
if err := os.WriteFile(fullPath, b, 0o0600); err != nil {
256+
t.Fatal(err)
257+
}
258+
250259
block, _ := pem.Decode(b)
251260
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
252261
if err != nil {
@@ -270,8 +279,11 @@ func TestSoftKMS_GetPublicKey(t *testing.T) {
270279
wantErr bool
271280
}{
272281
{"key", args{&apiv1.GetPublicKeyRequest{Name: "testdata/pub.pem"}}, pub, false},
282+
{"key full path", args{&apiv1.GetPublicKeyRequest{Name: fullPath}}, pub, false},
273283
{"key uri", args{&apiv1.GetPublicKeyRequest{Name: "softkms:testdata/pub.pem"}}, pub, false},
274284
{"key path uri", args{&apiv1.GetPublicKeyRequest{Name: "softkms:path=testdata/pub.pem"}}, pub, false},
285+
{"key full path uri", args{&apiv1.GetPublicKeyRequest{Name: "softkms:" + fullPath}}, pub, false},
286+
{"key full path value", args{&apiv1.GetPublicKeyRequest{Name: "softkms:path=" + fullPath}}, pub, false},
275287
{"cert", args{&apiv1.GetPublicKeyRequest{Name: "testdata/cert.crt"}}, pub, false},
276288
{"cert uri", args{&apiv1.GetPublicKeyRequest{Name: "softkms:testdata/cert.crt"}}, pub, false},
277289
{"cert path uri", args{&apiv1.GetPublicKeyRequest{Name: "softkms:path=testdata/cert.crt"}}, pub, false},
@@ -405,3 +417,25 @@ func TestSoftKMS_CreateDecrypter(t *testing.T) {
405417
})
406418
}
407419
}
420+
421+
func Test_filename(t *testing.T) {
422+
type args struct {
423+
s string
424+
}
425+
tests := []struct {
426+
name string
427+
args args
428+
want string
429+
}{
430+
{"ok name", args{"testdata/pub.pem"}, "testdata/pub.pem"},
431+
{"ok uri value", args{"softkms:path=testdata/pub.pem"}, "testdata/pub.pem"},
432+
{"ok uri value full", args{"softkms:path=/testdata/pub.pem"}, "/testdata/pub.pem"},
433+
{"ok uri opaque", args{"softkms:testdata/pub.pem"}, "testdata/pub.pem"},
434+
{"ok uri path", args{"softkms:/testdata/pub.pem"}, "/testdata/pub.pem"},
435+
}
436+
for _, tt := range tests {
437+
t.Run(tt.name, func(t *testing.T) {
438+
assert.Equal(t, tt.want, filename(tt.args.s))
439+
})
440+
}
441+
}

0 commit comments

Comments
 (0)