Kubernetes Security & Hardening›19 · Lab: a developer's own kubeconfig, key never shared

Lesson 19 of 20 · Practice: Vault, egress and tenant labs

Lab: a developer's own kubeconfig, key never shared

Onboard a new team end to end, playing both people: the admin creates the namespace, quota and group access, the developer makes her own key and certificate request, the admin signs it and sends back only public files, and the developer builds her own kubeconfig where 'kubectl get nodes' works and nothing else does. Then the day-2 changes: new teammate, more quota, renewal, someone leaving.

Practitioner

Open the lab on GitHublab-02-team-onboarding

Key wordshands-on labonboardingCertificateSigningRequestprivate keykubeconfigClusterRolenodesResourceQuotaday 2OIDC

What this lab is for

Authentication methods shows the certificate-request (CSR) commands. This lab practises the real handoff between two people, where the rule is: the private key never travels. The developer sends a request, the admin sends back a signed certificate, and both are public files. It also covers the access that surprises most teams on day one: seeing the nodes.

 MAYA (her laptop)                                  ADMIN
 1. openssl genrsa → maya.key   (stays here)
 2. openssl req → maya.csr  ──────── send ───────►  3. CSR object, read it, approve
                                                    4. maya.crt + ca.crt + API address
    maya.crt + ca.crt + URL  ◄────── send ────────     (all public)
 5. builds her own kubeconfig → kubectl works

Getting a library card: you fill in the form yourself and keep your PIN to yourself. The librarian stamps the form and hands back a card. The card alone is useless to anyone who doesn't know your PIN.

You can use the kind cluster from lesson 18, or a fresh kind create cluster --name ops-lab.

The request

Field Value
Team, namespace globex → team-globex
Group globex-devs (members: maya)
Quota 4 CPU, 8 GiB requests, 20 pods
Access deploy apps in their namespace, see the nodes

1. Admin: namespace, quota, defaults

kubectl create namespace team-globex
kubectl label namespace team-globex tenant=globex \
  pod-security.kubernetes.io/enforce=baseline pod-security.kubernetes.io/warn=restricted

kubectl apply -f - <<'EOF'
apiVersion: v1
kind: ResourceQuota
metadata: { name: globex-quota, namespace: team-globex }
spec:
  hard: { requests.cpu: "4", requests.memory: 8Gi, limits.cpu: "8", limits.memory: 16Gi, pods: "20",
          services.loadbalancers: "0", services.nodeports: "0" }
---
apiVersion: v1
kind: LimitRange
metadata: { name: globex-defaults, namespace: team-globex }
spec:
  limits:
    - type: Container
      defaultRequest: { cpu: 100m, memory: 128Mi }
      default:        { cpu: 500m, memory: 512Mi }
EOF

baseline blocks the dangerous pods (privileged, host access) but still runs a plain nginx image; warn=restricted shows the team what the stricter level would need. The LimitRange is there because a quota on requests.cpu rejects pods that don't declare requests.

2. Admin: access for the group

# inside their namespace: built-in "edit", bound to the GROUP, not to maya
kubectl create rolebinding globex-devs-edit -n team-globex --clusterrole=edit --group=globex-devs

# "kubectl get nodes" needs more: nodes are cluster-scoped, so a RoleBinding can't grant them
kubectl create clusterrole node-viewer --verb=get,list,watch --resource=nodes
kubectl create clusterrolebinding globex-devs-node-viewer --clusterrole=node-viewer --group=globex-devs

Predict first: without the second part, what does kubectl get nodes say for maya? Forbidden. Namespace access never includes cluster-scoped objects like nodes.

3. Maya: key and request, on her laptop

mkdir -p ~/maya-laptop && cd ~/maya-laptop          # pretend this is her machine
openssl genrsa -out maya.key 2048                   # never leaves this folder
openssl req -new -key maya.key -out maya.csr -subj "/CN=maya/O=globex-devs"

CN becomes her username, O her group. O=globex-devs is what makes step 2's bindings apply to her. She sends maya.csr by ticket or email: it contains only her public key.

4. Admin: read it, sign it, send back public files

mkdir -p ~/admin-inbox && cp ~/maya-laptop/maya.csr ~/admin-inbox/ && cd ~/admin-inbox
openssl req -in maya.csr -noout -subject            # must say CN=maya, O=globex-devs. Read it before approving!

kubectl apply -f - <<EOF
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata: { name: maya }
spec:
  request: $(base64 < maya.csr | tr -d '\n')
  signerName: kubernetes.io/kube-apiserver-client
  expirationSeconds: 604800          # 7 days
  usages: ["client auth"]
EOF
kubectl certificate approve maya
kubectl get csr maya -o jsonpath='{.status.certificate}' | base64 -d > maya.crt

# the cluster's public CA and address
kubectl config view --raw --minify -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' | base64 -d > ca.crt
kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}'; echo

cp maya.crt ca.crt ~/maya-laptop/                   # "send" them: no secrets in either file

Approving a CSR is an admin-level action

You get whatever names the request claims. A CSR with O=system:masters would become a full cluster admin that RBAC can't restrict. Always read the subject first, and keep the right to approve CSRs (certificatesigningrequests/approval) for a very small group.

5. Maya: build her own kubeconfig

In a new terminal, so nothing from the admin's kubeconfig leaks in:

cd ~/maya-laptop
export KUBECONFIG=$PWD/kubeconfig                   # this terminal is now "maya"
SERVER=https://127.0.0.1:51588                      # the address the admin sent (yours will differ)

kubectl config set-cluster ops-lab --server=$SERVER --certificate-authority=ca.crt --embed-certs=true
kubectl config set-credentials maya --client-key=maya.key --client-certificate=maya.crt --embed-certs=true
kubectl config set-context globex --cluster=ops-lab --user=maya --namespace=team-globex
kubectl config use-context globex
chmod 600 kubeconfig

She builds it herself because the private key is already on her machine; an admin-made kubeconfig would mean the admin had her key. On kind the API address is 127.0.0.1, which only works on your own machine; a real cluster has a real address or load-balancer name.

6. Maya: check the result

kubectl auth whoami                       # maya, groups [globex-devs system:authenticated]
kubectl get nodes                         # works (node-viewer)
kubectl create deployment web --image=nginx:1.27 --replicas=2
kubectl get pods                          # in team-globex, her context's namespace
kubectl describe resourcequota            # her usage against 4 CPU / 8Gi / 20 pods

What must fail:

kubectl get pods -n other-team            # Forbidden: another team
kubectl delete resourcequota globex-quota # Forbidden: "edit" can't change quota
kubectl delete node ops-lab-control-plane # Forbidden: node-viewer is read-only

Close the terminal (or unset KUBECONFIG) to be the admin again.

7. Day 2

Event What you do
A new teammate joins They make their own key and CSR with O=globex-devs; you approve. No RBAC change, because the binding is on the group.
The team needs more quota You edit globex-quota. The team can't change it.
Maya's certificate expires (7 days here) She sends a new CSR, gets a new maya.crt, re-runs set-credentials. Same kubeconfig file.
Maya leaves You can't revoke a certificate. Her group is written inside it, so removing her from a group list changes nothing until it expires. Short lifetimes are the only lever.

That last row is why companies use OIDC for people: remove her from the group in the identity provider and her next token simply doesn't have it. See OIDC login for kubectl, and for onboarding many teams through pull requests, Automating tenant onboarding.

8. Clean up

kubectl delete namespace team-globex
kubectl delete clusterrolebinding globex-devs-node-viewer
kubectl delete clusterrole node-viewer
kubectl delete csr maya
rm -rf ~/maya-laptop ~/admin-inbox

Check yourself

1. Why is it safe for maya to email her CSR, and for you to email back her certificate?

Both contain only public information: her public key and names, and the CA's signature over them. Without maya.key, which never left her laptop, neither file lets anyone log in.

2. Which command makes 'kubectl get nodes' work, and why can't a RoleBinding do it?

The ClusterRoleBinding to node-viewer. Nodes are cluster-scoped; a RoleBinding only grants rights inside its own namespace.

3. A second developer, raj, joins globex. What do you change in RBAC?

Nothing. raj's certificate says O=globex-devs, and the bindings are on that group.

4. How would you end maya's access today, before her certificate expires?

With certificates there's no clean way: the group is inside the certificate. You'd have to remove the group's bindings (affecting everyone in it) or rotate the cluster CA. That's why certificates for people should be short-lived and why OIDC is the better choice.

Recap

  • The private key never travels: the developer sends a CSR, the admin returns the certificate, CA and address.
  • Read the CSR subject before approving; approving is an admin-level power.
  • Bind groups, not users; add a ClusterRole for cluster-scoped things like nodes.
  • Certificates can't be revoked: keep them short, and use OIDC for people.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.