Skip to content

Commit

Permalink
apiserver: evaluate OmitManagedFields
Browse files Browse the repository at this point in the history
  • Loading branch information
tkashem committed Oct 12, 2021
1 parent 9ed4bc9 commit 7ea7c20
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
4 changes: 4 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/audit/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type AuditContext struct {
type RequestAuditConfig struct {
// OmitStages is the stages that need to be omitted from being audited.
OmitStages []audit.Stage

// OmitManagedFields indicates whether to omit the managed fields of the request
// and response bodies from being written to the API audit log.
OmitManagedFields bool
}

// RequestAuditConfigWithLevel includes Level at which the request is being audited.
Expand Down
18 changes: 16 additions & 2 deletions staging/src/k8s.io/apiserver/pkg/audit/policy/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func (p *policyRuleEvaluator) EvaluatePolicyRule(attrs authorizer.Attributes) au
return auditinternal.RequestAuditConfigWithLevel{
Level: rule.Level,
RequestAuditConfig: auditinternal.RequestAuditConfig{
OmitStages: rule.OmitStages,
OmitStages: rule.OmitStages,
OmitManagedFields: isOmitManagedFields(&rule, p.OmitManagedFields),
},
}
}
Expand All @@ -76,11 +77,24 @@ func (p *policyRuleEvaluator) EvaluatePolicyRule(attrs authorizer.Attributes) au
return auditinternal.RequestAuditConfigWithLevel{
Level: DefaultAuditLevel,
RequestAuditConfig: auditinternal.RequestAuditConfig{
OmitStages: p.OmitStages,
OmitStages: p.OmitStages,
OmitManagedFields: p.OmitManagedFields,
},
}
}

// isOmitManagedFields returns whether to omit managed fields from the request
// and response bodies from being written to the API audit log.
// If a user specifies OmitManagedFields inside a policy rule, that overrides
// the global policy default in Policy.OmitManagedFields.
func isOmitManagedFields(policyRule *audit.PolicyRule, policyDefault bool) bool {
if policyRule.OmitManagedFields == nil {
return policyDefault
}

return *policyRule.OmitManagedFields
}

// Check whether the rule matches the request attrs.
func ruleMatches(r *audit.PolicyRule, attrs authorizer.Attributes) bool {
user := attrs.GetUser()
Expand Down
85 changes: 85 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/audit/policy/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,88 @@ func TestUnionStages(t *testing.T) {
}
}
}

func TestOmitManagedFields(t *testing.T) {
// this authorizer.Attributes should match all policy rules
// specified in this test.
attributes := &authorizer.AttributesRecord{
Verb: "get",
}
matchingPolicyRule := audit.PolicyRule{
Level: audit.LevelRequestResponse,
Verbs: []string{
attributes.GetVerb(),
},
}

boolPtr := func(v bool) *bool {
return &v
}

tests := []struct {
name string
policy func() audit.Policy
want bool
}{
{
name: "global policy default is false, rule does not override",
policy: func() audit.Policy {
return audit.Policy{
OmitManagedFields: false,
Rules: []audit.PolicyRule{
*matchingPolicyRule.DeepCopy(),
},
}
},
},
{
name: "global policy default is true, rule does not override",
policy: func() audit.Policy {
return audit.Policy{
OmitManagedFields: true,
Rules: []audit.PolicyRule{
*matchingPolicyRule.DeepCopy(),
},
}
},
want: true,
},
{
name: "global policy default is true, rule overrides to false",
policy: func() audit.Policy {
rule := matchingPolicyRule.DeepCopy()
rule.OmitManagedFields = boolPtr(false)
return audit.Policy{
OmitManagedFields: true,
Rules: []audit.PolicyRule{*rule},
}
},
want: false,
},
{
name: "global policy default is false, rule overrides to true",
policy: func() audit.Policy {
rule := matchingPolicyRule.DeepCopy()
rule.OmitManagedFields = boolPtr(true)
return audit.Policy{
OmitManagedFields: false,
Rules: []audit.PolicyRule{*rule},
}
},
want: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
evaluator := &policyRuleEvaluator{
Policy: test.policy(),
}

got := evaluator.EvaluatePolicyRule(attributes)
if test.want != got.OmitManagedFields {
t.Errorf("Expected OmitManagedFields to match, want: %t, got: %t", test.want, got.OmitManagedFields)
}
})
}
}

0 comments on commit 7ea7c20

Please sign in to comment.