首页 > 解决方案 > 基于 Kubernetes 客户端证书的身份验证导致使用 system:anonymous 用户的请求

问题描述

我已按照这些说明在 Kubernetes 中创建普通用户并生成客户端证书https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/#normal-user

当我尝试通过提供证书和密钥使用 curl 向 API 服务器发送请求时,响应返回为"message": "forbidden: User \"system:anonymous\" cannot get path \"/apis\"

curl -k -v -L --cert ./userone.crt --key ./userone.key -k --request GET https://<host>:443/apis\?timeout\=32s

<Removed some text>

 HTTP/1.1 403 Forbidden
< Server: nginx/1.12.2
< Date: Tue, 02 Nov 2021 21:53:03 GMT
< Content-Type: application/json
< Content-Length: 237
< Connection: keep-alive
< Cache-Control: no-cache, private
< X-Content-Type-Options: nosniff
< 
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "forbidden: User \"system:anonymous\" cannot get path \"/apis\"",
  "reason": "Forbidden",
  "details": {
    
  },
  "code": 403

还尝试在我具有提升访问权限的地方模拟这个用户,这个新用户能够访问集群角色和角色绑定中配置的 Pod。这至少证明授权是有效的。

kubectl auth can-i -n xyz get pods --as=userone
yes

问题似乎是新用户无权访问 API 服务器上的 /apis 端点。尝试将 nonResourceURLs 添加为 ClusterRole 的一部分,但这也无济于事。

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: read-only-role
rules:
- apiGroups:
  - ""
  resources: ["*"]
  verbs:
  - get
  - list
  - watch
- nonResourceURLs:
  - /metrics
  - /api/*
  - /apis/*
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-only-binding
  namespace: xyz
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: read-only
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: userone

我们使用的是 Kubernetes 版本 1.18.17,并且 API 服务器也配置了 --client-ca-file 选项。感谢您对此的帮助。

标签: kubernetesclient-certificates

解决方案


推荐阅读