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

[advanced audit]add a policy wide omitStage #54634

Merged
merged 2 commits into from
Nov 21, 2017

Conversation

CaoShuFeng
Copy link
Contributor

@CaoShuFeng CaoShuFeng commented Oct 26, 2017

Related to: #54551
For example:

  1. only log panic events
apiVersion: audit.k8s.io/v1beta1
kind: Policy
omitStages:
  - "RequestReceived"
  - "ResponseStarted"
  - "ResponseComplete"
rules:
  - level: Request
  1. only log events inRequestReceived stage:
apiVersion: audit.k8s.io/v1beta1
kind: Policy
omitStages:
  - "ResponseStarted"
  - "ResponseComplete"
  - "Panic"
rules:
  - level: Request

Release note:

support a policy wide omitStage for advanced audit

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 26, 2017
@CaoShuFeng
Copy link
Contributor Author

/assign @crassirostris
/assign @sttts
cc @hzxuzhonghu

@crassirostris
Copy link

Wow, cool! I'll take a look later this week

@@ -213,3 +228,19 @@ func TestChecker(t *testing.T) {
test("subresource", audit.LevelRequest, nil, "getPodLogs", "getPods")
test("subresource", audit.LevelRequest, nil, "getPods", "getPodLogs")
}

func stageEqual(s1 []audit.Stage, s2 []audit.Stage) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

The following call returns true:

stageEqual([]audit.Stage{"foo", "bar", "baz"}, []audit.Stage{"foo", "foo", "foo"})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

return &policyChecker{*policy}
}

