This directory contains a complete implementation of the Policy Generation pattern, transforming compliance requirements into executable Cedar/OPA policy files using AI assistance.
This implementation demonstrates how to:
- Transform written compliance requirements into executable policy code
- Generate Cedar and OPA (Open Policy Agent) policies using AI
- Test and validate generated policies
- Integrate policy generation into CI/CD pipelines
iam_permissions.cedar- Example Cedar IAM policynetwork_policy.rego- Example OPA network policyconfig_rules.yml- Configuration rules and compliance requirementsgenerate-policies.sh- AI-powered policy generation scripttest-policies.sh- Policy testing and validation scriptcompliance-requirements.md- Written requirements for transformation
# Transform written requirements into Cedar policies
./generate-policies.sh requirements/encryption.md cedar > policies/encryption.cedar
# Generate OPA policies for network access
./generate-policies.sh requirements/network.md opa > policies/network.rego
# Test generated policies
./test-policies.shInput (compliance-requirements.md):
## SOC 2 Data Encryption Requirement
Data at rest must be AES-256 encrypted in transit and at rest per SOC 2.
All database connections must use TLS 1.2 or higher.Generated Cedar Policy:
permit(
principal,
action == Action::"s3:PutObject",
resource
) when {
resource has encryption &&
resource.encryption.algorithm == "AES-256" &&
resource.encryption.enabled == true
};
- AWS native policy language
- Type-safe policy evaluation
- Human-readable syntax
- Integration with AWS services
- Cloud-native policy framework
- Kubernetes integration
- Flexible rule evaluation
- JSON/YAML input support
Cedar Pipeline:
# .github/workflows/cedar-policy-validation.yml
- name: Generate Cedar Policies
run: |
ai "Convert docs/compliance/*.md into Cedar policies" > policies/generated.cedar
- name: Validate Cedar Policies
run: |
cedar validate --schema schema.cedarschema policies/generated.cedarOPA/Rego Pipeline:
# .github/workflows/opa-policy-validation.yml
- name: Generate OPA Policies
run: |
ai "Convert docs/compliance/*.md into OPA Rego policies" > policies/generated.rego
- name: Test OPA Policies
run: |
opa test policies/ data/test-cases/# Weekly compliance policy update
cron_job() {
ai "Review compliance-requirements.md for changes, update policies accordingly"
git add policies/
git commit -m "feat: update compliance policies based on requirement changes"
}- Unit tests for individual policy rules
- Integration tests with sample data
- Compliance verification tests
- Performance testing for policy evaluation
# Test Cedar policies
cedar validate --schema schema.cedarschema policies/*.cedar
# Test OPA policies with sample data
opa test policies/ data/test-cases/Requirements should be written in a consistent format:
- Requirement ID: Unique identifier
- Description: Clear compliance requirement
- Authority: Regulatory source (SOC 2, PCI DSS, etc.)
- Implementation: Technical implementation details
## REQ-001: Data Encryption
**Authority**: SOC 2 Type II
**Description**: All sensitive data must be encrypted at rest using AES-256
**Implementation**: Database encryption, file system encryption, key rotation- Natural language to policy transformation
- Compliance framework mapping
- Automated policy updates
- Conflict detection and resolution
- Performance optimization for policy evaluation
- Rule consolidation and simplification
- Dead code elimination
- Policy impact analysis
- Syntax Errors: Validate policy syntax before deployment
- Conflicting Rules: Use policy analyzers to detect conflicts
- Performance Issues: Profile policy evaluation times
- Compliance Gaps: Regular audit against requirements
# Validate Cedar policy syntax
cedar validate policies/generated.cedar
# Test OPA policy evaluation
opa eval -d policies/ "data.authz.allow"
# Check for policy conflicts
./analyze-conflicts.sh policies/When adding new policy generation capabilities:
- Update the requirements format documentation
- Add test cases for new policy types
- Validate against relevant compliance frameworks
- Update CI/CD integration examples
- Review all AI-generated policies before deployment
- Test policies with realistic data scenarios
- Maintain audit logs for policy changes
- Regular compliance validation and testing