Kubernetes Security & Hardening›Workload & Supply Chain · Cheat sheet & self-check

Workload & Supply Chain · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

10 · Pod Security & admission control

Pod Security Admission (namespace labels)

kubectl label ns shop pod-security.kubernetes.io/enforce=restrictedReject non-compliant pods
kubectl label ns shop pod-security.kubernetes.io/warn=restrictedWarn clients but allow
kubectl label ns shop pod-security.kubernetes.io/audit=restrictedRecord violations in the audit log
kubectl label --dry-run=server --overwrite ns shop pod-security.kubernetes.io/enforce=restrictedPreview which existing pods would violate

Custom policy

kubectl get validatingadmissionpoliciesBuilt-in CEL policies
kubectl get clusterpoliciesKyverno policies
kubectl get constrainttemplates,constraintsGatekeeper policies

11 · Secrets management

Encryption at rest

--encryption-provider-config=/etc/kubernetes/enc/enc.yamlkube-apiserver flag pointing at the EncryptionConfiguration
etcdctl … get /registry/secrets/<ns>/<name> | hexdump -C | headCheck the raw value is encrypted (k8s:enc:… prefix)
kubectl get secrets -A -o json | kubectl replace -f -Rewrite all Secrets so they're re-encrypted with the current key

Getting secrets into the cluster

kubectl get externalsecrets -AExternal Secrets Operator: sync status
kubeseal --format yaml < secret.yaml > sealed.yamlSealed Secrets: encrypt for Git
vault kv get secret/shop/dbRead a secret from Vault (CLI)

12 · Supply chain security

Inspect images

syft registry.example.com/shop/api:1.4.2 -o spdx-json > sbom.jsonGenerate an SBOM
trivy image --severity HIGH,CRITICAL registry.example.com/shop/api:1.4.2Scan for known vulnerabilities
trivy sbom sbom.jsonScan an existing SBOM
crane digest registry.example.com/shop/api:1.4.2The immutable digest behind a tag

Sign & verify (cosign)

cosign generate-key-pairCreate cosign.key / cosign.pub (or use keyless signing)
cosign sign --key cosign.key <image>@sha256:<digest>Sign an image by digest
cosign verify --key cosign.pub <image>@sha256:<digest>Verify the signature
cosign attest --key cosign.key --type spdxjson --predicate sbom.json <image>@sha256:<digest>Attach a signed SBOM attestation