/
vault.go
295 lines (250 loc) · 9.95 KB
/
vault.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
/*
Copyright 2020 The Rook Authors. All rights reserved.
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 kms
import (
"context"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/vault/api"
"github.com/libopenstorage/secrets"
"github.com/libopenstorage/secrets/vault"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// EtcVaultDir is vault config dir
EtcVaultDir = "/etc/vault"
// VaultSecretEngineKey is the type of secret engine used (kv, transit)
VaultSecretEngineKey = "VAULT_SECRET_ENGINE"
// VaultKVSecretEngineKey is a kv secret engine type
VaultKVSecretEngineKey = "kv"
// VaultTransitSecretEngineKey is a transit secret engine type
VaultTransitSecretEngineKey = "transit"
)
var (
vaultMandatoryConnectionDetails = []string{api.EnvVaultAddress}
)
// Used for unit tests mocking too as well as production code
var (
createTmpFile = ioutil.TempFile
getRemoveCertFiles = getRemoveCertFilesFunc
)
type removeCertFilesFunction func()
/* VAULT API INTERNAL VALUES
// Refer to https://pkg.golangclub.com/github.com/hashicorp/vault/api?tab=doc#pkg-constants
const EnvVaultAddress = "VAULT_ADDR"
const EnvVaultAgentAddr = "VAULT_AGENT_ADDR"
const EnvVaultCACert = "VAULT_CACERT"
const EnvVaultCAPath = "VAULT_CAPATH"
const EnvVaultClientCert = "VAULT_CLIENT_CERT"
const EnvVaultClientKey = "VAULT_CLIENT_KEY"
const EnvVaultClientTimeout = "VAULT_CLIENT_TIMEOUT"
const EnvVaultSRVLookup = "VAULT_SRV_LOOKUP"
const EnvVaultSkipVerify = "VAULT_SKIP_VERIFY"
const EnvVaultNamespace = "VAULT_NAMESPACE"
const EnvVaultTLSServerName = "VAULT_TLS_SERVER_NAME"
const EnvVaultWrapTTL = "VAULT_WRAP_TTL"
const EnvVaultMaxRetries = "VAULT_MAX_RETRIES"
const EnvVaultToken = "VAULT_TOKEN"
const EnvVaultMFA = "VAULT_MFA"
const EnvRateLimit = "VAULT_RATE_LIMIT"
*/
// InitVault inits the secret store
func InitVault(context *clusterd.Context, namespace string, config map[string]string) (secrets.Secrets, error) {
c := make(map[string]interface{})
// So that we don't alter the content of c.config for later iterations
// We just want to swap the name of the TLS config secret name --> file name for the kms lib
oriConfig := make(map[string]string)
for k, v := range config {
oriConfig[k] = v
}
// Populate TLS config
newConfigWithTLS, removeCertFiles, err := configTLS(context, namespace, oriConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize vault tls configuration")
}
defer removeCertFiles()
// Populate TLS config
for key, value := range newConfigWithTLS {
c[key] = string(value)
}
// Initialize Vault
v, err := vault.New(c)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize vault secret store")
}
return v, nil
}
// configTLS returns a map of TLS config that map physical files for the TLS library to load
// Also it returns a function to remove the temporary files (certs, keys)
// The signature has named result parameters to help building 'defer' statements especially for the
// content of removeCertFiles which needs to be populated by the files to remove if no errors and be
// nil on errors
func configTLS(clusterdContext *clusterd.Context, namespace string, config map[string]string) (newConfig map[string]string, removeCertFiles removeCertFilesFunction, retErr error) {
ctx := context.TODO()
var filesToRemove []*os.File
defer func() {
// Build the function that the caller should use to remove the temp files here
// create it when this function is returning based on the currently-recorded files
removeCertFiles = getRemoveCertFiles(filesToRemove)
if retErr != nil {
// If we encountered an error, remove the temp files
removeCertFiles()
// Also return an empty function to remove the temp files
// It's fine to use nil here since the defer from the calling functions is only
// triggered after evaluating any error, if on error the defer is not triggered since we
// have returned already
removeCertFiles = nil
}
}()
for _, tlsOption := range cephv1.VaultTLSConnectionDetails {
tlsSecretName := GetParam(config, tlsOption)
if tlsSecretName == "" {
continue
}
// If the string already has the correct path /etc/vault, we are in provisioner code and all the envs have been populated by the op already
if !strings.Contains(tlsSecretName, EtcVaultDir) {
secret, err := clusterdContext.Clientset.CoreV1().Secrets(namespace).Get(ctx, tlsSecretName, v1.GetOptions{})
if err != nil {
return nil, removeCertFiles, errors.Wrapf(err, "failed to fetch tls k8s secret %q", tlsSecretName)
}
// Generate a temp file
file, err := createTmpFile("", "")
if err != nil {
return nil, removeCertFiles, errors.Wrapf(err, "failed to generate temp file for k8s secret %q content", tlsSecretName)
}
// Write into a file
err = ioutil.WriteFile(file.Name(), secret.Data[tlsSecretKeyToCheck(tlsOption)], 0444)
if err != nil {
return nil, removeCertFiles, errors.Wrapf(err, "failed to write k8s secret %q content to a file", tlsSecretName)
}
logger.Debugf("replacing %q current content %q with %q", tlsOption, config[tlsOption], file.Name())
// Update the env var with the path
config[tlsOption] = file.Name()
// Add the file to the list of files to remove
filesToRemove = append(filesToRemove, file)
} else {
logger.Debugf("value of tlsOption %q tlsSecretName is already correct %q", tlsOption, tlsSecretName)
}
}
return config, removeCertFiles, nil
}
func getRemoveCertFilesFunc(filesToRemove []*os.File) removeCertFilesFunction {
return removeCertFilesFunction(func() {
for _, file := range filesToRemove {
logger.Debugf("closing %q", file.Name())
err := file.Close()
if err != nil {
logger.Errorf("failed to close file %q. %v", file.Name(), err)
}
logger.Debugf("closed %q", file.Name())
logger.Debugf("removing %q", file.Name())
err = os.Remove(file.Name())
if err != nil {
logger.Errorf("failed to remove file %q. %v", file.Name(), err)
}
logger.Debugf("removed %q", file.Name())
}
})
}
func put(v secrets.Secrets, secretName, secretValue string, keyContext map[string]string) error {
// First we must see if the key entry already exists, if it does we do nothing
key, err := get(v, secretName, keyContext)
if err != nil && err != secrets.ErrInvalidSecretId {
return errors.Wrapf(err, "failed to get secret %q in vault", secretName)
}
if key != "" {
logger.Debugf("key %q already exists in vault!", secretName)
return nil
}
// Build Secret
data := make(map[string]interface{})
data[secretName] = secretValue
// #nosec G104 Write the encryption key in Vault
err = v.PutSecret(secretName, data, keyContext)
if err != nil {
return errors.Wrapf(err, "failed to put secret %q in vault", secretName)
}
return nil
}
func get(v secrets.Secrets, secretName string, keyContext map[string]string) (string, error) {
// #nosec G104 Write the encryption key in Vault
s, err := v.GetSecret(secretName, keyContext)
if err != nil {
return "", err
}
return s[secretName].(string), nil
}
func delete(v secrets.Secrets, secretName string, keyContext map[string]string) error {
// #nosec G104 Write the encryption key in Vault
err := v.DeleteSecret(secretName, keyContext)
if err != nil {
return errors.Wrapf(err, "failed to delete secret %q in vault", secretName)
}
return nil
}
func buildKeyContext(config map[string]string) map[string]string {
// Key context is just the Vault namespace, available in the enterprise version only
keyContext := map[string]string{secrets.KeyVaultNamespace: config[api.EnvVaultNamespace]}
vaultNamespace, ok := config[api.EnvVaultNamespace]
if !ok || vaultNamespace == "" {
keyContext = map[string]string{}
}
return keyContext
}
// IsVault determines whether the configured KMS is Vault
func (c *Config) IsVault() bool {
return c.Provider == "vault"
}
func validateVaultConnectionDetails(clusterdContext *clusterd.Context, ns string, kmsConfig map[string]string) error {
ctx := context.TODO()
for _, option := range vaultMandatoryConnectionDetails {
if GetParam(kmsConfig, option) == "" {
return errors.Errorf("failed to find connection details %q", option)
}
}
// We do not support a directory with multiple CA since we fetch a k8s Secret and read its content
// So we operate with a single CA only
if GetParam(kmsConfig, api.EnvVaultCAPath) != "" {
return errors.Errorf("failed to validate TLS connection details. %q is not supported. use %q instead", api.EnvVaultCAPath, api.EnvVaultCACert)
}
// Validate potential TLS configuration
for _, tlsOption := range cephv1.VaultTLSConnectionDetails {
tlsSecretName := GetParam(kmsConfig, tlsOption)
if tlsSecretName != "" {
// Fetch the secret
s, err := clusterdContext.Clientset.CoreV1().Secrets(ns).Get(ctx, tlsSecretName, v1.GetOptions{})
if err != nil {
return errors.Errorf("failed to find TLS connection details k8s secret %q", tlsSecretName)
}
// Check the Secret key and its content
keyToCheck := tlsSecretKeyToCheck(tlsOption)
cert, ok := s.Data[keyToCheck]
if !ok || len(cert) == 0 {
return errors.Errorf("failed to find TLS connection key %q for %q in k8s secret %q", keyToCheck, tlsOption, tlsSecretName)
}
}
}
return nil
}
func tlsSecretKeyToCheck(tlsOption string) string {
if tlsOption == api.EnvVaultCACert || tlsOption == api.EnvVaultClientCert {
return vaultCACertSecretKeyName
} else if tlsOption == api.EnvVaultClientKey {
return vaultKeySecretKeyName
}
return ""
}