-
Notifications
You must be signed in to change notification settings - Fork 50
/
provider.go
375 lines (313 loc) · 10 KB
/
provider.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"golang.org/x/net/context"
yaml "gopkg.in/yaml.v2"
"github.com/pkg/errors"
"golang.org/x/net/http2"
"k8s.io/klog"
)
const (
// VaultObjectTypeSecret secret vault object type for HashiCorp vault
VaultObjectTypeSecret string = "secret"
defaultVaultAddress string = "https://127.0.0.1:8200"
defaultKubernetesServiceAccountPath string = "/var/run/secrets/kubernetes.io/serviceaccount/token"
defaultVaultKubernetesMountPath string = "kubernetes"
)
// Provider implements the secrets-store-csi-driver provider interface
// and communicates with the Vault API.
type Provider struct {
VaultAddress string
VaultCAPem string
VaultCACert string
VaultCAPath string
VaultRole string
VaultSkipVerify bool
VaultServerName string
VaultK8SMountPath string
KubernetesServiceAccountPath string
}
// KeyValueObject is the object stored in Vault's Key-Value store.
type KeyValueObject struct {
// the path of the Key-Value Vault objects
ObjectPath string `json:"objectPath" yaml:"objectPath"`
// the name of the Key-Value Vault objects
ObjectName string `json:"objectName" yaml:"objectName"`
// the version of the Key-Value Vault objects
ObjectVersion string `json:"objectVersion" yaml:"objectVersion"`
}
type StringArray struct {
Array []string `json:"array" yaml:"array"`
}
// NewProvider creates a new provider HashiCorp Vault.
func NewProvider() (*Provider, error) {
klog.V(2).Infof("NewVaultProvider")
var p Provider
return &p, nil
}
func readJWTToken(path string) (string, error) {
klog.V(2).Infof("vault: reading jwt token.....")
data, err := ioutil.ReadFile(path)
if err != nil {
return "", errors.Wrap(err, "failed to read jwt token")
}
return string(bytes.TrimSpace(data)), nil
}
func (p *Provider) createHTTPClient() (*http.Client, error) {
rootCAs, err := p.getRootCAsPools()
if err != nil {
return nil, err
}
tlsClientConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: rootCAs,
}
if p.VaultSkipVerify {
tlsClientConfig.InsecureSkipVerify = true
}
if p.VaultServerName != "" {
tlsClientConfig.ServerName = p.VaultServerName
}
transport := &http.Transport{
TLSClientConfig: tlsClientConfig,
}
if err := http2.ConfigureTransport(transport); err != nil {
return nil, errors.New("failed to configure http2")
}
return &http.Client{
Transport: transport,
}, nil
}
func (p *Provider) login(jwt string, roleName string) (string, error) {
klog.V(2).Infof("vault: performing vault login.....")
client, err := p.createHTTPClient()
if err != nil {
return "", err
}
addr := p.VaultAddress + "/v1/auth/" + p.VaultK8SMountPath + "/login"
body := fmt.Sprintf(`{"role": "%s", "jwt": "%s"}`, roleName, jwt)
klog.V(2).Infof("vault: vault address: %s\n", addr)
req, err := http.NewRequest(http.MethodPost, addr, strings.NewReader(body))
if err != nil {
return "", errors.Wrapf(err, "couldn't generate request")
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", errors.Wrapf(err, "couldn't login")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var b bytes.Buffer
io.Copy(&b, resp.Body)
return "", fmt.Errorf("failed to get successful response: %#v, %s",
resp, b.String())
}
var s struct {
Auth struct {
ClientToken string `json:"client_token"`
} `json:"auth"`
}
if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
return "", errors.Wrapf(err, "failed to read body")
}
return s.Auth.ClientToken, nil
}
func (p *Provider) getSecret(token string, secretPath string, secretName string, secretVersion string) (string, error) {
klog.V(2).Infof("vault: getting secrets from vault.....")
client, err := p.createHTTPClient()
if err != nil {
return "", err
}
if secretVersion == "" {
secretVersion = "0"
}
addr := p.VaultAddress + "/v1/secret/data" + secretPath + "?version=" + secretVersion
req, err := http.NewRequest(http.MethodGet, addr, nil)
// Set vault token.
req.Header.Set("X-Vault-Token", token)
if err != nil {
return "", errors.Wrapf(err, "couldn't generate request")
}
resp, err := client.Do(req)
if err != nil {
return "", errors.Wrapf(err, "couldn't get secret")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var b bytes.Buffer
io.Copy(&b, resp.Body)
return "", fmt.Errorf("failed to get successful response: %#v, %s",
resp, b.String())
}
var d struct {
Data struct {
Data map[string]string `json:"data"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&d); err != nil {
return "", errors.Wrapf(err, "failed to read body")
}
return d.Data.Data[secretName], nil
}
func (p *Provider) getRootCAsPools() (*x509.CertPool, error) {
switch {
case p.VaultCAPem != "":
certPool := x509.NewCertPool()
if err := loadCert(certPool, []byte(p.VaultCAPem)); err != nil {
return nil, err
}
return certPool, nil
case p.VaultCAPath != "":
certPool := x509.NewCertPool()
if err := loadCertFolder(certPool, p.VaultCAPath); err != nil {
return nil, err
}
return certPool, nil
case p.VaultCACert != "":
certPool := x509.NewCertPool()
if err := loadCertFile(certPool, p.VaultCACert); err != nil {
return nil, err
}
return certPool, nil
default:
certPool, err := x509.SystemCertPool()
if err != nil {
return nil, errors.Wrapf(err, "couldn't load system certs")
}
return certPool, err
}
}
// loadCert loads a single pem-encoded certificate into the given pool.
func loadCert(pool *x509.CertPool, pem []byte) error {
if ok := pool.AppendCertsFromPEM(pem); !ok {
return fmt.Errorf("failed to parse PEM")
}
return nil
}
// loadCertFile loads the certificate at the given path into the given pool.
func loadCertFile(pool *x509.CertPool, p string) error {
pem, err := ioutil.ReadFile(p)
if err != nil {
return errors.Wrapf(err, "couldn't read CA file from disk")
}
if err := loadCert(pool, pem); err != nil {
return errors.Wrapf(err, "couldn't load CA at %s", p)
}
return nil
}
// loadCertFolder iterates exactly one level below the given directory path and
// loads all certificates in that path. It does not recurse
func loadCertFolder(pool *x509.CertPool, p string) error {
if err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
return loadCertFile(pool, path)
}); err != nil {
return errors.Wrapf(err, "failed to load CAs at %s", p)
}
return nil
}
// MountSecretsStoreObjectContent mounts content of the vault object to target path
func (p *Provider) MountSecretsStoreObjectContent(ctx context.Context, attrib map[string]string, secrets map[string]string, targetPath string, permission os.FileMode) (err error) {
roleName := attrib["roleName"]
if roleName == "" {
return errors.Errorf("missing vault role name. please specify 'roleName' in pv definition.")
}
p.VaultRole = roleName
klog.V(2).Infof("vault: roleName %s", p.VaultRole)
p.VaultAddress = attrib["vaultAddress"]
if p.VaultAddress == "" {
p.VaultAddress = defaultVaultAddress
}
klog.V(2).Infof("vault: vault address %s", p.VaultAddress)
// One of the following variables should be set when vaultSkipTLSVerify is false.
// Otherwise, system certificates are used to make requests to vault.
p.VaultCAPem = attrib["vaultCAPem"]
p.VaultCACert = attrib["vaultCACertPath"]
p.VaultCAPath = attrib["vaultCADirectory"]
// Vault tls server name.
p.VaultServerName = attrib["vaultTLSServerName"]
if s := attrib["vaultSkipTLSVerify"]; s != "" {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
p.VaultSkipVerify = b
}
p.VaultK8SMountPath = attrib["vaultKubernetesMountPath"]
if p.VaultK8SMountPath == "" {
p.VaultK8SMountPath = defaultVaultKubernetesMountPath
}
p.KubernetesServiceAccountPath = attrib["vaultKubernetesServiceAccountPath"]
if p.KubernetesServiceAccountPath == "" {
p.KubernetesServiceAccountPath = defaultKubernetesServiceAccountPath
}
keyValueObjects := []KeyValueObject{}
objectsStrings := attrib["objects"]
fmt.Printf("objectsStrings: [%s]\n", objectsStrings)
var objects StringArray
err = yaml.Unmarshal([]byte(objectsStrings), &objects)
if err != nil {
fmt.Printf("unmarshall failed for objects")
return err
}
fmt.Printf("objects: [%v]", objects.Array)
for _, object := range objects.Array {
fmt.Printf("unmarshal object: [%s]\n", object)
var keyValueObject KeyValueObject
err = yaml.Unmarshal([]byte(object), &keyValueObject)
if err != nil {
fmt.Printf("unmarshall failed for keyValueObjects at index")
return err
}
keyValueObjects = append(keyValueObjects, keyValueObject)
}
for _, keyValueObject := range keyValueObjects {
content, err := p.GetKeyValueObjectContent(ctx, keyValueObject.ObjectPath, keyValueObject.ObjectName, keyValueObject.ObjectVersion)
if err != nil {
return err
}
objectContent := []byte(content)
if err := ioutil.WriteFile(path.Join(targetPath, keyValueObject.ObjectPath), objectContent, permission); err != nil {
return errors.Wrapf(err, "secrets-store csi driver failed to write %s at %s", keyValueObject.ObjectPath, targetPath)
}
klog.V(0).Infof("secrets-store csi driver wrote %s at %s", keyValueObject.ObjectPath, targetPath)
}
return nil
}
// GetKeyValueObjectContent get content of the vault object
func (p *Provider) GetKeyValueObjectContent(ctx context.Context, objectPath string, objectName string, objectVersion string) (content string, err error) {
// Read the jwt token from disk
jwt, err := readJWTToken(p.KubernetesServiceAccountPath)
if err != nil {
return "", err
}
// Authenticate to vault using the jwt token
token, err := p.login(jwt, p.VaultRole)
if err != nil {
return "", err
}
// Get Secret
value, err := p.getSecret(token, objectPath, objectName, objectVersion)
if err != nil {
return "", err
}
return value, nil
}