fiaas / k8s

Python client library for the Kubernetes API
https://fiaas.github.io/
Apache License 2.0
39 stars 24 forks source link

k8s.client.ClientError: 401: Unauthorized for url: #86

Closed FrantzT closed 4 years ago

FrantzT commented 4 years ago

Hello,

I was following your tutorial and tried the config.api_token but it failed to connect. It works fine when using config.cert.

from k8s import config from k8s.models.service import Service config.api_server = 'https://192.168.99.102:8443' config.verify_ssl = '/Users/x/.minikube/ca.crt' config.api_token = '****' Service.get('kubernetes')

Service.get('kubernetes') Traceback (most recent call last): File "", line 1, in File "/Users/x/Library/Python/3.8/lib/python/site-packages/k8s-0.15.1.dev4+ge880384-py3.8.egg/k8s/base.py", line 147, in get File "/Users/x/Library/Python/3.8/lib/python/site-packages/k8s-0.15.1.dev4+ge880384-py3.8.egg/k8s/client.py", line 112, in get File "/Users/x/Library/Python/3.8/lib/python/site-packages/k8s-0.15.1.dev4+ge880384-py3.8.egg/k8s/client.py", line 131, in _call File "/Users/x/Library/Python/3.8/lib/python/site-packages/k8s-0.15.1.dev4+ge880384-py3.8.egg/k8s/client.py", line 145, in _raise_on_status k8s.client.ClientError: 401: Unauthorized for url: https://192.168.99.102:8443/api/v1/namespaces/default/services/kubernetes Request: GET https://192.168.99.102:8443/api/v1/namespaces/default/services/kubernetes User-Agent: python-requests/2.22.0 Accept-Encoding: gzip, deflate Accept: / Connection: keep-alive Authorization: #REDACTED# Response: <<< Content-Type: application/json <<< Date: Thu, 23 Jul 2020 02:45:07 GMT <<< Content-Length: 129 <<< <<< {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Unauthorized","reason":"Unauthorized","code":401}

Thank you.

mortenlj commented 4 years ago

It looks like you are using minikube?

I think minikube uses client certificates and not tokens for the default user. Have you created additional users and trying to use a token for one of those?

I'm on my phone atm, so can't do much testing to investigate, but can take a look later. Can you tell me more about your setup?

FrantzT commented 4 years ago

Yes, it is minikube. It is probably the case, I found someone had similar issue with the default user. I will try create another user.

Thank you.

On Thu, Jul 23, 2020, 3:04 AM Morten Lied Johansen, < notifications@github.com> wrote:

It looks like you are using minikube?

I think minikube uses client certificates and not tokens for the default user. Have you created additional users and trying to use a token for one of those?

I'm on my phone atm, so can't do much testing to investigate, but can take a look later. Can you tell me more about your setup?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fiaas/k8s/issues/86#issuecomment-662851028, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC3ECMPNQXYBPBQOO5XRXRLR47OI5ANCNFSM4PFJP5ZQ .

mortenlj commented 4 years ago

I've started up a minikube cluster on my machine now, and as far as I can tell, you can't use tokens with the user created by minikube. If you have created the users yourself, I would look into the tokens you have created and check if you are using them correctly. One common mistake can be to use a base64 encoded token, which should not be base64 encoded when assigned to api_token.

I don't think this is a bug in the library, so I'm going to close this. Feel free to reopen if you think otherwise.

If you need help figuring out the problem, you can visit us in the Kubernetes Slack: #fiaas

FrantzT commented 4 years ago

Thank you Morten.

The user is created as "default" by minikube.

I think the culprit is the base64 encoding, which seems to be the default method for the token creation. I tried both (if I remember it correctly) encoded and not, but it still didn't work. I will give it a try again.

It is not an issue for me, just wanted to let you know.

I don't think it is a library bug, as I checked it and it passess the token as expected.

I tried to use the token with kubectl and it worked fine (base64 encoded) for the default user, so I was trying to use it with the k8s API hence reporting.

On Thu, 23 Jul 2020 at 16:04, Morten Lied Johansen notifications@github.com wrote:

I've started up a minikube cluster on my machine now, and as far as I can tell, you can't use tokens with the user created by minikube. If you have created the users yourself, I would look into the tokens you have created and check if you are using them correctly. One common mistake can be to use a base64 encoded token, which should not be base64 encoded when assigned to api_token.

I don't think this is a bug in the library, so I'm going to close this. Feel free to reopen if you think otherwise.

If you need help figuring out the problem, you can visit us in the Kubernetes Slack: #fiaas https://kubernetes.slack.com/archives/CNASTE7CP

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fiaas/k8s/issues/86#issuecomment-663205432, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC3ECMKELIKMDBK7IERRMS3R5CJTJANCNFSM4PFJP5ZQ .