mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2025-08-03 22:54:11 -04:00
Stop Auth methods from polling the config on every req. (#7420)
This commit is contained in:
parent
b26f3e582c
commit
aee9130a83
7 changed files with 168 additions and 106 deletions
|
@ -52,6 +52,10 @@ class AuthTestCase(unittest.TestCase):
|
|||
self.hs.handlers = TestHandlers(self.hs)
|
||||
self.auth = Auth(self.hs)
|
||||
|
||||
# AuthBlocking reads from the hs' config on initialization. We need to
|
||||
# modify its config instead of the hs'
|
||||
self.auth_blocking = self.auth._auth_blocking
|
||||
|
||||
self.test_user = "@foo:bar"
|
||||
self.test_token = b"_test_token_"
|
||||
|
||||
|
@ -321,15 +325,15 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_blocking_mau(self):
|
||||
self.hs.config.limit_usage_by_mau = False
|
||||
self.hs.config.max_mau_value = 50
|
||||
self.auth_blocking._limit_usage_by_mau = False
|
||||
self.auth_blocking._max_mau_value = 50
|
||||
lots_of_users = 100
|
||||
small_number_of_users = 1
|
||||
|
||||
# Ensure no error thrown
|
||||
yield defer.ensureDeferred(self.auth.check_auth_blocking())
|
||||
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
|
||||
self.store.get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(lots_of_users)
|
||||
|
@ -349,8 +353,8 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_blocking_mau__depending_on_user_type(self):
|
||||
self.hs.config.max_mau_value = 50
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.auth_blocking._max_mau_value = 50
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
|
||||
self.store.get_monthly_active_count = Mock(return_value=defer.succeed(100))
|
||||
# Support users allowed
|
||||
|
@ -370,12 +374,12 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_reserved_threepid(self):
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.hs.config.max_mau_value = 1
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
self.auth_blocking._max_mau_value = 1
|
||||
self.store.get_monthly_active_count = lambda: defer.succeed(2)
|
||||
threepid = {"medium": "email", "address": "reserved@server.com"}
|
||||
unknown_threepid = {"medium": "email", "address": "unreserved@server.com"}
|
||||
self.hs.config.mau_limits_reserved_threepids = [threepid]
|
||||
self.auth_blocking._mau_limits_reserved_threepids = [threepid]
|
||||
|
||||
with self.assertRaises(ResourceLimitError):
|
||||
yield defer.ensureDeferred(self.auth.check_auth_blocking())
|
||||
|
@ -389,8 +393,8 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_hs_disabled(self):
|
||||
self.hs.config.hs_disabled = True
|
||||
self.hs.config.hs_disabled_message = "Reason for being disabled"
|
||||
self.auth_blocking._hs_disabled = True
|
||||
self.auth_blocking._hs_disabled_message = "Reason for being disabled"
|
||||
with self.assertRaises(ResourceLimitError) as e:
|
||||
yield defer.ensureDeferred(self.auth.check_auth_blocking())
|
||||
self.assertEquals(e.exception.admin_contact, self.hs.config.admin_contact)
|
||||
|
@ -404,10 +408,10 @@ class AuthTestCase(unittest.TestCase):
|
|||
"""
|
||||
# this should be the default, but we had a bug where the test was doing the wrong
|
||||
# thing, so let's make it explicit
|
||||
self.hs.config.server_notices_mxid = None
|
||||
self.auth_blocking._server_notices_mxid = None
|
||||
|
||||
self.hs.config.hs_disabled = True
|
||||
self.hs.config.hs_disabled_message = "Reason for being disabled"
|
||||
self.auth_blocking._hs_disabled = True
|
||||
self.auth_blocking._hs_disabled_message = "Reason for being disabled"
|
||||
with self.assertRaises(ResourceLimitError) as e:
|
||||
yield defer.ensureDeferred(self.auth.check_auth_blocking())
|
||||
self.assertEquals(e.exception.admin_contact, self.hs.config.admin_contact)
|
||||
|
@ -416,8 +420,8 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_server_notices_mxid_special_cased(self):
|
||||
self.hs.config.hs_disabled = True
|
||||
self.auth_blocking._hs_disabled = True
|
||||
user = "@user:server"
|
||||
self.hs.config.server_notices_mxid = user
|
||||
self.hs.config.hs_disabled_message = "Reason for being disabled"
|
||||
self.auth_blocking._server_notices_mxid = user
|
||||
self.auth_blocking._hs_disabled_message = "Reason for being disabled"
|
||||
yield defer.ensureDeferred(self.auth.check_auth_blocking(user))
|
||||
|
|
|
@ -39,8 +39,13 @@ class AuthTestCase(unittest.TestCase):
|
|||
self.hs.handlers = AuthHandlers(self.hs)
|
||||
self.auth_handler = self.hs.handlers.auth_handler
|
||||
self.macaroon_generator = self.hs.get_macaroon_generator()
|
||||
|
||||
# MAU tests
|
||||
self.hs.config.max_mau_value = 50
|
||||
# AuthBlocking reads from the hs' config on initialization. We need to
|
||||
# modify its config instead of the hs'
|
||||
self.auth_blocking = self.hs.get_auth()._auth_blocking
|
||||
self.auth_blocking._max_mau_value = 50
|
||||
|
||||
self.small_number_of_users = 1
|
||||
self.large_number_of_users = 100
|
||||
|
||||
|
@ -119,7 +124,7 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_mau_limits_disabled(self):
|
||||
self.hs.config.limit_usage_by_mau = False
|
||||
self.auth_blocking._limit_usage_by_mau = False
|
||||
# Ensure does not throw exception
|
||||
yield defer.ensureDeferred(
|
||||
self.auth_handler.get_access_token_for_user_id(
|
||||
|
@ -135,7 +140,7 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_mau_limits_exceeded_large(self):
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
self.hs.get_datastore().get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(self.large_number_of_users)
|
||||
)
|
||||
|
@ -159,11 +164,11 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_mau_limits_parity(self):
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
|
||||
# If not in monthly active cohort
|
||||
self.hs.get_datastore().get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(self.hs.config.max_mau_value)
|
||||
return_value=defer.succeed(self.auth_blocking._max_mau_value)
|
||||
)
|
||||
with self.assertRaises(ResourceLimitError):
|
||||
yield defer.ensureDeferred(
|
||||
|
@ -173,7 +178,7 @@ class AuthTestCase(unittest.TestCase):
|
|||
)
|
||||
|
||||
self.hs.get_datastore().get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(self.hs.config.max_mau_value)
|
||||
return_value=defer.succeed(self.auth_blocking._max_mau_value)
|
||||
)
|
||||
with self.assertRaises(ResourceLimitError):
|
||||
yield defer.ensureDeferred(
|
||||
|
@ -186,7 +191,7 @@ class AuthTestCase(unittest.TestCase):
|
|||
return_value=defer.succeed(self.hs.get_clock().time_msec())
|
||||
)
|
||||
self.hs.get_datastore().get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(self.hs.config.max_mau_value)
|
||||
return_value=defer.succeed(self.auth_blocking._max_mau_value)
|
||||
)
|
||||
yield defer.ensureDeferred(
|
||||
self.auth_handler.get_access_token_for_user_id(
|
||||
|
@ -197,7 +202,7 @@ class AuthTestCase(unittest.TestCase):
|
|||
return_value=defer.succeed(self.hs.get_clock().time_msec())
|
||||
)
|
||||
self.hs.get_datastore().get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(self.hs.config.max_mau_value)
|
||||
return_value=defer.succeed(self.auth_blocking._max_mau_value)
|
||||
)
|
||||
yield defer.ensureDeferred(
|
||||
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
||||
|
@ -207,7 +212,7 @@ class AuthTestCase(unittest.TestCase):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def test_mau_limits_not_exceeded(self):
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
|
||||
self.hs.get_datastore().get_monthly_active_count = Mock(
|
||||
return_value=defer.succeed(self.small_number_of_users)
|
||||
|
|
|
@ -30,28 +30,31 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
|
|||
self.sync_handler = self.hs.get_sync_handler()
|
||||
self.store = self.hs.get_datastore()
|
||||
|
||||
def test_wait_for_sync_for_user_auth_blocking(self):
|
||||
# AuthBlocking reads from the hs' config on initialization. We need to
|
||||
# modify its config instead of the hs'
|
||||
self.auth_blocking = self.hs.get_auth()._auth_blocking
|
||||
|
||||
def test_wait_for_sync_for_user_auth_blocking(self):
|
||||
user_id1 = "@user1:test"
|
||||
user_id2 = "@user2:test"
|
||||
sync_config = self._generate_sync_config(user_id1)
|
||||
|
||||
self.reactor.advance(100) # So we get not 0 time
|
||||
self.hs.config.limit_usage_by_mau = True
|
||||
self.hs.config.max_mau_value = 1
|
||||
self.auth_blocking._limit_usage_by_mau = True
|
||||
self.auth_blocking._max_mau_value = 1
|
||||
|
||||
# Check that the happy case does not throw errors
|
||||
self.get_success(self.store.upsert_monthly_active_user(user_id1))
|
||||
self.get_success(self.sync_handler.wait_for_sync_for_user(sync_config))
|
||||
|
||||
# Test that global lock works
|
||||
self.hs.config.hs_disabled = True
|
||||
self.auth_blocking._hs_disabled = True
|
||||
e = self.get_failure(
|
||||
self.sync_handler.wait_for_sync_for_user(sync_config), ResourceLimitError
|
||||
)
|
||||
self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
||||
|
||||
self.hs.config.hs_disabled = False
|
||||
self.auth_blocking._hs_disabled = False
|
||||
|
||||
sync_config = self._generate_sync_config(user_id2)
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import json
|
|||
|
||||
from mock import Mock
|
||||
|
||||
from synapse.api.auth_blocking import AuthBlocking
|
||||
from synapse.api.constants import LoginType
|
||||
from synapse.api.errors import Codes, HttpResponseException, SynapseError
|
||||
from synapse.rest.client.v2_alpha import register, sync
|
||||
|
@ -45,11 +46,17 @@ class TestMauLimit(unittest.HomeserverTestCase):
|
|||
self.hs.config.limit_usage_by_mau = True
|
||||
self.hs.config.hs_disabled = False
|
||||
self.hs.config.max_mau_value = 2
|
||||
self.hs.config.mau_trial_days = 0
|
||||
self.hs.config.server_notices_mxid = "@server:red"
|
||||
self.hs.config.server_notices_mxid_display_name = None
|
||||
self.hs.config.server_notices_mxid_avatar_url = None
|
||||
self.hs.config.server_notices_room_name = "Test Server Notice Room"
|
||||
self.hs.config.mau_trial_days = 0
|
||||
|
||||
# AuthBlocking reads config options during hs creation. Recreate the
|
||||
# hs' copy of AuthBlocking after we've updated config values above
|
||||
self.auth_blocking = AuthBlocking(self.hs)
|
||||
self.hs.get_auth()._auth_blocking = self.auth_blocking
|
||||
|
||||
return self.hs
|
||||
|
||||
def test_simple_deny_mau(self):
|
||||
|
@ -121,6 +128,7 @@ class TestMauLimit(unittest.HomeserverTestCase):
|
|||
self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
||||
|
||||
def test_trial_users_cant_come_back(self):
|
||||
self.auth_blocking._mau_trial_days = 1
|
||||
self.hs.config.mau_trial_days = 1
|
||||
|
||||
# We should be able to register more than the limit initially
|
||||
|
@ -169,8 +177,8 @@ class TestMauLimit(unittest.HomeserverTestCase):
|
|||
self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
||||
|
||||
def test_tracked_but_not_limited(self):
|
||||
self.hs.config.max_mau_value = 1 # should not matter
|
||||
self.hs.config.limit_usage_by_mau = False
|
||||
self.auth_blocking._max_mau_value = 1 # should not matter
|
||||
self.auth_blocking._limit_usage_by_mau = False
|
||||
self.hs.config.mau_stats_only = True
|
||||
|
||||
# Simply being able to create 2 users indicates that the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue