如何解决禁止:用户,“代码”:使用curl
Kubernetes版本:v1.19.0
我已经创建了一个用户,并以cluster-admin角色执行了clusterrolebinding。
[root@project1-master ~]# kubectl describe clusterrole cluster-admin
Name: cluster-admin
Labels: kubernetes.io/bootstrapping=rbac-defaults
Annotations: rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
*.* [] [] [*]
[*] [] [*]
[root@project1-master ~]# kubectl describe clusterrolebinding sajeesh cluster-admin
Name: sajeesh
Labels: <none>
Annotations: <none>
Role:
Kind: ClusterRole
Name: cluster-admin
Subjects:
Kind Name Namespace
---- ---- ---------
User sajeesh
Name: cluster-admin
Labels: kubernetes.io/bootstrapping=rbac-defaults
Annotations: rbac.authorization.kubernetes.io/autoupdate: true
Role:
Kind: ClusterRole
Name: cluster-admin
Subjects:
Kind Name Namespace
---- ---- ---------
Group system:masters
我可以使用此用户帐户运行kubectl并获取pods信息:
[root@project1-master ~]# kubectl get pods --as sajeesh
NAME READY STATUS RESTARTS AGE
busyb 1/1 Running 3 21h
但是当我尝试使用curl访问kube-apiserver时,它显示如下所示的禁止错误:
[root@project1-master ~]# curl --cacert /etc/kubernetes/pki/ca.crt --cert sajeesh.crt --key sajeesh.key https://$IP:6443/api/v1/namespaces/default/pods/busyb
{
"kind": "Status","apiVersion": "v1","metadata": {
},"status": "Failure","message": "pods \"busyb\" is forbidden: User \"system:anonymous\" cannot get resource \"pods\" in API group \"\" in the namespace \"default\"","reason": "Forbidden","details": {
"name": "busyb","kind": "pods"
},"code": 403
我已经重新验证了该用户帐户提供的cacert,cert和key。它们是正确的。
任何建议为什么会发生这种情况以及如何解决它。
解决方法
集群绑定sajeesh
的名称为sajeesh
,组名为system:masters
。因此,证书需要将sajeesh
作为公用名(CN),将system:masters
作为组织(O)。当请求发送到kubernetes API服务器时,它将检查证书,并将CN
中的name
与O
和Group
与clusterrolebinding
进行匹配,如果不匹配您会收到403 Forbidden
错误。
您可以通过运行以下命令来进行验证
openssl x509 -in sajeesh.crt -text -noout
,
最终设法找到了问题。它与kubernetes无关,但是我正在使用curl命令。
curl --cacert /etc/kubernetes/pki/ca.crt --cert sajeesh.crt --key sajeesh.key https://$IP:6443/api/v1/namespaces/default/pods/busyb
当我与命令一起使用-v switch时,它显示:
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/kubernetes/pki/ca.crt
CApath: none
* warning: certificate file name "sajeesh.crt" handled as nickname; please use "./sajeesh.crt" to force file name
* NSS: client certificate not found: newsajeesh.crt
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
所以基本上,它是在寻找绝对路径作为参数--cert和--key
的输入curl --cacert /etc/kubernetes/pki/ca.crt --cert ./sajeesh.crt --key ./sajeesh.key https://$IP:6443/api/v1/namespaces/default/pods/busyb
给出绝对路径后,它工作正常,我能够得到输出。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。