implementing-policy-as-code-with-open-policy-agent

本技能涵盖在 Kubernetes 和 CI/CD 管道中实施 Open Policy Agent(OPA)和 Gatekeeper 进行策略即代码执行。 内容包括编写 Rego 策略、将 OPA Gatekeeper 部署为 Kubernetes 准入控制器、在开发中测试策略, 以及将策略评估集成到部署管道中。

9 stars

Best use case

implementing-policy-as-code-with-open-policy-agent is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

本技能涵盖在 Kubernetes 和 CI/CD 管道中实施 Open Policy Agent(OPA)和 Gatekeeper 进行策略即代码执行。 内容包括编写 Rego 策略、将 OPA Gatekeeper 部署为 Kubernetes 准入控制器、在开发中测试策略, 以及将策略评估集成到部署管道中。

Teams using implementing-policy-as-code-with-open-policy-agent should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/implementing-policy-as-code-with-open-policy-agent/SKILL.md --create-dirs "https://raw.githubusercontent.com/killvxk/cybersecurity-skills-zh/main/skills/implementing-policy-as-code-with-open-policy-agent/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/implementing-policy-as-code-with-open-policy-agent/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How implementing-policy-as-code-with-open-policy-agent Compares

Feature / Agentimplementing-policy-as-code-with-open-policy-agentStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

本技能涵盖在 Kubernetes 和 CI/CD 管道中实施 Open Policy Agent(OPA)和 Gatekeeper 进行策略即代码执行。 内容包括编写 Rego 策略、将 OPA Gatekeeper 部署为 Kubernetes 准入控制器、在开发中测试策略, 以及将策略评估集成到部署管道中。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# 使用 Open Policy Agent 实施策略即代码

## 使用场景

- 以编程方式跨 Kubernetes 集群强制执行组织安全策略时
- 需要准入控制以阻止创建不合规资源时
- 实施可进行版本控制、测试和审计的策略治理时
- 跨多个集群和环境标准化安全规则时
- 需要可扩展到 Kubernetes 之外(API 和 CI/CD)的灵活策略引擎时

**不适用于**漏洞扫描(使用 Trivy/Checkov)、运行时威胁检测(使用 Falco)或网络策略执行(使用 Kubernetes NetworkPolicy 或 Calico)。

## 前置条件

- 具有管理员访问权限的 Kubernetes 集群(用于 Gatekeeper 安装)
- 用于 Gatekeeper 部署的 Helm
- 用于本地策略测试的 OPA CLI 或 conftest
- 用于策略编写的 Rego 知识

## 操作流程

### 步骤 1:安装 OPA Gatekeeper

```bash
# 通过 Helm 安装 Gatekeeper
helm repo add gatekeeper https://open-policy-agent.github.io/gatekeeper/charts
helm install gatekeeper gatekeeper/gatekeeper \
  --namespace gatekeeper-system --create-namespace \
  --set replicas=3 \
  --set audit.replicas=1 \
  --set audit.writeToRAMDisk=true
```

### 步骤 2:创建约束模板

```yaml
# templates/k8s-required-labels.yaml
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
  name: k8srequiredlabels
spec:
  crd:
    spec:
      names:
        kind: K8sRequiredLabels
      validation:
        openAPIV3Schema:
          type: object
          properties:
            labels:
              type: array
              items:
                type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8srequiredlabels
        violation[{"msg": msg}] {
          provided := {label | input.review.object.metadata.labels[label]}
          required := {label | label := input.parameters.labels[_]}
          missing := required - provided
          count(missing) > 0
          msg := sprintf("Missing required labels: %v", [missing])
        }

---
# templates/k8s-container-limits.yaml
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
  name: k8scontainerlimits
spec:
  crd:
    spec:
      names:
        kind: K8sContainerLimits
      validation:
        openAPIV3Schema:
          type: object
          properties:
            cpu:
              type: string
            memory:
              type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8scontainerlimits
        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          not container.resources.limits.cpu
          msg := sprintf("Container %v has no CPU limit", [container.name])
        }
        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          not container.resources.limits.memory
          msg := sprintf("Container %v has no memory limit", [container.name])
        }

---
# templates/k8s-block-privileged.yaml
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
  name: k8sblockprivileged
spec:
  crd:
    spec:
      names:
        kind: K8sBlockPrivileged
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8sblockprivileged
        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          container.securityContext.privileged == true
          msg := sprintf("Privileged container not allowed: %v", [container.name])
        }
        violation[{"msg": msg}] {
          container := input.review.object.spec.initContainers[_]
          container.securityContext.privileged == true
          msg := sprintf("Privileged init container not allowed: %v", [container.name])
        }
```

