
Prerequisites
- Jenkins server with Docker installed
- Kubernetes cluster and
kubectlconfigured - OPA Conftest CLI available locally or via Docker
Ensure your
kubeconfig credentials are stored in Jenkins (e.g., under credentialsId: 'kubeconfig') before starting.Jenkins Pipeline Stages
Add a vulnerability scan stage between the Docker build and Kubernetes deployment:Defining the OPA Policy
Createopa-k8s-security.rego at the root of your project:
Running Conftest
From your project directory, run:If any policy is violated, Conftest exits with a non-zero code and prints the error. Your Jenkins pipeline will fail until you address the violation.
Fixing Policy Violations
1. Enforce runAsNonRoot
Update your Deployment spec to include a securityContext:
2. Specify Numeric User (Optional)
If you encounter aCreateContainerConfigError due to a non-numeric user, add runAsUser: