Skip to content

Commit 89e0e1e

Browse files
authored
Merge pull request #396 from Nordix/lentzi90/security-scan
Add weekly security scan using govulncheck and trivy
2 parents b36070c + 810a068 commit 89e0e1e

File tree

7 files changed

+185
-2
lines changed

7 files changed

+185
-2
lines changed

.github/workflows/e2e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040

4141
- name: Build and push a container image to Kind
4242
run: |
43-
docker build -t ${{ env.image_tag }} .
43+
make docker-build IMG=${{ env.image_tag }}
4444
kind load docker-image ${{ env.image_tag }} ${{ env.image_tag }} --name orc
4545
4646
- name: Deploy orc
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Weekly security scan
2+
3+
on:
4+
schedule:
5+
# Cron for every Monday at 8:42 UTC.
6+
- cron: "42 8 * * 1"
7+
8+
# Remove all permissions from GITHUB_TOKEN except metadata.
9+
permissions: {}
10+
11+
jobs:
12+
scan:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
branch: [main, release-1.0]
17+
name: Trivy
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Check out code
21+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag=v4.2.2
22+
with:
23+
ref: ${{ matrix.branch }}
24+
- name: Calculate go version
25+
id: vars
26+
run: echo "go_version=$(make go-version)" >> $GITHUB_OUTPUT
27+
- name: Set up Go
28+
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # tag=v5.4.0
29+
with:
30+
go-version: ${{ steps.vars.outputs.go_version }}
31+
- name: Run verify security target
32+
run: make verify-security

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Dockerfile.cross
2222
*.swp
2323
*.swo
2424
*~
25+
.devcontainer
2526

2627
# website dynamic assets
2728
/venv

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Build the manager binary
2-
FROM golang:1.23 AS builder
2+
ARG GO_VERSION="1.23"
3+
FROM golang:${GO_VERSION} AS builder
34
ARG TARGETOS
45
ARG TARGETARCH
56

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
IMG ?= controller:latest
33
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
44
ENVTEST_K8S_VERSION = 1.29.0
5+
TRIVY_VERSION = 0.49.1
6+
GO_VERSION ?= 1.23.9
57

68
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
79
ifeq (,$(shell go env GOBIN))
@@ -21,6 +23,9 @@ CONTAINER_TOOL ?= docker
2123
SHELL = /usr/bin/env bash -o pipefail
2224
.SHELLFLAGS = -ec
2325

26+
# Enables shell script tracing. Enable by running: TRACE=1 make <target>
27+
TRACE ?= 0
28+
2429
.PHONY: all
2530
all: build
2631

@@ -157,6 +162,7 @@ docker-build: ## Build docker image with the manager.
157162
source hack/version.sh && version::get_git_vars && version::get_build_date && \
158163
$(CONTAINER_TOOL) build \
159164
--tag ${IMG} \
165+
--build-arg "GO_VERSION=$(GO_VERSION)" \
160166
--build-arg "BUILD_DATE=$${BUILD_DATE}" \
161167
--build-arg "GIT_COMMIT=$${GIT_COMMIT}" \
162168
--build-arg "GIT_RELEASE_COMMIT=$${GIT_RELEASE_COMMIT}" \
@@ -185,6 +191,7 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
185191
$(CONTAINER_TOOL) buildx build \
186192
--platform=$(PLATFORMS) \
187193
--tag ${IMG} --push \
194+
--build-arg "GO_VERSION=$(GO_VERSION)" \
188195
--build-arg "BUILD_DATE=$${BUILD_DATE}" \
189196
--build-arg "GIT_COMMIT=$${GIT_COMMIT}" \
190197
--build-arg "GIT_RELEASE_COMMIT=$${GIT_RELEASE_COMMIT}" \
@@ -251,6 +258,28 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
251258
undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
252259
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
253260

261+
##@ Security
262+
263+
.PHONY: verify-container-images
264+
verify-container-images: ## Verify container images
265+
TRACE=$(TRACE) ./hack/verify-container-images.sh $(TRIVY_VERSION)
266+
267+
.PHONY: verify-govulncheck
268+
verify-govulncheck: $(GOVULNCHECK) ## Verify code for vulnerabilities
269+
$(GOVULNCHECK) ./... && R1=$$? || R1=$$?; \
270+
if [ "$$R1" -ne "0" ]; then \
271+
exit 1; \
272+
fi
273+
274+
.PHONY: verify-security
275+
verify-security: ## Verify code and images for vulnerabilities
276+
$(MAKE) verify-container-images && R1=$$? || R1=$$?; \
277+
$(MAKE) verify-govulncheck && R2=$$? || R2=$$?; \
278+
if [ "$$R1" -ne "0" ] || [ "$$R2" -ne "0" ]; then \
279+
echo "Check for vulnerabilities failed! There are vulnerabilities to be fixed"; \
280+
exit 1; \
281+
fi
282+
254283
##@ Dependencies
255284

256285
## Location to install dependencies to
@@ -269,6 +298,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
269298
GOLANGCI_KAL = $(LOCALBIN)/golangci-kube-api-linter
270299
MOCKGEN = $(LOCALBIN)/mockgen
271300
KUTTL = $(LOCALBIN)/kubectl-kuttl
301+
GOVULNCHECK = $(LOCALBIN)/govulncheck
272302

