__init__.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.api.constants import EventTypes
  16. from synapse.util.caches.descriptors import cachedInlineCallbacks
  17. from synapse.types import GroupID, get_domain_from_id
  18. from twisted.internet import defer
  19. import logging
  20. import re
  21. from six import string_types
  22. logger = logging.getLogger(__name__)
  23. class ApplicationServiceState(object):
  24. DOWN = "down"
  25. UP = "up"
  26. class AppServiceTransaction(object):
  27. """Represents an application service transaction."""
  28. def __init__(self, service, id, events):
  29. self.service = service
  30. self.id = id
  31. self.events = events
  32. def send(self, as_api):
  33. """Sends this transaction using the provided AS API interface.
  34. Args:
  35. as_api(ApplicationServiceApi): The API to use to send.
  36. Returns:
  37. A Deferred which resolves to True if the transaction was sent.
  38. """
  39. return as_api.push_bulk(
  40. service=self.service,
  41. events=self.events,
  42. txn_id=self.id
  43. )
  44. def complete(self, store):
  45. """Completes this transaction as successful.
  46. Marks this transaction ID on the application service and removes the
  47. transaction contents from the database.
  48. Args:
  49. store: The database store to operate on.
  50. Returns:
  51. A Deferred which resolves to True if the transaction was completed.
  52. """
  53. return store.complete_appservice_txn(
  54. service=self.service,
  55. txn_id=self.id
  56. )
  57. class ApplicationService(object):
  58. """Defines an application service. This definition is mostly what is
  59. provided to the /register AS API.
  60. Provides methods to check if this service is "interested" in events.
  61. """
  62. NS_USERS = "users"
  63. NS_ALIASES = "aliases"
  64. NS_ROOMS = "rooms"
  65. # The ordering here is important as it is used to map database values (which
  66. # are stored as ints representing the position in this list) to namespace
  67. # values.
  68. NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
  69. def __init__(self, token, hostname, url=None, namespaces=None, hs_token=None,
  70. sender=None, id=None, protocols=None, rate_limited=True):
  71. self.token = token
  72. self.url = url
  73. self.hs_token = hs_token
  74. self.sender = sender
  75. self.server_name = hostname
  76. self.namespaces = self._check_namespaces(namespaces)
  77. self.id = id
  78. if "|" in self.id:
  79. raise Exception("application service ID cannot contain '|' character")
  80. # .protocols is a publicly visible field
  81. if protocols:
  82. self.protocols = set(protocols)
  83. else:
  84. self.protocols = set()
  85. self.rate_limited = rate_limited
  86. def _check_namespaces(self, namespaces):
  87. # Sanity check that it is of the form:
  88. # {
  89. # users: [ {regex: "[A-z]+.*", exclusive: true}, ...],
  90. # aliases: [ {regex: "[A-z]+.*", exclusive: true}, ...],
  91. # rooms: [ {regex: "[A-z]+.*", exclusive: true}, ...],
  92. # }
  93. if not namespaces:
  94. namespaces = {}
  95. for ns in ApplicationService.NS_LIST:
  96. if ns not in namespaces:
  97. namespaces[ns] = []
  98. continue
  99. if type(namespaces[ns]) != list:
  100. raise ValueError("Bad namespace value for '%s'" % ns)
  101. for regex_obj in namespaces[ns]:
  102. if not isinstance(regex_obj, dict):
  103. raise ValueError("Expected dict regex for ns '%s'" % ns)
  104. if not isinstance(regex_obj.get("exclusive"), bool):
  105. raise ValueError(
  106. "Expected bool for 'exclusive' in ns '%s'" % ns
  107. )
  108. group_id = regex_obj.get("group_id")
  109. if group_id:
  110. if not isinstance(group_id, str):
  111. raise ValueError(
  112. "Expected string for 'group_id' in ns '%s'" % ns
  113. )
  114. try:
  115. GroupID.from_string(group_id)
  116. except Exception:
  117. raise ValueError(
  118. "Expected valid group ID for 'group_id' in ns '%s'" % ns
  119. )
  120. if get_domain_from_id(group_id) != self.server_name:
  121. raise ValueError(
  122. "Expected 'group_id' to be this host in ns '%s'" % ns
  123. )
  124. regex = regex_obj.get("regex")
  125. if isinstance(regex, string_types):
  126. regex_obj["regex"] = re.compile(regex) # Pre-compile regex
  127. else:
  128. raise ValueError(
  129. "Expected string for 'regex' in ns '%s'" % ns
  130. )
  131. return namespaces
  132. def _matches_regex(self, test_string, namespace_key):
  133. for regex_obj in self.namespaces[namespace_key]:
  134. if regex_obj["regex"].match(test_string):
  135. return regex_obj
  136. return None
  137. def _is_exclusive(self, ns_key, test_string):
  138. regex_obj = self._matches_regex(test_string, ns_key)
  139. if regex_obj:
  140. return regex_obj["exclusive"]
  141. return False
  142. @defer.inlineCallbacks
  143. def _matches_user(self, event, store):
  144. if not event:
  145. defer.returnValue(False)
  146. if self.is_interested_in_user(event.sender):
  147. defer.returnValue(True)
  148. # also check m.room.member state key
  149. if (event.type == EventTypes.Member and
  150. self.is_interested_in_user(event.state_key)):
  151. defer.returnValue(True)
  152. if not store:
  153. defer.returnValue(False)
  154. does_match = yield self._matches_user_in_member_list(event.room_id, store)
  155. defer.returnValue(does_match)
  156. @cachedInlineCallbacks(num_args=1, cache_context=True)
  157. def _matches_user_in_member_list(self, room_id, store, cache_context):
  158. member_list = yield store.get_users_in_room(
  159. room_id, on_invalidate=cache_context.invalidate
  160. )
  161. # check joined member events
  162. for user_id in member_list:
  163. if self.is_interested_in_user(user_id):
  164. defer.returnValue(True)
  165. defer.returnValue(False)
  166. def _matches_room_id(self, event):
  167. if hasattr(event, "room_id"):
  168. return self.is_interested_in_room(event.room_id)
  169. return False
  170. @defer.inlineCallbacks
  171. def _matches_aliases(self, event, store):
  172. if not store or not event:
  173. defer.returnValue(False)
  174. alias_list = yield store.get_aliases_for_room(event.room_id)
  175. for alias in alias_list:
  176. if self.is_interested_in_alias(alias):
  177. defer.returnValue(True)
  178. defer.returnValue(False)
  179. @defer.inlineCallbacks
  180. def is_interested(self, event, store=None):
  181. """Check if this service is interested in this event.
  182. Args:
  183. event(Event): The event to check.
  184. store(DataStore)
  185. Returns:
  186. bool: True if this service would like to know about this event.
  187. """
  188. # Do cheap checks first
  189. if self._matches_room_id(event):
  190. defer.returnValue(True)
  191. if (yield self._matches_aliases(event, store)):
  192. defer.returnValue(True)
  193. if (yield self._matches_user(event, store)):
  194. defer.returnValue(True)
  195. defer.returnValue(False)
  196. def is_interested_in_user(self, user_id):
  197. return (
  198. self._matches_regex(user_id, ApplicationService.NS_USERS)
  199. or user_id == self.sender
  200. )
  201. def is_interested_in_alias(self, alias):
  202. return bool(self._matches_regex(alias, ApplicationService.NS_ALIASES))
  203. def is_interested_in_room(self, room_id):
  204. return bool(self._matches_regex(room_id, ApplicationService.NS_ROOMS))
  205. def is_exclusive_user(self, user_id):
  206. return (
  207. self._is_exclusive(ApplicationService.NS_USERS, user_id)
  208. or user_id == self.sender
  209. )
  210. def is_interested_in_protocol(self, protocol):
  211. return protocol in self.protocols
  212. def is_exclusive_alias(self, alias):
  213. return self._is_exclusive(ApplicationService.NS_ALIASES, alias)
  214. def is_exclusive_room(self, room_id):
  215. return self._is_exclusive(ApplicationService.NS_ROOMS, room_id)
  216. def get_exlusive_user_regexes(self):
  217. """Get the list of regexes used to determine if a user is exclusively
  218. registered by the AS
  219. """
  220. return [
  221. regex_obj["regex"]
  222. for regex_obj in self.namespaces[ApplicationService.NS_USERS]
  223. if regex_obj["exclusive"]
  224. ]
  225. def get_groups_for_user(self, user_id):
  226. """Get the groups that this user is associated with by this AS
  227. Args:
  228. user_id (str): The ID of the user.
  229. Returns:
  230. iterable[str]: an iterable that yields group_id strings.
  231. """
  232. return (
  233. regex_obj["group_id"]
  234. for regex_obj in self.namespaces[ApplicationService.NS_USERS]
  235. if "group_id" in regex_obj and regex_obj["regex"].match(user_id)
  236. )
  237. def is_rate_limited(self):
  238. return self.rate_limited
  239. def __str__(self):
  240. # copy dictionary and redact token fields so they don't get logged
  241. dict_copy = self.__dict__.copy()
  242. dict_copy["token"] = "<redacted>"
  243. dict_copy["hs_token"] = "<redacted>"
  244. return "ApplicationService: %s" % (dict_copy,)