在 Jenkins 中调用 Puppet 扫描镜像漏洞?

解读

面试官真正想验证的是:

  1. 你是否理解 Puppet 在 DevSecOps 链路中的定位——它负责“让扫描工具以一致、可审计、可回滚的方式跑起来”,而不是自己去“看懂”漏洞。
  2. 你是否能把 Jenkins 的流水线模型与 Puppet 的声明式模型无缝衔接,做到“ Jenkins 触发、Puppet 保证环境、扫描工具产出报告、 Jenkins 消费结果”。
  3. 你是否熟悉国内合规场景:等保 2.0、关基、信创替代,要求“谁运维谁负责、谁建设谁备案”,因此扫描过程必须留痕、可审计、可重现。
  4. 你是否能把“扫描失败”当成一次配置漂移事件,用 Puppet 自动修复运行环境,而不是人工 SSH 上去装工具。

一句话:不是问“ Puppet 能不能扫漏洞”,而是问“怎么用 Puppet 让漏洞扫描在 Jenkins 里变成一次可重复、可审计、可治理的配置动作”。

知识点

  1. Puppet 的两种调用模式

    • Master-Agent: Jenkins 节点通过 puppet agent -t --tags vulnerability_scan 触发指定 Tag,实现“最小化变更”。
    • Bolt / Puppet-Lite: Jenkins 直接在构建节点用 bolt plan run profile::scan::image --targets localhost image=$IMAGE_TAG,无需 Master,适合云原生临时节点。
  2. 类与 Profile 分层设计

    • Role-Profile-Component 三层:Component 安装 Trivy/Clair/Anchore,Profile 拼装“安装-配置-扫描-回传”逻辑,Role 决定“谁需要被扫”。
    • 用 Hiera 做环境隔离:内网离线仓库地址、镜像仓库证书、HTTP 代理等全部下沉到 Hiera,Puppetfile 锁定版本,保证“同一套代码,同一套结果”。
  3. 报告回传与 Jenkins 消费

    • Puppet 侧用 file { '/tmp/scan_result.json': content => $scan_result, tag => 'scan_report' } 落盘, Jenkins 侧 archiveArtifacts 加 publishHTML 展示。
    • 高阶做法:把 SARIF 结果直接写回 GitLab MR API, Jenkins 通过 step([$class: 'GitLabCommitStatusPublisher']) 把状态写回,实现“代码即审计”。
  4. 合规留痕

    • Puppet 报告本身带 run_id、catalog_uuid、environment, Jenkins 把 BUILD_TAG 与 catalog_uuid 写入 MySQL,实现“构建号 ⇄ 配置版本”双向追溯,满足等保“变更审计”要求。
  5. 失败自愈

    • 扫描工具因缺失 libssl1.1 失败 → Puppet 检测到 Exec['trivy --version'] 返回非 0 → 触发 Package['libssl1.1'] 安装 → 重试扫描 → Jenkins 侧只感知最终退出码,实现“环境漂移自动修复”。

答案

示范一个最小可用但可直接落地的 Jenkinsfile + Puppet Profile,兼顾国内离线、合规、可回滚要求。

Jenkinsfile(Declarative)

pipeline {
  agent { label 'scan' }
  environment {
    IMAGE_TAG   = "${params.IMAGE_TAG}"
    PUPPET_ENV  = "production"
    SCAN_REPORT = "trivy-${BUILD_NUMBER}.json"
  }
  stages {
    stage('Prep') {
      steps {
        sh 'rm -f /tmp/scan_result.json'
      }
    }
    stage('Puppet Scan') {
      steps {
        // 国内离线场景:提前在 /etc/puppetlabs/puppet/puppet.conf 设置
        // pluginsync=true, ca_server=master.corp.local
        sh """
          puppet agent -t --environment ${PUPPET_ENV} \
                         --tags vulnerability_scan \
                         --image_tag ${IMAGE_TAG}
        """
      }
    }
    stage('Publish') {
      steps {
        archiveArtifacts artifacts: "/tmp/scan_result.json", fingerprint: true
        publishHTML([allowMissing : false,
                     alwaysLinkToLastBuild: true,
                     keepAll: true,
                     reportDir: '/tmp',
                     reportFiles: 'scan_result.html',
                     reportName: 'Trivy Report'])
      }
    }
  }
  post {
    always {
      // 把 catalog_uuid 写库,合规留痕
      script {
        def uuid = sh(script: "cat /var/lib/puppet/state/last_run_summary.yaml | grep catalog_uuid | awk '{print \\$2}'", returnStdout: true).trim()
        sh "mysql -h audit.corp.local -u builder -pBuilder@123 puppet_audit -e \"insert into scan_log(build_tag,image_tag,catalog_uuid) values('${BUILD_TAG}','${IMAGE_TAG}','${uuid}');\""
      }
    }
  }
}

Puppet Profile:profile::scan::vulnerability

class profile::scan::vulnerability (
  String $image_tag,
) {
  include profile::scan::trivy

  $result_file = '/tmp/scan_result.json'
  $html_file   = '/tmp/scan_result.html'

  exec { "trivy image --format json --output ${result_file} ${image_tag}":
    path        => '/usr/local/bin:/usr/bin:/bin',
    timeout     => 900,
    refreshonly => true,
    subscribe   => File[$result_file],
    notify      => Exec['convert2html'],
  }

  exec { 'convert2html':
    command     => "trivy image --format template --template @contrib/html.tpl -o ${html_file} ${image_tag}",
    path        => '/usr/local/bin:/usr/bin:/bin',
    refreshonly => true,
  }

  file { [$result_file, $html_file]:
    ensure => file,
    owner  => 'jenkins',
    mode   => '0644',
    tag    => 'scan_report',
  }
}

说明

  1. 离线安装:提前用 yum -y localinstall trivy-0.49.1.rpm 做成私有 Yum 源,Puppet Package['trivy'] 指向该源,解决“无法出网”痛点。
  2. 版本锁定:Puppetfile 中 mod 'puppet-trivy', git: 'https://git.corp.local/puppet-trivy.git', ref: 'v1.2.3',确保“今天扫的和半年前扫的用的是同一套规则”。
  3. 失败策略:扫描超时或返回非 0 → Jenkins 当前构建标记为 FAILURE → 触发 post { failure { emailext ... } },同时 Puppet 下次自动修复环境,无需人工登录。

拓展思考

  1. 多集群统一治理
    集团下有“信创 ARM 集群 + x86 集群 + 公有云 ACK”三类环境,扫描工具版本、漏洞库源地址均不同。可抽象出 profile::scan::vulnerability::arm64、profile::scan::vulnerability::x86、profile::scan::vulnerability::ack,用 Hiera 做键值覆盖,同一套 Role 代码,三套环境并行扫描, Jenkins 矩阵构建一次性出三份报告。

  2. 策略即代码
    把“高危漏洞数 > 5 则阻断部署”写成 OPA 策略, Jenkins 把 scan_result.json 推给 OPA sidecar,OPA 返回 allow=false 时流水线直接 error 'Policy violation',实现“安全左移”。 Puppet 负责保证“OPA 侧car 版本与策略库版本一致”,避免“策略漂移”。

  3. 零信任与签名
    等保 2.0 要求“重要操作应通过密码技术保证完整性”。可在 Puppet 侧用 gpg 对扫描报告签名, Jenkins 把 .json 与 .sig 一起归档,审计时能用公钥验证“报告未被 Jenkins 插件篡改”,满足关基评审专家“技术细节经得起问”的苛刻场景。