1
0
mirror of https://github.com/nginx-proxy/nginx-proxy synced 2024-11-08 07:49:22 +01:00

feat: print warning on unparsable VIRTUAL_HOST_MULTIPORTS

This commit is contained in:
Nicolas Duchon 2024-05-03 22:21:21 +02:00
parent 0baff189bc
commit 53e9a03ac9
No known key found for this signature in database
GPG Key ID: EA3151C66A4D79E7
3 changed files with 75 additions and 1 deletions

@ -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) }}

@ -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

@ -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