正确使用 Role.rules.resourceNames 创建对资源访问权限有限的 pod

lit*_*tle 9 kubernetes kubernetes-pod kubernetes-rbac

我正在尝试创建一个能够使用 来创建和更新特定配置映射的 Pod Role.rules.resourceNames。我能够从 Pod 内向 API 执行资源获取请求,但我无法创建资源,而是获取

$ kubectl logs rbac-test
Error from server (Forbidden): error when creating "/config/aaa.yaml": configmaps is forbidden: User "system:serviceaccount:default:rbac-test" cannot create resource "configmaps" in API group "" in the namespace "default"
Run Code Online (Sandbox Code Playgroud)

如果我删除resourceNames属性,我就能够创建配置映射,但我不一定希望这个 pod 能够随意创建和更新配置映射。如何限制 serviceAccount 的角色,以便该 pod 只能操作指定的 configmap aaa

编辑

我在 kubernetes slack 中得到了一些帮助(谢谢你,艾伦)。RBAC 发生在 URL 级别,因此创建 URL 时无法进行资源名称限制授权,因为 URL 中没有资源名称!

$ kubectl logs rbac-test
Error from server (Forbidden): error when creating "/config/aaa.yaml": configmaps is forbidden: User "system:serviceaccount:default:rbac-test" cannot create resource "configmaps" in API group "" in the namespace "default"
Run Code Online (Sandbox Code Playgroud)

Sha*_*atz 5

注意:您不能通过资源名称限制创建或删除集合请求。对于create,此限制是因为授权时不知道对象名称。

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


Pjo*_*erS 5

正如 Shai Katz 在之前的回答中根据文档提到的:使用 RBAC 授权 - 参考资源

您不能通过 进行限制createdeletecollection请求resourceName。对于create,此限制是因为授权时不知道对象名称。

编辑部分提供的解决方案 - 仍然不起作用。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: rbac-test
rules:
  - apiGroups: [ "" ]
    resources: [ "configmaps" ]
    verbs: [ "get", "create", "update" ]
    resourceNames: [ "aaa" ]
Run Code Online (Sandbox Code Playgroud)

此配置允许您仅获取和更新指定的现有配置映射aaa,而创建操作毫无意义,因为无法创建此资源。

如何限制 serviceAccount 的角色,以便该 pod 只能操作指定的 configmap

如果您编辑您的角色,您就可以实现您想要的目标

解决方案1

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: rbac-test
rules:
  - apiGroups: [ "" ]             #this rule will allow to update existing configmap aaa referenced by resourcename 
    resources: [ "configmaps" ]
    verbs: [ "get", "update" ]    
    resourceNames: [ "aaa" ]
  - apiGroups: [ "" ]             #this rule will allow you to create configmaps
    resources: [ "configmaps" ]
    verbs: [ "get", "create" ]   
Run Code Online (Sandbox Code Playgroud)

可以通过执行以下命令来验证:

$ kubectl auth can-i update configmaps/aaa --as=system:serviceaccount:default:rbac-test
yes
$ kubectl auth can-i create configmaps/new-aaa --as=system:serviceaccount:default:rbac-test
yes
Run Code Online (Sandbox Code Playgroud)

然而,在这种情况下rbac-test pod/sa可以update通过执行kubectl replace命令 fe 来执行操作:

$ kubectl replace -f /config/aaa.yaml

如果您感兴趣,$ kubectl apply您可能应该使用动词,patch如下例所示。

  - apiGroups: [ "" ]       
    resources: [ "configmaps" ]
    verbs: [ "get", "patch" ]    
    resourceNames: [ "aaa" ]
Run Code Online (Sandbox Code Playgroud)

解决方案 2 - 更具限制性的方法。

而不是授予您serviceAccount: rbac-test在 k8s api 中创建配置的权限。请考虑创建两个配置映射:

A)

$ kubectl create cm aaa ### an empty "aaa" configmap
Run Code Online (Sandbox Code Playgroud)

b)

kubectl apply -f - <<EOF
kind: ConfigMap
apiVersion: v1
metadata:
  name: aaa-config
data:
  aaa.yaml: |
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: aaa
    data:
      value: na
EOF
Run Code Online (Sandbox Code Playgroud)

configmap将被填充到POD使用中。Configmap.data将存储在pods以下目录中/config/aaa.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: aaa
data:
  value: na
Run Code Online (Sandbox Code Playgroud)

配置这个RBAC Role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: rbac-test
rules:
  - apiGroups: [ "" ]             #this rule will allow to patch existing empty "aaa" configmap aaa referenced by resourcename 
    resources: [ "configmaps" ]
    verbs: [ "get", "patch" ]    
    resourceNames: [ "aaa" ]
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将预先配置aaa为空configmap,并且您的pod相关人员rbac-test serviceAccount将能够patch使用存储/config/aaa.yamlPod.

$ kubectl auth can-i patch configmaps/aaa --as=system:serviceaccount:default:rbac-test
yes
$ kubectl auth can-i create configmaps/new-aaa --as=system:serviceaccount:default:rbac-test
no
Run Code Online (Sandbox Code Playgroud)

要检查可以在执行中使用哪些RBAC动词ConfigMap

$ kubectl api-resources -o wide | grep configmap
configmaps                        cm                                          true         ConfigMap                        [create delete deletecollection get list patch update watch]
Run Code Online (Sandbox Code Playgroud)