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

kubectl --token is not honored when ~/.kube/config has an authentication token #44476

Closed
jcbsmpsn opened this issue Apr 13, 2017 · 6 comments · Fixed by #45575
Closed

kubectl --token is not honored when ~/.kube/config has an authentication token #44476

jcbsmpsn opened this issue Apr 13, 2017 · 6 comments · Fixed by #45575
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Milestone

Comments

@jcbsmpsn
Copy link
Contributor

What keywords did you search in Kubernetes issues before filing this one? (If you have found any duplicates, you should instead reply there.):

kubectl token

BUG REPORT

Kubernetes version (use kubectl version):
kubectl version
Client Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.0", GitCommit:"fff5156092b56e6bd60fff75aad4dc9de6b6ef37", GitTreeState:"clean", BuildDate:"2017-03-28T16:36:33Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.1", GitCommit:"b0b7a323cc5a4a2019b2e9520c21c7830b7f708e", GitTreeState:"clean", BuildDate:"2017-04-03T20:33:27Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

Environment:

  • Cloud provider or hardware configuration:
    GKE

  • OS (e.g. from /etc/os-release):
    macOS Sierra 10.12.4

  • Kernel (e.g. uname -a):
    ➜ uname -a
    Darwin jacobsimpson-macbookpro.roam.corp.google.com 16.5.0 Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64

  • Install tools:

  • Others:

What happened:
I set up a second user, then I use kubectl --token to authenticate as the second user. The --token parameter is ignored and the authentication token from the ~/.kube/config file is used instead, so I'm authenticated as the user configured in the config file, not as the user set on the command line parameters.

I further verified this by commenting out the section in the config file with the authentication token and then the --token parameter starts to work as expected.

What you expected to happen:
I expect the command line parameters, when present, to override the config file settings.

How to reproduce it (as minimally and precisely as possible):

gcloud container clusters create test-cluster --cluster-version=1.6.1
primary_account=$(gcloud auth list 2>&1 | grep ACTIVE | sed 's/.*- \([^ ]*\) .*/\1/')
admin_token="$(gcloud auth print-access-token --account=$primary_account)"
gcloud iam service-accounts create user-1 --display-name=user-1
user_account=$(gcloud iam service-accounts list --format='value(email)' --filter='displayName:user-1')
gcloud iam service-accounts keys create --iam-account $user_account user_key.json
gcloud auth activate-service-account $user_account --key-file=user_key.json
user_token="$(gcloud auth print-access-token --account=$user_account)"
kubectl --token="$admin_token" get nodes

At this point, the error message should be indicating kubectl tried to authenticate with the user-1 account. Something like this:

➜  kubectl --token="$admin_token" get nodes
Error from server (Forbidden): User "user-1@project-name.iam.gserviceaccount.com" cannot list nodes at the cluster scope.: "No policy matched.\nRequired \"container.nodes.list\" permission." (get nodes)

Now edit ~/.kube/config, search for the auth-provider: section for your cluster and comment out the whole section for the test cluster.

- name: test-cluster
  user:
    #auth-provider:
    #  config:
    #    access-token: ya29.xxxxx
    #    cmd-args: config config-helper --format=json
    #    cmd-path: /.../gcloud
    #    expiry: 2017-04-11T21:06:25Z
    #    expiry-key: '{.credential.token_expiry}'
    #    token-key: '{.credential.access_token}'
    #  name: gcp

Now it should be possible to run these two commands. The one using the $admin_token will succeed, the one using the $user_token will fail with the error message indicating the user name.

kubectl --token="$admin_token" get nodes
kubectl --token="$user_token" get nodes

Anything else we need to know:

@liggitt
Copy link
Member

liggitt commented Apr 14, 2017

The token arg on the command line overrides the bearer token field in the config file, but I'm not sure the precedence of the token arg and the auth provider in the config file.

cc @cjcullen

@liggitt liggitt added sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI. kind/bug Categorizes issue or PR as related to a bug. labels Apr 14, 2017
@ericchiang
Copy link
Contributor

All auth providers currently override the "Authorization" header even if it's set. Should be an easy-ish check to add.

Long term we probably want to have the auth provider stop wrapping the transport and just act like a token source #39587 (comment), that way some central code would handle the HTTP headers and we wouldn't have to enforce this on a per plugin basis.

@wanghaoran1988
Copy link
Contributor

@ericchiang So the tokens from command line should be used, right? if so I can send a pr to fix this.

@ericchiang
Copy link
Contributor

@wanghaoran1988 the client auth plugins shouldn't override the "Authorization" headers. Yeah feel free to send a PR.

@pwittrock
Copy link
Member

@cjcullen Is this a regression or why 1.7?

@ericchiang
Copy link
Contributor

@pwittrock bug is pretty self contained in the GCP client auth plugin and the fix is short #45575

Not strictly a regression, but something that popped up over this cycle as a bug impacting users. Is that reason enough to get it in during the 1.7 freeze?

k8s-github-robot pushed a commit that referenced this issue Jun 9, 2017
Automatic merge from submit-queue

Make gcp auth provider not to override the Auth header if it's already exits

**What this PR does / why we need it**:
Make AuthProvider not wrap the transport if beartoken or basic auth is enabled
**Which issue this PR fixes** :
 fixes #44476 

**Special notes for your reviewer**:

**Release note**:
```
GCP auth plugin no longer overwrites existing Authorization headers.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Projects
None yet
6 participants