1
0

test_client_ips.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from mock import Mock
  17. from twisted.internet import defer
  18. from synapse.http.site import XForwardedForRequest
  19. from synapse.rest.client.v1 import admin, login
  20. from tests import unittest
  21. class ClientIpStoreTestCase(unittest.HomeserverTestCase):
  22. def make_homeserver(self, reactor, clock):
  23. hs = self.setup_test_homeserver()
  24. return hs
  25. def prepare(self, hs, reactor, clock):
  26. self.store = self.hs.get_datastore()
  27. def test_insert_new_client_ip(self):
  28. self.reactor.advance(12345678)
  29. user_id = "@user:id"
  30. self.get_success(
  31. self.store.insert_client_ip(
  32. user_id, "access_token", "ip", "user_agent", "device_id"
  33. )
  34. )
  35. # Trigger the storage loop
  36. self.reactor.advance(10)
  37. result = self.get_success(
  38. self.store.get_last_client_ip_by_device(user_id, "device_id")
  39. )
  40. r = result[(user_id, "device_id")]
  41. self.assertDictContainsSubset(
  42. {
  43. "user_id": user_id,
  44. "device_id": "device_id",
  45. "access_token": "access_token",
  46. "ip": "ip",
  47. "user_agent": "user_agent",
  48. "last_seen": 12345678000,
  49. },
  50. r,
  51. )
  52. def test_insert_new_client_ip_none_device_id(self):
  53. """
  54. An insert with a device ID of NULL will not create a new entry, but
  55. update an existing entry in the user_ips table.
  56. """
  57. self.reactor.advance(12345678)
  58. user_id = "@user:id"
  59. # Add & trigger the storage loop
  60. self.get_success(
  61. self.store.insert_client_ip(
  62. user_id, "access_token", "ip", "user_agent", None
  63. )
  64. )
  65. self.reactor.advance(200)
  66. self.pump(0)
  67. result = self.get_success(
  68. self.store._simple_select_list(
  69. table="user_ips",
  70. keyvalues={"user_id": user_id},
  71. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  72. desc="get_user_ip_and_agents",
  73. )
  74. )
  75. self.assertEqual(
  76. result,
  77. [
  78. {
  79. 'access_token': 'access_token',
  80. 'ip': 'ip',
  81. 'user_agent': 'user_agent',
  82. 'device_id': None,
  83. 'last_seen': 12345678000,
  84. }
  85. ],
  86. )
  87. # Add another & trigger the storage loop
  88. self.get_success(
  89. self.store.insert_client_ip(
  90. user_id, "access_token", "ip", "user_agent", None
  91. )
  92. )
  93. self.reactor.advance(10)
  94. self.pump(0)
  95. result = self.get_success(
  96. self.store._simple_select_list(
  97. table="user_ips",
  98. keyvalues={"user_id": user_id},
  99. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  100. desc="get_user_ip_and_agents",
  101. )
  102. )
  103. # Only one result, has been upserted.
  104. self.assertEqual(
  105. result,
  106. [
  107. {
  108. 'access_token': 'access_token',
  109. 'ip': 'ip',
  110. 'user_agent': 'user_agent',
  111. 'device_id': None,
  112. 'last_seen': 12345878000,
  113. }
  114. ],
  115. )
  116. def test_disabled_monthly_active_user(self):
  117. self.hs.config.limit_usage_by_mau = False
  118. self.hs.config.max_mau_value = 50
  119. user_id = "@user:server"
  120. self.get_success(
  121. self.store.insert_client_ip(
  122. user_id, "access_token", "ip", "user_agent", "device_id"
  123. )
  124. )
  125. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  126. self.assertFalse(active)
  127. def test_adding_monthly_active_user_when_full(self):
  128. self.hs.config.limit_usage_by_mau = True
  129. self.hs.config.max_mau_value = 50
  130. lots_of_users = 100
  131. user_id = "@user:server"
  132. self.store.get_monthly_active_count = Mock(
  133. return_value=defer.succeed(lots_of_users)
  134. )
  135. self.get_success(
  136. self.store.insert_client_ip(
  137. user_id, "access_token", "ip", "user_agent", "device_id"
  138. )
  139. )
  140. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  141. self.assertFalse(active)
  142. def test_adding_monthly_active_user_when_space(self):
  143. self.hs.config.limit_usage_by_mau = True
  144. self.hs.config.max_mau_value = 50
  145. user_id = "@user:server"
  146. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  147. self.assertFalse(active)
  148. # Trigger the saving loop
  149. self.reactor.advance(10)
  150. self.get_success(
  151. self.store.insert_client_ip(
  152. user_id, "access_token", "ip", "user_agent", "device_id"
  153. )
  154. )
  155. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  156. self.assertTrue(active)
  157. def test_updating_monthly_active_user_when_space(self):
  158. self.hs.config.limit_usage_by_mau = True
  159. self.hs.config.max_mau_value = 50
  160. user_id = "@user:server"
  161. self.get_success(
  162. self.store.register(user_id=user_id, token="123", password_hash=None)
  163. )
  164. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  165. self.assertFalse(active)
  166. # Trigger the saving loop
  167. self.reactor.advance(10)
  168. self.get_success(
  169. self.store.insert_client_ip(
  170. user_id, "access_token", "ip", "user_agent", "device_id"
  171. )
  172. )
  173. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  174. self.assertTrue(active)
  175. class ClientIpAuthTestCase(unittest.HomeserverTestCase):
  176. servlets = [admin.register_servlets, login.register_servlets]
  177. def make_homeserver(self, reactor, clock):
  178. hs = self.setup_test_homeserver()
  179. return hs
  180. def prepare(self, hs, reactor, clock):
  181. self.store = self.hs.get_datastore()
  182. self.user_id = self.register_user("bob", "abc123", True)
  183. def test_request_with_xforwarded(self):
  184. """
  185. The IP in X-Forwarded-For is entered into the client IPs table.
  186. """
  187. self._runtest(
  188. {b"X-Forwarded-For": b"127.9.0.1"},
  189. "127.9.0.1",
  190. {"request": XForwardedForRequest},
  191. )
  192. def test_request_from_getPeer(self):
  193. """
  194. The IP returned by getPeer is entered into the client IPs table, if
  195. there's no X-Forwarded-For header.
  196. """
  197. self._runtest({}, "127.0.0.1", {})
  198. def _runtest(self, headers, expected_ip, make_request_args):
  199. device_id = "bleb"
  200. access_token = self.login("bob", "abc123", device_id=device_id)
  201. # Advance to a known time
  202. self.reactor.advance(123456 - self.reactor.seconds())
  203. request, channel = self.make_request(
  204. "GET",
  205. "/_matrix/client/r0/admin/users/" + self.user_id,
  206. access_token=access_token,
  207. **make_request_args
  208. )
  209. request.requestHeaders.addRawHeader(b"User-Agent", b"Mozzila pizza")
  210. # Add the optional headers
  211. for h, v in headers.items():
  212. request.requestHeaders.addRawHeader(h, v)
  213. self.render(request)
  214. # Advance so the save loop occurs
  215. self.reactor.advance(100)
  216. result = self.get_success(
  217. self.store.get_last_client_ip_by_device(self.user_id, device_id)
  218. )
  219. r = result[(self.user_id, device_id)]
  220. self.assertDictContainsSubset(
  221. {
  222. "user_id": self.user_id,
  223. "device_id": device_id,
  224. "ip": expected_ip,
  225. "user_agent": "Mozzila pizza",
  226. "last_seen": 123456100,
  227. },
  228. r,
  229. )