Skip to content

Commit 45420b1

Browse files
authored
Fix force_tracing_for_users config when using MAS (#18334)
This is a copy of what we do for internal auth, and we should figure out a way to deduplicate some of this stuff: https://github.com/element-hq/synapse/blob/dd05cc55eedbf086ae224a13c9ae9f0332d96b1f/synapse/api/auth/internal.py#L62-L110
1 parent 19b0e23 commit 45420b1

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

changelog.d/18334.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `force_tracing_for_users` config when using delegated auth.

synapse/api/auth/msc3861_delegated.py

+51
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646
from synapse.http.site import SynapseRequest
4747
from synapse.logging.context import make_deferred_yieldable
48+
from synapse.logging.opentracing import active_span, force_tracing, start_active_span
4849
from synapse.types import Requester, UserID, create_requester
4950
from synapse.util import json_decoder
5051
from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
@@ -177,6 +178,7 @@ def __init__(self, hs: "HomeServer"):
177178
self._http_client = hs.get_proxied_http_client()
178179
self._hostname = hs.hostname
179180
self._admin_token: Callable[[], Optional[str]] = self._config.admin_token
181+
self._force_tracing_for_users = hs.config.tracing.force_tracing_for_users
180182

181183
# # Token Introspection Cache
182184
# This remembers what users/devices are represented by which access tokens,
@@ -363,6 +365,55 @@ async def get_user_by_req(
363365
allow_guest: bool = False,
364366
allow_expired: bool = False,
365367
allow_locked: bool = False,
368+
) -> Requester:
369+
"""Get a registered user's ID.
370+
371+
Args:
372+
request: An HTTP request with an access_token query parameter.
373+
allow_guest: If False, will raise an AuthError if the user making the
374+
request is a guest.
375+
allow_expired: If True, allow the request through even if the account
376+
is expired, or session token lifetime has ended. Note that
377+
/login will deliver access tokens regardless of expiration.
378+
379+
Returns:
380+
Resolves to the requester
381+
Raises:
382+
InvalidClientCredentialsError if no user by that token exists or the token
383+
is invalid.
384+
AuthError if access is denied for the user in the access token
385+
"""
386+
parent_span = active_span()
387+
with start_active_span("get_user_by_req"):
388+
requester = await self._wrapped_get_user_by_req(
389+
request, allow_guest, allow_expired, allow_locked
390+
)
391+
392+
if parent_span:
393+
if requester.authenticated_entity in self._force_tracing_for_users:
394+
# request tracing is enabled for this user, so we need to force it
395+
# tracing on for the parent span (which will be the servlet span).
396+
#
397+
# It's too late for the get_user_by_req span to inherit the setting,
398+
# so we also force it on for that.
399+
force_tracing()
400+
force_tracing(parent_span)
401+
parent_span.set_tag(
402+
"authenticated_entity", requester.authenticated_entity
403+
)
404+
parent_span.set_tag("user_id", requester.user.to_string())
405+
if requester.device_id is not None:
406+
parent_span.set_tag("device_id", requester.device_id)
407+
if requester.app_service is not None:
408+
parent_span.set_tag("appservice_id", requester.app_service.id)
409+
return requester
410+
411+
async def _wrapped_get_user_by_req(
412+
self,
413+
request: SynapseRequest,
414+
allow_guest: bool = False,
415+
allow_expired: bool = False,
416+
allow_locked: bool = False,
366417
) -> Requester:
367418
access_token = self.get_access_token_from_request(request)
368419

0 commit comments

Comments
 (0)