42 lines
1.9 KiB
Python
42 lines
1.9 KiB
Python
"""Лаба 7: проверка недоверенного TLS публичной точки входа (рубрика «4»).
|
||
|
||
Системное хранилище CA вместо CA стенда и имя хоста не из SAN
|
||
(`127.0.0.1` вместо `localhost`) — именно ошибка проверки сертификата, а не
|
||
отказ соединения: с CA стенда и `localhost` тот же адрес отвечает 200.
|
||
`make test-security` skeleton проверяет первое, но не второе.
|
||
"""
|
||
|
||
import ssl
|
||
import urllib.parse
|
||
|
||
import httpx
|
||
import pytest
|
||
|
||
from conftest import BASE_URL
|
||
from helpers import TLS_CLIENT_CERT, tls_context
|
||
|
||
HEALTH = "/health/live"
|
||
|
||
|
||
def _verify_error(url: str, context: ssl.SSLContext) -> str:
|
||
with pytest.raises(httpx.ConnectError) as caught:
|
||
httpx.get(url, verify=context, timeout=5)
|
||
cause: BaseException | None = caught.value
|
||
while cause is not None and not isinstance(cause, ssl.SSLCertVerificationError):
|
||
cause = cause.__cause__ or cause.__context__
|
||
assert isinstance(cause, ssl.SSLCertVerificationError), repr(caught.value)
|
||
return cause.verify_message
|
||
|
||
|
||
def test_untrusted_tls() -> None:
|
||
parts = urllib.parse.urlsplit(BASE_URL)
|
||
if parts.hostname != "localhost" or TLS_CLIENT_CERT:
|
||
pytest.skip(f"BASE_URL {BASE_URL} is not the public LB on localhost")
|
||
unknown_issuer = _verify_error(BASE_URL + HEALTH, ssl.create_default_context())
|
||
assert "issuer" in unknown_issuer or "self-signed" in unknown_issuer, unknown_issuer
|
||
by_ip = parts._replace(netloc=f"127.0.0.1:{parts.port}").geturl()
|
||
mismatch = _verify_error(by_ip + HEALTH, tls_context())
|
||
assert "mismatch" in mismatch.lower(), mismatch
|
||
# с CA стенда и localhost — работает: отказы выше не от недоступности
|
||
assert httpx.get(BASE_URL + HEALTH, verify=tls_context()).status_code == 200
|