chatrapathyee/cicd-security-pipeline
GitHub: chatrapathyee/cicd-security-pipeline
基于 GitHub Actions 的 DevSecOps 安全流水线项目,在 CI/CD 流程中自动化集成 SAST、依赖扫描、IaC 安全、容器扫描和密钥检测等安全控制。
Stars: 0 | Forks: 0
## # 安全的 DevSecOps CI/CD Pipeline
## 项目概述
本项目演示了如何构建一个**安全的 DevSecOps CI/CD pipeline**,在部署之前自动验证应用程序代码、基础设施、依赖项、secrets 和容器镜像。
安全控制直接集成到 CI/CD pipeline 中,以在软件开发生命周期 (SDLC) 的早期检测漏洞,而不是将安全视为最后的部署步骤。
该项目将基础设施即代码 (Terraform)、容器安全、静态应用程序安全测试 (SAST)、依赖项漏洞扫描、secret 检测和自动化策略执行结合到一个集中的 GitHub Actions 工作流中。
该实现遵循企业云环境中常用的现代 DevSecOps 实践。
# 核心功能
- ✅ 集中的 GitHub Actions DevSecOps pipeline
- ✅ 使用 Pytest 进行自动化 Python 单元测试
- ✅ 使用 Gitleaks 进行 secret 扫描
- ✅ 使用 Semgrep 进行静态应用程序安全测试 (SAST)
- ✅ 使用 pip-audit 进行 Python 依赖项漏洞扫描
- ✅ Terraform 验证
- ✅ 使用 Checkov 进行基础设施即代码安全扫描
- ✅ 使用 Trivy 进行 Docker 容器漏洞扫描
- ✅ 阻止不安全构建的安全门
- ✅ 每次推送时自动进行构建验证
#架构
CI/CD pipeline 遵循安全的左移安全模型。
开发者的提交会通过 GitHub Actions 自动进行验证。
每次提交都会触发:
1. 单元测试
2. Secret 检测
3. 静态代码分析
4. 依赖项漏洞扫描
5. Terraform 验证
6. 基础设施安全扫描
7. Docker 镜像构建
8. 容器漏洞扫描
9. 安全门
只有成功通过每个安全阶段的构建才被视为可部署的。
#DevSecOps Pipeline
```
Developer
│
▼
GitHub Repository
│
▼
GitHub Actions
│
├──────────────► Unit Tests (Pytest)
│
├──────────────► Secret Detection (Gitleaks)
│
├──────────────► SAST (Semgrep)
│
├──────────────► Dependency Scan (pip-audit)
│
├──────────────► Terraform Validate
│
├──────────────► Checkov IaC Scan
│
├──────────────► Docker Build
│
├──────────────► Trivy Container Scan
│
▼
Security Gate
│
▼
Build Approved
---
#Technology Stack
| Category | Technologies |
|-----------|--------------|
| Programming | Python 3.12 |
| Cloud | AWS |
| CI/CD | GitHub Actions |
| IaC | Terraform |
| Containers | Docker |
| Testing | Pytest |
| SAST | Semgrep |
| Secrets Detection | Gitleaks |
| Dependency Scanning | pip-audit |
| IaC Security | Checkov |
| Container Security | Trivy |
---
#Repository Structure
```text
cicd-security-pipeline/
│
├── .github/
│ └── workflows/
│ ├── devsecops-pipeline.yml # Centralized GitHub Actions pipeline
│ ├── security-scan.yml.disabled
│ ├── terraform-check.yml.disabled
│ └── container-scan.yml.disabled
│
├── app/
│ ├── __init__.py
│ ├── app.py # Sample Flask application
│ └── test_app.py # Unit tests
│
├── terraform/
│ ├── main.tf # Secure AWS infrastructure
│ ├── variables.tf
│ ├── versions.tf
│ └── terraform.tfvars.example
│
├── policies/
│ ├── checkov-config/
│ ├── semgrep-rules/
│ └── exceptions/
│
├── scripts/
│ ├── pre-commit-hook.sh
│ └── scan-results-parser.sh
│
├── docs/
│ ├── adding-exceptions.md
│ ├── tool-configuration.md
│ └── escalation-process.md
│
├── Dockerfile
├── requirements.txt
├── README.md
└── .gitignore
```
# 安全控制
该 pipeline 集成了多种安全控制,以在整个 CI/CD 生命周期内执行安全的软件开发实践。
| 安全阶段 | 工具 | 目的 |
|----------------|------|---------|
| 单元测试 | Pytest | 在安全检查之前验证应用程序功能 |
| Secret 检测 | Gitleaks | 检测硬编码的密码、API 密钥和凭证 |
| 静态应用程序安全测试 | Semgrep | 识别不安全的编码模式和常见漏洞 |
| 依赖项扫描 | pip-audit | 使用 Python Advisory Database 检测存在漏洞的 Python 包 |
| 基础设施验证 | Terraform | 验证基础设施即代码的语法和配置 |
| 基础设施安全 | Checkov | 根据安全最佳实践检测不安全的 Terraform 配置 |
| 容器安全 | Trivy | 扫描 Docker 镜像中的操作系统和应用程序漏洞 |
| 安全门 | GitHub Actions | 防止不安全的代码在 pipeline 中推进 |
# CI/CD 工作流
DevSecOps pipeline 遵循左移安全模型,每当代码推送到仓库时都会自动进行安全检查。
```
Developer Push
│
▼
GitHub Repository
│
▼
GitHub Actions
│
├────────► Unit Tests
│
├────────► Secret Detection
│
├────────► Static Code Analysis
│
├────────► Dependency Vulnerability Scan
│
├────────► Terraform Validation
│
├────────► Checkov IaC Scan
│
├────────► Docker Build
│
├────────► Trivy Container Scan
│
▼
Security Gate
│
▼
Pipeline Pass
```
# 使用的安全工具
## GitHub Actions
充当 CI/CD 编排平台,负责自动执行每个安全阶段。
## Gitleaks
扫描仓库中意外提交的 secrets,例如:
- AWS Access Keys
- GitHub Tokens
- API Keys
- 数据库密码
- 私钥
## Semgrep
执行静态应用程序安全测试 (SAST) 以识别:
- 命令注入
- SQL 注入
- 不安全的 Flask 配置
- 危险的函数调用
- 硬编码的凭证
## pip-audit
根据已知的 CVE 检查 Python 依赖项,并推荐安全的包版本。
## Terraform
在部署前验证基础设施语法。
## Checkov
对 Terraform 资源执行策略即代码验证。
示例检查包括:
- 启用加密
- 启用版本控制
- 阻止公共访问
- 配置日志记录
- 最小权限 IAM
## Trivy
扫描 Docker 镜像中的:
- 操作系统漏洞
- Python 包漏洞
- 配置错误
- Secrets
- 许可证问题
# 开始使用
## 前置条件
在运行项目之前,请安装以下软件。
| 工具 | 版本 |
|------|---------|
| Git | 最新版 |
| Python | 3.12+ |
| Docker Desktop | 最新版 |
| Terraform | >= 1.6 |
| AWS CLI | 最新版 |
| GitHub 账户 | 必需 |
## 克隆仓库
```
git clone https://github.com/chatrapathyee/cicd-security-pipeline.git
cd cicd-security-pipeline
```
# 运行 Flask 应用程序
## 安装依赖项
```
pip install -r requirements.txt
```
## 运行应用程序
```
python app/app.py
```
应用程序启动于:
```
http://localhost:5000
```
## 验证健康检查 Endpoint
```
curl http://localhost:5000/
```
预期响应
```
{
"service": "secure-cicd-demo",
"status": "healthy"
}
```
# 运行单元测试
在本地执行所有测试。
```
pytest app/test_app.py -v
```
预期输出
```
1 passed
```
# 构建 Docker 镜像
构建应用程序容器。
```
docker build -t secure-cicd-demo:latest .
```
验证镜像是否存在。
```
docker images
```
运行容器。
```
docker run -p 5000:5000 secure-cicd-demo:latest
```
# 部署 Terraform 基础设施
进入 Terraform 目录。
```
cd terraform
```
初始化 Terraform。
```
terraform init
```
格式化配置。
```
terraform fmt
```
验证配置。
```
terraform validate
```
生成执行计划。
```
terraform plan
```
部署基础设施。
```
terraform apply
```
测试后销毁资源。
```
terraform destroy
```
# 在本地运行安全扫描
## Secret 检测
```
gitleaks detect
```
## 静态应用程序安全测试
```
semgrep scan
```
## 依赖项漏洞扫描
```
pip-audit -r requirements.txt
```
## Terraform 安全扫描
```
checkov -d terraform
```
## 容器漏洞扫描
构建 Docker 镜像。
```
docker build -t secure-cicd-demo:latest .
```
运行 Trivy。
```
trivy image secure-cicd-demo:latest
```
# GitHub Actions Pipeline
每次向仓库推送都会自动触发集中的 DevSecOps pipeline。
该 pipeline 执行:
- 单元测试
- Secret 检测
- 静态应用程序安全测试
- 依赖项漏洞扫描
- Terraform 验证
- Checkov 基础设施安全扫描
- Docker 镜像构建
- Trivy 容器漏洞扫描
- 安全门验证
# 预期的 Pipeline 结果
成功执行会显示所有安全阶段均已通过。
```
✔ Unit Tests
✔ Secret Detection
✔ Static Application Security Testing
✔ Dependency Vulnerability Scan
✔ Terraform Validation
✔ IaC Security Scan
✔ Container Vulnerability Scan
✔ Security Gate
```
安全门确保仅当所有必需的安全检查成功完成时,代码才能继续推进。
# 项目成果
本项目演示了如何使用行业标准的 DevSecOps 工具实际实施安全的 CI/CD pipeline。
主要成就包括:
- 自动化安全测试
- 左移安全集成
- 基础设施即代码验证
- 容器漏洞评估
- Secret 检测
- 依赖项风险管理
- 策略即代码执行
- 安全门实施
- 持续安全自动化
标签:DevSecOps, GitHub Actions, IaC安全, SAST, 上游代理, 依赖扫描, 容器安全扫描, 机密检测, 模型提供商, 漏洞利用检测, 盲注攻击, 自动笔记, 请求拦截, 逆向工具