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

Changed kubectl config view to redact user token #88985

Merged
merged 1 commit into from
Mar 18, 2020

Conversation

brianpursley
Copy link
Member

@brianpursley brianpursley commented Mar 9, 2020

What type of PR is this?
/kind bug

What this PR does / why we need it:
When you run kubectl config view, the user token is not redacted like client-certificate-data and client-key-data. This PR redacts the user token.

Which issue(s) this PR fixes:
xref kubernetes/kubectl#667

Special notes for your reviewer:
None

Does this PR introduce a user-facing change?:

`kubectl config view` now redacts bearer tokens by default, similar to client certificates. The `--raw` flag can still be used to output full content.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. 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. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 9, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @brianpursley. 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.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@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 Mar 9, 2020
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 9, 2020
@brianpursley
Copy link
Member Author

/assign smarterclayton

@enj
Copy link
Member

enj commented Mar 9, 2020

IIRC the reason for redacting the cert data was that it made the output hard to read (since it is long). You can always cat the config file so not really sure it matters if we redact the token here.

// Flatten redacts raw data entries from the config object for a human-readable view.
func ShortenConfig(config *Config) {

The go doc and function name hint that this is not related to security.

@brianpursley
Copy link
Member Author

brianpursley commented Mar 10, 2020

IIRC the reason for redacting the cert data was that it made the output hard to read (since it is long). You can always cat the config file so not really sure it matters if we redact the token here.

// Flatten redacts raw data entries from the config object for a human-readable view.
func ShortenConfig(config *Config) {

The go doc and function name hint that this is not related to security.

@enj you are correct, it is not related to security.

I found some related discussion that its purpose is to shorten long data, like you said.

For reference, on my machine, the pertinent output without redaction looks like this for an AKS cluster:

  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    token: 35ae444a781d891fa006b509500f5c4f7fd30e79aa092fef98806e0996d6adcc7b3929a1943c2ed1439d501a9c2e888ffc1be1955dd804474e050eff84199e08

I guess it is debatable whether this is long enough for "redaction".

I personally don't have a preference, just trying to close out this issue. So if we want to leave it as-is and close the issue, I'm fine with that too.

@@ -98,6 +98,9 @@ func ShortenConfig(config *Config) {
if len(authInfo.ClientCertificateData) > 0 {
authInfo.ClientCertificateData = redactedBytes
}
if len(authInfo.Token) > 0 {
authInfo.Token = string(redactedBytes)
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this works after being cast to a string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, It is different than the others which are byte arrays. Token is a string.

Copy link
Member

@liggitt liggitt Mar 11, 2020

Choose a reason for hiding this comment

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

what does the output of this look like? tokens are strings, and the bytes you're assigning here are base-64 decoded versions of "REDACTED+"... I don't expect that would display well

we also have other potentially sensitive fields in the config (password, auth config); see corresponding sanitization of rest.Config printing in https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/rest/config.go#L158-L175

Copy link
Member

Choose a reason for hiding this comment

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

The goal of ShortenConfig was not to keep confidential data from the output (if you're running this command, you already have read access to the data), but to prevent massively long output from byte-serialized certificates.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed that it doesn't provide security.

I think you questioning is right though. There is a problem. The unit test passes, but the output is not "REDACTED" it is garbage. Good catch. I will re-visit this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry @lavalamp, I think your intuition was right, it doesn't work. Not sure what I was thinking... 😕

@lavalamp, @liggitt I pushed a new commit and the output looks like this:

- name: clusterUser_k8s_akstest
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    token: REDACTED

Copy link
Member

Choose a reason for hiding this comment

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

Why was the test wrong?

Copy link
Member Author

@brianpursley brianpursley Mar 11, 2020

Choose a reason for hiding this comment

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

Why was the test wrong?

It was a new test case I added that was wrong, so it wasn't a problem in an existing test. I made an assumption that string(redactedBytes) would result in "REDACTED" but that was wrong.

redactedBytes is defined as:

sDec, _ := base64.StdEncoding.DecodeString("REDACTED+")
redactedBytes = []byte(string(sDec))

I didn't look closely enough at how that actually worked.

It is sort of tricky in that it is starting with a "base-64 encoded" value of "REDACTED+" and then decoding it, storing it in bytes, so that later when it is re-encoded, it displays as a human-readable "REDACTED".

When I did string(redactedBytes) I got the base64 decode of "REDACTED+" which is actually "D@�\t1\x03".

My test was wrong in the same way, I was inadvertently checking for "D@�\t1\x03" instead of "REDACTED".

Copy link
Member Author

Choose a reason for hiding this comment

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

@lavalamp
Copy link
Member

I think it is fine to redact this for readability. It's not a security thing, though.

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Mar 10, 2020
@brianpursley
Copy link
Member Author

brianpursley commented Mar 10, 2020

Looks like I need to update some bazel tests...

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 10, 2020
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 11, 2020
@brianpursley
Copy link
Member Author

/unhold

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 11, 2020
@lavalamp
Copy link
Member

I was 100% fooled by the test. Why did the test output not match reality? Please fix that?

@brianpursley
Copy link
Member Author

@lavalamp as mentioned in my comment above, the wrong test was added by me in this commit, so it wasn't a problem with an existing test. I have updated the test and checked the output.

@lavalamp
Copy link
Member

lavalamp commented Mar 11, 2020 via email

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 11, 2020
@brianpursley
Copy link
Member Author

Thanks for the explanation! Are we not running the "Example" function as a test, then? Is it configured wrong? It sounds like that really should have failed.

to be honest, I don't know what that Example function was doing in the unit test. I just updated the commented out yaml so that it would be consistent with the change I made.

It appears not to be an actual test.

I just committed an update that turned it into a real unit test and checks the output against what was previously in the comment, so hopefully it will serve some purpose in the future.

@lavalamp
Copy link
Member

It's supposed to actually test stuff: https://blog.golang.org/examples

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 17, 2020
@brianpursley
Copy link
Member Author

It's supposed to actually test stuff: https://blog.golang.org/examples

Thanks, I'm still sort of new to Go, so wasn't familiar with that convention. TIL. 👨‍🎓

I reverted it back to the original "Example" test and confirmed the test DOES fail when running it locally.

--- FAIL: Example_minifyAndShorten (0.00s)
got:
clusters:
  cow-cluster:
    LocationOfOrigin: ""
    certificate-authority-data: DATA+OMITTED
    server: http://cow.org:8080
contexts:
  federal-context:
    LocationOfOrigin: ""
    cluster: cow-cluster
    user: red-user
current-context: federal-context
preferences: {}
users:
  red-user:
    LocationOfOrigin: ""
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    token: "D@�\t1\x03"
want:
clusters:
  cow-cluster:
    LocationOfOrigin: ""
    certificate-authority-data: DATA+OMITTED
    server: http://cow.org:8080
contexts:
  federal-context:
    LocationOfOrigin: ""
    cluster: cow-cluster
    user: red-user
current-context: federal-context
preferences: {}
users:
  red-user:
    LocationOfOrigin: ""
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    token: REDACTED
FAIL

This PR never actually got the needs-ok-to-test label removed, and I wasn't a Kubernetes member yet, so it never ran the tests in the CI or else I suspect it would have failed.

I am a member now, so let me try it...
/ok-to-test

Once the tests pass, I think this PR should be ready for re-review.

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 17, 2020
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 17, 2020
@brianpursley
Copy link
Member Author

/retest

@liggitt
Copy link
Member

liggitt commented Mar 18, 2020

/priority backlog
/lgtm
/approve
/remove-kind bug
/kind cleanup

@k8s-ci-robot k8s-ci-robot added priority/backlog Higher priority than priority/awaiting-more-evidence. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed kind/bug Categorizes issue or PR as related to a bug. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 18, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brianpursley, lavalamp, liggitt

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 merged commit 4bc907f into kubernetes:master Mar 18, 2020
@k8s-ci-robot k8s-ci-robot added this to the v1.19 milestone Mar 18, 2020
@brianpursley brianpursley deleted the kubectl-667 branch February 2, 2023 01:23
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/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants