Skip to content

Commit fde0d94

Browse files
authored
Merge pull request #622 from EndPositive/makefile-scanners
Introduce Makefile For All Scanners & Hooks
2 parents 3c8dc16 + 6c733f5 commit fde0d94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1550
-284
lines changed

.github/workflows/ci.yaml

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ jobs:
186186
push: true
187187
tags: ${{ steps.docker_meta.outputs.tags }}
188188
labels: ${{ steps.docker_meta.outputs.labels }}
189-
189+
190190
- name: Update Docker Hub Description
191191
uses: peter-evans/dockerhub-description@v2
192192
with:
@@ -246,7 +246,7 @@ jobs:
246246
push: true
247247
tags: ${{ steps.docker_meta.outputs.tags }}
248248
labels: ${{ steps.docker_meta.outputs.labels }}
249-
249+
250250
- name: Update Docker Hub Description
251251
uses: peter-evans/dockerhub-description@v2
252252
with:
@@ -300,7 +300,7 @@ jobs:
300300
tags: ${{ steps.docker_meta.outputs.tags }}
301301
labels: ${{ steps.docker_meta.outputs.labels }}
302302

303-
# ---- New Makefile based CI Pipeline steps ----
303+
# ---- New Makefile based CI Pipeline steps ----
304304

305305
makefile-scanner:
306306
name: "Make Scanners"
@@ -1033,15 +1033,19 @@ jobs:
10331033

10341034
- name: "cascading Scans ncrack Integration Tests"
10351035
run: |
1036-
kubectl -n integration-tests delete scans --all
1037-
# We'll run these in a separate namespace so that only the cascadingRules we want to test will be used
1038-
kubectl create namespace cascading-tests
1036+
1037+
# We'll recreate the namespace so that no cascadingRules of previous tests can exist in there
1038+
if kubectl get namespace integration-tests; then
1039+
kubectl delete namespace integration-tests
1040+
fi
1041+
kubectl create namespace integration-tests
1042+
10391043
# Install cascading-scans hook
1040-
helm upgrade --install dssh ./hooks/cascading-scans/ -n cascading-tests \
1044+
helm upgrade --install dssh ./hooks/cascading-scans/ -n integration-tests \
10411045
--set="hook.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/hook-cascading-scans" \
10421046
--set="hook.image.tag=sha-$(git rev-parse --short HEAD)"
10431047
# Install nmap
1044-
helm -n cascading-tests install nmap ./scanners/nmap/ \
1048+
helm -n integration-tests install nmap ./scanners/nmap/ \
10451049
--set="scanner.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/scanner-nmap" \
10461050
--set="scanner.image.tag=sha-$(git rev-parse --short HEAD)" \
10471051
--set="parser.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/parser-nmap" \
@@ -1051,8 +1055,8 @@ jobs:
10511055
# Install ncrack
10521056
printf "root\nadmin\n" > users.txt
10531057
printf "THEPASSWORDYOUCREATED\n123456\npassword\n" > passwords.txt
1054-
kubectl create secret generic --from-file users.txt --from-file passwords.txt ncrack-lists -n cascading-tests
1055-
cat <<EOF | helm -n cascading-tests install ncrack ./scanners/ncrack \
1058+
kubectl create secret generic --from-file users.txt --from-file passwords.txt ncrack-lists -n integration-tests
1059+
cat <<EOF | helm -n integration-tests install ncrack ./scanners/ncrack \
10561060
--set="scanner.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/scanner-ncrack" \
10571061
--set="scanner.image.tag=sha-$(git rev-parse --short HEAD)" \
10581062
--set="parser.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/parser-ncrack" \
@@ -1073,32 +1077,36 @@ jobs:
10731077
cd tests/integration/
10741078
npx jest --ci --color scanner/cascade-nmap-ncrack.test.js
10751079
# Clean up:
1076-
kubectl -n cascading-tests delete scans --all
1077-
kubectl delete namespace cascading-tests
1080+
kubectl -n integration-tests delete scans --all
1081+
kubectl delete namespace integration-tests
10781082
10791083
# ---- Cascading Scans SSLyze Integration Tests ----
10801084

