Kubernetes Security & Hardening›Practice: Vault, egress and tenant labs · Cheat sheet & self-check

Practice: Vault, egress and tenant labs · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

16 · Vault on Kubernetes: login, delivery and rotation

Connect Kubernetes

vault auth enable -path=k8s-prod kubernetesOne Kubernetes auth mount per cluster
vault write auth/k8s-prod/config kubernetes_host=... kubernetes_ca_cert=@ca.crt token_reviewer_jwt=...Tell Vault how to check tokens (TokenReview)
vault policy write payments-api policy.hclWhat may be read
vault write auth/k8s-prod/role/payments-api bound_service_account_names=... bound_service_account_namespaces=... audience=vault token_policies=...Who gets that policy
kubectl create token payments-api -n payments --audience=vaultA short token made for Vault, for testing

KV v2

vault kv put -mount=kv payments/db username=u password=@/dev/stdinWrite a new version (replaces all fields)
vault kv patch -mount=kv payments/db password=...Change one field, keep the rest
vault kv put -mount=kv -cas=3 payments/db ...Write only if the current version is 3
vault kv metadata get -mount=kv payments/dbList versions
vault kv rollback -mount=kv -version=2 payments/dbGo back to version 2

Delivery patterns

vault.hashicorp.com/agent-inject: "true"Agent Injector: sidecar renders files in /vault/secrets
SecretProviderClass (provider: vault)CSI provider: tmpfs volume, not in etcd
VaultAuth + VaultStaticSecret / VaultDynamicSecretVault Secrets Operator: syncs into a Kubernetes Secret

17 · Blocking internet egress by pod label

Policies

CiliumClusterwideNetworkPolicy + egressDeny toCIDRSet 0.0.0.0/0 except private rangesCilium: cluster-wide internet deny that beats namespace allows
enableDefaultDeny: { egress: false, ingress: false }Cilium: add only the deny, don't switch the pods to default-deny
Tier (order: 100) + GlobalNetworkPolicy with Pass for private ranges, then DenyCalico: the same, evaluated before namespace policies
labels: { internet-access: "true" } on the pod templateAllow one workload out

Test

kubectl run t1 --rm -it --restart=Never --image=curlimages/curl -- curl -m5 -sI https://example.comWithout the label: expect a timeout
kubectl run t2 ... -l internet-access=true -- curl -m5 -sI https://example.comWith the label: expect 200
kubectl get ciliumclusterwidenetworkpoliciesList Cilium cluster-wide policies
kubectl get globalnetworkpolicies.crd.projectcalico.orgList Calico global policies (or use calicoctl)

18 · Lab: attack your own tenant setup

Test access

kubectl auth whoamiWho the API server thinks you are, with groups
kubectl auth can-i --list -n acme-dev --as=bob --as-group=acme-devsEverything a user + group may do (always pass the group)
kubectl --context bob@ops-lab ...Test with the user's real certificate
kubectl create token acme-ci -n acme-prod --duration=10mShort-lived ServiceAccount token for testing

Find blocked workloads

kubectl get events -A --field-selector reason=FailedCreatePods refused by quota, LimitRange, Pod Security or a webhook
kubectl describe resourcequota -n acme-devUsed vs hard limits
kubectl describe limitrange -n acme-devDefaults, min and max per container

Clean up

kubectl delete csr alice bobRemove the certificate requests
kubectl config delete-context alice@ops-lab && kubectl config delete-user aliceRemove the kubeconfig entries
kind delete cluster --name ops-labRemove the lab cluster

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

Developer side

openssl genrsa -out maya.key 2048Private key: never leaves the laptop
openssl req -new -key maya.key -out maya.csr -subj "/CN=maya/O=globex-devs"Request: CN = username, O = group
kubectl config set-cluster ops-lab --server=$SERVER --certificate-authority=ca.crt --embed-certs=trueCluster entry from the public CA and address
kubectl config set-credentials maya --client-key=maya.key --client-certificate=maya.crt --embed-certs=trueUser entry from key + signed certificate
kubectl config set-context globex --cluster=ops-lab --user=maya --namespace=team-globexJoin them, with a default namespace

Admin side

openssl req -in maya.csr -noout -subjectRead the request before approving
kubectl certificate approve mayaApprove the CSR object
kubectl get csr maya -o jsonpath='{.status.certificate}' | base64 -d > maya.crtGet the signed certificate
kubectl create clusterrole node-viewer --verb=get,list,watch --resource=nodesRead-only access to nodes
kubectl create clusterrolebinding globex-devs-node-viewer --clusterrole=node-viewer --group=globex-devsGrant it to the group, cluster-wide

20 · Knowledge check: Kubernetes security

Commands behind the answers

kubectl auth whoamiWho the API server thinks you are
kubectl auth can-i --list -n <ns> --as=<user> --as-group=<group>Everything an identity may do
kubectl get events -A --field-selector reason=FailedCreateWorkloads refused at admission
kubeadm certs check-expirationControl-plane certificate expiry
kubectl create token <sa> --duration=10mShort-lived ServiceAccount token
kubectl get csrPending and approved certificate requests