/devsecops-skills:policy-as-code-guardrailsfrom devsecops-skills
Guides writing OPA/Rego, Kyverno, and Conftest policies for Kubernetes admission control, IaC (Terraform/CloudFormation) review, and CI/CD gates. Clarifies enforcement scope and limits.
Ships in devsecops-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
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.
apply runs.ConstraintTemplate/Constraint CRDs so Rego policies integrate with
standard Kubernetes admission webhooks and audit results as native
resources.audit/dry-run
mode first, not enforce, to surface what it would have blocked
against real traffic/manifests before it can cause an outage.Start with an audit-only policy to see impact before blocking anything. Kyverno example requiring non-root containers, in audit mode:
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
Write the equivalent in Rego/OPA/Gatekeeper if standardizing on OPA instead:
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])
}
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.
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:
spec:
validationFailureAction: Enforce
rules:
- name: check-runAsNonRoot
exclude:
resources:
namespaces: ["legacy-migration"] # documented, time-boxed exception
Add IaC-time policy checks with Conftest so violations are caught
before terraform apply, not after a resource already exists:
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])
}
# 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/
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:
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/*"
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:
opa test policies/ -v
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.
kubectl apply/console access or a misconfigured pipeline, whereas an
admission-time policy applies regardless of how the request arrived.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."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.
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):
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):
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:
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:
- 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
beforeapplyever runs, and a pod withoutrunAsNonRootis rejected by the API server at creation time regardless of whether it came from CI or a directkubectl apply`.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
policy-as-code, opa, kyverno, kubernetes-security, iac-security, devsecops
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin devsecops-skills17 skills in this plugin
/checkov-and-tfsec-iac-security-scanningConfigures Checkov and tfsec to scan Terraform/CloudFormation/Kubernetes IaC source for misconfigurations — hardcoded secrets, overly permissive IAM policies, unencrypted storage, open security groups — before `terraform apply` or `aws cloudformation deploy` ever runs. Use when a user asks to "scan my Terraform for security issues," "add Checkov/tfsec to CI," "find overly permissive IAM in my IaC," "write a Checkov suppression," "compare Checkov and tfsec," or "catch misconfigured cloud resources before they're provisioned."
/complete-devsecops-pipeline-for-kubernetes-from-scratchBuilds the complete security-gate sequence for a Kubernetes-targeted DevSecOps pipeline from scratch — SAST, SCA, container image scanning, a policy-as-code (OPA/Kyverno) admission gate placed before the GitOps handoff, and a secrets model where plaintext credentials never touch the pipeline because an External Secrets Operator pulls them cluster-side instead. Use when the user asks to "build a DevSecOps pipeline for Kubernetes from scratch," "add a policy gate before our GitOps commit," "make sure secrets never touch CI for our K8s deploys," or "sequence SAST/SCA/image-scan/policy gates for a container pipeline end-to-end."
/complete-devsecops-pipeline-for-serverless-from-scratchBuilds the complete security-gate sequence for a serverless (Lambda- style) DevSecOps pipeline from scratch — SAST, SCA against a much larger bundled transitive-dependency surface, package/artifact signing, and least-privilege execution-role scoping as the primary release gate (rather than a Kubernetes-style admission policy), with runtime secrets fetched from a managed secrets service instead of baked into the deployment package. Use when the user asks to "build a DevSecOps pipeline for a Lambda function from scratch," "gate a serverless release on execution-role scope instead of admission policy," "sign a Lambda deployment package," or "make sure a Lambda function's secrets aren't baked into its zip."
/complete-devsecops-pipeline-for-vm-based-workloads-from-scratchBuilds the complete security-gate sequence for a VM-based DevSecOps pipeline from scratch — SAST, SCA, golden-image scanning and CIS benchmark hardening baked into the machine image itself, config- management-applied secrets via Ansible Vault (never plaintext), and an ongoing patch/drift-detection gate that runs independently of any single deploy, distinct from the per-deploy scanning in the Kubernetes and serverless variants. Use when the user asks to "build a DevSecOps pipeline for EC2/VM workloads from scratch," "harden a golden AMI to CIS benchmarks in the pipeline," "apply secrets with Ansible Vault instead of plaintext," or "add ongoing patch/drift detection for a VM fleet."
/container-image-hardeningGuides building minimal, non-root, hardened container images — multi-stage builds, distroless/minimal base images, non-root users, read-only root filesystems, dropped Linux capabilities, and pinned/ digest-referenced base images. Use when the user asks to "harden this Dockerfile", "reduce container image attack surface/size", "make our container run as non-root", "switch to a distroless base image", "fix a container security scan finding about running as root or having a shell", or "set Kubernetes securityContext restrictions for a pod".
/critical-vulnerability-emergency-responseGuides the accelerated, out-of-band response when a critical CVE is publicly disclosed in a widely-used dependency, library, or platform component — rapid exposure assessment across the fleet, patch or temporary mitigation, verification, and closeout. Use when the user says "a critical CVE just dropped in X", "we need to check if we're affected by CVE-YYYY-NNNNN", "log4shell-style emergency patching", "zero-day just disclosed in our dependency", or "run an emergency vulnerability response". Distinct from the steady-state finding-queue management in security-finding-backlog-triage — this is the fire-drill process for one urgent, time-critical disclosure.
Other plugins' skills for the same job