Skip to content

Commit 3ace17f

Browse files
fix vault key path/key (#3558)
1 parent 84e7e9a commit 3ace17f

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

crypto/storage/vault/vault.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
const privateKeyPathName = "nuts-private-keys"
3636
const defaultPathPrefix = "kv"
37+
const vaultSecretkeyName = "key"
3738

3839
// StorageType is the name of this storage type, used in health check reports and configuration.
3940
const StorageType = "vaultkv"
@@ -102,8 +103,8 @@ func NewVaultKVStorage(config Config) (spi.Storage, error) {
102103
return vaultStorage, nil
103104
}
104105

105-
func (v vaultKVStorage) NewPrivateKey(ctx context.Context, keyName string) (crypto.PublicKey, string, error) {
106-
return spi.GenerateAndStore(ctx, v, keyName)
106+
func (v vaultKVStorage) NewPrivateKey(ctx context.Context, keyPath string) (crypto.PublicKey, string, error) {
107+
return spi.GenerateAndStore(ctx, v, keyPath)
107108
}
108109

109110
func configureVaultClient(cfg Config) (*vault.Client, error) {
@@ -142,7 +143,7 @@ func (v vaultKVStorage) checkConnection() error {
142143

143144
func (v vaultKVStorage) GetPrivateKey(ctx context.Context, keyName string, _ string) (crypto.Signer, error) {
144145
path := privateKeyPath(v.config.PathPrefix, keyName)
145-
value, err := v.getValue(ctx, path, keyName)
146+
value, err := v.getValue(ctx, path, vaultSecretkeyName)
146147
if err != nil {
147148
return nil, err
148149
}
@@ -181,7 +182,7 @@ func (v vaultKVStorage) storeValue(ctx context.Context, path, key string, value
181182

182183
func (v vaultKVStorage) PrivateKeyExists(ctx context.Context, keyName string, _ string) (bool, error) {
183184
path := privateKeyPath(v.config.PathPrefix, keyName)
184-
_, err := v.getValue(ctx, path, keyName)
185+
_, err := v.getValue(ctx, path, vaultSecretkeyName)
185186
if errors.Is(err, spi.ErrNotFound) {
186187
return false, nil
187188
}
@@ -224,14 +225,14 @@ func privateKeyListPath(prefix string) string {
224225
return filepath.Clean(path)
225226
}
226227

227-
func (v vaultKVStorage) SavePrivateKey(ctx context.Context, keyName string, key crypto.PrivateKey) error {
228-
path := privateKeyPath(v.config.PathPrefix, keyName)
228+
func (v vaultKVStorage) SavePrivateKey(ctx context.Context, keyPath string, key crypto.PrivateKey) error {
229+
path := privateKeyPath(v.config.PathPrefix, keyPath)
229230
pem, err := util.PrivateKeyToPem(key)
230231
if err != nil {
231232
return fmt.Errorf("unable to convert private key to pem format: %w", err)
232233
}
233234

234-
return v.storeValue(ctx, path, keyName, pem)
235+
return v.storeValue(ctx, path, vaultSecretkeyName, pem)
235236
}
236237

237238
func (v vaultKVStorage) DeletePrivateKey(ctx context.Context, kid string) error {

crypto/storage/vault/vault_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"errors"
2727
vault "github.com/hashicorp/vault/api"
2828
"github.com/nuts-foundation/nuts-node/core"
29+
"github.com/nuts-foundation/nuts-node/crypto/util"
2930
"github.com/stretchr/testify/assert"
3031
"github.com/stretchr/testify/require"
3132
"net/http"
@@ -103,6 +104,17 @@ func TestVaultKVStorage(t *testing.T) {
103104
assert.Equal(t, privateKey, result, "expected retrieved key to equal original")
104105
})
105106

107+
t.Run("get", func(t *testing.T) {
108+
pem, _ := util.PrivateKeyToPem(privateKey)
109+
vaultStorage := vaultKVStorage{config: DefaultConfig(), client: mockVaultClient{store: map[string]map[string]interface{}{"kv/nuts-private-keys/did:nuts:123#abc": {vaultSecretkeyName: pem}}}}
110+
111+
signer, err := vaultStorage.GetPrivateKey(ctx, keyName, version)
112+
113+
require.NoError(t, err)
114+
pem2, _ := util.PrivateKeyToPem(signer)
115+
assert.Equal(t, pem, pem2)
116+
})
117+
106118
t.Run("delete", func(t *testing.T) {
107119
t.Run("ok", func(t *testing.T) {
108120
vaultStorage := vaultKVStorage{client: mockVaultClient{store: map[string]map[string]interface{}{"kv/nuts-private-keys/did:nuts:123#abc": {}}}}
@@ -171,7 +183,7 @@ func TestVaultKVStorage(t *testing.T) {
171183
})
172184

173185
t.Run("error - encoding issues", func(t *testing.T) {
174-
vaultStorage := vaultKVStorage{config: DefaultConfig(), client: mockVaultClient{store: map[string]map[string]interface{}{"kv/nuts-private-keys/did:nuts:123#abc": {keyName: []byte("foo")}}}}
186+
vaultStorage := vaultKVStorage{config: DefaultConfig(), client: mockVaultClient{store: map[string]map[string]interface{}{"kv/nuts-private-keys/did:nuts:123#abc": {vaultSecretkeyName: []byte("foo")}}}}
175187

176188
t.Run("SavePrivateKey", func(t *testing.T) {
177189
err := vaultStorage.SavePrivateKey(ctx, keyName, "123")

docs/pages/release_notes.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
Release notes
33
#############
44

5+
***************
6+
Peanut (v6.0.2)
7+
***************
8+
9+
Release date: 2024-11-14
10+
11+
- `#3556 <https://github.com/nuts-foundation/nuts-node/issues/3556>`_: fix private key path when using native Hashicorp Vault integration,
12+
broken since v6.0.0 (pre-v6.0.0 keys couldn't be found, post-v6.0.0 keys have an incorrect name).
13+
- Update `github.com/golang-jwt/jwt/v4` to v4.5.1 to address vulnerability `GO-2024-3250 <https://pkg.go.dev/vuln/GO-2024-3250>`_.
14+
15+
**Full Changelog**: https://github.com/nuts-foundation/nuts-node/compare/v6.0.1...v6.0.2
16+
517
***************
618
Peanut (v6.0.1)
719
***************

0 commit comments

Comments
 (0)