### 步骤 3:应用约束

```yaml
# constraints/require-labels.yaml
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
  name: require-team-labels
spec:
  enforcementAction: deny
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Namespace"]
      - apiGroups: ["apps"]
        kinds: ["Deployment", "StatefulSet"]
    excludedNamespaces:
      - kube-system
      - gatekeeper-system
  parameters:
    labels:
      - "team"
      - "environment"
      - "cost-center"

---
# constraints/block-privileged.yaml
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sBlockPrivileged
metadata:
  name: block-privileged-containers
spec:
  enforcementAction: deny
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
      - apiGroups: ["apps"]
        kinds: ["Deployment", "DaemonSet", "StatefulSet"]
    excludedNamespaces:
      - kube-system
```

### 步骤 4:使用 conftest 测试策略

```bash
# 安装 conftest
brew install conftest

# 在本地针对 OPA 策略测试 Kubernetes 清单
conftest test deployment.yaml --policy policies/ --output json

# 针对 OPA 策略测试 Terraform
conftest test terraform/main.tf --policy policies/terraform/ --parser hcl2

# 测试 Dockerfile
conftest test Dockerfile --policy policies/docker/
```

```rego
# policies/kubernetes/deny_latest_tag.rego
package kubernetes

deny[msg] {
  input.kind == "Deployment"
  container := input.spec.template.spec.containers[_]
  endswith(container.image, ":latest")
  msg := sprintf("Container %v uses :latest tag. Pin to specific version.", [container.name])
}

deny[msg] {
  input.kind == "Deployment"
  container := input.spec.template.spec.containers[_]
  not contains(container.image, ":")
  msg := sprintf("Container %v has no tag. Pin to specific version.", [container.name])
}
```

### 步骤 5:在 CI/CD 中集成策略测试

```yaml
# .github/workflows/policy-test.yml
name: Policy Validation

on:
  pull_request:
    paths: ['k8s/**', 'terraform/**', 'policies/**']

jobs:
  conftest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install conftest
        run: |
          wget -q https://github.com/open-policy-agent/conftest/releases/download/v0.50.0/conftest_0.50.0_Linux_x86_64.tar.gz
          tar xzf conftest_0.50.0_Linux_x86_64.tar.gz
          sudo mv conftest /usr/local/bin/
      - name: Test K8s manifests
        run: conftest test k8s/**/*.yaml --policy policies/kubernetes/ --output json
      - name: Test Terraform
        run: conftest test terraform/*.tf --policy policies/terraform/ --parser hcl2
```

## 关键概念

| 术语 | 定义 |
|------|------|
| OPA | Open Policy Agent — 使用 Rego 语言进行策略决策的通用策略引擎 |
| Rego | OPA 用于编写策略规则的声明性查询语言 |
| Gatekeeper | 实现通过 ConstraintTemplates 进行准入控制的 Kubernetes 原生 OPA 集成 |
| ConstraintTemplate | 定义 Rego 策略逻辑和约束类参数模式的 CRD |
| Constraint | ConstraintTemplate 的实例,包含特定参数和范围(检查哪些资源) |
| 准入控制器 | 在持久化之前拦截 API 请求并可允许或拒绝的 Kubernetes 组件 |
| conftest | 用于针对 OPA 策略测试结构化数据(YAML、JSON、HCL)的 CLI 工具 |

## 工具与系统

- **Open Policy Agent(OPA)**:用于统一策略执行的通用策略引擎
- **Gatekeeper**:基于 OPA 构建的 Kubernetes 准入控制器,使用基于 CRD 的配置
- **conftest**:针对配置文件测试 OPA 策略的测试框架
- **Kyverno**:使用基于 YAML 策略的替代 Kubernetes 策略引擎(无需 Rego)
- **Styra DAS**:具有策略编写、测试和分发功能的商业 OPA 管理平台

## 常见场景

### 场景:跨集群强制执行容器安全标准

**背景**:多个开发团队部署到共享的 Kubernetes 集群。部分团队运行特权容器和没有资源限制的镜像,导致安全和稳定性问题。

**方法**:
1. 通过 GitOps(FluxCD 仓库中的 Helm chart)在所有集群上部署 Gatekeeper
2. 创建 ConstraintTemplates:禁止特权容器、必须资源限制、必须标签、禁用 latest 标签
3. 从 `enforcementAction: warn` 开始,识别违规而不阻止部署
4. 通知团队违规情况,并提供 2 周的修复窗口
5. 修复期后切换到 `enforcementAction: deny`
6. 为 kube-system 和监控命名空间添加 `excludedNamespaces`

