cairn-oss/cairn
GitHub: cairn-oss/cairn
Cairn 是一款本地优先的 IaC 审计工具,在单次扫描中融合成本与安全检查并直接生成可应用的修复补丁。
Stars: 0 | Forks: 0
Cairn
本地优先、云厂商无关的 IaC 审计工具,在单次扫描中融合成本与安全检查——覆盖 AWS、Azure、GCP、Kubernetes 及本地环境——并直接生成修复方案,而非仅仅标记问题。
Cairn 在**你的本地机器**上扫描 Terraform,一次性应用安全、成本、可靠性和治理规则,综合权衡它们之间的利弊,并提出具体且可直接应用的修复方案。 ``` $ cairn scan ./infra Cairn found 25 issue(s) in ./infra (6 cost, 6 governance, 3 reliability, 10 security): 1. [CRITICAL/SECURITY] aws_security_group.web (SEC001) at: infra/main.tf:3 problem: Ingress on port 22 is open to the entire internet (0.0.0.0/0) and covers SSH/RDP. fix: Restrict cidr_blocks to known ranges, or front the service with a load balancer or SSM Session Manager instead of exposing it directly. patch: cidr_blocks = ["10.0.0.0/8"] # replace with your trusted CIDR 9. [MEDIUM/COST] aws_instance.batch (COST001) problem: Instance type 'm5.4xlarge' is very large (~$561/month on-demand) and likely over-provisioned. saves: ~$420.48/month (estimate) patch: instance_type = "m5.xlarge" Trade-offs (cost x risk on the same resource): ⚖ aws_db_instance.main [COST + SECURITY] Sequence the security fix first, then right-size — resizing an exposed resource first just makes the breach cheaper to run. Estimated recoverable spend: ~$1,717.53/month 9 resource(s), 1 file(s), 0.12s. Local-only scan; nothing left this machine. ``` ## 为什么选择 Cairn 成本管理存在于一个工具中(如 Infracost、Kubecost),而安全检查则在另一个工具中(如 Checkov、Trivy),而且在做决策的那一刻,没有人去综合考量这两者。一个能省钱的变更可能会扩大你的攻击面——而没有任何扫描器会告诉你这一点。Cairn 在设计上与众不同: - **融合成本与安全。** 一次扫描,生成一份排名报告;当两者同时作用于同一资源时,提供明确的权衡建议,并在每项成本发现旁边附上预估的金额。 - **提供修复,而非仅仅标记。** 每个发现都附带浅显易懂的英文修复说明,并在安全的前提下提供可直接应用的 HCL 补丁。 - **架构设计上的本地优先。** 无 SaaS,无上传,无遥测。你的代码、密钥和基础设施数据永远不会离开你的机器。LLM 解释功能完全是可选的,并采用自带密钥(BYO-key)模式(或通过 Ollama 实现完全本地化)。 - **围绕扫描器的策略即代码。** 将你组织的标准——必需的标签、严重性阈值、遗留豁免——编码到一个可供审查的 `.cairn.yaml` 文件中。 - **为未来的运维而生。** 每一个操作都受[信任阶梯](docs/architecture.md#the-trust-ladder)约束:目前仅支持带有审计日志的只读模式;自动修复 PR 和基于策略的修复只有在建立信任之后才会推出。 ## 安装 需要 Python 3.10+。 ``` pip install cairn-iac ``` 或者从源码运行: ``` git clone https://github.com/cairn-oss/cairn && cd cairn pip install -e . ``` 或者通过 Docker 运行: ``` docker build -t cairn . && docker run --rm -v "$PWD:/scan" cairn scan /scan ``` ## 快速开始 ``` # 扫描目录(递归;跳过 .terraform/ 和 vendored 目录) cairn scan ./infra # 用于 CI 和代码扫描的机器格式 cairn scan ./infra --format json --output report.json cairn scan ./infra --format sarif --output report.sarif # GitHub Code Scanning cairn scan ./infra --format markdown --output report.md # paste into a PR # Gate CI:仅对 HIGH 或 CRITICAL 退出 1(默认),或选择你的界限 cairn scan ./infra --fail-on CRITICAL cairn scan ./infra --fail-on NEVER # report, never fail # 将修复起草为可供审查的提案(不作任何更改) cairn propose ./infra --output proposal.md # 此分支对账单有何影响?(如果超出预算则退出 1) cairn diff ./infra-main ./infra-branch # 查看所有规则 cairn rules ``` ### 策略即代码 将 `.cairn.yaml` 放在你的 Terraform 代码旁边: ``` min_severity: LOW fail_on: HIGH required_tags: [Owner, CostCenter] disabled_rules: [GOV001] severity_overrides: COST002: MEDIUM ignores: - rule: SEC001 resource: aws_security_group.legacy_* reason: grandfathered until the Q3 migration ``` 需要记录在案的例外情况?在代码行内为特定资源抑制某项规则,并且必须附带原因: ``` # cairn:ignore SEC001 reason=reachable only through the corporate VPN ``` 完整参考请查阅 [docs/configuration.md](docs/configuration.md)。 ### 可选的 AI 解释(自带密钥或完全本地化) Cairn 默认绝不会向任何地方发送任何数据。如果你需要定制化的解释,请配置一个 provider 并传入 `--explain`: ``` llm: provider: ollama # fully local — data still never leaves your machine model: llama3.1 ``` ``` cairn scan ./infra --explain ``` `openai` 和 `anthropic` provider 会使用你环境变量中自己的 API key(`OPENAI_API_KEY` / `ANTHROPIC_API_KEY`)。仅发送发现本身(规则、资源地址、消息)——绝不会发送你的文件。详情请见 [SECURITY.md](SECURITY.md)。 ### GitHub Actions ``` - uses: cairn-oss/cairn@v0 with: path: ./infra fail-on: HIGH ``` 或者将 SARIF 上传到 Code Scanning——请参阅 [docs/configuration.md#ci](docs/configuration.md#ci)。 ## 检查内容(跨 5 个 provider 的 42 条规则) | 类别 | 示例 | |---|---| | **安全** | 开放的安全组(SSH/RDP → CRITICAL)、未加密的 S3/RDS/EBS、公开的数据库和存储桶、`*:*` IAM 策略、硬编码的密钥、IMDSv1、明文监听器 | | **成本** | 超大规格的 EC2/RDS(附带 $/月 估算)、gp2→gp3、孤立的卷和 Elastic IP、上一代实例类型、没有资源限制的 K8s 容器 | | **可靠性** | 没有备份或删除保护的数据库、没有版本控制的存储桶、未固定的镜像、缺少健康探针 | | **治理** | 未打标签的资源、通过策略强制要求的组织标签 | | **Kubernetes** | 特权容器、root 容器、hostPath 挂载——清单和 Terraform 一次性同时扫描 | | **Azure / GCP / vSphere** | 网络暴露、公开的存储/DB、加密、超大规格、标签——适用于云厂商无关和本地环境,可通过 `--provider` 过滤 | 包含基本原理和参考的完整目录:[docs/rules.md](docs/rules.md)。 运行 `cairn providers` 查看实时覆盖范围。任何 provider 的 Terraform 都会被解析;来自没有规则包的 provider 的资源将被报告为**未扫描**(绝不会产生误导性的“无风险”结果)。覆盖详情:[docs/coverage.md](docs/coverage.md)。 ## 退出代码 | 代码 | 含义 | |---|---| | 0 | 扫描完成;没有达到或高于失败阈值的发现 | | 1 | 存在达到或高于失败阈值的发现 | | 2 | Cairn 无法运行(路径错误、配置损坏、使用错误) | ## 项目状态与路线图 Cairn 目前处于 **v0.5** 阶段——支持 Terraform + Kubernetes 扫描、`propose`(Trust Ladder 的第 1 阶段)、策略包、合并前成本差异比对,以及代码中确立的开源核心边界。已发布和规划中的版本详见 [ROADMAP.md](ROADMAP.md)。架构和决策记录存放在 [docs/](docs/) 中。 ## 免费与付费 此代码库中的所有内容将永久免费——在代码中已明确列出并经过测试(`cairn license` 可打印出该列表)。团队/企业级功能(组织汇总、SSO、合规性证据)位于独立的商业包中,永远不会限制核心功能。 ## 许可证 [MIT](LICENSE)。核心功能永久免费——请参阅 [docs/architecture.md](docs/architecture.md) 中的项目原则。标签:AI风险缓解, DevSecOps, EC2, ECS, FinOps, IaC, Petitpotam, Terraform, 上游代理, 子域名突变, 对称加密, 自动化审计, 请求拦截, 逆向工具