【问题标题】:Proper use of Role.rules.resourceNames for creating pods with limited access to resources正确使用 Role.rules.resourceNames 来创建对资源的访问受限的 pod
【发布时间】:2021-03-19 23:49:04
【问题描述】:

我正在尝试创建一个能够使用 Role.rules.resourceNames 创建和更新特定配置映射的 Pod。我能够从 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"

如果我删除 resourceNames 属性,我可以创建 configmap,但我不一定希望这个 pod 能够随意创建和更新 configmap。如何限制 serviceAccount 的角色,以便此 pod 只能操作命名的 configmap aaa

编辑

我在 kubernetes slack 上得到了一些帮助(谢谢你,Alan)。 RBAC 发生在 URL 级别,因此资源名称受限的授权不能用于创建 URL,因为 URL 中没有资源名称!

kind: Pod
apiVersion: v1
metadata:
  name: rbac-test
spec:
  restartPolicy: Never
  serviceAccountName: rbac-test
  imagePullSecrets:
    - name: docker-hub-creds 
  containers:
    - name: rbac-test
      image: bitnami/kubectl
      command: [ "kubectl" ]
      args: [ "apply", "-f", "/config/aaa.yaml" ]
      volumeMounts:
        - name: aaa-config
          mountPath: /config/
  volumes:
    - name: aaa-config
      configMap:
        name: aaa-config

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: aaa-config
data:
  aaa.yaml: |
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: aaa
    data:
      value: na

---
apiVersion: v1     
kind: ServiceAccount
metadata:
  name: rbac-test

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

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: rbac-test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: rbac-test
subjects:
  - kind: ServiceAccount
    name: rbac-test

【问题讨论】:

    标签: kubernetes kubernetes-pod kubernetes-rbac


    【解决方案1】:

    根据文档,正如 Shai Katz 在之前的回答中提到的那样:Using RBAC Authorization - Referring to resources

    您不能通过resourceName 限制createdeletecollection 请求。对于 create,这个限制是因为在授权时对象名称是未知的。

    编辑部分提供的解决方案 - 仍然无效。

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

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

    如何限制 serviceAccount 的角色,以便这个 pod 只能操作命名的 configmap

    如果你编辑你的Role,你可以实现你想要的

    解决方案 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" ]   
    

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

    $ 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
    

    但是,在这种情况下,rbac-test pod/sa 可以通过执行 kubectl replace 命令 f.e 来执行 update 操作:

    $ kubectl replace -f /config/aaa.yaml

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

      - apiGroups: [ "" ]       
        resources: [ "configmaps" ]
        verbs: [ "get", "patch" ]    
        resourceNames: [ "aaa" ]
    

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

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

    一)

    $ kubectl create cm aaa ### an empty "aaa" configmap
    

    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
    

    configmap 将使用volumes 填充到PODConfigmap.data 将存储在pods 目录下/config/aaa.yaml

    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: aaa
    data:
      value: na
    

    配置这个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" ]
    

    在这种情况下,您将预先配置 aaaconfigmap 并且您的 pod 与关联的 rbac-test serviceAccount 将能够使用存储在 Pod 内的 /config/aaa.yaml 中提供的 YAML 清单执行 patch 请求.

    $ 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
    

    要检查您可以在RBAC 中为ConfigMap 使用哪些动词,请执行:

    $ kubectl api-resources -o wide | grep configmap
    configmaps                        cm                                          true         ConfigMap                        [create delete deletecollection get list patch update watch]
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 2021-01-14
        • 1970-01-01
        • 2011-10-14
        • 2010-12-18
        • 1970-01-01
        • 2020-09-30
        相关资源
        最近更新 更多