“Hardcoded logic works until your infrastructure outgrows your brain.”
That’s the lesson I learned the hard way.
As someone building DevOps pipelines and cloud-native systems, I always thought writing access logic in code was normal.
But over time, it became clear: spreading policy decisions across scripts, CI files, YAMLs, and backend code was not just messy — it was dangerous.
That’s when I met Open Policy Agent (OPA).
And it clicked: Policies don’t belong in code. They belong in policies.
🧠 What Problem Does OPA Solve?
In real-world systems, we need to answer questions like:
“Can this user deploy to production?”
“Should this Terraform change be allowed?”
“Does this Kubernetes pod follow our security guidelines?”
“Are these API requests properly scoped and authorised?”
Now, without OPA, answers to these questions live in:
If-else statements are scattered across apps
Bash conditionals in CI/CD scripts
Cluster admission controllers
Terraform plan review comments on Slack
And worst — people’s heads
This approach scales like spaghetti.
OPA provides a declarative, centralised way to answer such questions — and enforce them.
🚀 What Is Open Policy Agent?
OPA is a general-purpose, lightweight policy engine that decouples policy decision-making from policy enforcement.
You write policies in a language called Rego (declarative, logic-based), and then plug OPA into:
- Kubernetes (via Gatekeeper or as a standalone webhook)
- Terraform (via Conftest)
- CI/CD pipelines (e.g., GitHub Actions, Jenkins)
- Service meshes (Envoy)
- APIs (custom integration)
OPA answers a simple question:
Given some input data, should this action be allowed?
🔍 Real-World Use Cases of OPA
Let’s stop with “buzzword DevSecOps” and look at where OPA shines:
1)Kubernetes Admission Control
You want every pod to:
- Have resource limits
- Belong to a specific namespace
- Disallow use of hostNetwork
You write one Rego policy and deploy it via Gatekeeper or your own admission webhook. Done.
2)Terraform Policy Checks
Instead of reviewing every PR for “Why is someone opening up 0.0.0.0/0 in the security group?”, just enforce:
Now every terraform plan is policy-guarded.
🔄 OPA vs RBAC vs ABAC
You might ask: “Don’t we already have RBAC?”
Yes — but RBAC breaks down when:
You need context-based access (time, location, payload)
You have dynamic teams and org structures
You want one engine to govern APIs, infra, cloud, and services
OPA enables fine-grained ABAC (Attribute-Based Access Control) and context-aware decisions across everything.
✍️ Writing Policies in Rego
Here’s a simple Rego policy that blocks container images from unknown registries:
OPA evaluates the input and returns whether it passes the policy or not — no need to mess with 15 scripts and 3 YAML files anymore.
🧩 Integrating OPA in Your Stack
Depending on your use case:
It’s not just for Kubernetes.
🛡️ Why OPA Is a Security Engineer’s Superpower
Security doesn’t scale with spreadsheets or tribal knowledge.
OPA lets you codify rules, intent, and organisational wisdom as machine-enforced policies.
You stop playing whack-a-mole.
You stop being the person who just says “no”.
You start being the person who writes the rules that say yes or no — transparently, fairly, and scalably.
🧪 Tips I Learned While Using OPA
Start small: one policy, one use case
Use OPA eval for local testing
Combine OPA with Conftest to test policies in CI/CD
Avoid overengineering, Rego — keep it readable
Structure inputs clearly, and document what your policies expect
⚠️ OPA Is Powerful, But Not a Silver Bullet
OPA makes decisions — it does not enforce them.
That’s your job — via admission webhooks, CI/CD gates, service logic, etc.
Additionally, Rego has a learning curve — it’s not as straightforward as JavaScript or Python. But once it clicks, it feels like writing the truth, not the code.
🧵 Final Thoughts
Don’t wait until your infrastructure becomes unmanageable.
If you’re already deep into DevOps, Platform Engineering, or SRE, OPA is that missing layer you didn’t know you needed.
Write once. Enforce everywhere. Audit always.
Stop burying your org’s policies in code and meetings.
Codify them with OPA — and bring clarity, consistency, and control back into your stack.
✉️ Got questions? Want to know how I use it across Kubernetes, GitHub Actions, and Terraform? DM me or drop a comment — always up for a good engineering conversation.
Top comments (0)