Notes
What OPA is
Open Policy Agent, or OPA, is a general-purpose policy engine, deliberately separate from any one system. You send it some input, a deploy request, a resource definition, a Terraform plan, and it evaluates your rules and returns a decision: allow or deny, usually with a reason. The rules are written in a language called Rego. The point of a separate engine is reuse: the same tool and the same style of policy enforce rules in Kubernetes, in CI pipelines, in APIs and over infrastructure code, so you learn it once.
- a general-purpose policy engine, decoupled from any one system
- you send it input (a request, a resource, a plan)
- it evaluates your Rego rules and returns allow or deny
- Rego: the policy language
- one engine, many uses: K8s, CI, APIs, Terraform
At Kubernetes admission
The classic use is Kubernetes admission control, which the signing episode touched on. An admission controller calls OPA on every deployment and denies anything that breaks policy: an unsigned image, a container running as root, a privileged pod, an image tagged latest, a workload with no resource limits. Gatekeeper is the OPA-based option, and Kyverno is a popular YAML-based alternative. The effect is that the cluster itself enforces your standards on every deploy, not a reviewer catching violations.
- an admission controller calls OPA on every deploy
- deny: no unsigned images, no running as root, no privileged pods, no latest tag, resource limits set
- tools: Gatekeeper (OPA), Kyverno
- the cluster enforces policy, not a human reviewer
In the pipeline
The same engine works earlier, in the pipeline. You evaluate Terraform plans, manifests and configuration files against policy in CI and block the pull request or the apply when they violate it. A tool called conftest runs OPA policies over config files. The nice property is reusing the same policies in CI and at admission: CI catches violations early for fast feedback, and admission enforces them at the last moment as a backstop, so a change that skips CI still cannot deploy. That is defence in depth for policy.
- evaluate Terraform plans, manifests, configs against policy in CI
- block the PR or the apply on violations
- conftest: run OPA policies over config files
- same policies as admission: catch early AND enforce late
- defence in depth
Writing a rule
Here is what a policy looks like. This Rego rule denies a pod if any container does not set run-as-non-root, and returns a clear message naming the container. The pattern is deny rules: allowed unless a rule objects, and each objection carries a human-readable reason. Good messages matter, because the developer who hits the guardrail needs to know what to fix. A guardrail that says access denied with no explanation makes a support ticket; one that says runs as root, set runAsNonRoot teaches.
package main
deny[msg] {
input.kind == "Pod"
c := input.spec.containers[_]
not c.securityContext.runAsNonRoot
msg := sprintf("%s runs as root",
[c.name])
}
# deny unless runAsNonRoot is set Paved roads
The deepest idea here is the paved road. A guardrail that only blocks leaves a developer stuck, hunting for how to comply. A paved road pairs the block with a secure default that just works: a golden Terraform module, a base manifest, a template that passes policy out of the box. You make the compliant path the easiest path, so people choose it naturally, and the guardrail is only there for the rare cliff edge. Guardrails plus golden modules give secure-by-default infrastructure.
- blocker: "denied", developer is stuck and annoyed
- paved road: a secure default template or module that just passes
- make the compliant path the easiest path
- guardrails + golden modules = secure by default
- block the cliff edges, pave the common road
Test your policies
Treat policies as the code they are. Version them, review them, and test them: OPA has a test framework, and a policy that wrongly allows or blocks is a bug. Roll a new guardrail out in warn mode first, logging what it would block, before switching to enforce, so you do not break everyone on day one. Allow audited exceptions rather than silent bypasses. Write clear deny messages with the fix. And start with a handful of high-value rules, not hundreds, so the signal stays strong.
- policies are code: version, review and TEST them (OPA has a test framework)
- roll out in warn mode first, then enforce
- allow audited exceptions, not silent bypasses
- clear deny messages with a fix
- start with a few high-value rules, not hundreds
Common mistakes
Mistakes. Enforcing a brand-new policy with no warn period, which breaks every team at once and burns trust. Cryptic deny messages that do not say how to fix the problem. Blockers with no paved road, so developers are stuck. Untested policies that wrongly allow something dangerous or wrongly block something fine. Turning on hundreds of rules at once instead of a focused few. And allowing silent bypasses instead of audited, reviewed exceptions, which quietly hollows out the whole system.
- enforcing a new policy with no warn period
- cryptic deny messages, no fix given
- blockers with no paved road
- untested policies that allow or block wrongly
- hundreds of rules at once
- silent bypasses instead of audited exceptions
Say it like this
In the interview: I enforce policy as code with Open Policy Agent and Rego, reusing the same policies in CI with conftest and at Kubernetes admission with Gatekeeper, caught early and enforced late. I write deny rules with clear fix messages, roll each new guardrail out in warn mode before enforcing, and test policies like any code. Crucially, I pair blockers with paved roads, golden modules and templates that pass policy by default, so the secure path is the easy one, and I allow audited exceptions.
- "Policy as code with OPA and Rego: the same policies in CI
- (conftest) and at Kubernetes
- admission (Gatekeeper). Deny
- rules with clear fix messages, rolled out warn-then-enforce, tested like code. I pair
- blockers with paved roads so the secure path is the easy
- one, and allow audited
- exceptions."