Kaynağa Gözat

feat: print warning on unparsable VIRTUAL_HOST_MULTIPORTS

Nicolas Duchon 1 yıl önce
ebeveyn
işleme
53e9a03ac9

+ 13 - 1
nginx.tmpl

@@ -459,7 +459,19 @@ proxy_set_header Proxy "";
 
 {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST_MULTIPORTS. */}}
 {{- range $vhosts_yaml, $containers := groupBy $globals.containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
-    {{- range $hostname, $vhost := (fromYaml $vhosts_yaml) }}
+    {{- /* Print a warning in the config if VIRTUAL_HOST_MULTIPORTS can't be parsed. */}}
+    {{- $parsedVhosts := fromYaml $vhosts_yaml }}
+    {{- if (empty $parsedVhosts) }}
+        {{- $containerNames := list }}
+        {{- range $container := $containers }}
+            {{- $containerNames = append $containerNames $container.Name }}
+        {{- end }}
+# /!\ WARNING: the VIRTUAL_HOST_MULTIPORTS environment variable used for {{ len $containerNames | plural "this container" "those containers" }} is not a valid YAML string:
+# {{ $containerNames | join ", " }}
+        {{- continue }}
+    {{- end }}
+
+    {{- range $hostname, $vhost := $parsedVhosts }}
         {{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
         {{- $paths := coalesce $vhost_data.paths (dict) }}
         

+ 18 - 0
test/test_multiports/test_multiports-invalid-syntax.py

@@ -0,0 +1,18 @@
+import pytest
+import re
+
+
+def test_virtual_hosts_with_syntax_error_should_not_be_reachable(docker_compose, nginxproxy):
+    r = nginxproxy.get("http://test1.nginx-proxy.tld")
+    assert r.status_code == 503
+    r = nginxproxy.get("http://test2.nginx-proxy.tld")
+    assert r.status_code == 503
+
+
+def test_config_should_have_multiports_warning_comments(docker_compose, nginxproxy):
+    conf = nginxproxy.get_conf().decode('ASCII')
+    matches = re.findall(r"the VIRTUAL_HOST_MULTIPORTS environment variable used for this container is not a valid YAML string", conf)
+    assert len(matches) == 3
+    assert "# invalidsyntax" in conf
+    assert "# hostnamerepeat" in conf
+    assert "# pathrepeat" in conf

+ 44 - 0
test/test_multiports/test_multiports-invalid-syntax.yml

@@ -0,0 +1,44 @@
+version: "2"
+
+services:
+  invalidsyntax:
+    image: web
+    container_name: invalidsyntax
+    expose:
+      - "80"
+    environment:
+      WEB_PORTS: "80"
+      VIRTUAL_HOST_MULTIPORTS: |-
+        test1.nginx-proxy.tld
+        test2.nginx-proxy.tld:
+
+  hostnamerepeat:
+    image: web
+    container_name: hostnamerepeat
+    expose:
+      - "80"
+    environment:
+      WEB_PORTS: "80"
+      VIRTUAL_HOST_MULTIPORTS: |-
+        test1.nginx-proxy.tld:
+        test1.nginx-proxy.tld:
+
+  pathrepeat:
+    image: web
+    container_name: pathrepeat
+    expose:
+      - "8080"
+      - "9000"
+    environment:
+      WEB_PORTS: "8080 9000"
+      VIRTUAL_HOST_MULTIPORTS: |-
+        test1.nginx-proxy.tld:
+          "/":
+            port: 8080
+          "/":
+            port: 9000
+
+  sut:
+    image: nginxproxy/nginx-proxy:test
+    volumes:
+      - /var/run/docker.sock:/tmp/docker.sock:ro