ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Kubescape 与 GitHub Code Scanning 集成实战:用 SARIF 把 Kubernetes 安全扫描结果送进代码安全看板

Kubescape 与 GitHub Code Scanning 集成实战:用 SARIF 把 Kubernetes 安全扫描结果送进代码安全看板 Kubescape 与 GitHub Code Scanning 集成实战用 SARIF 把 Kubernetes 安全扫描结果送进代码安全看板【免费下载链接】kubescapeKubescape is an open-source Kubernetes security platform for your IDE, CI/CD pipelines, and clusters. It includes risk analysis, security, compliance, and misconfiguration scanning, saving Kubernetes users and administrators precious time, effort, and resources.项目地址: https://gitcode.com/GitHub_Trending/ku/kubescape本指南讲解如何将开源 Kubernetes 安全平台 Kubescape 接入 GitHub Code Scanning——GitHub 原生的安全告警看板。Kubescape 会把对 Kubernetes 清单文件、Helm Chart 与 Kustomize 目录的扫描结果输出为 SARIFStatic Analysis Results Interchange Format文件再由 GitHub 解析为Security → Code scanning页面中的告警并支持在 Pull Request 的Files changed视图中内联标注。读完本文你将掌握完整的 GitHub Actions 工作流编写方法、合规阈值门禁配置、分支保护规则设置以及常见故障的排查手段。集成原理从扫描结果到 Code Scanning 告警Kubescape 与 GitHub Code Scanning 的对接建立在 SARIF 这一标准之上。SARIF 是静态分析工具输出的通用交换格式GitHub Code Scanning 与 Azure DevOps 等平台均可直接消费。整体数据流如下Push / PR → GitHub Actions → Kubescape scan → results.sarif → GitHub Security dashboard具体来说Kubescape 在 GitHub Actions 工作流中扫描仓库内的 Kubernetes 资源将结果写入.sarif文件工作流随后通过github/codeql-action/upload-sarif动作把该文件上传给 GitHubGitHub 解析 SARIF 后把每一条发现呈现为仓库Security → Code scanning看板中的一条告警。这一链路在仓库中的底层实现位于 SARIF 输出器 sarifprinter.go几点实现细节值得了解严重级别映射Kubescape 将每个控制项control的严重度评分score factor映射为 SARIF 的level。源码scoreFactorToSARIFSeverityLevelsarifprinter.go规定评分 ≥ 9.0 映射为error≥ 4.0 映射为warning其余映射为note。因此你在 Code Scanning 中看到的error/warning/note三级告警并非随机而是由控制项的 base score 决定的。安全严重度属性每个规则还带security-severity属性取值为控制项评分形如9.0GitHub Code Scanning 会据此渲染与筛选告警严重度。告警指纹与去重输出器为每条 finding 计算kubescapeFindingFingerprint基于控制 ID、资源 ID、文件相对路径、行号列号及失败证据生成 SHA-256 指纹GitHub 依据规则 ID 与位置去重避免同一问题重复刷屏。修复建议SARIF 结果中会附带fixes字段给出基于失败路径FixPath的替换建议同时消息文本会列出受影响字段。敏感字段如 Secret 数据、容器环境变量值默认以占位符脱敏这是有意为之——SARIF 文件常常被上传到 CI 看板或作为流水线产物提交具体逻辑见 sarifprinter.go 中showSecrets的注释说明。位置解析告警携带文件路径与精确行号、列号。主位置指向修复点若失败由多个字段引起还会为每个失败字段添加relatedLocations参见resolveReviewPathLocations。无法关联到文件路径的结果会被直接跳过因为 GitHub 不接受无位置的结果。格式限制重要SARIF 格式仅支持扫描本地文件或 Git 仓库不支持对运行中集群的扫描集群扫描无文件可定位。在 CI 中扫描仓库目录正好满足这一前提。前置条件一个 GitHub 仓库公开仓库或已开启 GitHub Advanced Security 的私有仓库仓库中包含 Kubernetes 清单文件、Helm Chart 或 Kustomize 配置仓库已启用 GitHub Actions。注意GitHub Code Scanning 对公开仓库免费私有仓库需要 GitHub Advanced Security。基本示例一键接入的 Workflow在仓库中创建.github/workflows/kubescape.ymlname: Kubescape on: push: branches: - main pull_request: branches: - main jobs: kubescape: name: Scan Kubernetes manifests runs-on: ubuntu-latest permissions: security-events: write # required to upload SARIF results actions: read contents: read steps: - name: Checkout code uses: actions/checkoutv4 - name: Install Kubescape run: | curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | bash echo $HOME/.kubescape/bin $GITHUB_PATH - name: Run Kubescape scan run: | kubescape scan . \ --format sarif \ --output results.sarif \ --verbose continue-on-error: true # upload results even when findings are detected - name: Upload SARIF to GitHub Code Scanning uses: github/codeql-action/upload-sarifv3 with: sarif_file: results.sarif几个关键点permissions.security-events: write必不可少缺少该权限时 SARIF 上传会静默失败而工作流本身不会报错--format sarif指定输出格式--output results.sarif指定输出文件上传步骤的sarif_file必须与之保持一致--verbose输出更详细的日志便于排查continue-on-error: true保证即使扫描发现大量问题扫描步骤会返回非零退出码后续上传步骤依然会执行告警照常进入看板。工作流运行结束后进入仓库的Security → Code scanning即可查看结果。安装脚本即仓库根目录下的 install.sh安全产品建议先阅读脚本内容再执行更多安装方式见 installation.md。扫描指定路径子目录、Helm Chart 与 Kustomize如果清单文件位于子目录直接把路径传给 Kubescape- name: Install Kubescape run: | curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | bash echo $HOME/.kubescape/bin $GITHUB_PATH - name: Run Kubescape scan run: | kubescape scan ./k8s/ \ --format sarif \ --output results.sarif continue-on-error: true也可以扫描 Helm Chart 或 Kustomize 目录只需指向其根目录。Kubescape 会自动识别目录中存在Chart.yaml即按 Helm Chart 处理默认加载values.yaml可用--helm-values指定自定义 values 文件存在kustomization.yaml/kustomization.yml/kustomization即按 Kustomize 处理会先渲染再扫描- name: Install Kubescape run: | curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | bash echo $HOME/.kubescape/bin $GITHUB_PATH - name: Run Kubescape scan run: | kubescape scan ./charts/my-app/ \ --format sarif \ --output results.sarif continue-on-error: true注意若目录同时包含Chart.yaml与kustomization.yamlKubescape 会将其视为 Helm Chart。另外Kustomize 可能跟随扫描根目录之外的引用嵌套的helmCharts.repo也可能拉取远程 Chart 内容因此只扫描你信任的源码。扫描特定合规框架默认的kubescape scan path运行的是综合安全视图allcontrols。若要限定到某个合规框架使用framework子命令- name: Install Kubescape run: | curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | bash echo $HOME/.kubescape/bin $GITHUB_PATH - name: Run Kubescape scan (NSA framework) run: | kubescape scan framework nsa . \ --format sarif \ --output results.sarif continue-on-error: true内置框架包括nsaNSA-CISA Kubernetes 加固指南、mitreMITRE ATTCK 容器化威胁矩阵以及按需下载的 CIS Benchmark 框架如cis-v1.23-t1.0.1。运行kubescape list frameworks可查看完整列表——该命令的实现位于 list.go其帮助文本列出了本地默认框架的查询方式从源码看Kubescape 内置的原生框架标识符为allcontrols、nsa、mitre见 datastructures.go更多框架含 CIS由策略库按需下载。框架子命令的解析入口见 framework.go支持nsa,mitre这种逗号分隔的多框架组合。设置合规阈值让工作流在分数过低时失败--compliance-threshold用于在合规分数低于阈值时让命令失败退出码 1。该标志在 scan.go 中定义取值是一个百分比0–100 之间源码在 framework.go 中对越界值做了校验默认值为 0它只对scan framework、scan control、scan workload以及--view resource|control生效普通的kubectl scan path安全视图不会评估该阈值。若低于阈值框架扫描会以错误scan compliance-score is below permitted threshold结束见 framework.go。与continue-on-error: true组合使用确保 SARIF 上传仍会执行- name: Install Kubescape run: | curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | bash echo $HOME/.kubescape/bin $GITHUB_PATH - name: Run Kubescape scan run: | kubescape scan . \ --format sarif \ --output results.sarif continue-on-error: true - name: Upload SARIF to GitHub Code Scanning uses: github/codeql-action/upload-sarifv3 with: sarif_file: results.sarif - name: Enforce compliance threshold run: | kubescape scan . \ --compliance-threshold 80 \ --format pretty-printer注意这里有意运行 Kubescape 两次。第一次始终生成 SARIF 文件供上传第二次应用阈值当分数低于 80 时该步骤返回失败未设置continue-on-error从而让整个 Job 失败、阻止合并。注意旧标志--fail-threshold短名-t已被弃用源码中它被绑定到独立变量并标记为 deprecated提示改用--compliance-threshold见 scan.go请勿在新工作流中使用。使用官方 Kubescape GitHub Action除手动安装外还可直接使用官方 GitHub Action仓库kubescape/github-action本仓库 README 与文档均将其作为推荐接入方式name: Kubescape on: push: branches: - main pull_request: branches: - main jobs: kubescape: name: Scan Kubernetes manifests runs-on: ubuntu-latest permissions: security-events: write actions: read contents: read steps: - name: Checkout code uses: actions/checkoutv4 - name: Run Kubescape scan uses: kubescape/github-actionmain continue-on-error: true with: format: sarif outputFile: results.sarif args: . - name: Upload SARIF to GitHub Code Scanning uses: github/codeql-action/upload-sarifv3 with: sarif_file: results.sarif该动作封装了 Kubescape 的安装与扫描参数format: sarif对应--format sarifoutputFile: results.sarif对应--output results.sarifargs: .则是传给kubescape scan的路径参数。查看 GitHub Security 看板中的结果工作流完成后打开仓库首页点击Security标签在左侧边栏选择Code scanning每条 Kubescape 发现都会列作一条告警包含控制项名称与 ID例如Privileged container/C-0057受影响的文件与行号严重级别error、warning或note对应关系见前文源码分析指向 Kubescape 控制项文档的修复指引链接SARIF 规则的help字段即由控制项的 remediation 说明生成。在 Pull Request 上发现的告警还会内联标注在Files changed标签页中审查者无需跳转即可看到问题所在行。分支保护规则阻止引入新问题的 PR 合并要让引入新安全发现的 PR 无法合并可配置分支保护规则要求 Code Scanning 检查通过进入仓库Settings → Branches点击Add branch protection rule或编辑main分支的既有规则启用Require status checks to pass before merging搜索并选择 Kubescape 工作流检查例如Kubescape / Scan Kubernetes manifests可选启用Require branches to be up to date before merging点击Save changes。规则生效后Kubescape 工作流检查必须通过PR 才能合并。注意关于 continue-on-error 与门禁的配合基本示例与官方 Action 示例都在扫描步骤使用了continue-on-error: true——它会把步骤标记为失败但让整个 Job 成功而分支保护以 Job 结论为准因此这两类工作流无论发现多少问题检查都会通过。若要让安全发现真正阻止合并应使用上文设置合规阈值的模式专门的Enforce compliance threshold步骤不带continue-on-error阈值不达标即让 Job 失败。另外也可以在Settings → Code security → Code scanning中配置失败严重度failure severity让 GitHub 本身在达到该严重度的告警时阻止合并。故障排查Security 标签页中没有告警在Actions标签页确认工作流是否成功运行确认permissions块包含security-events: write——缺少该权限时 SARIF 上传会静默失败私有仓库需在Settings → Security analysis中确认已启用 GitHub Advanced Security确认上传步骤的sarif_file路径与扫描步骤的--output路径一致。results.sarif: no such file or directory扫描步骤在写出文件之前就失败了。查看工作流日志常见原因kubescape: command not found——安装后 PATH 未持久化确认 Install 步骤包含echo $HOME/.kubescape/bin $GITHUB_PATH扫描路径下没有找到 Kubernetes 清单文件Kubescape 安装失败网络问题或缺少curl/bash。可在扫描命令后添加--verbose获取详细输出。设置了continue-on-error: true但扫描仍以退出码 1 结束这是预期行为。continue-on-error: true允许后续步骤继续执行但会把该步骤标记为失败只要后续步骤包括 SARIF 上传正常完成整个 Job 依然成功。多次运行后出现重复告警GitHub 依据规则 ID 与位置对 Code Scanning 告警去重。若出现重复检查是否为同一提交同时上传了push与pull_request两次事件的结果。告警严重度显示为noneKubescape 将控制项严重度映射为 SARIF 级别见前文scoreFactorToSARIFSeverityLevel的映射规则。如果控制项没有严重度信息请确认 Kubescape 版本为 v3 或更高kubescape version延伸阅读官方 GitHub Actionkubescape/github-action可通过uses: kubescape/github-actionmain直接引用更多 SARIF 输出细节与格式对照可阅读 getting-started.md 中的 Output Formats 一节JSON、JUnit、SARIF、HTML、PDFKubescape 命令参考cli-reference.md其他 CI 集成Azure DevOps、CircleCI、GitLab CI可参考 azure-pipelines.md、circleci.md、gitlab-ci.md控制项库与修复指引仓库 rules 目录收录了每个控制项的raw.rego规则与rule.metadata.json元数据可离线查阅控制项 ID、描述与修复建议。【免费下载链接】kubescapeKubescape is an open-source Kubernetes security platform for your IDE, CI/CD pipelines, and clusters. It includes risk analysis, security, compliance, and misconfiguration scanning, saving Kubernetes users and administrators precious time, effort, and resources.项目地址: https://gitcode.com/GitHub_Trending/ku/kubescape创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表