|
| 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 |
0 commit comments