k8s.io/apiserver@v0.31.1/pkg/audit/policy/reader.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 policy 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/runtime/schema" 25 "k8s.io/apimachinery/pkg/runtime/serializer" 26 auditinternal "k8s.io/apiserver/pkg/apis/audit" 27 auditv1 "k8s.io/apiserver/pkg/apis/audit/v1" 28 "k8s.io/apiserver/pkg/apis/audit/validation" 29 "k8s.io/apiserver/pkg/audit" 30 "k8s.io/klog/v2" 31 ) 32 33 var ( 34 apiGroupVersions = []schema.GroupVersion{ 35 auditv1.SchemeGroupVersion, 36 } 37 apiGroupVersionSet = map[schema.GroupVersion]bool{} 38 ) 39 40 func init() { 41 for _, gv := range apiGroupVersions { 42 apiGroupVersionSet[gv] = true 43 } 44 } 45 46 func LoadPolicyFromFile(filePath string) (*auditinternal.Policy, error) { 47 if filePath == "" { 48 return nil, fmt.Errorf("file path not specified") 49 } 50 policyDef, err := ioutil.ReadFile(filePath) 51 if err != nil { 52 return nil, fmt.Errorf("failed to read file path %q: %+v", filePath, err) 53 } 54 55 ret, err := LoadPolicyFromBytes(policyDef) 56 if err != nil { 57 return nil, fmt.Errorf("%v: from file %v", err.Error(), filePath) 58 } 59 60 return ret, nil 61 } 62 63 func LoadPolicyFromBytes(policyDef []byte) (*auditinternal.Policy, error) { 64 policy := &auditinternal.Policy{} 65 strictDecoder := serializer.NewCodecFactory(audit.Scheme, serializer.EnableStrict).UniversalDecoder() 66 67 // Try strict decoding first. 68 _, gvk, err := strictDecoder.Decode(policyDef, nil, policy) 69 if err != nil { 70 if !runtime.IsStrictDecodingError(err) { 71 return nil, fmt.Errorf("failed decoding: %w", err) 72 } 73 var ( 74 lenientDecoder = audit.Codecs.UniversalDecoder(apiGroupVersions...) 75 lenientErr error 76 ) 77 _, gvk, lenientErr = lenientDecoder.Decode(policyDef, nil, policy) 78 if lenientErr != nil { 79 return nil, fmt.Errorf("failed lenient decoding: %w", lenientErr) 80 } 81 klog.Warningf("Audit policy contains errors, falling back to lenient decoding: %v", err) 82 } 83 84 // Ensure the policy file contained an apiVersion and kind. 85 gv := schema.GroupVersion{Group: gvk.Group, Version: gvk.Version} 86 if !apiGroupVersionSet[gv] { 87 return nil, fmt.Errorf("unknown group version field %v in policy", gvk) 88 } 89 90 if err := validation.ValidatePolicy(policy); err != nil { 91 return nil, err.ToAggregate() 92 } 93 94 policyCnt := len(policy.Rules) 95 if policyCnt == 0 { 96 return nil, fmt.Errorf("loaded illegal policy with 0 rules") 97 } 98 99 klog.V(4).InfoS("Load audit policy rules success", "policyCnt", policyCnt) 100 return policy, nil 101 }