SecurityWithAdarsh/DevSecOps-CI-CD-Pipeline
GitHub: SecurityWithAdarsh/DevSecOps-CI-CD-Pipeline
一套基于 GitHub Actions 的企业级 DevSecOps 流水线模板,将 SAST、SCA、密钥扫描、容器扫描与 DAST 等安全检测环节全链路编排到 CI/CD 流程中。
Stars: 0 | Forks: 0
# 🛡️ DevSecOps CI/CD Pipeline
[](https://github.com/SecurityWithAdarsh/DevSecOps-CI-CD-Pipeline/actions/workflows/devsecops-pipeline.yml)
[](https://github.com/SecurityWithAdarsh/DevSecOps-CI-CD-Pipeline/actions/workflows/codeql.yml)
[](https://github.com/SecurityWithAdarsh/DevSecOps-CI-CD-Pipeline/actions/workflows/secret-scan.yml)
[](LICENSE)
**作者:** [Adarsh Singh](https://linkedin.com/in/adarsh-singh-cybersecurity) | DevSecOps Engineer | [SecurityWithAdarsh](https://github.com/SecurityWithAdarsh)
## 📋 目录
- [架构](#-architecture)
- [Pipeline 阶段](#-pipeline-stages)
- [安全工具](#-security-tools)
- [项目结构](#-project-structure)
- [快速开始](#-quick-start)
- [Kubernetes 部署](#-kubernetes-deployment)
- [安全报告](#-security-reports)
- [Secrets 管理](#-secrets-management)
- [贡献](#-contributing)
## 🏗️ 架构
```
┌─────────────────────────────────────────────────────────────────────────┐
│ GitHub Repository │
│ │
│ Code Push / PR │
│ │ │
│ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌────────────────────┐ │
│ │ SAST │ │ SCA │ │ Secret │ │ Unit Tests │ │
│ │ Bandit │──▶│ Safety │──▶│ Scan │──▶│ + Code Coverage │ │
│ │ Semgrep │ │ pip-audit│ │ Gitleaks │ │ (pytest) │ │
│ │ CodeQL │ └──────────┘ └──────────┘ └────────┬───────────┘ │
│ └──────────┘ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Docker Build │ │
│ │ + Push GHCR │ │
│ └──────┬───────┘ │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Container │ │
│ │ Scan (Trivy)│ │
│ └──────┬───────┘ │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ DAST (OWASP │ │
│ │ ZAP) │ │
│ └──────┬───────┘ │
│ │ │
│ ┌────────────────────────────┤ │
│ │ │ │
│ ▼ ▼ │
│ ┌────────────┐ ┌────────────┐ │
│ │ Staging │ │ Production │ │
│ │ (develop) │ │ (main) │ │
│ └────────────┘ └────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
```
## 🔄 Pipeline 阶段
| # | 阶段 | 工具 | 触发条件 | 准入条件 |
|---|-------|-------|---------|------|
| 1 | **SAST** | Bandit, Semgrep, CodeQL | Push/PR | ❌ Medium+ 级别漏洞时失败 |
| 2 | **SCA** | Safety, pip-audit | Push/PR | ❌ 存在已知 CVEs 时失败 |
| 3 | **Secret Scan** | Gitleaks | Push/PR | ❌ 发现任何 secret 时失败 |
| 4 | **Unit Tests** | pytest + coverage | 在 SAST/SCA 之后 | ❌ 测试失败时失败 |
| 5 | **Build** | Docker Buildx, GHCR | 测试通过后 | 推送已签名的镜像 |
| 6 | **Container Scan** | Trivy | 构建完成后 | ❌ 发现 CRITICAL/HIGH 漏洞时失败 |
| 7 | **DAST** | OWASP ZAP | 容器扫描之后 | ⚠️ 发现问题时发出警告 |
| 8 | **Deploy Staging** | kubectl + Kustomize | `develop` 分支 | 人工审批 |
| 9 | **Deploy Prod** | kubectl + Kustomize | `main` 分支 | 人工审批 |
## 🛠️ 安全工具
### 静态应用安全测试 (SAST)
| 工具 | 检查内容 | 配置 |
|------|---------------|--------|
| [Bandit](https://bandit.readthedocs.io/) | Python 安全反模式、硬编码密码、SQL injection | `security/bandit/bandit.ini` |
| [Semgrep](https://semgrep.dev/) | 自定义规则 + 社区规则集 (flask, secrets, python) | `security/semgrep/semgrep-rules.yml` |
| [CodeQL](https://codeql.github.com/) | 由 GitHub 提供的深度语义代码分析 | `.github/workflows/codeql.yml` |
### 软件组成分析 (SCA)
| 工具 | 检查内容 |
|------|---------------|
| [Safety](https://pyup.io/safety/) | Python 依赖项中的已知 CVEs (PyPI advisory DB) |
| [pip-audit](https://github.com/pypa/pip-audit) | 通过 OSV 和 PyPI 顾问进行漏洞审计 |
| [GitHub Dependency Review](https://docs.github.com/en/code-security/supply-chain-security) | PR 级别的依赖差异比对及漏洞标记 |
### Secret 检测
| 工具 | 检查内容 |
|------|---------------|
| [Gitleaks](https://github.com/gitleaks/gitleaks) | 150+ 种 secret 模式:AWS keys、GCP tokens、JWT、SSH keys、API tokens |
### 容器安全
| 工具 | 检查内容 | 配置 |
|------|---------------|--------|
| [Trivy](https://aquasecurity.github.io/trivy/) | OS 包、语言依赖、配置错误、嵌入的 secrets | `security/trivy/trivy.yaml` |
### 动态应用安全测试 (DAST)
| 工具 | 测试内容 |
|------|--------------|
| [OWASP ZAP](https://www.zaproxy.org/) | XSS、SQLi、CSRF、安全标头、主动爬取 |
## 📁 项目结构
```
DevSecOps-CI-CD-Pipeline/
│
├── .github/
│ └── workflows/
│ ├── devsecops-pipeline.yml # Main pipeline (SAST→SCA→Build→Scan→DAST→Deploy)
│ ├── codeql.yml # GitHub CodeQL analysis
│ ├── dependency-review.yml # PR dependency gate
│ └── secret-scan.yml # Gitleaks secret scanning
│
├── app/
│ ├── __init__.py # Flask app factory
│ ├── routes.py # API routes (/health, /api/info)
│ └── templates/
│ └── index.html # Pipeline visualization UI
│
├── tests/
│ └── test_app.py # Pytest unit tests
│
├── security/
│ ├── bandit/bandit.ini # Bandit SAST config
│ ├── semgrep/semgrep-rules.yml # Custom Semgrep rules
│ └── trivy/trivy.yaml # Trivy scanner config
│
├── k8s/
│ ├── base/ # Kustomize base manifests
│ │ ├── deployment.yaml # Hardened K8s deployment
│ │ ├── service.yaml
│ │ ├── network-policy.yaml # Zero-trust NetworkPolicy
│ │ └── kustomization.yaml
│ └── overlays/
│ ├── dev/ # Staging (1 replica)
│ └── prod/ # Production (3 replicas)
│
├── scripts/
│ ├── run-security-scan.sh # Local security scan runner
│ └── zap-scan.sh # Local OWASP ZAP runner
│
├── reports/ # Generated security reports (gitignored)
├── .zap/rules.tsv # ZAP rule overrides
├── .gitleaks.toml # Gitleaks config
├── Dockerfile # Multi-stage, hardened
├── docker-compose.yml # Local dev + DAST profile
├── main.py # App entrypoint
├── requirements.txt # Production dependencies
├── requirements-dev.txt # Dev/security tool dependencies
├── pytest.ini # Test config
├── SECURITY.md # Vulnerability disclosure policy
└── CONTRIBUTING.md
```
## ⚡ 快速开始
### 前置条件
- Python 3.12+
- Docker
- (可选)kubectl + Kubernetes 集群
### 本地运行
```
# Clone 仓库
git clone https://github.com/SecurityWithAdarsh/DevSecOps-CI-CD-Pipeline.git
cd DevSecOps-CI-CD-Pipeline
# 创建虚拟环境
python -m venv venv && source venv/bin/activate
# 安装依赖
pip install -r requirements.txt -r requirements-dev.txt
# 运行应用
python main.py
# → 访问 http://localhost:5000
```
### 使用 Docker 运行
```
# 构建并启动
docker compose up --build
# 对运行中的应用运行 DAST
docker compose --profile dast up zap
```
### 在本地运行安全扫描
```
# 运行所有安全工具 (Bandit + Safety + Semgrep + Trivy)
bash scripts/run-security-scan.sh
# 运行单元测试并生成覆盖率
pytest
```
## ☸️ Kubernetes 部署
```
# 部署到 staging
kubectl apply -k k8s/overlays/dev/
# 部署到 production
kubectl apply -k k8s/overlays/prod/
# Watch rollout
kubectl rollout status deployment/devsecops-app -n production
```
### K8s 清单中的安全加固
- `runAsNonRoot: true` — 禁止使用 root 容器
- `readOnlyRootFilesystem: true` — 不可变的容器文件系统
- `allowPrivilegeEscalation: false`
- `capabilities: drop: [ALL]`
- `seccompProfile: RuntimeDefault`
- `NetworkPolicy` — 限制 ingress/egress 流量
## 📊 安全报告
所有报告均作为 GitHub Actions artifacts 生成(保留 30 天):
| 报告 | 工具 | 格式 |
|--------|------|--------|
| `bandit-report.json` | Bandit | JSON |
| `semgrep-report.json` | Semgrep | JSON |
| `safety-report.json` | Safety | JSON |
| `pip-audit-report.json` | pip-audit | JSON |
| `trivy-report.json` | Trivy | JSON |
| `trivy-results.sarif` | Trivy | SARIF → GitHub Security 标签页 |
| `zap-report.json` | OWASP ZAP | JSON |
| `coverage.xml` | pytest-cov | XML |
## 🔐 Secrets 管理
**切勿提交 secrets。** 此 Pipeline 使用:
- **GitHub Secrets** 存储 `GITHUB_TOKEN`(自动提供)、kubeconfig、registry 凭证
- **Gitleaks** 在 PR 准入阶段拦截包含 secret 的提交
- **Trivy** 检测嵌入到 Docker 镜像中的 secrets
完整 Pipeline 运行所需的 secrets:
| Secret | 使用方 |
|--------|---------|
| `GITHUB_TOKEN` | 自动提供 — 用于 GHCR 推送、release 创建 |
| `KUBECONFIG` | kubectl 部署步骤(需手动添加) |
## 📄 许可证
MIT License — 详见 [LICENSE](LICENSE)
## 🤝 联系方式
**Adarsh Singh** — DevSecOps Engineer | Application Security | OT/ICS Security
- 🔗 [LinkedIn](https://linkedin.com/in/adarsh-singh-cybersecurity)
- 🐙 [GitHub](https://github.com/SecurityWithAdarsh)
- 🎯 TryHackMe — 全球前 5%
标签:CI/CD流水线, DevSecOps, Docker, GitHub Actions, 上游代理, 子域名突变, 安全测试, 安全防御评估, 攻击性安全, 自动笔记, 请求拦截, 逆向工具