/
webhook.go
378 lines (332 loc) · 12.7 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
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
package envconsul
import (
"context"
"fmt"
"net/http"
"path"
"strings"
"time"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kitlog "github.com/go-kit/kit/log"
"github.com/mitchellh/mapstructure"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)
const EnvconsulInjectorFQDN = "envconsul-injector.vault.crd.gocardless.com"
func NewWebhook(logger kitlog.Logger, mgr manager.Manager, injectorOpts InjectorOptions, opts ...func(*admission.Handler)) (*admission.Webhook, error) {
var handler admission.Handler
handler = &injector{
logger: kitlog.With(logger, "component", "EnvconsulInjector"),
decoder: mgr.GetAdmissionDecoder(),
client: mgr.GetClient(),
opts: injectorOpts,
}
for _, opt := range opts {
opt(&handler)
}
namespaceSelectors := &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
metav1.LabelSelectorRequirement{
Key: injectorOpts.NamespaceLabel,
Operator: metav1.LabelSelectorOpIn,
Values: []string{"enabled"},
},
},
}
return builder.NewWebhookBuilder().
Name(EnvconsulInjectorFQDN).
Mutating().
Operations(admissionregistrationv1beta1.Create).
ForType(&corev1.Pod{}).
FailurePolicy(admissionregistrationv1beta1.Fail).
NamespaceSelector(namespaceSelectors).
Handlers(handler).
WithManager(mgr).
Build()
}
type injector struct {
logger kitlog.Logger
decoder types.Decoder
client client.Client
opts InjectorOptions
}
type InjectorOptions struct {
Image string // image of theatre to use when constructing pod
InstallPath string // location of vault installation directory
NamespaceLabel string // namespace label that enables webhook to operate on
VaultConfigMapKey client.ObjectKey // reference to the vault config configMap
ServiceAccountTokenFile string // mount path of our projected service account token
ServiceAccountTokenExpiry time.Duration // Kubelet expiry for the service account token
ServiceAccountTokenAudience string // optional token audience
}
var (
podLabels = []string{"pod_namespace"}
handleTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "theatre_vault_envconsul_injector_handle_total",
Help: "Count of requests handled by the webhook",
},
podLabels,
)
mutateTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "theatre_vault_envconsul_injector_mutate_total",
Help: "Count of pods mutated by the webhook",
},
podLabels,
)
skipTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "theatre_vault_envconsul_injector_skip_total",
Help: "Count of pods skipped by the webhook, as they lack annotations",
},
podLabels,
)
errorsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "theatre_vault_envconsul_injector_errors_total",
Help: "Count of not-allowed responses from webhook",
},
podLabels,
)
)
func (i *injector) Handle(ctx context.Context, req types.Request) (resp types.Response) {
labels := prometheus.Labels{"pod_namespace": req.AdmissionRequest.Namespace}
logger := kitlog.With(i.logger, "uuid", string(req.AdmissionRequest.UID))
logger.Log("event", "request.start")
defer func(start time.Time) {
logger.Log("event", "request.end", "duration", time.Since(start).Seconds())
handleTotal.With(labels).Inc()
{ // add 0 to initialise the metrics
mutateTotal.With(labels).Add(0)
skipTotal.With(labels).Add(0)
errorsTotal.With(labels).Add(0)
}
// Catch any Allowed=false responses, as this means we've failed to accept this pod
if !resp.Response.Allowed {
errorsTotal.With(labels).Inc()
}
}(time.Now())
pod := &corev1.Pod{}
if err := i.decoder.Decode(req, pod); err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
// This webhook receives requests on all pod creation and so is in the critical
// path for all pod creation. We need to exit for pods that don't have the
// annotation on them here so they cant start uninterrupted in the event
// code futher along returns an error.
if _, ok := pod.Annotations[fmt.Sprintf("%s/configs", EnvconsulInjectorFQDN)]; !ok {
logger.Log("event", "pod.skipped", "msg", "no annotation found")
skipTotal.With(labels).Inc()
return admission.PatchResponse(pod, pod)
}
// ensure the pod has a namespace if it has one as we use it in the secretMountPathPrefix
pod.Namespace = req.AdmissionRequest.Namespace
// if the request object (pod) has a name use it
if req.AdmissionRequest.Name != "" {
pod.Name = req.AdmissionRequest.Name
}
logger = kitlog.With(logger,
"pod_namespace", pod.Namespace,
"pod_name", pod.Name,
)
mutateTotal.With(labels).Inc() // we're committed to mutating this pod now
vaultConfigMap := &corev1.ConfigMap{}
if err := i.client.Get(ctx, i.opts.VaultConfigMapKey, vaultConfigMap); err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
vaultConfig, err := newVaultConfig(vaultConfigMap)
if err != nil {
logger.Log("event", "vault.config", "error", err)
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
mutatedPod := podInjector{InjectorOptions: i.opts, vaultConfig: vaultConfig}.Inject(*pod)
if mutatedPod == nil {
logger.Log("event", "pod.skipped", "msg", "no annotation found during inject - this should never occur")
return admission.PatchResponse(pod, pod)
}
return admission.PatchResponse(pod, mutatedPod)
}
// vaultConfig specifies the structure we expect to find in a cluster-global namespace,
// which we intend to be provisioned as part of whatever process generates the auth
// backend in Vault.
//
// If we can't parse the configmap into this structure, we should fail our webhook.
type vaultConfig struct {
Address string `mapstructure:"address"`
AuthMountPath string `mapstructure:"auth_mount_path"`
AuthRole string `mapstructure:"auth_role"`
SecretMountPathPrefix string `mapstructure:"secret_mount_path_prefix"`
}
func newVaultConfig(cfgmap *corev1.ConfigMap) (vaultConfig, error) {
var cfg vaultConfig
return cfg, mapstructure.Decode(cfgmap.Data, &cfg)
}
// podInjector isolates the logic around injecting theatre-envconsul away from anything to
// do with mutating webhooks. This makes it easy to unit test without getting tangled in
// webhook noise.
type podInjector struct {
InjectorOptions
vaultConfig
}
// Inject configures the given pod to use theatre-envconsul. If it returns nil, it's
// because the pod isn't configured for injection.
func (i podInjector) Inject(pod corev1.Pod) *corev1.Pod {
containerConfigs := parseContainerConfigs(pod)
if containerConfigs == nil {
return nil
}
mutatedPod := pod.DeepCopy()
expirySeconds := int64(i.ServiceAccountTokenExpiry / time.Second)
mutatedPod.Spec.InitContainers = append(mutatedPod.Spec.InitContainers, i.buildInitContainer())
mutatedPod.Spec.Volumes = append(
mutatedPod.Spec.Volumes,
// Installation directory for theatre binaries, used as a scratch installation path
corev1.Volume{
Name: "theatre-envconsul-install",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
// Projected service account tokens that are automatically rotated, unlike the default
// service account tokens Kubernetes normally mounts.
corev1.Volume{
Name: "theatre-envconsul-serviceaccount",
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
// Ensure this token is readable by whatever user the container might run in, as
// your application might run with a non-root user but must be able to access
// its secrets.
DefaultMode: func() *int32 { mode := int32(444); return &mode }(),
Sources: []corev1.VolumeProjection{
corev1.VolumeProjection{
ServiceAccountToken: &corev1.ServiceAccountTokenProjection{
Path: path.Base(i.ServiceAccountTokenFile),
ExpirationSeconds: &expirySeconds,
Audience: i.ServiceAccountTokenAudience,
},
},
},
},
},
},
)
// If we don't already have an fsGroup set, we'll need to configure it so we can read
// the contents of the volumes we mount. Failing to do this will prevent us from reading
// the projected service account token.
if mutatedPod.Spec.SecurityContext == nil {
mutatedPod.Spec.SecurityContext = &corev1.PodSecurityContext{}
}
if mutatedPod.Spec.SecurityContext.FSGroup == nil {
defaultFSGroup := int64(1000)
mutatedPod.Spec.SecurityContext.FSGroup = &defaultFSGroup
}
secretMountPathPrefix := path.Join(i.vaultConfig.SecretMountPathPrefix, pod.Namespace, pod.Spec.ServiceAccountName)
for idx, container := range mutatedPod.Spec.Containers {
containerConfigPath, ok := containerConfigs[container.Name]
if !ok {
continue
}
mutatedPod.Spec.Containers[idx] = i.configureContainer(container, containerConfigPath, secretMountPathPrefix)
}
return mutatedPod
}
// parseContainerConfigs extracts the pod annotation and parses that configuration
// required for this container.
//
// envconsul-injector.vault.crd.gocardless.com/configs: app:config.yaml,sidecar
//
// Valid values for the annotation are:
//
// annotation ::= container_config | ',' annotation
// container_config ::= container_name ( ':' config_file )?
//
// If no config file is specified, we inject theatre-envconsul but don't load
// configuration from files, relying solely on environment variables.
func parseContainerConfigs(pod corev1.Pod) map[string]string {
configString, ok := pod.Annotations[fmt.Sprintf("%s/configs", EnvconsulInjectorFQDN)]
if !ok {
return nil
}
containerConfigs := map[string]string{}
for _, containerConfig := range strings.Split(configString, ",") {
elems := strings.SplitN(containerConfig, ":", 2)
if len(elems) == 1 {
containerConfigs[strings.TrimSpace(elems[0])] = "" // no config file means just inject
} else {
containerConfigs[strings.TrimSpace(elems[0])] = strings.TrimSpace(elems[1]) // otherwise use specified config file
}
}
return containerConfigs
}
func (i podInjector) buildInitContainer() corev1.Container {
return corev1.Container{
Name: "theatre-envconsul-injector",
Image: i.Image,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"theatre-envconsul", "install", "--path", i.InstallPath},
VolumeMounts: []corev1.VolumeMount{
corev1.VolumeMount{
Name: "theatre-envconsul-install",
MountPath: i.InstallPath,
ReadOnly: false,
},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("64Mi"),
corev1.ResourceCPU: resource.MustParse("50m"),
},
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("64Mi"),
corev1.ResourceCPU: resource.MustParse("50m"),
},
},
}
}
// configureContainer returns a copy with the command modified to run theatre-envconsul,
// along with a volume mount that will contain the envconsul binaries.
func (i podInjector) configureContainer(reference corev1.Container, containerConfigPath, secretMountPathPrefix string) corev1.Container {
c := &reference
args := []string{"exec"}
args = append(args, "--install-path", i.InstallPath)
args = append(args, "--vault-address", i.Address)
args = append(args, "--vault-path-prefix", secretMountPathPrefix)
args = append(args, "--auth-backend-mount-path", i.AuthMountPath)
args = append(args, "--auth-backend-role", i.AuthRole)
args = append(args, "--service-account-token-file", i.ServiceAccountTokenFile)
if containerConfigPath != "" {
args = append(args, "--config-file", containerConfigPath)
}
execCommand := []string{"--"}
execCommand = append(execCommand, reference.Command...)
execCommand = append(execCommand, reference.Args...)
args = append(args, execCommand...)
c.Command = []string{path.Join(i.InstallPath, "theatre-envconsul")}
c.Args = args
c.VolumeMounts = append(
c.VolumeMounts,
// Mount the binaries from our installation, ensuring we can run the command in this
// container
corev1.VolumeMount{
Name: "theatre-envconsul-install",
MountPath: i.InstallPath,
ReadOnly: true,
},
// Explicitly mount service account tokens from the projected volume
corev1.VolumeMount{
Name: "theatre-envconsul-serviceaccount",
MountPath: path.Dir(i.ServiceAccountTokenFile),
ReadOnly: true,
},
)
return *c
}