Security - OPA Gatekeeper
Overview & Architecture
In Kubernetes, Admission Controllers enforce policies on objects during create, update, and delete operations.
Admission control is fundamental to policy enforcement in Kubernetes.
For example, by deploying OPA as an admission controller you can
- Require specific labels on all resources.
- Require container images come from the corporate image registry.
- Require all Pods specify resource requests and limits.
- Prevent conflicting Ingress objects from being created.
Admission controllers can also mutate incoming objects. By deploying OPA as a mutating admission controller you can:
- Inject sidecar containers into Pods.
- Set specific annotations on all resources.
- Rewrite container images to point at the corporate image registry.
- Include node and pod (anti-)affinity selectors on Deployments.
These are just examples of policies you can enforce with admission controllers and OPA.
There are dozens of other policies you will want to enforce in your Kubernetes clusters for security, cost, and availability reasons.
What is OPA Gatekeeper?
OPA Gatekeeper is a specialized project providing first-class integration between OPA and Kubernetes.
OPA Gatekeeper adds the following on top of plain OPA:
- An extensible, parameterized policy library.
- Native Kubernetes CRDs for instantiating the policy library (aka “constraints”).
- Native Kubernetes CRDs for extending the policy library (aka “constraint templates”).
- Audit functionality.
Recommendation
- OPA Gatekeeper is the go-to project for using OPA for Kubernetes admission control.
- Plain OPA and Kube-mgmt (see below) are alternatives that can be reached for if you want to use the management features of OPA
- such as status logs, decision logs, and bundles.
How Does It Work With Plain OPA and Kube-mgmt?
Admission Control Flow
The Kubernetes API Server is configured to query OPA for admission control decisions when objects (e.g., Pods, Services, etc.) are created, updated, or deleted.
- The API Server sends the entire Kubernetes object in the webhook request to OPA.
- OPA evaluates the policies it has loaded using the admission review as
input
.
For example, the following policy denies objects that include container images referring to illegal registries:
1 | package kubernetes.admission |
The
input
document contains the following fields:
Field | Desc |
---|---|
input.request.kind | specifies the type of the object (e.g., Pod , Service , etc.) |
input.request.operation | specifies the type of the operation, i.e., CREATE , UPDATE , DELETE , CONNECT . |
input.request.userInfo | specifies the identity of the caller. |
input.request.object | contains the entire Kubernetes object. |
input.request.oldObject | specifies the previous version of the Kubernetes object on UPDATE and DELETE . |
1 | { |
The policies you give to OPA ultimately generate an admission review response that is sent back to the API Server.
1 | { |
The API Server implements a deny overrides conflict resolution strategy.
If any admission controller denies the request, the request is denied (even if one of the later admission controllers were to allow the request.)
Policy and Data Caching
- Policies can be loaded into OPA dynamically via ConfigMap objects using the kube-mgmt sidecar container.
- The kube-mgmt sidecar container can also load any other Kubernetes object into OPA as JSON under data.
- This lets you enforce policies that rely on an eventually consistent snapshot of the Kubernetes cluster as context.