From e6d58420986d859a2dd276a38586a17844a3ffb8 Mon Sep 17 00:00:00 2001 From: Joao M Date: Thu, 18 Aug 2022 04:00:52 +0000 Subject: [PATCH] Change statistics --- README.md | 20 ++++++------ templates/haproxy.cfg.j2 | 14 ++++---- tests/expected/services-letsencrypt.txt | 3 -- tests/expected/services-multiple-hosts.txt | 3 -- tests/expected/ssl-loose.txt | 12 +++++++ tests/expected/static.txt | 3 -- tests/test_parser.py | 37 ++++++++++++++++++---- 7 files changed, 59 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index e53fdbc..f77d57c 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,16 @@ The mapping to `/var/run/docker.sock` is necessary to discover the docker contai The environment variables will setup the HAProxy. -| Environment Variable | Description | -|-------------------------------|-------------------------------------------------------------------------------| -| EASYHAPROXY_DISCOVER | How `haproxy.cfg` will be created: `static`, `docker` or `swarm` | -| EASYHAPROXY_LABEL_PREFIX | (Optional) The key will search for matching resources. Default: `easyhaproxy`. | -| EASYHAPROXY_LETSENCRYPT_EMAIL | (Optional) The email will be used to request the certificate to Letsencrypt | +| Environment Variable | Description | +|-------------------------------|---------------------------------------------------------------------------------------------------------------| +| EASYHAPROXY_DISCOVER | How `haproxy.cfg` will be created: `static`, `docker` or `swarm` | +| EASYHAPROXY_LABEL_PREFIX | (Optional) The key will search for matching resources. Default: `easyhaproxy`. | +| EASYHAPROXY_LETSENCRYPT_EMAIL | (Optional) The email will be used to request the certificate to Letsencrypt | | EASYHAPROXY_SSL_MODE | (Optional) `STRICT` supports only the most recent TLS version; `DEFAULT` good SSL integration with recent browsers; `LOOSE` supports all old SSL protocols for old browsers (not recommended). | -| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. Default: `admin` | -| HAPROXY_PASSWORD | The HAProxy password to the statistics. If not set, it will disable stats. | -| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. Default: `1936` | -| HAPROXY_CUSTOMERRORS | (Optional) If HAProxy will use custom HTML errors. true/false. Default: false | +| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. Default: `admin` | +| HAPROXY_PASSWORD | (Optional) The HAProxy password to the statistics. If not set, statistics will be available with no password | +| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. Default: `1936`. If set to `false`, disable statistics | +| HAPROXY_CUSTOMERRORS | (Optional) If HAProxy will use custom HTML errors. true/false. Default: `false` | The environment variable `EASYHAPROXY_DISCOVER` will define where is located your containers (see below for more details): @@ -258,7 +258,7 @@ Caveats: You must expose some ports on the EasyHAProxy container and in the firewall. However, you don't need to expose the other container ports because EasyHAProxy will handle that. - The ports `80` and `443`. -- If you enable the HAProxy statistics, you must also expose the port defined in `HAPROXY_STATS_PORT` environment variable. +- If you enable the HAProxy statistics, you must also expose the port defined in `HAPROXY_STATS_PORT` environment variable (default 1936). Be aware that statististics are enabled by default with no password. - Every port defined in `easyhaproxy.[definitions].port` also should be exposed. e.g. diff --git a/templates/haproxy.cfg.j2 b/templates/haproxy.cfg.j2 index c47c127..6e183de 100644 --- a/templates/haproxy.cfg.j2 +++ b/templates/haproxy.cfg.j2 @@ -26,23 +26,23 @@ defaults errorfile 504 /etc/haproxy/errors-custom/504.http {% endif %} -{% if "stats" in data %} +{% set data_stats = data["stats"] | default({}) %} +{% if data_stats["port"] | default(1936) | int > 0 %} frontend stats - bind *:{{ data["stats"]["port"] | default(1936) }} + bind *:{{ data_stats["port"] | default(1936) }} mode http stats enable stats hide-version stats realm Haproxy\ Statistics stats uri / - stats auth {{ data["stats"]["username"] }}:{{ data["stats"]["password"] }} -# acl is_proxystats hdr(host) -i some.host.com -# default_backend srv_stats -# use_backend srv_stats if is_proxystats + {% if data_stats["password"] | default("") != "" %} + stats auth {{ data_stats["username"] | default("admin") }}:{{ data_stats["password"] }} + {% endif %} default_backend srv_stats backend srv_stats mode http - server Local 127.0.0.1:{{ data["stats"]["port"] | default(1936) }} + server Local 127.0.0.1:{{ data_stats["port"] | default(1936) }} {% endif %} {% for o in data["easymapping"] -%} {% set mode = o["mode"] or "http" %} diff --git a/tests/expected/services-letsencrypt.txt b/tests/expected/services-letsencrypt.txt index 7dc6abc..809b8a6 100644 --- a/tests/expected/services-letsencrypt.txt +++ b/tests/expected/services-letsencrypt.txt @@ -36,9 +36,6 @@ frontend stats stats realm Haproxy\ Statistics stats uri / stats auth admin:password -# acl is_proxystats hdr(host) -i some.host.com -# default_backend srv_stats -# use_backend srv_stats if is_proxystats default_backend srv_stats backend srv_stats diff --git a/tests/expected/services-multiple-hosts.txt b/tests/expected/services-multiple-hosts.txt index 4459421..5da3bed 100644 --- a/tests/expected/services-multiple-hosts.txt +++ b/tests/expected/services-multiple-hosts.txt @@ -36,9 +36,6 @@ frontend stats stats realm Haproxy\ Statistics stats uri / stats auth joe:s3cr3t -# acl is_proxystats hdr(host) -i some.host.com -# default_backend srv_stats -# use_backend srv_stats if is_proxystats default_backend srv_stats backend srv_stats diff --git a/tests/expected/ssl-loose.txt b/tests/expected/ssl-loose.txt index 6236f95..b03571e 100644 --- a/tests/expected/ssl-loose.txt +++ b/tests/expected/ssl-loose.txt @@ -19,6 +19,18 @@ defaults timeout client 10s timeout server 10m +frontend stats + bind *:1936 + mode http + stats enable + stats hide-version + stats realm Haproxy\ Statistics + stats uri / + default_backend srv_stats + +backend srv_stats + mode http + server Local 127.0.0.1:1936 backend letsencrypt_backend mode http diff --git a/tests/expected/static.txt b/tests/expected/static.txt index ad76627..1656236 100644 --- a/tests/expected/static.txt +++ b/tests/expected/static.txt @@ -36,9 +36,6 @@ frontend stats stats realm Haproxy\ Statistics stats uri / stats auth admin:test123 -# acl is_proxystats hdr(host) -i some.host.com -# default_backend srv_stats -# use_backend srv_stats if is_proxystats default_backend srv_stats backend srv_stats diff --git a/tests/test_parser.py b/tests/test_parser.py index 83028ba..81b4dfc 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -19,7 +19,10 @@ def test_parser_doesnt_crash(): line_list = load_fixture("no-services") result = { - "customerrors": False + "customerrors": False, + "stats": { + "port": "false" + } } cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) @@ -38,6 +41,9 @@ def test_parser_finds_services(): "customerrors": False, "letsencrypt": { "email": LETSENCRYPT_EMAIL + }, + "stats": { + "port": 0 } } @@ -65,6 +71,9 @@ def test_parser_finds_services_changed_label(): "lookup_label": "haproxy", "letsencrypt": { "email": LETSENCRYPT_EMAIL + }, + "stats": { + "port": 0 } } @@ -91,6 +100,9 @@ def test_parser_finds_services_raw(): "customerrors": False, "letsencrypt": { "email": LETSENCRYPT_EMAIL + }, + "stats": { + "port": 0 } } @@ -277,7 +289,10 @@ def test_parser_tcp(): line_list = load_fixture("services-tcp") result = { - "customerrors": False + "customerrors": False, + "stats": { + "port": 0 + } } cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) @@ -294,7 +309,10 @@ def test_parser_multi_containers(): line_list = load_fixture("services-multi-containers") result = { - "customerrors": False + "customerrors": False, + "stats": { + "port": 0 + } } cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) @@ -334,7 +352,10 @@ def test_parser_redirect_ssl(): result = { "customerrors": False, - "ssl_mode": "loose" + "ssl_mode": "loose", + "stats": { + "port": 0 + } } cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) @@ -352,7 +373,10 @@ def test_parser_ssl_strict(): result = { "customerrors": False, - "ssl_mode": "strict" + "ssl_mode": "strict", + "stats": { + "port": False + } } cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) @@ -369,7 +393,7 @@ def test_parser_ssl_loose(): result = { "customerrors": False, - "ssl_mode": "loose" + "ssl_mode": "loose", } cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) @@ -387,7 +411,6 @@ def test_parser_ssl_letsencrypt(): result = { "customerrors": True, "stats": { - "username": "admin", "password": "password" }, "letsencrypt": {