Course contentsShow
← Security
Lesson 3005 of 3,05256. Security Automation and ToolingFree lesson

Policy Enforcement with OPA and Sentinel

Implementing policy as code to enforce security and compliance rules before infrastructure deployment using Open Policy Agent.

Policy Enforcement with OPA and Sentinel

You'll learn how to implement policy-as-code using Open Policy Agent (OPA) and Sentinel to automatically enforce security and compliance rules before infrastructure is deployed.

What Is Policy-as-Code?

Instead of manually reviewing infrastructure configurations for security violations, policy-as-code treats security rules as executable code. Tools like Open Policy Agent (OPA) and HashiCorp Sentinel evaluate infrastructure-as-code templates before deployment, blocking non-compliant resources automatically.

Think of it as an automated security bouncer at the door: infrastructure changes must pass policy checks before they're allowed into production.

How It Works

Open Policy Agent (OPA) uses a declarative policy language called Rego. You write rules defining what's allowed (or forbidden), and OPA evaluates IaC templates against those rules:

# Example OPA policy - no S3 buckets without encryption
deny[msg] {
  resource := input.resource_changes[_]
  resource.type == "aws_s3_bucket"
  not resource.change.after.server_side_encryption_configuration
  msg := "S3 buckets must enable server-side encryption"
}

Sentinel (HashiCorp's policy framework) uses a similar concept with its own language, often integrated directly into Terraform workflows.

Both tools sit in your CI/CD pipeline: when someone submits IaC changes, the policy engine evaluates them. If violations are detected, the deployment fails with clear error messages explaining what must be fixed.

Why This Matters

Policy-as-code prevents security misconfigurations from reaching production by catching them during the development phase. It's scalable (automated, not manual review), versioned (policies live in Git alongside infrastructure code), and consistent (same rules apply everywhere).

Key Takeaway: Open Policy Agent and Sentinel transform security and compliance rules into executable code that automatically blocks non-compliant infrastructure changes before deployment, shifting security enforcement left in the development lifecycle.