From 79819aeacb9a609273cff0db7000df4da7d61040 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Date: Wed, 9 Nov 2022 18:27:22 -0300 Subject: [PATCH] issue #32 --- docs/docker-environment.md | 29 +++++++++++++++-------------- src/easymapping/__init__.py | 2 +- src/functions/__init__.py | 9 ++++++--- src/main.py | 2 +- src/processor/__init__.py | 3 ++- src/tests/test_containerenv.py | 16 ++++++++++++++++ 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/docs/docker-environment.md b/docs/docker-environment.md index 1ed6288..fd41020 100644 --- a/docs/docker-environment.md +++ b/docs/docker-environment.md @@ -1,19 +1,20 @@ # Docker environment variables -| Environment Variable | Description | Default | -|-------------------------------|-------------------------------------------------------------------------------------------------|------------------| -| EASYHAPROXY_DISCOVER | How the services will be discovered to create `haproxy.cfg`: `static`, `docker`, `swarm` or `kubernetes` | **required** | -| EASYHAPROXY_LABEL_PREFIX | (Optional) The key will search for matching resources. | `easyhaproxy` | -| EASYHAPROXY_LETSENCRYPT_EMAIL | (Optional) The email will be used to request the certificate to Letsencrypt | *empty* | -| 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). | `default`| -| EASYHAPROXY_REFRESH_CONF | (Optional) Check configuration every N seconds. | 10 | -| EASYHAPROXY_LOG_LEVEL | (Optional) The log level for EasyHAproxy messages. Available: TRACE,DEBUG,INFO,WARN,ERROR,FATAL | DEBUG | -| CERTBOT_LOG_LEVEL | (Optional) The log level for Certbot messages. Available: TRACE,DEBUG,INFO,WARN,ERROR,FATAL | DEBUG | -| HAPROXY_LOG_LEVEL | (Optional) The log level for HAProxy messages. Available: TRACE,DEBUG,INFO,WARN,ERROR,FATAL | DEBUG | -| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. | `admin` | -| HAPROXY_PASSWORD | (Optional) The HAProxy password to the statistics. If not set, statistics will be available with no password | *empty* | -| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. If set to `false`, disable statistics | `1936` | -| HAPROXY_CUSTOMERRORS | (Optional) If HAProxy will use custom HTML errors. true/false. | `false` | +| Environment Variable | Description | Default | +|---------------------------------|-------------------------------------------------------------------------------------------------|------------------| +| EASYHAPROXY_DISCOVER | How the services will be discovered to create `haproxy.cfg`: `static`, `docker`, `swarm` or `kubernetes` | **required** | +| EASYHAPROXY_LABEL_PREFIX | (Optional) The key will search for matching resources. | `easyhaproxy` | +| EASYHAPROXY_LETSENCRYPT_EMAIL | (Optional) The email will be used to request the certificate to Letsencrypt | *empty* | +| EASYHAPROXY_LETSENCRYPT_STAGING | (Optional) If true, will try to connect to the Letsencrypt test server | false | +| 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). | `default`| +| EASYHAPROXY_REFRESH_CONF | (Optional) Check configuration every N seconds. | 10 | +| EASYHAPROXY_LOG_LEVEL | (Optional) The log level for EasyHAproxy messages. Available: TRACE,DEBUG,INFO,WARN,ERROR,FATAL | DEBUG | +| CERTBOT_LOG_LEVEL | (Optional) The log level for Certbot messages. Available: TRACE,DEBUG,INFO,WARN,ERROR,FATAL | DEBUG | +| HAPROXY_LOG_LEVEL | (Optional) The log level for HAProxy messages. Available: TRACE,DEBUG,INFO,WARN,ERROR,FATAL | DEBUG | +| HAPROXY_USERNAME | (Optional) The HAProxy username to the statistics. | `admin` | +| HAPROXY_PASSWORD | (Optional) The HAProxy password to the statistics. If not set, statistics will be available with no password | *empty* | +| HAPROXY_STATS_PORT | (Optional) The HAProxy port to the statistics. If set to `false`, disable statistics | `1936` | +| HAPROXY_CUSTOMERRORS | (Optional) If HAProxy will use custom HTML errors. true/false. | `false` | diff --git a/src/easymapping/__init__.py b/src/easymapping/__init__.py index 6ad8af7..c2626a6 100644 --- a/src/easymapping/__init__.py +++ b/src/easymapping/__init__.py @@ -49,7 +49,7 @@ class HaproxyConfigGenerator: def __init__(self, mapping): self.mapping = mapping self.mapping.setdefault("ssl_mode", 'default') - self.mapping.setdefault("letsencrypt", {"email": ""}) + self.mapping.setdefault("letsencrypt", {"email": "", "staging": False}) self.mapping["ssl_mode"] = self.mapping["ssl_mode"].lower() self.label = DockerLabelHandler(mapping['lookup_label'] if 'lookup_label' in mapping else "easyhaproxy") self.letsencrypt_hosts = [] diff --git a/src/functions/__init__.py b/src/functions/__init__.py index 244b454..3438086 100644 --- a/src/functions/__init__.py +++ b/src/functions/__init__.py @@ -174,9 +174,10 @@ class DaemonizeHAProxy: class Certbot: - def __init__(self, certs, email): + def __init__(self, certs, email, staging): self.certs = certs self.email = email + self.staging = staging def check_certificates(self, hosts): if self.email == "" or len(hosts) == 0: @@ -201,7 +202,7 @@ class Certbot: Functions.log(Functions.CERTBOT_LOG, Functions.DEBUG, "Renew certificate for %s" % (host)) renew_certs.append(host_arg) - certbot_certonly = ('/usr/bin/certbot certonly ' + certbot_certonly = ('/usr/bin/certbot certonly {staging}' ' --standalone' ' --preferred-challenges http' ' --http-01-port 2080' @@ -210,7 +211,9 @@ class Certbot: ' --no-eff-email' ' --non-interactive' ' --max-log-backups=0' - ' %s --email %s' % (' '.join(request_certs), self.email) + ' {certs} --email {email}'.format(certs = ' '.join(request_certs), + email = self.email, + staging = '--staging' if self.staging else '') ) ret_reload = False diff --git a/src/main.py b/src/main.py index da5f413..b9017a5 100644 --- a/src/main.py +++ b/src/main.py @@ -22,7 +22,7 @@ def start(): haproxy.haproxy("start") haproxy.sleep() - certbot = Certbot(Consts.certs_letsencrypt, os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL")) + certbot = Certbot(Consts.certs_letsencrypt, os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL"), os.getenv("EASYHAPROXY_LETSENCRYPT_STAGING", "false").lower() in ["true", "1", "yes"]) while True: if old_haproxy is not None: diff --git a/src/processor/__init__.py b/src/processor/__init__.py index 3c17c6c..6fe66e8 100644 --- a/src/processor/__init__.py +++ b/src/processor/__init__.py @@ -27,7 +27,8 @@ class ContainerEnv: env_vars["lookup_label"] = os.getenv("EASYHAPROXY_LABEL_PREFIX") if os.getenv("EASYHAPROXY_LABEL_PREFIX") else "easyhaproxy" if (os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL")): env_vars["letsencrypt"] = { - "email": os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL") + "email": os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL"), + "staging": os.getenv("EASYHAPROXY_LETSENCRYPT_STAGING", "false").lower() in ["true", "1", "yes"] } return env_vars diff --git a/src/tests/test_containerenv.py b/src/tests/test_containerenv.py index 5e64b0f..afc5be0 100644 --- a/src/tests/test_containerenv.py +++ b/src/tests/test_containerenv.py @@ -95,8 +95,24 @@ def test_container_env_stats_password(): "lookup_label": "easyhaproxy", "letsencrypt": { "email": "acme@example.org", + "staging": False } } == ContainerEnv.read() finally: os.environ['EASYHAPROXY_LETSENCRYPT_EMAIL'] = '' +def test_container_env_letsencrypt(): + os.environ['EASYHAPROXY_LETSENCRYPT_EMAIL'] = 'acme@example.org' + os.environ['EASYHAPROXY_LETSENCRYPT_STAGING'] = 'true' + try: + assert { + "customerrors": False, + "ssl_mode": "default", + "lookup_label": "easyhaproxy", + "letsencrypt": { + "email": "acme@example.org", + "staging": True + } + } == ContainerEnv.read() + finally: + os.environ['EASYHAPROXY_LETSENCRYPT_EMAIL'] = '' \ No newline at end of file