← back

DevSecOps Security Practices

90% coverage with actionable steps

TL;DR
Security integrated into every phase of development. Shift left: catch vulnerabilities early. DevSecOps = automation + continuous scanning + least privilege + defense in depth. Focus on the 90% that matters: dependency scanning, secrets management, container security, infrastructure as code validation, and runtime monitoring.

The DevSecOps Pipeline

Security at every stage: Code → Build → Test → Deploy → Run → Monitor

🔒 Shift Left

Catch vulnerabilities during development, not production. Automate security checks in CI/CD.

🔄 Continuous Scanning

Scan dependencies, containers, IaC, and runtime continuously. New CVEs emerge daily.

🎯 Least Privilege

Every service, user, and container gets only the permissions it needs. Nothing more.

🛡️ Defense in Depth

Multiple layers: network isolation, secrets encryption, runtime protection, audit logging.

1. Code Phase develop

Dependency Scanning

Scan for known vulnerabilities in third-party libraries before they enter your codebase.

Rust: cargo audit
  • Install: cargo install cargo-audit
  • Run in CI: cargo audit --deny warnings
  • Auto-fix: cargo audit fix (updates Cargo.lock)
JavaScript/TypeScript: npm audit or yarn audit
  • Run: npm audit --audit-level=moderate
  • Fix: npm audit fix
  • Alternative: snyk test (more comprehensive)
Python: pip-audit or safety
  • Install: pip install pip-audit
  • Run: pip-audit -r requirements.txt
Go: govulncheck
  • Install: go install golang.org/x/vuln/cmd/govulncheck@latest
  • Run: govulncheck ./...
Snyk Dependabot Renovate OWASP Dependency-Check

Secrets Management

Never commit secrets. Use environment variables, secret managers, or encrypted vaults.

Detect secrets before commit:
  • git-secrets or gitleaks in pre-commit hooks
  • GitHub: enable secret scanning (automatically detects leaked keys)
  • GitLab: configure Secret Detection in CI
Store secrets securely:
  • Use .env files (gitignored) for local dev
  • Production: Vault, AWS Secrets Manager, GCP Secret Manager, Azure Key Vault
  • Kubernetes: use kubectl create secret or External Secrets Operator
⚠️ Common mistakes:
  • Hardcoded API keys in source code
  • Secrets in Docker image layers (use build args, not ENV in Dockerfile)
  • Secrets in logs or error messages

Static Analysis (SAST)

Analyze source code for security issues without executing it.

Semgrep CodeQL SonarQube Bandit (Python) gosec (Go)
Run SAST in CI:
# GitHub Actions example
- name: Semgrep
  uses: returntocorp/semgrep-action@v1
  with:
    config: p/security-audit

2. Build Phase ci

Container Image Scanning

Scan Docker images for vulnerabilities before pushing to registry.

Trivy (fast, comprehensive, multi-purpose)
  • Install: brew install trivy or download binary
  • Scan image: trivy image myapp:latest
  • Fail on HIGH/CRITICAL: trivy image --severity HIGH,CRITICAL --exit-code 1 myapp:latest
  • Scan IaC: trivy config ./terraform
  • Scan filesystem: trivy fs .
Snyk Container
  • Scan: snyk container test myapp:latest
  • Monitor: snyk container monitor myapp:latest
Trivy Snyk Grype Clair

Use Minimal Base Images

Smaller attack surface = fewer vulnerabilities.

Prefer:
  • alpine (5 MB) over ubuntu (77 MB)
  • distroless (Google) - no shell, no package manager
  • scratch for static binaries (Go, Rust)
  • CIS-hardened images (CIS Benchmarks compliance)
Multi-stage builds:
# Build stage (large)
FROM rust:1.75 AS builder
WORKDIR /app
COPY . .
RUN cargo build --release

# Runtime stage (minimal)
FROM gcr.io/distroless/cc-debian12
COPY --from=builder /app/target/release/myapp /
CMD ["/myapp"]
💡 CIS Hardened Images: Use images that follow CIS Docker Benchmark (e.g., cis-docker-benchmark). Check: CIS Docker Benchmark

SBOM (Software Bill of Materials)

Generate a manifest of all components in your software. Required for compliance (e.g., Executive Order 14028).

