I am trying to create a Pod that is able to create and update a specific configmap using Role.rules.resourceNames
. I am able to perform a get request for the resource to the API from within the pod, but I'm not able to create the resource, instead getting
$ 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"
If I remove the resourceNames
attribute I am able to create the configmap, but I don't necessarily want this pod to be able to create and update configmaps willy-nilly. How can I restrict the serviceAccount's roles so that this pod can manipulate only the named configmap aaa
?
I got some help in the kubernetes slack (thank you, Alan). RBAC happens at the level of the URL so resourceName-restricted authorization can't happen for create URLs since there is no resource name in the 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