hazzikri/trivy-container-security

GitHub: hazzikri/trivy-container-security

将 Trivy 容器漏洞扫描与 IaC 配置检测集成到 GitHub Actions CI/CD 流水线中,实现安全左移的自动化漏洞拦截与审计追踪。

Stars: 0 | Forks: 0

# 使用 Trivy 实现容器安全左移 [![Trivy 镜像扫描](https://img.shields.io/badge/Security-Trivy%20Integrated-46a2f1?style=for-the-badge&logo=aquasecurity)](https://github.com/hazzikri/trivy-container-security/actions) [![GitHub Actions](https://img.shields.io/badge/CI%2FCD-GitHub%20Actions-2088FF?style=for-the-badge&logo=github-actions)](https://github.com/hazzikri/trivy-container-security/actions) [![Docker](https://img.shields.io/badge/Target-Docker%20Image-2496ED?style=for-the-badge&logo=docker)](https://hub.docker.com/) [![SARIF 上传](https://img.shields.io/badge/Reports-GitHub%20Security%20Tab-darkgreen?style=for-the-badge&logo=github)](https://docs.github.com/en/code-security) 这是一个**安全左移的 DevSecOps** 实现,它将 **Trivy**(由 Aqua Security 提供)直接集成到 CI/CD 流水线中,以便在代码进入生产环境**之前**自动检测容器镜像漏洞和基础设施配置错误。 ## 🔐 本项目展示的内容 这是在 **PT Link Net Tbk** 生产环境中应用的容器安全方法的实际实现。在这些环境中,Trivy 被嵌入到 Azure DevOps 流水线中,并被配置为在遇到 CRITICAL(严重)漏洞时自动中断构建。 ``` ┌──────────────────────────────────────────────────────────────┐ │ GitHub Actions Pipeline │ │ │ │ ┌────────────┐ ┌─────────────────┐ ┌────────────────┐ │ │ │ Code Push │──▶│ Docker Build │──▶│ Trivy Image │ │ │ │ to main │ │ (Dockerfile) │ │ Scan │ │ │ └────────────┘ └─────────────────┘ └───────┬────────┘ │ │ │ │ │ ┌──────────────────────────────┘ │ │ ▼ │ │ ┌─────────────────────┐ ┌─────────────────────┐ │ │ │ CRITICAL/HIGH CVE? │ │ GitHub Security Tab │ │ │ │ ────────────── │──▶│ SARIF Report │ │ │ │ YES → FAIL BUILD │ │ (all severities) │ │ │ │ NO → PASS │ └─────────────────────┘ │ │ └─────────────────────┘ │ └──────────────────────────────────────────────────────────────┘ ``` ## ⚙️ 流水线任务 ### 任务 1:`trivy-image-scan` 从源代码构建 Docker 镜像,然后使用 Trivy 对其进行扫描: - 如果在 OS 包或库中发现任何 `CRITICAL` 或 `HIGH` 严重级别的 CVE,则**中断构建**。 - 生成 **SARIF 报告**并上传至 GitHub Security 标签页,以提供完整的审计追踪。 - 忽略当前没有可用修复方案的漏洞(`ignore-unfixed: true`),以避免产生干扰信息。 ### 任务 2:`trivy-config-scan` 扫描仓库中的所有 IaC 配置文件(Kubernetes YAML、Terraform HCL、Dockerfile): - 检测安全配置错误,例如以 root 用户身份运行容器、暴露敏感端口或缺少资源限制。 - 扫描结果将显示在 GitHub Security 标签页中,供开发人员审查。 ## 🚀 在本地运行 Trivy ### 扫描 Docker 镜像 ``` # 安装 Trivy (Linux) curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh # 扫描来自 Docker Hub 的 image trivy image python:3.12-slim # 仅扫描 CRITICAL 和 HIGH,发现任何问题则失败 trivy image --exit-code 1 --severity CRITICAL,HIGH python:3.12-slim ``` ### 扫描本地文件系统 / 配置文件 ``` # 扫描当前目录以检查 misconfigurations trivy config . # 扫描 Kubernetes manifests trivy config ./kubernetes/ ``` ### 构建并扫描此仓库的应用 ``` docker build -t demo-app:local -f app/Dockerfile app/ trivy image --severity CRITICAL,HIGH demo-app:local ``` ## 🔧 关键配置决策 | 决策 | 原因 | |---|---| | 遇到 CRITICAL/HIGH 时 `exit-code: 1` | **硬性拦截** — 如果容器存在已知且可被利用的 CVE,则构建无法继续 | | `ignore-unfixed: true` | 仅标记有可用补丁的问题,减少误报干扰 | | 上传 SARIF | 集成到 GitHub Advanced Security 中,实现集中化的漏洞管理 | | 每周定时扫描 | 捕获已部署镜像中新发布的 CVE(运行时偏移) | | 配置扫描设为非阻塞(`exit-code: 0`) | 配置错误发现将作为警告进入 Security 标签页,而不会导致构建失败 | ## 📌 生产环境背景 在 PT Link Net Tbk 的企业环境中,此模式进行了以下扩展: - 使用 Trivy Azure Pipelines 任务实现 **Azure DevOps 集成**。 - 引入 **Microsoft Defender for DevOps** 以提供额外的机密扫描和代码安全态势管理。 - 针对任何需要跨团队跟踪的 CRITICAL 级别发现,实现**自动创建 Jira 工单**。
标签:DevSecOps, GitHub Actions, IaC配置审计, Web截图, 上游代理, 子域名突变, 安全扫描, 容器安全, 时序注入, 自动笔记, 请求拦截