Skip to content

Commit aba092b

Browse files
Jop Zitmannigthknight
authored andcommitted
Makefiles: add generic hook and common makefile (sharing with scanner makefile)
Signed-off-by: Jop Zitman <jop.zitman@secura.com>
1 parent ddb022a commit aba092b

File tree

3 files changed

+200
-125
lines changed

3 files changed

+200
-125
lines changed

common.mk

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/make -f
2+
#
3+
# SPDX-FileCopyrightText: 2021 iteratec GmbH
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
#
8+
# This Makefile is intended to be used for developement and testing only.
9+
# For using this scanner/hook in production please use the helm chart.
10+
# See: <https://docs.securecodebox.io/docs/getting-started/installation>
11+
#
12+
# This Makefile expects some additional software to be installed:
13+
# - git
14+
# - node + npm
15+
# - docker
16+
# - kind
17+
# - kubectl
18+
# - helm
19+
# - yq
20+
21+
ifeq ($(include_guard),)
22+
$(error you should never run this makefile directly!)
23+
endif
24+
ifeq ($(name),)
25+
$(error name ENV is not set)
26+
endif
27+
28+
# Thx to https://stackoverflow.com/questions/5618615/check-if-a-program-exists-from-a-makefile
29+
EXECUTABLES = make docker kind git node npm npx kubectl helm yq
30+
K := $(foreach exec,$(EXECUTABLES),\
31+
$(if $(shell which $(exec)),some string,$(error "ERROR: The prerequisites are not met to execute this makefile! No '$(exec)' found in your PATH")))
32+
33+
# Variables you might want to override:
34+
#
35+
# IMG_NS: Defines the namespace under which the images are build.
36+
# For `securecodebox/scanner-nmap` `securecodebox` is the namespace
37+
# Defaults to `securecodebox`
38+
#
39+
# BASE_IMG_TAG: Defines the tag of the base image used to build this scanner/hook
40+
#
41+
# IMG_TAG: Tag used to tag the newly created image. Defaults to the shortend commit hash
42+
# prefixed with `sha-` e.g. `sha-ef8de4b7`
43+
#
44+
# JEST_VERSION Defines the jest version used for executing the tests. Defaults to latest
45+
#
46+
# Examples:
47+
# make all IMG_TAG=main
48+
# make deploy IMG_TAG=$(git rev-parse --short HEAD)
49+
# make integration-tests
50+
#
51+
52+
SHELL = /bin/sh
53+
54+
IMG_NS ?= securecodebox
55+
GIT_TAG ?= $$(git rev-parse --short HEAD)
56+
BASE_IMG_TAG ?= latest
57+
IMG_TAG ?= "sha-$(GIT_TAG)"
58+
JEST_VERSION ?= latest
59+
60+
parser-prefix = parser
61+
scanner-prefix = scanner
62+
hook-prefix = hook
63+
64+
test: | unit-tests docker-build docker-export kind-import deploy deploy-test-deps integration-tests
65+
66+
.PHONY: help unit-tests-hook install-deps docker-build docker-export kind-import deploy deploy-test-deps integration-tests all build test
67+
68+
install-deps-js:
69+
@echo ".: ⚙️ Installing all $(module) specific dependencies."
70+
cd ./.. && npm ci
71+
cd ../../${module}-sdk/nodejs && npm ci
72+
cd ./${module}/ && npm ci
73+
74+
unit-test-js: install-deps-js
75+
@echo ".: 🧪 Starting unit-tests for '$(name)' $(module) with 'jest@$(JEST_VERSION)'."
76+
npx --yes --package jest@$(JEST_VERSION) jest --ci --colors --coverage --passWithNoTests ${name}/${module}/
77+
78+
common-docker-build:
79+
@echo ".: ⚙️ Build '$(name)' $(module) with BASE_IMG_TAG: '$(BASE_IMG_TAG)'."
80+
docker build --build-arg=scannerVersion=$(shell yq e .appVersion ./Chart.yaml) --build-arg=baseImageTag=$(BASE_IMG_TAG) --build-arg=namespace=$(IMG_NS) -t $(IMG_NS)/$(module)-$(name):$(IMG_TAG) -f ./$(module)/Dockerfile ./$(module)
81+
82+
common-docker-export:
83+
@echo ".: ⚙️ Saving new docker image archive to '$(module)-$(name).tar'."
84+
docker save $(IMG_NS)/$(module)-$(name):$(IMG_TAG) -o $(module)-$(name).tar
85+
86+
common-kind-import:
87+
@echo ".: 💾 Importing the image archive '$(module)-$(name).tar' to local kind cluster."
88+
kind load image-archive ./$(module)-$(name).tar
89+
90+
deploy-test-deps: deploy-test-dep-namespace
91+
92+
deploy-test-dep-namespace:
93+
# If not exists create namespace where the tests will be executed
94+
kubectl create namespace demo-targets --dry-run=client -o yaml | kubectl apply -f -
95+
96+
deploy-test-dep-dummy-ssh:
97+
# Install dummy-ssh app
98+
helm -n demo-targets upgrade --install dummy-ssh ../../demo-targets/dummy-ssh/ --set="fullnameOverride=dummy-ssh" --wait
99+
100+
deploy-test-dep-unsafe-https:
101+
# Install unsafe-https app
102+
helm -n demo-targets upgrade --install unsafe-https ../../demo-targets/unsafe-https/ --set="fullnameOverride=unsafe-https" --wait
103+
104+
deploy-test-dep-bodgeit:
105+
# Install bodgeit app
106+
helm -n demo-targets upgrade --install bodgeit ../../demo-targets/bodgeit/ --set="fullnameOverride=bodgeit" --wait
107+
108+
deploy-test-dep-petstore:
109+
# Install bodgeit app
110+
helm -n demo-targets upgrade --install petstore ../../demo-targets/swagger-petstore/ --set="fullnameOverride=petstore" --wait
111+
112+
deploy-test-dep-old-wordpress:
113+
# Install old-wordpress app
114+
helm -n demo-targets upgrade --install old-wordpress ../../demo-targets/old-wordpress/ --set="fullnameOverride=old-wordpress" --wait
115+
116+
deploy-test-dep-juiceshop:
117+
# Install juiceshop app
118+
helm -n demo-targets upgrade --install juiceshop ../../demo-targets/juice-shop/ --set="fullnameOverride=juiceshop" --wait
119+
120+
deploy-test-dep-nginx:
121+
# Delete leftover nginx's. Unfortunately can't create deployment only if not exists (like namespaces)
122+
kubectl delete deployment nginx --namespace demo-targets --ignore-not-found --wait
123+
kubectl delete svc nginx --namespace demo-targets --ignore-not-found --wait
124+
# Install plain nginx server
125+
kubectl create deployment --image nginx:alpine nginx --namespace demo-targets
126+
kubectl expose deployment nginx --port 80 --namespace demo-targets
127+
128+
deploy-test-dep-http-webhook:
129+
helm -n integration-tests upgrade --install http-webhook ../../demo-targets/http-webhook/
130+
131+
deploy-test-dep-test-scan:
132+
cd ../../scanners/test-scan/ && $(MAKE) docker-build docker-export kind-import && \
133+
helm -n integration-tests upgrade --install test-scan . \
134+
--set="scanner.image.repository=docker.io/$(IMG_NS)/$(scanner-prefix)-test-scan" \
135+
--set="parser.image.repository=docker.io/$(IMG_NS)/$(parser-prefix)-test-scan" \
136+
--set="parser.image.tag=$(IMG_TAG)" \
137+
--set="scanner.image.tag=$(IMG_TAG)" \
138+
--set="parser.env[0].name=CRASH_ON_FAILED_VALIDATION" \
139+
--set-string="parser.env[0].value=true"
140+
141+
clean:
142+
@echo ".: 🧹 Cleaning up all generated files."
143+
rm -f ./$(module)-$(name).tar
144+
rm -rf ./$(module)/node_modules
145+
rm -rf ./$(module)/coverage
146+
rm -rf ./integration-tests/node_modules
147+
rm -rf ./integration-tests/coverage
148+
rm -rf ../node_modules
149+
rm -rf ../coverage

