Skip to content

Commit cbad063

Browse files
committed
🔧 Refactor database engine usage to utilize AsyncEngine for improved async support
1 parent 1e51ccb commit cbad063

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

services/web/server/src/simcore_service_webserver/security/_authz_db.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
from typing import TypedDict
33

44
import sqlalchemy as sa
5-
from aiopg.sa import Engine
6-
from aiopg.sa.result import ResultProxy
75
from models_library.basic_types import IdInt
86
from models_library.products import ProductName
97
from models_library.users import UserID
108
from pydantic import TypeAdapter
119
from simcore_postgres_database.models.groups import user_to_groups
1210
from simcore_postgres_database.models.products import products
1311
from simcore_postgres_database.models.users import UserRole
12+
from sqlalchemy.ext.asyncio import AsyncEngine
1413

1514
from ..db.models import UserStatus, users
1615

@@ -22,33 +21,38 @@ class AuthInfoDict(TypedDict, total=True):
2221
role: UserRole
2322

2423

25-
async def get_active_user_or_none(engine: Engine, email: str) -> AuthInfoDict | None:
24+
async def get_active_user_or_none(
25+
engine: AsyncEngine, *, email: str
26+
) -> AuthInfoDict | None:
2627
"""Gets a user with email if ACTIVE othewise return None
2728
2829
Raises:
29-
DatabaseError: unexpected errors found in https://github.com/ITISFoundation/osparc-simcore/issues/880 and https://github.com/ITISFoundation/osparc-simcore/pull/1160
30+
DatabaseError: unexpected errors found in
31+
https://github.com/ITISFoundation/osparc-simcore/issues/880 and
32+
https://github.com/ITISFoundation/osparc-simcore/pull/1160
3033
"""
31-
async with engine.acquire() as conn:
32-
result: ResultProxy = await conn.execute(
34+
async with engine.connect() as conn:
35+
result = await conn.execute(
3336
sa.select(users.c.id, users.c.role).where(
3437
(users.c.email == email) & (users.c.status == UserStatus.ACTIVE)
3538
)
3639
)
37-
row = await result.fetchone()
38-
assert (
39-
row is None or TypeAdapter(IdInt).validate_python(row.id) is not None # nosec
40+
row = result.one_or_none()
41+
42+
assert ( # nosec
43+
row is None or TypeAdapter(IdInt).validate_python(row.id) is not None
4044
)
41-
assert (
42-
row is None or TypeAdapter(UserRole).validate_python(row.role) is not None # nosec
45+
assert ( # nosec
46+
row is None or TypeAdapter(UserRole).validate_python(row.role) is not None
4347
)
4448

4549
return AuthInfoDict(id=row.id, role=row.role) if row else None
4650

4751

4852
async def is_user_in_product_name(
49-
engine: Engine, user_id: UserID, product_name: ProductName
53+
engine: AsyncEngine, *, user_id: UserID, product_name: ProductName
5054
) -> bool:
51-
async with engine.acquire() as conn:
55+
async with engine.connect() as conn:
5256
return (
5357
await conn.scalar(
5458
sa.select(users.c.id)

services/web/server/src/simcore_service_webserver/security/_authz_policy.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
)
1313
from models_library.products import ProductName
1414
from models_library.users import UserID
15+
from servicelib.aiohttp.db_asyncpg_engine import get_async_engine
1516
from simcore_postgres_database.aiopg_errors import DatabaseError
1617

17-
from ..db.plugin import get_database_engine
1818
from ._authz_access_model import (
1919
AuthContextDict,
2020
OptionalContext,
@@ -62,7 +62,9 @@ async def _get_auth_or_none(self, *, email: str) -> AuthInfoDict | None:
6262
web.HTTPServiceUnavailable: if database raises an exception
6363
"""
6464
with _handle_exceptions_as_503():
65-
return await get_active_user_or_none(get_database_engine(self._app), email)
65+
return await get_active_user_or_none(
66+
get_async_engine(self._app), email=email
67+
)
6668

6769
@cached(
6870
ttl=_AUTHZ_BURST_CACHE_TTL,
@@ -78,7 +80,7 @@ async def _has_access_to_product(
7880
"""
7981
with _handle_exceptions_as_503():
8082
return await is_user_in_product_name(
81-
get_database_engine(self._app), user_id, product_name
83+
get_async_engine(self._app), user_id=user_id, product_name=product_name
8284
)
8385

8486
@property
@@ -122,11 +124,6 @@ async def permits(
122124
:return: True if user has permission to execute this operation within the given context
123125
"""
124126
if identity is None or permission is None:
125-
_logger.debug(
126-
"Invalid %s of %s. Denying access.",
127-
f"{identity=}",
128-
f"{permission=}",
129-
)
130127
return False
131128

132129
auth_info = await self._get_auth_or_none(email=identity)

0 commit comments

Comments
 (0)