Merge pull request #68632 from deads2k/server-06-audit

allow audit policy to be loaded from any byte source
pull/58/head
k8s-ci-robot 2018-10-11 16:58:47 -07:00 committed by GitHub
commit 89c3fd52e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 4 deletions

View File

@ -55,17 +55,26 @@ func LoadPolicyFromFile(filePath string) (*auditinternal.Policy, error) {
return nil, fmt.Errorf("failed to read file path %q: %+v", filePath, err) return nil, fmt.Errorf("failed to read file path %q: %+v", filePath, err)
} }
ret, err := LoadPolicyFromBytes(policyDef)
if err != nil {
return nil, fmt.Errorf("%v: from file %v", err.Error(), filePath)
}
return ret, nil
}
func LoadPolicyFromBytes(policyDef []byte) (*auditinternal.Policy, error) {
policy := &auditinternal.Policy{} policy := &auditinternal.Policy{}
decoder := audit.Codecs.UniversalDecoder(apiGroupVersions...) decoder := audit.Codecs.UniversalDecoder(apiGroupVersions...)
_, gvk, err := decoder.Decode(policyDef, nil, policy) _, gvk, err := decoder.Decode(policyDef, nil, policy)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed decoding file %q: %v", filePath, err) return nil, fmt.Errorf("failed decoding: %v", err)
} }
// Ensure the policy file contained an apiVersion and kind. // Ensure the policy file contained an apiVersion and kind.
if !apiGroupVersionSet[schema.GroupVersion{Group: gvk.Group, Version: gvk.Version}] { if !apiGroupVersionSet[schema.GroupVersion{Group: gvk.Group, Version: gvk.Version}] {
return nil, fmt.Errorf("unknown group version field %v in policy file %s", gvk, filePath) return nil, fmt.Errorf("unknown group version field %v in policy", gvk)
} }
if err := validation.ValidatePolicy(policy); err != nil { if err := validation.ValidatePolicy(policy); err != nil {
@ -74,8 +83,8 @@ func LoadPolicyFromFile(filePath string) (*auditinternal.Policy, error) {
policyCnt := len(policy.Rules) policyCnt := len(policy.Rules)
if policyCnt == 0 { if policyCnt == 0 {
return nil, fmt.Errorf("loaded illegal policy with 0 rules from file %s", filePath) return nil, fmt.Errorf("loaded illegal policy with 0 rules")
} }
glog.V(4).Infof("Loaded %d audit policy rules from file %s", policyCnt, filePath) glog.V(4).Infof("Loaded %d audit policy rules", policyCnt)
return policy, nil return policy, nil
} }