Course contentsShow
← Cloud and Infrastructure
Lesson 2339 of 2,98950. Compliance and GovernanceFree lesson

OPA Policy Development

Writing Rego policies to enforce compliance rules, including structure, syntax, and common patterns.

OPA Policy Development

You'll learn how to write Rego policies that enforce compliance rules using proper structure, syntax, and patterns.

What is Rego Policy Development?

Rego is the declarative policy language used by Open Policy Agent. Writing Rego policies means translating compliance requirements into executable rules that can automatically evaluate whether your infrastructure, configurations, or API requests meet your standards. Think of it as writing a recipe that OPA follows to make consistent "allow or deny" decisions.

Core Rego Structure

A basic Rego policy consists of rules that produce boolean values or structured data. Each rule evaluates conditions against input data:

package compliance.storage

# Rule that denies unencrypted storage buckets
deny[msg] {
    input.resource.type == "storage_bucket"
    not input.resource.encryption_enabled
    msg := "Storage buckets must have encryption enabled"
}

Common Patterns

Default deny pattern: Start by denying everything, then explicitly allow what passes:

default allow = false

allow {
    input.user.role == "admin"
}

Conditional rules: Use multiple conditions in a single rule block. All must be true:

deny[msg] {
    input.resource.type == "database"
    input.resource.public_access == true
    input.resource.environment == "production"
    msg := "Production databases cannot be publicly accessible"
}

Helper functions: Break complex logic into reusable functions:

is_production {
    input.environment == "production"
}

deny[msg] {
    is_production
    # additional checks
}

Key Concepts

Rego evaluates all rules that match, building sets of results. The := operator assigns values, while == tests equality. Rules without conditions always evaluate to true.

Key Takeaway: Rego policies translate compliance rules into structured code blocks that test conditions against input data, producing deny messages or allow decisions using declarative logic patterns.