Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Policy-as-code enforcement and compliance validation using Open Policy Agent (OPA). Use when: (1) Enforcing security and compliance policies across infrastructure and applications, (2) Validating Kubernetes admission control policies, (3) Implementing policy-as-code for compliance frameworks (SOC2, PCI-DSS, GDPR, HIPAA), (4) Testing and evaluating OPA Rego policies, (5) Integrating policy checks into CI/CD pipelines, (6) Auditing configuration drift against organizational security standards, (7)
.claude/skills/aiskillstore-policy-opa/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 127% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 125% | 0% |
| case-10 | ✗→✓ | ▲ Improved | 61% | 0% |
| case-11 | ✗→✓ | ▲ Improved | 113% | 0% |
| case-13 | ✗→✓ | ▲ Improved | 229% | 0% |
This skill enables policy-as-code enforcement using Open Policy Agent (OPA) for compliance validation, security policy enforcement, and configuration auditing. OPA provides a unified framework for policy evaluation across cloud-native environments, Kubernetes, CI/CD pipelines, and infrastructure-as-code.
Use OPA to codify security requirements, compliance controls, and organizational standards as executable policies written in Rego. Automatically validate configurations, prevent misconfigurations, and maintain continuous compliance.
bash# macOS brew install opa # Linux curl -L -o opa https://openpolicyagent.org/downloads/latest/opa_linux_amd64 chmod +x opa # Verify installation opa version
bash# Evaluate a policy against input data opa eval --data policy.rego --input input.json 'data.example.allow' # Test policies with unit tests opa test policy.rego policy_test.rego --verbose # Run OPA server for live policy evaluation opa run --server --addr localhost:8181
Identify compliance requirements and security controls to enforce:
Create policy files in Rego language. Use the provided templates in assets/ for common patterns:
Example: Kubernetes Pod Security Policy
regopackage kubernetes.admission import future.keywords.contains import future.keywords.if deny[msg] { input.request.kind.kind == "Pod" container := input.request.object.spec.containers[_] container.securityContext.privileged == true msg := sprintf("Privileged containers are not allowed: %v", [container.name]) } deny[msg] { input.request.kind.kind == "Pod" container := input.request.object.spec.containers[_] not container.securityContext.runAsNonRoot msg := sprintf("Container must run as non-root: %v", [container.name]) }
Example: Compliance Control Validation (SOC2)
regopackage compliance.soc2 import future.keywords.if # CC6.1: Logical and physical access controls deny[msg] { input.kind == "Deployment" not input.spec.template.metadata.labels["data-classification"] msg := "SOC2 CC6.1: All deployments must have data-classification label" } # CC6.6: Encryption in transit deny[msg] { input.kind == "Service" input.spec.type == "LoadBalancer" not input.metadata.annotations["service.beta.kubernetes.io/aws-load-balancer-ssl-cert"] msg := "SOC2 CC6.6: LoadBalancer services must use SSL/TLS encryption" }
Write comprehensive tests for policy validation:
regopackage kubernetes.admission_test import data.kubernetes.admission test_deny_privileged_container { input := { "request": { "kind": {"kind": "Pod"}, "object": { "spec": { "containers": [{ "name": "nginx", "securityContext": {"privileged": true} }] } } } } count(admission.deny) > 0 } test_allow_unprivileged_container { input := { "request": { "kind": {"kind": "Pod"}, "object": { "spec": { "containers": [{ "name": "nginx", "securityContext": {"privileged": false, "runAsNonRoot": true} }] } } } } count(admission.deny) == 0 }
Run tests:
bashopa test . --verbose
Use the bundled evaluation script for policy validation:
bash# Evaluate single file ./scripts/evaluate_policy.py --policy policies/ --input config.yaml # Evaluate directory of configurations ./scripts/evaluate_policy.py --policy policies/ --input configs/ --recursive # Output results in JSON format for CI/CD integration ./scripts/evaluate_policy.py --policy policies/ --input config.yaml --format json
Or use OPA directly:
bash# Evaluate with formatted output opa eval --data policies/ --input config.yaml --format pretty 'data.compliance.violations' # Bundle evaluation for complex policies opa eval --bundle policies.tar.gz --input config.yaml 'data'
Add policy validation to your CI/CD workflow:
GitHub Actions Example:
yaml- name: Validate Policies uses: open-policy-agent/setup-opa@v2 with: version: latest - name: Run Policy Tests run: opa test policies/ --verbose - name: Evaluate Configuration run: | opa eval --data policies/ --input deployments/ \ --format pretty 'data.compliance.violations' > violations.json if [ $(jq 'length' violations.json) -gt 0 ]; then echo "Policy violations detected!" cat violations.json exit 1 fi
GitLab CI Example:
yamlpolicy-validation: image: openpolicyagent/opa:latest script: - opa test policies/ --verbose - opa eval --data policies/ --input configs/ --format pretty 'data.compliance.violations' artifacts: reports: junit: test-results.xml
Enforce policies at cluster level using OPA Gatekeeper:
bash# Install OPA Gatekeeper kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper.yaml # Apply constraint template kubectl apply -f assets/k8s-constraint-template.yaml # Apply constraint kubectl apply -f assets/k8s-constraint.yaml # Test admission control kubectl apply -f test-pod.yaml # Should be denied if violates policy
Generate compliance reports using the bundled reporting script:
bash# Generate compliance report ./scripts/generate_report.py --policy policies/ --audit-logs audit.json --output compliance-report.html # Export violations for SIEM integration ./scripts/generate_report.py --policy policies/ --audit-logs audit.json --format json --output violations.json
scripts/)evaluate_policy.py - Evaluate OPA policies against configuration files with formatted outputgenerate_report.py - Generate compliance reports from policy evaluation resultstest_policies.sh - Run OPA policy unit tests with coverage reportingreferences/)rego-patterns.md - Common Rego patterns for security and compliance policiescompliance-frameworks.md - Policy templates mapped to SOC2, PCI-DSS, GDPR, HIPAA controlskubernetes-security.md - Kubernetes security policies and admission control patternsiac-policies.md - Infrastructure-as-code policy validation for Terraform, CloudFormationassets/)k8s-pod-security.rego - Kubernetes pod security policy templatek8s-constraint-template.yaml - OPA Gatekeeper constraint templatek8s-constraint.yaml - Example Gatekeeper constraint configurationsoc2-compliance.rego - SOC2 compliance controls as OPA policiespci-dss-compliance.rego - PCI-DSS requirements as OPA policiesgdpr-compliance.rego - GDPR data protection policiesterraform-security.rego - Terraform security best practices policiesci-cd-pipeline.yaml - CI/CD integration examples (GitHub Actions, GitLab CI)Enforce security policies at pod creation time:
regopackage kubernetes.admission deny[msg] { input.request.kind.kind == "Pod" not input.request.object.spec.securityContext.runAsNonRoot msg := "Pods must run as non-root user" }
Validate Terraform configurations before apply:
regopackage terraform.security deny[msg] { resource := input.resource_changes[_] resource.type == "aws_s3_bucket" not resource.change.after.server_side_encryption_configuration msg := sprintf("S3 bucket %v must have encryption enabled", [resource.name]) }
Map policies to specific compliance controls:
regopackage compliance.soc2 # SOC2 CC6.1: Logical and physical access controls cc6_1_violations[msg] { input.kind == "RoleBinding" input.roleRef.name == "cluster-admin" msg := sprintf("SOC2 CC6.1 VIOLATION: cluster-admin binding for %v", [input.metadata.name]) }
Enforce data handling policies based on classification:
regopackage data.classification deny[msg] { input.metadata.labels["data-classification"] == "restricted" input.spec.template.spec.volumes[_].hostPath msg := "Restricted data cannot use hostPath volumes" }
Implement attribute-based access control (ABAC):
regopackage api.authz import future.keywords.if allow if { input.method == "GET" input.path[0] == "public" } allow if { input.method == "GET" input.user.role == "admin" } allow if { input.method == "POST" input.user.role == "editor" input.resource.owner == input.user.id }
conftest or OPA CLISolution:
opa eval --data policy.rego --input input.json --explain full 'data.example.allow'opa fmt to format policies and catch syntax errorsSolution:
kubectl get pods -n gatekeeper-systemkubectl get constraintskubectl logs -n gatekeeper-system -l control-plane=controller-managerSolution:
opa test . --verboseprint() statements in Rego for debuggingSolution:
opa build policies/ -o bundle.tar.gzinput.key patterns| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 11,536 | 7,676 | -33% | 1 | 1 | 0% | 2,047 | 4,645 | +127% | 0 | 0 | — |
case-02 | fail→fail | 24,204 | 29,094 | +20% | 1 | 1 | 0% | 5,139 | 9,416 | +83% | 0 | 0 | — |
case-03 | fail→pass | 11,729 | 9,507 | -19% | 1 | 1 | 0% | 2,214 | 4,987 | +125% | 0 | 0 | — |
case-04 | fail→fail | 15,420 | 8,998 | -42% | 1 | 1 | 0% | 2,688 | 4,950 | +84% | 0 | 0 | — |
case-05 | fail→fail | 14,372 | 8,097 | -44% | 1 | 1 | 0% | 2,553 | 4,773 | +87% | 0 | 0 | — |
case-06 | pass→pass | 17,102 | 7,101 | -58% | 1 | 1 | 0% | 3,284 | 4,621 | +41% | 0 | 0 | — |
case-07 | pass→pass | 16,435 | 12,150 | -26% | 1 | 1 | 0% | 3,093 | 5,584 | +81% | 0 | 0 | — |
case-08 | pass→pass | 12,311 | 9,439 | -23% | 1 | 1 | 0% | 2,180 | 4,878 | +124% | 0 | 0 | — |
case-09 | fail→fail | 10,389 | 10,304 | -1% | 1 | 1 | 0% | 2,169 | 5,330 | +146% | 0 | 0 | — |
case-10 | fail→pass | 15,993 | 8,331 | -48% | 1 | 1 | 0% | 3,004 | 4,835 | +61% | 0 | 0 | — |
case-11 | fail→pass | 11,921 | 7,816 | -34% | 1 | 1 | 0% | 2,185 | 4,650 | +113% | 0 | 0 | — |
case-12 | pass→pass | 14,059 | 11,595 | -18% | 1 | 1 | 0% | 2,688 | 5,299 | +97% | 0 | 0 | — |
case-13 | fail→pass | 6,916 | 2,693 | -61% | 1 | 1 | 0% | 1,111 | 3,658 | +229% | 0 | 0 | — |
case-14 | pass→pass | 14,140 | 12,489 | -12% | 1 | 1 | 0% | 2,324 | 5,270 | +127% | 0 | 0 | — |
case-15 | pass→pass | 9,331 | 4,065 | -56% | 1 | 1 | 0% | 1,554 | 3,724 | +140% | 0 | 0 | — |
case-16 | pass→pass | 13,993 | 7,825 | -44% | 1 | 1 | 0% | 2,450 | 4,759 | +94% | 0 | 0 | — |
case-17 | pass→pass | 7,703 | 4,227 | -45% | 1 | 1 | 0% | 1,431 | 3,951 | +176% | 0 | 0 | — |
case-18 | pass→pass | 4,778 | 3,935 | -18% | 1 | 1 | 0% | 654 | 3,787 | +479% | 0 | 0 | — |
case-19 | pass→pass | 14,837 | 9,162 | -38% | 1 | 1 | 0% | 2,377 | 4,689 | +97% | 0 | 0 | — |
case-20 | pass→pass | 7,715 | 4,952 | -36% | 1 | 1 | 0% | 1,382 | 4,121 | +198% | 0 | 0 | — |
case-21 | pass→pass | 8,749 | 7,884 | -10% | 1 | 1 | 0% | 1,730 | 4,803 | +178% | 0 | 0 | — |
case-22 | pass→pass | 10,645 | 6,901 | -35% | 1 | 1 | 0% | 2,060 | 4,508 | +119% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +23 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.