**注意事项**:立即以 deny 模式部署 Gatekeeper 可能会破坏现有工作负载。始终从 warn 模式开始。过于严格且没有系统命名空间豁免的策略可能阻止集群组件正常运行。

## 输出格式

```
OPA 策略评估报告
==============================
集群:production-east
日期:2026-02-23
Gatekeeper 版本:3.16.0

约束摘要:
  K8sRequiredLabels:        12 个违规(warn)
  K8sBlockPrivileged:        0 个违规(deny)
  K8sContainerLimits:        8 个违规(deny)
  K8sBlockLatestTag:         3 个违规(deny)

已拦截部署(deny):
  [K8sContainerLimits] deployment/api-server in ns/payments
    - 容器 'api' 没有内存限制
  [K8sBlockLatestTag] deployment/frontend in ns/web
    - 容器 'nginx' 使用 :latest 标签

审计违规(warn):
  [K8sRequiredLabels] namespace/staging
    - 缺少标签:{cost-center}
```

Related Skills

testing-for-open-redirect-vulnerabilities

9
from killvxk/cybersecurity-skills-zh

通过分析 URL 重定向参数、绕过技术和利用链,识别并测试 Web 应用程序中的开放重定向漏洞,用于网络钓鱼和 Token 窃取。

performing-open-source-intelligence-gathering

9
from killvxk/cybersecurity-skills-zh

开源情报(OSINT)收集是红队演练的第一个主动阶段,操作员收集关于目标组织的公开可用信息,以识别攻击面、社会工程学目标、技术栈和凭据泄露情况。

performing-dmarc-policy-enforcement-rollout

9
from killvxk/cybersecurity-skills-zh

执行从 p=none 监控到 p=quarantine 再到 p=reject 执行的分阶段 DMARC 推进,确保所有合法邮件来源在封锁未授权发件人之前完成认证。

performing-content-security-policy-bypass

9
from killvxk/cybersecurity-skills-zh

通过利用错误配置、JSONP 端点、不安全指令和策略注入技术,分析并绕过内容安全策略(CSP)实现,以实现跨站脚本攻击。

performing-authenticated-scan-with-openvas

9
from killvxk/cybersecurity-skills-zh

使用 OpenVAS/Greenbone 漏洞管理(GVM)框架配置并执行凭据认证漏洞扫描,支持 SSH 和 SMB 凭据,实现全面的主机级别安全评估。

implementing-zero-trust-with-hashicorp-boundary

9
from killvxk/cybersecurity-skills-zh

使用 HashiCorp Boundary 实现具备动态凭据代理、会话录制和 Vault 集成的身份感知零信任基础设施访问管理。

implementing-zero-trust-with-beyondcorp

9
from killvxk/cybersecurity-skills-zh

使用身份感知代理(IAP,Identity-Aware Proxy)、上下文感知访问策略、设备信任验证和 Access Context Manager,部署 Google BeyondCorp Enterprise 零信任访问控制,对 GCP 资源和内部应用强制执行基于身份和安全态势的访问。

implementing-zero-trust-network-access

9
from killvxk/cybersecurity-skills-zh

通过配置身份感知代理、微分段、基于条件访问策略的持续验证,以及在 AWS、Azure 和 GCP 环境中以 BeyondCorp 风格的架构替代传统 VPN 访问,在云环境中实施零信任网络访问(ZTNA)。

implementing-zero-trust-network-access-with-zscaler

9
from killvxk/cybersecurity-skills-zh

使用 Zscaler 实施零信任网络访问(Zero Trust Network Access,ZTNA),通过 Zscaler Private Access(ZPA)配置应用分段、访问策略和连接器,替代传统 VPN 架构

implementing-zero-trust-in-cloud

9
from killvxk/cybersecurity-skills-zh

本技能指导组织按照 NIST SP 800-207 和 Google BeyondCorp 原则在云环境中实施零信任(Zero Trust)架构,涵盖以身份为中心的访问控制、微分段(Micro-Segmentation)、持续验证、设备信任评估,以及部署身份感知代理(Identity-Aware Proxy)以消除 AWS、Azure 和 GCP 环境中的隐式网络信任。

implementing-zero-trust-for-saas-applications

9
from killvxk/cybersecurity-skills-zh

使用 CASB、SSPM、条件访问策略、OAuth 应用治理和会话控制,为 SaaS 应用实施零信任访问控制, 对云托管服务强制执行身份验证、设备合规性检查和数据保护。

implementing-zero-trust-dns-with-nextdns

9
from killvxk/cybersecurity-skills-zh

将 NextDNS 实施为零信任 DNS 过滤层,提供加密解析、威胁情报阻断、隐私保护,以及跨所有端点的组织策略执行。