OPA
Open Policy Agent
A general-purpose policy engine. Manages policies for Kubernetes and API gateways.
Open Policy Agent
A general-purpose policy engine. Manages policies for Kubernetes and API gateways.
OPA (Open Policy Agent) is a CNCF project and a general-purpose policy engine for managing policy as code in cloud-native environments. Policies are written in a declarative language called Rego, and it is applied not only to Kubernetes but also to API gateways, CI/CD, microservices, and many other places.
The core principle of OPA is "separation of policy and data." When policies (Rego) and data (JSON) are loaded into OPA, it returns an allow/deny decision for a query. This enables centralized policy management.
In Kubernetes, OPA is used through Gatekeeper. In API gateways, it is integrated with Envoy and Kong to implement API access control. In Terraform, IaC policies are validated with conftest.
The Rego language may feel unfamiliar at first, but it can express complex conditional logic concisely. You can test Rego online in the OPA Playground.
# OPA μ€μΉ (CLI)
brew install opa
# κΈ°λ³Έ Rego μ μ±
μμ (authz.rego)
package authz
default allow = false
# κ΄λ¦¬μλ λͺ¨λ μ κ·Ό νμ©
allow {
input.user.role == "admin"
}
# μΌλ° μ¬μ©μλ μμ μ 리μμ€λ§ μ κ·Ό κ°λ₯
allow {
input.user.role == "user"
input.resource.owner == input.user.id
}
# μ½κΈ° μμ
μ κ³΅κ° λ¦¬μμ€μ νμ©
allow {
input.action == "read"
input.resource.public == true
}
# OPA μλ² μ€ν
opa run --server authz.rego
# μ μ±
쿼리 (curl)
curl -X POST localhost:8181/v1/data/authz/allow \
-H "Content-Type: application/json" \
-d '{"input": {"user": {"role": "admin"}, "action": "delete", "resource": {"id": "123"}}}'
# conftestλ‘ Terraform μ μ±
κ²μ¦
# policy/terraform.rego
package terraform
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_s3_bucket"
not resource.change.after.versioning[0].enabled
msg := sprintf("S3 bucket %v must have versioning enabled", [resource.address])
}
# μ€ν
conftest test terraform.plan.json
Architect: "We're going to centralize API access control with OPA. If the Envoy sidecar queries OPA to make the allow/deny decision, we can apply policies without modifying service code."
Developer: "Do I need to learn Rego?"
Architect: "You only need the basic syntax. Try practicing in the OPA Playground."
Interviewer: "How would you implement Policy as Code?"
Candidate: "I'd build a central policy engine with OPA. I'd use Gatekeeper for Kubernetes, Envoy+OPA for API gateways, and conftest for IaC validation in CI. Rego policies are version-controlled in Git, and policy changes are approved through PR review."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Reviewer: "This Rego is negating a condition without not. In Rego, you have to use not explicitly."
Author: "Ah, that was a Rego syntax mistake. I'll fix it."