mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-23 04:21:52 +00:00
ded28baa2f
* feat(Users): PRWLR-4718 make user email the default login username * feat(Token): PRWLR-4718 add serializers, views and urls for access and refresh tokens * feat(Token): PRWLR-4718 add first membership tenant in token if not present in json body * feat(Users): PRWLR-4718 add company_name to model * feat(Users): PRWLR-4718 create tenant and membership when creating new user * fix(BaseView): PRWLR-4718 add tenant_id to serializer context * fix(Tests): PRWLR-4718 use authorization with unit tests * fix(Views): PRWLR-4718 fix tenant retrieval from request * fix(Tests): PRWLR-4718 fix tests * fix(Fixtures): PRWLR-4718 fix tenant memberships ordering * chore(Tokens): PRWLR-4718 update token url * chore(Spec): PRWLR-4718 update API spec * feat(Tokens): PRWLR-4718 enable token refresh blacklisting * feat(Tokens): PRWLR-4718 implement RS256 algorithm and dev valid keys * chore(env): PRWLR-4718 update .env.example * chore(Deps): PRWLR-4015 update prowler dep * fix(Resources, Findings): PRWLR-4015 fix permission issues on models and migrations * feat(Scans, Tasks, Resources, Findings): PRWLR-4015 perform whole flow when executing scans * fix(Settings): PRWLR-4015 fix devel JWT settings * chore(Scans, Tasks): PRWLR-4015 improve docs and responses format * test(Scan, Provider): PRWLR-4015 fix unit tests * chore(Environment): PRWLR-4015 fix .env.example values * test(Scan): PRWLR-4015 add unit tests for scan task * chore(Task): PRWLR-4015 give proper format to task result on scan perform * feat(Provider, Scan): PRWLR-4015 add all provider types to connection check and scan * fix(Logging): PRWLR-4015 fix API logger middleware to include tenant and user IDs
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import logging
|
|
import time
|
|
|
|
from config.custom_logging import BackendLogger
|
|
|
|
|
|
def extract_auth_info(request) -> dict:
|
|
if not hasattr(request, "auth") or request.auth is not None:
|
|
tenant_id = request.auth.get("tenant_id", "N/A")
|
|
user_id = request.auth.get("user_id", "N/A")
|
|
else:
|
|
tenant_id, user_id = "N/A", "N/A"
|
|
return {"tenant_id": tenant_id, "user_id": user_id}
|
|
|
|
|
|
class APILoggingMiddleware:
|
|
"""
|
|
Middleware for logging API requests.
|
|
|
|
This middleware logs details of API requests, including the typical request metadata among other useful information.
|
|
|
|
Args:
|
|
get_response (Callable): A callable to get the response, typically the next middleware or view.
|
|
"""
|
|
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
self.logger = logging.getLogger(BackendLogger.API)
|
|
|
|
def __call__(self, request):
|
|
request_start_time = time.time()
|
|
|
|
response = self.get_response(request)
|
|
duration = time.time() - request_start_time
|
|
auth_info = extract_auth_info(request)
|
|
self.logger.info(
|
|
"",
|
|
extra={
|
|
"user_id": auth_info["user_id"],
|
|
"tenant_id": auth_info["tenant_id"],
|
|
"method": request.method,
|
|
"path": request.path,
|
|
"query_params": request.GET.dict(),
|
|
"status_code": response.status_code,
|
|
"duration": duration,
|
|
},
|
|
)
|
|
|
|
return response
|