A production-ready collection of Kyverno policies for enforcing security best practices on Kubernetes clusters. This repository implements a defense-in-depth approach using policy-as-code to secure K3s and Kubernetes workloads.
This project provides a comprehensive security policy framework using Kyverno, a Kubernetes-native policy engine. The policies enforce Pod Security Standards, image verification, resource governance, and compliance requirements without requiring webhook configurations or separate policy servers.
- Pod Security Standards Enforcement: Implements baseline security controls preventing privileged containers, host namespace access, and dangerous capabilities
- Image Security: Blocks
:latesttags and enforces cosign keyless signature verification - Resource Governance: Mandates resource requests/limits and governance labels
- Automatic Remediation: Mutates workloads to apply security defaults (drop capabilities, enforce non-root execution)
- Compliance Reporting: Python-based CLI tool generates HTML audit reports mapping policy violations to compliance frameworks
.
├── kyverno-policies/
│ └── clusterpolicies/ # Kyverno ClusterPolicy definitions
├── cli/ # Compliance reporting tools
├── cluster/
│ └── tests/ # Test pod manifests (good/bad examples)
│ └── bad-pod.yaml # Bad pod example
| └── good-pod.yaml # Good pod example
| └── test-policies.py # Test script for pod deployments
├── rbac/ # RBAC roles, bindings, and escalation prevention
├── ARCHITECTURE.md # System design documentation
├── COMPLIANCE_MAPPING.md # Policy-to-framework mapping
└── README.md
- Kubernetes cluster (tested on K3s)
- Kyverno 1.12 or higher installed
kubectlconfigured with cluster access- Python 3.8+ (for compliance reporting)
# Install Kyverno via Helm
helm repo add kyverno https://kyverno.github.io/kyverno/
helm repo update
helm install kyverno kyverno/kyverno -n kyverno --create-namespace
# Or via kubectl
kubectl create -f https://github.com/kyverno/kyverno/releases/download/v1.12.0/install.yaml# Clone the repository
git clone https://github.com/brad-eck/kubernetes-security-policies.git
cd kubernetes-security-policies
# Apply all ClusterPolicies
kubectl apply -f ./kyverno-policies/clusterpolicies/# List installed policies
kubectl get clusterpolicies
# Check policy status
kubectl describe clusterpolicy <policy-name># Test with a compliant pod (should succeed)
kubectl apply -f ./cluster/test/good-pod.yaml
# Test with a non-compliant pod (should fail)
kubectl apply -f ./cluster/test/bad-pod.yamlBaseline Pod Security
- Blocks privileged containers
- Prevents host namespace sharing (PID, IPC, network)
- Restricts host path volumes
- Enforces security context constraints
Capabilities Management
- Drops all Linux capabilities by default
- Restricts addition of dangerous capabilities (NET_RAW, SYS_ADMIN, etc.)
- Validates securityContext settings
Image Security
- Blocks
:latestimage tags - Requires valid cosign signatures (keyless verification)
- Enforces
imagePullPolicy: IfNotPresent
Resource Requirements
- Mandates CPU and memory requests
- Mandates CPU and memory limits
- Prevents unbounded resource consumption
Labeling Standards
- Requires governance labels (owner, team, environment, app)
- Validates label format and values
Role-Based Access Control
- Seven distinct personas: cluster admin, namespace admin, developer, CI/CD deployer, monitoring, security auditor, log collector
- Least-privilege design with explicit verb and resource grants (no wildcards outside break-glass)
- Group-based bindings for humans, dedicated ServiceAccounts for workloads
- Aggregated ClusterRoles for extensible CRD visibility
- Kyverno policy to block wildcard RBAC rules and prevent privilege escalation
Automatic Security Hardening
- Sets
runAsNonRoot: true - Drops all capabilities automatically
- Configures
imagePullPolicy: IfNotPresent - Applies read-only root filesystem where applicable
The repository includes a Python CLI tool that generates audit-ready HTML reports from Kyverno PolicyReports, and a Python script that tests pod examples that can be found in /tests.
cd cli
python3 -m venv venv
source venv/bin/activate
pip install rich./compliance_report.pyThe report is saved to reports/compliance_report.html and includes:
- Policy compliance status by namespace
- Violation details with resource information
- Mapping to compliance frameworks (CIS, PCI-DSS, SOC 2, NIST)
- Color-coded status indicators
Kyverno policies support multiple enforcement modes:
- Enforce: Blocks non-compliant resources (default)
- Audit: Logs violations without blocking
- Background: Scans existing resources
To change a policy mode, edit the validationFailureAction field:
spec:
validationFailureAction: Audit # or EnforceAdd namespace exclusions to policies:
spec:
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- resources:
namespaces:
- kube-system
- kyvernoModify minimum resource requirements in resource governance policies:
pattern:
spec:
containers:
- resources:
requests:
memory: "128Mi" # Adjust as needed
cpu: "100m"Update allowed registries in image verification policies:
imageReferences:
- "docker.io/*"
- "gcr.io/my-project/*"
- "my-registry.example.com/*"# View policy reports in a namespace
kubectl get policyreport -n <namespace>
# Detailed report view
kubectl describe policyreport -n <namespace># List all failures
kubectl get policyreport -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.summary.fail}{"\n"}{end}'# Check Kyverno logs
kubectl logs -n kyverno -l app.kubernetes.io/name=kyverno
# View admission review events
kubectl get events --field-selector reason=PolicyViolation- Start with Audit Mode: Test policies in audit mode before enforcing
- Gradual Rollout: Apply policies to non-production namespaces first
- Exception Management: Document and review namespace exclusions regularly
- Regular Reviews: Audit policy reports weekly
- Update Policies: Keep Kyverno and policies updated for latest security fixes
- Policy Drift: Regularly audit applied policies against repository
- Admission Control: Kyverno policies run in admission control path; ensure high availability
- Resource Impact: Monitor Kyverno resource consumption in large clusters
- Privilege Escalation: Protect Kyverno namespace with strict RBAC; see
rbac/for complete role definitions - Image Verification: Maintain cosign public keys securely
- RBAC Hygiene: Audit bindings regularly with
kubectl get clusterrolebindings,rolebindings -A -l app.kubernetes.io/managed-by=kubernetes-security-policies