如何查看 Group kind 的 subject 成员

How to view members of subject with Group kind

有一个名为 cluster-admin 的默认值 ClusterRoleBinding
当我 运行 kubectl get clusterrolebindings cluster-admin -o yaml 我得到:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  creationTimestamp: 2018-06-13T12:19:26Z
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: cluster-admin
  resourceVersion: "98"
  selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/cluster-admin
  uid: 0361e9f2-6f04-11e8-b5dd-000c2904e34b
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: Group
  name: system:masters

subjects 字段中我有:

- apiGroup: rbac.authorization.k8s.io
  kind: Group
  name: system:masters

如何查看群组 system:masters 的成员?
我阅读了 here 关于群组的内容,但我不明白如何才能看到群组中的成员,如上面 system:masters 的示例。

我注意到当我使用命令解码 /etc/kubernetes/pki/apiserver-kubelet-client.crt 时: openssl x509 -in apiserver-kubelet-client.crt -text -noout 它包含主题 system:masters 但我仍然不明白这个组中的用户是谁:

Issuer: CN=kubernetes
Validity
    Not Before: Jul 31 19:08:36 2018 GMT
    Not After : Jul 31 19:08:37 2019 GMT
Subject: O=system:masters, CN=kube-apiserver-kubelet-client
Subject Public Key Info:
    Public Key Algorithm: rsaEncryption
        Public-Key: (2048 bit)
        Modulus:

答案已更新

好像没办法用kubectl。在 Kubernetes 配置中没有像 Group 这样的对象可以 "get"。

Kubernetes 中的组信息目前由 Authenticator 模块提供,通常它只是用户中的字符串 属性。

也许您可以从用户证书的主题中获取组列表,或者如果您使用 GKE、EKS 或 AKS,则组属性存储在云用户管理系统中。

https://kubernetes.io/docs/reference/access-authn-authz/rbac/ https://kubernetes.io/docs/reference/access-authn-authz/authentication/

可以从 ClusterRoleBinding 对象请求有关系统组中 ClusterRole 成员身份的信息。 (例如,对于 "system:masters",它仅显示 cluster-admin ClusterRole):

使用 jq:

kubectl get clusterrolebindings -o json | jq -r '.items[] | select(.subjects[0].kind=="Group") | select(.subjects[0].name=="system:masters")'

如果只想列出姓名:

kubectl get clusterrolebindings -o json | jq -r '.items[] | select(.subjects[0].kind=="Group") | select(.subjects[0].name=="system:masters") | .metadata.name'

使用 go-templates:

kubectl get clusterrolebindings -o go-template='{{range .items}}{{range .subjects}}{{.kind}}-{{.name}} {{end}} {{" - "}} {{.metadata.name}} {{"\n"}}{{end}}' | grep "^Group-system:masters"

有关系统组的一些附加信息可以在 GitHub issue #44418 or in RBAC document:

中找到

不可否认,这里的聚会迟到了。

通读 the Kubernetes 'Authenticating' docs。 Kubernetes 没有用于定义和控制用户的内置机制(与用于为 Pods 提供集群身份的 ServiceAccounts 不同,因此服务 运行 在他们身上)。

这意味着 Kubernetes 因此没有任何内部数据库可供参考,以确定和显示组成员身份。

在较小的集群中,x509 证书通常用于对用户进行身份验证。 API 服务器配置为为此目的信任 CA,然后用户将获得由该 CA 签名的证书。正如您所注意到的,如果主题包含 'Organisation' 字段,则该字段将映射到 Kubernetes 组。如果您希望一个用户成为多个组的成员,那么您可以指定多个 'O' 字段。 (顺便说一句,在我看来,使用 'OU' 字段会更有意义,但事实并非如此)

在回答您的问题时,似乎在用户通过证书进行身份验证的集群的情况下,您唯一的途径是访问颁发的证书,并检查 'O' 主题中的字段。我想在更高级的情况下,Kubernetes 将与集中式工具(如 AD)集成,可以在本地查询组成员资格。