-
Notifications
You must be signed in to change notification settings - Fork 451
/
admission.go
308 lines (260 loc) · 11.3 KB
/
admission.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
// Copyright (c) 2021 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 auditpolicy
import (
"context"
"fmt"
"net/http"
"time"
acadmission "github.com/gardener/gardener/pkg/admissioncontroller/webhooks/admission"
gardencore "github.com/gardener/gardener/pkg/apis/core"
gardencorehelper "github.com/gardener/gardener/pkg/apis/core/helper"
gardencoreinstall "github.com/gardener/gardener/pkg/apis/core/install"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/version"
auditv1alpha1 "github.com/gardener/gardener/third_party/apiserver/pkg/apis/audit/v1alpha1"
auditv1beta1 "github.com/gardener/gardener/third_party/apiserver/pkg/apis/audit/v1beta1"
"github.com/Masterminds/semver"
"github.com/go-logr/logr"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
audit_internal "k8s.io/apiserver/pkg/apis/audit"
auditv1 "k8s.io/apiserver/pkg/apis/audit/v1"
auditvalidation "k8s.io/apiserver/pkg/apis/audit/validation"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
const (
// HandlerName is the name of this admission webhook handler.
HandlerName = "auditpolicy_validator"
// WebhookPath is the HTTP handler path for this admission webhook handler.
WebhookPath = "/webhooks/audit-policies"
auditPolicyConfigMapDataKey = "policy"
)
var (
policyDecoder runtime.Decoder
internalDecoder runtime.Decoder
shootGK = schema.GroupKind{Group: "core.gardener.cloud", Kind: "Shoot"}
configmapGK = schema.GroupKind{Group: "", Kind: "ConfigMap"}
)
// New creates a new handler for validating audit policies.
func New(logger logr.Logger) *handler {
return &handler{
logger: logger,
}
}
func init() {
auditPolicyScheme := runtime.NewScheme()
schemeBuilder := runtime.NewSchemeBuilder(
auditv1alpha1.AddToScheme,
auditv1beta1.AddToScheme,
auditv1.AddToScheme,
audit_internal.AddToScheme,
)
utilruntime.Must(schemeBuilder.AddToScheme(auditPolicyScheme))
policyDecoder = serializer.NewCodecFactory(auditPolicyScheme).UniversalDecoder()
// create decoder that decodes Shoots from all known API versions to the internal version, but does not perform defaulting
gardencoreScheme := runtime.NewScheme()
gardencoreinstall.Install(gardencoreScheme)
codecFactory := serializer.NewCodecFactory(gardencoreScheme)
internalDecoder = versioning.NewCodec(nil, codecFactory.UniversalDeserializer(), runtime.UnsafeObjectConvertor(gardencoreScheme),
gardencoreScheme, gardencoreScheme, nil, runtime.DisabledGroupVersioner, runtime.InternalGroupVersioner, gardencoreScheme.Name())
}
type handler struct {
apiReader client.Reader
cache client.Reader
decoder *admission.Decoder
logger logr.Logger
}
var _ admission.Handler = &handler{}
func (h *handler) InjectAPIReader(reader client.Reader) error {
h.apiReader = reader
return nil
}
func (h *handler) InjectCache(cache cache.Cache) error {
h.cache = cache
return nil
}
func (h *handler) InjectDecoder(d *admission.Decoder) error {
h.decoder = d
return nil
}
// Handle implements the webhook handler for audit log policy validation
func (h *handler) Handle(ctx context.Context, request admission.Request) admission.Response {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
requestGK := schema.GroupKind{Group: request.Kind.Group, Kind: request.Kind.Kind}
defer cancel()
switch requestGK {
case shootGK:
return h.admitShoot(ctx, request)
case configmapGK:
return h.admitConfigMap(ctx, request)
}
return acadmission.Allowed("resource is not core.gardener.cloud/v1beta1.shoot or v1.configmap")
}
func (h *handler) admitShoot(ctx context.Context, request admission.Request) admission.Response {
if request.Operation != admissionv1.Create && request.Operation != admissionv1.Update {
return acadmission.Allowed("operation is not Create or Update")
}
if request.SubResource != "" {
return acadmission.Allowed("subresources are not handled")
}
shoot := &gardencore.Shoot{}
if err := runtime.DecodeInto(internalDecoder, request.Object.Raw, shoot); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
if shoot.DeletionTimestamp != nil {
// don't mutate shoot if it's already marked for deletion, otherwise gardener-apiserver will deny the user's/
// controller's request, because we changed the spec
return acadmission.Allowed("shoot is already marked for deletion")
}
var oldAuditPolicyConfigMapName, newAuditPolicyConfigMapName string
if request.Operation == admissionv1.Update {
oldShoot := &gardencore.Shoot{}
if err := runtime.DecodeInto(internalDecoder, request.OldObject.Raw, oldShoot); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
// skip verification if spec wasn't changed
// this way we make sure, that users/gardenlet can always annotate/label the shoot if the spec doesn't change
if apiequality.Semantic.DeepEqual(oldShoot.Spec, shoot.Spec) {
return acadmission.Allowed("shoot spec was not changed")
}
oldAuditPolicyConfigMapName = gardencorehelper.GetShootAuditPolicyConfigMapName(oldShoot.Spec.Kubernetes.KubeAPIServer)
}
newAuditPolicyConfigMapName = gardencorehelper.GetShootAuditPolicyConfigMapName(shoot.Spec.Kubernetes.KubeAPIServer)
if newAuditPolicyConfigMapName == "" {
return acadmission.Allowed("shoot resource is not specifying any audit policy")
}
// oldAuditPolicyConfigMapName is empty for CREATE shoot requests that specify audit policy reference
if oldAuditPolicyConfigMapName == newAuditPolicyConfigMapName {
return acadmission.Allowed("audit policy configmap was not changed")
}
auditPolicyCm := &corev1.ConfigMap{}
if err := h.apiReader.Get(ctx, kutil.Key(shoot.Namespace, newAuditPolicyConfigMapName), auditPolicyCm); err != nil {
if apierrors.IsNotFound(err) {
return admission.Errored(http.StatusUnprocessableEntity, fmt.Errorf("referenced audit policy does not exist: namespace: %s, name: %s", shoot.Namespace, newAuditPolicyConfigMapName))
}
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("could not retrieve config map: %s", err))
}
auditPolicy, err := getAuditPolicy(auditPolicyCm)
if err != nil {
return admission.Errored(http.StatusUnprocessableEntity, fmt.Errorf("error getting auditlog policy from ConfigMap %s/%s: %w", shoot.Namespace, newAuditPolicyConfigMapName, err))
}
var errCode int32
if request.Operation == admissionv1.Create {
errCode, err = validateAuditPolicySemanticsForKubernetesVersion(auditPolicy, &shoot.Spec.Kubernetes.Version)
} else {
errCode, err = validateAuditPolicySemantics(auditPolicy)
}
if err != nil {
return admission.Errored(errCode, err)
}
return acadmission.Allowed("referenced audit policy is valid")
}
func (h *handler) admitConfigMap(ctx context.Context, request admission.Request) admission.Response {
var (
oldCm = &corev1.ConfigMap{}
cm = &corev1.ConfigMap{}
)
if request.Operation != admissionv1.Update {
return acadmission.Allowed("operation is not update")
}
if err := h.decoder.Decode(request, cm); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
// lookup if configmap is referenced by any shoot in the same namespace
shootList := &gardencorev1beta1.ShootList{}
if err := h.cache.List(ctx, shootList, client.InNamespace(request.Namespace)); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
configMapIsReferenced := false
for _, shoot := range shootList.Items {
if gardencorev1beta1helper.GetShootAuditPolicyConfigMapName(shoot.Spec.Kubernetes.KubeAPIServer) == request.Name {
configMapIsReferenced = true
break
}
}
if !configMapIsReferenced {
return acadmission.Allowed("configmap is not referenced by a Shoot")
}
auditPolicy, err := getAuditPolicy(cm)
if err != nil {
return admission.Errored(http.StatusUnprocessableEntity, err)
}
if err = h.getOldObject(request, oldCm); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
oldAuditPolicy, ok := oldCm.Data[auditPolicyConfigMapDataKey]
if ok && oldAuditPolicy == auditPolicy {
return acadmission.Allowed("audit policy not changed")
}
errCode, err := validateAuditPolicySemantics(auditPolicy)
if err != nil {
return admission.Errored(errCode, err)
}
return acadmission.Allowed("configmap change is valid")
}
func validateAuditPolicySemantics(auditPolicy string) (errCode int32, err error) {
return validateAuditPolicySemanticsForKubernetesVersion(auditPolicy, nil)
}
func validateAuditPolicySemanticsForKubernetesVersion(auditPolicy string, kubernetesVersion *string) (errCode int32, err error) {
auditPolicyObj, schemaVersion, err := policyDecoder.Decode([]byte(auditPolicy), nil, nil)
if err != nil {
return http.StatusUnprocessableEntity, fmt.Errorf("failed to decode the provided audit policy: %w", err)
}
auditPolicyInternal, ok := auditPolicyObj.(*audit_internal.Policy)
if !ok {
return http.StatusInternalServerError, fmt.Errorf("failure to cast to audit Policy type: %v", schemaVersion)
}
errList := auditvalidation.ValidatePolicy(auditPolicyInternal)
if len(errList) != 0 {
return http.StatusUnprocessableEntity, fmt.Errorf("provided invalid audit policy: %v", errList)
}
if kubernetesVersion != nil && schemaVersion.Version != "v1" {
v, err := semver.NewVersion(*kubernetesVersion)
if err != nil {
return http.StatusUnprocessableEntity, fmt.Errorf("unable to build version: %w", err)
}
if !version.ConstraintK8sLess124.Check(v) {
return http.StatusUnprocessableEntity, fmt.Errorf("audit policy with apiVersion '%s' is not supported for kubernetes version >= 1.24.0", schemaVersion.Version)
}
}
return 0, nil
}
func (h *handler) getOldObject(request admission.Request, oldObj runtime.Object) error {
if len(request.OldObject.Raw) != 0 {
return h.decoder.DecodeRaw(request.OldObject, oldObj)
}
return fmt.Errorf("could not find old object")
}
func getAuditPolicy(cm *corev1.ConfigMap) (string, error) {
auditPolicy, ok := cm.Data[auditPolicyConfigMapDataKey]
if !ok {
return "", fmt.Errorf("missing '.data.policy' in audit policy configmap")
}
if len(auditPolicy) == 0 {
return "", fmt.Errorf("empty audit policy. Provide non-empty audit policy")
}
return auditPolicy, nil
}