-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (128 loc) · 3.54 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
const envPrefix = "SECRET_"
var (
vaultAddr = flag.String("h", "http://localhost:8200", "Vault address")
vaultToken = flag.String("k", "", "Vault token")
kubeToken = flag.String("t", "", "Kubernetes token")
vaultKubeRole = flag.String("r", "", "Vault kubernetes role")
debugMode = flag.Bool("d", false, "Debug mode")
)
type KubeLoginData struct {
Jwt string `json:"jwt"`
Role string `json:"role"`
}
type Auth struct {
Client_token string `json:"client_token"`
Accessor string `json:"accessor"`
Policies []string `json:"policies"`
Token_policies []string `json:"token_policies"`
Metadata map[string]string
Lease_duration int `json:"lease_duration"`
Renewable bool `json:"bool"`
Entity_id string `json:"entity_id"`
Token_type string `json:"token_type"`
}
type SecretData struct {
Data map[string]string `json:"data"`
Metadata map[string]string `json:"metadata"`
}
type VaultResponse struct {
Request_id string `json:"request_id"`
Lease_id string `json:"lease_id"`
Renewable bool `json:"renewable"`
Lease_duration int `json:"lease_duration"`
SecretData SecretData `json:"data,omitempty"`
Wrap_info string `json:"wrap_info"`
Warnings string `json:"warnings"`
Auth Auth `json:"auth,omitempty"`
}
type Secrets struct {
key string
value string
}
func main() {
flag.Parse()
if *vaultToken == "" {
logInfo(fmt.Sprintf("Logging in to Vault: %s", *vaultAddr))
param := &KubeLoginData{
Jwt: *kubeToken,
Role: *vaultKubeRole,
}
resp := vaultKubernetesLogin(param)
vaultToken = &resp.Auth.Client_token
}
secrets := getTargetSecrets(envPrefix)
for _, value := range vaultRetrieveSecrets(secrets) {
fmt.Printf("\nexport %v=%v", value.key, value.value)
}
}
func vaultRetrieveSecrets(secrets []Secrets) (values []Secrets) {
for _, secret := range secrets {
p := strings.Split(secret.value, "/")
rootPath, subPath := p[0], strings.Join(p[1:], "/")
url := fmt.Sprintf("%s/v1/%s/data/%s", *vaultAddr, rootPath, subPath)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("X-Vault-Token", *vaultToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
r := VaultResponse{}
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &r)
values = append(values,
Secrets{
key: secret.key,
value: r.SecretData.Data[strings.ToLower(secret.key)],
},
)
}
return
}
func vaultKubernetesLogin(param *KubeLoginData) (r VaultResponse) {
b, _ := json.Marshal(param)
req, err := http.NewRequest("POST", *vaultAddr+"/v1/auth/kubernetes/login", bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &r)
if resp.StatusCode != 200 {
fmt.Printf("\nError occurred: %s", body)
}
return
}
func getTargetSecrets(prefix string) (secrets []Secrets) {
for _, e := range os.Environ() {
kv := strings.Split(e, "=")
matched, _ := regexp.MatchString(fmt.Sprintf("%s.*", prefix), kv[0])
if !matched {
continue
}
secrets = append(secrets, Secrets{key: kv[0][len(prefix):len(kv[0])], value: kv[1]})
}
return
}
func logInfo(message string) {
if !*debugMode {
return
}
fmt.Printf("\n%s", message)
}