Generate SBOM:
  • Syft: syft packages myapp:latest -o spdx-json > sbom.json
  • Trivy: trivy image --format cyclonedx myapp:latest > sbom.json

3. Deploy Phase cd

Infrastructure as Code (IaC) Scanning

Validate Terraform, CloudFormation, Kubernetes manifests for misconfigurations.

Trivy IaC scanning:
  • Scan Terraform: trivy config ./terraform
  • Scan K8s manifests: trivy config ./k8s
  • Fail on HIGH: trivy config --severity HIGH,CRITICAL --exit-code 1 .
Other tools:
  • tfsec - Terraform-specific scanner
  • checkov - Multi-cloud IaC scanner (Python)
  • kube-score - Kubernetes manifest validation

Kubernetes Security

Harden K8s clusters and workloads.

Pod Security Standards:
  • Enforce restricted pod security policy
  • Drop capabilities: drop: ["ALL"]
  • Run as non-root: runAsNonRoot: true
  • Read-only root filesystem: readOnlyRootFilesystem: true
  • No privileged containers: privileged: false
Network Policies:
  • Default deny all traffic
  • Explicit allow for required connections
  • Use NetworkPolicy resources
Secrets management:
  • Use External Secrets Operator (syncs from Vault/AWS/GCP)
  • Encrypt secrets at rest (KMS)
  • RBAC: limit who can read secrets
kube-bench kubesec Polaris OPA Gatekeeper

4. Runtime Phase prod

Runtime Security Monitoring

Detect anomalous behavior in production.

Falco (CNCF project - runtime threat detection)
  • Monitors syscalls for suspicious activity
  • Detects: shell execution in containers, unexpected network connections, privilege escalation
  • Deploy as DaemonSet in K8s
Container Runtime Security:
  • Use gVisor or Kata Containers for additional isolation
  • Enable AppArmor/SELinux profiles
  • Use seccomp to restrict syscalls

Continuous Vulnerability Scanning

Running containers accumulate new CVEs. Scan regularly.

Schedule scans:
  • Daily scans of running images
  • Alert on new HIGH/CRITICAL vulnerabilities
  • Auto-redeploy patched images (with testing)

Logging & Audit

Centralize logs, enable audit trails.

Log security events:
  • Authentication attempts (success/failure)
  • Authorization failures
  • Secrets access
  • Admin actions
  • K8s API server audit logs
Falco Sysdig Aqua Security Wazuh

90% Coverage Checklist

Focus on these high-impact practices first:

✅ Critical (Do these first)

✅ High Impact (Do next)

✅ Good to Have (After basics)

Essential Tools

🔍 Trivy

All-in-one scanner: containers, IaC, filesystems, repos. Fast, comprehensive, easy to integrate.

github.com/aquasecurity/trivy

🐍 Snyk

Developer-first security. Scans dependencies, containers, IaC. Great IDE integrations.

snyk.io

🔐 HashiCorp Vault

Secrets management. Centralized, encrypted, audited. Dynamic secrets, key rotation.

vaultproject.io

🚨 Falco

Runtime threat detection. Monitors syscalls for anomalies. CNCF graduated project.

falco.org

📋 Semgrep

Fast SAST. Custom rules, low false positives. Great for open-source projects.

semgrep.dev

🛡️ OPA Gatekeeper

Policy enforcement for K8s. Define and enforce security policies as code.

OPA Gatekeeper

CI/CD Integration Example

GitHub Actions workflow with security checks:

name: Security Scan
on: [push, pull_request]

jobs:
  security:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # Dependency scanning
      - name: Run npm audit
        run: npm audit --audit-level=moderate

      # SAST
      - name: Semgrep
        uses: returntocorp/semgrep-action@v1
        with:
          config: p/security-audit

      # Secrets detection
      - name: Gitleaks
        uses: gitleaks/gitleaks-action@v2

      # Build image
      - name: Build Docker image
        run: docker build -t myapp:${{ github.sha }} .

      # Container scanning
      - name: Trivy scan
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: myapp:${{ github.sha }}
          severity: HIGH,CRITICAL
          exit-code: 1

      # IaC scanning
      - name: Trivy IaC scan
        run: trivy config --severity HIGH,CRITICAL --exit-code 1 ./terraform

References & Learning