Give Teammates and CI a Scoped Kubeconfig, and Lock Down the Namespace They Get
Sooner or later someone who is not you needs access to your cluster: a contractor who should only touch the staging namespace, a CI pipeline that deploys one app, a colleague who needs to read logs. The shortcut is to send them your own kubeconfig, which is how admin credentials end up in chat history and CI variables. The right answer is a dedicated identity with exactly the access required, and a kubeconfig built for it. Doing that by hand is a fiddly, easy-to-get-wrong sequence of kubectl commands. And even when you get it right, there is a gap that RBAC does not close.
How people do it today
The manual recipe has about six steps:
- Create a ServiceAccount in the target namespace.
- Write a Role (or pick a ClusterRole) with the verbs and resources you want to allow.
- Create a RoleBinding tying the ServiceAccount to the role, in each namespace you want to grant.
- Mint a token, either a short-lived one with
kubectl create tokenor a long-lived one from a Secret of typekubernetes.io/service-account-token. - Assemble a kubeconfig by hand: cluster server URL, CA data, the token, a context.
- Remember, months later, which ServiceAccounts you created and delete them when the contractor leaves.
The failure modes are predictable: a RoleBinding in the wrong namespace, a role that quietly includes Secrets, a long-lived token nobody tracked, a grant that is never revoked. For the broader picture of auditing this kind of access, see Who Has Access to What?
The gap RBAC does not close
Suppose you carefully grant a teammate edit access in a single namespace. They cannot read other namespaces. Good. But "edit" includes creating pods, and a pod spec can ask for a lot. If the namespace has no Pod Security enforcement, a pod can run privileged, mount hostPath volumes, or join the host network. That is a well-known path from "can create pods in one namespace" to "root on the node", and from there to other tenants' workloads. RBAC says who may create pods. It says nothing about what those pods may be. The same goes for capacity: with no quota, one namespace can request enough CPU and memory to starve the node for everyone else.
Two Kubernetes mechanisms cover this. Pod Security Admission enforces a Pod Security Standard on a namespace through the label pod-security.kubernetes.io/enforce. A ResourceQuota caps total consumption in a namespace, and a LimitRange supplies default container requests and limits, which a quota on limits needs, otherwise pods without limits are rejected.
Scoped Kubeconfigs in Kubexer
Kubexer's Access Control → Scoped Kubeconfigs page automates the recipe above. You choose a subject name, one or more namespaces and an access level, and Kubexer creates the ServiceAccount and the RBAC, mints a token, and produces a kubeconfig you download and hand over. The access level is a preset or a custom selection:
- Read-only: get, list and watch on all resources.
- Edit: read everything, write common workloads, no RBAC or Secret writes.
- Admin: full access within the namespace.
- Custom: pick specific resources and verbs.
Tokens can be long-lived or short-lived with a chosen expiry, and Kubexer does not store the token; it exists only in the kubeconfig you download. Names are derived from the grant so you can find them again, and editing a preset cannot be used to escalate its own privileges. Revoke tears down the ServiceAccount and its RBAC, and any kubeconfig using it stops working immediately. Scoped Kubeconfigs are a Pro feature.
Lock down the namespaces, on by default
New in the form is a Lock down namespaces option, on by default for every access level. It closes the gap above before any access exists:
- Pod Security (enforce): sets
pod-security.kubernetes.io/enforceon each selected namespace to Baseline (the default) or Restricted, or Off if you want the namespace's current label left alone. Baseline blocks privileged containers,hostPathvolumes, host networking and PID, and added capabilities. Restricted adds running as non-root, dropping all capabilities and a default seccomp profile. Kubexer records which grant changed the label and what the previous value was. - Resource quota plus default container limits: creates a ResourceQuota and a LimitRange owned by the grant. The defaults are 1 CPU, 1Gi of memory and 1 pod for the quota, and 500m CPU and 512Mi memory limits with 100m and 128Mi requests per container. All of them are editable, and the quota applies to both requests and limits. You can untick it if you only want Pod Security. Note that the default quota allows a single pod, so raise it for anything beyond a smoke test.
A quota object looks like this once created, with the grant ID in the name:
apiVersion: v1
kind: ResourceQuota
metadata:
name: contractor-staging-quota
namespace: staging
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "1"
limits.memory: 1Gi
pods: "1"
The name above is illustrative; Kubexer derives the real one from the grant.
A dry run before anything changes
Tightening Pod Security on a namespace that already has workloads can be a surprise. So the form dry-runs the lock-down against the API server first and shows what would happen per namespace: the label being set or changed (with the old value that will be restored on revoke), existing quotas and limit ranges that will remain enforced alongside the new ones, and any error. If existing pods would violate the chosen Pod Security level, the API server's warnings are listed, and Generate stays disabled until you decide: apply anyway (existing pods keep running, new ones that violate the level are rejected), switch to Baseline, or turn Pod Security off. If the dry run cannot be performed, generation is blocked rather than attempted blindly. The form also lists every object that will be created or labelled before you press the button.
Revoke undoes the lock-down too
When you revoke or delete a grant, a checkbox, on by default, offers to remove the lock-down it applied. The quota and limit range the grant created are deleted. A Pod Security label is restored to its previous value only if it is still what Kubexer set and the namespace still says this grant set it; if someone changed it since, it is left alone and you are told why. Labels, quotas and limit ranges that existed before the grant are never removed.
A typical CI example
Say your pipeline deploys one app into shop-staging. Create a scoped kubeconfig named for the pipeline, select shop-staging, choose Edit, pick a short-lived token that matches your rotation, keep Lock down namespaces on with Baseline, and raise the pod count and CPU in the quota to match your app. Download the kubeconfig and store it as a CI secret. The pipeline can deploy workloads in that namespace and nothing else; it cannot write RBAC or Secrets, cannot start a privileged pod, and cannot consume more than the quota.
Honest limits
- You need enough permission to create the ServiceAccount, RBAC and (for the lock-down) to label namespaces and create quotas. Kubexer runs a preflight check and disables Generate when it cannot confirm access, rather than failing halfway.
- Pod Security Admission needs a cluster version that has it enabled (stable since Kubernetes 1.25). The grantee still holds a bearer token: treat the downloaded file as a secret, and prefer short-lived tokens where you can.
- Baseline and Restricted are blunt tools. Some legitimate workloads need exceptions, and per-workload exemptions are a cluster-level configuration this form does not manage.
- A ResourceQuota is a cap, not a guarantee. It does not stop a determined user from filling the allowed amount, and it does not replace network policies, admission policies or auditing.
- Read-only access still means reading everything in the namespace, including ConfigMaps. Check with the Security Matrix if you are unsure what a preset reaches.
Wrapping up
Least privilege has two halves: what an identity may ask the API server to do, and what the namespace will accept. Scoped kubeconfigs cover the first without sharing your own credentials, and namespace lock-down covers the second with Pod Security enforcement and a quota, previewed with a server-side dry run and undone on revoke. Download Kubexer to set it up for your next contractor or pipeline.