diff --git a/README.md b/README.md index c8d0119..3797ad6 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Important: easyhaproxy needs to be in the same network of the containers or othe | Tag | Description | Example | |---------------------------------------|---------------------------------------------------------------------------------------------------------|--------------| -| easyhaproxy.[definition].host | Host HAProxy is listening | somehost.com | +| easyhaproxy.[definition].host | Host(s) HAProxy is listening. More than one host use comma as delimiter | somehost.com OR host1.com,host2.com | | easyhaproxy.[definition].mode | (Optional) Is this `http` or `tcp` mode in HAProxy. (Defaults to http) | http | | easyhaproxy.[definition].port | (Optional) Port HAProxy will listen for the host. (Defaults to 80) | 80 | | easyhaproxy.[definition].localport | (Optional) Port container is listening. (Defaults to 80) | 8080 | diff --git a/assets/scripts/swarm.py b/assets/scripts/swarm.py index b746146..52fd829 100644 --- a/assets/scripts/swarm.py +++ b/assets/scripts/swarm.py @@ -2,7 +2,7 @@ import os from easymapping import HaproxyConfigGenerator with open("/tmp/.docker_data", 'r') as content_file: - lineList = content_file.readlines() + line_list = content_file.readlines() result = { "customerrors": True if os.getenv("HAPROXY_CUSTOMERRORS") == "true" else False @@ -18,7 +18,7 @@ if os.getenv("HAPROXY_PASSWORD"): result["lookup_label"] = os.getenv("EASYHAPROXY_LABEL_PREFIX") if os.getenv("EASYHAPROXY_LABEL_PREFIX") else "easyhaproxy" cfg = HaproxyConfigGenerator(result) -print(cfg.generate(lineList)) +print(cfg.generate(line_list)) path = os.path.dirname(os.path.realpath(__file__)) with open(path + "/letsencrypt_hosts.txt", 'w') as fp: diff --git a/easymapping/__init__.py b/easymapping/__init__.py index 2b8856c..2c5777c 100644 --- a/easymapping/__init__.py +++ b/easymapping/__init__.py @@ -114,7 +114,6 @@ class HaproxyConfigGenerator: self.label.create([definition, "letsencrypt"]), False ) and self.letsencrypt_email != "" - self.letsencrypt_hosts.append(d[host_label]) if letsencrypt and d[host_label] not in self.letsencrypt_hosts else self.letsencrypt_hosts if port not in easymapping: easymapping[port] = { @@ -136,41 +135,42 @@ class HaproxyConfigGenerator: "" ) - easymapping[port]["hosts"].setdefault(d[host_label], {}) - easymapping[port]["hosts"][d[host_label]].setdefault("containers", []) - easymapping[port]["hosts"][d[host_label]].setdefault("letsencrypt", False) - easymapping[port]["hosts"][d[host_label]]["containers"] += ["{}:{}".format(container, ct_port)] - easymapping[port]["hosts"][d[host_label]]["letsencrypt"] = letsencrypt - - if letsencrypt: - if "443" not in easymapping: - easymapping["443"] = { - "mode": "http", - "health-check": "ssl", - "port": "443", - "hosts": dict(), - "redirect": dict(), - } - easymapping["443"]["hosts"][d[host_label]] = dict(easymapping[port]["hosts"][d[host_label]]) - easymapping["443"]["hosts"][d[host_label]]["letsencrypt"] = False - easymapping["443"]["ssl_cert"] = "/etc/haproxy/certs" - - - # handle SSL - ssl_label = self.label.create([definition, "sslcert"]) - if self.label.has_label(ssl_label): - filename = "{}/{}.pem".format( - self.ssl_cert_folder, d[host_label] + for hostname in d[host_label].split(","): + hostname = hostname.strip() + easymapping[port]["hosts"].setdefault(hostname, {}) + easymapping[port]["hosts"][hostname].setdefault("containers", []) + easymapping[port]["hosts"][hostname].setdefault("letsencrypt", False) + easymapping[port]["hosts"][hostname]["containers"] += ["{}:{}".format(container, ct_port)] + easymapping[port]["hosts"][hostname]["letsencrypt"] = letsencrypt + easymapping[port]["redirect"] = self.label.get_json( + self.label.create([definition, "redirect"]) ) - easymapping[port]["ssl_cert"] = filename - with open(filename, 'wb') as file: - file.write( - base64.b64decode(d[ssl_label]) - ) - # handle redirects - easymapping[port]["redirect"] = self.label.get_json( - self.label.create([definition, "redirect"]) - ) + if letsencrypt: + if "443" not in easymapping: + easymapping["443"] = { + "mode": "http", + "health-check": "ssl", + "port": "443", + "hosts": dict(), + "redirect": dict(), + } + easymapping["443"]["hosts"][hostname] = dict(easymapping[port]["hosts"][hostname]) + easymapping["443"]["hosts"][hostname]["letsencrypt"] = False + easymapping["443"]["ssl_cert"] = "/etc/haproxy/certs" + self.letsencrypt_hosts.append(hostname) if hostname not in self.letsencrypt_hosts else self.letsencrypt_hosts + + + # handle SSL + ssl_label = self.label.create([definition, "sslcert"]) + if self.label.has_label(ssl_label): + filename = "{}/{}.pem".format( + self.ssl_cert_folder, d[host_label] + ) + easymapping[port]["ssl_cert"] = filename + with open(filename, 'wb') as file: + file.write( + base64.b64decode(d[ssl_label]) + ) return easymapping.values() diff --git a/tests/expected/services-multiple-hosts.txt b/tests/expected/services-multiple-hosts.txt new file mode 100644 index 0000000..4459421 --- /dev/null +++ b/tests/expected/services-multiple-hosts.txt @@ -0,0 +1,80 @@ +global + log stdout format raw local0 info + maxconn 2000 + tune.ssl.default-dh-param 2048 + + # intermediate configuration + ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 + ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256 + ssl-default-bind-options prefer-client-ciphers no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets + + ssl-default-server-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 + ssl-default-server-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256 + ssl-default-server-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets + + ssl-dh-param-file /etc/haproxy/dhparam + +defaults + log global + + timeout connect 3s + timeout client 10s + timeout server 10m + errorfile 400 /etc/haproxy/errors-custom/400.http + errorfile 403 /etc/haproxy/errors-custom/403.http + errorfile 408 /etc/haproxy/errors-custom/408.http + errorfile 500 /etc/haproxy/errors-custom/500.http + errorfile 502 /etc/haproxy/errors-custom/502.http + errorfile 503 /etc/haproxy/errors-custom/503.http + errorfile 504 /etc/haproxy/errors-custom/504.http + +frontend stats + bind *:1937 + mode http + stats enable + stats hide-version + 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 + mode http + server Local 127.0.0.1:1937 + +frontend http_in_19901 + bind *:19901 + mode http + redirect prefix www.google.com code 301 if { hdr(host) -i google.helloworld.com } + + acl is_rule_hello_com_19901_1 hdr(host) -i hello.com + acl is_rule_hello_com_19901_2 hdr(host) -i hello.com:19901 + use_backend srv_hello_com_19901 if is_rule_hello_com_19901_1 OR is_rule_hello_com_19901_2 + + acl is_rule_www_helloworld_com_19901_1 hdr(host) -i www.helloworld.com + acl is_rule_www_helloworld_com_19901_2 hdr(host) -i www.helloworld.com:19901 + use_backend srv_www_helloworld_com_19901 if is_rule_www_helloworld_com_19901_1 OR is_rule_www_helloworld_com_19901_2 + +backend srv_hello_com_19901 + balance roundrobin + mode http + option forwardfor + http-request set-header X-Forwarded-Port %[dst_port] + http-request add-header X-Forwarded-Proto https if { ssl_fc } + server srv-0 3e63154954b0:80 check weight 1 + server srv-1 eb294c110eb1:80 check weight 1 +backend srv_www_helloworld_com_19901 + balance roundrobin + mode http + option forwardfor + http-request set-header X-Forwarded-Port %[dst_port] + http-request add-header X-Forwarded-Proto https if { ssl_fc } + server srv-0 3e63154954b0:80 check weight 1 + server srv-1 eb294c110eb1:80 check weight 1 + +backend letsencrypt_backend + mode http + server certbot 127.0.0.1:2080 \ No newline at end of file diff --git a/tests/fixtures/services-multiple-hosts b/tests/fixtures/services-multiple-hosts new file mode 100644 index 0000000..44bfc19 --- /dev/null +++ b/tests/fixtures/services-multiple-hosts @@ -0,0 +1,3 @@ +db79d3a910f4={"com.docker.compose.config-hash":"5bde40f52451521ad201e70de1291397376a0498a7c955624a609da3b60e7e8e","com.docker.compose.container-number":"1","com.docker.compose.depends_on":"","com.docker.compose.image":"sha256:ea39067705590557dd0cd951664a10970ceefcb725a3c1f43690d6d6d4ed5fce","com.docker.compose.oneoff":"False","com.docker.compose.project":"docker","com.docker.compose.project.config_files":"/workspace/docker-easy-haproxy/examples/docker/docker-compose-multi-containers.yml","com.docker.compose.project.working_dir":"/workspace/docker-easy-haproxy/examples/docker","com.docker.compose.service":"haproxy","com.docker.compose.version":"2.8.0"} +3e63154954b0={"com.docker.compose.config-hash":"4e0cbdd8372c6779863799e5021ed8178f74b55bd8e070abcdffaf87eb7baa36","com.docker.compose.container-number":"1","com.docker.compose.depends_on":"","com.docker.compose.image":"sha256:bea3509d6fdc8d7f9ec95563a5a226dc977ee74fb3e980e0de70e892c2d38dde","com.docker.compose.oneoff":"False","com.docker.compose.project":"docker","com.docker.compose.project.config_files":"/workspace/docker-easy-haproxy/examples/docker/docker-compose-multi-containers.yml","com.docker.compose.project.working_dir":"/workspace/docker-easy-haproxy/examples/docker","com.docker.compose.service":"nginx","com.docker.compose.version":"2.8.0","easyhaproxy.http.host":"hello.com, www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","easyhaproxy.http.redirect":"{\"google.helloworld.com\": \"www.google.com\"}"} +eb294c110eb1={"com.docker.compose.config-hash":"4e0cbdd8372c6779863799e5021ed8178f74b55bd8e070abcdffaf87eb7baa36","com.docker.compose.container-number":"2","com.docker.compose.depends_on":"","com.docker.compose.image":"sha256:bea3509d6fdc8d7f9ec95563a5a226dc977ee74fb3e980e0de70e892c2d38dde","com.docker.compose.oneoff":"False","com.docker.compose.project":"docker","com.docker.compose.project.config_files":"/workspace/docker-easy-haproxy/examples/docker/docker-compose-multi-containers.yml","com.docker.compose.project.working_dir":"/workspace/docker-easy-haproxy/examples/docker","com.docker.compose.service":"nginx","com.docker.compose.version":"2.8.0","easyhaproxy.http.host":"hello.com, www.helloworld.com","easyhaproxy.http.localport":"80","easyhaproxy.http.port":"19901","easyhaproxy.http.redirect":"{\"google.helloworld.com\": \"www.google.com\"}"} \ No newline at end of file diff --git a/tests/test_parser.py b/tests/test_parser.py index ccb9b05..efbb359 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -295,5 +295,28 @@ def test_parser_multi_containers(): assert [] == cfg.letsencrypt_hosts +def test_parser_multiple_hosts(): + line_list = load_fixture("services-multiple-hosts") + + result = { + "customerrors": True, + "stats": { + "username": "joe", + "password": "s3cr3t", + "port": "1937" + } + } + + cfg = easymapping.HaproxyConfigGenerator(result, CERTS_FOLDER) + haproxy_config = cfg.generate(line_list) + + assert len(haproxy_config) > 0 + path = os.path.dirname(os.path.realpath(__file__)) + with open(path + "/expected/services-multiple-hosts.txt", 'r') as expected_file: + assert expected_file.read() == haproxy_config + assert [] == cfg.letsencrypt_hosts + + #test_parser_finds_services_raw() #test_parser_tcp() +#test_parser_multiple_hosts()