Skip to content

Commit 7830907

Browse files
add targets for verifying code and images for vulnerabilities (#1486)
Signed-off-by: Prajyot-Parab <[email protected]>
1 parent b0ab74f commit 7830907

File tree

8 files changed

+1766
-53
lines changed

8 files changed

+1766
-53
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
# Build the manager binary
18-
FROM --platform=${BUILDPLATFORM} golang:1.20.7 as toolchain
18+
FROM --platform=${BUILDPLATFORM} golang:1.20.11 as toolchain
1919

2020
# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
2121
ARG goproxy=https://proxy.golang.org,direct

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ ROOT_DIR_RELATIVE := .
1818

1919
include $(ROOT_DIR_RELATIVE)/common.mk
2020

21+
GO_VERSION ?= 1.20.11
22+
2123
# Image URL to use all building/pushing image targets
2224
IMG ?= controller:latest
2325
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
@@ -46,6 +48,8 @@ MOCKGEN := $(TOOLS_BIN_DIR)/mockgen
4648
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
4749
CONVERSION_VERIFIER := $(TOOLS_BIN_DIR)/conversion-verifier
4850
SETUP_ENVTEST := $(TOOLS_BIN_DIR)/setup-envtest
51+
GOVULNCHECK := $(TOOLS_BIN_DIR)/govulncheck
52+
TRIVY := $(TOOLS_BIN_DIR)/trivy
4953

5054
STAGING_REGISTRY ?= gcr.io/k8s-staging-capi-ibmcloud
5155
STAGING_BUCKET ?= artifacts.k8s-staging-capi-ibmcloud.appspot.com
@@ -512,6 +516,27 @@ verify-gen: generate ## Verfiy go generated files are up to date
512516
verify-conversions: $(CONVERSION_VERIFIER) ## Verifies expected API conversion are in place
513517
$(CONVERSION_VERIFIER)
514518

519+
.PHONY: verify-container-images
520+
verify-container-images: $(TRIVY) ## Verify container images
521+
TRACE=$(TRACE) ./hack/verify-container-images.sh
522+
523+
.PHONY: verify-govulncheck
524+
verify-govulncheck: $(GOVULNCHECK) ## Verify code for vulnerabilities
525+
$(GOVULNCHECK) ./... && R1=$$? || R1=$$?; \
526+
$(GOVULNCHECK) -C "$(TOOLS_DIR)" ./... && R2=$$? || R2=$$?; \
527+
if [ "$$R1" -ne "0" ] || [ "$$R2" -ne "0" ]; then \
528+
exit 1; \
529+
fi
530+
531+
.PHONY: verify-security
532+
verify-security: ## Verify code and images for vulnerabilities
533+
$(MAKE) verify-container-images && R1=$$? || R1=$$?; \
534+
$(MAKE) verify-govulncheck && R2=$$? || R2=$$?; \
535+
if [ "$$R1" -ne "0" ] || [ "$$R2" -ne "0" ]; then \
536+
echo "Check for vulnerabilities failed! There are vulnerabilities to be fixed"; \
537+
exit 1; \
538+
fi
539+
515540
## --------------------------------------
516541
## Cleanup / Verification
517542
## --------------------------------------
@@ -543,6 +568,10 @@ clean-temporary: ## Remove all temporary files and folders
543568
clean-release: ## Remove the release folder
544569
rm -rf $(RELEASE_DIR)
545570

571+
.PHONY: clean-release-git
572+
clean-release-git: ## Restores the git files usually modified during a release
573+
git restore ./*manager_image_patch.yaml ./*manager_pull_policy.yaml
574+
546575
.PHONY: clean-generated-conversions
547576
clean-generated-conversions: ## Remove files generated by conversion-gen from the mentioned dirs
548577
(IFS=','; for i in $(SRC_DIRS); do find $$i -type f -name 'zz_generated.conversion*' -exec rm -f {} \;; done)
@@ -561,3 +590,11 @@ clean-kind: ## Cleans up the kind cluster with the name $CAPI_KIND_CLUSTER_NAME
561590
kind-cluster: ## Create a new kind cluster designed for development with Tilt
562591
hack/kind-install.sh
563592

593+
## --------------------------------------
594+
## Helpers
595+
## --------------------------------------
596+
597+
##@ helpers:
598+
599+
go-version: ## Print the go version we use to compile our binaries and images
600+
@echo $(GO_VERSION)

hack/ccm/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ARG TARGETPLATFORM=linux/amd64
1818
ARG ARCH=amd64
1919

2020
# Build IBM cloud controller manager binary
21-
FROM golang:1.20.7 AS ccm-builder
21+
FROM golang:1.20.11 AS ccm-builder
2222
ARG ARCH
2323
ARG POWERVS_CLOUD_CONTROLLER_COMMIT
2424
WORKDIR /build

hack/tools/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,11 @@ $(CONVERSION_VERIFIER): $(BIN_DIR) go.mod go.sum ## Build a local copy of conver
121121
SETUP_ENVTEST := $(BIN_DIR)/setup-envtest
122122
$(SETUP_ENVTEST): $(BIN_DIR) go.mod go.sum ## Build a local copy of setup-envtest.
123123
go build -tags=capibmtools -o $@ sigs.k8s.io/controller-runtime/tools/setup-envtest
124+
125+
GOVULNCHECK := $(BIN_DIR)/govulncheck
126+
$(GOVULNCHECK): $(BIN_DIR) go.mod go.sum ## Build a local copy of govulncheck.
127+
go build -tags=capibmtools -o $@ golang.org/x/vuln/cmd/govulncheck
128+
129+
TRIVY := $(BIN_DIR)/trivy
130+
$(TRIVY): $(BIN_DIR) go.mod go.sum ## Build a local copy of trivy.
131+
go build -tags=capibmtools -o $@ github.com/aquasecurity/trivy/cmd/trivy

hack/tools/go.mod

Lines changed: 289 additions & 17 deletions
Large diffs are not rendered by default.

hack/tools/go.sum

Lines changed: 1379 additions & 34 deletions
Large diffs are not rendered by default.

hack/tools/tools.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ limitations under the License.
2121
package tools
2222

2323
import (
24+
_ "github.com/aquasecurity/trivy/cmd/trivy"
2425
_ "github.com/drone/envsubst/v2/cmd/envsubst"
2526
_ "github.com/golang/mock/mockgen"
2627
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
2728
_ "github.com/itchyny/gojq/cmd/gojq"
2829
_ "github.com/joelanford/go-apidiff"
2930
_ "github.com/onsi/ginkgo/v2/ginkgo"
31+
_ "golang.org/x/vuln/cmd/govulncheck"
3032
_ "gotest.tools/gotestsum"
3133
_ "k8s.io/code-generator/cmd/conversion-gen"
3234
_ "sigs.k8s.io/cluster-api/hack/tools/conversion-verifier"

hack/verify-container-images.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
GO_ARCH="$(go env GOARCH)"
26+
27+
REPO_ROOT=$(git rev-parse --show-toplevel)
28+
29+
TRIVY="${REPO_ROOT}/hack/tools/bin/trivy"
30+
31+
# Builds all the container images to be scanned and cleans up changes to ./*manager_image_patch.yaml ./*manager_pull_policy.yaml.
32+
make REGISTRY=gcr.io/k8s-staging-capi-ibmcloud PULL_POLICY=IfNotPresent TAG=dev OUTPUT_TYPE=type=docker docker-build
33+
make clean-release-git
34+
35+
# Scan the images
36+
"${TRIVY}" image -q --exit-code 1 --ignore-unfixed --severity MEDIUM,HIGH,CRITICAL gcr.io/k8s-staging-capi-ibmcloud/cluster-api-ibmcloud-controller-"${GO_ARCH}":dev && R1=$? || R1=$?
37+
38+
echo ""
39+
BRed='\033[1;31m'
40+
BGreen='\033[1;32m'
41+
NC='\033[0m' # No
42+
43+
if [ "$R1" -ne "0" ]
44+
then
45+
echo -e "${BRed}Check container images failed! There are vulnerabilities to be fixed${NC}"
46+
exit 1
47+
fi
48+
49+
echo -e "${BGreen}Check container images passed! No vulnerability found${NC}"

0 commit comments

Comments
 (0)