From 1b03d22c3b10f5101f52b52295cad4cde31c13ad Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Fri, 20 Oct 2023 11:10:12 -0400 Subject: [PATCH 1/3] chore: move mark authz done fn into mixin for use in authz service --- bento_lib/auth/middleware/base.py | 16 ++++++++-------- bento_lib/auth/types.py | 13 +++++++++++++ bento_lib/responses/fastapi_errors.py | 8 ++++---- bento_lib/responses/flask_errors.py | 9 +++++---- 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 bento_lib/auth/types.py diff --git a/bento_lib/auth/middleware/base.py b/bento_lib/auth/middleware/base.py index 790b0d3..acd2116 100644 --- a/bento_lib/auth/middleware/base.py +++ b/bento_lib/auth/middleware/base.py @@ -6,14 +6,19 @@ from typing import Any, Callable, Iterable from ..exceptions import BentoAuthException +from ..types import EvaluationResultMatrix -__all__ = ["EvaluationResultMatrix", "BaseAuthMiddleware"] +__all__ = ["MarkAuthzDoneMixin", "BaseAuthMiddleware"] -EvaluationResultMatrix = tuple[tuple[bool, ...], ...] +class MarkAuthzDoneMixin(ABC): + @staticmethod + @abstractmethod + def mark_authz_done(request: Any): # pragma: no cover + pass -class BaseAuthMiddleware(ABC): +class BaseAuthMiddleware(ABC, MarkAuthzDoneMixin): def __init__( self, bento_authz_service_url: str, @@ -192,11 +197,6 @@ async def async_evaluate_one( ) )[0][0] - @staticmethod - @abstractmethod - def mark_authz_done(request: Any): # pragma: no cover - pass - def check_authz_evaluate( self, request: Any, diff --git a/bento_lib/auth/types.py b/bento_lib/auth/types.py new file mode 100644 index 0000000..bc85608 --- /dev/null +++ b/bento_lib/auth/types.py @@ -0,0 +1,13 @@ +from typing import Type + +from ..auth.middleware.base import MarkAuthzDoneMixin + +__all__ = [ + "EvaluationResultMatrix", + "MarkAuthzDoneType", +] + +EvaluationResultMatrix = tuple[tuple[bool, ...], ...] + +# Allow subclass OR instance, since mark_authz_done is a static method: +MarkAuthzDoneType = MarkAuthzDoneMixin | Type[MarkAuthzDoneMixin] diff --git a/bento_lib/responses/fastapi_errors.py b/bento_lib/responses/fastapi_errors.py index 81a96f5..4c64d3f 100644 --- a/bento_lib/responses/fastapi_errors.py +++ b/bento_lib/responses/fastapi_errors.py @@ -8,8 +8,8 @@ from starlette.responses import Response from typing import Callable -from ..auth.middleware.fastapi import FastApiAuthMiddleware from ..auth.exceptions import BentoAuthException +from ..auth.types import MarkAuthzDoneType from .errors import http_error __all__ = [ @@ -26,7 +26,7 @@ def _log_if_500(logger: logging.Logger, code: int, exc: Exception) -> None: def http_exception_handler_factory( logger: logging.Logger, - authz: FastApiAuthMiddleware | None = None, + authz: MarkAuthzDoneType | None = None, **kwargs, ) -> Callable[[Request, HTTPException], Response]: def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse: @@ -41,7 +41,7 @@ def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse def bento_auth_exception_handler_factory( logger: logging.Logger, - authz: FastApiAuthMiddleware | None = None, + authz: MarkAuthzDoneType | None = None, **kwargs, ) -> Callable[[Request, BentoAuthException], Response]: def bento_auth_exception_handler(request: Request, exc: BentoAuthException) -> JSONResponse: @@ -55,7 +55,7 @@ def bento_auth_exception_handler(request: Request, exc: BentoAuthException) -> J def validation_exception_handler_factory( - authz: FastApiAuthMiddleware | None = None, + authz: MarkAuthzDoneType | None = None, **kwargs, ) -> Callable[[Request, RequestValidationError], Response]: def validation_exception_handler(request: Request, exc: RequestValidationError) -> JSONResponse: diff --git a/bento_lib/responses/flask_errors.py b/bento_lib/responses/flask_errors.py index 39492f9..a762d21 100644 --- a/bento_lib/responses/flask_errors.py +++ b/bento_lib/responses/flask_errors.py @@ -5,8 +5,9 @@ from functools import partial from typing import Callable -from bento_lib.auth.middleware.flask import FlaskAuthMiddleware -from bento_lib.responses import errors +from ..auth.middleware.flask import FlaskAuthMiddleware +from ..auth.types import MarkAuthzDoneType +from ..responses import errors __all__ = [ @@ -38,7 +39,7 @@ def flask_error_wrap_with_traceback(fn: Callable, *args, **kwargs) -> Callable: service_name = kwargs.pop("service_name", "Bento Service") logger = kwargs.pop("logger", None) - authz: FlaskAuthMiddleware | None = kwargs.pop("authz", None) + authz: MarkAuthzDoneType | None = kwargs.pop("authz", None) def handle_error(e): if logger: @@ -62,7 +63,7 @@ def flask_error_wrap(fn: Callable, *args, **kwargs) -> Callable: :return: The wrapped function """ - authz: FlaskAuthMiddleware | None = kwargs.pop("authz", None) + authz: MarkAuthzDoneType | None = kwargs.pop("authz", None) def handle_error(e): if authz: From aa3162820e1ab127a3251d4a99baaa153bf0ac7d Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Fri, 20 Oct 2023 11:10:30 -0400 Subject: [PATCH 2/3] chore: bump version to 9.0.0a3 --- bento_lib/package.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bento_lib/package.cfg b/bento_lib/package.cfg index 7bd5eb8..3c2f5ab 100644 --- a/bento_lib/package.cfg +++ b/bento_lib/package.cfg @@ -1,5 +1,5 @@ [package] name = bento_lib -version = 9.0.0a2 +version = 9.0.0a3 authors = David Lougheed, Paul Pillot author_emails = david.lougheed@mail.mcgill.ca, paul.pillot@computationalgenomics.ca From 4bd714728b0de8d00e837de93d55d074fb315aa1 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Fri, 20 Oct 2023 11:25:44 -0400 Subject: [PATCH 3/3] fix: issues with MarkAuthzDoneMixin --- bento_lib/auth/middleware/base.py | 10 ++-------- bento_lib/auth/middleware/mark_authz_done_mixin.py | 12 ++++++++++++ bento_lib/auth/types.py | 2 +- bento_lib/responses/flask_errors.py | 1 - 4 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 bento_lib/auth/middleware/mark_authz_done_mixin.py diff --git a/bento_lib/auth/middleware/base.py b/bento_lib/auth/middleware/base.py index acd2116..a75c175 100644 --- a/bento_lib/auth/middleware/base.py +++ b/bento_lib/auth/middleware/base.py @@ -7,15 +7,9 @@ from ..exceptions import BentoAuthException from ..types import EvaluationResultMatrix +from .mark_authz_done_mixin import MarkAuthzDoneMixin -__all__ = ["MarkAuthzDoneMixin", "BaseAuthMiddleware"] - - -class MarkAuthzDoneMixin(ABC): - @staticmethod - @abstractmethod - def mark_authz_done(request: Any): # pragma: no cover - pass +__all__ = ["BaseAuthMiddleware"] class BaseAuthMiddleware(ABC, MarkAuthzDoneMixin): diff --git a/bento_lib/auth/middleware/mark_authz_done_mixin.py b/bento_lib/auth/middleware/mark_authz_done_mixin.py new file mode 100644 index 0000000..1494c74 --- /dev/null +++ b/bento_lib/auth/middleware/mark_authz_done_mixin.py @@ -0,0 +1,12 @@ +from abc import abstractmethod +from typing import Any + + +__all__ = ["MarkAuthzDoneMixin"] + + +class MarkAuthzDoneMixin: + @staticmethod + @abstractmethod + def mark_authz_done(request: Any): # pragma: no cover + pass diff --git a/bento_lib/auth/types.py b/bento_lib/auth/types.py index bc85608..d37dd45 100644 --- a/bento_lib/auth/types.py +++ b/bento_lib/auth/types.py @@ -1,6 +1,6 @@ from typing import Type -from ..auth.middleware.base import MarkAuthzDoneMixin +from ..auth.middleware.mark_authz_done_mixin import MarkAuthzDoneMixin __all__ = [ "EvaluationResultMatrix", diff --git a/bento_lib/responses/flask_errors.py b/bento_lib/responses/flask_errors.py index a762d21..d384e78 100644 --- a/bento_lib/responses/flask_errors.py +++ b/bento_lib/responses/flask_errors.py @@ -5,7 +5,6 @@ from functools import partial from typing import Callable -from ..auth.middleware.flask import FlaskAuthMiddleware from ..auth.types import MarkAuthzDoneType from ..responses import errors