hooks.mk

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/make -f
2+
#
3+
# SPDX-FileCopyrightText: 2021 iteratec GmbH
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
#
8+
# This Makefile is intended to be used for developement and testing only.
9+
# For using this scanner/hook in production please use the helm chart.
10+
# See: <https://docs.securecodebox.io/docs/getting-started/installation>
11+
#
12+
# This Makefile expects some additional software to be installed:
13+
# - git
14+
# - node + npm
15+
# - docker
16+
# - kind
17+
# - kubectl
18+
# - helm
19+
# - yq
20+
21+
module = hook
22+
prefix = hook
23+
name = ${hook}
24+
25+
include ../../common.mk
26+
27+
docker-build: | common-docker-build
28+
docker-export: | common-docker-export
29+
kind-import: | common-kind-import
30+
31+
unit-tests:
32+
@$(MAKE) -s unit-test-js module=$(hook-prefix)

scanners.mk

Lines changed: 19 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,9 @@
1818
# - helm
1919
# - yq
2020

21-
ifeq ($(include_guard),)
22-
$(error you should never run this makefile directly!)
23-
endif
24-
ifeq ($(scanner),)
25-
$(error scanner ENV is not set)
26-
endif
27-
28-
# Thx to https://stackoverflow.com/questions/5618615/check-if-a-program-exists-from-a-makefile
29-
EXECUTABLES = make docker kind git node npm npx kubectl helm yq
30-
K := $(foreach exec,$(EXECUTABLES),\
31-
$(if $(shell which $(exec)),some string,$(error "ERROR: The prerequisites are not met to execute this makefile! No '$(exec)' found in your PATH")))
32-
33-
34-
# Variables you might want to override:
35-
#
36-
# IMG_NS: Defines the namespace under which the images are build.
37-
# For `securecodebox/scanner-nmap` `securecodebox` is the namespace
38-
# Defaults to `securecodebox`
39-
#
40-
# BASE_IMG_TAG: Defines the tag of the base image used to build this scanner/hook
41-
#
42-
# IMG_TAG: Tag used to tag the newly created image. Defaults to the shortend commit hash
43-
# prefixed with `sha-` e.g. `sha-ef8de4b7`
44-
#
45-
# JEST_VERSION Defines the jest version used for executing the tests. Defaults to latest
46-
#
47-
# Examples:
48-
# make all IMG_TAG=main
49-
# make deploy IMG_TAG=$(git rev-parse --short HEAD)
50-
# make integration-tests
51-
#
21+
name = ${scanner}
5222

