iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
DevOps

Dev's Ops 啟程系列 第 29

[Day 29] Trivy - 介紹、操作與導入CI/CD

Aqua Trivy

Vulnerability and Misconfiguration Scanning

Trivy 可以掃描漏洞與有問題的設定檔

在這個容器的時代,如何讓你的Image保持一定的安全性?最好盡量挑選官方原廠出來且經過認證的Image,另外加上使用Image Scanner會越來越顯的重要。

官方github : https://github.com/aquasecurity/trivy

選擇Trivy的原因:

  • golang 專案 (自己的偏好XD)
  • github 星星數夠多
  • 方便容器化操作使用
Github Repo Star Language
Trivy 8.7k golang
Clair 8.2k golang
Anchore 1.3k python

以上數據統計到 台灣時間 2021-10-07

操作快速導覽

image 漏洞掃描

trivy image ${YOUR_IMAGE}

ex: 用alpine image測試掃描功能

trivy image alpine:3.14.2

以下為掃描結果

https://ithelp.ithome.com.tw/upload/images/20211006/2011528913LTQ51zVF.png

輸出 json

trivy image ${YOUR_IMAGE} -f json

跳過更新

因為trivy會限制你的每天更新次數,所以需要更新時再下指令更新到本機

trivy image --skip-update ${YOUR_IMAGE}

將trivy 導入CI/CD

首先有個基本的image - trivy-build.dockerfile

選定一個trivy版本,要RUN --download-db-only 是因為之後就不用每次使用此image都要更新漏洞資料庫囉,若要更新再重新build一個image即可。

FROM aquasec/trivy:0.19.2

RUN trivy image --download-db-only 

with CI/CD (以Drone為例)

以下提供我的Drone Pipeline設定檔案給各位參考

Drone - image scan

使用 --skip-update 避免被鎖住更新,因為trivy官方有限制每天更新次數。

kind: pipeline
type: docker
name: image-scan

steps:
- name: scan-using-images
  image: rain123473/trivy:latest
  commands:
    - trivy image --skip-update

Drone - trivy database update by tag event

我這裡是設計在你的git repo內build個tag就可以觸發更新trivy的漏洞資料庫了,至於更新的頻率可以自行調整,我的話是每天更新一次就好囉。

kind: pipeline
type: docker
name: image-scan

steps:
- name: update-trivy-database
  when:
    ref: refs/tags/*
  image: alpine:3.14.2
  commands:
    - docker build --no-cache --pull --force-rm -t "rain123473/trivy:${DRONE_TAG}" -f trivy-build.dockerfile .
    - docker push "rain123473/trivy:${DRONE_TAG}"
    - docker tag "rain123473/trivy:${DRONE_TAG}" "rain123473/trivy:latest"
    - docker push "rain123473/trivy:latest"
    - docker rmi "rain123473/trivy:${DRONE_TAG}"
  privileged: true
  volumes:
  - name: docker
    path: /var/run/docker.sock

volumes:
- name: docker
  host:
    path: /var/run/docker.sock

以上這樣子就將trivy導入CI/CD流程內囉,各位在玩玩看吧~/images/emoticon/emoticon07.gif


上一篇
[Day 28] Gitea - 如何自簽憑證與Nginx注意
下一篇
[Day 30] 在 Dev's Ops 啟程之後
系列文
Dev's Ops 啟程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言