The API request

Every request passes three gates

kubectl apply -f web.yamlkubectlrun by aliceAuthenticationwho are you?Authorisationmay you do this?Admissionis it acceptable?a certificate or a token✗ 401 Unauthorizedthe RBAC rules✗ 403 Forbiddenpolicies and quotas✗ rejected, with a reasonetcdsavedEvery request passes three gates: who are you, may you, and is it acceptable?Whoever sends it, a person, a pipeline or a pod, a request is treated the same way. The APIserver first identifies the caller, then checks that this caller may do this action, then letsadmission controllers inspect the object itself. Failing any gate ends the request there.

Who are you? May you? Is it acceptable?

RBAC

A Role says what; a RoleBinding says who

alicea userRoleBindinggives the role to aliceRole pod-readerget, list, watch on pods, in the namespace shopalice asks toAllowed?Whylist pods -n shop✓yesthe role allows list on podsdelete pods -n shop✗nono rule allows deletelist pods -n tools✗nothe binding covers shop onlyget secrets -n shop✗nothe role names pods onlyA Role lists what may be done. A RoleBinding says who may do it.Rules only ever allow. With nothing granted, nothing is permitted, and there are no deny rulesto reason about. A Role and its binding live in one namespace. ClusterRole andClusterRoleBinding are the same idea for the whole cluster.

A Role lists actions. A RoleBinding gives it to someone.

RBAC · Going deeper · 1 of 2

Lab: grant a permission and test it

TypeLook forkubectl create namespace shopkubectl create role pod-reader --verb=get,list,watch --resource=pods -n shopkubectl create rolebinding alice-reads --role=pod-reader --user=alice -n shopkubectl auth can-i list pods -n shop --as alicekubectl auth can-i delete pods -n shop --as alicekubectl auth can-i list pods -n default --as alicenamespace/shop createdrole pod-reader createdrolebinding alice-reads createdyesnonoLab: grant one permission and test exactly what it allows."kubectl auth can-i" asks the API server the question without doing the action. "--as" asks for someone else.As the cluster's administrator you may impersonate alice. She does not need to exist.

Create a role and a binding, then ask can-i.

RBAC, written down · Going deeper · 2 of 2

The Role and the binding as written, and how far each pair reaches

The Role: what may be doneThe RoleBinding: who may do itapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: pod-reader namespace: shoprules:- apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"]apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: alice-reads namespace: shopsubjects:- kind: User name: alice apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.ioapiGroups "" is the core group: pods, services, secrets.Deployments are in "apps". Logs are the resource pods/log.Ready-made ClusterRoles: view, edit, admin, cluster-admin.A binding joins subjects to one role. Which pair you choose decides how far the rights reach.Role + RoleBinding: one namespace. ClusterRole + RoleBinding: the same rules, but only in the binding's namespace, so onedefinition serves many namespaces. ClusterRole + ClusterRoleBinding: every namespace, plus nodes and other cluster-wide kinds.A subject is a User, a Group or a ServiceAccount. roleRef cannot be edited: delete the binding and make a new one.

rules in the Role; subjects and roleRef in the binding.

Service accounts

Pods have identities too

Pod deployerAPI serverServiceAccount deployerthe pod's identitya token, mounted as a fileshort-lived and renewed for the poda request, with the tokenRBACchecks its rightsWhoever takes over this podgets exactly these rights.Pods have identities too. Give each one only the rights it needs.Every pod runs as a service account: the one you name, or its namespace's "default". Its tokenis placed in the pod, so any program inside can call the API as that account. The default accounthas no rights. The classic mistake is binding a pod's account to cluster-admin to make an error go away.

A pod runs as a service account, and holds its token.

The token · Going deeper

The identity is a file in the pod, good for about an hour

spec: serviceAccountName: deployer automountServiceAccountToken: true$ kubectl exec deployer -- ls /var/run/secrets/kubernetes.io/serviceaccountca.crt namespace tokenleft out: the account named defaulttrue is the default; false mounts nothingtypical outputFileWhat it istokena signed token that names this pod and its account; valid for about 1 hour, and replaced by the kubelet before it expiresca.crtthe certificate authority with which to check the API servernamespacethe pod's own namespaceThe token is short-lived and tied to the pod: delete the pod and the token stops working.To RBAC the pod is the user system:serviceaccount:shop:deployer. Test its rights from outside: kubectl auth can-i createdeployments -n shop --as=system:serviceaccount:shop:deployer. A pod that never calls the API should set automount to false,leaving an intruder no token to take. kubectl create token deployer prints a token for use outside the cluster.

serviceAccountName picks the account. The token is mounted and renewed.

Security context

Limit what a container can do to its node

securityContext: runAsNonRoot: true allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: { drop: [ALL] } seccompProfile: { type: RuntimeDefault }refuse to start as the root userno gaining of extra rights laterthe image's files cannot be alteredgive up root's special kernel powersblock unusual system callsPod Security Standards: three ready-made levelsPrivilegedno restrictions: for system podsBaselineblocks the known dangerous settingsRestricteddemands settings like those aboveA security context limits what a container can do to the node it runs on.Containers share the node's kernel, so a root process that breaks out is root on the node.These settings shrink that risk. A label on a namespace makes the cluster enforce a whole level.

Not root, no escalation, read-only files, no extra powers.

Pod Security Admission · Going deeper

One label on a namespace enforces a level; two more only warn

apiVersion: v1kind: Namespacemetadata: name: shop labels: pod-security.kubernetes.io/enforce: baseline pod-security.kubernetes.io/warn: restricted pod-security.kubernetes.io/audit: restricteda pod that breaks baseline is refuseda pod short of restricted runs, and its author is warnedand the shortfall is written to the audit logLevelWhat it refusesFitsPrivilegednothingsystem pods, as in kube-systemBaselineprivileged containers, host network and PID, hostPath volumes, extra capabilitiesmost applications, unchangedRestrictedall of that, and also: running as root, privilege escalation, capabilities not droppedapplications written for itEnforcement checks pods, not Deployments: the Deployment is accepted, and its pods never appear.kubectl apply succeeds and kubectl get pods shows nothing. The reason is in the ReplicaSet's events: kubectl describe rs showsFailedCreate, with the words violates PodSecurity. So start with warn and audit, read what would fail, and enforce afterwards.This built-in admission controller took the place of PodSecurityPolicy, which was removed in v1.25.

enforce refuses. warn and audit only report.

Images

Check an image before it is allowed to run

Buildfrom sourceScanknown vulnerabilitiesSignwho built itRegistrystored by digestAdmissionverify, then runcompare every package withpublic lists of flaws (CVEs)✗ stop on critical findingsa signature thatcannot be forgedunsigned, or from anunknown registry:✗ refusedMost of what runs in your containers was written by someone else. Check it first.An image carries a base system and hundreds of packages. A scanner such as Trivy lists the knownflaws in them, at build time and again later, because new flaws are found in old software everyday. Signing proves where an image came from, and an admission policy can insist on it.

Scan it, sign it, and verify at the door.

Lesson complete

You finished “Security in the cluster”

Nicely done.

Open in a new tab