【问题标题】:forbidden: User , "code": 403 error while accessing kube-apiserver using curl禁止:用户,“代码”:使用 curl 访问 kube-apiserver 时出现 403 错误
【发布时间】:2020-12-30 19:44:47
【问题描述】:

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 并获取 pod 信息:

[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 、证书和密钥。它们是正确的。

关于为什么会发生这种情况以及如何解决它的任何建议。

【问题讨论】:

    标签: kubernetes rbac kube-apiserver


    【解决方案1】:

    集群角色绑定sajeesh 具有名称sajeesh 和组system:masters。因此证书需要具有sajeesh 作为通用名称(CN) 和system:masters 作为组织(O)。当请求发送到 kubernetes API 服务器时,它将检查证书并将 CNnameOGroupclusterrolebinding 匹配,如果不匹配,则会出现 403 Forbidden 错误。

    您可以通过运行以下命令来验证上述内容

    openssl x509 -in sajeesh.crt -text -noout
    

    【讨论】:

    • 是的 CN 是 sajeesh ,但是 O 是不同的,因为我希望这个用户成为名为 group1 的不同组的一部分。签名算法:sha256WithRSAEncryption 颁发者:CN=kubernetes 有效期不早于:9 月 12 日 07:44:59 2020 GMT 不晚于:9 月 12 日 07:44:59 2021 GMT 主题:CN=sajeesh, O=group1
    • 正如我所说O 需要有system:masters,因为这就是你在集群角色绑定中所拥有的......否则编辑或创建一个新的集群角色绑定Groupgroup1
    • 如果您在我的问题中看到“kubectl describe clusterrolebinding sajeesh cluster-admin”的输出,则对用户名 sajeesh 和组 system:masters.so 进行了绑定,理想情况下,它应该提供对这个用户也是支持这一想法的另一个理由是“kubectl get pods --as sajeesh”运行良好。
    • 我已经重新创建了用户(csr&certificate)提到 O 作为 system:masters 发行人:CN=kubernetes 有效期不早于:2020 年 9 月 12 日 15:50:00 GMT 不晚于:9 月 12 日 15:50:00 2021 GMT 主题:CN=sajeesh, O=system:masters 当我使用 curl 访问它时仍然显示相同的错误。 "message": "pods \"busyb\" 被禁止:用户 \"system:anonymous\" 无法在命名空间 \"default\"" 中的 API 组 \"\" 中获取资源 \"pods\"
    • 你能检查一下你在 kubeconfig 中拥有的客户端证书吗?和生成的证书对比一下,看看除了CN有什么区别
    【解决方案2】:

    终于找到问题了。它与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 开关和命令时,它显示:

    * 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 
    

    在给出绝对路径后它工作正常,我能够得到输出。

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多