Kubernetes Admission Webhook
Overview
Casbin K8s-Gatekeeper is a Kubernetes admission webhook that uses Casbin for authorization. You define models and policies declaratively to allow or deny operations on any Kubernetes resource—no custom code in the webhook. Maintained by the Casbin community: github.com/casbin/k8s-gatekeeper.
Basic example
Example: deny deployments that use images with a specific tag, using only config:
Model:
[request_definition]
r = obj
[policy_definition]
p = obj,eft
[policy_effect]
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
contain(split(accessWithWildcard(${OBJECT}.Spec.Template.Spec.Containers , "*", "Image"),":",1) , p.obj)
Policy:
p, "1.14.1",deny
This uses standard Casbin ACL language, which should be straightforward if you've read the introductory chapters.
Casbin K8s-Gatekeeper offers several advantages:
- Simple to use—write ACL configurations instead of extensive code
- Supports live configuration updates without plugin restarts
- Flexible—apply arbitrary rules to any Kubernetes resource using
kubectl gatekeeper - Simplifies Kubernetes admission webhook implementation—no need to understand webhook internals or write webhook code. Just define constraints and write Casbin ACL.
- Community-maintained—contact us with questions or issues
1.1 How Casbin K8s-Gatekeeper Works
K8s-Gatekeeper is an admission webhook for Kubernetes that uses Casbin to enforce custom access control rules, preventing unwanted operations on Kubernetes resources.
Casbin is an efficient open-source access control library supporting various authorization models. For details, see the Overview.
Admission webhooks in Kubernetes are HTTP callbacks that receive and process admission requests. K8s-Gatekeeper is a ValidatingAdmissionWebhook that accepts or rejects admission requests. Admission requests are HTTP requests describing operations on Kubernetes resources (e.g., creating or deleting a deployment). For more information, see the Kubernetes documentation.
1.2 Example Workflow
When someone creates a deployment with an nginx pod (via kubectl or Kubernetes clients), Kubernetes generates an admission request like this (in YAML format):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.1
ports:
- containerPort: 80
This request passes through middleware layers, including K8s-Gatekeeper. K8s-Gatekeeper detects all Casbin enforcers stored in Kubernetes etcd (created and maintained by users via kubectl or the provided Go client). Each enforcer contains a Casbin model and policy. The admission request is evaluated by each enforcer sequentially, and must pass all enforcers to be accepted.
(If you're unfamiliar with Casbin enforcers, models, or policies, see Get Started).
For instance, if an administrator wants to block the 'nginx:1.14.1' image while allowing 'nginx:1.3.1', they can create an enforcer with this model and policy (creation and configuration details follow in subsequent sections):
Model:
[request_definition]
r = obj
[policy_definition]
p = obj,eft
[policy_effect]
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
access(r.obj.Request.Object.Object.Spec.Template.Spec.Containers , 0, "Image") == p.obj
Policy:
p, "nginx:1.13.1",allow
p, "nginx:1.14.1",deny
Creating an enforcer with this model and policy will reject the admission request, preventing Kubernetes from creating the deployment.
2. Installing K8s-gatekeeper
Three installation methods are available: External webhook, Internal webhook, and Helm.
These installation methods are for evaluation purposes only. For production deployments, review Chapter 5. Advanced settings and apply necessary security modifications before installation.
2.1 Internal Webhook
2.1.1 Step 1: Build the Image
For internal webhook deployment, K8s-gatekeeper runs as a Kubernetes service. Build the image:
docker build --target webhook -t k8s-gatekeeper .
This creates a local image named 'k8s-gatekeeper:latest'.
For minikube users, run eval $(minikube -p minikube docker-env) before 'docker build'.
2.1.2 Step 2: Deploy Services and Resources
Run these commands:
kubectl apply -f config/rbac.yaml
kubectl apply -f config/webhook_deployment.yaml
kubectl apply -f config/webhook_internal.yaml
Verify deployment with kubectl get pods.
2.1.3 Step 3: Install CRD Resources
Install custom resource definitions:
kubectl apply -f config/auth.casbin.org_casbinmodels.yaml
kubectl apply -f config/auth.casbin.org_casbinpolicies.yaml