/
audit.go
159 lines (135 loc) · 6.44 KB
/
audit.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
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apiserver
import (
"context"
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
auditv1 "k8s.io/apiserver/pkg/apis/audit/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
)
var auditCodec runtime.Codec
func init() {
auditScheme := runtime.NewScheme()
utilruntime.Must(auditv1.AddToScheme(auditScheme))
var (
ser = json.NewSerializerWithOptions(json.DefaultMetaFactory, auditScheme, auditScheme, json.SerializerOptions{
Yaml: true,
Pretty: false,
Strict: false,
})
versions = schema.GroupVersions([]schema.GroupVersion{
auditv1.SchemeGroupVersion,
})
)
auditCodec = serializer.NewCodecFactory(auditScheme).CodecForVersions(ser, ser, versions, versions)
}
const (
// SecretWebhookKubeconfigDataKey is a constant for a key in the data of the secret containing a kubeconfig.
SecretWebhookKubeconfigDataKey = "kubeconfig.yaml"
configMapAuditPolicyDataKey = "audit-policy.yaml"
volumeNameAuditPolicy = "audit-policy-config"
volumeNameAuditWebhookKubeconfig = "audit-webhook-kubeconfig"
volumeMountPathAuditPolicy = "/etc/kubernetes/audit"
volumeMountPathAuditWebhookKubeconfig = "/etc/kubernetes/webhook/audit"
)
// ReconcileSecretAuditWebhookKubeconfig reconciles the secret containing the kubeconfig for audit webhooks.
func ReconcileSecretAuditWebhookKubeconfig(ctx context.Context, c client.Client, secret *corev1.Secret, auditConfig *AuditConfig) error {
if auditConfig == nil || auditConfig.Webhook == nil || len(auditConfig.Webhook.Kubeconfig) == 0 {
// We don't delete the secret here as we don't know its name (as it's unique). Instead, we rely on the usual
// garbage collection for unique secrets/configmaps.
return nil
}
return ReconcileSecretWebhookKubeconfig(ctx, c, secret, auditConfig.Webhook.Kubeconfig)
}
// ReconcileSecretWebhookKubeconfig reconciles the secret containing a kubeconfig for webhooks.
func ReconcileSecretWebhookKubeconfig(ctx context.Context, c client.Client, secret *corev1.Secret, kubeconfig []byte) error {
secret.Data = map[string][]byte{SecretWebhookKubeconfigDataKey: kubeconfig}
utilruntime.Must(kubernetesutils.MakeUnique(secret))
return client.IgnoreAlreadyExists(c.Create(ctx, secret))
}
// ReconcileConfigMapAuditPolicy reconciles the ConfigMap containing the audit policy.
func ReconcileConfigMapAuditPolicy(ctx context.Context, c client.Client, configMap *corev1.ConfigMap, auditConfig *AuditConfig) error {
defaultPolicy := &auditv1.Policy{
Rules: []auditv1.PolicyRule{
{Level: auditv1.LevelNone},
},
}
data, err := runtime.Encode(auditCodec, defaultPolicy)
if err != nil {
return err
}
policy := string(data)
if auditConfig != nil && auditConfig.Policy != nil {
policy = *auditConfig.Policy
}
configMap.Data = map[string]string{configMapAuditPolicyDataKey: policy}
utilruntime.Must(kubernetesutils.MakeUnique(configMap))
return client.IgnoreAlreadyExists(c.Create(ctx, configMap))
}
// InjectAuditSettings injects the audit settings into `gardener-apiserver` and `kube-apiserver` deployments.
func InjectAuditSettings(deployment *appsv1.Deployment, configMapAuditPolicy *corev1.ConfigMap, secretWebhookKubeconfig *corev1.Secret, auditConfig *AuditConfig) {
deployment.Spec.Template.Spec.Containers[0].Args = append(deployment.Spec.Template.Spec.Containers[0].Args, fmt.Sprintf("--audit-policy-file=%s/%s", volumeMountPathAuditPolicy, configMapAuditPolicyDataKey))
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(deployment.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: volumeNameAuditPolicy,
MountPath: volumeMountPathAuditPolicy,
})
deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeNameAuditPolicy,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapAuditPolicy.Name,
},
},
},
})
if auditConfig == nil || auditConfig.Webhook == nil {
deployment.Spec.Template.Spec.Containers[0].Args = append(deployment.Spec.Template.Spec.Containers[0].Args,
"--audit-log-path=/tmp/audit/audit.log",
"--audit-log-maxsize=100",
"--audit-log-maxbackup=5",
)
return
}
if len(auditConfig.Webhook.Kubeconfig) > 0 {
deployment.Spec.Template.Spec.Containers[0].Args = append(deployment.Spec.Template.Spec.Containers[0].Args, fmt.Sprintf("--audit-webhook-config-file=%s/%s", volumeMountPathAuditWebhookKubeconfig, SecretWebhookKubeconfigDataKey))
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(deployment.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: volumeNameAuditWebhookKubeconfig,
MountPath: volumeMountPathAuditWebhookKubeconfig,
ReadOnly: true,
})
deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeNameAuditWebhookKubeconfig,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretWebhookKubeconfig.Name,
},
},
})
}
if v := auditConfig.Webhook.BatchMaxSize; v != nil {
deployment.Spec.Template.Spec.Containers[0].Args = append(deployment.Spec.Template.Spec.Containers[0].Args, fmt.Sprintf("--audit-webhook-batch-max-size=%d", *v))
}
if v := auditConfig.Webhook.Version; v != nil {
deployment.Spec.Template.Spec.Containers[0].Args = append(deployment.Spec.Template.Spec.Containers[0].Args, "--audit-webhook-version="+*v)
}
}