9t29zhmwdh-coder/azure-policy-drift-detector

GitHub: 9t29zhmwdh-coder/azure-policy-drift-detector

一款用 Rust 编写的只读 CLI 工具,用于跨订阅检测 Azure Policy 配置漂移并生成分级修复报告。

Stars: 0 | Forks: 0

RayStudio Logo

Azure Policy Drift Detector

**一个只读的 Rust CLI 工具,用于检测跨订阅的 Azure Policy 漂移,优先处理不合规的资源并生成修复报告。** Azure Policy Drift Detector 使用应用程序凭据连接到 Azure Resource Graph 和 Policy Insights,并将资源配置与活动的策略分配进行比较。完全只读,没有任何数据会离开您的本地计算机。 符合 [Microsoft 云安全基准 (MCSB)](https://learn.microsoft.com/en-us/security/benchmark/azure/overview),专为 Azure 治理与合规团队设计。 ![Rust](https://img.shields.io/badge/Rust-1.78+-orange?logo=rust) ![Azure Policy](https://img.shields.io/badge/Azure%20Policy-blue?logo=microsoftazure) ![平台](https://img.shields.io/badge/Platform-Windows%20%7C%20Linux-lightgrey?logo=windows) ![许可证](https://img.shields.io/badge/License-MIT-green) [![Azure Ready](https://img.shields.io/badge/Azure-Resource%20Graph%20%7C%20Policy%20Insights-blue?logo=microsoftazure)](docs/azure_setup.md) [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/11029494bc003911.svg)](https://github.com/9t29zhmwdh-coder/azure-policy-drift-detector/actions/workflows/ci.yml) ## 功能 | 功能 | 描述 | |---|---| | 资源发现 | 通过 Azure Resource Graph (KQL) 查询订阅中的所有资源 | | 策略状态检索 | 从 Azure Policy Insights 获取最新的合规状态 | | 漂移检测 | 识别不合规的配置、标签不匹配和策略豁免 | | 风险优先级划分 | 根据策略类别(安全、合规、运营)按严重程度对发现的问题进行分类 | | JSON 导出 | 用于与工单系统集成、机器可读的合规报告 | | Markdown 导出 | 包含发现的问题、描述和修复步骤的人类可读报告 | | SARIF stub | 为集成 GitHub Advanced Security (v0.2) 做好准备 | | GitHub Action 模板 | 可直接用于计划内合规检查的工作流模板 | ## 所需的 Azure RBAC 角色 在 Entra ID 中注册应用程序,并在订阅范围内分配以下角色: | 角色 | 用途 | |---|---| | `Reader` | Azure Resource Graph 查询 | | `Policy Insights Data Reader` | 读取策略合规状态 | 这两个角色均为只读。不需要也不会使用任何写入权限。 ## 应用注册设置 1. 打开 [Azure 门户](https://portal.azure.com) 并导航至 **Entra ID > App registrations > New registration** 2. 为应用程序命名(例如 `apdd-scanner`)并进行注册 3. 导航至您的 **Subscription > Access control (IAM) > Add role assignment** 4. 将 `Reader` 和 `Policy Insights Data Reader` 分配给该应用程序 5. 转到 **Entra ID > App registrations > your app > Certificates and secrets > New client secret** 6. 立即复制密钥值。它将不再显示。 7. 记下您的 **Tenant ID**、**Client ID**、**Client Secret** 和 **Subscription ID** ## 快速开始 ``` git clone https://github.com/9t29zhmwdh-coder/azure-policy-drift-detector cd azure-policy-drift-detector cp .env.example .env # 填写你的 credentials cargo build --release # 扫描 drift findings ./target/release/apdd scan # 仅显示 critical 和 high 严重级别的 findings ./target/release/apdd scan --min-severity high # 导出为 Markdown 报告 ./target/release/apdd export --format md --output report.md # 导出为 JSON ./target/release/apdd export --format json --output report.json ``` ## 配置 ``` AZURE_TENANT_ID=your-tenant-id AZURE_CLIENT_ID=your-client-id AZURE_CLIENT_SECRET=your-client-secret AZURE_SUBSCRIPTION_ID=your-subscription-id ``` `.env` 文件已列在 `.gitignore` 中。凭据永远不会被提交。 ## 漂移严重程度 | 级别 | 触发条件 | 示例 | |---|---|---| | 严重 | 与安全相关的策略违规 | 网络安全、加密、身份 | | 高 | 合规框架违规或不合规的配置 | ISO、NIST、CIS benchmark 策略 | | 中 | 标签不匹配或缺少必需的标签 | 成本中心标签、环境标签 | | 低 | 轻微的配置偏差 | 命名约定 | | 信息 | 策略豁免处于活动状态 | 具有有效豁免的资源 | ## GitHub Action 集成 将 `.github/workflows/policy-check-template.yml` 复制到您的存储库,并配置所需的密钥(`AZURE_TENANT_ID`、`AZURE_CLIENT_ID`、`AZURE_CLIENT_SECRET`、`AZURE_SUBSCRIPTION_ID`)。该工作流每周运行一次,并将漂移报告作为构建产物上传。 ## 示例输出 ``` === Azure Policy Drift Detector === Resources scanned: 214 Non-compliant: 8 Exempt: 2 Drift findings: 10 +----------+-------------------------------+-------------------+------------------------------------------+ | Severity | Type | Resource | Policy | +----------+-------------------------------+-------------------+------------------------------------------+ | CRITICAL | Non-Compliant Configuration | vm-prod-001 | Enable network security group on subnet | | HIGH | Non-Compliant Configuration | storage-backup | Require encryption in transit | | MEDIUM | Tag Mismatch | aks-cluster-eu | Require cost center tag | | INFO | Policy Exempt | vm-legacy-002 | Audit unmanaged disks | +----------+-------------------------------+-------------------+------------------------------------------+ Summary: 1 Critical, 3 High, 4 Medium, 0 Low ``` ## 要求 - Rust 1.78+ - 带有应用程序注册的 Azure 订阅 - 可访问 `login.microsoftonline.com` 和 `management.azure.com` 的网络 **作者:** [Rafael Yilmaz](https://github.com/9t29zhmwdh-coder) · **状态:** 早期发布 · **最后更新:** 2026 年 6 月
标签:Azure, Rust, 云资源配置, 可视化界面, 命令行工具(CLI), 策略漂移检测, 网络流量审计, 通知系统