|
1 | 1 | import inspect |
| 2 | +import ssl |
| 3 | +from pathlib import Path |
2 | 4 |
|
3 | 5 | import pytest |
4 | 6 | from pytest_httpx import HTTPXMock |
5 | 7 |
|
6 | | -from infrahub_sdk import InfrahubClient, InfrahubClientSync |
| 8 | +from infrahub_sdk import Config, InfrahubClient, InfrahubClientSync |
7 | 9 | from infrahub_sdk.exceptions import NodeNotFoundError |
8 | 10 | from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync |
9 | 11 | from tests.unit.sdk.conftest import BothClients |
|
28 | 30 |
|
29 | 31 | client_types = ["standard", "sync"] |
30 | 32 |
|
| 33 | +CURRENT_DIRECTORY = Path(__file__).parent |
| 34 | + |
| 35 | + |
| 36 | +async def test_verify_config_caches_default_ssl_context(monkeypatch) -> None: |
| 37 | + contexts: list[tuple[str | None, object]] = [] |
| 38 | + |
| 39 | + def fake_create_default_context(*args: object, **kwargs: object) -> object: |
| 40 | + context = object() |
| 41 | + contexts.append((kwargs.get("cafile"), context)) |
| 42 | + return context |
| 43 | + |
| 44 | + monkeypatch.setattr("ssl.create_default_context", fake_create_default_context) |
| 45 | + |
| 46 | + client = InfrahubClient(config=Config(address="http://mock")) |
| 47 | + |
| 48 | + first = client.config.tls_context |
| 49 | + second = client.config.tls_context |
| 50 | + |
| 51 | + assert first is second |
| 52 | + assert contexts == [(None, first)] |
| 53 | + |
| 54 | + |
| 55 | +async def test_verify_config_caches_tls_ca_file_context(monkeypatch) -> None: |
| 56 | + contexts: list[tuple[str | None, object]] = [] |
| 57 | + |
| 58 | + def fake_create_default_context(*args: object, **kwargs: object) -> object: |
| 59 | + context = object() |
| 60 | + contexts.append((kwargs.get("cafile"), context)) |
| 61 | + return context |
| 62 | + |
| 63 | + monkeypatch.setattr("ssl.create_default_context", fake_create_default_context) |
| 64 | + |
| 65 | + client = InfrahubClient( |
| 66 | + config=Config(address="http://mock", tls_ca_file=str(CURRENT_DIRECTORY / "test_data/path-1.pem")) |
| 67 | + ) |
| 68 | + |
| 69 | + first = client.config.tls_context |
| 70 | + second = client.config.tls_context |
| 71 | + |
| 72 | + assert first is second |
| 73 | + assert contexts == [(str(CURRENT_DIRECTORY / "test_data/path-1.pem"), first)] |
| 74 | + |
| 75 | + client.config.tls_ca_file = str(CURRENT_DIRECTORY / "test_data/path-2.pem") |
| 76 | + third = client.config.tls_context |
| 77 | + |
| 78 | + assert third is first |
| 79 | + assert contexts == [ |
| 80 | + (str(CURRENT_DIRECTORY / "test_data/path-1.pem"), first), |
| 81 | + ] |
| 82 | + |
| 83 | + |
| 84 | +async def test_verify_config_respects_tls_insecure(monkeypatch) -> None: |
| 85 | + def fake_create_default_context(*args: object, **kwargs: object) -> object: |
| 86 | + raise AssertionError("create_default_context should not be called when TLS is insecure") |
| 87 | + |
| 88 | + monkeypatch.setattr("ssl.create_default_context", fake_create_default_context) |
| 89 | + |
| 90 | + client = InfrahubClient(config=Config(address="http://mock", tls_insecure=True)) |
| 91 | + |
| 92 | + verify_value = client.config.tls_context |
| 93 | + |
| 94 | + assert verify_value.check_hostname is False |
| 95 | + assert verify_value.verify_mode == ssl.CERT_NONE |
| 96 | + |
| 97 | + |
| 98 | +async def test_verify_config_uses_custom_tls_context(monkeypatch) -> None: |
| 99 | + def fake_create_default_context(*args: object, **kwargs: object) -> object: |
| 100 | + raise AssertionError("create_default_context should not be called when custom context is provided") |
| 101 | + |
| 102 | + monkeypatch.setattr("ssl.create_default_context", fake_create_default_context) |
| 103 | + |
| 104 | + config = Config(address="http://mock") |
| 105 | + custom_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT) |
| 106 | + config.set_ssl_context(custom_context) |
| 107 | + |
| 108 | + client = InfrahubClient(config=config) |
| 109 | + |
| 110 | + clone_client = client.clone() |
| 111 | + |
| 112 | + assert client.config.tls_context is custom_context |
| 113 | + assert clone_client.config.tls_context is custom_context |
| 114 | + |
31 | 115 |
|
32 | 116 | async def test_method_sanity() -> None: |
33 | 117 | """Validate that there is at least one public method and that both clients look the same.""" |
|
0 commit comments