Skip to content

Commit 4fc6f55

Browse files
authored
Use root ca explicitly in http-based adapters (#262)
* Use root ca explicitly in http-based adapters * Check cert's file encoding * Add typing * fix imports
1 parent b1e6bdb commit 4fc6f55

File tree

26 files changed

+99
-27
lines changed

26 files changed

+99
-27
lines changed

lib/dl_api_commons/dl_api_commons/aiohttp/aiohttp_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __attrs_post_init__(self): # type: ignore # 2024-01-24 # TODO: Function is
113113
self._session = self._make_session()
114114

115115
def _make_session(self) -> aiohttp.ClientSession:
116-
ssl_context = ssl.create_default_context(cadata=self._ca_data.decode("utf-8"))
116+
ssl_context = ssl.create_default_context(cadata=self._ca_data.decode("ascii"))
117117
return aiohttp.ClientSession(
118118
cookies=self.cookies,
119119
headers=self.headers,

lib/dl_api_lib_testing/dl_api_lib_testing/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def control_api_app_settings(
134134
rqe_config_subprocess=rqe_config_subprocess,
135135
)
136136

137-
@pytest.fixture(scope="function")
137+
@pytest.fixture(scope="session")
138138
def ca_data(self) -> bytes:
139139
return get_root_certificates()
140140

lib/dl_configs/dl_configs/utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
from typing import (
34
Callable,
@@ -12,6 +13,9 @@
1213
TEMP_ROOT_CERTIFICATES_FOLDER_PATH = "/tmp/ssl/certs/"
1314

1415

16+
LOGGER = logging.getLogger(__name__)
17+
18+
1519
_T = TypeVar("_T")
1620

1721

@@ -25,8 +29,21 @@ def validator(value: _T) -> _T:
2529

2630

2731
def get_root_certificates(path: str = DEFAULT_ROOT_CERTIFICATES_FILENAME) -> bytes:
32+
"""
33+
expects a path to a file with PEM certificates
34+
35+
aiohttp-based clients expect certificates as an ascii string to create ssl.sslContext
36+
while grpc-clients expect them as a byte representation of an ascii string to create the special grpc ssl context
37+
"""
2838
with open(path, "rb") as fobj:
29-
return fobj.read()
39+
ca_data = fobj.read()
40+
# fail fast
41+
try:
42+
ca_data.decode("ascii")
43+
except UnicodeDecodeError:
44+
LOGGER.exception("Looks like the certificates are not in PEM format")
45+
raise
46+
return ca_data
3047

3148

3249
def get_root_certificates_path() -> str:

lib/dl_connector_bitrix_gds/dl_connector_bitrix_gds/core/connection_executors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ async def _make_target_conn_dto_pool(self) -> Sequence[BitrixGDSConnTargetDTO]:
5959
connect_timeout=self._conn_options.connect_timeout, # type: ignore # TODO: fix
6060
redis_conn_params=conn_params,
6161
redis_caches_ttl=caches_ttl,
62+
ca_data=self._ca_data.decode("ascii"),
6263
)
6364
]

lib/dl_connector_bitrix_gds/dl_connector_bitrix_gds/core/target_dto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import attr
44

5-
from dl_core.connection_executors.models.connection_target_dto_base import ConnTargetDTO
5+
from dl_core.connection_executors.models.connection_target_dto_base import BaseAiohttpConnTargetDTO
66
from dl_core.utils import secrepr
77

88

@@ -15,7 +15,7 @@ def hide_pass(value: Optional[dict]) -> str:
1515

1616

1717
@attr.s(frozen=True)
18-
class BitrixGDSConnTargetDTO(ConnTargetDTO):
18+
class BitrixGDSConnTargetDTO(BaseAiohttpConnTargetDTO):
1919
portal: str = attr.ib(kw_only=True)
2020
token: str = attr.ib(kw_only=True, repr=secrepr)
2121

lib/dl_connector_bundle_chs3/dl_connector_bundle_chs3/chs3_base/core/connection_executors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async def _make_target_conn_dto_pool(self) -> list[BaseFileS3ConnTargetDTO]: #
3434
access_key_id=self._conn_dto.access_key_id,
3535
secret_access_key=self._conn_dto.secret_access_key,
3636
replace_secret=self._conn_dto.replace_secret,
37+
ca_data=self._ca_data.decode("ascii"),
3738
)
3839
)
3940
return dto_pool

