toriyama237/terraform-devsecops-pipeline

GitHub: toriyama237/terraform-devsecops-pipeline

该项目为 Terraform 提供了一套四步 GitHub Actions 流水线,通过自动化 lint、安全扫描、计划预览和人工审批门禁,在基础设施变更中落地 Shift-Left 安全实践。

Stars: 0 | Forks: 0

# Pipeline CI/CD DevSecOps — Terraform 自动化 [![Terraform CI (DevSecOps)](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/03de7bb87b222056.svg)](https://github.com/toriyama237/terraform-devsecops-pipeline/actions/workflows/terraform-ci.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) [![Terraform](https://img.shields.io/badge/Terraform-%3E%3D1.6-7B42BC?logo=terraform&logoColor=white)](https://www.terraform.io/) [![Security: Trivy](https://img.shields.io/badge/Security-Trivy-1904DA?logo=aquasecurity&logoColor=white)](https://trivy.dev/) ## 4 步 Pipeline ``` flowchart LR A[Pull Request] --> B["Étape 1 · Linting
terraform fmt + tflint"] B --> C["Étape 2 · Sécurité
Trivy (analyse statique IaC)"] C -->| faille HIGH/CRITICAL| X[ Pipeline bloqué] C -->| clean| D["Étape 3 · Plan
terraform plan → commentaire PR"] D --> E[ Merge sur main] E --> F["Étape 4 · Validation humaine
GitHub Environment (Required reviewer)"] F -->|approuvé| G["terraform apply"] ``` | # | 步骤 | 工具 | 角色 | 文件 | | - | ----- | ----- | ---- | ------- | | 1 | **Linting** | `terraform fmt`, `tflint` | 确保代码格式规范并符合最佳实践 | [`terraform-ci.yml`](.github/workflows/terraform-ci.yml) | | 2 | **安全** | `Trivy` | 静态分析:如果检测到漏洞(开放端口、公开的 bucket、缺少加密等),则**阻断**部署 | [`terraform-ci.yml`](.github/workflows/terraform-ci.yml) | | 3 | **规划** | `terraform plan` | 生成计划并**自动发布到 Pull Request 中** | [`terraform-ci.yml`](.github/workflows/terraform-ci.yml) | | 4 | **人工验证** | GitHub Environments | 在执行 `terraform apply` 之前,等待**管理员批准** | [`terraform-apply.yml`](.github/workflows/terraform-apply.yml) | ## 仓库结构 ``` . ├── .github/workflows/ │ ├── terraform-ci.yml # PR : lint → sécurité → plan → commentaire │ └── terraform-apply.yml # main : apply gated par approbation humaine ├── infra/ # Module racine (VPC sécurisée + bucket S3 durci) │ ├── versions.tf │ ├── providers.tf │ ├── variables.tf │ ├── main.tf │ ├── outputs.tf │ └── terraform.tfvars.example ├── modules/secure-network/ # Module réseau réutilisable et durci │ ├── main.tf # VPC + Flow Logs (KMS) + Security Group restreint │ ├── variables.tf │ ├── outputs.tf │ └── versions.tf ├── .tflint.hcl # Règles de linting ├── .trivyignore # Exceptions de sécurité documentées et justifiées ├── Makefile # Rejoue le pipeline en local └── README.md ``` ## 内置安全 (Shift-Left) 示例基础设施**默认经过安全加固**: - **VPC** 带有加密的 *Flow Logs*(使用 KMS,并启用轮换),用于网络审计。 - **Security Group**:将 SSH **限制**在受信任的 IP 范围内——绝不使用 `0.0.0.0/0`。 Terraform 的 `validation` 甚至明确禁止将其对全网开放。 - **默认 Security Group 被锁定**(不允许任何流量)。 - **S3 bucket**:SSE-KMS 加密,启用 *versioning*,阻止公开访问, 配置生命周期,并且**策略拒绝所有非 HTTPS 流量**(`aws:SecureTransport`)。 **Trivy** 扫描会在每次 PR 时运行。**任何 HIGH/CRITICAL 级别的漏洞都会阻断 pipeline。** 极少数的例外情况被集中管理,并在 [`.trivyignore`](.trivyignore) 中**说明理由**。 ## 本地使用 前置条件:[Terraform](https://developer.hashicorp.com/terraform/install) ≥ 1.6, [TFLint](https://github.com/terraform-linters/tflint),[Trivy](https://trivy.dev/)。 ``` make help # liste les commandes disponibles make ci # rejoue tout le pipeline : lint + sécurité + validate + plan make security # uniquement le scan de sécurité Trivy ``` 本地 `plan` 使用了 `ci_mode=true`,这使得生成计划时**不需要真实的 AWS 凭据**(使用虚拟凭据 + `init -backend=false`)。 ## 实际生产部署 要启用实际部署(`terraform apply`): 1. **远程 Backend**:取消注释 [`infra/versions.tf`](infra/versions.tf) 中的 `backend "s3"` 代码块(S3 + DynamoDB 锁定)。 2. **OIDC 身份验证**:为 GitHub Actions 创建一个受信任的 IAM role,并取消注释 [`terraform-apply.yml`](.github/workflows/terraform-apply.yml) 中的 `configure-aws-credentials` 步骤——代码仓库中**不存储任何长期有效的 secret**。 3. **Secret / 变量**:填写 `AWS_ACCOUNT_ID`(以及 `AWS_ROLE_ARN`)。 4. **审批门禁**:在 `Settings → Environments → production` 中,添加一个 **Required reviewer**。在批准之前,`apply` job 将一直处于等待状态。 ## 展示技能 - 使用 GitHub Actions 进行自动化和持续集成 (**CI/CD**)。 - **Shift-Left Security**:IaC 静态分析,可阻断高风险部署。 - 模块化、格式化并经过 linting 的基础设施即代码。 - 基础设施变更的**人工验证**(权限分离)。 ## 许可证 根据 [MIT](LICENSE) 许可证分发。
标签:DevSecOps, ECS, GitHub Actions, Terraform, 上游代理, 漏洞利用检测, 自动化运维, 自动笔记, 错误基检测, 静态代码分析