k8s.io/apiserver@v0.31.1/pkg/admission/audit.go (about)

     1  /*
     2  Copyright 2018 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 admission
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"k8s.io/apiserver/pkg/audit"
    24  )
    25  
    26  // auditHandler logs annotations set by other admission handlers
    27  type auditHandler struct {
    28  	Interface
    29  }
    30  
    31  var _ Interface = &auditHandler{}
    32  var _ MutationInterface = &auditHandler{}
    33  var _ ValidationInterface = &auditHandler{}
    34  
    35  // WithAudit is a decorator for a admission phase. It saves annotations
    36  // of attribute into the audit event. Attributes passed to the Admit and
    37  // Validate function must be instance of privateAnnotationsGetter or
    38  // AnnotationsGetter, otherwise an error is returned.
    39  func WithAudit(i Interface) Interface {
    40  	if i == nil {
    41  		return i
    42  	}
    43  	return &auditHandler{Interface: i}
    44  }
    45  
    46  func (handler *auditHandler) Admit(ctx context.Context, a Attributes, o ObjectInterfaces) error {
    47  	if !handler.Interface.Handles(a.GetOperation()) {
    48  		return nil
    49  	}
    50  	if err := ensureAnnotationGetter(a); err != nil {
    51  		return err
    52  	}
    53  	var err error
    54  	if mutator, ok := handler.Interface.(MutationInterface); ok {
    55  		err = mutator.Admit(ctx, a, o)
    56  		handler.logAnnotations(ctx, a)
    57  	}
    58  	return err
    59  }
    60  
    61  func (handler *auditHandler) Validate(ctx context.Context, a Attributes, o ObjectInterfaces) error {
    62  	if !handler.Interface.Handles(a.GetOperation()) {
    63  		return nil
    64  	}
    65  	if err := ensureAnnotationGetter(a); err != nil {
    66  		return err
    67  	}
    68  	var err error
    69  	if validator, ok := handler.Interface.(ValidationInterface); ok {
    70  		err = validator.Validate(ctx, a, o)
    71  		handler.logAnnotations(ctx, a)
    72  	}
    73  	return err
    74  }
    75  
    76  func ensureAnnotationGetter(a Attributes) error {
    77  	_, okPrivate := a.(privateAnnotationsGetter)
    78  	_, okPublic := a.(AnnotationsGetter)
    79  	if okPrivate || okPublic {
    80  		return nil
    81  	}
    82  	return fmt.Errorf("attributes must be an instance of privateAnnotationsGetter or AnnotationsGetter")
    83  }
    84  
    85  func (handler *auditHandler) logAnnotations(ctx context.Context, a Attributes) {
    86  	ae := audit.AuditEventFrom(ctx)
    87  	if ae == nil {
    88  		return
    89  	}
    90  
    91  	var annotations map[string]string
    92  	switch a := a.(type) {
    93  	case privateAnnotationsGetter:
    94  		annotations = a.getAnnotations(ae.Level)
    95  	case AnnotationsGetter:
    96  		annotations = a.GetAnnotations(ae.Level)
    97  	default:
    98  		// this will never happen, because we have already checked it in ensureAnnotationGetter
    99  	}
   100  
   101  	audit.AddAuditAnnotationsMap(ctx, annotations)
   102  }