k8s.io/apiserver@v0.31.1/plugin/pkg/audit/log/backend.go (about)

     1  /*
     2  Copyright 2017 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 log
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"strings"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	auditinternal "k8s.io/apiserver/pkg/apis/audit"
    27  	"k8s.io/apiserver/pkg/audit"
    28  )
    29  
    30  const (
    31  	// FormatLegacy saves event in 1-line text format.
    32  	FormatLegacy = "legacy"
    33  	// FormatJson saves event in structured json format.
    34  	FormatJson = "json"
    35  
    36  	// PluginName is the name of this plugin, to be used in help and logs.
    37  	PluginName = "log"
    38  )
    39  
    40  // AllowedFormats are the formats known by log backend.
    41  var AllowedFormats = []string{
    42  	FormatLegacy,
    43  	FormatJson,
    44  }
    45  
    46  type backend struct {
    47  	out     io.Writer
    48  	format  string
    49  	encoder runtime.Encoder
    50  }
    51  
    52  var _ audit.Backend = &backend{}
    53  
    54  func NewBackend(out io.Writer, format string, groupVersion schema.GroupVersion) audit.Backend {
    55  	return &backend{
    56  		out:     out,
    57  		format:  format,
    58  		encoder: audit.Codecs.LegacyCodec(groupVersion),
    59  	}
    60  }
    61  
    62  func (b *backend) ProcessEvents(events ...*auditinternal.Event) bool {
    63  	success := true
    64  	for _, ev := range events {
    65  		success = b.logEvent(ev) && success
    66  	}
    67  	return success
    68  }
    69  
    70  func (b *backend) logEvent(ev *auditinternal.Event) bool {
    71  	line := ""
    72  	switch b.format {
    73  	case FormatLegacy:
    74  		line = audit.EventString(ev) + "\n"
    75  	case FormatJson:
    76  		bs, err := runtime.Encode(b.encoder, ev)
    77  		if err != nil {
    78  			audit.HandlePluginError(PluginName, err, ev)
    79  			return false
    80  		}
    81  		line = string(bs[:])
    82  	default:
    83  		audit.HandlePluginError(PluginName, fmt.Errorf("log format %q is not in list of known formats (%s)",
    84  			b.format, strings.Join(AllowedFormats, ",")), ev)
    85  		return false
    86  	}
    87  	if _, err := fmt.Fprint(b.out, line); err != nil {
    88  		audit.HandlePluginError(PluginName, err, ev)
    89  		return false
    90  	}
    91  	return true
    92  }
    93  
    94  func (b *backend) Run(stopCh <-chan struct{}) error {
    95  	return nil
    96  }
    97  
    98  func (b *backend) Shutdown() {
    99  	// Nothing to do here.
   100  }
   101  
   102  func (b *backend) String() string {
   103  	return PluginName
   104  }