-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook.go
336 lines (287 loc) · 8.94 KB
/
webhook.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"github.com/marmotherder/mimir/clients"
"k8s.io/api/admission/v1beta1"
core_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
//patchReq is a struct for a patch request that doesn't exist in types from admission
type patchReq struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
// hook is the main function for inbound hook requests
func hook(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") != "application/json" {
http.Error(w, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
return
}
as := v1beta1.AdmissionResponse{}
ar, pod, err := readRequest(r.Body)
if err != nil {
setResultMessage(&as, err.Error())
}
if ar != nil && ar.Request.Operation == v1beta1.Create && err == nil {
if err != nil {
setResultMessage(&as, err.Error())
} else {
err := runCreateHook(w, r, ar, pod, &as)
if err != nil {
setResultMessage(&as, err.Error())
} else {
as.Allowed = true
}
}
} else if ar != nil && ar.Request.Operation == v1beta1.Delete && err == nil {
as.Allowed = true
kc, err := clients.NewK8SClient(opts.IsPod, opts.KubeconfigPath)
if err != nil {
setResultMessage(&as, err.Error())
} else {
genName := fmt.Sprintf("%s-%s", release, ar.Request.Name)
err := kc.CoreV1().Secrets(ar.Request.Namespace).Delete(genName, &meta_v1.DeleteOptions{})
if err != nil {
setResultMessage(&as, err.Error())
} else {
setResultMessage(&as, fmt.Sprintf("Deleted secret %s in namespace %s", genName, ar.Request.Namespace))
}
}
}
dispatchResponse(ar, as, w)
}
// loadSecret will load a secret from the remote secrets manager based on the annotations on the pod
func loadSecret(name, namespace, remote string) (*core_v1.Secret, *kubernetes.Clientset, map[string]string, error) {
smc, _, err := loadClient()
if err != nil {
return nil, nil, nil, err
}
secret, err := smc.GetSecret(remote)
if err != nil {
return nil, nil, nil, err
}
kc, err := clients.NewK8SClient(opts.IsPod, opts.KubeconfigPath)
if err != nil {
return nil, nil, nil, err
}
genName := fmt.Sprintf("%s-%s", release, name)
kc.CoreV1().Secrets(namespace).Delete(genName, &meta_v1.DeleteOptions{})
data := make(map[string][]byte)
for k, v := range secret.Data {
data[k] = []byte(v)
}
k8sSecret := &core_v1.Secret{
Type: core_v1.SecretTypeOpaque,
Data: data,
ObjectMeta: meta_v1.ObjectMeta{
Name: genName,
Namespace: namespace,
},
}
k8sSecret.Annotations = map[string]string{clients.Hook: sOpts.Hook}
return k8sSecret, kc, secret.Data, nil
}
// addPatchReq adds a patchReq struct to a slice in a repeatable generic way
func addPatchReq(isNew func() bool, op string, path string, patchReqs *[]patchReq, value interface{}) {
req := patchReq{
Op: op,
Value: value,
}
if isNew() {
req.Path = path
} else {
req.Path = fmt.Sprintf("%s/-", path)
}
*patchReqs = append(*patchReqs, req)
}
// runCreateHook is a split out function for running the steps when creating a pod
func runCreateHook(w http.ResponseWriter, r *http.Request, ar *v1beta1.AdmissionReview, pod *core_v1.Pod, as *v1beta1.AdmissionResponse) error {
remote, path, local, runEnv, err := shouldMutate(pod)
if err != nil {
return err
}
if remote == nil {
return nil
}
podName := pod.Name
if local != nil && *local != "" {
podName = *local
}
if podName == "" {
podName = string(ar.Request.UID)
}
namespace := pod.Namespace
if namespace == "" {
namespace = ar.Request.Namespace
}
if namespace == "" {
namespace = "default"
}
k8sSecret, kc, data, err := loadSecret(podName, namespace, *remote)
if err != nil {
return err
}
patchReqs := make([]patchReq, 0)
if path != nil {
vol := core_v1.Volume{Name: k8sSecret.Name}
vol.Secret = &core_v1.SecretVolumeSource{SecretName: k8sSecret.Name}
addPatchReq(func() bool { return len(pod.Spec.Volumes) == 0 }, "add", "/spec/volumes", &patchReqs, vol)
}
envs := make([]core_v1.EnvVar, 0)
if runEnv {
for k := range data {
selector := &core_v1.SecretKeySelector{Key: k}
selector.Name = k8sSecret.Name
envs = append(envs, core_v1.EnvVar{
Name: k,
ValueFrom: &core_v1.EnvVarSource{
SecretKeyRef: selector,
},
})
}
}
containerPatches := func(specPath string, containers []core_v1.Container) {
for idx, container := range containers {
if path != nil {
add := core_v1.VolumeMount{
Name: k8sSecret.Name,
ReadOnly: true,
MountPath: *path,
}
addPatchReq(func() bool { return len(container.VolumeMounts) == 0 }, "add", fmt.Sprintf("/spec/%s/%d/volumeMounts", specPath, idx), &patchReqs, add)
}
if len(envs) > 0 {
addPatchReq(func() bool { return len(container.Env) == 0 }, "add", fmt.Sprintf("/spec/%s/%d/env", specPath, idx), &patchReqs, envs)
}
}
}
containerPatches("containers", pod.Spec.Containers)
containerPatches("initContainers", pod.Spec.InitContainers)
annotations := make(map[string]string)
if len(pod.Annotations) > 0 {
annotations = pod.Annotations
}
annotations[clients.Managed] = "true"
addPatchReq(func() bool { return true }, "add", "/metadata/annotations", &patchReqs, annotations)
patchData, err := json.Marshal(patchReqs)
if err != nil {
return err
}
as.Patch = patchData
as.PatchType = func() *v1beta1.PatchType {
p := v1beta1.PatchTypeJSONPatch
return &p
}()
if _, err := kc.CoreV1().Secrets(namespace).Create(k8sSecret); err != nil {
return err
}
setResultMessage(as, fmt.Sprintf("Created secret %s in namespace %s", k8sSecret.Name, namespace))
return nil
}
// readRequest reads the inbound hook request
func readRequest(body io.ReadCloser) (*v1beta1.AdmissionReview, *core_v1.Pod, error) {
var ar v1beta1.AdmissionReview
if err := json.NewDecoder(body).Decode(&ar); err != nil {
return nil, nil, err
}
if len(ar.Request.Object.Raw) > 0 {
var pod core_v1.Pod
if err := json.Unmarshal(ar.Request.Object.Raw, &pod); err != nil {
return nil, nil, err
}
return &ar, &pod, nil
}
return &ar, nil, nil
}
// shouldMutate will scan the pod for the desired annotations
// If the annotations exist, return the paths we need to progress with the secrets creation
func shouldMutate(pod *core_v1.Pod) (*string, *string, *string, bool, error) {
isHook := false
remote := ""
path := ""
env := "false"
local := ""
for k, v := range pod.Annotations {
switch k {
case clients.Hook:
if v == sOpts.Hook {
isHook = true
}
case clients.Remote:
remote = v
case clients.Path:
path = v
case clients.Env:
env = v
case clients.Local:
local = v
}
}
runEnv, _ := strconv.ParseBool(env)
if !isHook {
return nil, nil, nil, false, nil
}
if remote == "" {
return nil, nil, nil, false, errors.New("Missing properties for remote secret name")
}
if path != "" {
return &remote, &path, &local, runEnv, nil
}
return &remote, nil, &local, runEnv, nil
}
// setResultMessage adds a message to the response struct, can be errors or otherwise
func setResultMessage(as *v1beta1.AdmissionResponse, message string) {
status := meta_v1.Status{Message: message}
as.Result = &status
}
// dispatchResponse writes out the json response payload from the hook
func dispatchResponse(ar *v1beta1.AdmissionReview, as v1beta1.AdmissionResponse, w http.ResponseWriter) {
resp := v1beta1.AdmissionReview{}
resp.Response = &as
if ar.Request != nil {
resp.Response.UID = ar.Request.UID
}
payloadjson, err := json.Marshal(resp)
if err != nil {
http.Error(w, "could not decode request message", http.StatusInternalServerError)
}
log.Println(string(payloadjson))
w.Write(payloadjson)
}
// shutdownServer runs when a term or kill command reaches the application. It is designed to deconstruct the k8s objects created by the init container
func shutdownServer(srv *http.Server) {
log.Println("Server shutdown started. Will try to cleanup dynamic resources")
kc, err := clients.NewK8SClient(opts.IsPod, opts.KubeconfigPath)
if err != nil {
log.Fatal(err)
}
log.Printf("Using release %s\n", release)
whs, err := kc.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().List(meta_v1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", release)})
if err != nil {
log.Println(err.Error())
} else {
for _, whs := range whs.Items {
log.Printf("Removing mutating webhook configuration %s\n", whs.Name)
kc.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Delete(whs.Name, &meta_v1.DeleteOptions{})
}
}
csr, err := kc.CertificatesV1beta1().CertificateSigningRequests().List(meta_v1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", release)})
if err != nil {
log.Println(err.Error())
} else {
for _, csr := range csr.Items {
log.Printf("Removing csr %s\n", csr.Name)
kc.CertificatesV1beta1().CertificateSigningRequests().Delete(csr.Name, &meta_v1.DeleteOptions{})
}
}
srv.Shutdown(context.Background())
}