-
Notifications
You must be signed in to change notification settings - Fork 31
/
Makefile
276 lines (226 loc) · 12 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
PWD := $(CURDIR)
GIT_VERSION := $(shell git describe --tags --match "v[0-9]*")
VARMOR_PATH := cmd/varmor
CLASSIFIER_PATH := cmd/classifier
REGISTRY_AP ?= elkeid-ap-southeast-1.cr.volces.com
REGISTRY_DEV ?= elkeid-ap-southeast-1.cr.volces.com
NAMESPACE ?= varmor
NAMESPACE_DEV ?= varmor-test
REPO_AP = $(REGISTRY_AP)/$(NAMESPACE)
REPO_DEV = $(REGISTRY_DEV)/$(NAMESPACE_DEV)
VARMOR_IMAGE_NAME := varmor
VARMOR_IMAGE_TAG := $(shell VERSION=$(GIT_VERSION); echo $${VERSION%-*-*})
VARMOR_IMAGE_TAG_DEV := $(GIT_VERSION)
CLASSIFIER_IMAGE_NAME := classifier
CLASSIFIER_IMAGE_TAG := $(VARMOR_IMAGE_TAG)
CLASSIFIER_IMAGE_TAG_DEV := $(VARMOR_IMAGE_TAG_DEV)
VARMOR_IMAGE_AP ?= $(REPO_AP)/$(VARMOR_IMAGE_NAME):$(VARMOR_IMAGE_TAG)
VARMOR_IMAGE_DEV ?= $(REPO_DEV)/$(VARMOR_IMAGE_NAME):$(VARMOR_IMAGE_TAG_DEV)
CLASSIFIER_IMAGE_AP ?= $(REPO_AP)/$(CLASSIFIER_IMAGE_NAME):$(CLASSIFIER_IMAGE_TAG)
CLASSIFIER_IMAGE_DEV ?= $(REPO_DEV)/$(CLASSIFIER_IMAGE_NAME):$(CLASSIFIER_IMAGE_TAG_DEV)
CHART_APP_VERSION := $(VARMOR_IMAGE_TAG)
CHART_APP_VERSION_DEV := $(GIT_VERSION)
CHART_VERSION := $(shell echo $(CHART_APP_VERSION)| sed 's/^v//')
CHART_VERSION_DEV := $(shell echo $(CHART_APP_VERSION_DEV)| sed 's/^v//')
KERNEL_RELEASE = $(shell uname -r)
APPARMOR_ABI_NAME = kernel-$(KERNEL_RELEASE)
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
# Download controller-gen locally if necessary.
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen:
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
# Download envtest-setup locally if necessary.
ENVTEST = $(shell pwd)/bin/setup-envtest
envtest:
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
.PHONY: all
all: build
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Development
.PHONY: generate-apparmor-abi
generate-apparmor-abi: ## Generate the AppArmor feature ABI of development environment. All policy must be developed and tested under this ABI.
@echo "[+] Generate the AppArmor feature ABI of development environment."
rm -rf config/apparmor.d/abi/*
aa-features-abi -x -w config/apparmor.d/abi/$(APPARMOR_ABI_NAME)
cp config/apparmor.d/abi/$(APPARMOR_ABI_NAME) config/apparmor.d/abi/varmor
.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
@echo "[+] Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects"
$(CONTROLLER_GEN) crd paths="./apis/varmor/..." output:crd:artifacts:config=config/crds
cp config/crds/* manifests/varmor/templates/crds/
.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
@echo "[+] Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations."
$(CONTROLLER_GEN) object:headerFile="scripts/boilerplate.go.txt" paths="./..."
@echo "[+] Patch zz_generated.deepcopy.go to add custom deepcopy for SyscallRawRules"
sed -i '/\tif in.SyscallRawRules != nil {/,/\t}/c\\tif in.SyscallRawRules != nil {\n\t\tin, out := &in.SyscallRawRules, &out.SyscallRawRules\n\t\t*out = make([]specs_go.LinuxSyscall, len(*in))\n\t\tlinuxSyscallDeepCopyInto(in, out)' apis/varmor/v1beta1/zz_generated.deepcopy.go
.PHONY: build-ebpf
build-ebpf: ## Generate the ebpf code and lib.
@echo "[+] Generate the ebpf code and lib."
make -C ./vArmor-ebpf generate-ebpf
.PHONY: copy-ebpf
copy-ebpf: ## Copy the ebpf code and lib.
@echo "[+] Copy the ebpf code and lib."
cp vArmor-ebpf/pkg/processtracer/bpf_bpfel.go pkg/processtracer
cp vArmor-ebpf/pkg/processtracer/bpf_bpfel.o pkg/processtracer
cp vArmor-ebpf/pkg/bpfenforcer/bpf_bpfel.go pkg/lsm/bpfenforcer
cp vArmor-ebpf/pkg/bpfenforcer/bpf_bpfel.o pkg/lsm/bpfenforcer
goimports:
ifeq (, $(shell which goimports))
@{ \
echo "goimports not found!";\
echo "installing goimports...";\
go install golang.org/x/tools/cmd/goimports@latest
}
else
GO_IMPORTS=$(shell which goimports)
endif
.PHONY: fmt
fmt: ## Run go fmt against code.
@echo "[+] Run go fmt against code."
go fmt ./... && $(GO_IMPORTS) -w ./
.PHONY: vet
vet: ## Run go vet against code.
@echo "[+] Run go vet against code."
go vet ./...
.PHONY: test-unit
test-unit: ## Run unit tests.
@echo "[+] Running unit tests."
go test ./... -coverprofile coverage.out
.PHONY: test
test: manifests generate fmt vet test-unit ## Run tests.
##@ Build
.PHONY: local
local: ## Build local binary.
@echo "[+] Build local binary."
go build -o bin/vArmor $(PWD)/$(VARMOR_PATH)
.PHONY: build
build: manifests generate build-ebpf copy-ebpf vet local ## Build local binary when apis or bpf code were modified.
.PHONY: docker-build
docker-build: docker-build-varmor-amd64 docker-build-varmor-arm64 docker-build-classifier-amd64 docker-build-classifier-arm64 ## Build container images.
.PHONY: docker-build-dev
docker-build-dev: docker-build-varmor-amd64-dev docker-build-varmor-arm64-dev docker-build-classifier-amd64-dev docker-build-classifier-arm64-dev ## Build container images without check, only for development.
.PHONY: docker-build-dev-ci
docker-build-dev-ci: docker-build-varmor-amd64-dev docker-build-classifier-amd64-dev docker-save-ci-dev ## Build container images without check, only for development.
docker-build-varmor-amd64:
@echo "[+] Build varmor-amd64 image for release version"
@docker buildx build --file $(PWD)/$(VARMOR_PATH)/Dockerfile --tag $(VARMOR_IMAGE_AP)-amd64 --platform linux/amd64 --build-arg TARGETPLATFORM="linux/amd64" --load .
docker-build-varmor-arm64:
@echo "[+] Build varmor-arm64 image for the release version"
@docker buildx build --file $(PWD)/$(VARMOR_PATH)/Dockerfile --tag $(VARMOR_IMAGE_AP)-arm64 --platform linux/arm64 --build-arg TARGETPLATFORM="linux/arm64" --load .
docker-build-classifier-amd64:
@echo "[+] Build classifier-amd64 image for the release version"
@docker buildx build --file $(PWD)/$(CLASSIFIER_PATH)/Dockerfile --tag $(CLASSIFIER_IMAGE_AP)-amd64 --platform linux/amd64 --load .
docker-build-classifier-arm64:
@echo "[+] Build classifier-arm64 image for the release version"
@docker buildx build --file $(PWD)/$(CLASSIFIER_PATH)/Dockerfile --tag $(CLASSIFIER_IMAGE_AP)-arm64 --platform linux/arm64 --load .
docker-build-varmor-amd64-dev:
@echo "[+] Build varmor-amd64 image for the development version"
@docker buildx build --file $(PWD)/$(VARMOR_PATH)/Dockerfile --tag $(VARMOR_IMAGE_DEV)-amd64 --platform linux/amd64 --build-arg TARGETPLATFORM="linux/amd64" --load .
docker-build-varmor-arm64-dev:
@echo "[+] Build varmor-arm64 image for the development version"
@docker buildx build --file $(PWD)/$(VARMOR_PATH)/Dockerfile --tag $(VARMOR_IMAGE_DEV)-arm64 --platform linux/arm64 --build-arg TARGETPLATFORM="linux/arm64" --load .
docker-build-classifier-amd64-dev:
@echo "[+] Build classifier-amd64 image for the development version"
@docker buildx build --file $(PWD)/$(CLASSIFIER_PATH)/Dockerfile --tag $(CLASSIFIER_IMAGE_DEV)-amd64 --platform linux/amd64 --load .
docker-build-classifier-arm64-dev:
@echo "[+] Build classifier-arm64 image for the development version"
@docker buildx build --file $(PWD)/$(CLASSIFIER_PATH)/Dockerfile --tag $(CLASSIFIER_IMAGE_DEV)-arm64 --platform linux/arm64 --load .
docker-save-ci-dev:
@docker tag $(VARMOR_IMAGE_DEV)-amd64 $(VARMOR_IMAGE_DEV)
@docker tag $(CLASSIFIER_IMAGE_DEV)-amd64 $(CLASSIFIER_IMAGE_DEV)
@echo "[+] Saving varmor-amd64 image to varmor-amd64.tar"
@docker save $(VARMOR_IMAGE_DEV) -o varmor-amd64.tar
@echo "[+] Saving classifier-amd64 image to classifier-amd64.tar"
@docker save $(CLASSIFIER_IMAGE_DEV) -o classifier-amd64.tar
##@ Package
.PHONY: helm-package
helm-package: ## Package helm chart.
helm package manifests/varmor --version $(CHART_VERSION) --app-version $(CHART_APP_VERSION)
helm-package-dev: ## Package helm chart for development.
helm package manifests/varmor --version $(CHART_VERSION_DEV) --app-version $(CHART_APP_VERSION_DEV)
.PHONY: demo-package
demo-package: ## Package the demo resources.
tar -C ../ -czf vArmor-test.tar.gz ./vArmor/config ./vArmor/test
##@ Push artifacts (Note: Logging in to the registry is required beforehand.)
.PHONY: push
push-dev: ## Push images and chart to the private repository for development.
docker push $(VARMOR_IMAGE_DEV)-amd64
@echo "----------------------------------------"
docker push $(VARMOR_IMAGE_DEV)-arm64
@echo "----------------------------------------"
-docker manifest rm $(VARMOR_IMAGE_DEV)
@echo "----------------------------------------"
docker manifest create $(VARMOR_IMAGE_DEV) $(VARMOR_IMAGE_DEV)-amd64 $(VARMOR_IMAGE_DEV)-arm64
@echo "----------------------------------------"
docker manifest push $(VARMOR_IMAGE_DEV)
@echo "----------------------------------------"
docker push $(CLASSIFIER_IMAGE_DEV)-amd64
@echo "----------------------------------------"
docker push $(CLASSIFIER_IMAGE_DEV)-arm64
@echo "----------------------------------------"
-docker manifest rm $(CLASSIFIER_IMAGE_DEV)
@echo "----------------------------------------"
docker manifest create $(CLASSIFIER_IMAGE_DEV) $(CLASSIFIER_IMAGE_DEV)-amd64 $(CLASSIFIER_IMAGE_DEV)-arm64
@echo "----------------------------------------"
docker manifest push $(CLASSIFIER_IMAGE_DEV)
@echo "----------------------------------------"
helm push varmor-$(CHART_VERSION_DEV).tgz oci://$(REPO_DEV)
push: ## Push images and chart to the public repository for release.
docker push $(VARMOR_IMAGE_AP)-amd64
@echo "----------------------------------------"
docker push $(VARMOR_IMAGE_AP)-arm64
@echo "----------------------------------------"
-docker manifest rm $(VARMOR_IMAGE_AP)
@echo "----------------------------------------"
docker manifest create $(VARMOR_IMAGE_AP) $(VARMOR_IMAGE_AP)-amd64 $(VARMOR_IMAGE_AP)-arm64
@echo "----------------------------------------"
docker manifest push $(VARMOR_IMAGE_AP)
@echo "----------------------------------------"
docker push $(CLASSIFIER_IMAGE_AP)-amd64
@echo "----------------------------------------"
docker push $(CLASSIFIER_IMAGE_AP)-arm64
@echo "----------------------------------------"
-docker manifest rm $(CLASSIFIER_IMAGE_AP)
@echo "----------------------------------------"
docker manifest create $(CLASSIFIER_IMAGE_AP) $(CLASSIFIER_IMAGE_AP)-amd64 $(CLASSIFIER_IMAGE_AP)-arm64
@echo "----------------------------------------"
docker manifest push $(CLASSIFIER_IMAGE_AP)
@echo "----------------------------------------"
helm push varmor-$(CHART_VERSION).tgz oci://$(REPO_AP)