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
As mention by Shai Katz in previous answer according to documentation: Using RBAC Authorization - Referring to resources
Solution provided in the edit section - still will not work.
This config allows you to only get and update existing configmap named
aaawhile the create operation is pointless as this resource cannot be created..How can I restrict the serviceAccount's roles so that this pod can manipulate only the named configmap
You can achieve what you want if you will edit your Role
Solution 1
It can be verified by executing commands below:
However, in this scenario
rbac-test pod/sacan performupdateaction by executingkubectl replacecommand f.e:$ kubectl replace -f /config/aaa.yamlI you are interested with
$ kubectl applyprobably you should use verbpatchlike in the example below.Solution 2 - More restrictive approach.
Instead of giving your
serviceAccount: rbac-testpermission to create configs in k8s api. Please consider creating two configmaps:a)
b)
This
configmapwill be populated to thePODusing volumes.Configmap.datawill be stored inside thepodsdirectory under/config/aaa.yamlConfigure this
RBAC Role:In this scenario you will have preconfigured
aaaemptyconfigmapand yourpodwith associatedrbac-test serviceAccountwill be able to performpatchrequest using provided YAML manifest stored in/config/aaa.yamlinside thePod.To check which verbs you can use in
RBACforConfigMapexecute: