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

audit newest impersonated user info in the ResponseStarted, ResponseComplete audit stage #48184

Merged
merged 1 commit into from
Sep 3, 2017

Conversation

CaoShuFeng
Copy link
Contributor

@CaoShuFeng CaoShuFeng commented Jun 28, 2017

Impersonation will automatically add system:authenticated, system:serviceaccounts group to the impersonated user info. This pr use the newest impersonated user info in the second audit event. This will help users to debug rbac problems.

Release note:

[advanced audit] audit newest impersonated user info in the ResponseStarted, ResponseComplete audit stage

@liggitt @sttts

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 28, 2017
@k8s-ci-robot
Copy link
Contributor

Hi @CaoShuFeng. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 28, 2017
@k8s-github-robot k8s-github-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jun 28, 2017
// UpdateEventAttribs updates authorizer attributes according to attribs
func UpdateEventAttribs(ev *auditinternal.Event, attribs authorizer.Attributes) {
if user := attribs.GetUser(); user != nil {
ev.User.Username = user.GetName()
Copy link
Member

Choose a reason for hiding this comment

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

mutating this seems wrong. I'd want both the requesting user and effective/impersonated user in my audit log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liggitt
Thanks for your review.
I will research it.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

We have to extend the event type for that I guess.

/cc @timstclair

Choose a reason for hiding this comment

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

I'm not sure if I'm following what this is doing, but ev.User should be the "real" user, and ev.ImpersonatedUser should hold the the impersonated user attributes. What are we missing?

I agree with @liggitt that the audit event should be thought of as "append only". I.e. we shouldn't change information from earlier 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.

Hi, @tallclair
I think we can extend the event type, add a new ev.EffectiveUser to events.

The ev.User in the log is the "real" user, but it is not the "effective" user. Because impersonation will change the user info after the audit filter gathers event info.

Can we add this to the TODO list @tallclair ?

@ericchiang What's your opinion?

Copy link
Contributor Author

@CaoShuFeng CaoShuFeng Jul 14, 2017

Choose a reason for hiding this comment

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

Usually EffectiveUser is equal to ImpersonatedUser.
But when user run kubectl with --as=username option, the difference comes.

This tables shows the difference, we can see that some groups are automatically added.
2017-07-14 10 52 22

The related codes are here:
https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apiserver/pkg/endpoints/filters/impersonation.go#L87
https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apiserver/pkg/endpoints/filters/impersonation.go#L121

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we don't want to add a new field "EffectiveUser", I think we can copy some codes form impersonation.go to audit.go, and we can also add the same groups to the impersonated user.
Then the impersonated user will become effective user.

Copy link
Member

Choose a reason for hiding this comment

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

Don't copy code, I'd expect impersonated user to hold the output of the impersonation filter, what you are calling the effective user

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'd expect impersonated user to hold the output of the impersonation filter, what you are calling the effective user

I aggree.

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 updated the code.
Please review again, thanks.

@CaoShuFeng CaoShuFeng changed the title audit real authorizer attributes in the second audit stage audit real impersonated user info in the ResponseStarted, ResponseComplete audit stage Jul 17, 2017
@CaoShuFeng CaoShuFeng changed the title audit real impersonated user info in the ResponseStarted, ResponseComplete audit stage audit newest impersonated user info in the ResponseStarted, ResponseComplete audit stage Jul 17, 2017
@ncdc
Copy link
Member

ncdc commented Jul 17, 2017

/unassign
/assign @liggitt
fyi @mattmoyer

@k8s-ci-robot k8s-ci-robot assigned liggitt and unassigned ncdc Jul 17, 2017
@ericchiang
Copy link
Contributor

ericchiang commented Jul 17, 2017

Okay, so the issue is that the API server adds additional groups to the impersonated user that the audit log isn't picking up.

Rather than mutating the event log later, can we move impersonation before audit log in the handler chain and have the impersonation filter (filters.WithImpersionation) add another context key indicating the original user?

Something like:

package request

// Existing API

func UserFrom(ctx Context) (user.Info, bool) {}
func WithUser(parent Context, user user.Info) Context {}

// NewAPI. Maybe parameter names could be improved?

// WithImpersionation indicates that the user performing the request is now impersonating the
// provided user. Further calls to WithUser on the returned context will return the impersonating
// user info.
func WithImpersionation(parent Context, impersonator, impersonating user.Info) Context {}

// ImpersionationFrom returns the full impersonation information from the request. If the request
// did not use impersonation, it returns false indicating that UserFrom should be used instead.
func ImpersionationFrom(ctx Context) (impersonator, impersonating, user.Info, ok bool) {}

Then have the audit log mechanism use ImpersionationFrom?

cc @sttts

@sttts
Copy link
Contributor

sttts commented Jul 18, 2017

can we move impersonation before audit log in the handler chain and have the impersonation filter

We can. But then we loose the 403 from impersonation. @soltysh was working on a catch-up audit handler to get the 403s from authentication. I guess his approach will also work with impersonation if we switch the handler chain order. @soltysh ?

@CaoShuFeng
Copy link
Contributor Author

Sorry to disturb you guys again and again.
What about just use my current solution?

  1. As far as I can see, this only add the automatically added groups into the impersonated user. No info will be deleted.
  2. Further more, this only effect the request with impersonated user setted but impersonated groups not setted. I most cases, this change has no effect.
  3. If we document this well, I thint it will not do any harm.

Thanks very much.

@soltysh
Copy link
Contributor

soltysh commented Aug 7, 2017

We can. But then we loose the 403 from impersonation. @soltysh was working on a catch-up audit handler to get the 403s from authentication. I guess his approach will also work with impersonation if we switch the handler chain order. @soltysh ?

That shouldn't be the problem. The issue I'm trying to address in openshift (so far) is to be able to log failed authz requests, which our current implementation omits due to design decision we've made at the beginning. @crassirostris @sttts @ericchiang not sure if we want to address this at some point in k8s?

@ericchiang
Copy link
Contributor

/ok-to-test

Some possible options:

  • Merge as is.
  • Remove impersonation logic from audit.NewEventFromRequest and just do it here. Don't capture request impersonation headers, only the actual user that gets authenticated.
  • Differentiate between request impersonation headers and actual impersonated user.

I think I'm for the first one.

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 7, 2017
@CaoShuFeng
Copy link
Contributor Author

/test pull-kubernetes-e2e-gce-etcd3

@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 17, 2017
@crassirostris
Copy link

@CaoShuFeng What's the status of this PR?

@@ -142,6 +143,11 @@ func WithImpersonation(handler http.Handler, requestContextMapper request.Reques
}
}

if !groupsSpecified && username != user.Anonymous {
Copy link
Contributor

Choose a reason for hiding this comment

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

Now I'm puzzled. Why we need to do this, if we have this snippet in place and audit happens before impersonation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have 4 stages.
This change will effect the other three stages rather than "StageRequestReceived".

We do this because the snippet may not complete.

Copy link
Contributor Author

@CaoShuFeng CaoShuFeng Aug 31, 2017

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I see the groups and UID might be different than in the old code. Can we drop the old snippet and use this instead? Having two code paths for the same things is confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 to what Stefan said, replace the old code with the new one, entirely. I did not understand you correctly, initially.

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.

@soltysh
Copy link
Contributor

soltysh commented Aug 31, 2017

@CaoShuFeng will you be able to update this one asap, as well?

@CaoShuFeng
Copy link
Contributor Author

@CaoShuFeng will you be able to update this one asap, as well?

I have get off work today. I will update this tomorrow morning.

Log the newest impersonated user info in the second audit event. This
will help users to debug rbac problems.
@k8s-ci-robot k8s-ci-robot removed the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 1, 2017
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 1, 2017
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 1, 2017
@sttts
Copy link
Contributor

sttts commented Sep 1, 2017

/lgtm
/approve no-issue

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

[APPROVALNOTIFIER] This PR is APPROVED

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

Associated issue requirement bypassed by: sttts

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

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 1, 2017
@soltysh soltysh added this to the v1.8 milestone Sep 1, 2017
@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 51301, 50497, 50112, 48184, 50993)

@k8s-github-robot k8s-github-robot merged commit 134b667 into kubernetes:master Sep 3, 2017
@ericchiang ericchiang added the sig/auth Categorizes an issue or PR as relevant to SIG Auth. label Sep 7, 2017
@CaoShuFeng CaoShuFeng deleted the impersonate_audit branch November 6, 2017 05:44
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. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/auth Categorizes an issue or PR as relevant to SIG Auth. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.