首页 > 解决方案 > API Server:由于错误而无法验证请求:无效的不记名令牌

问题描述

我设置了一个包含 3 个主节点和 2 个工作节点的示例 Kubernetes 集群。我试图将它连接到 OpenId 提供程序(在我的情况下为 Keycloak)。但是在查询 API 时,我从 kubectl 收到以下消息:

error: You must be logged in to the server (Unauthorized)

或通过卷曲:

curl -H "Authorization: Bearer $token" -k https://192.168.178.30:6443/api/v1/namespaces/default/pods
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
}

在 API 服务器日志中它说

Unable to authenticate the request due to an error: invalid bearer token

根据jwt.io ,我的令牌似乎是有效的。

API 服务器的配置

在 API 服务器中,我指定了以下参数:

oidc-issuer-url: https://192.168.178.25:8443/auth/realms/master
oidc-client-id: account
oidc-ca-file: /etc/kubernetes/ssl/keycloak-rootCA.pem
oidc-username-claim: sub
oidc-groups-claim: groups

我必须指定 CA 文件,因为用于 keycloak 的证书是自签名的

证书似乎可以工作,因为没有 CA 文件的 curl 会导致错误;不指定 CA 文件的地方:

ansible@node1:~$ curl https://192.168.178.25:8443/auth/realms/master
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.


ansible@node1:~$ curl --cacert /etc/kubernetes/ssl/keycloak-rootCA.pem https://192.168.178.25:8443/auth/realms/master
{"realm":"master","public_key":"...","token-service":"https://192.168.178.25:8443/auth/realms/master/protocol/openid-connect","account-service":"https://192.168.178.25:8443/auth/realms/master/account","tokens-not-before":0}

智威汤逊令牌

我的 JWT 令牌的有效负载如下所示:

{
  "jti": "455feab7-5828-49f3-9243-a2ce0a1ae6f9",
  "exp": 1557246081,
  "nbf": 0,
  "iat": 1557246021,
  "iss": "https://192.168.178.25:8443/auth/realms/master",
  "aud": "account",
  "sub": "78f9bdc8-d1a4-43c4-ab83-f1b4645519f6",
  "typ": "ID",
  "azp": "account",
  "auth_time": 1557246020,
  "session_state": "ccafaa62-a888-43dd-9135-1c5a31766e0b",
  "acr": "1",
  "email_verified": true,
  "name": "Testuser",
  "groups": [
    "group1",
    "k8s-admin"
  ],
  "preferred_username": "dummy-username",
  "given_name": "Firstname1",
  "family_name": "Lastname1",
  "email": "me@example.com"
}

当我尝试使用生成的服务帐户(来自 kubernetes)的令牌登录时,一切正常。

附加信息

环境是通过 Kubespray 设置的,API 服务器参数按以下方式指定:

kube_oidc_url: https://192.168.178.25:8443/auth/realms/master
kube_oidc_client_id: account
kube_oidc_ca_file: "{{ kube_cert_dir }}/keycloak-rootCA.pem"
kube_oidc_username_claim: sub
kube_oidc_groups_claim: groups
#kube_oidc_username_prefix: "oidc:"
#kube_oidc_groups_prefix: "oidc:"

谢谢你的帮助。

标签: kuberneteskeycloakkubernetes-apiserverkubespray

解决方案


问题是 Kubespray 没有超越我的设置进入 api-server (/etc/kubernetes/manifests/kube-apiserver.yaml) 的 Pod 规范。当我在每个 api-server 的 Pod 规范中添加参数时(我有一个多主设置),它按预期工作:

- --oidc-issuer-url=https://192.168.178.25:8443/auth/realms/master
- --oidc-client-id=account
- --oidc-username-claim=email
- '--oidc-username-prefix=oidc:'
- --oidc-groups-claim=groups
- '--oidc-groups-prefix=oidc:'
- --oidc-ca-file=/etc/kubernetes/ssl/keycloak-rootCA.pem

推荐阅读