Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 92103cb

Browse files
authored
Decouple synapse.api.auth_blocking.AuthBlocking from synapse.api.auth.Auth. (#13021)
1 parent a164a46 commit 92103cb

File tree

14 files changed

+63
-50
lines changed

14 files changed

+63
-50
lines changed

changelog.d/13021.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Decouple `synapse.api.auth_blocking.AuthBlocking` from `synapse.api.auth.Auth`.

synapse/api/auth.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from twisted.web.server import Request
2121

2222
from synapse import event_auth
23-
from synapse.api.auth_blocking import AuthBlocking
2423
from synapse.api.constants import EventTypes, HistoryVisibility, Membership
2524
from synapse.api.errors import (
2625
AuthError,
@@ -67,8 +66,6 @@ def __init__(self, hs: "HomeServer"):
6766
10000, "token_cache"
6867
)
6968

70-
self._auth_blocking = AuthBlocking(self.hs)
71-
7269
self._track_appservice_user_ips = hs.config.appservice.track_appservice_user_ips
7370
self._track_puppeted_user_ips = hs.config.api.track_puppeted_user_ips
7471
self._macaroon_secret_key = hs.config.key.macaroon_secret_key
@@ -711,14 +708,3 @@ async def check_user_in_room_or_world_readable(
711708
"User %s not in room %s, and room previews are disabled"
712709
% (user_id, room_id),
713710
)
714-
715-
async def check_auth_blocking(
716-
self,
717-
user_id: Optional[str] = None,
718-
threepid: Optional[dict] = None,
719-
user_type: Optional[str] = None,
720-
requester: Optional[Requester] = None,
721-
) -> None:
722-
await self._auth_blocking.check_auth_blocking(
723-
user_id=user_id, threepid=threepid, user_type=user_type, requester=requester
724-
)

synapse/handlers/auth.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class AuthHandler:
199199
def __init__(self, hs: "HomeServer"):
200200
self.store = hs.get_datastores().main
201201
self.auth = hs.get_auth()
202+
self.auth_blocking = hs.get_auth_blocking()
202203
self.clock = hs.get_clock()
203204
self.checkers: Dict[str, UserInteractiveAuthChecker] = {}
204205
for auth_checker_class in INTERACTIVE_AUTH_CHECKERS:
@@ -985,7 +986,7 @@ async def create_access_token_for_user_id(
985986
not is_appservice_ghost
986987
or self.hs.config.appservice.track_appservice_user_ips
987988
):
988-
await self.auth.check_auth_blocking(user_id)
989+
await self.auth_blocking.check_auth_blocking(user_id)
989990

990991
access_token = self.generate_access_token(target_user_id_obj)
991992
await self.store.add_access_token_to_user(
@@ -1439,7 +1440,7 @@ async def validate_short_term_login_token(
14391440
except Exception:
14401441
raise AuthError(403, "Invalid login token", errcode=Codes.FORBIDDEN)
14411442

1442-
await self.auth.check_auth_blocking(res.user_id)
1443+
await self.auth_blocking.check_auth_blocking(res.user_id)
14431444
return res
14441445

14451446
async def delete_access_token(self, access_token: str) -> None:

synapse/handlers/message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ async def _expire_event(self, event_id: str) -> None:
444444
class EventCreationHandler:
445445
def __init__(self, hs: "HomeServer"):
446446
self.hs = hs
447-
self.auth = hs.get_auth()
447+
self.auth_blocking = hs.get_auth_blocking()
448448
self._event_auth_handler = hs.get_event_auth_handler()
449449
self.store = hs.get_datastores().main
450450
self._storage_controllers = hs.get_storage_controllers()
@@ -605,7 +605,7 @@ async def create_event(
605605
Returns:
606606
Tuple of created event, Context
607607
"""
608-
await self.auth.check_auth_blocking(requester=requester)
608+
await self.auth_blocking.check_auth_blocking(requester=requester)
609609

610610
if event_dict["type"] == EventTypes.Create and event_dict["state_key"] == "":
611611
room_version_id = event_dict["content"]["room_version"]

synapse/handlers/register.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def __init__(self, hs: "HomeServer"):
9191
self.clock = hs.get_clock()
9292
self.hs = hs
9393
self.auth = hs.get_auth()
94+
self.auth_blocking = hs.get_auth_blocking()
9495
self._auth_handler = hs.get_auth_handler()
9596
self.profile_handler = hs.get_profile_handler()
9697
self.user_directory_handler = hs.get_user_directory_handler()
@@ -276,7 +277,7 @@ async def register_user(
276277

277278
# do not check_auth_blocking if the call is coming through the Admin API
278279
if not by_admin:
279-
await self.auth.check_auth_blocking(threepid=threepid)
280+
await self.auth_blocking.check_auth_blocking(threepid=threepid)
280281

281282
if localpart is not None:
282283
await self.check_username(localpart, guest_access_token=guest_access_token)

synapse/handlers/room.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def __init__(self, hs: "HomeServer"):
110110
self.store = hs.get_datastores().main
111111
self._storage_controllers = hs.get_storage_controllers()
112112
self.auth = hs.get_auth()
113+
self.auth_blocking = hs.get_auth_blocking()
113114
self.clock = hs.get_clock()
114115
self.hs = hs
115116
self.spam_checker = hs.get_spam_checker()
@@ -706,7 +707,7 @@ async def create_room(
706707
"""
707708
user_id = requester.user.to_string()
708709

709-
await self.auth.check_auth_blocking(requester=requester)
710+
await self.auth_blocking.check_auth_blocking(requester=requester)
710711

711712
if (
712713
self._server_notices_mxid is not None

synapse/handlers/sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def __init__(self, hs: "HomeServer"):
237237
self.event_sources = hs.get_event_sources()
238238
self.clock = hs.get_clock()
239239
self.state = hs.get_state_handler()
240-
self.auth = hs.get_auth()
240+
self.auth_blocking = hs.get_auth_blocking()
241241
self._storage_controllers = hs.get_storage_controllers()
242242
self._state_storage_controller = self._storage_controllers.state
243243

@@ -280,7 +280,7 @@ async def wait_for_sync_for_user(
280280
# not been exceeded (if not part of the group by this point, almost certain
281281
# auth_blocking will occur)
282282
user_id = sync_config.user.to_string()
283-
await self.auth.check_auth_blocking(requester=requester)
283+
await self.auth_blocking.check_auth_blocking(requester=requester)
284284

285285
res = await self.response_cache.wrap(
286286
sync_config.request_key,

synapse/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from twisted.web.resource import Resource
3030

3131
from synapse.api.auth import Auth
32+
from synapse.api.auth_blocking import AuthBlocking
3233
from synapse.api.filtering import Filtering
3334
from synapse.api.ratelimiting import Ratelimiter, RequestRatelimiter
3435
from synapse.appservice.api import ApplicationServiceApi
@@ -379,6 +380,10 @@ def get_notifier(self) -> Notifier:
379380
def get_auth(self) -> Auth:
380381
return Auth(self)
381382

383+
@cache_in_self
384+
def get_auth_blocking(self) -> AuthBlocking:
385+
return AuthBlocking(self)
386+
382387
@cache_in_self
383388
def get_http_client_context_factory(self) -> IPolicyForHTTPS:
384389
if self.config.tls.use_insecure_ssl_client_just_for_testing_do_not_use:

synapse/server_notices/resource_limits_server_notices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, hs: "HomeServer"):
3737
self._server_notices_manager = hs.get_server_notices_manager()
3838
self._store = hs.get_datastores().main
3939
self._storage_controllers = hs.get_storage_controllers()
40-
self._auth = hs.get_auth()
40+
self._auth_blocking = hs.get_auth_blocking()
4141
self._config = hs.config
4242
self._resouce_limited = False
4343
self._account_data_handler = hs.get_account_data_handler()
@@ -91,7 +91,7 @@ async def maybe_send_server_notice_to_user(self, user_id: str) -> None:
9191
# Normally should always pass in user_id to check_auth_blocking
9292
# if you have it, but in this case are checking what would happen
9393
# to other users if they were to arrive.
94-
await self._auth.check_auth_blocking()
94+
await self._auth_blocking.check_auth_blocking()
9595
except ResourceLimitError as e:
9696
limit_msg = e.msg
9797
limit_type = e.limit_type

tests/api/test_auth.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from twisted.test.proto_helpers import MemoryReactor
2020

2121
from synapse.api.auth import Auth
22+
from synapse.api.auth_blocking import AuthBlocking
2223
from synapse.api.constants import UserTypes
2324
from synapse.api.errors import (
2425
AuthError,
@@ -49,7 +50,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
4950

5051
# AuthBlocking reads from the hs' config on initialization. We need to
5152
# modify its config instead of the hs'
52-
self.auth_blocking = self.auth._auth_blocking
53+
self.auth_blocking = AuthBlocking(hs)
5354

5455
self.test_user = "@foo:bar"
5556
self.test_token = b"_test_token_"
@@ -362,36 +363,41 @@ def test_blocking_mau(self):
362363
small_number_of_users = 1
363364

364365
# Ensure no error thrown
365-
self.get_success(self.auth.check_auth_blocking())
366+
self.get_success(self.auth_blocking.check_auth_blocking())
366367

367368
self.auth_blocking._limit_usage_by_mau = True
368369

369370
self.store.get_monthly_active_count = simple_async_mock(lots_of_users)
370371

371-
e = self.get_failure(self.auth.check_auth_blocking(), ResourceLimitError)
372+
e = self.get_failure(
373+
self.auth_blocking.check_auth_blocking(), ResourceLimitError
374+
)
372375
self.assertEqual(e.value.admin_contact, self.hs.config.server.admin_contact)
373376
self.assertEqual(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
374377
self.assertEqual(e.value.code, 403)
375378

376379
# Ensure does not throw an error
377380
self.store.get_monthly_active_count = simple_async_mock(small_number_of_users)
378-
self.get_success(self.auth.check_auth_blocking())
381+
self.get_success(self.auth_blocking.check_auth_blocking())
379382

380383
def test_blocking_mau__depending_on_user_type(self):
381384
self.auth_blocking._max_mau_value = 50
382385
self.auth_blocking._limit_usage_by_mau = True
383386

384387
self.store.get_monthly_active_count = simple_async_mock(100)
385388
# Support users allowed
386-
self.get_success(self.auth.check_auth_blocking(user_type=UserTypes.SUPPORT))
389+
self.get_success(
390+
self.auth_blocking.check_auth_blocking(user_type=UserTypes.SUPPORT)
391+
)
387392
self.store.get_monthly_active_count = simple_async_mock(100)
388393
# Bots not allowed
389394
self.get_failure(
390-
self.auth.check_auth_blocking(user_type=UserTypes.BOT), ResourceLimitError
395+
self.auth_blocking.check_auth_blocking(user_type=UserTypes.BOT),
396+
ResourceLimitError,
391397
)
392398
self.store.get_monthly_active_count = simple_async_mock(100)
393399
# Real users not allowed
394-
self.get_failure(self.auth.check_auth_blocking(), ResourceLimitError)
400+
self.get_failure(self.auth_blocking.check_auth_blocking(), ResourceLimitError)
395401

396402
def test_blocking_mau__appservice_requester_allowed_when_not_tracking_ips(self):
397403
self.auth_blocking._max_mau_value = 50
@@ -419,7 +425,7 @@ def test_blocking_mau__appservice_requester_allowed_when_not_tracking_ips(self):
419425
app_service=appservice,
420426
authenticated_entity="@appservice:server",
421427
)
422-
self.get_success(self.auth.check_auth_blocking(requester=requester))
428+
self.get_success(self.auth_blocking.check_auth_blocking(requester=requester))
423429

424430
def test_blocking_mau__appservice_requester_disallowed_when_tracking_ips(self):
425431
self.auth_blocking._max_mau_value = 50
@@ -448,7 +454,8 @@ def test_blocking_mau__appservice_requester_disallowed_when_tracking_ips(self):
448454
authenticated_entity="@appservice:server",
449455
)
450456
self.get_failure(
451-
self.auth.check_auth_blocking(requester=requester), ResourceLimitError
457+
self.auth_blocking.check_auth_blocking(requester=requester),
458+
ResourceLimitError,
452459
)
453460

454461
def test_reserved_threepid(self):
@@ -459,18 +466,21 @@ def test_reserved_threepid(self):
459466
unknown_threepid = {"medium": "email", "address": "[email protected]"}
460467
self.auth_blocking._mau_limits_reserved_threepids = [threepid]
461468

462-
self.get_failure(self.auth.check_auth_blocking(), ResourceLimitError)
469+
self.get_failure(self.auth_blocking.check_auth_blocking(), ResourceLimitError)
463470

464471
self.get_failure(
465-
self.auth.check_auth_blocking(threepid=unknown_threepid), ResourceLimitError
472+
self.auth_blocking.check_auth_blocking(threepid=unknown_threepid),
473+
ResourceLimitError,
466474
)
467475

468-
self.get_success(self.auth.check_auth_blocking(threepid=threepid))
476+
self.get_success(self.auth_blocking.check_auth_blocking(threepid=threepid))
469477

470478
def test_hs_disabled(self):
471479
self.auth_blocking._hs_disabled = True
472480
self.auth_blocking._hs_disabled_message = "Reason for being disabled"
473-
e = self.get_failure(self.auth.check_auth_blocking(), ResourceLimitError)
481+
e = self.get_failure(
482+
self.auth_blocking.check_auth_blocking(), ResourceLimitError
483+
)
474484
self.assertEqual(e.value.admin_contact, self.hs.config.server.admin_contact)
475485
self.assertEqual(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
476486
self.assertEqual(e.value.code, 403)
@@ -485,7 +495,9 @@ def test_hs_disabled_no_server_notices_user(self):
485495

486496
self.auth_blocking._hs_disabled = True
487497
self.auth_blocking._hs_disabled_message = "Reason for being disabled"
488-
e = self.get_failure(self.auth.check_auth_blocking(), ResourceLimitError)
498+
e = self.get_failure(
499+
self.auth_blocking.check_auth_blocking(), ResourceLimitError
500+
)
489501
self.assertEqual(e.value.admin_contact, self.hs.config.server.admin_contact)
490502
self.assertEqual(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
491503
self.assertEqual(e.value.code, 403)
@@ -495,4 +507,4 @@ def test_server_notices_mxid_special_cased(self):
495507
user = "@user:server"
496508
self.auth_blocking._server_notices_mxid = user
497509
self.auth_blocking._hs_disabled_message = "Reason for being disabled"
498-
self.get_success(self.auth.check_auth_blocking(user))
510+
self.get_success(self.auth_blocking.check_auth_blocking(user))

tests/handlers/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3838
# MAU tests
3939
# AuthBlocking reads from the hs' config on initialization. We need to
4040
# modify its config instead of the hs'
41-
self.auth_blocking = hs.get_auth()._auth_blocking
41+
self.auth_blocking = hs.get_auth_blocking()
4242
self.auth_blocking._max_mau_value = 50
4343

4444
self.small_number_of_users = 1

tests/handlers/test_register.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ async def get_or_create_user(
699699
"""
700700
if localpart is None:
701701
raise SynapseError(400, "Request must include user id")
702-
await self.hs.get_auth().check_auth_blocking()
702+
await self.hs.get_auth_blocking().check_auth_blocking()
703703
need_register = True
704704

705705
try:

tests/handlers/test_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def prepare(self, reactor, clock, hs: HomeServer):
4545

4646
# AuthBlocking reads from the hs' config on initialization. We need to
4747
# modify its config instead of the hs'
48-
self.auth_blocking = self.hs.get_auth()._auth_blocking
48+
self.auth_blocking = self.hs.get_auth_blocking()
4949

5050
def test_wait_for_sync_for_user_auth_blocking(self):
5151
user_id1 = "@user1:test"

tests/server_notices/test_resource_limits_server_notices.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def test_maybe_send_server_notice_to_user_flag_off(self):
9696
def test_maybe_send_server_notice_to_user_remove_blocked_notice(self):
9797
"""Test when user has blocked notice, but should have it removed"""
9898

99-
self._rlsn._auth.check_auth_blocking = Mock(return_value=make_awaitable(None))
99+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
100+
return_value=make_awaitable(None)
101+
)
100102
mock_event = Mock(
101103
type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
102104
)
@@ -112,7 +114,7 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):
112114
"""
113115
Test when user has blocked notice, but notice ought to be there (NOOP)
114116
"""
115-
self._rlsn._auth.check_auth_blocking = Mock(
117+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
116118
return_value=make_awaitable(None),
117119
side_effect=ResourceLimitError(403, "foo"),
118120
)
@@ -132,7 +134,7 @@ def test_maybe_send_server_notice_to_user_add_blocked_notice(self):
132134
"""
133135
Test when user does not have blocked notice, but should have one
134136
"""
135-
self._rlsn._auth.check_auth_blocking = Mock(
137+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
136138
return_value=make_awaitable(None),
137139
side_effect=ResourceLimitError(403, "foo"),
138140
)
@@ -145,7 +147,9 @@ def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self):
145147
"""
146148
Test when user does not have blocked notice, nor should they (NOOP)
147149
"""
148-
self._rlsn._auth.check_auth_blocking = Mock(return_value=make_awaitable(None))
150+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
151+
return_value=make_awaitable(None)
152+
)
149153

150154
self.get_success(self._rlsn.maybe_send_server_notice_to_user(self.user_id))
151155

@@ -156,7 +160,9 @@ def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
156160
Test when user is not part of the MAU cohort - this should not ever
157161
happen - but ...
158162
"""
159-
self._rlsn._auth.check_auth_blocking = Mock(return_value=make_awaitable(None))
163+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
164+
return_value=make_awaitable(None)
165+
)
160166
self._rlsn._store.user_last_seen_monthly_active = Mock(
161167
return_value=make_awaitable(None)
162168
)
@@ -170,7 +176,7 @@ def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(self):
170176
Test that when server is over MAU limit and alerting is suppressed, then
171177
an alert message is not sent into the room
172178
"""
173-
self._rlsn._auth.check_auth_blocking = Mock(
179+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
174180
return_value=make_awaitable(None),
175181
side_effect=ResourceLimitError(
176182
403, "foo", limit_type=LimitBlockingTypes.MONTHLY_ACTIVE_USER
@@ -185,7 +191,7 @@ def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self):
185191
"""
186192
Test that when a server is disabled, that MAU limit alerting is ignored.
187193
"""
188-
self._rlsn._auth.check_auth_blocking = Mock(
194+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
189195
return_value=make_awaitable(None),
190196
side_effect=ResourceLimitError(
191197
403, "foo", limit_type=LimitBlockingTypes.HS_DISABLED
@@ -202,7 +208,7 @@ def test_maybe_send_server_notice_when_alerting_suppressed_room_blocked(self):
202208
When the room is already in a blocked state, test that when alerting
203209
is suppressed that the room is returned to an unblocked state.
204210
"""
205-
self._rlsn._auth.check_auth_blocking = Mock(
211+
self._rlsn._auth_blocking.check_auth_blocking = Mock(
206212
return_value=make_awaitable(None),
207213
side_effect=ResourceLimitError(
208214
403, "foo", limit_type=LimitBlockingTypes.MONTHLY_ACTIVE_USER

0 commit comments

Comments
 (0)