-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_marker.go
74 lines (56 loc) · 1.58 KB
/
env_marker.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
package kmsdecrypt
import (
"fmt"
"os"
"strings"
)
// DecryptEnv will return a map[string]string decrypted Key-Value pairs from ENV variables that includes the supplied Marker m in it's Key.
// If m is an empty string it will default to "KMS_DECRYPT". The returned maps Key will have the marker removed from it's Key-name.
// Returns map[string]string and error.
func (d *KmsDecrypter) DecryptEnv(m string) (map[string]string, error) {
if m == "" {
m = "KMS_DECRYPT"
}
envs := os.Environ()
resultChannel := make(chan resultKeyValue)
count := 0
for _, env := range envs {
slice := strings.SplitN(env, "=", 2)
if len(slice) != 2 {
continue
}
newkey := ""
key := slice[0]
value := slice[1]
// If marker wasn't found, continue to next env var.
if !strings.Contains(key, m) {
continue
}
count++
// Create the new Key name.
surrounding := fmt.Sprintf("_%v_", m)
leading := fmt.Sprintf("_%v", m)
trailing := fmt.Sprintf("%v_", m)
switch {
case strings.Contains(key, surrounding):
newkey = strings.Replace(key, surrounding, "", 1)
case strings.Contains(key, leading):
newkey = strings.Replace(key, leading, "", 1)
case strings.Contains(key, trailing):
newkey = strings.Replace(key, trailing, "", 1)
default:
newkey = strings.Replace(key, m, "", 1)
}
go d.decryptKeyValue(&newkey, &value, resultChannel)
}
// Wait for all go-routines to finish.
result := make(map[string]string)
for i := 0; i < count; i++ {
res := <-resultChannel
if res.err != nil {
return result, res.err
}
result[res.key] = res.val
}
return result, nil
}