diff --git a/examples/authelia/autoconf.yml b/examples/authelia/autoconf.yml new file mode 100644 index 000000000..9e0716c85 --- /dev/null +++ b/examples/authelia/autoconf.yml @@ -0,0 +1,81 @@ +version: '3' + +services: + + # APPLICATIONS + app1: + image: node + working_dir: /home/node/app + networks: + bw-services: + aliases: + - app1 + volumes: + - ./js-app:/home/node/app + environment: + - NODE_ENV=production + command: bash -c "npm install express && node index.js" + labels: + - bunkerweb.USE_REVERSE_PROXY=yes + - bunkerweb.REVERSE_PROXY_URL=/ + - bunkerweb.REVERSE_PROXY_HOST=http://app1:3000 + - bunkerweb.REVERSE_PROXY_AUTH_REQUEST=/authelia + - bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SIGNIN_URL=https://auth.example.com/?rd=$$scheme%3A%2F%2F$$host$$request_uri + - bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SET=$$user $$upstream_http_remote_user;$$groups $$upstream_http_remote_groups;$$name $$upstream_http_remote_name;$$email $$upstream_http_remote_email + - bunkerweb.REVERSE_PROXY_HEADERS=Remote-User $$user;Remote-Groups $$groups;Remote-Name $$name;Remote-Email $$email + - bunkerweb.REVERSE_PROXY_URL_999=/authelia + - bunkerweb.REVERSE_PROXY_HOST_999=http://authelia:9091/api/verify + - bunkerweb.REVERSE_PROXY_HEADERS_999=X-Original-URL + + app2: + image: tutum/hello-world + networks: + bw-services: + aliases: + - app2 + labels: + - bunkerweb.SERVER_NAME=app2.example.com + - bunkerweb.USE_REVERSE_PROXY=yes + - bunkerweb.REVERSE_PROXY_URL=/ + - bunkerweb.REVERSE_PROXY_HOST=http://app2 + - bunkerweb.REVERSE_PROXY_AUTH_REQUEST=/authelia + - bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SIGNIN_URL=https://auth.example.com/?rd=$$scheme%3A%2F%2F$$host$$request_uri + - bunkerweb.REVERSE_PROXY_AUTH_REQUEST_SET=$$user $$upstream_http_remote_user;$$groups $$upstream_http_remote_groups;$$name $$upstream_http_remote_name;$$email $$upstream_http_remote_email + - bunkerweb.REVERSE_PROXY_HEADERS=Remote-User $$user;Remote-Groups $$groups;Remote-Name $$name;Remote-Email $$email + - bunkerweb.REVERSE_PROXY_URL_999=/authelia + - bunkerweb.REVERSE_PROXY_HOST_999=http://authelia:9091/api/verify + - bunkerweb.REVERSE_PROXY_HEADERS_999=X-Original-URL + + # AUTHELIA + authelia: + image: authelia/authelia + container_name: authelia + volumes: + - ./authelia:/config + restart: unless-stopped + healthcheck: + disable: true + environment: + - TZ=Europe/Paris + labels: + - bunkerweb.SERVER_NAME=auth.example.com + - bunkerweb.USE_REVERSE_PROXY=yes + - bunkerweb.REVERSE_PROXY_URL=/ + - bunkerweb.REVERSE_PROXY_HOST=http://authelia:9091 + - bunkerweb.REVERSE_PROXY_INTERCEPT_ERRORS=no + + redis: + image: redis:alpine + container_name: redis + volumes: + - ./redis:/data + expose: + - 6379 + restart: unless-stopped + environment: + - TZ=Europe/Paris + +networks: + bw-services: + external: + name: bw-services \ No newline at end of file diff --git a/tests/AutoconfTest.py b/tests/AutoconfTest.py index b3c67f8c3..1db50e687 100644 --- a/tests/AutoconfTest.py +++ b/tests/AutoconfTest.py @@ -5,6 +5,7 @@ from shutil import copytree from traceback import format_exc from subprocess import run from time import sleep +from logger import log class AutoconfTest(Test) : @@ -52,7 +53,7 @@ class AutoconfTest(Test) : if not healthy : raise(Exception("autoconf stack is not healthy")) except : - self._log("exception while running AutoconfTest.init()\n" + format_exc(), error=True) + log("AUTOCONF", "❌", "exception while running AutoconfTest.init()\n" + format_exc()) return False return True @@ -66,7 +67,7 @@ class AutoconfTest(Test) : ret = False rmtree("/tmp/autoconf") except : - self._log("exception while running AutoconfTest.end()\n" + format_exc(), error=True) + log("AUTOCONF", "❌", "exception while running AutoconfTest.end()\n" + format_exc()) return False return ret @@ -74,7 +75,7 @@ class AutoconfTest(Test) : try : super()._setup_test() test = "/tmp/tests/" + self._name - compose = "/tmp/tests/" + self._name + "/docker-compose.yml" + compose = "/tmp/tests/" + self._name + "/service.yml" example_data = "./examples/" + self._name + "/bw-data" self._replace_in_file(compose, r"bunkerity/bunkerweb:.*$", "10.20.1.1:5000/bw-tests:latest") self._replace_in_file(compose, r"\./bw\-data:/", "/tmp/bw-data:/") @@ -91,14 +92,14 @@ class AutoconfTest(Test) : for cp_dir in listdir(example_data) : if isdir(join(example_data, cp_dir)) : copytree(join(example_data, cp_dir), join("/tmp/bw-data", cp_dir)) - proc = run("docker-compose pull", shell=True, cwd=test) + proc = run("docker-compose -f autoconf.yml pull", shell=True, cwd=test) if proc.returncode != 0 : raise(Exception("docker-compose pull failed")) - proc = run("docker-compose up -d", shell=True, cwd=test) + proc = run("docker-compose -f autoconf.yml up -d", shell=True, cwd=test) if proc.returncode != 0 : raise(Exception("docker-compose up failed")) except : - self._log("exception while running AutoconfTest._setup_test()\n" + format_exc(), error=True) + log("AUTOCONF", "❌", "exception while running AutoconfTest._setup_test()\n" + format_exc()) self._cleanup_test() return False self._cleanup_test() @@ -107,12 +108,12 @@ class AutoconfTest(Test) : def _cleanup_test(self) : try : test = "/tmp/tests/" + self._name - proc = run("docker-compose down -v", shell=True, cwd=test) + proc = run("docker-compose -f autoconf.yml down -v", shell=True, cwd=test) if proc.returncode != 0 : raise(Exception("docker-compose down failed")) super()._cleanup_test() except : - self._log("exception while running AutoconfTest._setup_test()\n" + format_exc(), error=True) + log("AUTOCONF", "❌", "exception while running AutoconfTest._cleanup_test()\n" + format_exc()) return False return True \ No newline at end of file diff --git a/tests/DockerTest.py b/tests/DockerTest.py index d7a3d9e92..ed57e9357 100644 --- a/tests/DockerTest.py +++ b/tests/DockerTest.py @@ -4,6 +4,7 @@ from os import chown, walk, getenv, listdir from shutil import copytree from traceback import format_exc from subprocess import run +from logger import log class DockerTest(Test) : @@ -21,11 +22,11 @@ class DockerTest(Test) : try : if not Test.init() : return False - for root, dirs, files in walk("/tmp/bw-data") : - for name in dirs + files : - chown(join(root, name), 101, 101) + proc = run("sudo chown -R 101:101 /tmp/bw-data", shell=True) + if proc.returncode != 0 : + raise(Exception("chown failed (autoconf stack)")) except : - self._log("exception while running DockerTest.init()\n" + format_exc(), error=True) + log("DOCKER", "❌", "exception while running DockerTest.init()\n" + format_exc()) return False return True @@ -57,7 +58,7 @@ class DockerTest(Test) : if proc.returncode != 0 : raise(Exception("docker-compose up failed")) except : - self._log("exception while running DockerTest._setup_test()\n" + format_exc(), error=True) + log("DOCKER", "❌", "exception while running DockerTest._setup_test()\n" + format_exc()) self._cleanup_test() return False self._cleanup_test() @@ -71,7 +72,7 @@ class DockerTest(Test) : raise(Exception("docker-compose down failed")) super()._cleanup_test() except : - self._log("exception while running DockerTest._setup_test()\n" + format_exc(), error=True) + log("DOCKER", "❌", "exception while running DockerTest._cleanup_test()\n" + format_exc()) return False return True \ No newline at end of file diff --git a/tests/Test.py b/tests/Test.py index 711d77d93..75dbac116 100644 --- a/tests/Test.py +++ b/tests/Test.py @@ -9,6 +9,7 @@ from os import mkdir, makedirs, walk, chmod from re import sub, search, MULTILINE from datetime import datetime from subprocess import run +from logger import log class Test(ABC) : @@ -17,15 +18,7 @@ class Test(ABC) : self.__kind = kind self.__timeout = timeout self.__tests = tests - self._log("instiantiated with " + str(len(tests)) + " tests and timeout of " + str(timeout) + "s") - - def _log(self, msg, error=False) : - when = datetime.today().strftime("[%Y-%m-%d %H:%M:%S]") - what = self._name + " - " + self.__kind + " - " + msg - if error : - print(when + " " + what, flush=True, file=stderr) - else : - print(when + " " + what, flush=True) + log("TEST", "ℹ️", "instiantiated with " + str(len(tests)) + " tests and timeout of " + str(timeout) + "s") # Class method # called once before running all the different tests for a given integration @@ -41,7 +34,7 @@ class Test(ABC) : if not isdir("/tmp/tests") : mkdir("/tmp/tests") except : - print("exception while running Test.init()\n" + format_exc(), flush=True, file=stderr) + log("TEST", "❌", "exception while running Test.init()\n" + format_exc()) return False return True @@ -62,7 +55,7 @@ class Test(ABC) : run("sudo rm -rf /tmp/tests/" + self._name, shell=True) copytree("./examples/" + self._name, "/tmp/tests/" + self._name) except : - self._log("exception while running Test._setup_test()\n" + format_exc(), error=True) + log("TEST", "❌", "exception while running Test._setup_test()\n" + format_exc()) return False return True @@ -71,7 +64,7 @@ class Test(ABC) : try : run("sudo rm -rf /tmp/tests/" + self._name, shell=True) except : - self._log("exception while running Test._cleanup_test()\n" + format_exc(), error=True) + log("TEST", "❌", "exception while running Test._cleanup_test()\n" + format_exc()) return False return True @@ -89,11 +82,11 @@ class Test(ABC) : break if all_ok : elapsed = str(int(time() - start)) - self._log("success (" + elapsed + "/" + str(self.__timeout) + "s)") + log("TEST", "ℹ️", "success (" + elapsed + "/" + str(self.__timeout) + "s)") return self._cleanup_test() - self._log("tests not ok, retrying in 1s ...", error=True) + log("TEST", "⚠️", "tests not ok, retrying in 1s ...") sleep(1) - self._log("failed (timeout = " + str(self.__timeout) + "s)", error=True) + log("TEST", "❌", "failed (timeout = " + str(self.__timeout) + "s)") return False # run a single test @@ -107,7 +100,7 @@ class Test(ABC) : r = get(ex_url, timeout=5) return test["string"].casefold() in r.text.casefold() except : - self._log("exception while running test of type " + test["type"] + " on URL " + test["url"] + "\n" + format_exc(), error=True) + log("TEST", "❌", "exception while running test of type " + test["type"] + " on URL " + test["url"] + "\n" + format_exc()) return False raise(Exception("unknow test type " + test["type"]))