-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathhashicorpvault_handler.go
352 lines (313 loc) · 10.2 KB
/
hashicorpvault_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright 2021 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resolver
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/go-logr/logr"
vaultapi "github.com/hashicorp/vault/api"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)
// HashicorpVaultHandler is specification of Hashi Corp Vault
type HashicorpVaultHandler struct {
vault *kedav1alpha1.HashiCorpVault
client *vaultapi.Client
stopCh chan struct{}
}
// NewHashicorpVaultHandler creates a HashicorpVaultHandler object
func NewHashicorpVaultHandler(v *kedav1alpha1.HashiCorpVault) *HashicorpVaultHandler {
return &HashicorpVaultHandler{
vault: v,
}
}
// Initialize the Vault client
func (vh *HashicorpVaultHandler) Initialize(logger logr.Logger) error {
config := vaultapi.DefaultConfig()
client, err := vaultapi.NewClient(config)
if err != nil {
return err
}
err = client.SetAddress(vh.vault.Address)
if err != nil {
return err
}
if len(vh.vault.Namespace) > 0 {
client.SetNamespace(vh.vault.Namespace)
}
token, err := vh.token(client)
if err != nil {
return err
}
if len(token) > 0 {
client.SetToken(token)
}
lookup, err := client.Auth().Token().LookupSelf()
// If token is not valid so get out of here early
if err != nil {
return err
}
if renew, ok := lookup.Data["renewable"].(bool); ok && renew {
vh.stopCh = make(chan struct{})
go vh.renewToken(logger)
}
vh.client = client
return nil
}
// token Extract a vault token from the Authentication method
func (vh *HashicorpVaultHandler) token(client *vaultapi.Client) (string, error) {
var token string
switch vh.vault.Authentication {
case kedav1alpha1.VaultAuthenticationToken:
// Got token from VAULT_TOKEN env variable
switch {
case len(client.Token()) > 0:
break
case len(vh.vault.Credential.Token) > 0:
token = vh.vault.Credential.Token
default:
return token, errors.New("could not get Vault token")
}
case kedav1alpha1.VaultAuthenticationKubernetes:
if len(vh.vault.Mount) == 0 {
return token, errors.New("auth mount not in config")
}
if len(vh.vault.Role) == 0 {
return token, errors.New("k8s role not in config")
}
if vh.vault.Credential == nil {
defaultCred := kedav1alpha1.Credential{
ServiceAccount: "/var/run/secrets/kubernetes.io/serviceaccount/token",
}
vh.vault.Credential = &defaultCred
}
if len(vh.vault.Credential.ServiceAccount) == 0 {
return token, errors.New("k8s SA file not in config")
}
// Get the JWT from POD
jwt, err := os.ReadFile(vh.vault.Credential.ServiceAccount)
if err != nil {
return token, err
}
data := map[string]interface{}{"jwt": string(jwt), "role": vh.vault.Role}
secret, err := client.Logical().Write(fmt.Sprintf("auth/%s/#", vh.vault.Mount), data)
if err != nil {
return token, err
}
token = secret.Auth.ClientToken
default:
return token, fmt.Errorf("vault auth method %s is not supported", vh.vault.Authentication)
}
return token, nil
}
// renewToken takes charge of renewing the vault token
func (vh *HashicorpVaultHandler) renewToken(logger logr.Logger) {
secret, err := vh.client.Auth().Token().RenewSelf(0)
if err != nil {
logger.Error(err, "Vault renew token: failed to create the payload")
}
renewer, err := vh.client.NewLifetimeWatcher(&vaultapi.RenewerInput{
Secret: secret,
//Grace: time.Duration(15 * time.Second),
//Increment: 60,
})
if err != nil {
logger.Error(err, "Vault renew token: cannot create the renewer")
}
go renewer.Renew()
defer func() {
renewer.Stop()
close(vh.stopCh)
}()
RenewWatcherLoop:
for {
select {
case <-vh.stopCh:
break RenewWatcherLoop
case err := <-renewer.DoneCh():
if err != nil {
logger.Error(err, "error renewing token")
}
break RenewWatcherLoop
}
}
}
// Read is used to get a secret from vault Read api. (e.g. secret)
func (vh *HashicorpVaultHandler) Read(path string) (*vaultapi.Secret, error) {
return vh.client.Logical().Read(path)
}
// Write is used to get a secret from vault that needs to pass along data and uses the vault Write api. (e.g. pki)
func (vh *HashicorpVaultHandler) Write(path string, data map[string]interface{}) (*vaultapi.Secret, error) {
return vh.client.Logical().Write(path, data)
}
// Stop is responsible for stopping the renewal token process
func (vh *HashicorpVaultHandler) Stop() {
if vh.stopCh != nil {
vh.stopCh <- struct{}{}
}
}
// getPkiRequest format the pkiData in a format that the vault sdk understands.
func (vh *HashicorpVaultHandler) getPkiRequest(pkiData *kedav1alpha1.VaultPkiData) (map[string]interface{}, error) {
var data map[string]interface{}
a, err := json.Marshal(pkiData)
if err != nil {
return nil, err
}
err = json.Unmarshal(a, &data)
if err != nil {
return nil, err
}
return data, nil
}
// getSecretValue extract the secret value from the vault api response. As the vault api returns us a map[string]interface{},
// specific handling might be needed for some secret type.
func (vh *HashicorpVaultHandler) getSecretValue(secret *kedav1alpha1.VaultSecret, vaultSecret *vaultapi.Secret) (string, error) {
if secret.Type == kedav1alpha1.VaultSecretTypeGeneric {
if _, ok := vaultSecret.Data["data"]; ok {
// Probably a v2 secret
secret.Type = kedav1alpha1.VaultSecretTypeSecretV2
} else {
secret.Type = kedav1alpha1.VaultSecretTypeSecret
}
}
switch secret.Type {
case kedav1alpha1.VaultSecretTypePki:
if vData, ok := vaultSecret.Data[secret.Key]; ok {
if secret.Key == "ca_chain" {
// Cast the secret to []interface{}
if ai, ok := vData.([]interface{}); ok {
// Cast the secret to []string
stringSlice := make([]string, len(ai))
for i, v := range ai {
stringSlice[i] = v.(string)
}
return strings.Join(stringSlice, "\n"), nil
}
err := fmt.Errorf("key '%s' is not castable to []interface{}", secret.Key)
return "", err
}
if s, ok := vData.(string); ok {
return s, nil
}
// If this happens, bad data from vault
err := fmt.Errorf("key '%s' is not castable to string", secret.Key)
return "", err
}
err := fmt.Errorf("key '%s' not found", secret.Key)
return "", err
case kedav1alpha1.VaultSecretTypeSecret:
if vData, ok := vaultSecret.Data[secret.Key]; ok {
if s, ok := vData.(string); ok {
return s, nil
}
err := fmt.Errorf("key '%s' is not castable to string", secret.Key)
return "", err
}
err := fmt.Errorf("key '%s' not found", secret.Key)
return "", err
case kedav1alpha1.VaultSecretTypeSecretV2:
if v2Data, ok := vaultSecret.Data["data"].(map[string]interface{}); ok {
if value, ok := v2Data[secret.Key]; ok {
if s, ok := value.(string); ok {
return s, nil
}
err := fmt.Errorf("key '%s' is not castable to string", secret.Key)
return "", err
}
err := fmt.Errorf("key '%s' not found", secret.Key)
return "", err
}
// Unreachable
return "", nil
default:
err := fmt.Errorf("unsupported vault secret type %s", secret.Type)
return "", err
}
}
// SecretGroup is used to group secret together by path, secretType and vaultPkiData.
type SecretGroup struct {
path string
secretType kedav1alpha1.VaultSecretType
vaultPkiData kedav1alpha1.VaultPkiData
}
// fetchSecret returns the vaultSecret at a given vault path. If the secret is a pki, then the secret will use the
// vault Write method and will send the pkiData along
func (vh *HashicorpVaultHandler) fetchSecret(secretType kedav1alpha1.VaultSecretType, path string, vaultPkiData *kedav1alpha1.VaultPkiData) (*vaultapi.Secret, error) {
var vaultSecret *vaultapi.Secret
var err error
switch secretType {
case kedav1alpha1.VaultSecretTypePki:
data, err := vh.getPkiRequest(vaultPkiData)
if err != nil {
return nil, err
}
vaultSecret, err = vh.Write(path, data)
if err != nil {
return nil, err
}
case kedav1alpha1.VaultSecretTypeSecret, kedav1alpha1.VaultSecretTypeSecretV2, kedav1alpha1.VaultSecretTypeGeneric:
vaultSecret, err = vh.Read(path)
if err != nil {
return nil, err
}
default:
err = fmt.Errorf("unsupported vault secret type %s", secretType)
return nil, err
}
return vaultSecret, nil
}
// ResolveSecrets allows to resolve a slice of secrets by vault. The function returns the list of secrets with the value updated.
// If multiple secret refers to the same SecretGroup, the secret will be fetched only once.
func (vh *HashicorpVaultHandler) ResolveSecrets(secrets []kedav1alpha1.VaultSecret) ([]kedav1alpha1.VaultSecret, error) {
// Group secret by path and type, this allows to fetch a path only once. This is useful for dynamic credentials
grouped := make(map[SecretGroup][]kedav1alpha1.VaultSecret)
vaultSecrets := make(map[SecretGroup]*vaultapi.Secret)
for _, e := range secrets {
group := SecretGroup{secretType: e.Type, path: e.Path, vaultPkiData: e.PkiData}
if _, ok := grouped[group]; !ok {
grouped[group] = make([]kedav1alpha1.VaultSecret, 0)
}
grouped[group] = append(grouped[group], e)
}
// For each group fetch the secret from vault
for group := range grouped {
vaultSecret, err := vh.fetchSecret(group.secretType, group.path, &group.vaultPkiData)
if err != nil {
// could not fetch secret, skipping group
continue
}
vaultSecrets[group] = vaultSecret
}
// For each secret in each group, fetch the value and add to out
out := make([]kedav1alpha1.VaultSecret, 0)
for group, unFetchedSecrets := range grouped {
vaultSecret := vaultSecrets[group]
for _, secret := range unFetchedSecrets {
if vaultSecret == nil {
// This happens if we were not able to fetch the secret from vault
secret.Value = ""
} else {
value, err := vh.getSecretValue(&secret, vaultSecret)
if err != nil {
secret.Value = ""
} else {
secret.Value = value
}
}
out = append(out, secret)
}
}
return out, nil
}