MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / BaseTestSSLConnection

Class BaseTestSSLConnection

tests/test_connect.py:2024–2196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2022
2023
2024class BaseTestSSLConnection(tb.ConnectedTestCase, tb.ProxiedClusterTestCase):
2025 @classmethod
2026 def get_connection_spec(cls, kwargs={}):
2027 # Keep administrative connections and unproxied SSL tests on the
2028 # cluster. Proxy tests explicitly supply the shared proxy's port.
2029 return tb.ClusterTestCase.get_connection_spec.__func__(cls, kwargs)
2030
2031 @classmethod
2032 def get_server_settings(cls):
2033 conf = super().get_server_settings()
2034 conf.update({
2035 'ssl': 'on',
2036 'ssl_cert_file': SSL_CERT_FILE,
2037 'ssl_key_file': SSL_KEY_FILE,
2038 'ssl_ca_file': CLIENT_CA_CERT_FILE,
2039 })
2040 if cls.cluster.get_pg_version() >= (12, 0):
2041 conf['ssl_min_protocol_version'] = 'TLSv1.2'
2042 conf['ssl_max_protocol_version'] = 'TLSv1.2'
2043
2044 return conf
2045
2046 @classmethod
2047 def setup_cluster(cls):
2048 cls.cluster = cls.new_cluster(pg_cluster.TempCluster)
2049 cls.start_cluster(
2050 cls.cluster, server_settings=cls.get_server_settings())
2051
2052 def setUp(self):
2053 super().setUp()
2054
2055 self.cluster.reset_hba()
2056
2057 create_script = []
2058 create_script.append('CREATE ROLE ssl_user WITH LOGIN;')
2059 create_script.append('GRANT ALL ON SCHEMA public TO ssl_user;')
2060
2061 self._add_hba_entry()
2062
2063 # Put hba changes into effect
2064 self.cluster.reload()
2065
2066 create_script = '\n'.join(create_script)
2067 self.loop.run_until_complete(self.con.execute(create_script))
2068
2069 def tearDown(self):
2070 # Reset cluster's pg_hba.conf since we've meddled with it
2071 self.cluster.trust_local_connections()
2072
2073 drop_script = []
2074 drop_script.append('REVOKE ALL ON SCHEMA public FROM ssl_user;')
2075 drop_script.append('DROP ROLE ssl_user;')
2076 drop_script = '\n'.join(drop_script)
2077 self.loop.run_until_complete(self.con.execute(drop_script))
2078
2079 super().tearDown()
2080
2081 def _add_hba_entry(self):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected