紙の裏のメモ

崖の上のポニョみたいなイントネーションです。坂の上の雲みたいな(ry

Container Analysis APIを用いてコンテナイメージの脆弱性のSummaryのdataを取得する

目次

  • Container Analysisの必要性
  • Container Analysis APIの利用
  • gcloud コマンドでうまくいかなかったことについて
  • おわりに

Container Analysisの必要性

コンテナ イメージによって開発やデプロイはよりスムーズに行えるようになった。 それに伴いコンテナイメージ内のOSに対する脆弱性にも注意深くなる必要がある。

GCPでは Container Analysisという機能があり、Container Registry 内のコンテナ イメージをScanし脆弱性を検知してくれる。

https://cloud.google.com/container-registry/docs/container-analysis?hl=ja (2019-10-28現在ではベータ版である。)

この機能を有効にするとCloud ConsoleのContainer Registryのページから検知された脆弱性を確認することができる。

また、Contanier Analysis のRest APIを利用してSummaryを取得することもできる。この記事ではそちらに焦点をあてたい。

Container Analysis APIの利用

Cloud Consoleで脆弱性の確認はできるものの、それだけでは毎回脆弱性の情報を拾いに行く必要が出てくる。 流石にそれだと面倒なので、定期的にそのdataをSlackにPostする仕組みを整えた。

以下具体的な説明だが、ここではSlackでごにょごにょする部分には触れずに、Container Analysis APIをどう利用したかのみを書く。

今回はContainer Registryのprojects.occurrences.getVulnerabilitySummaryのmethodを利用して脆弱性の情報を取得した。

初めはgcloud コマンドで取得するつもりでいたが、うまくいかなかったので断念した。それについては後ほど触れる。 実行したコマンドはこれ。 [RESOURCE_URL]はhttps://[HOSTNAME]/[PROJECT_ID]/[IMAGE_ID]@sha256:[HASH]を入れる。

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" 'https://containeranalysis.googleapis.com/v1beta1/projects/{PROJECT_ID}/occurrences:vulnerabilitySummary?filter=resourceUrl%3D%22[RESOURCE_URL]%22'

そこで得たdataをjqを利用して加工、SlackへPostする。という流れ。

一応jqを利用した部分のスクリプトをメモしておく。

severityがHIGHな件数を取得するスクリプト

curl ....(同上) | jq -r ".counts[] | select(.severity == \"HIGH\" and .resource.name == \"[project名]\") | .totalCount")

2つ目が修正可能な件数を取得するもの。

curl ....(同上) | jq -r "[.counts[] | select(.resource.name == \"[project 名]\") | .fixableCount // 0 | tonumber] | add // 0")

gcloud コマンドでうまくいかなかったことについて

ドキュメントによると以下のようなgcloudコマンドでイメージの脆弱性の情報を取得できるとある。(2019-10-29時点)

gcloud beta container images list-tags --show-occurrences [HOSTNAME]/[PROJECT_ID]/[IMAGE_ID]

しかし、このコマンドによって得られた脆弱性の情報は Cloud Console上で確認できる脆弱性の情報と一致しなかった。 具体的に言うと脆弱性のレベルの内訳が一致していないかった。

調べた感じだと 単純にcvssから算出した 重大度とlinux distributionから指定された重大度があるようで、 Cloud Console上ではlinux distributionから指定された重大度を加味した情報が表示されている。その一方で、上記のgcloud コマンドでは それが加味されておらず、内訳が一致しなかった模様。

https://cloud.google.com/container-registry/docs/container-analysis(英語ページより)

Two types of severity are associated with each vulnerability:

- Effective severity - The severity level assigned by the Linux distribution. If distribution-specific severity levels are unavailable, Container Analysis uses the severity level assigned by the note provider.

- CVSS score- The Common Vulnerability Scoring System score and associated severity level. Refer to the CVSS 3 specificationfor details on how CVSS scores are calculated.

For a given vulnerability, the severity derived from a calculated CVSS score might not match the effective severity. Linux distributions that assign severity levels use their own criteria to assess the specific impacts of a vulnerability on their distributions.

おわりに

gcloudコマンドで欲しかった情報が取れなかったのは少しつらかった。 今はbeta版ということもあるので仕方はない部分もあるが、そのうちそのあたりは修正されていくのではと思う。