-
Notifications
You must be signed in to change notification settings - Fork 119
/
secrets.go
415 lines (374 loc) · 11.3 KB
/
secrets.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright 2019 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package config
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/newrelic/nri-flex/internal/load"
"github.com/newrelic/nri-flex/internal/utils"
)
// loadSecrets if secrets configured fetch, store and substitute secrets
func loadSecrets(config *load.Config) error {
var ymlStr string
var err error
for name, secret := range config.Secrets {
if secret.Kind == "" {
err = fmt.Errorf("config: secret needs 'kind' parameter to be set")
load.Logrus.WithFields(logrus.Fields{
"secret": name,
}).Error(err.Error())
break
}
if secret.File == "" && secret.Data == "" && secret.HTTP.URL == "" {
err = fmt.Errorf("config: secret needs 'file', 'data' and 'http' parameter to be set")
load.Logrus.WithFields(logrus.Fields{
"secret": name,
}).Errorf(err.Error())
break
}
load.Logrus.WithFields(logrus.Fields{
"secret": name,
"kind": secret.Kind,
}).Debug("config: fetching secret")
tempSecret := secret
secretResult := ""
results := map[string]interface{}{}
switch secret.Kind {
case "aws-kms":
if secret.Region == "" {
err = fmt.Errorf("config: secret needs 'region' parameter to be set")
load.Logrus.WithFields(logrus.Fields{
"secret": name,
"kind": secret.Kind,
}).Error(err.Error())
break
}
secretResult = awskmsDecrypt(name, tempSecret)
case "vault":
if secret.HTTP.URL == "" {
err = fmt.Errorf("config: vault secret requires 'http' parameter to be set")
load.Logrus.WithFields(logrus.Fields{
"secret": name,
"kind": secret.Kind,
}).Error(err.Error())
break
}
vaultFetch(name, tempSecret, results)
// decrypt secret locally using simpleEncrypDecryp module
case "local":
if secret.Key == "" {
err = fmt.Errorf("config: local secret requires 'key' parameter to be set")
load.Logrus.WithFields(logrus.Fields{
"secret": name,
"kind": secret.Kind,
}).Error(err.Error())
break
}
secretResult = localDecrypt(name, tempSecret)
}
if secretResult != "" || len(results) > 0 {
// convert config to string, only the first time
if ymlStr == "" {
ymlBytes, e := yaml.Marshal(config)
if e != nil {
err = fmt.Errorf("config: secret marshal failed")
load.Logrus.WithFields(logrus.Fields{
"secret": name,
"kind": secret.Kind,
"err": e,
}).Error(err.Error())
break
}
ymlStr = string(ymlBytes)
}
if secretResult != "" {
results["secret.result"] = secretResult
}
if secret.Type != "" && secret.Kind != "vault" {
handleDataType(results, secret.Type)
}
ymlStr = subSecrets(ymlStr, name, results)
}
}
// if ymlStr has a value it means a secret was successfully retrieved, decrypted, and substitutions were attempted
// we can then attempt to read and overwrite the config
if ymlStr != "" {
*config, err = ReadYML(ymlStr)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"config": config.Name,
"err": err,
}).Error("config: secret unmarshal failed")
}
}
return err
}
// subSecrets substitute secrets into yml str and return
func subSecrets(configStr string, secretKey string, secrets map[string]interface{}) string {
variableReplaces := regexp.MustCompile(`\${secret\.`+secretKey+`:.*?}`).FindAllString(configStr, -1)
for _, variableReplace := range variableReplaces {
variableKey := strings.TrimSuffix(strings.Split(variableReplace, "${secret."+secretKey+":")[1], "}") // eg. "channel"
if variableKey == "value" {
configStr = strings.Replace(configStr, variableReplace, fmt.Sprintf("%v", secrets["secret.result"]), -1)
} else if secrets[variableKey] != nil {
configStr = strings.Replace(configStr, variableReplace, fmt.Sprintf("%v", secrets[variableKey]), -1)
}
}
return configStr
}
// vaultFetch fetch from Hashicorp Vault
func vaultFetch(name string, secret load.Secret, results map[string]interface{}) {
load.Logrus.WithFields(logrus.Fields{"name": name}).Debug("config: fetching vault secret")
bytes, err := httpWrapper(secret)
if err != nil {
load.Logrus.WithFields(logrus.Fields{"name": name, "err": err}).Error("config: fetching vault secret failed")
} else {
var jsonInterface map[string]interface{}
err := json.Unmarshal(bytes, &jsonInterface)
if err != nil {
load.Logrus.WithFields(logrus.Fields{"name": name, "err": err}).Error("config: vault data unmarshal failed")
} else {
// v1 and v2 engines have this available
if jsonInterface["data"] != nil {
load.Logrus.WithFields(logrus.Fields{"name": name}).Debug("config: fetching vault secret success")
switch firstData := jsonInterface["data"].(type) {
case map[string]interface{}:
isV2 := false
if firstData["data"] != nil { // v2 format
switch secondData := firstData["data"].(type) {
case map[string]interface{}:
// handle v2 data
isV2 = true
for key, val := range secondData {
results[key] = val
}
}
}
if !isV2 {
for key, val := range firstData {
results[key] = val
}
}
}
}
}
}
}
// awskmsDecrypt perform aws kms decrypt and return plaintext
func awskmsDecrypt(name string, secret load.Secret) string {
load.Logrus.WithFields(logrus.Fields{"name": name}).Debug("config: attempting to aws kms decrypt secret")
var secretData []byte
if secret.File != "" {
var fileData []byte
fileData, err := ioutil.ReadFile(secret.File)
if err == nil {
secretData, err = base64.StdEncoding.DecodeString(string(fileData))
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"name": name,
"err": err,
}).Error("config: secret base64 decode failed")
}
} else {
load.Logrus.WithFields(logrus.Fields{
"name": name,
"err": err,
"file": secret.File,
}).Error("config: aws kms read file failed")
}
} else if secret.Data != "" {
var err error
secretData, err = base64.StdEncoding.DecodeString(secret.Data)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"name": name,
"err": err,
}).Error("config: aws kms base64 decode failed")
}
} else if secret.HTTP.URL != "" {
bytes, err := httpWrapper(secret)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"url": secret.HTTP.URL,
"name": name,
"err": err,
}).Error("config: aws kms http fetch failed")
} else {
var err error
secretData, err = base64.StdEncoding.DecodeString(string(bytes))
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"name": name,
"err": err,
}).Error("config: aws kms base64 decode failed")
}
}
}
if len(secretData) > 0 {
var sess *session.Session
sharedConfigFiles := []string{}
if secret.CredentialFile != "" {
sharedConfigFiles = append(sharedConfigFiles, secret.CredentialFile)
}
if secret.ConfigFile != "" {
sharedConfigFiles = append(sharedConfigFiles, secret.ConfigFile)
}
if len(sharedConfigFiles) > 0 {
load.Logrus.WithFields(logrus.Fields{
"name": name,
}).Debug("config: aws kms decrypt using custom credentials and/or config")
sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
SharedConfigFiles: sharedConfigFiles,
}))
} else {
load.Logrus.WithFields(logrus.Fields{
"name": name,
}).Debug("config: aws kms decrypt using default credentials")
sess = session.Must(session.NewSession(&aws.Config{
Region: aws.String(secret.Region),
}))
}
kmsClient := kms.New(sess)
params := &kms.DecryptInput{
CiphertextBlob: secretData,
}
resp, err := kmsClient.Decrypt(params)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"name": name,
}).Error("config: aws kms decrypt secret failed")
return ""
}
result := string(resp.Plaintext)
load.Logrus.WithFields(logrus.Fields{
"name": name,
"secret": result,
}).Debug("config: aws kms decrypt secret success")
return result
}
return ""
}
func handleDataType(results map[string]interface{}, dataType string) {
switch dataType {
case "json":
var jsonResult map[string]interface{}
err := json.Unmarshal([]byte(results["secret.result"].(string)), &jsonResult)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"err": err,
}).Error("config: secret unmarshal failed")
} else {
for key, value := range jsonResult {
results[key] = fmt.Sprintf("%v", value)
}
}
case "equal":
commaSplit := strings.Split(results["secret.result"].(string), ",")
for _, initialSplit := range commaSplit {
equalSplit := strings.SplitN(initialSplit, "=", 2)
if len(equalSplit) == 2 {
results[equalSplit[0]] = fmt.Sprintf("%v", equalSplit[1])
}
}
}
}
func httpWrapper(secret load.Secret) ([]byte, error) {
client := &http.Client{}
tlsConf := &tls.Config{}
if secret.HTTP.TLSConfig.InsecureSkipVerify {
tlsConf.InsecureSkipVerify = secret.HTTP.TLSConfig.InsecureSkipVerify
}
if secret.HTTP.TLSConfig.Ca != "" {
rootCAs := x509.NewCertPool()
ca, err := ioutil.ReadFile(secret.HTTP.TLSConfig.Ca)
if err != nil {
load.Logrus.WithError(err).Error("config: secret failed to read tls ca")
} else {
rootCAs.AppendCertsFromPEM(ca)
tlsConf.RootCAs = rootCAs
}
}
clientConf := &http.Transport{
TLSClientConfig: tlsConf,
}
client.Transport = clientConf
req, err := http.NewRequest("GET", secret.HTTP.URL, nil)
if err != nil {
return nil, err
}
for header, value := range secret.HTTP.Headers {
req.Header.Add(header, value)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return bytes, nil
}
return nil, fmt.Errorf("http fetch failed %v %v", resp.StatusCode, string(bytes))
}
// localDecrypt perform local decrypt and return plaintext if decrpyted successfully
func localDecrypt(name string, secret load.Secret) string {
load.Logrus.WithFields(logrus.Fields{"name": name}).Debug("config: attempting to local decrypt secret")
var secretData []byte
if secret.File != "" {
var fileData []byte
fileData, err := ioutil.ReadFile(secret.File)
if err == nil {
secretData, err = hex.DecodeString(string(fileData))
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"name": name,
}).WithError(err).Error("config: local secret hex decode failed")
}
} else {
load.Logrus.WithFields(logrus.Fields{
"name": name,
"file": secret.File,
}).WithError(err).Error("config: local read file failed")
}
} else if secret.Data != "" {
var err error
secretData, err = hex.DecodeString(secret.Data)
if err != nil {
load.Logrus.WithFields(logrus.Fields{
"name": name,
}).WithError(err).Error("config: local secret hex decode failed")
}
}
if len(secretData) > 0 {
if secret.Key != "" {
result, err := utils.Decrypt(secretData, secret.Key)
if err == nil {
return string(result)
}
load.Logrus.WithFields(logrus.Fields{
"name": name,
"key": secret.Key,
}).Error("config: local unable to decrypt using key provided, return encrypted data as is")
}
}
return string(secretData)
}