<p>I need to create a kubeconfig with restricted access, I want to be able to provide permission to update configmap in specific namesapce, how can I create such a kubeconfig with the following permission </p>

<blockquote>
  <ol>
  <li>for specefic namespace (myns) </li>
  <li>update only configmap (mycm)</li>
  </ol>
</blockquote>

<p>Is there a simple way to create it ?</p>

<p>The <strong>tricky part</strong> here is that I need that some program will have access to cluster X and modify only this comfigMap, How would I do it from outside process without providing the full kubeconfig file which can be problematic from security reason</p>

<p>To make it clear, I own the cluster, I just want to give to some program restricted permissions</p>

<p>This is not straight forward. But still possible.</p>

<p>Create the namespace myns if not exists.</p>

<pre><code>$ kubectl create ns myns
namespace/myns created
</code></pre>

<p>Create a service account cm-user in myns namespace. It'll create a secret token as well.</p>

<pre><code>$ kubectl create sa cm-user -n myns
serviceaccount/cm-user created

$  kubectl get sa cm-user -n myns
NAME      SECRETS   AGE
cm-user   1         18s

$ kubectl get secrets -n myns
NAME                  TYPE                                  DATA   AGE
cm-user-token-kv5j5   kubernetes.io/service-account-token   3      63s
default-token-m7j9v   kubernetes.io/service-account-token   3      96s
</code></pre>

<p>Get the token and ca.crt from <code>cm-user-token-kv5j5</code> secret.</p>

<pre><code>$ kubectl get secrets cm-user-token-kv5j5 -n myns -oyaml
</code></pre>

<p>Base64 decode the value of token from <code>cm-user-token-kv5j5</code>.</p>

<p>Now create a user using the decoded token.</p>

<pre><code>$ kubectl config set-credentials cm-user --token=&lt;decoded token value&gt;
User "cm-user" set.
</code></pre>

<p>Now generate a kubeconfig file kubeconfig-cm.</p>

<pre><code>apiVersion: v1
kind: Config
clusters:
- cluster:
    certificate-authority-data: &lt;ca.crt value from cm-user-token-kv5j5 secret&gt;
    server: &lt;kubernetes server&gt;
  name: &lt;cluster&gt;
contexts:
- context:
    cluster:&lt;cluster&gt;
    namespace: myns
    user: cm-user
  name: cm-user
current-context: cm-user 
users:
- name: cm-user
  user:
    token: &lt;decoded token&gt;
</code></pre>

<p>Now create a role and rolebinding for sa cm-user.</p>

<pre><code>---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: myns
  name: cm-user-role
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["update", "get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cm-user-rb
  namespace: myns
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: cm-user-role
subjects:
- namespace: myns
  kind: ServiceAccount
  name: cm-user
</code></pre>

<p>We are done. Now using this kubeconfig file you can update the mycm configmap. It doesn't have any other privileges.</p>

<pre><code>$ kubectl get cm -n myns --kubeconfig kubeconfig-cm
NAME                 DATA   AGE
mycm                 0      8s

$ kubectl delete cm mycm -n myns --kubeconfig kubeconfig-cm
Error from server (Forbidden): configmaps "mycm" is forbidden: User "system:serviceaccount:myns:cm-user" cannot delete resource "configmaps" in API group "" in the namespace "myns"
</code></pre>