10811085
- name: "cascading Scans sslyze Integration Tests"
10821086
run: |
1083-
kubectl -n integration-tests delete scans --all
1084-
# We'll run these in a separate namespace so that only the cascadingRules we want to test will be used
1085-
kubectl create namespace cascading-tests
1087+
1088+
# We'll recreate the namespace so that no cascadingRules of previous tests can exist in there
1089+
if kubectl get namespace integration-tests; then
1090+
kubectl delete namespace integration-tests
1091+
fi
1092+
kubectl create namespace integration-tests
1093+
10861094
# Install cascading-scans hook
1087-
helm upgrade --install dssh ./hooks/cascading-scans/ -n cascading-tests --wait \
1095+
helm upgrade --install dssh ./hooks/cascading-scans/ -n integration-tests --wait \
10881096
--set="hook.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/hook-cascading-scans" \
10891097
--set="hook.image.tag=sha-$(git rev-parse --short HEAD)"
10901098
# Install unsafe-https
1091-
helm upgrade --install unsafe-https ./demo-targets/unsafe-https/ -n cascading-tests --wait
1099+
helm upgrade --install unsafe-https ./demo-targets/unsafe-https/ -n integration-tests --wait
10921100
# Install nmap
1093-
helm -n cascading-tests install nmap ./scanners/nmap/ --wait \
1101+
helm -n integration-tests install nmap ./scanners/nmap/ --wait \
10941102
--set="scanner.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/scanner-nmap" \
10951103
--set="scanner.image.tag=sha-$(git rev-parse --short HEAD)" \
10961104
--set="parser.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/parser-nmap" \
10971105
--set="parser.image.tag=sha-$(git rev-parse --short HEAD)" \
10981106
--set="parser.env[0].name=CRASH_ON_FAILED_VALIDATION" \
10991107
--set-string="parser.env[0].value=true"
11001108
# Install sslyze
1101-
helm -n cascading-tests install sslyze ./scanners/sslyze/ --wait \
1109+
helm -n integration-tests install sslyze ./scanners/sslyze/ --wait \
11021110
--set="parser.image.repository=docker.io/${{ env.DOCKER_NAMESPACE }}/parser-sslyze" \
11031111
--set="parser.image.tag=sha-$(git rev-parse --short HEAD)" \
11041112
--set="parser.env[0].name=CRASH_ON_FAILED_VALIDATION" \
@@ -1107,8 +1115,8 @@ jobs:
11071115
cd tests/integration/
11081116
npx jest --ci --color scanner/cascade-nmap-sslyze.test.js
11091117
# Clean up:
1110-
kubectl -n cascading-tests delete scans --all
1111-
kubectl delete namespace cascading-tests
1118+
kubectl -n integration-tests delete scans --all
1119+
kubectl delete namespace integration-tests
11121120
11131121
# ---- Debuging Cluster on Failure ----
11141122

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ npm-ci-all: ## Runs npm ci in all node module subfolders.
2929

3030
.PHONY:
3131
npm-test-all: ## Runs all Jest based test suites.
32-
npm test
32+
npm test -- --testPathIgnorePatterns "/integration-tests/"
33+
34+
test-all: ## Runs all makefile based test suites.
35+
@echo ".: ⚙ Installing the operator for makefile based testing."
36+
cd ./operator && $(MAKE) -s docker-build docker-export kind-import helm-deploy
37+
@echo ".: ⚙ Running make test for all scanner and hook modules."
38+
for dir in scanners/*/ hooks/*/ ; do \
39+
cd $$dir; \
40+
echo ".: ⚙ Running make test for '$$dir'."; \
41+
$(MAKE) -s test || exit 1 ; \
42+
cd -; \
43+
done;
3344

3445
.PHONY:
3546
help: ## Display this help screen.

