-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessor.go
211 lines (168 loc) · 4.61 KB
/
processor.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"reflect"
)
type FullSecret struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Metadata map[string]string `json:"metadata"`
Data map[string]string `json:"data"`
Type string `json:"type"`
}
var secretsEndpoint = fmt.Sprintf("/api/v1/namespaces/%s/secrets", namespace)
func processEvent(event VaultEvent) {
log.Println("Processing VaultEvent")
vaultObj := event.Object
// vaultObj := VaultObject{event.Object.(VaultObject)}
token := vaultObj.Spec.TokenRef
url := vaultObj.Spec.Url
path := vaultObj.Spec.Path
if event.Type == "ADDED" {
log.Println(yellow("Getting Token ", token, " URL ", url, " Path ", path))
vClient, _ := createVaultClient(token, url)
secret, err := vClient.read(path)
if err != nil {
return
}
processSecrets(vaultObj.Spec.Secret, secret)
} else if event.Type == "DELETED" {
// delete Secret
ok, err := deleteSecret(vaultObj.Spec.Secret)
if ok {
log.Println(red("["+vaultObj.Spec.Secret, "] Secret Deleted"))
}
if err != nil {
log.Println(err)
}
}
}
type customSecretList struct {
Items []VaultObject `json:"items"`
}
var (
allCustomSecrets *customSecretList
)
func deleteSecret(secretName string) (bool, error) {
var b []byte
secretContent := bytes.NewBuffer(b)
req, err := http.NewRequest("DELETE", apiHost+secretsEndpoint+"/"+secretName, secretContent)
if err != nil {
log.Println(red(err))
return false, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if resp.StatusCode != 200 {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func syncSecrets() {
// iterate through all customSecrets and Secret to ensure they are similar
// open up CustomSecret
resp, err := http.Get(apiHost + customSecretsEndpoint)
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&allCustomSecrets)
// log.Println(allCustomSecrets.Items)
for _, item := range allCustomSecrets.Items {
token := item.Spec.TokenRef
url := item.Spec.Url
path := item.Spec.Path
vClient, err := createVaultClient(token, url)
if err != nil {
log.Println(err)
break
}
secret, err := vClient.read(path)
if err != nil {
break
}
err = processSecrets(item.Spec.Secret, secret)
if err != nil {
log.Fatal("Error!")
}
}
}
func processSecrets(secret string, data map[string]interface{}) error {
pdata := make(map[string]string)
for k, v := range data {
pdata[k] = base64.StdEncoding.EncodeToString([]byte(v.(string)))
}
metadata := make(map[string]string)
metadata["name"] = secret
// pull secrets
var secretTmp = &FullSecret{
APIVersion: "v1",
Data: pdata,
Kind: "Secret",
Metadata: metadata,
Type: "Opaque",
}
var currentSecret *FullSecret
resp, err := http.Get(apiHost + secretsEndpoint + "/" + secret)
if err != nil {
return err
}
// compare secrets with data
if resp.StatusCode == 200 {
// replace Secret
content := json.NewDecoder(resp.Body)
content.Decode(¤tSecret)
if !reflect.DeepEqual(secretTmp.Data, currentSecret.Data) {
log.Println(green("["+secret, "] Vault Secret has changed."))
modifySecret(secretTmp)
} else {
log.Println("Vault Secret has not changed. Not replacing.")
}
} else if resp.StatusCode == 404 {
log.Println(red("["+secret, "] Vault Secret does not exist"))
writeSecret(secretTmp)
}
// if secrets different, update secret with new data
return nil
}
func writeSecret(secret *FullSecret) {
var b []byte
secretContent := bytes.NewBuffer(b)
err := json.NewEncoder(secretContent).Encode(secret)
req, err := http.NewRequest("POST", apiHost+secretsEndpoint, secretContent)
if err != nil {
log.Println(err)
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != 201 {
log.Println("Unable to write secret.")
} else {
log.Println(green("Secret written."))
}
}
func modifySecret(secret *FullSecret) {
var b []byte
secretContent := bytes.NewBuffer(b)
err := json.NewEncoder(secretContent).Encode(secret)
req, err := http.NewRequest("PUT", apiHost+secretsEndpoint+"/"+secret.Metadata["name"], secretContent)
if err != nil {
log.Println(err)
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != 200 {
log.Println("Unable to modify secret.")
} else {
log.Println(green("Secret modified"))
}
//output, _ := ioutil.ReadAll(resp.Body)
}