|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from hazelcast.asyncio.client import HazelcastClient |
| 8 | +from hazelcast.config import SSLProtocol |
| 9 | +from hazelcast.errors import IllegalStateError |
| 10 | +from tests.integration.asyncio.base import HazelcastTestCase |
| 11 | +from tests.util import compare_client_version, get_abs_path |
| 12 | + |
| 13 | +current_directory = os.path.abspath( |
| 14 | + os.path.join( |
| 15 | + os.path.dirname(__file__), "../../../backward_compatible/ssl_tests/hostname_verification" |
| 16 | + ) |
| 17 | +) |
| 18 | + |
| 19 | +MEMBER_CONFIG = """ |
| 20 | +<hazelcast xmlns="http://www.hazelcast.com/schema/config" |
| 21 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 22 | + xsi:schemaLocation="http://www.hazelcast.com/schema/config |
| 23 | + http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd"> |
| 24 | + <network> |
| 25 | + <ssl enabled="true"> |
| 26 | + <factory-class-name> |
| 27 | + com.hazelcast.nio.ssl.BasicSSLContextFactory |
| 28 | + </factory-class-name> |
| 29 | + <properties> |
| 30 | + <property name="keyStore">%s</property> |
| 31 | + <property name="keyStorePassword">123456</property> |
| 32 | + <property name="keyStoreType">PKCS12</property> |
| 33 | + <property name="protocol">TLSv1.2</property> |
| 34 | + </properties> |
| 35 | + </ssl> |
| 36 | + </network> |
| 37 | +</hazelcast> |
| 38 | +""" |
| 39 | + |
| 40 | + |
| 41 | +@unittest.skipIf( |
| 42 | + sys.version_info < (3, 7), |
| 43 | + "Hostname verification feature requires Python 3.7+", |
| 44 | +) |
| 45 | +@unittest.skipIf( |
| 46 | + compare_client_version("5.1") < 0, |
| 47 | + "Tests the features added in 5.1 version of the client", |
| 48 | +) |
| 49 | +@pytest.mark.enterprise |
| 50 | +class SslHostnameVerificationTest(unittest.IsolatedAsyncioTestCase, HazelcastTestCase): |
| 51 | + def setUp(self): |
| 52 | + self.rc = self.create_rc() |
| 53 | + self.cluster = None |
| 54 | + |
| 55 | + async def asyncTearDown(self): |
| 56 | + await self.shutdown_all_clients() |
| 57 | + self.rc.terminateCluster(self.cluster.id) |
| 58 | + self.rc.exit() |
| 59 | + |
| 60 | + async def test_hostname_verification_with_loopback_san(self): |
| 61 | + # SAN entry is present with different possible values |
| 62 | + file_name = "tls-host-loopback-san" |
| 63 | + self.start_member_with(f"{file_name}.p12") |
| 64 | + await self.start_client_with(f"{file_name}.pem", "127.0.0.1:5701") |
| 65 | + await self.start_client_with(f"{file_name}.pem", "localhost:5701") |
| 66 | + |
| 67 | + async def test_hostname_verification_with_loopback_dns_san(self): |
| 68 | + # SAN entry is present, but only with `dns:localhost` |
| 69 | + file_name = "tls-host-loopback-san-dns" |
| 70 | + self.start_member_with(f"{file_name}.p12") |
| 71 | + await self.start_client_with(f"{file_name}.pem", "localhost:5701") |
| 72 | + with self.assertRaisesRegex(IllegalStateError, "Unable to connect to any cluster"): |
| 73 | + await self.start_client_with(f"{file_name}.pem", "127.0.0.1:5701") |
| 74 | + |
| 75 | + async def test_hostname_verification_with_different_san(self): |
| 76 | + # There is a valid entry, but it does not match with the address of the member. |
| 77 | + file_name = "tls-host-not-our-san" |
| 78 | + self.start_member_with(f"{file_name}.p12") |
| 79 | + with self.assertRaisesRegex(IllegalStateError, "Unable to connect to any cluster"): |
| 80 | + await self.start_client_with(f"{file_name}.pem", "localhost:5701") |
| 81 | + with self.assertRaisesRegex(IllegalStateError, "Unable to connect to any cluster"): |
| 82 | + await self.start_client_with(f"{file_name}.pem", "127.0.0.1:5701") |
| 83 | + |
| 84 | + async def test_hostname_verification_with_loopback_cn(self): |
| 85 | + # No entry in SAN but an entry in CN which checked as a fallback |
| 86 | + # when no entry in SAN is present. |
| 87 | + file_name = "tls-host-loopback-cn" |
| 88 | + self.start_member_with(f"{file_name}.p12") |
| 89 | + await self.start_client_with(f"{file_name}.pem", "localhost:5701") |
| 90 | + # See https://stackoverflow.com/a/8444863/12394291. IP addresses in CN |
| 91 | + # are not supported. So, we don't have a test for it. |
| 92 | + with self.assertRaisesRegex(IllegalStateError, "Unable to connect to any cluster"): |
| 93 | + await self.start_client_with(f"{file_name}.pem", "127.0.0.1:5701") |
| 94 | + |
| 95 | + async def test_hostname_verification_with_no_entry(self): |
| 96 | + # No entry either in the SAN or CN. No way to verify hostname. |
| 97 | + file_name = "tls-host-no-entry" |
| 98 | + self.start_member_with(f"{file_name}.p12") |
| 99 | + with self.assertRaisesRegex(IllegalStateError, "Unable to connect to any cluster"): |
| 100 | + await self.start_client_with(f"{file_name}.pem", "localhost:5701") |
| 101 | + with self.assertRaisesRegex(IllegalStateError, "Unable to connect to any cluster"): |
| 102 | + await self.start_client_with(f"{file_name}.pem", "127.0.0.1:5701") |
| 103 | + |
| 104 | + async def test_hostname_verification_disabled(self): |
| 105 | + # When hostname verification is disabled, the scenarious that |
| 106 | + # would fail in `test_hostname_verification_with_no_entry` will |
| 107 | + # no longer fail, showing that it is working as expected. |
| 108 | + file_name = "tls-host-no-entry" |
| 109 | + self.start_member_with(f"{file_name}.p12") |
| 110 | + await self.start_client_with(f"{file_name}.pem", "localhost:5701", check_hostname=False) |
| 111 | + await self.start_client_with(f"{file_name}.pem", "127.0.0.1:5701", check_hostname=False) |
| 112 | + |
| 113 | + async def start_client_with( |
| 114 | + self, |
| 115 | + truststore_name: str, |
| 116 | + member_address: str, |
| 117 | + *, |
| 118 | + check_hostname=True, |
| 119 | + ) -> HazelcastClient: |
| 120 | + return await self.create_client( |
| 121 | + { |
| 122 | + "cluster_name": self.cluster.id, |
| 123 | + "cluster_members": [member_address], |
| 124 | + "ssl_enabled": True, |
| 125 | + "ssl_protocol": SSLProtocol.TLSv1_2, |
| 126 | + "ssl_cafile": get_abs_path(current_directory, truststore_name), |
| 127 | + "ssl_check_hostname": check_hostname, |
| 128 | + "cluster_connect_timeout": 0, |
| 129 | + } |
| 130 | + ) |
| 131 | + |
| 132 | + def start_member_with(self, keystore_name: str) -> None: |
| 133 | + config = MEMBER_CONFIG % get_abs_path(current_directory, keystore_name) |
| 134 | + self.cluster = self.create_cluster(self.rc, config) |
| 135 | + self.cluster.start_member() |
0 commit comments