k8s.io/apiserver@v0.31.1/pkg/apis/audit/fuzzer/fuzzer.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 fuzzer
    18  
    19  import (
    20  	"strings"
    21  
    22  	fuzz "github.com/google/gofuzz"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
    26  	"k8s.io/apiserver/pkg/apis/audit"
    27  )
    28  
    29  // Funcs returns the fuzzer functions for the audit api group.
    30  func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
    31  	return []interface{}{
    32  		func(e *audit.Event, c fuzz.Continue) {
    33  			c.FuzzNoCustom(e)
    34  			switch c.RandBool() {
    35  			case true:
    36  				e.RequestObject = nil
    37  			case false:
    38  				e.RequestObject = &runtime.Unknown{
    39  					TypeMeta:    runtime.TypeMeta{APIVersion: "", Kind: ""},
    40  					Raw:         []byte(`{"apiVersion":"","kind":"Pod","someKey":"someValue"}`),
    41  					ContentType: runtime.ContentTypeJSON,
    42  				}
    43  			}
    44  			switch c.RandBool() {
    45  			case true:
    46  				e.ResponseObject = nil
    47  			case false:
    48  				e.ResponseObject = &runtime.Unknown{
    49  					TypeMeta:    runtime.TypeMeta{APIVersion: "", Kind: ""},
    50  					Raw:         []byte(`{"apiVersion":"","kind":"Pod","someKey":"someValue"}`),
    51  					ContentType: runtime.ContentTypeJSON,
    52  				}
    53  			}
    54  		},
    55  		func(o *audit.ObjectReference, c fuzz.Continue) {
    56  			c.FuzzNoCustom(o)
    57  			switch c.Intn(3) {
    58  			case 0:
    59  				// core api group
    60  				o.APIGroup = ""
    61  				o.APIVersion = "v1"
    62  			case 1:
    63  				// other group
    64  				o.APIGroup = "rbac.authorization.k8s.io"
    65  				o.APIVersion = "v1beta1"
    66  			default:
    67  				// use random value, but without / as it is used as separator
    68  				o.APIGroup = strings.Replace(o.APIGroup, "/", "-", -1)
    69  				o.APIVersion = strings.Replace(o.APIVersion, "/", "-", -1)
    70  			}
    71  		},
    72  	}
    73  }