我正在尝试创建一个能够使用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"
如果我删除该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