kube-apiserver authentification ( Unauthorized )

7/5/2018

i created user teamcity to be able to use kube-apiserver

kubectl create serviceaccount teamcity

With the command below i get the secrets name

kubectl get accourcissements teamcity -o yaml

To find the token who were generated by the last command i use

kubectl get secret teamcity-token-lmr6z -o yaml

when i try to connect by curl i've an error and i dont understand where is my mistake :(

 curl -v -Sskk -H "Authorization: bearer ZXlKaGJH......wWHNIVzZ3" https://10.109.0.88:6443/api/v1/namespaces

 HTTP/1.1 401 Unauthorized
 Content-Type: application/json
 Date: Thu, 05 Jul 2018 13:14:00 GMT
 Content-Length: 165

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
* Connection #0 to host 10.109.0.88 left intact

I found a small description on kubernetes about why i get this error (section : Anonymous requests) https://kubernetes.io/docs/reference/access-authn-authz/authentication/

But i still not understand where is my mistake because with kubectl it's work

kubectl  --token=ZXlKaGJHY2lPaUpTVXpJ........swWHNIVzZ3 get svc
NAME                       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
hello-kubernetes           NodePort    192.2.0.159   <none>        80:17502/TCP   13d
hello-kubernetes-olivier   NodePort    192.2.0.235   <none>        80:17296/TCP   13d
kubernetes                 ClusterIP   192.2.0.1     <none>        443/TCP        14d 
-- morla
kubectl
kubernetes
minikube

1 Answer

7/5/2018

It might be your typo the part of "bearer", as i remeber it's "Bearer".

Some command sample is as follows, Kubernetes - Accessing Clusters

$ APISERVER=$(kubectl config view | grep server | cut -f 2- -d ":" | tr -d " ")

$ TOKEN=$(kubectl describe secret $(kubectl get secrets | grep default | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d '\t')

$ curl $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "10.0.1.149:443"
    }
  ]
}
-- Daein Park
Source: StackOverflow