53-
SHELL = /bin/sh
54-
55-
IMG_NS ?= securecodebox
56-
GIT_TAG ?= $$(git rev-parse --short HEAD)
57-
BASE_IMG_TAG ?= latest
58-
IMG_TAG ?= "sha-$(GIT_TAG)"
59-
JEST_VERSION ?= latest
60-
61-
scanner-prefix = scanner
62-
parser-prefix = parser
23+
include ../../common.mk
6324

6425
ifeq ($(custom_scanner),)
6526
docker-build: | docker-build-parser
@@ -73,112 +34,45 @@ else
7334
deploy: deploy-with-scanner
7435
endif
7536

76-
77-
test: | unit-tests-parser docker-build docker-export kind-import deploy deploy-test-deps integration-tests
78-
79-
all: | clean install-deps unit-tests-parser docker-build docker-export kind-import deploy deploy-test-deps integration-tests
80-
81-
.PHONY: unit-tests-parser install-deps docker-build docker-export kind-import deploy deploy-test-deps integration-tests all build test
82-
83-
unit-tests-parser:
84-
@echo ".: 🧪 Starting unit-tests for '$(scanner)' parser with 'jest@$(JEST_VERSION)'."
85-
npx --yes --package jest@$(JEST_VERSION) jest --ci --colors --coverage --passWithNoTests ${scanner}/parser/
37+
unit-tests:
38+
@$(MAKE) -s unit-test-js module=$(parser-prefix)
8639

