diff --git a/src/easymapping/__init__.py b/src/easymapping/__init__.py index 79f3700..2c1d232 100644 --- a/src/easymapping/__init__.py +++ b/src/easymapping/__init__.py @@ -1,5 +1,6 @@ import base64 import json +import os import re from jinja2 import Environment, FileSystemLoader @@ -91,9 +92,10 @@ class HaproxyConfigGenerator: ) # Get enabled plugins from config - enabled_list = self.mapping.get("plugins", {}).get("enabled") + enabled_list = self.mapping.get("plugins", {}).get("enabled", []) + # If enabled list contains only empty string, treat as no plugins enabled if enabled_list and len(enabled_list) > 0 and enabled_list[0] == "": - enabled_list = None + enabled_list = [] global_results = self.plugin_manager.execute_global_plugins(global_context, enabled_list) self.global_plugin_configs = [r.haproxy_config for r in global_results if r.haproxy_config] @@ -101,7 +103,8 @@ class HaproxyConfigGenerator: import logging logging.warning(f"Failed to execute global plugins: {e}") - file_loader = FileSystemLoader('templates') + templates_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'templates') + file_loader = FileSystemLoader(templates_dir) env = Environment(loader=file_loader) env.trim_blocks = True env.lstrip_blocks = True @@ -207,7 +210,7 @@ class HaproxyConfigGenerator: ) # Check if plugins are enabled for this domain (from labels) - enabled_plugins = None + enabled_plugins = [] if self.label.has_label(self.label.create([definition, "plugins"])): enabled_plugins = self.label.get( self.label.create([definition, "plugins"]), diff --git a/src/tests/expected/no-services.txt b/src/tests/expected/no-services.txt index 7624237..a898508 100644 --- a/src/tests/expected/no-services.txt +++ b/src/tests/expected/no-services.txt @@ -23,6 +23,7 @@ defaults timeout server 10m + backend certbot_backend mode http server certbot 127.0.0.1:2080 diff --git a/src/tests/expected/services-letsencrypt.txt b/src/tests/expected/services-letsencrypt.txt index d95732d..ffb28c6 100644 --- a/src/tests/expected/services-letsencrypt.txt +++ b/src/tests/expected/services-letsencrypt.txt @@ -29,6 +29,7 @@ defaults errorfile 503 /etc/haproxy/errors-custom/503.http errorfile 504 /etc/haproxy/errors-custom/504.http + frontend stats bind *:1936 mode http diff --git a/src/tests/expected/services-multi-containers.txt b/src/tests/expected/services-multi-containers.txt index 3b1644d..0cfedd8 100644 --- a/src/tests/expected/services-multi-containers.txt +++ b/src/tests/expected/services-multi-containers.txt @@ -23,6 +23,7 @@ defaults timeout server 10m + frontend http_in_19901 bind *:19901 mode http diff --git a/src/tests/expected/services-multiple-hosts.txt b/src/tests/expected/services-multiple-hosts.txt index 66810d5..0b3a64d 100644 --- a/src/tests/expected/services-multiple-hosts.txt +++ b/src/tests/expected/services-multiple-hosts.txt @@ -29,6 +29,7 @@ defaults errorfile 503 /etc/haproxy/errors-custom/503.http errorfile 504 /etc/haproxy/errors-custom/504.http + frontend stats bind *:1937 mode http diff --git a/src/tests/expected/services-redirect-ssl.txt b/src/tests/expected/services-redirect-ssl.txt index ad841cc..8c51afc 100644 --- a/src/tests/expected/services-redirect-ssl.txt +++ b/src/tests/expected/services-redirect-ssl.txt @@ -21,6 +21,7 @@ defaults timeout server 10m + frontend http_in_80 bind *:80 mode http diff --git a/src/tests/expected/services-tcp.txt b/src/tests/expected/services-tcp.txt index 2c19128..f9a41bd 100644 --- a/src/tests/expected/services-tcp.txt +++ b/src/tests/expected/services-tcp.txt @@ -23,6 +23,7 @@ defaults timeout server 10m + frontend tcp_in_31339 bind *:31339 mode tcp diff --git a/src/tests/expected/services.txt b/src/tests/expected/services.txt index 82f8a6c..2c876f9 100644 --- a/src/tests/expected/services.txt +++ b/src/tests/expected/services.txt @@ -23,6 +23,7 @@ defaults timeout server 10m + frontend tcp_in_31339 bind *:31339 mode tcp diff --git a/src/tests/expected/ssl-loose.txt b/src/tests/expected/ssl-loose.txt index 239e566..e9cb61f 100644 --- a/src/tests/expected/ssl-loose.txt +++ b/src/tests/expected/ssl-loose.txt @@ -20,6 +20,7 @@ defaults timeout client 10s timeout server 10m + frontend stats bind *:1936 mode http diff --git a/src/tests/expected/ssl-strict.txt b/src/tests/expected/ssl-strict.txt index 7c3306f..0f26d3c 100644 --- a/src/tests/expected/ssl-strict.txt +++ b/src/tests/expected/ssl-strict.txt @@ -18,6 +18,7 @@ defaults timeout server 10m + backend certbot_backend mode http server certbot 127.0.0.1:2080 diff --git a/src/tests/expected/static.txt b/src/tests/expected/static.txt index 1d0d9a2..2002c62 100644 --- a/src/tests/expected/static.txt +++ b/src/tests/expected/static.txt @@ -29,6 +29,7 @@ defaults errorfile 503 /etc/haproxy/errors-custom/503.http errorfile 504 /etc/haproxy/errors-custom/504.http + frontend stats bind *:1936 mode http diff --git a/src/tests/test_containerenv.py b/src/tests/test_containerenv.py index 27d672e..f5837d7 100644 --- a/src/tests/test_containerenv.py +++ b/src/tests/test_containerenv.py @@ -20,7 +20,12 @@ def test_container_env_empty(): "server": False, "retry_count": 60, "preferred_challenges": "http", - "manual_auth_hook": False} + "manual_auth_hook": False}, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] + } } == ContainerEnv.read() # os.environ['CERTBOT_LOG_LEVEL'] = 'warn' @@ -45,7 +50,12 @@ def test_container_env_customerrors(): "server": False, "retry_count": 60, "preferred_challenges": "http", - "manual_auth_hook": False} + "manual_auth_hook": False}, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] + } } == ContainerEnv.read() finally: del os.environ['HAPROXY_CUSTOMERRORS'] @@ -70,7 +80,12 @@ def test_container_env_sslmode(): "server": False, "retry_count": 60, "preferred_challenges": "http", - "manual_auth_hook": False} + "manual_auth_hook": False}, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] + } } == ContainerEnv.read() finally: del os.environ['EASYHAPROXY_SSL_MODE'] @@ -96,7 +111,12 @@ def test_container_env_stats(): "server": False, "retry_count": 60, "preferred_challenges": "http", - "manual_auth_hook": False} + "manual_auth_hook": False}, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] + } } == ContainerEnv.read() finally: del os.environ['HAPROXY_USERNAME'] @@ -128,7 +148,12 @@ def test_container_env_stats_password(): "server": False, "retry_count": 60, "preferred_challenges": "http", - "manual_auth_hook": False} + "manual_auth_hook": False}, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] + } } == ContainerEnv.read() finally: del os.environ['HAPROXY_PASSWORD'] @@ -160,7 +185,12 @@ def test_container_env_stats_password_2(): "server": False, "retry_count": 60, "preferred_challenges": "http", - "manual_auth_hook": False} + "manual_auth_hook": False}, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] + } } == ContainerEnv.read() finally: del os.environ['HAPROXY_USERNAME'] @@ -189,6 +219,11 @@ def test_container_env_certbot_email(): "retry_count": 60, "preferred_challenges": "http", "manual_auth_hook": False + }, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] } } == ContainerEnv.read() finally: @@ -222,6 +257,11 @@ def test_container_env_certbot_full(): 'retry_count': 10, "preferred_challenges": "dns", "manual_auth_hook": "something_manual_auth_hook" + }, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] } } == ContainerEnv.read() finally: @@ -257,6 +297,11 @@ def test_container_log_level(): "retry_count": 60, "preferred_challenges": "http", "manual_auth_hook": False + }, + "plugins": { + "abort_on_error": False, + "config": {}, + "enabled": [] } } == ContainerEnv.read() finally: diff --git a/src/tests/test_parser.py b/src/tests/test_parser.py index 3f6f1fa..1aa0f4b 100644 --- a/src/tests/test_parser.py +++ b/src/tests/test_parser.py @@ -124,7 +124,8 @@ def test_parser_finds_services_raw(): "my-stack_agent:9001" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] } }, "redirect": { @@ -142,7 +143,8 @@ def test_parser_finds_services_raw(): "my-stack_cadvisor:8080" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] }, "node-exporter.quantum.example.org":{ "balance": "roundrobin", @@ -150,7 +152,8 @@ def test_parser_finds_services_raw(): "my-stack_node-exporter:9100" ], "certbot": True, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] } }, "redirect": { @@ -168,7 +171,8 @@ def test_parser_finds_services_raw(): "my-stack_node-exporter:9100" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] }, "www.somehost.com.br":{ "balance": "roundrobin", @@ -176,7 +180,8 @@ def test_parser_finds_services_raw(): "some-service:80" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] } }, "redirect": { @@ -199,7 +204,8 @@ def test_parser_finds_services_raw(): "some-service:80" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] } }, "redirect": { @@ -465,7 +471,8 @@ def test_parser_finds_services_clone_to_ssl_raw(): "10.152.183.215:8080" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] }, "valida.me":{ "balance":"roundrobin", @@ -473,7 +480,8 @@ def test_parser_finds_services_clone_to_ssl_raw(): "10.152.183.62:8080" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] }, "www.valida.me":{ "balance":"roundrobin", @@ -481,7 +489,8 @@ def test_parser_finds_services_clone_to_ssl_raw(): "10.152.183.62:8080" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] } }, "mode": "http", @@ -499,7 +508,8 @@ def test_parser_finds_services_clone_to_ssl_raw(): "10.152.183.215:8080" ], "certbot": False, - "redirect_ssl": False + "redirect_ssl": False, + "plugin_configs": [] } }, "mode": "http",