πŸ”’ Security

OPA

Open Policy Agent

A general-purpose policy engine. Manages policies for Kubernetes and API gateways.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

# 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

πŸ—£οΈ How It's Said in Practice

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."

⚠️ Cautions

πŸ“š Learn More