Practice: Vault, egress and tenant labs · wrap-up
Cheat sheet & self-check
Every command from this section on one page.
Connect Kubernetes
vault auth enable -path=k8s-prod kubernetes | One 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.hcl | What 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=vault | A short token made for Vault, for testing |
KV v2
vault kv put -mount=kv payments/db username=u password=@/dev/stdin | Write 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/db | List versions |
vault kv rollback -mount=kv -version=2 payments/db | Go 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 / VaultDynamicSecret | Vault Secrets Operator: syncs into a Kubernetes Secret |
Policies
CiliumClusterwideNetworkPolicy + egressDeny toCIDRSet 0.0.0.0/0 except private ranges | Cilium: 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 Deny | Calico: the same, evaluated before namespace policies |
labels: { internet-access: "true" } on the pod template | Allow one workload out |
Test
kubectl run t1 --rm -it --restart=Never --image=curlimages/curl -- curl -m5 -sI https://example.com | Without the label: expect a timeout |
kubectl run t2 ... -l internet-access=true -- curl -m5 -sI https://example.com | With the label: expect 200 |
kubectl get ciliumclusterwidenetworkpolicies | List Cilium cluster-wide policies |
kubectl get globalnetworkpolicies.crd.projectcalico.org | List Calico global policies (or use calicoctl) |
Test access
kubectl auth whoami | Who the API server thinks you are, with groups |
kubectl auth can-i --list -n acme-dev --as=bob --as-group=acme-devs | Everything 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=10m | Short-lived ServiceAccount token for testing |
Find blocked workloads
kubectl get events -A --field-selector reason=FailedCreate | Pods refused by quota, LimitRange, Pod Security or a webhook |
kubectl describe resourcequota -n acme-dev | Used vs hard limits |
kubectl describe limitrange -n acme-dev | Defaults, min and max per container |
Clean up
kubectl delete csr alice bob | Remove the certificate requests |
kubectl config delete-context alice@ops-lab && kubectl config delete-user alice | Remove the kubeconfig entries |
kind delete cluster --name ops-lab | Remove the lab cluster |
Developer side
openssl genrsa -out maya.key 2048 | Private 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=true | Cluster entry from the public CA and address |
kubectl config set-credentials maya --client-key=maya.key --client-certificate=maya.crt --embed-certs=true | User entry from key + signed certificate |
kubectl config set-context globex --cluster=ops-lab --user=maya --namespace=team-globex | Join them, with a default namespace |
Admin side
openssl req -in maya.csr -noout -subject | Read the request before approving |
kubectl certificate approve maya | Approve the CSR object |
kubectl get csr maya -o jsonpath='{.status.certificate}' | base64 -d > maya.crt | Get the signed certificate |
kubectl create clusterrole node-viewer --verb=get,list,watch --resource=nodes | Read-only access to nodes |
kubectl create clusterrolebinding globex-devs-node-viewer --clusterrole=node-viewer --group=globex-devs | Grant it to the group, cluster-wide |
Commands behind the answers
kubectl auth whoami | Who 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=FailedCreate | Workloads refused at admission |
kubeadm certs check-expiration | Control-plane certificate expiry |
kubectl create token <sa> --duration=10m | Short-lived ServiceAccount token |
kubectl get csr | Pending and approved certificate requests |