Gaveta-cmd/secure-cicd-pipeline

GitHub: Gaveta-cmd/secure-cicd-pipeline

该项目提供了一个基于 GitHub Actions 的九阶段安全 CI/CD 流水线,通过密钥扫描、SAST、依赖审计、IaC 扫描、策略即代码和人工审批门禁,确保代码在通过全部安全控制后才能进入生产环境。

Stars: 0 | Forks: 0

# CloudSecOps 流水线 一个基于 GitHub Actions 构建的生产级安全 CI/CD pipeline,旨在在软件交付生命周期的每个阶段实施安全控制。该 pipeline 针对 Python/Flask API,但其架构是语言无关的,可适应任何技术栈。 ## 概述 大多数 CI pipeline 将安全视为事后补充——仅在最后附加一次扫描。本项目采取了相反的方法:安全是每个阶段的一等门禁,除非通过所有控制,否则代码无法到达生产环境。如果任何阶段失败,pipeline 将在下一个阶段开始前停止。 ``` push / PR │ ▼ [1] Secrets Scan (Gitleaks) │ ├──────────────────────────────┐ ▼ ▼ [2] SAST [3] Dependency Scan Semgrep + Bandit pip-audit (OSV) │ │ └──────────┬───────────────────┘ │ ▼ │ [5] IaC Scan │ Checkov + Trivy │ │ ▼ ▼ [4] Unit Tests (pytest ≥ 80% coverage) │ ▼ [6] Policy Gate (OPA / Conftest) │ ▼ [7] Security Summary (GitHub Step Summary) │ (main branch only) ▼ [8] Manual Approval Gate ← environment protection rule │ ▼ [9] Deploy to Production ``` ## 安全控制 | 阶段 | 工具 | 捕获内容 | |-------|------|-----------------| | 密钥扫描 | [Gitleaks](https://github.com/gitleaks/gitleaks) | 提交和历史记录中硬编码的 API 密钥、token、密码 | | SAST — 规则集 | [Semgrep](https://semgrep.dev) | OWASP Top 10、Flask 特定漏洞、不安全的模式 | | SAST — 深度 | [Bandit](https://bandit.readthedocs.io) | Python 安全反模式(subprocess 注入、弱加密等) | | 依赖审计 | [pip-audit](https://pypi.org/project/pip-audit/) | 通过 OSV 数据库发现的第三方包中的已知 CVE | | IaC 分析 | [Checkov](https://www.checkov.io) | 配置错误的 GitHub Actions workflow 和基础设施文件 | | 容器/文件系统 | [Trivy](https://trivy.dev) | 文件系统和 OS 包中的 CVE | | 策略即代码 | [OPA / Conftest](https://www.conftest.dev) | 自定义治理规则(版本锁定、覆盖率阈值、批准门禁) | | 批准门禁 | GitHub Environments | 任何生产部署前需要人工签字确认 | 来自 Bandit、Checkov 和 Trivy 的 SARIF 结果会上传到 GitHub Code Scanning,使发现的问题在仓库的 Security 标签页中可见。 ## 威胁模型(摘要) | 威胁 | 控制 | |--------|---------| | 源代码中泄露的凭证 | 每次推送时运行 Gitleaks,`.gitignore` 阻止 `.env` 文件 | | 易受攻击的第三方库 | 针对 OSV 运行 pip-audit,如果存在已修复版本则阻止 pipeline | | 不安全的代码模式(SQLi、路径遍历等) | Semgrep(OWASP Top 10 规则集)+ Bandit | | 权限过大的配置错误 workflow | Checkov IaC 扫描 + OPA 策略强制执行 `permissions: read` | | 未经授权的生产部署 | 手动批准环境门禁,`main` 上的分支保护 | | 掩盖 bug 的低测试覆盖率 | 如果 pytest 覆盖率 < 80%,OPA 策略将阻止部署 | ## 仓库结构 ``` . ├── .github/ │ └── workflows/ │ └── security-pipeline.yml # Full 9-stage pipeline definition ├── app/ │ ├── __init__.py # Flask application factory │ ├── config.py # Environment-based configuration │ ├── routes.py # API endpoints with input validation │ └── tests/ │ └── test_routes.py # pytest unit tests ├── policies/ │ ├── workflow_security.rego # OPA: workflow governance rules │ ├── dependency_policy.rego # OPA: dependency pinning + banned packages │ └── config_policy.rego # OPA: pytest coverage threshold enforcement ├── main.py # Application entry point ├── requirements.txt # Production dependencies (exact versions) ├── requirements-dev.txt # Dev/security tooling dependencies ├── pytest.ini # Test configuration with coverage gate ├── .env.example # Environment variable template (no real secrets) └── .gitignore # Blocks secrets, build artifacts, reports ``` ## 本地运行 **前置条件:** Python 3.12+,pip ``` # 克隆并设置环境 git clone https://github.com//secure-cicd-pipeline cd secure-cicd-pipeline python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install -r requirements-dev.txt # 复制 env 模板 cp .env.example .env # 使用本地值编辑 .env # 运行 API python main.py # → http://localhost:5000/health # 运行单元测试 pytest # 在本地运行 SAST bandit -r app/ --severity-level medium semgrep --config p/python --config p/flask app/ # 审计依赖项 pip-audit -r requirements.txt # 运行 OPA 策略(需要安装 conftest) conftest test .github/workflows/security-pipeline.yml --policy policies/ ``` ## Pipeline 设置 (GitHub) 1. **Fork / push** 此仓库到 GitHub。 2. 在 *Settings → Environments* 下**创建一个 `production` 环境**,并至少添加一个必需的审查者。 3. **添加仓库密钥**(可选,用于完整功能): - `SEMGREP_APP_TOKEN` — 用于 Semgrep Cloud 仪表板结果 - `GITLEAKS_LICENSE` — 用于 Gitleaks Pro(免费层级无需此项) 4. Push 到 `main` 或发起 pull request — pipeline 将自动触发。 ## API 端点 | 方法 | 路径 | 描述 | |--------|------|-------------| | `GET` | `/health` | 存活检查 | | `GET` | `/items` | 列出所有项目 | | `GET` | `/items/` | 按 ID 获取项目 | | `POST` | `/items` | 创建新项目 | ``` curl http://localhost:5000/health # {"status": "ok", "version": "1.0.0"} curl -X POST http://localhost:5000/items \ -H "Content-Type: application/json" \ -d '{"name": "Widget C", "category": "hardware"}' # {"id": "3", "name": "Widget C", "category": "hardware"} ``` ## 关键设计决策 **在安全的前提下并行执行。** 密钥扫描首先运行并作为所有内容的门禁。SAST、依赖扫描和 IaC 扫描在其之后并行运行,从而减少了总 pipeline 时间。单元测试和策略门禁在所有安全扫描完成后运行。 **全面使用 SARIF。** Bandit、Checkov 和 Trivy 均输出 SARIF,GitHub 会将其原生摄取到 Code Scanning 中。无需离开 GitHub 即可在 pull request 中审查发现的问题。 **策略即代码优于临时脚本。** OPA/Rego 策略是版本控制的、可测试且可审计的。添加新的治理规则是一次伴随代码审查的代码更改,而不是一次性的 shell 脚本。 **pipeline 中不包含密钥。** `GITHUB_TOKEN` 是 pipeline 默认使用的唯一 token,其权限范围限定为 `contents: read`。可选集成(如 Semgrep Cloud)使用存储在 GitHub 中的密钥,绝不会出现在代码中。
标签:DevSecOps, GitHub Actions, IaC扫描, Python Flask, SAST, 上游代理, 依赖审计, 安全规则引擎, 盲注攻击, 自动笔记, 逆向工具