😀

Azure DevOps の Azure Pipelines に Trivy をインストールして Docker コンテナ

に公開

世間でソフトウェアの脆弱性が公表され、CVSS スコアが高いので自社が使っている OS やソフトウェア、開発しているアプリは大丈夫なのか?と、その都度対応していたりします。例えば Docker コンテナーを Azure Pipelines でビルドしているなら、パイプラインに脆弱性スキャンを追加して、その時点で脆弱性が含まれる場合は停止して、開発ライフサイクルの最初の方で対処しておけばラクになります。それでは、DevSecOps の一片を Azure Pipelines で実践してみたいと思います。

Azure DevOps に Trivy エクステンションをインストール

下記サイトの Visual Studio Marketplace より Trivy を Azure DevOps にインストールします。

https://marketplace.visualstudio.com/items?itemName=AquaSecurityOfficial.trivy-official

[ Get it free ] をクリックします。

image.png

[ Install ] をクリックします。

image.png

[ Proceed to organization ] をクリックします。

image.png

Azure Pipelines で Trivy を検証

検証用 Azure Repos リポジトリを作成し、Azure Pipelines で新規に下記のパイプラインを作成します。

azure-pipelines.yml
trigger:
- main

pool:
  vmImage: ubuntu-latest

jobs:
- job: TrivyContainerScan
  steps:
  - task: trivy@1
    inputs:
     image: centos:7.6.1810

[ Save and run ] または [ Run ] で実行してみます。

image.png

下記のようにパイプラインが失敗します。

image.png

[ Trivy ] タブをクリックすると脆弱性スキャンの結果が確認できます。CRITICAL が 3 件、全部で 1175 件の脆弱性があることがわかります。

image.png

Docker コンテナーをビルドして Azure Container Registory に登録して検証

検証用 Azure Repos リポジトリに下記の Dockerfile を作成します。

Dockerfile
FROM centos:7.6.1810

RUN yum update -y

Azure Pipelines で以下のようにパイプラインを変更します。

※コンテナーレジストリは自身の環境に合わせて変更してください。

azure-pipelines.yml
trigger:
- main

pool:
  vmImage: ubuntu-latest

jobs:
- job: DockerBuildPush
  steps:
  - task: Docker@2
    inputs:
      containerRegistry: mnrlabo
      repository: test-centos
      command: buildAndPush
      Dockerfile: '**/Dockerfile'
- job: TrivyContainerScan
  steps:
  - task: Docker@2
    inputs:
      containerRegistry: mnrlabo
      command: login
  - task: trivy@1
    inputs:
      image: mnrlabo.azurecr.io/test-centos:$(Build.BuildId)

ファイルを保存するとパイプラインが走るので結果を確認します。

以下のエラーで Trivy そのものの動作が失敗しているようです。

* unable to initialize Podman client

image.png

パイプラインの中で Trivy そのものを Docker からバイナリ実行に変更するため、docker: false を追加します。

azure-pipelines.yml
trigger:
- main

pool:
  vmImage: ubuntu-latest

jobs:
- job: DockerBuildPush
  steps:
  - task: Docker@2
    inputs:
      containerRegistry: mnrlabo
      repository: test-centos
      command: buildAndPush
      Dockerfile: '**/Dockerfile'
- job: TrivyContainerScan
  steps:
  - task: Docker@2
    inputs:
      containerRegistry: mnrlabo
      command: login
  - task: trivy@1
    inputs:
      docker: false
      image: mnrlabo.azurecr.io/test-centos:$(Build.BuildId)

Dockerfile 内で yum update -y を組み込む事で、脆弱性を 1175 から 861 件に減らす事ができました。また、CRITICAL の脆弱性は無くなりました。

image.png

脆弱性を 0 にする事は簡単ではないようです。

Discussion