From dc8570ca8716ecca8ac1730c6b5a8f9b29743e8a Mon Sep 17 00:00:00 2001 From: florian Date: Sat, 23 Jul 2022 11:29:18 +0200 Subject: [PATCH] tests - add status type --- examples/magento/setup-autoconf.yml | 10 ++++ examples/magento/tests.json | 2 +- examples/mongo-express/autoconf.yml | 41 ++++++++++++++++ .../modsec/www.example.com/mongo-express.conf | 0 examples/mongo-express/docker-compose.yml | 46 ++++++++---------- .../{setup.sh => setup-docker.sh} | 4 +- examples/mongo-express/setup-swarm.sh | 13 +++++ examples/mongo-express/swarm.yml | 48 +++++++++++++++++++ examples/mongo-express/tests.json | 16 +++++++ tests/Test.py | 13 +++-- 10 files changed, 160 insertions(+), 33 deletions(-) create mode 100755 examples/magento/setup-autoconf.yml create mode 100644 examples/mongo-express/autoconf.yml rename examples/mongo-express/{bw_data => bw-data}/configs/modsec/www.example.com/mongo-express.conf (100%) rename examples/mongo-express/{setup.sh => setup-docker.sh} (63%) mode change 100644 => 100755 create mode 100755 examples/mongo-express/setup-swarm.sh create mode 100644 examples/mongo-express/swarm.yml create mode 100644 examples/mongo-express/tests.json diff --git a/examples/magento/setup-autoconf.yml b/examples/magento/setup-autoconf.yml new file mode 100755 index 000000000..49d951315 --- /dev/null +++ b/examples/magento/setup-autoconf.yml @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ $(id -u) -ne 0 ] ; then + echo "❌ Run me as root" + exit 1 +fi + +mkdir elasticsearch-data +chown 1001:1001 elasticsearch-data +chmod 770 elasticsearch-data diff --git a/examples/magento/tests.json b/examples/magento/tests.json index 6fd376641..050a2c2ab 100644 --- a/examples/magento/tests.json +++ b/examples/magento/tests.json @@ -11,7 +11,7 @@ { "type": "string", "url": "https://www.example.com", - "string": "hello" + "string": "magento" } ] } diff --git a/examples/mongo-express/autoconf.yml b/examples/mongo-express/autoconf.yml new file mode 100644 index 000000000..6580bb121 --- /dev/null +++ b/examples/mongo-express/autoconf.yml @@ -0,0 +1,41 @@ +version: "3" + +services: + + mongo: + image: mongo:latest + networks: + bw-services: + aliases: + - mongo + volumes: + - ./db:/data/db + environment: + - MONGO_INITDB_ROOT_USERNAME=root # replace with a less obvious username + - MONGO_INITDB_ROOT_PASSWORD=toor # replace with a better password + - MONGO_INITDB_DATABASE=mongo # replace with the database name of your choice + + mongo-ui: + image: mongo-express:latest + networks: + bw-services: + aliases: + - mongo-ui + environment: + - ME_CONFIG_MONGODB_SERVER=mongo + - ME_CONFIG_MONGODB_ADMINUSERNAME=root # replace with a less obvious username + - ME_CONFIG_MONGODB_ADMINPASSWORD=toor # replace with a better password + - ME_CONFIG_BASICAUTH_USERNAME=changeme # replace with a better username + - ME_CONFIG_BASICAUTH_PASSWORD=changeme # replace with a better password + restart: unless-stopped + depends_on: + - mongo + labels: + - bunkerweb.USE_REVERSE_PROXY=yes + - bunkerweb.REVERSE_PROXY_URL=/ + - bunkerweb.REVERSE_PROXY_HOST=http://mongo-ui:8081 + +networks: + bw-services: + external: + name: bw-services \ No newline at end of file diff --git a/examples/mongo-express/bw_data/configs/modsec/www.example.com/mongo-express.conf b/examples/mongo-express/bw-data/configs/modsec/www.example.com/mongo-express.conf similarity index 100% rename from examples/mongo-express/bw_data/configs/modsec/www.example.com/mongo-express.conf rename to examples/mongo-express/bw-data/configs/modsec/www.example.com/mongo-express.conf diff --git a/examples/mongo-express/docker-compose.yml b/examples/mongo-express/docker-compose.yml index 81abed22f..a60de12e1 100644 --- a/examples/mongo-express/docker-compose.yml +++ b/examples/mongo-express/docker-compose.yml @@ -1,9 +1,28 @@ version: "3" services: + + mybunker: + image: bunkerity/bunkerweb:1.4.2 + ports: + - 80:8080 + - 443:8443 + volumes: + - ./bw-data:/data + environment: + - SERVER_NAME=www.example.com # replace with your domain + - SERVE_FILES=no + - DISABLE_DEFAULT_SERVER=yes + - AUTO_LETS_ENCRYPT=yes + - USE_CLIENT_CACHE=yes + - USE_GZIP=yes + - AUTO_LETS_ENCRYPT=yes + - USE_REVERSE_PROXY=yes + - REVERSE_PROXY_URL=/ + - REVERSE_PROXY_HOST=http://mongo-ui:8081 + mongo: image: mongo:latest - container_name: mongo volumes: - ./db:/data/db environment: @@ -21,27 +40,4 @@ services: - ME_CONFIG_BASICAUTH_PASSWORD=changeme # replace with a better password restart: unless-stopped depends_on: - - mongo - - mybunker: - image: bunkerity/bunkerweb:1.4.2 - ports: - - 80:8080 - - 443:8443 - volumes: - - ./bw_data:/data - environment: - - SERVER_NAME=www.example.com # replace with your domain - - SERVE_FILES=no - - DISABLE_DEFAULT_SERVER=yes - - AUTO_LETS_ENCRYPT=yes - - USE_CLIENT_CACHE=yes - - USE_GZIP=yes - - AUTO_LETS_ENCRYPT=yes - - USE_REVERSE_PROXY=yes - - REVERSE_PROXY_URL=/ - - REVERSE_PROXY_HOST=http://mongo-ui:8081 - - USE_ANTIBOT=javascript # put the antibot of your choice here - -volumes: - bw_config: \ No newline at end of file + - mongo \ No newline at end of file diff --git a/examples/mongo-express/setup.sh b/examples/mongo-express/setup-docker.sh old mode 100644 new mode 100755 similarity index 63% rename from examples/mongo-express/setup.sh rename to examples/mongo-express/setup-docker.sh index 9c8f5bffa..0faae2d5a --- a/examples/mongo-express/setup.sh +++ b/examples/mongo-express/setup-docker.sh @@ -5,5 +5,5 @@ if [ $(id -u) -ne 0 ] ; then exit 1 fi -chown -R root:101 bw_data -chmod -R 770 bw_data \ No newline at end of file +chown -R root:101 bw-data +chmod -R 770 bw-data diff --git a/examples/mongo-express/setup-swarm.sh b/examples/mongo-express/setup-swarm.sh new file mode 100755 index 000000000..f11ae1e9d --- /dev/null +++ b/examples/mongo-express/setup-swarm.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# docker-compose doesn't support assigning labels to configs +# so we need to create the configs with the CLI +# bunkerweb.CONFIG_TYPE accepted values are http, stream, server-http, server-stream, default-server-http, modsec and modsec-crs +# bunkerweb.CONFIG_SITE lets you choose on which web service the config should be applied (MULTISITE mode) and if it's not set, the config will be applied for all services +# more info at https://docs.bunkerweb.io + +# remove configs if existing +docker config rm cfg_me_modsec + +# create configs +docker config create -l bunkerweb.CONFIG_TYPE=modsec -l bunkerweb.CONFIG_SITE=www.example.com cfg_me_modsec ./bw-data/configs/modsec/www.example.com/mongo-express.conf \ No newline at end of file diff --git a/examples/mongo-express/swarm.yml b/examples/mongo-express/swarm.yml new file mode 100644 index 000000000..6ccc8aa23 --- /dev/null +++ b/examples/mongo-express/swarm.yml @@ -0,0 +1,48 @@ +version: "3" + +services: + + mongo: + image: mongo:latest + networks: + - bw-services + volumes: + - db:/data/db + environment: + - MONGO_INITDB_ROOT_USERNAME=root # replace with a less obvious username + - MONGO_INITDB_ROOT_PASSWORD=toor # replace with a better password + - MONGO_INITDB_DATABASE=mongo # replace with the database name of your choice + deploy: + placement: + constraints: + - "node.role==worker" + + mongo-ui: + image: mongo-express:latest + networks: + - bw-services + environment: + - ME_CONFIG_MONGODB_SERVER=mongo + - ME_CONFIG_MONGODB_ADMINUSERNAME=root # replace with a less obvious username + - ME_CONFIG_MONGODB_ADMINPASSWORD=toor # replace with a better password + - ME_CONFIG_BASICAUTH_USERNAME=changeme # replace with a better username + - ME_CONFIG_BASICAUTH_PASSWORD=changeme # replace with a better password + restart: unless-stopped + depends_on: + - mongo + deploy: + placement: + constraints: + - "node.role==worker" + labels: + - bunkerweb.USE_REVERSE_PROXY=yes + - bunkerweb.REVERSE_PROXY_URL=/ + - bunkerweb.REVERSE_PROXY_HOST=http://mongo-ui:8081 + +networks: + bw-services: + external: + name: bw-services + +volumes: + db: \ No newline at end of file diff --git a/examples/mongo-express/tests.json b/examples/mongo-express/tests.json new file mode 100644 index 000000000..0886f1c42 --- /dev/null +++ b/examples/mongo-express/tests.json @@ -0,0 +1,16 @@ +{ + "name": "mongo-express", + "kinds": [ + "docker", + "autoconf", + "swarm" + ], + "timeout": 60, + "tests": [ + { + "type": "status", + "url": "https://www.example.com", + "status": 401 + } + ] +} \ No newline at end of file diff --git a/tests/Test.py b/tests/Test.py index df8e07ce0..00884e062 100644 --- a/tests/Test.py +++ b/tests/Test.py @@ -94,14 +94,17 @@ class Test(ABC) : # run a single test def __run_test(self, test) : try : + ex_url = test["url"] + for ex_domain, test_domain in self._domains.items() : + if search(ex_domain, ex_url) : + ex_url = sub(ex_domain, test_domain, ex_url) + break if test["type"] == "string" : - ex_url = test["url"] - for ex_domain, test_domain in self._domains.items() : - if search(ex_domain, ex_url) : - ex_url = sub(ex_domain, test_domain, ex_url) - break r = get(ex_url, timeout=5) return test["string"].casefold() in r.text.casefold() + elif test["type"] == "status" : + r = get(ex_url, timeout=5) + return test["status"] == r.status_code except : #log("TEST", "❌", "exception while running test of type " + test["type"] + " on URL " + ex_url + "\n" + format_exc()) return False