Skip to content

Commit

Permalink
chore: BI-0 switch to aiohttp Middleware typing (#747)
Browse files Browse the repository at this point in the history
* chore: BI-0 switch to aiohttp Middleware typing

* revert some bad cq

* add mypy comment

* bring AIOHTTPMethodMiddleware back

* update dl_native_auth
  • Loading branch information
KonstantAnxiety authored Dec 13, 2024
1 parent fc61ee9 commit ab244a5
Show file tree
Hide file tree
Showing 33 changed files with 92 additions and 85 deletions.
13 changes: 7 additions & 6 deletions app/dl_data_api/dl_data_api/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import ssl
from typing import Optional

from aiohttp.typedefs import Middleware

from dl_api_commons.aio.middlewares.auth_trust_middleware import auth_trust_middleware
from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_lib.app.data_api.app import (
DataApiAppFactory,
EnvSetupResult,
Expand Down Expand Up @@ -84,8 +85,8 @@ def set_up_environment(
self,
connectors_settings: dict[ConnectionType, ConnectorSettingsBase],
) -> EnvSetupResult:
sr_middleware_list: list[AIOHTTPMiddleware]
usm_middleware_list: list[AIOHTTPMiddleware]
sr_middleware_list: list[Middleware]
usm_middleware_list: list[Middleware]

ca_data = get_multiple_root_certificates(
self._settings.CA_FILE_PATH,
Expand Down Expand Up @@ -139,7 +140,7 @@ def set_up_environment(

return result

def _get_auth_middleware(self) -> AIOHTTPMiddleware:
def _get_auth_middleware(self) -> Middleware:
if self._settings.AUTH is None or self._settings.AUTH.TYPE == "NONE":
return self._get_auth_middleware_none()

Expand All @@ -155,7 +156,7 @@ def _get_auth_middleware(self) -> AIOHTTPMiddleware:

def _get_auth_middleware_none(
self,
) -> AIOHTTPMiddleware:
) -> Middleware:
return auth_trust_middleware(
fake_user_id="_user_id_",
fake_user_name="_user_name_",
Expand All @@ -164,7 +165,7 @@ def _get_auth_middleware_none(
def _get_auth_middleware_zitadel(
self,
ca_data: bytes,
) -> AIOHTTPMiddleware:
) -> Middleware:
self._settings: DataApiAppSettingsOS
assert self._settings.AUTH is not None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@


if TYPE_CHECKING:
from aiohttp.typedefs import Handler

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from aiohttp.typedefs import (
Handler,
Middleware,
)


def auth_trust_middleware(
fake_user_id: Optional[str] = None,
fake_user_name: Optional[str] = None,
fake_tenant: Optional[TenantDef] = None,
fake_auth: Optional[AuthData] = None,
) -> AIOHTTPMiddleware:
) -> Middleware:
@web.middleware
@aiohttp_wrappers.DLRequestBase.use_dl_request
async def actual_auth_trust_middleware(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from aiohttp import web
from aiohttp.typedefs import Handler
from aiohttp.typedefs import (
Handler,
Middleware,
)

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.crypto import get_hmac_hex_digest


def body_signature_validation_middleware(hmac_key: bytes, header: str) -> AIOHTTPMiddleware:
def body_signature_validation_middleware(hmac_key: bytes, header: str) -> Middleware:
@web.middleware
async def actual_middleware(request: web.Request, handler: Handler) -> web.StreamResponse:
if not hmac_key: # do not consider an empty hmac key as valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
)

from aiohttp import web
from aiohttp.typedefs import Handler
from aiohttp.typedefs import (
Handler,
Middleware,
)
from multidict import CIMultiDict

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.aiohttp import aiohttp_wrappers
from dl_api_commons.headers import (
DEFAULT_RCI_PLAIN_HEADERS,
Expand All @@ -21,7 +23,7 @@
def commit_rci_middleware(
rci_extra_plain_headers: Optional[Sequence[str]] = None,
rci_extra_secret_headers: Optional[Sequence[str]] = None,
) -> AIOHTTPMiddleware:
) -> Middleware:
plain_headers_to_rci = append_extra_headers_and_normalize(DEFAULT_RCI_PLAIN_HEADERS, rci_extra_plain_headers or ())
secret_headers_to_rci = append_extra_headers_and_normalize(
DEFAULT_RCI_SECRET_HEADERS, rci_extra_secret_headers or ()
Expand Down
9 changes: 5 additions & 4 deletions lib/dl_api_commons/dl_api_commons/aio/middlewares/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
hdrs,
web,
)
from aiohttp.typedefs import Handler

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from aiohttp.typedefs import (
Handler,
Middleware,
)


ALL_METHODS = ("DELETE", "GET", "OPTIONS", "PATCH", "POST", "PUT")
Expand All @@ -19,7 +20,7 @@ def cors_middleware(
allow_methods: tuple[str, ...] = ALL_METHODS,
allow_credentials: bool = False,
max_age: Optional[int] = None,
) -> AIOHTTPMiddleware:
) -> Middleware:
@web.middleware
async def middleware(request: web.Request, handler: Handler) -> web.StreamResponse:
is_options_request = request.method == "OPTIONS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import logging

from aiohttp import web
from aiohttp.typedefs import Handler
from aiohttp.typedefs import (
Handler,
Middleware,
)

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.aiohttp.aiohttp_wrappers import (
DLRequestBase,
RequiredResourceCommon,
Expand All @@ -22,7 +24,7 @@
def master_key_middleware(
master_key: str,
header: DLHeaders = DLHeadersCommon.API_KEY,
) -> AIOHTTPMiddleware:
) -> Middleware:
@web.middleware
@DLRequestBase.use_dl_request
async def actual_master_key_middleware(dl_request: DLRequestBase, handler: Handler) -> web.StreamResponse:
Expand Down
15 changes: 0 additions & 15 deletions lib/dl_api_commons/dl_api_commons/aio/typing.py

This file was deleted.

14 changes: 8 additions & 6 deletions lib/dl_api_commons/dl_api_commons/aiohttp/aiohttp_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
)

from aiohttp import web
from aiohttp.typedefs import Handler

from dl_api_commons.aio.typing import (
AIOHTTPMethodMiddleware,
AIOHTTPMiddleware,
from aiohttp.typedefs import (
Handler,
Middleware,
)

from dl_api_commons.base_models import RequestContextInfo
from dl_api_commons.exc import InvalidHeaderException
from dl_api_commons.logging import RequestLoggingContextController
Expand All @@ -33,6 +32,9 @@
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 @@ -239,7 +241,7 @@ def required_resources(self) -> FrozenSet[RequiredResource]:
@classmethod
def use_dl_request(
cls: Type[_SELF_TYPE], coro: Callable[[_SELF_TYPE, Handler], Awaitable[web.StreamResponse]]
) -> AIOHTTPMiddleware:
) -> Middleware:
if not inspect.iscoroutinefunction(coro):
raise ValueError("This decorator may only be applied to a coroutine")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Middleware to reset asyncio event loop on each request
"""

from __future__ import annotations

import asyncio
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Middleware to reset context on each request.
"""

from __future__ import annotations

import contextvars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Middleware to extract OpenTracing context from HTTP headers and creating root request span.
Should be placed after ContextVarMiddleware
"""

from __future__ import annotations

import contextlib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
app = Flask(__name__)
FlaskWSGIMiddleware().wrap_flask_app(app)
"""

from __future__ import annotations

import abc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import logging

from aiohttp import web
from aiohttp.typedefs import Handler
from aiohttp.typedefs import (
Handler,
Middleware,
)

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.logging import RequestObfuscator
from dl_api_lib.aio.aiohttp_wrappers import DSAPIRequest
from dl_api_lib.app.data_api.resources.base import (
Expand All @@ -20,7 +22,7 @@
LOGGER = logging.getLogger(__name__)


def json_body_middleware() -> AIOHTTPMiddleware:
def json_body_middleware() -> Middleware:
@web.middleware
@DSAPIRequest.use_dl_request
async def actual_body_log_middleware(dl_request: DSAPIRequest, handler: Handler) -> web.StreamResponse:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import logging

from aiohttp import web
from aiohttp.typedefs import Handler
from aiohttp.typedefs import (
Handler,
Middleware,
)

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.aiohttp.aiohttp_wrappers import RequiredResourceCommon
from dl_api_commons.base_models import TenantCommon
from dl_api_lib.aio.aiohttp_wrappers import DSAPIRequest
Expand All @@ -15,7 +17,7 @@
LOGGER = logging.getLogger(__name__)


def public_api_key_middleware(api_keys: tuple[str, ...]) -> AIOHTTPMiddleware:
def public_api_key_middleware(api_keys: tuple[str, ...]) -> Middleware:
if not isinstance(api_keys, tuple):
raise TypeError(f"API key must be a tuple, not '{type(api_keys)}'")

Expand Down
8 changes: 4 additions & 4 deletions lib/dl_api_lib/dl_api_lib/app/data_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
)

from aiohttp import web
from aiohttp.typedefs import Middleware
import attr

from dl_api_commons.aio.middlewares.commit_rci import commit_rci_middleware
from dl_api_commons.aio.middlewares.request_bootstrap import RequestBootstrap
from dl_api_commons.aio.middlewares.request_id import RequestId
from dl_api_commons.aio.middlewares.tracing import TracingService
from dl_api_commons.aio.server_header import ServerHeader
from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.sentry_config import (
SentryConfig,
configure_sentry_for_aiohttp,
Expand Down Expand Up @@ -101,9 +101,9 @@ async def add_connection_close(request: web.Request, response: web.StreamRespons

@attr.s(frozen=True)
class EnvSetupResult:
auth_mw_list: list[AIOHTTPMiddleware] = attr.ib(kw_only=True)
sr_middleware_list: list[AIOHTTPMiddleware] = attr.ib(kw_only=True)
usm_middleware_list: list[AIOHTTPMiddleware] = attr.ib(kw_only=True)
auth_mw_list: list[Middleware] = attr.ib(kw_only=True)
sr_middleware_list: list[Middleware] = attr.ib(kw_only=True)
usm_middleware_list: list[Middleware] = attr.ib(kw_only=True)


TDataApiSettings = TypeVar("TDataApiSettings", bound=DataApiAppSettings)
Expand Down
4 changes: 2 additions & 2 deletions lib/dl_auth_api_lib/dl_auth_api_lib/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
)

from aiohttp import web
from aiohttp.typedefs import Middleware
import attr

from dl_api_commons.aio.middlewares.commit_rci import commit_rci_middleware
from dl_api_commons.aio.middlewares.request_bootstrap import RequestBootstrap
from dl_api_commons.aio.middlewares.request_id import RequestId
from dl_api_commons.aio.middlewares.tracing import TracingService
from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.sentry_config import (
SentryConfig,
configure_sentry_for_aiohttp,
Expand All @@ -37,7 +37,7 @@ class OAuthApiAppFactory(Generic[_TSettings], abc.ABC):
_settings: _TSettings = attr.ib()

@abc.abstractmethod
def get_auth_middlewares(self) -> list[AIOHTTPMiddleware]:
def get_auth_middlewares(self) -> list[Middleware]:
raise NotImplementedError()

def set_up_sentry(self, secret_sentry_dsn: str, release: str | None) -> None:
Expand Down
4 changes: 2 additions & 2 deletions lib/dl_auth_api_lib/dl_auth_api_lib_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import os

import aiohttp.pytest_plugin
from aiohttp.typedefs import Middleware
import pytest

from dl_api_commons.aio.typing import AIOHTTPMiddleware
from dl_api_commons.base_models import (
NoAuthData,
TenantCommon,
Expand Down Expand Up @@ -87,7 +87,7 @@ def oauth_app_settings(monkeypatch, config_path):


class TestingOAuthApiAppFactory(OAuthApiAppFactory[AuthAPISettings]):
def get_auth_middlewares(self) -> list[AIOHTTPMiddleware]:
def get_auth_middlewares(self) -> list[Middleware]:
return []


Expand Down
3 changes: 1 addition & 2 deletions lib/dl_auth_native/dl_auth_native/middlewares/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import aiohttp.web as aiohttp_web
import attr

import dl_api_commons.aio.typing as dl_api_commons_aio_typing
import dl_api_commons.aiohttp.aiohttp_wrappers as dl_api_commons_aiohttp_aiohttp_wrappers
import dl_auth_native.middlewares.base as middlewares_base

Expand All @@ -14,7 +13,7 @@

@attr.s
class AioHTTPMiddleware(middlewares_base.BaseMiddleware):
def get_middleware(self) -> dl_api_commons_aio_typing.AIOHTTPMiddleware:
def get_middleware(self) -> aiohttp_typedefs.Middleware:
@aiohttp_web.middleware
@dl_api_commons_aiohttp_aiohttp_wrappers.DLRequestBase.use_dl_request
async def inner(
Expand Down
1 change: 1 addition & 0 deletions lib/dl_configs/dl_configs/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
All the classes (instead of dicts) for static checking.
"""

from __future__ import annotations


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Here we test old autonomous loaders of sub-configs (which actually use new declarative framework).
In future all loaders which are tested here must be removed.
"""

from dl_configs.rqe import (
RQEConfig,
rqe_config_from_env,
Expand Down
Loading

0 comments on commit ab244a5

Please sign in to comment.