lib/dl_connector_bundle_chs3/dl_connector_bundle_chs3/chs3_base/core/target_dto.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import attr
44

5-
from dl_core.connection_executors.models.connection_target_dto_base import BaseSQLConnTargetDTO
5+
from dl_core.connection_executors.models.connection_target_dto_base import (
6+
BaseAiohttpConnTargetDTO,
7+
BaseSQLConnTargetDTO,
8+
)
69
from dl_core.utils import secrepr
710

811

912
@attr.s
10-
class BaseFileS3ConnTargetDTO(BaseSQLConnTargetDTO):
13+
class BaseFileS3ConnTargetDTO(BaseAiohttpConnTargetDTO, BaseSQLConnTargetDTO):
1114
protocol: str = attr.ib(kw_only=True)
1215
disable_value_processing: bool = attr.ib(kw_only=True)
1316

lib/dl_connector_bundle_chs3/dl_connector_bundle_chs3_tests/db/base/core/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,29 @@ def task_processor_factory(self) -> TaskProcessorFactory:
107107
@pytest.fixture(scope="session")
108108
def conn_sync_service_registry(
109109
self,
110+
root_certificates: bytes,
110111
conn_bi_context: RequestContextInfo,
111112
task_processor_factory: TaskProcessorFactory,
112113
) -> ServicesRegistry:
113114
return self.service_registry_factory(
114115
conn_exec_factory_async_env=False,
115116
conn_bi_context=conn_bi_context,
116117
task_processor_factory=task_processor_factory,
118+
root_certificates_data=root_certificates,
117119
)
118120

119121
@pytest.fixture(scope="session")
120122
def conn_async_service_registry(
121123
self,
124+
root_certificates: bytes,
122125
conn_bi_context: RequestContextInfo,
123126
task_processor_factory: TaskProcessorFactory,
124127
) -> ServicesRegistry:
125128
return self.service_registry_factory(
126129
conn_exec_factory_async_env=True,
127130
conn_bi_context=conn_bi_context,
128131
task_processor_factory=task_processor_factory,
132+
root_certificates_data=root_certificates,
129133
)
130134

131135
@pytest.fixture(scope="function")

lib/dl_connector_bundle_chs3/dl_connector_bundle_chs3_tests/db/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from dl_api_lib_testing.initialization import initialize_api_lib_test
9+
from dl_testing.utils import get_root_certificates
910

1011
from dl_connector_bundle_chs3.chs3_base.core.us_connection import BaseFileS3Connection
1112
from dl_connector_bundle_chs3_tests.db.config import API_TEST_CONFIG
@@ -21,3 +22,8 @@ def _patched(self: Any, s3_filename_suffix: str) -> str:
2122
return s3_filename_suffix
2223

2324
monkeypatch.setattr(BaseFileS3Connection, "get_full_s3_filename", _patched)
25+
26+
27+
@pytest.fixture(scope="session")
28+
def root_certificates() -> bytes:
29+
return get_root_certificates()

lib/dl_connector_chyt/dl_connector_chyt/core/connection_executors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ async def _get_target_conn_dto(self) -> CHYTConnTargetDTO:
6363
insert_quorum=self._conn_options.insert_quorum,
6464
insert_quorum_timeout=self._conn_options.insert_quorum_timeout,
6565
disable_value_processing=self._conn_options.disable_value_processing,
66+
ca_data=self._ca_data.decode("ascii"),
6667
)
6768

6869

0 commit comments

Comments
 (0)