2024-12-30 11:59:07 +01:00
|
|
|
import pathlib
|
2023-02-02 22:02:06 -05:00
|
|
|
import re
|
2024-12-30 11:59:07 +01:00
|
|
|
from typing import List
|
2023-02-02 17:17:00 -05:00
|
|
|
|
|
|
|
import backoff
|
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-12-30 11:59:07 +01:00
|
|
|
def docker_compose_files(compose_file) -> List[str]:
|
|
|
|
data_dir = pathlib.Path(__file__).parent.joinpath("test_fallback.data")
|
|
|
|
return [
|
|
|
|
data_dir.joinpath("compose.base.yml"),
|
|
|
|
data_dir.joinpath(compose_file).as_posix()
|
|
|
|
]
|
2023-02-02 17:17:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def docker_compose_file(data_dir, compose_file):
|
|
|
|
return os.path.join(data_dir, compose_file)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def get(docker_compose, nginxproxy, want_err_re):
|
|
|
|
|
|
|
|
@backoff.on_exception(
|
|
|
|
backoff.constant,
|
2024-10-03 09:07:33 +02:00
|
|
|
requests.exceptions.SSLError,
|
2023-02-02 17:17:00 -05:00
|
|
|
giveup=lambda e: want_err_re and want_err_re.search(str(e)),
|
|
|
|
interval=.3,
|
|
|
|
max_tries=30,
|
|
|
|
jitter=None)
|
|
|
|
def _get(url):
|
|
|
|
return nginxproxy.get(url, allow_redirects=False)
|
|
|
|
|
|
|
|
return _get
|
|
|
|
|
|
|
|
|
2024-10-03 09:07:33 +02:00
|
|
|
INTERNAL_ERR_RE = re.compile("TLSV1_UNRECOGNIZED_NAME")
|
2023-02-02 22:02:06 -05:00
|
|
|
|
|
|
|
|
2023-02-02 17:17:00 -05:00
|
|
|
@pytest.mark.parametrize("compose_file,url,want_code,want_err_re", [
|
|
|
|
# Has default.crt.
|
|
|
|
("withdefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
|
|
|
|
("withdefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
|
|
|
|
("withdefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
|
|
|
|
("withdefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
|
|
|
|
("withdefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
|
|
|
|
("withdefault.yml", "https://http-only.nginx-proxy.test/", 503, None),
|
2024-11-03 21:06:23 +01:00
|
|
|
("withdefault.yml", "http://missing-cert.nginx-proxy.test/", 301, None),
|
2024-11-03 20:10:32 +01:00
|
|
|
("withdefault.yml", "https://missing-cert.nginx-proxy.test/", 200, None),
|
2024-11-04 10:19:09 +01:00
|
|
|
("withdefault.yml", "http://missing-cert.default-untrusted.nginx-proxy.test/", 200, None),
|
|
|
|
("withdefault.yml", "https://missing-cert.default-untrusted.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2023-02-02 17:17:00 -05:00
|
|
|
("withdefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
|
|
|
("withdefault.yml", "https://unknown.nginx-proxy.test/", 503, None),
|
2024-12-01 20:24:53 +01:00
|
|
|
# Same as withdefault.yml, except default.crt is not trusted (TRUST_DEFAULT_CERT=false).
|
|
|
|
("untrusteddefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
|
|
|
|
("untrusteddefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
|
|
|
|
("untrusteddefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
|
|
|
|
("untrusteddefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
|
|
|
|
("untrusteddefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
|
|
|
|
("untrusteddefault.yml", "https://http-only.nginx-proxy.test/", 503, None),
|
|
|
|
("untrusteddefault.yml", "http://missing-cert.nginx-proxy.test/", 200, None),
|
|
|
|
("untrusteddefault.yml", "https://missing-cert.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
|
|
|
("untrusteddefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
|
|
|
("untrusteddefault.yml", "https://unknown.nginx-proxy.test/", 503, None),
|
2023-02-02 22:02:06 -05:00
|
|
|
# Same as withdefault.yml, except there is no default.crt.
|
|
|
|
("nodefault.yml", "http://https-and-http.nginx-proxy.test/", 301, None),
|
|
|
|
("nodefault.yml", "https://https-and-http.nginx-proxy.test/", 200, None),
|
|
|
|
("nodefault.yml", "http://https-only.nginx-proxy.test/", 503, None),
|
|
|
|
("nodefault.yml", "https://https-only.nginx-proxy.test/", 200, None),
|
|
|
|
("nodefault.yml", "http://http-only.nginx-proxy.test/", 200, None),
|
|
|
|
("nodefault.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
|
|
|
("nodefault.yml", "http://missing-cert.nginx-proxy.test/", 200, None),
|
|
|
|
("nodefault.yml", "https://missing-cert.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
|
|
|
("nodefault.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
|
|
|
("nodefault.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2024-11-04 10:19:09 +01:00
|
|
|
# HTTPS_METHOD=nohttp on nginx-proxy, HTTPS_METHOD unset on the app container.
|
2024-05-14 22:30:06 +02:00
|
|
|
("nohttp.yml", "http://https-only.nginx-proxy.test/", 503, None),
|
2023-02-04 18:59:38 -05:00
|
|
|
("nohttp.yml", "https://https-only.nginx-proxy.test/", 200, None),
|
2024-05-14 22:30:06 +02:00
|
|
|
("nohttp.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
2024-11-04 10:19:09 +01:00
|
|
|
("nohttp.yml", "https://unknown.nginx-proxy.test/", 503, None),
|
|
|
|
# HTTPS_METHOD=redirect on nginx-proxy, HTTPS_METHOD=nohttp on the app container.
|
2024-05-14 22:30:06 +02:00
|
|
|
("nohttp-on-app.yml", "http://https-only.nginx-proxy.test/", 503, None),
|
2023-02-04 18:59:38 -05:00
|
|
|
("nohttp-on-app.yml", "https://https-only.nginx-proxy.test/", 200, None),
|
2024-05-14 22:30:06 +02:00
|
|
|
("nohttp-on-app.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
2024-11-04 10:19:09 +01:00
|
|
|
("nohttp-on-app.yml", "https://unknown.nginx-proxy.test/", 503, None),
|
|
|
|
# Same as nohttp.yml, except there are two vhosts with a missing cert, the second
|
|
|
|
# one being configured not to trust the default certificate. This causes its
|
2024-05-14 22:30:06 +02:00
|
|
|
# HTTPS_METHOD=nohttp setting to effectively become HTTPS_METHOD=noredirect.
|
2023-02-04 18:59:38 -05:00
|
|
|
("nohttp-with-missing-cert.yml", "http://https-only.nginx-proxy.test/", 503, None),
|
|
|
|
("nohttp-with-missing-cert.yml", "https://https-only.nginx-proxy.test/", 200, None),
|
2024-11-04 10:19:09 +01:00
|
|
|
("nohttp-with-missing-cert.yml", "http://missing-cert.nginx-proxy.test/", 503, None),
|
|
|
|
("nohttp-with-missing-cert.yml", "https://missing-cert.nginx-proxy.test/", 200, None),
|
|
|
|
("nohttp-with-missing-cert.yml", "http://missing-cert.default-untrusted.nginx-proxy.test/", 200, None),
|
|
|
|
("nohttp-with-missing-cert.yml", "https://missing-cert.default-untrusted.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2023-02-04 18:59:38 -05:00
|
|
|
("nohttp-with-missing-cert.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
2024-11-04 10:19:09 +01:00
|
|
|
("nohttp-with-missing-cert.yml", "https://unknown.nginx-proxy.test/", 503, None),
|
2023-02-04 18:59:38 -05:00
|
|
|
# HTTPS_METHOD=nohttps on nginx-proxy, HTTPS_METHOD unset on the app container.
|
|
|
|
("nohttps.yml", "http://http-only.nginx-proxy.test/", 200, None),
|
2024-06-06 21:44:45 +02:00
|
|
|
("nohttps.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2023-02-04 18:59:38 -05:00
|
|
|
("nohttps.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
2024-06-06 21:44:45 +02:00
|
|
|
("nohttps.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2023-02-04 18:59:38 -05:00
|
|
|
# HTTPS_METHOD=redirect on nginx-proxy, HTTPS_METHOD=nohttps on the app container.
|
|
|
|
("nohttps-on-app.yml", "http://http-only.nginx-proxy.test/", 200, None),
|
2024-06-06 21:44:45 +02:00
|
|
|
("nohttps-on-app.yml", "https://http-only.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2023-02-04 18:59:38 -05:00
|
|
|
("nohttps-on-app.yml", "http://unknown.nginx-proxy.test/", 503, None),
|
2024-06-06 21:44:45 +02:00
|
|
|
("nohttps-on-app.yml", "https://unknown.nginx-proxy.test/", None, INTERNAL_ERR_RE),
|
2023-04-07 18:05:48 -04:00
|
|
|
# Custom nginx config that has a `server` directive that uses `default_server` and simply
|
|
|
|
# returns 418. Nginx should successfully start (in particular, the `default_server` in the
|
|
|
|
# custom config should not conflict with the fallback server generated by nginx-proxy) and nginx
|
|
|
|
# should prefer that server for handling requests for unknown vhosts.
|
|
|
|
("custom-fallback.yml", "http://unknown.nginx-proxy.test/", 418, None),
|
2023-02-02 17:17:00 -05:00
|
|
|
])
|
2024-12-30 11:59:07 +01:00
|
|
|
def test_fallback(get, compose_file, url, want_code, want_err_re):
|
2023-02-02 17:17:00 -05:00
|
|
|
if want_err_re is None:
|
|
|
|
r = get(url)
|
|
|
|
assert r.status_code == want_code
|
|
|
|
else:
|
2024-10-03 09:07:33 +02:00
|
|
|
with pytest.raises(requests.exceptions.SSLError, match=want_err_re):
|
2023-02-02 17:17:00 -05:00
|
|
|
get(url)
|