Version 0.4.0
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 1m40s
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 1m40s
Changes: - put ObservabilityMiddleware before ExceptionHandlerMiddleware to avoid repetative code - add application startup and last metrics update metrics along with CPU usage metric and threads count - move host and port to new uvicorn section at config along with new reload and forwarded_allow_ips - add request_id and remove trace_id/span_id generation if tracing is disabled - move logging logic from utils to observability - pass trace_id/span_id in HEX form
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
"""Authentication dependency function is defined here."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import jwt
|
||||
from fastapi import Request
|
||||
|
||||
from . import logger_dep
|
||||
|
||||
|
||||
@dataclass
|
||||
class AuthenticationData:
|
||||
api_key: str | None
|
||||
jwt_payload: dict | None
|
||||
jwt_original: str | None
|
||||
|
||||
|
||||
def obtain(request: Request) -> AuthenticationData:
|
||||
if hasattr(request.state, "auth_dep"):
|
||||
return request.state.auth_dep
|
||||
auth = AuthenticationData(None, None, None)
|
||||
if (value := request.headers.get("X-API-Key")) is not None:
|
||||
auth.api_key = value
|
||||
if (value := request.headers.get("Authorization")) is not None and value.startswith("Bearer "):
|
||||
value = value[7:]
|
||||
auth.jwt_original = value
|
||||
try:
|
||||
auth.jwt_payload = jwt.decode(value, algorithms=["HS256"], options={"verify_signature": False})
|
||||
except Exception: # pylint: disable=broad-except
|
||||
logger = logger_dep.obtain(request)
|
||||
logger.warning("failed to parse Authorization header as jwt", value=value)
|
||||
logger.debug("failed to parse Authorization header as jwt", exc_info=True)
|
||||
request.state.auth_dep = auth
|
||||
return auth
|
||||
46
{{project_slug}}/dependencies/auth_dep.py.jinja
Normal file
46
{{project_slug}}/dependencies/auth_dep.py.jinja
Normal file
@@ -0,0 +1,46 @@
|
||||
"""Authentication dependency function is defined here."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import jwt
|
||||
from fastapi import Request
|
||||
|
||||
from {{project_slug}}.exceptions.auth import NotAuthorizedError
|
||||
|
||||
from . import logger_dep
|
||||
|
||||
|
||||
@dataclass
|
||||
class AuthenticationData:
|
||||
api_key: str | None
|
||||
jwt_payload: dict | None
|
||||
|
||||
|
||||
def _from_request(request: Request, required: bool = True) -> AuthenticationData | None:
|
||||
if not hasattr(request.state, "auth_dep"):
|
||||
auth = AuthenticationData(None, None)
|
||||
if (value := request.headers.get("X-API-Key")) is not None:
|
||||
auth.api_key = value
|
||||
if (value := request.headers.get("Authorization")) is not None and value.startswith("Bearer "):
|
||||
value = value[7:]
|
||||
try:
|
||||
auth.jwt_payload = jwt.decode(value, algorithms=["HS256"], options={"verify_signature": False})
|
||||
except Exception: # pylint: disable=broad-except
|
||||
logger = logger_dep.from_request(request)
|
||||
logger.warning("failed to parse Authorization header as jwt", value=value)
|
||||
logger.debug("failed to parse Authorization header as jwt", exc_info=True)
|
||||
if auth.api_key is not None or auth.jwt_payload is not None:
|
||||
request.state.auth_dep = auth
|
||||
else:
|
||||
request.state.auth_dep = None
|
||||
if required and request.state.auth_dep is None:
|
||||
raise NotAuthorizedError()
|
||||
return request.state.auth_dep
|
||||
|
||||
|
||||
def from_request_optional(request: Request) -> AuthenticationData | None:
|
||||
return _from_request(request, required=False)
|
||||
|
||||
|
||||
def from_request(request: Request) -> AuthenticationData:
|
||||
return _from_request(request, required=True)
|
||||
@@ -18,10 +18,17 @@ def init_dispencer(app: FastAPI, connection_manager: PostgresConnectionManager)
|
||||
app.state.postgres_connection_manager_dep = connection_manager
|
||||
|
||||
|
||||
def obtain(app_or_request: FastAPI | Request) -> PostgresConnectionManager:
|
||||
"""Get a PostgresConnectionManager from request's app state."""
|
||||
if isinstance(app_or_request, Request):
|
||||
app_or_request = app_or_request.app
|
||||
if not hasattr(app_or_request.state, "postgres_connection_manager_dep"):
|
||||
def from_app(app: FastAPI) -> PostgresConnectionManager:
|
||||
"""Get a connection_manager from app state."""
|
||||
if not hasattr(app.state, "postgres_connection_manager_dep"):
|
||||
raise ValueError("PostgresConnectionManager dispencer was not initialized at app preparation")
|
||||
return app_or_request.state.postgres_connection_manager_dep
|
||||
return app.state.postgres_connection_manager_dep
|
||||
|
||||
|
||||
async def from_request(request: Request) -> PostgresConnectionManager:
|
||||
"""Get a PostgresConnectionManager from request or app state."""
|
||||
if hasattr(request.state, "postgres_connection_manager_dep"):
|
||||
connection_manager = request.state.postgres_connection_manager_dep
|
||||
if isinstance(connection_manager, PostgresConnectionManager):
|
||||
return connection_manager
|
||||
return from_app(request.app)
|
||||
|
||||
@@ -9,7 +9,7 @@ def init_dispencer(app: FastAPI, logger: BoundLogger) -> None:
|
||||
if hasattr(app.state, "logger"):
|
||||
if not isinstance(app.state.logger_dep, BoundLogger):
|
||||
raise ValueError(
|
||||
"logger attribute of app's state is already set" f"with other value ({app.state.logger_dep})"
|
||||
f"logger attribute of app's state is already set with other value ({app.state.logger_dep})"
|
||||
)
|
||||
return
|
||||
|
||||
@@ -24,14 +24,17 @@ def attach_to_request(request: Request, logger: BoundLogger) -> None:
|
||||
request.state.logger_dep = logger
|
||||
|
||||
|
||||
def obtain(app_or_request: FastAPI | Request) -> BoundLogger:
|
||||
"""Get a logger from request or app state."""
|
||||
if isinstance(app_or_request, Request):
|
||||
if hasattr(app_or_request.state, "logger_dep"):
|
||||
logger = app_or_request.state.logger_dep
|
||||
if isinstance(logger, BoundLogger):
|
||||
return logger
|
||||
app_or_request = app_or_request.app
|
||||
if not hasattr(app_or_request.state, "logger_dep"):
|
||||
def from_app(app: FastAPI) -> BoundLogger:
|
||||
"""Get a logger from app state."""
|
||||
if not hasattr(app.state, "logger_dep"):
|
||||
raise ValueError("BoundLogger dispencer was not initialized at app preparation")
|
||||
return app_or_request.state.logger_dep
|
||||
return app.state.logger_dep
|
||||
|
||||
|
||||
def from_request(request: Request) -> BoundLogger:
|
||||
"""Get a logger from request or app state."""
|
||||
if hasattr(request.state, "logger_dep"):
|
||||
logger = request.state.logger_dep
|
||||
if isinstance(logger, BoundLogger):
|
||||
return logger
|
||||
return from_app(request.app)
|
||||
|
||||
@@ -5,22 +5,25 @@ from fastapi import FastAPI, Request
|
||||
from {{project_slug}}.observability.metrics import Metrics
|
||||
|
||||
|
||||
def init_dispencer(app: FastAPI, connection_manager: Metrics) -> None:
|
||||
def init_dispencer(app: FastAPI, metrics: Metrics) -> None:
|
||||
"""Initialize Metrics dispencer at app's state."""
|
||||
if hasattr(app.state, "metrics_dep"):
|
||||
if not isinstance(app.state.metrics_dep, Metrics):
|
||||
raise ValueError(
|
||||
"metrics_dep attribute of app's state is already set" f"with other value ({app.state.metrics_dep})"
|
||||
f"metrics_dep attribute of app's state is already set with other value ({app.state.metrics_dep})"
|
||||
)
|
||||
return
|
||||
|
||||
app.state.metrics_dep = connection_manager
|
||||
app.state.metrics_dep = metrics
|
||||
|
||||
|
||||
def obtain(app_or_request: FastAPI | Request) -> Metrics:
|
||||
"""Get a Metrics from request's app state."""
|
||||
if isinstance(app_or_request, Request):
|
||||
app_or_request = app_or_request.app
|
||||
if not hasattr(app_or_request.state, "metrics_dep"):
|
||||
def from_app(app: FastAPI) -> Metrics:
|
||||
"""Get a Metrics from app state."""
|
||||
if not hasattr(app.state, "metrics_dep"):
|
||||
raise ValueError("Metrics dispencer was not initialized at app preparation")
|
||||
return app_or_request.state.metrics_dep
|
||||
return app.state.metrics_dep
|
||||
|
||||
|
||||
async def from_request(request: Request) -> Metrics:
|
||||
"""Get a Metrics from request's app state."""
|
||||
return from_app(request.app)
|
||||
|
||||
Reference in New Issue
Block a user