Skip to content

Commit 35ad5ef

Browse files
committed
Add trivy image scan
Signed-off-by: Ivan Valdes <[email protected]>
1 parent c36edc5 commit 35ad5ef

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ ifeq (, $(shell which golangci-lint))
186186
$(shell curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION))
187187
endif
188188

189+
TRIVY_VERSION = $(shell cd tools/mod && go list -m -f {{.Version}} github.com/aquasecurity/trivy)
190+
.PHONY: install-trivy
191+
install-trivy: bin/trivy
192+
bin/trivy:
193+
curl -sSfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b ./bin $(TRIVY_VERSION)
194+
195+
.PHONY: run-trivy-image-scan
196+
run-trivy-image-scan: install-trivy
197+
./scripts/scan_latest_released_image.sh
198+
189199
.PHONY: install-lazyfs
190200
install-lazyfs: bin/lazyfs
191201
bin/lazyfs:

scripts/scan_latest_released_image.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Default image registry to use.
6+
REGISTRY=${REGISTRY:-gcr.io/etcd-development/etcd}
7+
8+
# Default severity levels to report.
9+
SEVERITY=${SEVERITY:-HIGH,CRITICAL}
10+
11+
source ./scripts/test_lib.sh
12+
13+
if ! command -v trivy >/dev/null; then
14+
log_error "Error: Cannot find trivy. Please follow the installation instructions at: https://trivy.dev/latest/getting-started/"
15+
exit 1
16+
fi
17+
18+
# Returns the latest tag for the given branch.
19+
function latest_branch_tag {
20+
local branch=$1
21+
local minor="${branch#release-}"
22+
23+
git -c 'versionsort.suffix=-' \
24+
ls-remote --exit-code --refs --sort='version:refname' --tags origin "v${minor}"'*' \
25+
| tail --lines=1 \
26+
| cut --delimiter='/' --fields=3
27+
}
28+
29+
function main {
30+
local current_branch
31+
current_branch=$(git rev-parse --abbrev-ref HEAD)
32+
if [[ ! "${current_branch}" =~ ^release-[0-9]+.[0-9]+$ ]]; then
33+
log_error "Error: This script is intended to be run only on stable release branches (current branch: ${current_branch})."
34+
return 1
35+
fi
36+
37+
local latest_tag
38+
latest_tag=$(latest_branch_tag "$current_branch")
39+
40+
trivy image --severity "${SEVERITY}" "${REGISTRY}:${latest_tag}"
41+
}
42+
43+
main

0 commit comments

Comments
 (0)