Logo van Policy As Code Guardrails

Policy As Code Guardrails

Community
selvarajmurugesan90
policy-as-code-guardrails

Guides writing and enforcing security/compliance policies as code using Open Policy Agent (OPA)/Rego, Kyverno, or Conftest, applied to Kubernetes admission control, CI/CD pipeline gates, and Infrastructure as Code (Terraform/CloudFormation) review. Use when the user asks to "write an OPA policy", "block insecure Terraform/Kubernetes configs", "add admission control guardrails", "enforce that all images must be signed/non-root/from an approved registry", or "codify our security baseline as automated policy instead of a manual checklist". Explains what enforcement actually guarantees versus what still needs separate controls.

Overzicht

Uitgeverselvarajmurugesan90
Repositoryops-engineering-skills
Skillnaampolicy-as-code-guardrails
Sterren
44
Forks
18
Meegeleverde bestanden
Alleen instructies
LicentieApache-2.0
Links
  • Instructies in Markdown

    Het model laadt het bestand SKILL.md alleen als dat nodig is. Dat kost dus alleen tokens als een verzoek daadwerkelijk bij de skill past.

  • Werkt met elk LLM

    AI-skills bestaan uit instructies in gewone Markdown, niet uit aanbiederspecifieke code. Daardoor werken ze met GPT, Claude, Gemini, Grok of een lokaal model.

  • Compleet zonder extra bestanden

    Alles wat het model nodig heeft, staat in de instructies — geen extra bestanden om te synchroniseren.

  • Open source

    Gepubliceerd door selvarajmurugesan90 op GitHub. Bekijk de bronbestanden voordat je de skill installeert.

Installatie

Installeer de AI-skill Policy As Code Guardrails in TypingMind om deze met elk LLM te gebruiken, of voeg de skill toe aan een andere agent die SKILL.md kan lezen.

1

Installeren in TypingMind

TypingMind installeert een skill rechtstreeks vanuit de GitHub-map. De app leest SKILL.md, bundelt de bijbehorende bestanden en slaat het resultaat lokaal op.

  1. Open de app en ga naar Plug-ins → Skills.
  2. Kies "Installeren via GitHub".
  3. Plak de onderstaande URL van de skillmap en bevestig.
    https://github.com/selvarajmurugesan90/ops-engineering-skills/tree/main/plugins/devsecops/skills/policy-as-code-guardrails
  4. Schakel de skill in voor elke chat waarin je de skill wilt kunnen gebruiken.
Plug-ins → Skills → Skill toevoegen → Via GitHub-URL. Plak vervolgens de map-URL en druk op Doorgaan.
2

Installeren in een andere agent

Elke agent die het Agent Skills-formaat kan lezen, kan deze skill gebruiken — kopieer de map naar de skillmap van die agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/selvarajmurugesan90/ops-engineering-skills.git /tmp/ops-engineering-skills
mkdir -p .claude/skills
cp -r /tmp/ops-engineering-skills/plugins/devsecops/skills/policy-as-code-guardrails .claude/skills/policy-as-code-guardrails
Start Claude Code na het kopiëren opnieuw op, zodat de nieuwe skill wordt herkend.

Gebruik deze skill in TypingMind

Schakel Policy As Code Guardrails in voor een chat naar keuze in TypingMind en het model doet de rest. De naam en beschrijving staan in de systeemprompt. Zodra een verzoek bij de skill past, laadt het model zelf de volledige instructies. Je hoeft de skill nooit handmatig aan te roepen. Pas als het model de skill daadwerkelijk gebruikt, kost dat tokens.

Het model laadt Policy As Code Guardrails automatisch zodra een verzoek erbij past.

Werkt met elk AI-model

AI-skills zijn gewone Markdown-instructies in plaats van aanbiederspecifieke code. Daardoor is Policy As Code Guardrails niet gebonden aan het model waarvoor de skill is geschreven. Installeer de skill één keer in TypingMind en gebruik deze met GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama of een lokaal model dat je zelf draait — allemaal met je eigen API-sleutels.

  • Alleen geladen wanneer nodig

    De systeemprompt bevat alleen de naam en beschrijving. De instructies worden bij het eerste bijpassende verzoek opgehaald, dus een ongebruikte skill kost niets.

  • Wissel van model tijdens een chat

    Omdat de skill uit instructies bestaat en niet uit code, blijft deze werken als je van model wisselt — het volgende model leest hetzelfde SKILL.md-bestand.