common.mk

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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 java python
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: | clean-integration-tests 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 javascript dependencies."
70+
cd ./.. && npm ci
71+
cd ./../.. && npm ci
72+
cd ../../${module}-sdk/nodejs && npm ci
73+
cd ./${module}/ && npm ci
74+
75+
unit-test-js: install-deps-js
76+
@echo ".: 🧪 Starting unit-tests for '$(name)' $(module) with 'jest@$(JEST_VERSION)'."
77+
npx --yes --package jest@$(JEST_VERSION) jest --ci --colors --coverage --passWithNoTests ${name}/${module}/
78+
79+
install-deps-py:
80+
@echo ".: ⚙️ Installing all $(module) specific python dependencies."
81+
python -m pip install --upgrade pip setuptools wheel pytest
82+
cd ./$(module)/ && pip install -r requirements.txt
83+
84+
unit-test-py: install-deps-py
85+
cd ./$(module)/ && pytest --ignore-glob='*_local.py' --ignore=tests/docker
86+
87+
unit-test-java:
88+
cd ./$(module)/ && ./gradlew test
89+
90+
common-docker-build:
91+
@echo ".: ⚙️ Build '$(name)' $(module) with BASE_IMG_TAG: '$(BASE_IMG_TAG)'."
92+
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)
93+
94+
common-docker-export:
95+
@echo ".: ⚙️ Saving new docker image archive to '$(module)-$(name).tar'."
96+
docker save $(IMG_NS)/$(module)-$(name):$(IMG_TAG) -o $(module)-$(name).tar
97+
98+
common-kind-import:
99+
@echo ".: 💾 Importing the image archive '$(module)-$(name).tar' to local kind cluster."
100+
kind load image-archive ./$(module)-$(name).tar
101+
102+
deploy-test-deps: deploy-test-dep-namespace
103+
104+
deploy-test-dep-namespace:
105+
# If not exists create namespace where the tests will be executed
106+
kubectl create namespace demo-targets --dry-run=client -o yaml | kubectl apply -f -
107+
108+
deploy-test-dep-dummy-ssh:
109+
# Install dummy-ssh app
110+
helm -n demo-targets upgrade --install dummy-ssh ../../demo-targets/dummy-ssh/ --set="fullnameOverride=dummy-ssh" --wait
111+
112+
deploy-test-dep-unsafe-https:
113+
# Install unsafe-https app
114+
helm -n demo-targets upgrade --install unsafe-https ../../demo-targets/unsafe-https/ --set="fullnameOverride=unsafe-https" --wait
115+
116+
deploy-test-dep-bodgeit:
117+
# Install bodgeit app
118+
helm -n demo-targets upgrade --install bodgeit ../../demo-targets/bodgeit/ --set="fullnameOverride=bodgeit" --wait
119+
120+
deploy-test-dep-petstore:
121+
# Install bodgeit app
122+
helm -n demo-targets upgrade --install petstore ../../demo-targets/swagger-petstore/ --set="fullnameOverride=petstore" --wait
123+
124+
deploy-test-dep-old-wordpress:
125+
# Install old-wordpress app
126+
helm -n demo-targets upgrade --install old-wordpress ../../demo-targets/old-wordpress/ --set="fullnameOverride=old-wordpress" --wait
127+
128+
deploy-test-dep-juiceshop:
129+
# Install juiceshop app
130+
helm -n demo-targets upgrade --install juiceshop ../../demo-targets/juice-shop/ --set="fullnameOverride=juiceshop" --wait
131+
132+
deploy-test-dep-nginx:
133+
# Delete leftover nginx's. Unfortunately can't create deployment only if not exists (like namespaces)
134+
kubectl delete deployment nginx --namespace demo-targets --ignore-not-found --wait
135+
kubectl delete svc nginx --namespace demo-targets --ignore-not-found --wait
136+
# Install plain nginx server
137+
kubectl create deployment --image nginx:alpine nginx --namespace demo-targets
138+
kubectl expose deployment nginx --port 80 --namespace demo-targets
139+
140+
deploy-test-dep-http-webhook:
141+
helm -n integration-tests upgrade --install http-webhook ../../demo-targets/http-webhook/
142+
143+
deploy-test-dep-test-scan:
144+
cd ../../scanners/test-scan/ && $(MAKE) docker-build docker-export kind-import && \
145+
helm -n integration-tests upgrade --install test-scan . \
146+
--set="scanner.image.repository=docker.io/$(IMG_NS)/$(scanner-prefix)-test-scan" \
147+
--set="parser.image.repository=docker.io/$(IMG_NS)/$(parser-prefix)-test-scan" \
148+
--set="parser.image.tag=$(IMG_TAG)" \
149+
--set="scanner.image.tag=$(IMG_TAG)" \
150+
--set="parser.env[0].name=CRASH_ON_FAILED_VALIDATION" \
151+
--set-string="parser.env[0].value=true"
152+
153+
clean:
154+
@echo ".: 🧹 Cleaning up all generated files."
155+
rm -f ./$(module)-$(name).tar
156+
rm -rf ./$(module)/node_modules
157+
rm -rf ./$(module)/coverage
158+
rm -rf ./integration-tests/node_modules
159+
rm -rf ./integration-tests/coverage
160+
rm -rf ../node_modules
161+
rm -rf ../coverage
162+
163+
clean-integration-tests:
164+
@echo ".: 🧹 Resetting 'integration-tests' namespace"
165+
kubectl delete namespace integration-tests --wait || true
166+
kubectl create namespace integration-tests
167+
168+
clean-demo-targets:
169+
@echo ".: 🧹 Resetting 'demo-targets' namespace"
170+
kubectl delete namespace demo-targets --wait || true
171+
kubectl create namespace demo-targets

hooks.mk

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
module = $(hook-prefix)
28+
29+
docker-build: | common-docker-build
30+
docker-export: | common-docker-export
31+
kind-import: | common-kind-import
32+
33+
unit-tests:
34+
@$(MAKE) -s unit-test-js
35+
36+
deploy:
37+
@echo ".: 💾 Deploying '$(name)' $(hook-prefix) HelmChart with the docker tag '$(IMG_TAG)' into kind namespace 'integration-tests'."
38+
helm -n integration-tests upgrade --install $(name) . --wait \
39+
--set="hook.image.repository=docker.io/$(IMG_NS)/$(hook-prefix)-$(name)" \
40+
--set="hook.image.tag=$(IMG_TAG)"
41+
42+
integration-tests:
43+
@echo ".: 🩺 Starting integration test in kind namespace 'integration-tests'."
44+
kubectl -n integration-tests delete scans --all
45+
cd ../../tests/integration/ && npm ci && npx --yes --package jest@$(JEST_VERSION) jest --verbose --ci --colors --coverage --passWithNoTests hooks/$(name)-$(hook-prefix).test.js

hooks/cascading-scans/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
*.map
77
**.js
88
!**.test.js
9+
*.tar

hooks/cascading-scans/.helmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ config/*
3333
Dockerfile
3434
.dockerignore
3535
docs/*
36+
*.tar
3637
hook/*
3738
integration-tests/*
3839
examples/*

0 commit comments

Comments
 (0)