diff --git a/common_lib/services/profile.py b/common_lib/services/profile.py index b6a4345..a7061e7 100644 --- a/common_lib/services/profile.py +++ b/common_lib/services/profile.py @@ -3,11 +3,12 @@ from uuid import UUID from fastapi import HTTPException, status from typing import List, Dict from config import settings +from .utils import ssl_transport async def get_profile_by_user_id(user_id: UUID, token: str) -> dict: try: - async with httpx.AsyncClient(timeout=5.0, verify=False) as client: + async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0,) as client: response = await client.get( f"{settings.PROFILE_SERVICE}/user_id/{user_id}", headers={"Authorization": f"Bearer {token}"} @@ -27,7 +28,7 @@ async def get_profile_by_user_id(user_id: UUID, token: str) -> dict: async def get_profiles_by_user_ids(user_ids: List[UUID], token: str, user_id: UUID) -> Dict[str, dict]: try: - async with httpx.AsyncClient(timeout=5.0, verify=False) as client: + async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0,) as client: response = await client.post( f"{settings.PROFILE_SERVICE}/user_ids/internal", headers={"Authorization": f"Bearer {token}"}, diff --git a/common_lib/utils/__init__.py b/common_lib/utils/__init__.py index 4d66c12..421c430 100644 --- a/common_lib/utils/__init__.py +++ b/common_lib/utils/__init__.py @@ -1,3 +1,4 @@ from .utils import get_datetime +from .utils import ssl_transport -__all__ = ["get_datetime"] +__all__ = ["get_datetime", "ssl_transport"] diff --git a/common_lib/utils/auth.py b/common_lib/utils/auth.py index 49cee6a..40ee271 100644 --- a/common_lib/utils/auth.py +++ b/common_lib/utils/auth.py @@ -6,6 +6,7 @@ from typing import List from dataclasses import dataclass from config import settings +from .utils import ssl_transport auth_scheme = HTTPBearer() @@ -27,11 +28,7 @@ async def _fetch_current_user( user_agent = request.headers.get("User-Agent", "(unknown)") try: - async with httpx.AsyncClient( - verify=settings.CA_CERT, - cert=(settings.CLIENT_FULLCHAIN, settings.CLIENT_PRIVKEY), - timeout=5.0 - ) as client: + async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0,) as client: response = await client.post( f"{settings.TOKEN_SERVICE}/decode", json={ diff --git a/common_lib/utils/ssl_transport.py b/common_lib/utils/ssl_transport.py new file mode 100644 index 0000000..0b3f92b --- /dev/null +++ b/common_lib/utils/ssl_transport.py @@ -0,0 +1,8 @@ +import ssl +import httpx +from config import settings + +ctx = ssl.create_default_context(cafile=settings.CA_CERT) +ctx.load_cert_chain(certfile=settings.CLIENT_FULLCHAIN, keyfile=settings.CLIENT_PRIVKEY) + +ssl_transport = httpx.AsyncHTTPTransport(verify=ctx, http2=True)