Debian Codegraph @

pkg/admission/plugin/policy/validating/dispatcher.go

raw
1 /*
2 Copyright 2022 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package validating
18
19 import (
20 "context"
21 "errors"
22 "fmt"
23 "strings"
24
25 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
26 v1 "k8s.io/api/core/v1"
27 k8serrors "k8s.io/apimachinery/pkg/api/errors"
28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29 "k8s.io/apimachinery/pkg/runtime"
30 "k8s.io/apimachinery/pkg/runtime/schema"
31 utiljson "k8s.io/apimachinery/pkg/util/json"
32 "k8s.io/apiserver/pkg/admission"
33 admissionauthorizer "k8s.io/apiserver/pkg/admission/plugin/authorizer"
34 "k8s.io/apiserver/pkg/admission/plugin/policy/generic"
35 celmetrics "k8s.io/apiserver/pkg/admission/plugin/policy/validating/metrics"
36 celconfig "k8s.io/apiserver/pkg/apis/cel"
37 "k8s.io/apiserver/pkg/authorization/authorizer"
38 "k8s.io/apiserver/pkg/warning"
39 "k8s.io/klog/v2"
40 )
41
42 type dispatcher struct {
43 matcher generic.PolicyMatcher
44 authz authorizer.Authorizer
45 }
46
47 var _ generic.Dispatcher[PolicyHook] = &dispatcher{}
48
49 func NewDispatcher(
50 authorizer authorizer.Authorizer,
51 matcher generic.PolicyMatcher,
52 ) generic.Dispatcher[PolicyHook] {
53 return &dispatcher{
54 matcher: matcher,
55 authz: authorizer,
56 }
57 }
58
59 // contains the cel PolicyDecisions along with the ValidatingAdmissionPolicy and ValidatingAdmissionPolicyBinding
60 // that determined the decision
61 type policyDecisionWithMetadata struct {
62 PolicyDecision
63 Definition *admissionregistrationv1.ValidatingAdmissionPolicy
64 Binding *admissionregistrationv1.ValidatingAdmissionPolicyBinding
65 }
66
67 func (c *dispatcher) Start(ctx context.Context) error {
68 return nil
69 }
70
71 // Dispatch implements generic.Dispatcher.
72 func (c *dispatcher) Dispatch(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces, hooks []PolicyHook) error {
73
74 var deniedDecisions []policyDecisionWithMetadata
75
76 addConfigError := func(err error, definition *admissionregistrationv1.ValidatingAdmissionPolicy, binding *admissionregistrationv1.ValidatingAdmissionPolicyBinding) {
77 // we always default the FailurePolicy if it is unset and validate it in API level
78 var policy admissionregistrationv1.FailurePolicyType
79 if definition.Spec.FailurePolicy == nil {
80 policy = admissionregistrationv1.Fail
81 } else {
82 policy = *definition.Spec.FailurePolicy
83 }
84
85 // apply FailurePolicy specified in ValidatingAdmissionPolicy, the default would be Fail
86 switch policy {
87 case admissionregistrationv1.Ignore:
88 // TODO: add metrics for ignored error here
89 return
90 case admissionregistrationv1.Fail:
91 var message string
92 if binding == nil {
93 message = fmt.Errorf("failed to configure policy: %w", err).Error()
94 } else {
95 message = fmt.Errorf("failed to configure binding: %w", err).Error()
96 }
97 deniedDecisions = append(deniedDecisions, policyDecisionWithMetadata{
98 PolicyDecision: PolicyDecision{
99 Action: ActionDeny,
100 Message: message,
101 },
102 Definition: definition,
103 Binding: binding,
104 })
105 default:
106 deniedDecisions = append(deniedDecisions, policyDecisionWithMetadata{
107 PolicyDecision: PolicyDecision{
108 Action: ActionDeny,
109 Message: fmt.Errorf("unrecognized failure policy: '%v'", policy).Error(),
110 },
111 Definition: definition,
112 Binding: binding,
113 })
114 }
115 }
116
117 authz := admissionauthorizer.NewCachingAuthorizer(c.authz)
118
119 for _, hook := range hooks {
120 // versionedAttributes will be set to non-nil inside of the loop, but
121 // is scoped outside of the param loop so we only convert once. We defer
122 // conversion so that it is only performed when we know a policy matches,
123 // saving the cost of converting non-matching requests.
124 var versionedAttr *admission.VersionedAttributes
125
126 definition := hook.Policy
127 matches, matchResource, matchKind, err := c.matcher.DefinitionMatches(a, o, NewValidatingAdmissionPolicyAccessor(definition))
128 if err != nil {
129 // Configuration error.
130 addConfigError(err, definition, nil)
131 continue
132 }
133 if !matches {
134 // Policy definition does not match request
135 continue
136 } else if hook.ConfigurationError != nil {
137 // Configuration error.
138 addConfigError(hook.ConfigurationError, definition, nil)
139 continue
140 }
141
142 auditAnnotationCollector := newAuditAnnotationCollector()
143 for _, binding := range hook.Bindings {
144 // If the key is inside dependentBindings, there is guaranteed to
145 // be a bindingInfo for it
146 matches, err := c.matcher.BindingMatches(a, o, NewValidatingAdmissionPolicyBindingAccessor(binding))
147 if err != nil {
148 // Configuration error.
149 addConfigError(err, definition, binding)
150 continue
151 }
152 if !matches {
153 continue
154 }
155
156 params, err := generic.CollectParams(
157 hook.Policy.Spec.ParamKind,
158 hook.ParamInformer,
159 hook.ParamScope,
160 binding.Spec.ParamRef,
161 a.GetNamespace(),
162 )
163
164 if err != nil {
165 addConfigError(err, definition, binding)
166 continue
167 } else if versionedAttr == nil && len(params) > 0 {
168 // As optimization versionedAttr creation is deferred until
169 // first use. Since > 0 params, we will validate
170 va, err := admission.NewVersionedAttributes(a, matchKind, o)
171 if err != nil {
172 wrappedErr := fmt.Errorf("failed to convert object version: %w", err)
173 addConfigError(wrappedErr, definition, binding)
174 continue
175 }
176 versionedAttr = va
177 }
178
179 var validationResults []ValidateResult
180 var namespace *v1.Namespace
181 namespaceName := a.GetNamespace()
182
183 // Special case, the namespace object has the namespace of itself (maybe a bug).
184 // unset it if the incoming object is a namespace
185 if gvk := a.GetKind(); gvk.Kind == "Namespace" && gvk.Version == "v1" && gvk.Group == "" {
186 namespaceName = ""
187 }
188
189 // if it is cluster scoped, namespaceName will be empty
190 // Otherwise, get the Namespace resource.
191 if namespaceName != "" {
192 namespace, err = c.matcher.GetNamespace(namespaceName)
193 if err != nil {
194 return err
195 }
196 }
197
198 for _, param := range params {
199 var p runtime.Object = param
200 if p != nil && p.GetObjectKind().GroupVersionKind().Empty() {
201 // Make sure param has TypeMeta populated
202 // This is a simple hack to make sure typeMeta is
203 // available to CEL without making copies of objects, etc.
204 p = &wrappedParam{
205 TypeMeta: metav1.TypeMeta{
206 APIVersion: definition.Spec.ParamKind.APIVersion,
207 Kind: definition.Spec.ParamKind.Kind,
208 },
209 nested: param,
210 }
211 }
212
213 validationResults = append(validationResults,
214 hook.Evaluator.Validate(
215 ctx,
216 matchResource,
217 versionedAttr,
218 p,
219 namespace,
220 celconfig.RuntimeCELCostBudget,
221 authz,
222 ),
223 )
224 }
225
226 for _, validationResult := range validationResults {
227 for i, decision := range validationResult.Decisions {
228 switch decision.Action {
229 case ActionAdmit:
230 if decision.Evaluation == EvalError {
231 celmetrics.Metrics.ObserveAdmission(ctx, decision.Elapsed, definition.Name, binding.Name, ErrorType(&decision))
232 }
233 case ActionDeny:
234 for _, action := range binding.Spec.ValidationActions {
235 switch action {
236 case admissionregistrationv1.Deny:
237 deniedDecisions = append(deniedDecisions, policyDecisionWithMetadata{
238 Definition: definition,
239 Binding: binding,
240 PolicyDecision: decision,
241 })
242 celmetrics.Metrics.ObserveRejection(ctx, decision.Elapsed, definition.Name, binding.Name, ErrorType(&decision))
243 case admissionregistrationv1.Audit:
244 publishValidationFailureAnnotation(binding, i, decision, versionedAttr)
245 celmetrics.Metrics.ObserveAudit(ctx, decision.Elapsed, definition.Name, binding.Name, ErrorType(&decision))
246 case admissionregistrationv1.Warn:
247 warning.AddWarning(ctx, "", fmt.Sprintf("Validation failed for ValidatingAdmissionPolicy '%s' with binding '%s': %s", definition.Name, binding.Name, decision.Message))
248 celmetrics.Metrics.ObserveWarn(ctx, decision.Elapsed, definition.Name, binding.Name, ErrorType(&decision))
249 }
250 }
251 default:
252 return fmt.Errorf("unrecognized evaluation decision '%s' for ValidatingAdmissionPolicyBinding '%s' with ValidatingAdmissionPolicy '%s'",
253 decision.Action, binding.Name, definition.Name)
254 }
255 }
256
257 for _, auditAnnotation := range validationResult.AuditAnnotations {
258 switch auditAnnotation.Action {
259 case AuditAnnotationActionPublish:
260 value := auditAnnotation.Value
261 if len(auditAnnotation.Value) > maxAuditAnnotationValueLength {
262 value = value[:maxAuditAnnotationValueLength]
263 }
264 auditAnnotationCollector.add(auditAnnotation.Key, value)
265 case AuditAnnotationActionError:
266 // When failurePolicy=fail, audit annotation errors result in deny
267 d := policyDecisionWithMetadata{
268 Definition: definition,
269 Binding: binding,
270 PolicyDecision: PolicyDecision{
271 Action: ActionDeny,
272 Evaluation: EvalError,
273 Message: auditAnnotation.Error,
274 Elapsed: auditAnnotation.Elapsed,
275 },
276 }
277 deniedDecisions = append(deniedDecisions, d)
278 celmetrics.Metrics.ObserveRejection(ctx, auditAnnotation.Elapsed, definition.Name, binding.Name, ErrorType(&d.PolicyDecision))
279 case AuditAnnotationActionExclude: // skip it
280 default:
281 return fmt.Errorf("unsupported AuditAnnotation Action: %s", auditAnnotation.Action)
282 }
283 }
284 }
285 }
286 auditAnnotationCollector.publish(definition.Name, a)
287 }
288
289 if len(deniedDecisions) > 0 {
290 // TODO: refactor admission.NewForbidden so the name extraction is reusable but the code/reason is customizable
291 var message string
292 deniedDecision := deniedDecisions[0]
293 if deniedDecision.Binding != nil {
294 message = fmt.Sprintf("ValidatingAdmissionPolicy '%s' with binding '%s' denied request: %s", deniedDecision.Definition.Name, deniedDecision.Binding.Name, deniedDecision.Message)
295 } else {
296 message = fmt.Sprintf("ValidatingAdmissionPolicy '%s' denied request: %s", deniedDecision.Definition.Name, deniedDecision.Message)
297 }
298 err := admission.NewForbidden(a, errors.New(message)).(*k8serrors.StatusError)
299 reason := deniedDecision.Reason
300 if len(reason) == 0 {
301 reason = metav1.StatusReasonInvalid
302 }
303 err.ErrStatus.Reason = reason
304 err.ErrStatus.Code = reasonToCode(reason)
305 err.ErrStatus.Details.Causes = append(err.ErrStatus.Details.Causes, metav1.StatusCause{Message: message})
306 return err
307 }
308 return nil
309 }
310
311 func publishValidationFailureAnnotation(binding *admissionregistrationv1.ValidatingAdmissionPolicyBinding, expressionIndex int, decision PolicyDecision, attributes admission.Attributes) {
312 key := "validation.policy.admission.k8s.io/validation_failure"
313 // Marshal to a list of failures since, in the future, we may need to support multiple failures
314 valueJSON, err := utiljson.Marshal([]ValidationFailureValue{{
315 ExpressionIndex: expressionIndex,
316 Message: decision.Message,
317 ValidationActions: binding.Spec.ValidationActions,
318 Binding: binding.Name,
319 Policy: binding.Spec.PolicyName,
320 }})
321 if err != nil {
322 klog.Warningf("Failed to set admission audit annotation %s for ValidatingAdmissionPolicy %s and ValidatingAdmissionPolicyBinding %s: %v", key, binding.Spec.PolicyName, binding.Name, err)
323 }
324 value := string(valueJSON)
325 if err := attributes.AddAnnotation(key, value); err != nil {
326 klog.Warningf("Failed to set admission audit annotation %s to %s for ValidatingAdmissionPolicy %s and ValidatingAdmissionPolicyBinding %s: %v", key, value, binding.Spec.PolicyName, binding.Name, err)
327 }
328 }
329
330 const maxAuditAnnotationValueLength = 10 * 1024
331
332 // validationFailureValue defines the JSON format of a "validation.policy.admission.k8s.io/validation_failure" audit
333 // annotation value.
334 type ValidationFailureValue struct {
335 Message string `json:"message"`
336 Policy string `json:"policy"`
337 Binding string `json:"binding"`
338 ExpressionIndex int `json:"expressionIndex"`
339 ValidationActions []admissionregistrationv1.ValidationAction `json:"validationActions"`
340 }
341
342 type auditAnnotationCollector struct {
343 annotations map[string][]string
344 }
345
346 func newAuditAnnotationCollector() auditAnnotationCollector {
347 return auditAnnotationCollector{annotations: map[string][]string{}}
348 }
349
350 func (a auditAnnotationCollector) add(key, value string) {
351 // If multiple bindings produces the exact same key and value for an audit annotation,
352 // ignore the duplicates.
353 for _, v := range a.annotations[key] {
354 if v == value {
355 return
356 }
357 }
358 a.annotations[key] = append(a.annotations[key], value)
359 }
360
361 func (a auditAnnotationCollector) publish(policyName string, attributes admission.Attributes) {
362 for key, bindingAnnotations := range a.annotations {
363 var value string
364 if len(bindingAnnotations) == 1 {
365 value = bindingAnnotations[0]
366 } else {
367 // Multiple distinct values can exist when binding params are used in the valueExpression of an auditAnnotation.
368 // When this happens, the values are concatenated into a comma-separated list.
369 value = strings.Join(bindingAnnotations, ", ")
370 }
371 if err := attributes.AddAnnotation(policyName+"/"+key, value); err != nil {
372 klog.Warningf("Failed to set admission audit annotation %s to %s for ValidatingAdmissionPolicy %s: %v", key, value, policyName, err)
373 }
374 }
375 }
376
377 // A workaround to fact that native types do not have TypeMeta populated, which
378 // is needed for CEL expressions to be able to access the value.
379 type wrappedParam struct {
380 metav1.TypeMeta
381 nested runtime.Object
382 }
383
384 func (w *wrappedParam) MarshalJSON() ([]byte, error) {
385 return nil, errors.New("MarshalJSON unimplemented for wrappedParam")
386 }
387
388 func (w *wrappedParam) UnmarshalJSON(data []byte) error {
389 return errors.New("UnmarshalJSON unimplemented for wrappedParam")
390 }
391
392 func (w *wrappedParam) ToUnstructured() interface{} {
393 res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(w.nested)
394
395 if err != nil {
396 return nil
397 }
398
399 metaRes, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&w.TypeMeta)
400 if err != nil {
401 return nil
402 }
403
404 for k, v := range metaRes {
405 res[k] = v
406 }
407
408 return res
409 }
410
411 func (w *wrappedParam) DeepCopyObject() runtime.Object {
412 return &wrappedParam{
413 TypeMeta: w.TypeMeta,
414 nested: w.nested.DeepCopyObject(),
415 }
416 }
417
418 func (w *wrappedParam) GetObjectKind() schema.ObjectKind {
419 return w
420 }