2022-06-20 12:16:55 +02:00
|
|
|
""" tests ssl validation and CA setting etc """
|
|
|
|
|
|
|
|
from pathlib import Path
|
2022-12-12 04:44:46 +01:00
|
|
|
from ssl import SSLCertVerificationError
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
import aiohttp.client_exceptions
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
from kanidm import KanidmClient
|
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_valid() -> None:
|
|
|
|
"""tests a valid connection"""
|
|
|
|
|
|
|
|
url = "https://badssl.com"
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri=url,
|
|
|
|
)
|
2022-06-20 12:16:55 +02:00
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
|
|
|
print(f"{result.status_code=}")
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_self_signed() -> None:
|
|
|
|
"""tests with a self-signed cert"""
|
|
|
|
|
|
|
|
url = "https://self-signed.badssl.com"
|
|
|
|
|
2022-12-12 04:44:46 +01:00
|
|
|
print("testing self.?signed cert with defaults and expecting an error")
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri=url,
|
|
|
|
)
|
|
|
|
with pytest.raises(aiohttp.client_exceptions.ClientConnectorCertificateError):
|
|
|
|
await client.call_get("/")
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_self_signed_with_verify() -> None:
|
|
|
|
"""tests with a self-signed cert"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://self-signed.badssl.com",
|
|
|
|
verify_certificate=False,
|
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_self_signed_no_verify_certificate() -> None:
|
|
|
|
"""tests with a self-signed cert"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://self-signed.badssl.com",
|
|
|
|
verify_certificate=False,
|
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_wrong_hostname_throws_error() -> None:
|
|
|
|
"""tests with validate hostnames and wrong hostname in the cert"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(uri="https://wrong.host.badssl.com/", verify_hostnames=True)
|
|
|
|
with pytest.raises(
|
|
|
|
aiohttp.client_exceptions.ClientConnectorCertificateError,
|
|
|
|
match="Cannot connect to host wrong.host.badssl.com:443",
|
|
|
|
):
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_wrong_hostname_dont_verify_hostnames() -> None:
|
|
|
|
"""tests with validate hostnames and wrong hostname in the cert"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://wrong.host.badssl.com/",
|
|
|
|
verify_hostnames=False,
|
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_wrong_hostname_verify_certificate() -> None:
|
|
|
|
"""tests with validate hostnames and wrong hostname in the cert"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://wrong.host.badssl.com/",
|
|
|
|
verify_hostnames=False,
|
|
|
|
verify_certificate=False,
|
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_revoked() -> None:
|
2022-10-31 13:23:24 +01:00
|
|
|
"""tests with a revoked certificate"""
|
|
|
|
|
|
|
|
with pytest.raises(aiohttp.ClientConnectorCertificateError):
|
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://revoked.badssl.com/",
|
|
|
|
verify_certificate=True,
|
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://revoked.badssl.com/",
|
2022-10-31 13:23:24 +01:00
|
|
|
verify_certificate=False,
|
2022-09-29 02:08:15 +02:00
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_expired() -> None:
|
|
|
|
"""tests with an expired certificate"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://expired.badssl.com/",
|
|
|
|
)
|
|
|
|
with pytest.raises(
|
|
|
|
aiohttp.client_exceptions.ClientConnectorCertificateError,
|
|
|
|
match="certificate verify failed: certificate has expired",
|
|
|
|
):
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_expired_ignore() -> None:
|
|
|
|
"""tests with an expired certificate"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://expired.badssl.com/",
|
|
|
|
verify_certificate=False,
|
|
|
|
)
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_untrusted_root_throws() -> None:
|
|
|
|
"""tests with an untrusted root, which should throw an error"""
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://untrusted-root.badssl.com/",
|
|
|
|
)
|
|
|
|
with pytest.raises(
|
2022-12-12 04:44:46 +01:00
|
|
|
SSLCertVerificationError,
|
|
|
|
match="certificate verify failed: self.?signed certificate in certificate chain",
|
2022-09-29 02:08:15 +02:00
|
|
|
):
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|
2022-06-20 12:16:55 +02:00
|
|
|
|
|
|
|
|
2022-08-17 01:26:11 +02:00
|
|
|
@pytest.mark.network
|
2022-06-20 12:16:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ssl_untrusted_root_configured() -> None:
|
|
|
|
"""tests with an untrusted root, which should throw an error"""
|
|
|
|
|
|
|
|
testcert = Path("./tests/badssl_trusted_ca.pem").resolve()
|
|
|
|
|
|
|
|
if not testcert.exists():
|
|
|
|
pytest.skip(f"The trusted cert is missing from {testcert}")
|
|
|
|
|
2022-09-29 02:08:15 +02:00
|
|
|
client = KanidmClient(
|
|
|
|
uri="https://untrusted-root.badssl.com/",
|
|
|
|
ca_path=testcert.resolve().as_posix(),
|
|
|
|
)
|
|
|
|
with pytest.raises(
|
|
|
|
aiohttp.client_exceptions.ClientConnectorCertificateError,
|
2022-12-12 04:44:46 +01:00
|
|
|
match="certificate verify failed: self.?signed certificate in certificate chain",
|
2022-09-29 02:08:15 +02:00
|
|
|
):
|
|
|
|
result = await client.call_get("/")
|
|
|
|
assert result.content
|