Skip to main content
๐ŸŽ“ Claude Code Masterclass Learn AI-assisted development on Udemy โ€” plus the companion book on Leanpub & Amazon. Start Learning
Shift-left security with policy-as-code
DevOps

Shift-Left Security: Integrating Policy-as-Code in CI/CD...

Implement policy-as-code with OPA Gatekeeper, Kyverno, and Checkov in your CI/CD pipelines. Catch misconfigurations before they reach production.

LB
Luca Berton
ยท 1 min read

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 dockerfile

CI/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: false

Essential Policies

Every Kubernetes deployment should enforce:

  1. No latest tags โ€” pin image versions
  2. Resource limits required โ€” prevent noisy neighbors
  3. No privileged containers โ€” security baseline
  4. Read-only root filesystem โ€” prevent runtime modification
  5. Non-root user โ€” drop unnecessary privileges
  6. Network policies exist โ€” default deny
  7. No host networking โ€” container isolation
  8. Liveness/readiness probes โ€” health checking

Key Practices

  • Start with Audit mode โ€” see what would fail before enforcing
  • Exempt system namespaces โ€” kube-system needs 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.

#security #policy-as-code #cicd #opa #kyverno
Share:
Cloud Infrastructure Design

Need help with Cloud Infrastructure Design?

Build resilient, cost-effective cloud environments with expert architecture consulting.

Learn more about Cloud Infrastructure Design

Want to operate this yourself, in production?

Take the free AI Platform Engineer Readiness Scorecard to see which skills transfer โ€” then build a production-shaped AI platform in the 4-week Bootcamp.

Take the Scorecard โ†’
Luca Berton โ€” AI & Cloud Advisor, Docker Captain

Luca Berton

AI & Cloud Advisor ยท Docker Captain ยท KubeCon Speaker

15+ years in enterprise infrastructure. Author of 8 technical books, creator of Ansible Pilot (1M+ YouTube views, 648K site users). Former Red Hat engineer. Speaker at KubeCon EU 2026 and Red Hat Summit 2026.

Free 30-min AI & Cloud consultation

Book Now