8740
install-deps:
88-
@echo ".: ⚙️ Installing all scanner specific dependencies."
89-
cd ./.. && npm ci
90-
cd ../../parser-sdk/nodejs && npm ci
91-
cd ./parser/ && npm ci
41+
@$(MAKE) -s install-deps-js module=$(parser-prefix)
9242

9343
docker-build-parser:
94-
@echo ".: ⚙️ Build parser with BASE_IMG_TAG: '$(BASE_IMG_TAG)'."
95-
docker build --build-arg=baseImageTag=$(BASE_IMG_TAG) --build-arg=namespace=$(IMG_NS) -t $(IMG_NS)/$(parser-prefix)-$(scanner):$(IMG_TAG) -f ./parser/Dockerfile ./parser
44+
@$(MAKE) -s common-docker-build module=$(parser-prefix)
9645

9746
docker-export-parser:
98-
@echo ".: ⚙️ Saving new docker image archive to '$(parser-prefix)-$(scanner).tar'."
99-
docker save $(IMG_NS)/$(parser-prefix)-$(scanner):$(IMG_TAG) -o $(parser-prefix)-$(scanner).tar
47+
@$(MAKE) -s common-docker-export module=$(parser-prefix)
10048

10149
kind-import-parser:
102-
@echo ".: 💾 Importing the image archive '$(parser-prefix)-$(scanner).tar' to local kind cluster."
103-
kind load image-archive ./$(parser-prefix)-$(scanner).tar
50+
@$(MAKE) -s common-kind-import module=$(parser-prefix)
10451

10552
docker-build-scanner:
106-
@echo ".: ⚙️ Build custom scanner with BASE_IMG_TAG: '$(BASE_IMG_TAG)'."
107-
docker build --build-arg=scannerVersion=$(shell yq e .appVersion ./Chart.yaml) --build-arg=namespace=$(IMG_NS) -t $(IMG_NS)/$(scanner-prefix)-$(scanner):$(IMG_TAG) -f ./scanner/Dockerfile ./scanner
53+
@$(MAKE) -s common-docker-build module=$(scanner-prefix)
10854

10955
docker-export-scanner:
110-
@echo ".: ⚙️ Saving new docker image archive to '$(scanner-prefix)-$(scanner).tar'."; \
111-
docker save $(IMG_NS)/$(scanner-prefix)-$(scanner):$(IMG_TAG) -o $(scanner-prefix)-$(scanner).tar; \
56+
@$(MAKE) -s common-docker-export module=$(scanner-prefix)
11257

11358
kind-import-scanner:
114-
@echo ".: 💾 Importing the image archive '$(scanner-prefix)-$(scanner).tar' to local kind cluster."
115-
kind load image-archive ./$(scanner-prefix)-$(scanner).tar
59+
@$(MAKE) -s common-kind-import module=$(scanner-prefix)
11660

11761
deploy-without-scanner:
118-
@echo ".: 💾 Deploying '$(scanner)' scanner HelmChart with the docker tag '$(IMG_TAG)' into kind namespace 'integration-tests'."
119-
helm -n integration-tests upgrade --install $(scanner) ./ --wait \
120-
--set="parser.image.repository=docker.io/$(IMG_NS)/$(parser-prefix)-$(scanner)" \
62+
@echo ".: 💾 Deploying '$(name)' $(scanner-prefix) HelmChart with the docker tag '$(IMG_TAG)' into kind namespace 'integration-tests'."
63+
helm -n integration-tests upgrade --install $(name) ./ --wait \
64+
--set="parser.image.repository=docker.io/$(IMG_NS)/$(parser-prefix)-$(name)" \
12165
--set="parser.image.tag=$(IMG_TAG)"
12266

