Catch It Before Production
Shift-left security means finding misconfigurations in CI/CD โ not in production incident reports. Policy-as-code tools make this automated, consistent, and fast.
The Policy-as-Code Stack
Kyverno: Kubernetes-Native Policies
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-resource-limits
spec:
validationFailureAction: Enforce
rules:
- name: check-limits
match:
any:
- resources:
kinds:
- Pod
validate:
message: "CPU and memory limits are required"
pattern:
spec:
containers:
- resources:
limits:
memory: "?*"
cpu: "?*"OPA/Gatekeeper: Flexible Policy Engine
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sDisallowedTags
metadata:
name: no-latest-tag
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
namespaces: ["production"]
parameters:
tags: ["latest"]
exemptImages:
- "registry.internal/infra/*"Checkov: IaC Scanning
# Scan Terraform
checkov -d ./terraform/ --framework terraform --output json
# Scan Kubernetes manifests
checkov -d ./k8s/ --framework kubernetes --compact
# Scan Dockerfiles
checkov -d . --framework dockerfileCI/CD Integration
# GitLab CI example
stages:
- validate
- build
- deploy
security-scan:
stage: validate
image: bridgecrew/checkov:latest
script:
- checkov -d . --framework terraform,kubernetes,dockerfile
--output cli --output junitxml
--output-file-path console,checkov-results.xml
--soft-fail-on LOW
--hard-fail-on HIGH,CRITICAL
artifacts:
reports:
junit: checkov-results.xml
kyverno-test:
stage: validate
image: ghcr.io/kyverno/kyverno-cli:latest
script:
- kyverno apply ./policies/ --resource ./k8s/
allow_failure: falseEssential Policies
Every Kubernetes deployment should enforce:
- No
latesttags โ pin image versions - Resource limits required โ prevent noisy neighbors
- No privileged containers โ security baseline
- Read-only root filesystem โ prevent runtime modification
- Non-root user โ drop unnecessary privileges
- Network policies exist โ default deny
- No host networking โ container isolation
- Liveness/readiness probes โ health checking
Key Practices
- Start with
Auditmode โ see what would fail before enforcing - Exempt system namespaces โ
kube-systemneeds special permissions - Version your policies โ treat them like code
- Document exceptions โ when a policy is bypassed, record why
- Report on compliance trends โ track improvement over time
Implementing shift-left security? I help teams build secure CI/CD pipelines with policy-as-code. Get in touch.
