plexsystems/konstraint
GitHub: plexsystems/konstraint
一个面向 OPA Gatekeeper 的策略管理 CLI 工具,自动将 Rego 策略转化为 ConstraintTemplate 并生成配套文档。
Stars: 392 | Forks: 52
# Konstraint
[](https://goreportcard.com/report/github.com/plexsystems/konstraint)
[](https://github.com/plexsystems/konstraint/releases)

Konstraint 是一个 CLI 工具,用于在使用 [Gatekeeper](https://github.com/open-policy-agent/gatekeeper) 时协助创建和管理模板与约束。
## 安装
```
go install github.com/plexsystems/konstraint@latest
```
每个版本还提供了 Docker 镜像:
```
docker run -v $PWD:/konstraint ghcr.io/plexsystems/konstraint create /konstraint/examples
```
## 使用说明
要创建 Gatekeeper 资源,请使用 `konstraint create `。
要生成随附的文档,请使用 `konstraint doc `。
这两个命令都支持 `--output` 标志来指定保存输出的位置。有关更详细的用法文档,请参阅 [CLI 文档](docs/cli/konstraint.md)。
## 为什么会有这个工具
### 自动将 Rego 复制到 ConstraintTemplate
在为 Gatekeeper 编写策略时,必须将 Rego 添加到 [ConstraintTemplates](https://github.com/open-policy-agent/gatekeeper#constraint-templates) 中,Gatekeeper 才能执行该策略。这就产生了一种情况:Rego 是在 `.rego` 文件中编写的,然后被复制到 ConstraintTemplate 中。当需要修改 Rego 时,两处内容都必须更新。
### 库更改时自动更新所有 ConstraintTemplate
Gatekeeper 支持使用 `libs` 字段将 _库_ 导入到 `ConstraintTemplates` 中。如果导入的库需要更改,则必须更新每个模板以包含此新更改。
### 支持为 Conftest 和 Gatekeeper 编写相同的策略
使用 Gatekeeper 时,策略是在 [AdmissionReview](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#webhook-request-and-response) 的上下文中进行评估的。这意味着策略通常以 `input.review.object` 作为前缀编写。
而使用 [Conftest](https://github.com/open-policy-agent/conftest) 时,策略是针对 `yaml` 文件编写的。
这就造成了一种情况:需要根据评估策略的上下文,以不同的方式编写策略。
### Kubernetes 库
在 [examples/lib](examples/lib) 目录中,有多个库支持同时为 Conftest 和 Gatekeeper 编写策略。
您可以根据需要在策略中包含任意数量的这些库。
#### 目的
通过首先在本地计算机上使用 `Conftest` 验证 Kubernetes 清单,我们可以捕获到原本会违反策略的清单,而无需部署到运行 Gatekeeper 的集群中。
## 常见问题
**Konstraint 运行没有报错,但看不到任何新文件。**
这通常意味着没有找到策略,或者策略没有任何 `violation[]` 规则,因此它们与 Gatekeeper 不兼容。
有关更多信息,请参阅[如何创建约束](docs/constraint_creation.md)。
**我的 ConstraintTemplate 缺少输入参数**
可以通过在注释头块中使用一个或多个 `@parameter ` 标签来指定输入参数。如果您使用了输入参数,Konstraint 将跳过为该策略生成 `Constraint` 资源。
有关更多信息,请参阅[使用输入参数](docs/constraint_creation.md#using-input-parameters)。
标签:Chrome Headless, Conftest, DevSecOps, EVTX分析, Go语言, Groq API, K8s策略, Kubernetes安全, OPA, Rego, Web截图, 上游代理, 代码复用, 子域名突变, 安全基线, 容器安全, 教学环境, 文档生成, 日志审计, 程序破解, 策略即代码, 约束模板, 聊天机器人安全, 自动化合规, 请求拦截, 靶场