func unionStages(s1 []audit.Stage, s2 []audit.Stage) []audit.Stage {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add fast path?

if len(s1) == 0 {
    return s2
}
if len(s2) == 0 {
    return s1
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be helpful if we have duplicated elements in s1 or s2.

For example:
s1 = nil
s2 = []audit.Stage{"foo", "foo", "bar"}

return &policyChecker{*policy}
}

func unionStages(s1 []audit.Stage, s2 []audit.Stage) []audit.Stage {
m := make(map[audit.Stage]struct{})
Copy link
Member

Choose a reason for hiding this comment

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

why not reuse "k8s.io/apimachinery/pkg/util/sets"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, but "k8s.io/apimachinery/pkg/util/sets" only supports int64, string, int, string, etc.
It doesn't work well with our audit.Stage.

Do I miss something?
If we can, I will be happy to use it.

Copy link
Member

Choose a reason for hiding this comment

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

How about type convert ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

package main

import (
	"fmt"
)

type Stage string

func main() {
	a := Stage("aaaaa")
	b := string(a)
	fmt.Printf(b)
	/*
	s := make([]Stage, 3)
	ss := []string(s)
	fmt.Print(s)
	*/
}

Doesn't work as expected.

Copy link
Member

Choose a reason for hiding this comment

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

can not convert slice, one element by one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't want to convert them one by one.
So I wrote this function.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 27, 2017
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 27, 2017
@CaoShuFeng
Copy link
Contributor Author

/test pull-kubernetes-unit

@@ -156,6 +156,11 @@ type Policy struct {
// The default audit level is None, but can be overridden by a catch-all rule at the end of the list.
// PolicyRules are strictly ordered.
Rules []PolicyRule `json:"rules" protobuf:"bytes,2,rep,name=rules"`

// OmitStages specify events generated in which stages will not be emitted to backend.
Copy link
Contributor

Choose a reason for hiding this comment

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

Having difficulties to parse this sentence (same for the one in the rules). Isn't this simpler:

OmitStages are a list of stages for which no events are created. Note that this can also
be specified per rule in which case the union of stages is skipped.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

result := make([]audit.Stage, len(m))
i := 0
for key := range m {
result[i] = key
Copy link
Contributor

Choose a reason for hiding this comment

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

append, no index i, and make([]audit.Stage, 0, len(m))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 30, 2017
@@ -153,6 +153,10 @@ type Policy struct {
// The default audit level is None, but can be overridden by a catch-all rule at the end of the list.
// PolicyRules are strictly ordered.
Rules []PolicyRule

// OmitStages are a list of stages for which no events are created. Note that this can also
// be specified per rule in which case the union of stages is skipped.

Choose a reason for hiding this comment

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

are a list

is a list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -131,4 +134,20 @@ func TestValidatePolicy(t *testing.T) {
t.Errorf("[%d] Expected policy %#v to be invalid!", i, policy)
}
}

// test invalid omitStages in policy

Choose a reason for hiding this comment

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

Why not just add to the list of errorCases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

return &policyChecker{*policy}
}

func unionStages(s1 []audit.Stage, s2 []audit.Stage) []audit.Stage {
m := make(map[audit.Stage]struct{})

Choose a reason for hiding this comment

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

Why not map[T]bool? I think bool looks little bit cleaner than struct{}{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is copied from here:

return &policyChecker{*policy}
}

func unionStages(s1 []audit.Stage, s2 []audit.Stage) []audit.Stage {
m := make(map[audit.Stage]struct{})
for _, s := range s1 {

Choose a reason for hiding this comment

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

for _, s := range append(s1, s2...)?

Or you can make one parameter of type ...[]audit.Stage, still looks cleaner IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for _, s := range append(s1, s2...)?

append will cause an extra memory copy, I guess.

Or you can make one parameter of type ...[]audit.Stage, still looks cleaner IMO

Done.

assert.True(t, stageEqual(expOmitStages, actualOmitStages), "request:%s rules:%s, expected stages: %v, actual stages: %v",
req, strings.Join(ruleNames, ","), expOmitStages, actualOmitStages)

// test policy.OmitStages

Choose a reason for hiding this comment

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

Please don't do this (hacking around existing code structure and adding a comment)

Change the test signature and move test cases out of the method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

return &policyChecker{*policy}
}

func unionStages(s1 []audit.Stage, s2 []audit.Stage) []audit.Stage {

Choose a reason for hiding this comment

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

Please test this method separately

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@CaoShuFeng
Copy link
Contributor Author

/test pull-kubernetes-unit

1 similar comment
@CaoShuFeng
Copy link
Contributor Author

/test pull-kubernetes-unit

Copy link

@crassirostris crassirostris left a comment

Choose a reason for hiding this comment

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

SGTM, several nits

return &policyChecker{*policy}
}

func unionStages(s1, s2 []audit.Stage) []audit.Stage {

Choose a reason for hiding this comment

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

Why not

func unionStages(stageLists ...[]audit.Stage) []audit.Stage {
  m := make(map[audit.Stage]bool)
  for _, sl := range stageLists {
    for _, s := range sl {
      m[s] = true
    }
  }
  result := make([]audit.Stage, 0, len(m))
  for key := range m {
      result = append(result, key)
  }
  return result
}
  • Why do you need exactly two parameters in this case?
  • map[T]bool is more frequently used approach than map[T]struct{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Path: "/api/v1/namespaces/default/pods/busybox",
},
}
var tim = &user.DefaultInfo{

Choose a reason for hiding this comment

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

Replace with one var ( ... ) block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


// stageEqual returns true if s1 and s2 are super set of each other
func stageEqual(s1, s2 []audit.Stage) bool {
s1 = unionStages(s1, nil)

Choose a reason for hiding this comment

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

If you convert them both to map[T]bool and then compare maps, this it not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

req, strings.Join(ruleNames, ","), expOmitStages, actualOmitStages)
}

test("namespaced", audit.LevelMetadata, globalStages, "default")

Choose a reason for hiding this comment

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

You can extract the method signature to a named type and then extract this copypasta to a separate method that takes the test function

Then you don't even need the separate method, just call this new method twice with a different set of global stages

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@CaoShuFeng CaoShuFeng force-pushed the omit_stage branch 2 times, most recently from 668a071 to 3864e41 Compare November 6, 2017 05:57
@CaoShuFeng CaoShuFeng force-pushed the omit_stage branch 2 times, most recently from 46e4d37 to c0b5b43 Compare November 8, 2017 02:05
@CaoShuFeng
Copy link
Contributor Author

/test pull-kubernetes-node-e2e

@ncdc ncdc removed their assignment Nov 14, 2017
@tallclair
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 17, 2017
Copy link

@crassirostris crassirostris left a comment

Choose a reason for hiding this comment

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

LGTM

@sttts PTAL

}

func TestChecker(t *testing.T) {
// test audit level

Choose a reason for hiding this comment

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

Nit: extra comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

func TestChecker(t *testing.T) {
// test audit level
testAuditLevel(t, nil)
// test omitStages pre rule

Choose a reason for hiding this comment

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

Nit: make consistent with the next test function by adding an extra line and changing the comment description

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@k8s-github-robot k8s-github-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 19, 2017
@sttts
Copy link
Contributor

sttts commented Nov 20, 2017

/approve no-issue

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 20, 2017
@crassirostris
Copy link

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 20, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CaoShuFeng, crassirostris, sttts, tallclair

Associated issue: 54551

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@CaoShuFeng
Copy link
Contributor Author

This needs a milestone.

@k8s-github-robot
Copy link

[MILESTONENOTIFIER] Milestone Pull Request Labels Incomplete

@CaoShuFeng @crassirostris @sttts @tallclair

Action required: This pull request requires label changes. If the required changes are not made within 2 days, the pull request will be moved out of the v1.9 milestone.

kind: Must specify exactly one of kind/bug, kind/cleanup or kind/feature.
priority: Must specify exactly one of priority/critical-urgent, priority/important-longterm or priority/important-soon.
sig owner: Must specify at least one label prefixed with sig/.

Help

@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 52322, 54634). If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit 7b9affa into kubernetes:master Nov 21, 2017
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Nov 21, 2017

@CaoShuFeng: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kubernetes-e2e-gce 4a20d72 link /test pull-kubernetes-e2e-gce
pull-kubernetes-e2e-kops-aws 4a20d72 link /test pull-kubernetes-e2e-kops-aws

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/audit cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. milestone/incomplete-labels release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants