A Kubernetes Admission Webhook that uses Casbin for Policy-as-Code enforcement. This webhook validates Kubernetes API requests based on Casbin policies, enabling fine-grained access control over cluster resources.
The Casbin Admission Webhook acts as a Kubernetes ValidatingWebhookConfiguration that intercepts API requests before they are persisted to etcd. Each request is evaluated against Casbin policies to determine if it should be allowed or denied.
Flow:
# Clone the repository git clone https://github.com/casbin/casbin-admission-webhook.git cd casbin-admission-webhook # Generate certificates and deploy to Kubernetes make deploy
This will:
casbin-system namespacekubectl get pods -n casbin-system kubectl logs -n casbin-system -l app=casbin-admission-webhook
The Casbin model defines the request format and matching rules. Default model (deploy/kubernetes/model.conf):
[request_definition] r = sub, obj, act [policy_definition] p = sub, obj, act [policy_effect] e = some(where (p.eft == allow)) [matchers] m = r.sub == p.sub && (r.obj == p.obj || p.obj == "*") && (r.act == p.act || p.act == "*")
The policy file defines access rules (deploy/kubernetes/policy.csv):
# Format: p, subject, object, action # Allow admin to do everything p, admin, *, * # Allow developer to create/update/delete pods in development namespace p, developer, pods/development, CREATE p, developer, pods/development, UPDATE p, developer, pods/development, DELETE # Allow viewer to get resources p, viewer, */*, GET
Request Format:
subject: Kubernetes username (from UserInfo)object: {resource}/{namespace} (e.g., pods/default)action: Kubernetes operation (CREATE, UPDATE, DELETE, etc.)Edit the ConfigMap to update policies:
kubectl edit configmap casbin-config -n casbin-system # Restart pods to reload configuration kubectl rollout restart deployment casbin-admission-webhook -n casbin-system
# Install dependencies go mod download # Run tests make test # Build binary make build # Run locally (requires valid certs and config) make run
# Run all tests go test ./... -v # Run with coverage make coverage
# Build image make docker-build # Build and push (requires Docker Hub credentials) make docker-push VERSION=v1.0.0
# Deploy everything kubectl apply -f deploy/kubernetes/deployment.yaml kubectl apply -f deploy/kubernetes/webhook-config.yaml
If you prefer to manage certificates yourself:
./deploy/kubernetes/generate-certs.sh casbin-system casbin-admission-webhook
The webhook supports the following environment variables:
| Variable | Description | Default |
|---|---|---|
WEBHOOK_PORT | HTTPS server port | 8443 |
TLS_CERT_FILE | Path to TLS certificate | /etc/webhook/certs/tls.crt |
TLS_KEY_FILE | Path to TLS private key | /etc/webhook/certs/tls.key |
CASBIN_MODEL_FILE | Path to Casbin model file | /etc/webhook/casbin/model.conf |
CASBIN_POLICY_FILE | Path to Casbin policy file | /etc/webhook/casbin/policy.csv |
# Admins can do everything p, system:serviceaccount:kube-system:admin, *, * # Developers can manage pods in dev namespace p, developer@example.com, pods/dev, CREATE p, developer@example.com, pods/dev, UPDATE p, developer@example.com, pods/dev, DELETE # Viewers can only read p, viewer@example.com, */*, GET
# Team A can only access team-a namespace p, team-a, */team-a, * # Team B can only access team-b namespace p, team-b, */team-b, *
# Allow creating configmaps but not secrets p, developer, configmaps/*, CREATE p, developer, secrets/*, "" # Allow reading everything p, developer, */*, GET
Check webhook configuration:
kubectl get validatingwebhookconfigurations casbin-admission-webhook -o yaml
Verify service endpoints:
kubectl get endpoints -n casbin-system
Check webhook logs:
kubectl logs -n casbin-system -l app=casbin-admission-webhook -f
Regenerate certificates:
./deploy/kubernetes/generate-certs.sh casbin-system casbin-admission-webhook kubectl rollout restart deployment casbin-admission-webhook -n casbin-system
make undeploy
Or manually:
kubectl delete -f deploy/kubernetes/webhook-config.yaml kubectl delete -f deploy/kubernetes/deployment.yaml kubectl delete namespace casbin-system
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.