Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sec: BI-5958 bump aiohttp & yarl #768

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def set_span_tag(span: Optional[opentracing.Span], tag_name: str, tag_value: Any
span.set_tag(tag_name, tag_value)

@web.middleware
async def middleware(self, request: web.Request, handler: Handler) -> web.StreamResponse: # type: ignore # TODO: fix
async def middleware(self, request: web.Request, handler: Handler) -> web.StreamResponse: # type: ignore[return]
root_span: Optional[opentracing.Span] = None
operation_name = get_endpoint_code(request)

Expand Down
11 changes: 6 additions & 5 deletions lib/dl_api_commons/dl_api_commons/aiohttp/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ class BIAioHTTPClient:
retrier: BaseRetrier = attr.ib(factory=NoRetriesRetrier)

_ca_data: bytes = attr.ib()
_session: Optional[aiohttp.ClientSession] = attr.ib(init=False)

def __attrs_post_init__(self) -> None:
self._session = self._make_session()
_session: Optional[aiohttp.ClientSession] = attr.ib(init=False, default=None)

def _make_session(self) -> aiohttp.ClientSession:
ssl_context = ssl.create_default_context(cadata=self._ca_data.decode("ascii"))
Expand All @@ -123,7 +120,8 @@ def _make_session(self) -> aiohttp.ClientSession:
)

async def close(self) -> None:
await self._session.close() # type: ignore # 2024-01-24 # TODO: Item "None" of "ClientSession | None" has no attribute "close" [union-attr]
if self._session is not None:
await self._session.close()

async def __aenter__(self) -> BIAioHTTPClient:
return self
Expand Down Expand Up @@ -156,6 +154,9 @@ async def _request(
conn_timeout_sec: Optional[float] = None,
read_timeout_sec: Optional[float] = None,
) -> Optional[Any]:
if self._session is None:
self._session = self._make_session()

timeout = aiohttp.ClientTimeout(
sock_connect=conn_timeout_sec or self.conn_timeout_sec,
sock_read=read_timeout_sec or self.read_timeout_sec,
Expand Down
7 changes: 2 additions & 5 deletions lib/dl_api_commons/dl_api_commons/aiohttp/aiohttp_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
from dl_constants.api_constants import DLHeaders


AIOHTTPMethodMiddleware = Callable[[Any, web.Request, Handler], Awaitable[web.StreamResponse]]


class RequiredResource(enum.Enum):
pass

Expand Down Expand Up @@ -261,7 +258,7 @@ async def wrapper(request: web.Request, handler: Handler) -> web.StreamResponse:
@classmethod
def use_dl_request_on_method(
cls, coro: Callable[[Any, _SELF_TYPE, Handler], Awaitable[web.StreamResponse]]
) -> AIOHTTPMethodMiddleware:
) -> Middleware:
if not inspect.iscoroutinefunction(coro):
raise ValueError("This decorator may only be applied to a coroutine")

Expand All @@ -270,7 +267,7 @@ async def wrapper(self: Any, request: web.Request, handler: Handler) -> web.Stre
dl_request = request[cls.KEY_DL_REQUEST]
return await coro(self, dl_request, handler)

return wrapper
return wrapper # type: ignore[return-value] # TODO FIX: method-based middlewares are not covered by aiohttp typing


_DL_REQUEST_TV = TypeVar("_DL_REQUEST_TV", bound=DLRequestBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def conn_target_dto(
yield next(iter(target_conn_dto_pool))

@pytest.fixture(scope="function", params=[True, False], ids=["async", "sync"])
def remote_adapter(
async def remote_adapter(
self,
conn_target_dto: ConnTargetDTO,
query_executor_options: RemoteQueryExecutorData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,7 @@ def prepare_us(us_config):


@pytest.fixture(scope="function")
@pytest.mark.usefixtures("loop")
def default_async_usm_per_test(bi_context, prepare_us, us_config, root_certificates):
async def default_async_usm_per_test(bi_context, prepare_us, us_config, root_certificates):
rci = RequestContextInfo.create_empty()
return AsyncUSManager(
us_base_url=us_config.base_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def run(self) -> TaskResult:
conn = aiohttp.TCPConnector()
else:
socket_path = self._ctx.secure_reader_settings.socket
secure_reader_endpoint = f"http+unix://{urllib.parse.quote_plus(socket_path)}"
secure_reader_endpoint = f"unix://{urllib.parse.quote_plus(socket_path)}"
conn = aiohttp.UnixConnector(path=socket_path)

async with aiohttp.ClientSession(connector=conn, loop=loop) as session:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ def default_sync_usm(bi_context, prepare_us, us_config):


@pytest.fixture(scope="function")
@pytest.mark.usefixtures("loop")
def default_async_usm_per_test(bi_context, prepare_us, us_config, root_certificates):
async def default_async_usm_per_test(bi_context, prepare_us, us_config, root_certificates):
rci = RequestContextInfo.create_empty()
return AsyncUSManager(
us_base_url=us_config.base_url,
Expand Down
Loading
Loading