summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorDaniel Wagner-Hall <daniel@matrix.org>2016-01-14 14:34:01 +0000
committerreview.rocks <nobody@review.rocks>2016-01-14 14:34:01 +0000
commit2680043bc6a64053b93b9bab144aeb5f45007976 (patch)
tree5f14aaa4a07e93f283464717d34ed897062de72f /synapse/storage
parentMerge pull request #498 from matrix-org/push_rule_enabled_fix (diff)
downloadsynapse-2680043bc6a64053b93b9bab144aeb5f45007976.tar.xz
Require ID and as_token be unique for ASs
Defaults ID to as_token if not specified. This will change
when IDs are fully supported.
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/appservice.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/synapse/storage/appservice.py b/synapse/storage/appservice.py
index f4bc457eca..b5aa55c0a3 100644
--- a/synapse/storage/appservice.py
+++ b/synapse/storage/appservice.py
@@ -20,6 +20,7 @@ from twisted.internet import defer
 
 from synapse.api.constants import Membership
 from synapse.appservice import ApplicationService, AppServiceTransaction
+from synapse.config._base import ConfigError
 from synapse.storage.roommember import RoomsForUser
 from synapse.types import UserID
 from ._base import SQLBaseStore
@@ -145,6 +146,7 @@ class ApplicationServiceStore(SQLBaseStore):
 
     def _load_appservice(self, as_info):
         required_string_fields = [
+            # TODO: Add id here when it's stable to release
             "url", "as_token", "hs_token", "sender_localpart"
         ]
         for field in required_string_fields:
@@ -186,7 +188,7 @@ class ApplicationServiceStore(SQLBaseStore):
             namespaces=as_info["namespaces"],
             hs_token=as_info["hs_token"],
             sender=user_id,
-            id=as_info["as_token"]  # the token is the only unique thing here
+            id=as_info["id"] if "id" in as_info else as_info["as_token"],
         )
 
     def _populate_appservice_cache(self, config_files):
@@ -197,10 +199,32 @@ class ApplicationServiceStore(SQLBaseStore):
             )
             return
 
+        # Dicts of value -> filename
+        seen_as_tokens = {}
+        seen_ids = {}
+
         for config_file in config_files:
             try:
                 with open(config_file, 'r') as f:
                     appservice = self._load_appservice(yaml.load(f))
+                    if appservice.id in seen_ids:
+                        raise ConfigError(
+                            "Cannot reuse ID across application services: "
+                            "%s (files: %s, %s)" % (
+                                appservice.id, config_file, seen_ids[appservice.id],
+                            )
+                        )
+                    seen_ids[appservice.id] = config_file
+                    if appservice.token in seen_as_tokens:
+                        raise ConfigError(
+                            "Cannot reuse as_token across application services: "
+                            "%s (files: %s, %s)" % (
+                                appservice.token,
+                                config_file,
+                                seen_as_tokens[appservice.token],
+                            )
+                        )
+                    seen_as_tokens[appservice.token] = config_file
                     logger.info("Loaded application service: %s", appservice)
                     self.services_cache.append(appservice)
             except Exception as e: