Sub topic 8
Giving the deployer only the permissions it needs
This chapter assumes you know core Kubernetes objects (Namespace, Deployment, ServiceAccount), can read YAML, and have used kubectl. It also assumes you have seen a workflow that deploys with a kubeconfig secret.
Why the deployer needs a small role
The identity in your kubeconfig decides what a leaked credential can do. If the workflow uses a cluster-admin credential, anyone who steals it can read every Secret and delete every namespace. A deploy job only needs to change one Deployment's image. Kubernetes RBAC (role-based access control) lets you grant exactly that and nothing more.
RBAC has three parts:
- A ServiceAccount is the identity the pipeline uses.
- A Role lists allowed actions on resources inside one namespace.
- A RoleBinding attaches a Role to a subject, such as a ServiceAccount.
A ClusterRole and ClusterRoleBinding work the same way but apply cluster-wide. For a deployer, prefer the namespaced versions.
The manifests
This file creates the identity, the Role, and the binding in the shop namespace.
apiVersion: v1
kind: ServiceAccount
metadata:
name: github-deployer
namespace: shop
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployer
namespace: shop
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "patch", "update"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: deployer
namespace: shop
subjects:
- kind: ServiceAccount
name: github-deployer
namespace: shop
roleRef:
kind: Role
name: deployer
apiGroup: rbac.authorization.k8s.ioEach rule has three fields:
apiGroupsnames the API group. Deployments live inapps. Core objects such as Pods use an empty string,"".resourcesnames the object types.verbsnames the allowed actions.
kubectl set image reads the Deployment and then patches it, so get and patch are required. kubectl rollout status watches the Deployment, which needs get, list, and watch. update covers tools that replace the whole object.
Check the result before you trust it
Use kubectl auth can-i with --as to test the identity. You need admin rights to impersonate.
SA=system:serviceaccount:shop:github-deployer
kubectl auth can-i patch deployments -n shop --as "$SA"
kubectl auth can-i delete deployments -n shop --as "$SA"
kubectl auth can-i get secrets -n shop --as "$SA"
kubectl auth can-i patch deployments -n other --as "$SA"The first command should print yes. The other three should print no.
Common errors
forbiddenonrollout status. The Role lackswatchorlist. Add the missing verb.forbiddenon a different resource.kubectl rollout statusalso looks at ReplicaSets in some versions and setups. If you see this, addreplicasetsin theappsgroup withget,list, andwatch. Add only what the error names.- Wrong namespace. A Role grants nothing outside its own namespace. A RoleBinding in
shopcannot grant access inother. - Binding the wrong subject. The
namespaceinsubjectsmust match the ServiceAccount's namespace.
Getting a credential for the ServiceAccount
A ServiceAccount does not come with a long-lived token on current Kubernetes versions. For a kubeconfig secret, request a token with kubectl create token github-deployer -n shop --duration=24h. Short lifetimes limit damage, but you must rotate the secret. The OIDC chapter shows how to remove that chore.
Exercise
A deploy job runs kubectl set image and kubectl rollout status in namespace shop. Which verbs does the Role need on deployments in the apps group, and why is a RoleBinding in shop not enough to let the job deploy to namespace staging?
Show answer
It needs get and patch for set image, and get, list, and watch for rollout status. A RoleBinding only applies to the namespace it is created in, and the Role it references grants rights only there. To deploy to staging, create a Role and RoleBinding in staging as well.