12367
deploy-with-scanner:
124-
@echo ".: 💾 Deploying '$(scanner)' scanner HelmChart with the docker tag '$(IMG_TAG)' into kind namespace 'integration-tests'."
125-
helm -n integration-tests upgrade --install $(scanner) ./ --wait \
126-
--set="parser.image.repository=docker.io/$(IMG_NS)/$(parser-prefix)-$(scanner)" \
68+
@echo ".: 💾 Deploying '$(name)' $(scanner-prefix) HelmChart with the docker tag '$(IMG_TAG)' into kind namespace 'integration-tests'."
69+
helm -n integration-tests upgrade --install $(name) ./ --wait \
70+
--set="parser.image.repository=docker.io/$(IMG_NS)/$(parser-prefix)-$(name)" \
12771
--set="parser.image.tag=$(IMG_TAG)" \
128-
--set="scanner.image.repository=docker.io/$(IMG_NS)/$(scanner-prefix)-$(scanner)" \
72+
--set="scanner.image.repository=docker.io/$(IMG_NS)/$(scanner-prefix)-$(name)" \
12973
--set="scanner.image.tag=$(IMG_TAG)"
13074

131-
deploy-test-deps: deploy-test-dep-namespace
132-
133-
deploy-test-dep-namespace:
134-
# If not exists create namespace where the tests will be executed
135-
kubectl create namespace demo-targets --dry-run=client -o yaml | kubectl apply -f -
136-
137-
deploy-test-dep-dummy-ssh:
138-
# Install dummy-ssh app
139-
helm -n demo-targets upgrade --install dummy-ssh ../../demo-targets/dummy-ssh/ --set="fullnameOverride=dummy-ssh" --wait
140-
141-
deploy-test-dep-unsafe-https:
142-
# Install unsafe-https app
143-
helm -n demo-targets upgrade --install unsafe-https ../../demo-targets/unsafe-https/ --set="fullnameOverride=unsafe-https" --wait
144-
145-
deploy-test-dep-bodgeit:
146-
# Install bodgeit app
147-
helm -n demo-targets upgrade --install bodgeit ../../demo-targets/bodgeit/ --set="fullnameOverride=bodgeit" --wait
148-
149-
deploy-test-dep-petstore:
150-
# Install bodgeit app
151-
helm -n demo-targets upgrade --install petstore ../../demo-targets/swagger-petstore/ --set="fullnameOverride=petstore" --wait
152-
153-
deploy-test-dep-old-wordpress:
154-
# Install old-wordpress app
155-
helm -n demo-targets upgrade --install old-wordpress ../../demo-targets/old-wordpress/ --set="fullnameOverride=old-wordpress" --wait
156-
157-
deploy-test-dep-juiceshop:
158-
# Install juiceshop app
159-
helm -n demo-targets upgrade --install juiceshop ../../demo-targets/juice-shop/ --set="fullnameOverride=juiceshop" --wait
160-
161-
deploy-test-dep-nginx:
162-
# Delete leftover nginx's. Unfortunately can't create deployment only if not exists (like namespaces)
163-
kubectl delete deployment nginx --namespace demo-targets --ignore-not-found --wait
164-
kubectl delete svc nginx --namespace demo-targets --ignore-not-found --wait
165-
# Install plain nginx server
166-
kubectl create deployment --image nginx:alpine nginx --namespace demo-targets
167-
kubectl expose deployment nginx --port 80 --namespace demo-targets
168-
169-
install-integration-test-deps:
170-
17175
integration-tests:
17276
@echo ".: 🩺 Starting integration test in kind namespace 'integration-tests'."
17377
kubectl -n integration-tests delete scans --all
174-
cd ../../tests/integration/ && npm ci && npx --yes --package jest@$(JEST_VERSION) jest --verbose --ci --colors --coverage --passWithNoTests scanner/${scanner}.test.js
175-
176-
clean:
177-
@echo ".: 🧹 Cleaning up all generated files."
178-
rm -f ./$(parser-prefix)-$(scanner).tar
179-
rm -rf ./parser/node_modules
180-
rm -rf ./parser/coverage
181-
rm -rf ./integration-tests/node_modules
182-
rm -rf ./integration-tests/coverage
183-
rm -rf ../node_modules
184-
rm -rf ../coverage
78+
cd ../../tests/integration/ && npm ci && npx --yes --package jest@$(JEST_VERSION) jest --verbose --ci --colors --coverage --passWithNoTests ${scanner-prefix}/${name}.test.js

0 commit comments

Comments
 (0)