request: Refactor to store requestor_for_logs in ZulipRequestNotes.

This commit is contained in:
PIG208 2021-07-09 19:25:36 +08:00 committed by Tim Abbott
parent 3f9a5e1e17
commit 5475334b16
6 changed files with 24 additions and 23 deletions

View File

@ -39,7 +39,7 @@ from zerver.lib.exceptions import (
)
from zerver.lib.queue import queue_json_publish
from zerver.lib.rate_limiter import RateLimited, RateLimitedIPAddr, RateLimitedUser
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.request import REQ, get_request_notes, has_request_variables
from zerver.lib.response import json_method_not_allowed, json_success, json_unauthorized
from zerver.lib.subdomains import get_subdomain, user_matches_subdomain
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
@ -432,7 +432,7 @@ def do_login(request: HttpRequest, user_profile: UserProfile) -> None:
and also adds helpful data needed by our server logs.
"""
django_login(request, user_profile)
request._requestor_for_logs = user_profile.format_requestor_for_logs()
get_request_notes(request).requestor_for_logs = user_profile.format_requestor_for_logs()
process_client(request, user_profile, is_browser_view=True)
if settings.TWO_FACTOR_AUTHENTICATION_ENABLED:
# Log in with two factor authentication as well.
@ -815,7 +815,8 @@ def internal_notify_view(is_tornado_view: bool) -> Callable[[ViewFuncT], ViewFun
raise RuntimeError("Tornado notify view called with no Tornado handler")
if not is_tornado_view and is_tornado_request:
raise RuntimeError("Django notify view called with Tornado handler")
request._requestor_for_logs = "internal"
request_notes = get_request_notes(request)
request_notes.requestor_for_logs = "internal"
return view_func(request, *args, **kwargs)
return _wrapped_func_arguments

View File

@ -394,9 +394,9 @@ class LogRequests(MiddlewareMixin):
remote_ip = request.META["REMOTE_ADDR"]
# Get the requestor's identifier and client, if available.
try:
requestor_for_logs = request._requestor_for_logs
except Exception:
request_notes = get_request_notes(request)
requestor_for_logs = request_notes.requestor_for_logs
if requestor_for_logs is None:
if hasattr(request, "user") and hasattr(request.user, "format_requestor_for_logs"):
requestor_for_logs = request.user.format_requestor_for_logs()
else:
@ -413,7 +413,6 @@ class LogRequests(MiddlewareMixin):
content = response.content
content_iter = None
request_notes = get_request_notes(request)
assert request_notes.log_data is not None
write_log_line(
request_notes.log_data,

View File

@ -49,6 +49,7 @@ from zerver.lib.request import (
RequestConfusingParmsError,
RequestVariableConversionError,
RequestVariableMissingError,
get_request_notes,
has_request_variables,
)
from zerver.lib.response import json_response, json_success
@ -1521,27 +1522,27 @@ class TestInternalNotifyView(ZulipTestCase):
def test_valid_internal_requests(self) -> None:
secret = "random"
req: HttpRequest = self.Request(
request: HttpRequest = self.Request(
POST=dict(secret=secret),
META=dict(REMOTE_ADDR="127.0.0.1"),
)
with self.settings(SHARED_SECRET=secret):
self.assertTrue(authenticate_notify(req))
self.assertEqual(self.internal_notify(False, req), self.BORING_RESULT)
self.assertEqual(req._requestor_for_logs, "internal")
self.assertTrue(authenticate_notify(request))
self.assertEqual(self.internal_notify(False, request), self.BORING_RESULT)
self.assertEqual(get_request_notes(request).requestor_for_logs, "internal")
with self.assertRaises(RuntimeError):
self.internal_notify(True, req)
self.internal_notify(True, request)
req._tornado_handler = "set"
request._tornado_handler = "set"
with self.settings(SHARED_SECRET=secret):
self.assertTrue(authenticate_notify(req))
self.assertEqual(self.internal_notify(True, req), self.BORING_RESULT)
self.assertEqual(req._requestor_for_logs, "internal")
self.assertTrue(authenticate_notify(request))
self.assertEqual(self.internal_notify(True, request), self.BORING_RESULT)
self.assertEqual(get_request_notes(request).requestor_for_logs, "internal")
with self.assertRaises(RuntimeError):
self.internal_notify(False, req)
self.internal_notify(False, request)
def test_internal_requests_with_broken_secret(self) -> None:
secret = "random"

View File

@ -237,8 +237,8 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
request_notes.log_data = old_request_notes.log_data
if request_notes.rate_limit is not None:
request_notes.rate_limit = old_request_notes.rate_limit
if hasattr(request, "_requestor_for_logs"):
request._requestor_for_logs = old_request._requestor_for_logs
if request_notes.requestor_for_logs is not None:
request_notes.requestor_for_logs = old_request_notes.requestor_for_logs
request.user = old_request.user
request.client = old_request.client
request.client_name = old_request.client_name

View File

@ -50,7 +50,7 @@ def get_events_internal(
request: HttpRequest, user_profile_id: int = REQ(json_validator=check_int)
) -> HttpResponse:
user_profile = get_user_profile_by_id(user_profile_id)
request._requestor_for_logs = user_profile.format_requestor_for_logs()
get_request_notes(request).requestor_for_logs = user_profile.format_requestor_for_logs()
process_client(request, user_profile, client_name="internal")
return get_events_backend(request, user_profile)

View File

@ -53,7 +53,7 @@ from zerver.lib.mobile_auth_otp import otp_encrypt_api_key
from zerver.lib.push_notifications import push_notifications_enabled
from zerver.lib.pysa import mark_sanitized
from zerver.lib.realm_icon import realm_icon_url
from zerver.lib.request import REQ, JsonableError, has_request_variables
from zerver.lib.request import REQ, JsonableError, get_request_notes, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.sessions import set_expirable_session_var
from zerver.lib.subdomains import get_subdomain, is_subdomain_root_or_alias
@ -354,7 +354,7 @@ def finish_mobile_flow(request: HttpRequest, user_profile: UserProfile, otp: str
# Mark this request as having a logged-in user for our server logs.
process_client(request, user_profile)
request._requestor_for_logs = user_profile.format_requestor_for_logs()
get_request_notes(request).requestor_for_logs = user_profile.format_requestor_for_logs()
return response
@ -859,7 +859,7 @@ def api_fetch_api_key(
# Mark this request as having a logged-in user for our server logs.
process_client(request, user_profile)
request._requestor_for_logs = user_profile.format_requestor_for_logs()
get_request_notes(request).requestor_for_logs = user_profile.format_requestor_for_logs()
api_key = get_api_key(user_profile)
return json_success({"api_key": api_key, "email": user_profile.delivery_email})