273303
## Tool Versions
274304
KUSTOMIZE_VERSION ?= v5.6.0
@@ -278,6 +308,7 @@ GOLANGCI_LINT_VERSION ?= v2.0.1
278308
KAL_VERSION ?= v0.0.0-20250501211755-2c83ed303cde
279309
MOCKGEN_VERSION ?= v0.5.0
280310
KUTTL_VERSION ?= v0.22.0
311+
GOVULNCHECK_VERSION ?= v1.1.4
281312

282313
.PHONY: kustomize
283314
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -327,6 +358,11 @@ kuttl: $(KUTTL) ## Download kuttl locally if necessary.
327358
$(KUTTL): $(LOCALBIN)
328359
$(call go-install-tool,$(KUTTL),github.com/kudobuilder/kuttl/cmd/kubectl-kuttl,$(KUTTL_VERSION))
329360

361+
.PHONY: govulncheck
362+
govulncheck: $(GOVULNCHECK) ## Download govulncheck locally if necessary.
363+
$(GOVULNCHECK): $(LOCALBIN)
364+
$(call go-install-tool,$(GOVULNCHECK),golang.org/x/vuln/cmd/govulncheck,$(GOVULNCHECK_VERSION))
365+
330366
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
331367
# $1 - target path with name of binary
332368
# $2 - package url which can be installed
@@ -342,3 +378,8 @@ mv $(1) $(1)-$(3) ;\
342378
} ;\
343379
ln -sf $(1)-$(3) $(1)
344380
endef
381+
382+
##@ helpers:
383+
384+
go-version: ## Print the go version we use to compile our binaries and images
385+
@echo $(GO_VERSION)

hack/ensure-trivy.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# Copyright 2023 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
if [[ "${TRACE-0}" == "1" ]]; then
22+
set -o xtrace
23+
fi
24+
25+
VERSION=${1}
26+
27+
GO_OS="$(go env GOOS)"
28+
if [[ "${GO_OS}" == "linux" ]]; then
29+
TRIVY_OS="Linux"
30+
elif [[ "${GO_OS}" == "darwin"* ]]; then
31+
TRIVY_OS="macOS"
32+
fi
33+
34+
GO_ARCH="$(go env GOARCH)"
35+
if [[ "${GO_ARCH}" == "amd" ]]; then
36+
TRIVY_ARCH="32bit"
37+
elif [[ "${GO_ARCH}" == "amd64"* ]]; then
38+
TRIVY_ARCH="64bit"
39+
elif [[ "${GO_ARCH}" == "arm" ]]; then
40+
TRIVY_ARCH="ARM"
41+
elif [[ "${GO_ARCH}" == "arm64" ]]; then
42+
TRIVY_ARCH="ARM64"
43+
fi
44+
45+
TOOL_BIN=bin
46+
mkdir -p ${TOOL_BIN}
47+
48+
TRIVY="${TOOL_BIN}/trivy/${VERSION}/trivy"
49+
50+
# Downloads trivy scanner
51+
if [ ! -f "$TRIVY" ]; then
52+
curl -L -o ${TOOL_BIN}/trivy.tar.gz "https://github.com/aquasecurity/trivy/releases/download/v${VERSION}/trivy_${VERSION}_${TRIVY_OS}-${TRIVY_ARCH}.tar.gz"
53+
mkdir -p "${TOOL_BIN}/trivy/${VERSION}"
54+
tar -xf "${TOOL_BIN}/trivy.tar.gz" -C "${TOOL_BIN}/trivy/${VERSION}" trivy
55+
chmod +x "${TOOL_BIN}/trivy/${VERSION}/trivy"
56+
rm "${TOOL_BIN}/trivy.tar.gz"
57+
fi

hack/verify-container-images.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
if [[ "${TRACE-0}" == "1" ]]; then
22+
set -o xtrace
23+
fi
24+
25+
VERSION=${1}
26+
GO_ARCH="$(go env GOARCH)"
27+
DB_MIRROR="public.ecr.aws/aquasecurity/trivy-db"
28+
29+
REPO_ROOT=$(git rev-parse --show-toplevel)
30+
"${REPO_ROOT}/hack/ensure-trivy.sh" "${VERSION}"
31+
32+
TRIVY="${REPO_ROOT}/bin/trivy/${VERSION}/trivy"
33+
34+
# Build the container image to be scanned
35+
make IMG=quay.io/orc/openstack-resource-controller-${GO_ARCH}:dev docker-build
36+
37+
# Scan the images
38+
"${TRIVY}" image --db-repository="${DB_MIRROR}" -q --exit-code 1 --ignore-unfixed --severity MEDIUM,HIGH,CRITICAL quay.io/orc/openstack-resource-controller-"${GO_ARCH}":dev && R1=$? || R1=$?
39+
40+
echo ""
41+
BRed='\033[1;31m'
42+
BGreen='\033[1;32m'
43+
NC='\033[0m' # No
44+
45+
if [ "$R1" -ne "0" ]
46+
then
47+
echo -e "${BRed}Check container images failed! There are vulnerabilities to be fixed${NC}"
48+
exit 1
49+
fi
50+
51+
echo -e "${BGreen}Check container images passed! No vulnerability found${NC}"

0 commit comments

Comments
 (0)