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

Add exit code 1 on not allowed to kubectl auth can-i #59579

Merged
merged 1 commit into from
Jun 5, 2018

Conversation

fbac
Copy link
Contributor

@fbac fbac commented Feb 8, 2018

What this PR does / why we need it:
kubectl auth can-i verb resource always returns 0 status, even if the user can't

With this commit, kubectl will return exit code 1 when a verb is not allowed. It doesn't affect quiet option.

Release note:

Fix kubectl auth can-i exit code. It will return 1 if the user is not allowed and 0 if it's allowed.

@k8s-ci-robot
Copy link
Contributor

@fbac: Adding do-not-merge/release-note-label-needed because the release note process has not been followed.

One of the following labels is required "release-note", "release-note-action-required", or "release-note-none".
Please see: https://git.k8s.io/community/contributors/devel/pull-requests.md#write-release-notes-if-needed.

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.

@k8s-ci-robot k8s-ci-robot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Feb 8, 2018
@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 Feb 8, 2018
@ericchiang
Copy link
Contributor

I think "can-i" answering "no" is still a successful result. For example:

$ kubectl can-i --as=system:anonymous get pods
no

If you get a non-zero exit code, can-i could also have failed. I don't think that'd be the correct way to detect if a user didn't have access.

@fbac
Copy link
Contributor Author

fbac commented Feb 8, 2018

When can-i fails it raises an error with CheckErr. This exit 1 only happens if err == nil

In my opinion, current behavior is a bit inconsistent. With the same non-allowed query it returns different exit codes
kubectl auth can-i verb resource --quiet returns exit code 1
kubectl auth can-i verb resource returns exit code 0

With this little fix, it will return 1 always when not allowed, 0 when allowed. It doesn affect --quiet nor the CheckErr if something went bad.

@ericchiang
Copy link
Contributor

In my opinion, current behavior is a bit inconsistent. With the same non-allowed query it returns different exit codes
kubectl auth can-i verb resource --quiet returns exit code 1
kubectl auth can-i verb resource returns exit code 0

This seems intentional: #43900

This is minor, but the current behavior makes sense to me.

  • Interactive -> you view the output
  • Non-interactive -> have to go off of return code

/ok-to-test
cc @kubernetes/sig-auth-pr-reviews in case others have opinions here.

@k8s-ci-robot k8s-ci-robot added sig/auth Categorizes an issue or PR as relevant to SIG Auth. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 8, 2018
@fbac
Copy link
Contributor Author

fbac commented Feb 9, 2018

/test pull-kubernetes-bazel-test

@fbac
Copy link
Contributor Author

fbac commented Apr 8, 2018

@dims @ghodss hi, just wanted to know if I'm missing something in this PR to get it reviewed. Thanks!

@fbac
Copy link
Contributor Author

fbac commented Apr 26, 2018

Hi, can anybody in @kubernetes/sig-auth-pr-reviews review this?

@k8s-ci-robot
Copy link
Contributor

@fbac: Reiterating the mentions to trigger a notification:
@kubernetes/sig-auth-pr-reviews

In response to this:

Hi, can anybody in @kubernetes/sig-auth-pr-reviews review this?

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.

@dims
Copy link
Member

dims commented Apr 30, 2018

/release-note-none

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Apr 30, 2018
@dims
Copy link
Member

dims commented May 3, 2018

/retest

@fbac
Copy link
Contributor Author

fbac commented May 4, 2018

PTAL @soltysh can you review this?

@liggitt
Copy link
Member

liggitt commented May 10, 2018

this change makes sense to me. needs a test and a release note

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 11, 2018
@k8s-ci-robot k8s-ci-robot removed the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 11, 2018
@fbac
Copy link
Contributor Author

fbac commented May 11, 2018

/retest

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels May 11, 2018
@@ -166,6 +174,11 @@ func TestRunAccessCheck(t *testing.T) {
}

actualAllowed, err := test.o.RunAccessCheck()
actualReturnCode := 0
if !actualAllowed {
actualReturnCode = 1
Copy link
Member

Choose a reason for hiding this comment

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

this isn't really testing your change... this test would pass even without your change. I expected something in test-cmd-util.sh that actually checked exit code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're completely right. I've written an small test in test-cmd-util.sh, but becase the apiserver is always in AlwaysAllow during tests, does it makes sense? It will never return exit 1.

Copy link
Member

Choose a reason for hiding this comment

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

hrmm... true. I guess we'll skip that for now and go with manual testing.

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 11, 2018
@fbac
Copy link
Contributor Author

fbac commented May 11, 2018

/retest

@@ -5219,6 +5219,12 @@ runTests() {

output_message=$(kubectl auth can-i list jobs.batch/bar -n foo --quiet 2>&1 "${kube_flags[@]}")
kube::test::if_empty_string "${output_message}"

output_message=$(kubectl auth can-i get pods --subresource=log 2>&1 "${kube_flags[@]}"; echo $?)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still not testing your particular change. Can you add a test for your case, iow. one that will return non-zero when not allowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually there is no way to test kubectl returning exit code 1, since the apiserver is in AlwaysAllow, how can we test this?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we'll have to live with a manual test. See #59579 (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.

@soltysh so what changes should I make? Since there is no way to test error exit 1 I think the commit is good as it is now.

@soltysh soltysh added this to the v1.11 milestone Jun 5, 2018
Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 5, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fbac, soltysh

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

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 5, 2018
@soltysh
Copy link
Contributor

soltysh commented Jun 5, 2018

/sig cli
/status approved-for-milestone
/kind bug
/priority important-longterm

@k8s-ci-robot
Copy link
Contributor

You must be a member of the kubernetes/kubernetes-milestone-maintainers github team to add status labels.

@k8s-ci-robot k8s-ci-robot added sig/cli Categorizes an issue or PR as relevant to SIG CLI. kind/bug Categorizes issue or PR as related to a bug. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. labels Jun 5, 2018
@k8s-github-robot
Copy link

[MILESTONENOTIFIER] Milestone Pull Request: Up-to-date for process

@fbac @soltysh

Pull Request Labels
  • sig/auth sig/cli: Pull Request will be escalated to these SIGs if needed.
  • priority/important-longterm: Escalate to the pull request owners; move out of the milestone after 1 attempt.
  • kind/bug: Fixes a bug discovered during the current release.
Help

@fbac
Copy link
Contributor Author

fbac commented Jun 5, 2018

/test pull-kubernetes-e2e-kops-aws
/test pull-kubernetes-bazel-test

@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. If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit fa20f87 into kubernetes:master Jun 5, 2018
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. 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. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants