beginner15 min readgetting-startedUpdated: 2024-06-10

Policy Language Basics

Learn the common patterns and syntax used in policy definitions across different tools

๐Ÿท๏ธ Topics Covered

policy language syntax comparisonrego vs sentinel vs cedar policy languageshow to choose policy languagepolicy language features comparisondeclarative policy language tutorialpolicy language best practices guide

Policy Language Syntax Comparison Tutorial: Rego vs Sentinel vs Cedar

Policy languages are domain-specific languages designed to express rules, constraints, and governance requirements in a structured, machine-readable format. Different tools use different syntax, but they all share common patterns and concepts.

๐Ÿ”‘ Key Concepts

Understanding these fundamental concepts will help you learn any policy language more effectively.

๐Ÿ“

Declarative

Describe what should be true, not how to achieve it

๐Ÿ”

Conditions

Logic that determines when rules apply

โšก

Actions

What happens when conditions are met or violated

๐ŸŽฏ

Scope

Which resources or contexts the policy applies to

Rego Policy Language Tutorial: Open Policy Agent Syntax Guide

Rego is OPA's declarative query language. It's designed to be expressive yet safe for untrusted policies.

Basic Rego Structure

Example Code

# Package declaration - organizes policies
package aws.ec2.security

# Import statements for reusable logic
import future.keywords.in

# Default decision (optional)
default allow := false

# Rule with conditions
allow if {
    input.resource_type == "aws_instance"
    input.instance_type in ["t3.micro", "t3.small"]
    has_security_group
}

# Helper rule
has_security_group if {
    input.security_groups[_]
}

๐Ÿ”ง Syntax Breakdown

  • package - Namespace for the policy
  • default - Sets default value for undefined variables
  • if - Conditions that must be true
  • input - Data being evaluated
  • _ - Wildcard/anonymous variable

Azure Policy Language Tutorial: JSON Examples and Best Practices

Azure Policy uses JSON-based definitions with logical operators and functions.

Azure Policy Structure

Example Code

{
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Storage/storageAccounts"
      },
      {
        "not": {
          "field": "Microsoft.Storage/storageAccounts/encryption.services.blob.enabled",
          "equals": "true"
        }
      }
    ]
  },
  "then": {
    "effect": "deny",
    "details": {
      "message": "Storage account must have blob encryption enabled"
    }
  }
}

๐Ÿ”ง Key Elements

  • if - Condition block
  • allOf/anyOf - Logical operators
  • field - Resource property to check
  • then - Action to take
  • effect - Policy enforcement mode

Declarative Policy Language Tutorial Step by Step: Common Patterns

๐Ÿ” Resource Filtering

Select specific resource types or properties to evaluate

โœ… Required Properties

Ensure specific properties exist and have correct values

๐Ÿšซ Forbidden Values

Block certain configurations or values

๐Ÿ“‹ Allowed Lists

Restrict values to approved options

Policy Language Best Practices Guide: Learning Policy Languages from Scratch

1

๐Ÿ“ Clear Naming

Use descriptive names for policies, rules, and variables

โŒ Bad

  • rule1, check_stuff

โœ… Good

  • require_s3_encryption, block_public_access
2

๐ŸŽฏ Specific Conditions

Make conditions as specific as possible to avoid false positives

โŒ Bad

  • input.type == "storage"

โœ… Good

  • input.resource_type == "aws_s3_bucket"
3

๐Ÿ’ฌ Helpful Messages

Provide clear, actionable error messages

โŒ Bad

  • "Policy violation"

โœ… Good

  • "S3 bucket must enable server-side encryption with AES256 or KMS"
4

๐Ÿงช Test Early

Test policies with sample data before deploying

โŒ Bad

  • Deploy untested policies to production

โœ… Good

  • Create test cases for both compliant and non-compliant resources

Next Steps