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

A policy with 0 rules should return an error #51782

Merged
merged 1 commit into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion staging/src/k8s.io/apiserver/pkg/audit/policy/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func LoadPolicyFromFile(filePath string) (*auditinternal.Policy, error) {
return nil, err.ToAggregate()
}

glog.V(4).Infof("Loaded %d audit policy rules from file %s\n", len(policy.Rules), filePath)
policyCnt := len(policy.Rules)
if policyCnt == 0 {
return nil, fmt.Errorf("loaded illegal policy with 0 rules from file %s", filePath)
}
glog.V(4).Infof("Loaded %d audit policy rules from file %s", policyCnt, filePath)
return policy, nil
}
58 changes: 41 additions & 17 deletions staging/src/k8s.io/apiserver/pkg/audit/policy/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

const policyDefV1alpha1 = `
apiVersion: audit.k8s.io/v1beta1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disregard. This should be alpha.

apiVersion: audit.k8s.io/v1alpha1
kind: Policy
rules:
- level: None
Expand Down Expand Up @@ -91,16 +91,11 @@ var expectedPolicy = &audit.Policy{
}

func TestParserV1alpha1(t *testing.T) {
// Create a policy file.
f, err := ioutil.TempFile("", "policy.yaml")
require.NoError(t, err)
defer os.Remove(f.Name())

_, err = f.WriteString(policyDefV1alpha1)
f, err := writePolicy(policyDefV1alpha1, t)
require.NoError(t, err)
require.NoError(t, f.Close())
defer os.Remove(f)

policy, err := LoadPolicyFromFile(f.Name())
policy, err := LoadPolicyFromFile(f)
require.NoError(t, err)

assert.Len(t, policy.Rules, 3) // Sanity check.
Expand All @@ -110,20 +105,49 @@ func TestParserV1alpha1(t *testing.T) {
}

func TestParserV1beta1(t *testing.T) {
// Create a policy file.
f, err := ioutil.TempFile("", "policy.yaml")
f, err := writePolicy(policyDefV1beta1, t)
require.NoError(t, err)
defer os.Remove(f.Name())
defer os.Remove(f)

_, err = f.WriteString(policyDefV1beta1)
require.NoError(t, err)
require.NoError(t, f.Close())

policy, err := LoadPolicyFromFile(f.Name())
policy, err := LoadPolicyFromFile(f)
require.NoError(t, err)

assert.Len(t, policy.Rules, 3) // Sanity check.
if !reflect.DeepEqual(policy, expectedPolicy) {
t.Errorf("Unexpected policy! Diff:\n%s", diff.ObjectDiff(policy, expectedPolicy))
}
}

func TestPolicyCntCheck(t *testing.T) {
//a set of testCases

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment adds no new information => not needed

var testCases = []struct {
caseName, policy string
}{
{
"policyWithNoRule",
`apiVersion: audit.k8s.io/v1beta1
kind: Policy`,
},
{"emptyPolicyFile", ""},
}

for _, tc := range testCases {
f, err := writePolicy(tc.policy, t)
require.NoError(t, err)
defer os.Remove(f)

_, err = LoadPolicyFromFile(f)
assert.Errorf(t, err, "loaded illegal policy with 0 rules from testCase %s", tc.caseName)
}
}

func writePolicy(policy string, t *testing.T) (string, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: usually *testing.T comes as a first argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for your careful instruction! I learned a lot.^_^

f, err := ioutil.TempFile("", "policy.yaml")
require.NoError(t, err)

_, err = f.WriteString(policy)
require.NoError(t, err)
require.NoError(t, f.Close())

return f.Name(), nil
}