はじめに
あけましておめでとうございます。2022年1発目の記事です。
GitHub Actionsを使用して、以前適当に作成したGo製テストサーバーに対して脆弱性チェックと行いました。
結果は以下です。
github.com
GitHub Actions / Trivy / Dependabot について
GitHub Actionsとは、Githubに組み込まれているCI, CDワークフローを駆動するCI/CDシステムです。
github.co.jp
Trivyとは、リポジトリ(アプリケーションの依存ライブラリ)/コンテナイメージの脆弱性スキャン、Terraform, Kubernetes等の設定ファイルに対するセキュリティ不備検出機能を備えたツールです。
aquasecurity.github.io
Dependabotとは、リポジトリ(アプリケーションの依存ライブラリ)の脆弱性を検知し、それを解決するためのPRを自動で生成してくれるサービスです。特徴としては、GitHubに買収されていることもあり、GitHub Nativeになっており、GitHub Actionsに対しても適用することができます。
Dependabot の設定
Dependabotで go.mod
GitHub Actions
を対象として脆弱性の検知をDailyで行うように設定しました。
設定についての詳細は以下を参照ください。
docs.github.com
設定ファイルは以下です。非常にシンプルに設定できます。これを .github/dependabot.yaml
に記載することで各対象に対して、脆弱性のチェック、PRの生成を行ってくれます。
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
実際に作成されたPRは以下です。
github.com
Trivyの設定
Trivyでは、コンテナイメージのスキャン、リポジトリのスキャン、IaC(Dockerfile, Kubernetes Manifest)のスキャンを行いました。リポジトリのスキャンはDependabotと役割が被ってしまうのではと思うかもしれませんが、対応している機能、カバー範囲が違うので現状は両方設定するのが良さそうに思います。GitHub Actionについては、Trivyを使用したGitHub Actionを公式が用意しているのでこれを使用しています。
github.com
設定は以下です。
コンテナイメージのスキャン
GitHub ActionsでDocker ImageのBuild/Pushを行っているので、その後、PushしたDocker Imageに対してスキャンを行います。
name: Build and push Docker image and Scanning by Trivy vulnerability
on:
push:
branches:
- master
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}/go-test-server
jobs:
docker:
name: Build and push Docker image and Scanning by Trivy vulnerability
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2.4.0
- name: Log in to the Container registry
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
uses: docker/metadata-action@v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha
id: meta
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: '${{ steps.meta.outputs.tags }}'
format: 'template'
template: '@/contrib/sarif.tpl'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: 'trivy-results.sarif'
steps.metaでtagの指定を行い、GitHub Container RegistryにPushしています。このときPushしたものと同様のDocker ImageをTrivyで参照するため、image-ref
に '${{ steps.meta.outputs.tags }}'
を設定しています。
因みに上記の設定ではDocker Imageのtag設定は sha-<commit hash : 7>
になります。
PushしたDocker Imageは以下にあります。
github.com
「コンテナイメージのスキャン」と違い前後のstepに依存する設定はないので該当部分抜粋となります。
- name: Run Trivy vulnerability scanner in repo mode
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
ignore-unfixed: true
format: 'template'
template: '@/contrib/sarif.tpl'
output: 'trivy-results.sarif'
severity: 'CRITICAL'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: 'trivy-results.sarif'
IaC(Dockerfile, Kubernetes Manifest)のスキャン
「リポジトリのスキャン」同様、該当部分抜粋となります。
- name: Run Trivy vulnerability scanner in IaC mode
uses: aquasecurity/trivy-action@master
with:
scan-type: 'config'
hide-progress: false
format: 'table'
exit-code: '1'
ignore-unfixed: true
severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: 'trivy-results.sarif'
終わりに
最近、業務で手を動かせていないのでリハビリとして触りたかったツールを触れたのでよかったです。
近々、他のリポジトリに対しても適用していきたいと思います。また、業務ではGitLabを使っているため(メリデメはありますが)、個人的にはGitHub ActionsとDependabotが使いやすいため、やはりGitHubの方が楽だなと感じました。
今後は、この辺りをもっとDeepに使っていきたいのとPolicy as Code(OPA, Rego)、Renovate周りが個人的に熱いのでプライベートで触っていき業務に活かせればと思います。