Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow audit policy to be loaded from any byte source #68632

Merged
merged 1 commit into from
Oct 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions staging/src/k8s.io/apiserver/pkg/audit/policy/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,26 @@ func LoadPolicyFromFile(filePath string) (*auditinternal.Policy, error) {
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{}
decoder := audit.Codecs.UniversalDecoder(apiGroupVersions...)

_, gvk, err := decoder.Decode(policyDef, nil, policy)
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.
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 {
Expand All @@ -74,8 +83,8 @@ func LoadPolicyFromFile(filePath string) (*auditinternal.Policy, error) {

policyCnt := len(policy.Rules)
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
}