@@ -16,10 +16,11 @@ import (
16
16
17
17
// VaultSecrets implements a secret.Store backed by Hashicorp Vault
18
18
type VaultSecrets struct {
19
- client * api.Client
20
- path string
21
- version int
22
- renewal time.Duration
19
+ client * api.Client
20
+ enginepath string
21
+ path string
22
+ version int
23
+ renewal time.Duration
23
24
}
24
25
25
26
var _ secret.Store = & VaultSecrets {}
@@ -31,7 +32,6 @@ func New(addr, basepath, token string, renewal time.Duration) (v *VaultSecrets,
31
32
}
32
33
33
34
v = & VaultSecrets {
34
- path : basepath ,
35
35
renewal : renewal ,
36
36
}
37
37
@@ -47,19 +47,17 @@ func New(addr, basepath, token string, renewal time.Duration) (v *VaultSecrets,
47
47
return nil , errors .Wrap (err , "failed to connect to vault server" )
48
48
}
49
49
50
- enginepath := strings .Split (basepath , "/" )[0 ]
51
- if len (enginepath ) == 0 {
52
- enginepath = basepath
53
- }
50
+ // engine is the first component of base, then the rest is the actual path.
51
+ v .enginepath , v .path = splitPath (basepath )
54
52
55
- if v .version , err = getKVEngineVersion (v .client , enginepath ); err != nil {
56
- return nil , errors .Wrapf (err , "failed to determine KV engine version at '/%s'" , enginepath )
53
+ if v .version , err = getKVEngineVersion (v .client , v . enginepath ); err != nil {
54
+ return nil , errors .Wrapf (err , "failed to determine KV engine version at '/%s'" , v . enginepath )
57
55
}
58
56
59
57
zap .L ().Debug ("created new vault client for secrets engine" ,
60
58
zap .Int ("kv_version" , v .version ),
61
59
zap .String ("basepath" , basepath ),
62
- zap .String ("enginepath" , enginepath ))
60
+ zap .String ("enginepath" , v . enginepath ))
63
61
64
62
return v , nil
65
63
}
@@ -112,30 +110,41 @@ func (v *VaultSecrets) Renew(ctx context.Context) error {
112
110
return nil
113
111
}
114
112
113
+ func splitPath (basepath string ) (string , string ) {
114
+ basepath = strings .Trim (basepath , "/" )
115
+ s := strings .SplitN (basepath , "/" , 2 )
116
+ if len (s [0 ]) == 0 {
117
+ return basepath , "/"
118
+ } else if len (s ) == 1 {
119
+ return basepath , "/"
120
+ }
121
+ return s [0 ], s [1 ]
122
+ }
123
+
115
124
// builds the correct path to a secret based on the kv version
116
125
func (v * VaultSecrets ) buildPath (item string ) string {
117
126
if v .version == 1 {
118
- return path .Join (v .path , item )
119
- } else {
120
- return path .Join (v .path , "data" , item )
127
+ path .Split (v .path )
128
+ return path .Join (v .enginepath , v .path , item )
121
129
}
130
+ return path .Join (v .enginepath , "data" , v .path , item )
122
131
}
123
132
124
133
// pulls out the kv secret data for v1 and v2 secrets
125
134
func kvToMap (version int , data map [string ]interface {}) (env map [string ]string , err error ) {
126
135
if version == 1 {
127
136
env = make (map [string ]string )
128
137
for k , v := range data {
129
- env [k ] = v .(string )
138
+ env [k ] = v .(string ) //nolint:err - we know it's a string already
130
139
}
131
140
} else if version == 2 {
132
141
env = make (map [string ]string )
133
142
if kv , ok := data ["data" ].(map [string ]interface {}); ok {
134
143
for k , v := range kv {
135
- env [k ] = v .(string )
144
+ env [k ] = v .(string ) //nolint:err - we know it's a string already
136
145
}
137
146
} else {
138
- return nil , errors .New ("could not interpret KV v2 response data as hashtable, this is likely a change in the KV v2 API, please open an issue. " )
147
+ return nil , errors .New ("could not interpret KV v2 response data as hashtable, this is likely a change in the KV v2 API, please open an issue" )
139
148
}
140
149
} else {
141
150
return nil , errors .Errorf ("unrecognised KV version: %d" , version )
0 commit comments