Skillinstructies

Dit is de inhoud van SKILL.md die het model laadt. Lees deze vóór de installatie — een skill bestaat uit instructies die je model zal volgen.

Policy as Code & Guardrails

Purpose

Security and compliance requirements ("containers must not run as root", "S3 buckets must not be public", "images must come from an approved registry and be signed") are traditionally enforced through manual review checklists or after-the-fact audits — both slow, inconsistent, and easy to skip under deadline pressure. Policy as Code expresses these requirements as machine-readable, version-controlled rules (Rego for Open Policy Agent, Kyverno's YAML-based policies, Sentinel for HashiCorp products) and enforces them automatically at a control point: a Kubernetes admission webhook rejecting a non-compliant pod at creation time, a CI step failing a plan that would create a public S3 bucket, a pre-merge check blocking a Dockerfile that runs as root. This turns security requirements from documentation into guardrails that hold even when nobody remembers to check the checklist — but it only guarantees what the policy actually checks, and only at the point where it's wired in; a policy engine is not a substitute for the underlying controls (scanning, signing, least privilege) it's verifying the presence of.

When to use

  • The user asks to "write an OPA/Rego policy" or "add Kyverno policies" for a Kubernetes cluster.
  • The user wants to block Terraform/CloudFormation/Pulumi plans that would create insecure infrastructure (public storage buckets, unencrypted volumes, overly permissive security groups/IAM) before apply runs.
  • The user wants to enforce that container images are signed, scanned, non-root, or from an approved registry, at the Kubernetes admission layer rather than trusting CI alone.
  • A security team wants to "codify" a manual review checklist so it becomes an automated, consistent gate instead of a document reviewers interpret inconsistently.
  • The user is troubleshooting a policy that's too strict (blocking legitimate deploys) or too permissive (letting through what it should block), and needs help writing/testing Rego or Kyverno rules.
  • The user wants a "dry-run"/audit mode before switching a new policy to enforcing, to avoid a surprise outage from a policy rollout.

Prerequisites & environment

  • Open Policy Agent (OPA) + Rego — general-purpose policy engine usable for Kubernetes admission (via Gatekeeper), CI/CD gates, API authorization, and more; steeper learning curve (Rego is its own declarative language) but the most flexible and widely adopted option.
  • Gatekeeper — OPA's Kubernetes-native wrapper, providing ConstraintTemplate/Constraint CRDs so Rego policies integrate with standard Kubernetes admission webhooks and audit results as native resources.
  • Kyverno — Kubernetes-native alternative to OPA/Gatekeeper using plain YAML instead of Rego; lower learning curve for teams already comfortable with Kubernetes manifests, slightly less general-purpose outside the Kubernetes context.
  • Conftest — runs OPA/Rego policies against structured config files (Terraform plan JSON, Kubernetes YAML, Dockerfiles via a parser) outside a live cluster, useful for CI-time IaC policy checks before anything is applied.
  • HashiCorp Sentinel — policy-as-code tied specifically to Terraform Cloud/Enterprise; relevant if already standardized on that platform.
  • A Kubernetes cluster with admission webhook support (standard in any reasonably current distribution) if enforcing at the cluster level; cluster-admin or equivalent to install Gatekeeper/Kyverno.
  • A staged rollout plan: every new policy should run in audit/dry-run mode first, not enforce, to surface what it would have blocked against real traffic/manifests before it can cause an outage.

Step-by-step guidance

  1. Start with an audit-only policy to see impact before blocking anything. Kyverno example requiring non-root containers, in audit mode:

    yaml
    apiVersion: kyverno.io/v1
    kind: ClusterPolicy
    metadata:
      name: require-run-as-non-root
    spec:
      validationFailureAction: Audit   # start here, not Enforce
      rules:
        - name: check-runAsNonRoot
          match:
            resources:
              kinds: [Pod]
          validate:
            message: "Containers must set runAsNonRoot: true"
            pattern:
              spec:
                securityContext:
                  runAsNonRoot: true
  2. Write the equivalent in Rego/OPA/Gatekeeper if standardizing on OPA instead:

    rego
    package k8s.security
    
    deny[msg] {
      input.request.kind.kind == "Pod"
      container := input.request.object.spec.containers[_]
      not container.securityContext.runAsNonRoot
      msg := sprintf("container %v must set runAsNonRoot: true", [container.name])
    }
  3. Review audit results for a full deploy cycle (at least a week, or however long covers your normal deployment cadence) before flipping to enforcing — this catches legitimate workloads the policy would have broken (e.g. a base image that genuinely needs root for a startup step) so you can add a scoped exception rather than breaking production on rollout day.

  4. Switch to enforcing once audit is clean, and add a documented, narrowly-scoped exception mechanism for genuine exceptions rather than disabling the policy for a whole namespace:

    yaml
    spec:
      validationFailureAction: Enforce
      rules:
        - name: check-runAsNonRoot
          exclude:
            resources:
              namespaces: ["legacy-migration"]   # documented, time-boxed exception
  5. Add IaC-time policy checks with Conftest so violations are caught before terraform apply, not after a resource already exists:

    rego
    package terraform.security
    
    deny[msg] {
      resource := input.resource_changes[_]
      resource.type == "aws_s3_bucket"
      resource.change.after.acl == "public-read"
      msg := sprintf("S3 bucket %v must not be public-read", [resource.address])
    }
    yaml
    # CI step
    - name: Terraform plan
      run: terraform plan -out=plan.tfplan && terraform show -json plan.tfplan > plan.json
    - name: Conftest policy check
      run: conftest test plan.json --policy policies/
  6. Enforce supply-chain policies at admission (signed images, approved registries) as a defense-in-depth layer even when CI already checks these, since CI checks can be bypassed by anyone who can push directly to the registry or apply manifests outside the pipeline:

    yaml
    apiVersion: kyverno.io/v1
    kind: ClusterPolicy
    metadata:
      name: restrict-image-registries
    spec:
      validationFailureAction: Enforce
      rules:
        - name: allowed-registries
          match:
            resources:
              kinds: [Pod]
          validate:
            message: "Images must come from approved registry"
            pattern:
              spec:
                containers:
                  - image: "registry.example.internal/*"
  7. Unit-test policies themselves, not just run them against live traffic — both OPA (opa test) and Kyverno (kyverno test) support writing test cases with expected allow/deny outcomes:

    bash
    opa test policies/ -v
  8. Version-control policies and review changes like code (PR review, changelog) — a policy change is a security-relevant change to production behavior and should go through the same rigor as application code, including a rollback plan.

Best practices

  • Always roll out new policies in audit/dry-run mode first, and review actual audit hits before enforcing — this is the single highest-value habit for avoiding "the new policy broke prod on a Friday."
  • Keep policies narrowly scoped and composable (one policy per concern: non-root, registry allowlist, resource limits) rather than one giant monolithic policy — easier to test, easier to grant a scoped exception to one rule without disabling everything else.
  • Enforce supply-chain and admission policies as defense-in-depth even when the same check exists in CI — CI can be bypassed by direct kubectl apply/console access or a misconfigured pipeline, whereas an admission-time policy applies regardless of how the request arrived.
  • Be explicit about what a passing policy check actually proves: a policy requiring runAsNonRoot: true proves that field is set — it says nothing about whether the application itself is secure, whether its image is vulnerability-free, or whether it's from a trusted source. Don't let "policy passed" get conflated with "workload is safe."
  • Time-box and document every exception (a namespace exclusion, a policy override annotation) with an owner and a review date — undocumented, permanent exceptions are how guardrails quietly become no-ops.
  • Unit test policies against both cases that should pass and cases that should fail — a policy that's never been tested against a should-be-rejected input can silently be a no-op due to a typo in a field path.

Common pitfalls

  • Symptom: A new Gatekeeper/Kyverno policy is deployed directly in Enforce mode and immediately blocks a legitimate deployment, triggering an incident. Fix: Always deploy new policies in Audit/dry-run first, review what they would have blocked over a representative time window, then switch to enforcing with documented exceptions for anything legitimate the audit surfaced.

  • Symptom: A Rego policy compiles and runs without error but never actually denies anything, even for input that should clearly violate it. Fix: Check the exact JSON path being matched against the real admission request/plan JSON (log or dump input and compare field names/nesting) — a common cause is a typo'd field path or an assumption about input structure that doesn't match what Gatekeeper or Conftest actually passes in.

  • Symptom: The team believes "we have policy enforcement, so we're compliant," and treats it as equivalent to a full security review. Fix: Clarify explicitly what the policy checks (e.g. "runAsNonRoot is set") versus what it doesn't (image content, application logic, business risk) — policy as code automates checking for the presence of specific, narrowly-defined properties; it is not a substitute for broader review or for controls like sast-integration and software-composition-analysis-sca that check different things entirely.

  • Symptom: A policy is bypassed because a workload was applied directly to the cluster by an admin outside the normal CI/CD path, and the equivalent CI-time check never ran. Fix: This is expected if enforcement only exists in CI — add the same policy at the admission-controller layer (Gatekeeper/Kyverno) so it applies regardless of how the resource was created, not only to pipeline-originated changes.

  • Symptom: Namespace-wide exceptions accumulate over time ("legacy", "migration", "temp-exception") and are never revisited, quietly hollowing out the policy's effectiveness. Fix: Require an expiry/review date and owner on every exception at creation time, and schedule periodic (e.g. quarterly) exception reviews to close out ones that are no longer needed.

Worked example

A platform team codifies three baseline Kubernetes security requirements as Kyverno policies, rolling them out audit-first, plus an OPA/Conftest check blocking public S3 buckets in Terraform CI.

policies/require-non-root.yaml (rolled out in Audit, then Enforce after a clean week):

yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: require-run-as-non-root
spec:
  validationFailureAction: Enforce
  background: true
  rules:
    - name: check-runAsNonRoot
      match:
        resources:
          kinds: [Pod]
      exclude:
        resources:
          namespaces: ["legacy-migration"]  # exception owner: platform-team, review: 2026-10-01
      validate:
        message: "Containers must set securityContext.runAsNonRoot: true"
        pattern:
          spec:
            securityContext:
              runAsNonRoot: true

policies/s3-public-block.rego (checked via Conftest in CI before terraform apply):

rego
package terraform.security

deny[msg] {
  resource := input.resource_changes[_]
  resource.type == "aws_s3_bucket_acl"
  resource.change.after.acl == "public-read"
  msg := sprintf("%v must not set a public-read ACL", [resource.address])
}

deny[msg] {
  resource := input.resource_changes[_]
  resource.type == "aws_s3_bucket_public_access_block"
  resource.change.after.block_public_acls == false
  msg := sprintf("%v must set block_public_acls = true", [resource.address])
}

opa test case verifying the policy actually fires on bad input:

rego
package terraform.security

test_public_bucket_denied {
  results := deny with input as {
    "resource_changes": [{
      "type": "aws_s3_bucket_acl",
      "address": "aws_s3_bucket_acl.example",
      "change": {"after": {"acl": "public-read"}}
    }]
  }
  count(results) == 1
}

CI wiring:

yaml
- name: Conftest policy check
  run: |
    terraform show -json plan.tfplan > plan.json
    conftest test plan.json --policy policies/

Result: a plan that would set a public-read ACL fails CI with `FAIL - main

  • aws_s3_bucket_acl.example must not set a public-read ACLbeforeapplyever runs, and a pod withoutrunAsNonRootis rejected by the API server at creation time regardless of whether it came from CI or a directkubectl apply`.

Cross-references

  • secure-cicd-gates — where IaC policy checks (Conftest/OPA) fit relative to SAST/SCA gates in the overall pipeline.
  • secrets-management — a common policy-as-code target (e.g. "no plaintext secret fields in manifests") that this skill's enforcement mechanisms can codify.
  • container-image-hardening — many of the properties enforced by admission policies (non-root, read-only filesystem, no privilege escalation) originate as image/container build practices this skill only verifies are present.

Veelgestelde vragen

Wat doet de AI-skill Policy As Code Guardrails?

Guides writing and enforcing security/compliance policies as code using Open Policy Agent (OPA)/Rego, Kyverno, or Conftest, applied to Kubernetes admission control, CI/CD pipeline gates, and Infrastructure as Code (Terraform/CloudFormation) review. Use when the user asks to "write an OPA policy", "block insecure Terraform/Kubernetes configs", "add admission control guardrails", "enforce that all images must be signed/non-root/from an approved registry", or "codify our security baseline as automated policy instead of a manual checklist". Explains what enforcement actually guarantees versus w...

Waarom Policy As Code Guardrails gebruiken in TypingMind?

Omdat je deze skill één keer installeert en met elk model kunt gebruiken. Policy As Code Guardrails bestaat uit gewone Markdown in plaats van aanbiederspecifieke code. Dezelfde skill werkt dus met GPT-5, Claude, Gemini, Grok of een lokaal model. Ook als je midden in een chat van model wisselt, blijft de skill werken. TypingMind werkt met je eigen API-sleutels, dus je betaalt aanbieders rechtstreeks in plaats van een abonnement per gebruikersplek af te sluiten. Je skills en chats blijven in je eigen opslag.

Hoe installeer ik Policy As Code Guardrails in TypingMind?

Open Plug-ins → Skills → Installeren vanaf GitHub in TypingMind en plak https://github.com/selvarajmurugesan90/ops-engineering-skills/tree/main/plugins/devsecops/skills/policy-as-code-guardrails. TypingMind leest het bestand SKILL.md en installeert het als een skill die je per chat kunt inschakelen.

Welke AI-modellen kunnen Policy As Code Guardrails gebruiken?

Elk model dat je in TypingMind koppelt. AI-skills bestaan uit gewone Markdown-instructies in plaats van aanbiederspecifieke code. GPT, Claude, Gemini, Grok en lokale modellen kunnen deze skill dus allemaal laden wanneer een verzoek erbij past.

Hoeveel AI-modellen kan ik met Policy As Code Guardrails gebruiken?

Zoveel als je wilt. Je kunt Policy As Code Guardrails gebruiken met elk model dat skills ondersteunt, zoals GPT, Claude, Gemini, Grok, DeepSeek, Mistral of Llama. Allemaal in TypingMind met je eigen API-sleutels.

Is de AI-skill Policy As Code Guardrails gratis?

Ja. Deze skill is door selvarajmurugesan90 op GitHub gepubliceerd onder de licentie Apache-2.0. Je betaalt alleen je eigen AI-aanbieder voor de tokens die je gebruikt.

Wat zijn AI-skills?

Een AI-skill is een herbruikbare bundel instructies die een AI-model leert hoe het één specifieke taak moet uitvoeren. De skill volgt het open Agent Skills-formaat: het bestand SKILL.md bevat de naam en beschrijving van de skill. Daarnaast kan de skill scripts, sjablonen of referentiebestanden bevatten die het model nodig heeft. Het model leest de instructies alleen als je verzoek bij de skill past. Een geïnstalleerde skill kost dus niets zolang deze niet wordt gebruikt.

Waarin verschillen AI-skills van plug-ins of MCP-servers?

Een plug-in of MCP-server geeft een model nieuwe tools om aan te roepen: code die ergens wordt uitgevoerd en een resultaat teruggeeft. Een AI-skill geeft het model juist kennis en een werkwijze: hoe het een taak moet aanpakken, welke stappen het moet volgen en hoe goede uitvoer eruitziet. Skills bestaan uit gewone Markdown en hebben dus geen server, API-sleutel of runtime nodig. Ze werken met elk model.

Bekijk alles

Maak nu je eigen AI-werkruimte

Schrijf je in voor onze nieuwsbrief en blijf op de hoogte van nieuwe functies en toekomstige weggeefacties 👇