2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-03-31 02:22:31 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2016-05-17 04:28:58 -06:00
|
|
|
from ._base import Config, ConfigError
|
|
|
|
|
|
|
|
from synapse.appservice import ApplicationService
|
|
|
|
from synapse.types import UserID
|
|
|
|
|
|
|
|
import urllib
|
|
|
|
import yaml
|
|
|
|
import logging
|
|
|
|
|
2018-04-06 16:37:36 -06:00
|
|
|
from six import string_types
|
|
|
|
|
2016-05-17 04:28:58 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-03-31 02:22:31 -06:00
|
|
|
|
|
|
|
|
|
|
|
class AppServiceConfig(Config):
|
|
|
|
|
2015-04-29 21:24:44 -06:00
|
|
|
def read_config(self, config):
|
|
|
|
self.app_service_config_files = config.get("app_service_config_files", [])
|
2016-08-18 07:59:55 -06:00
|
|
|
self.notify_appservices = config.get("notify_appservices", True)
|
2015-03-31 02:22:31 -06:00
|
|
|
|
2015-09-22 05:57:40 -06:00
|
|
|
def default_config(cls, **kwargs):
|
2015-04-29 21:24:44 -06:00
|
|
|
return """\
|
|
|
|
# A list of application service config file to use
|
|
|
|
app_service_config_files: []
|
|
|
|
"""
|
2016-05-17 04:28:58 -06:00
|
|
|
|
|
|
|
|
|
|
|
def load_appservices(hostname, config_files):
|
|
|
|
"""Returns a list of Application Services from the config files."""
|
|
|
|
if not isinstance(config_files, list):
|
|
|
|
logger.warning(
|
|
|
|
"Expected %s to be a list of AS config files.", config_files
|
|
|
|
)
|
|
|
|
return []
|
|
|
|
|
|
|
|
# Dicts of value -> filename
|
|
|
|
seen_as_tokens = {}
|
|
|
|
seen_ids = {}
|
|
|
|
|
|
|
|
appservices = []
|
|
|
|
|
|
|
|
for config_file in config_files:
|
|
|
|
try:
|
|
|
|
with open(config_file, 'r') as f:
|
|
|
|
appservice = _load_appservice(
|
|
|
|
hostname, yaml.load(f), config_file
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
appservices.append(appservice)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Failed to load appservice from '%s'", config_file)
|
|
|
|
logger.exception(e)
|
|
|
|
raise
|
|
|
|
return appservices
|
|
|
|
|
|
|
|
|
|
|
|
def _load_appservice(hostname, as_info, config_filename):
|
|
|
|
required_string_fields = [
|
2016-08-30 10:16:00 -06:00
|
|
|
"id", "as_token", "hs_token", "sender_localpart"
|
2016-05-17 04:28:58 -06:00
|
|
|
]
|
|
|
|
for field in required_string_fields:
|
2018-04-06 16:37:36 -06:00
|
|
|
if not isinstance(as_info.get(field), string_types):
|
2016-05-17 04:28:58 -06:00
|
|
|
raise KeyError("Required string field: '%s' (%s)" % (
|
|
|
|
field, config_filename,
|
|
|
|
))
|
|
|
|
|
2016-08-30 10:16:00 -06:00
|
|
|
# 'url' must either be a string or explicitly null, not missing
|
|
|
|
# to avoid accidentally turning off push for ASes.
|
2018-04-06 16:37:36 -06:00
|
|
|
if (not isinstance(as_info.get("url"), string_types) and
|
2016-08-30 10:20:31 -06:00
|
|
|
as_info.get("url", "") is not None):
|
2016-08-30 10:16:00 -06:00
|
|
|
raise KeyError(
|
|
|
|
"Required string field or explicit null: 'url' (%s)" % (config_filename,)
|
|
|
|
)
|
|
|
|
|
2016-05-17 04:28:58 -06:00
|
|
|
localpart = as_info["sender_localpart"]
|
|
|
|
if urllib.quote(localpart) != localpart:
|
|
|
|
raise ValueError(
|
|
|
|
"sender_localpart needs characters which are not URL encoded."
|
|
|
|
)
|
|
|
|
user = UserID(localpart, hostname)
|
|
|
|
user_id = user.to_string()
|
|
|
|
|
2016-10-18 10:04:09 -06:00
|
|
|
# Rate limiting for users of this AS is on by default (excludes sender)
|
|
|
|
rate_limited = True
|
|
|
|
if isinstance(as_info.get("rate_limited"), bool):
|
|
|
|
rate_limited = as_info.get("rate_limited")
|
|
|
|
|
2016-05-17 04:28:58 -06:00
|
|
|
# namespace checks
|
|
|
|
if not isinstance(as_info.get("namespaces"), dict):
|
|
|
|
raise KeyError("Requires 'namespaces' object.")
|
|
|
|
for ns in ApplicationService.NS_LIST:
|
|
|
|
# specific namespaces are optional
|
|
|
|
if ns in as_info["namespaces"]:
|
|
|
|
# expect a list of dicts with exclusive and regex keys
|
|
|
|
for regex_obj in as_info["namespaces"][ns]:
|
|
|
|
if not isinstance(regex_obj, dict):
|
|
|
|
raise ValueError(
|
|
|
|
"Expected namespace entry in %s to be an object,"
|
|
|
|
" but got %s", ns, regex_obj
|
|
|
|
)
|
2018-04-06 16:37:36 -06:00
|
|
|
if not isinstance(regex_obj.get("regex"), string_types):
|
2016-05-17 04:28:58 -06:00
|
|
|
raise ValueError(
|
|
|
|
"Missing/bad type 'regex' key in %s", regex_obj
|
|
|
|
)
|
|
|
|
if not isinstance(regex_obj.get("exclusive"), bool):
|
|
|
|
raise ValueError(
|
|
|
|
"Missing/bad type 'exclusive' key in %s", regex_obj
|
|
|
|
)
|
2016-08-18 07:56:02 -06:00
|
|
|
# protocols check
|
|
|
|
protocols = as_info.get("protocols")
|
|
|
|
if protocols:
|
|
|
|
# Because strings are lists in python
|
|
|
|
if isinstance(protocols, str) or not isinstance(protocols, list):
|
|
|
|
raise KeyError("Optional 'protocols' must be a list if present.")
|
|
|
|
for p in protocols:
|
|
|
|
if not isinstance(p, str):
|
|
|
|
raise KeyError("Bad value for 'protocols' item")
|
2016-08-30 09:21:16 -06:00
|
|
|
|
2016-08-30 10:20:31 -06:00
|
|
|
if as_info["url"] is None:
|
2016-08-30 09:21:16 -06:00
|
|
|
logger.info(
|
2016-08-30 10:16:00 -06:00
|
|
|
"(%s) Explicitly empty 'url' provided. This application service"
|
|
|
|
" will not receive events or queries.",
|
2016-08-30 09:21:16 -06:00
|
|
|
config_filename,
|
|
|
|
)
|
2016-05-17 04:28:58 -06:00
|
|
|
return ApplicationService(
|
|
|
|
token=as_info["as_token"],
|
2017-11-16 10:54:27 -07:00
|
|
|
hostname=hostname,
|
2016-05-17 04:28:58 -06:00
|
|
|
url=as_info["url"],
|
|
|
|
namespaces=as_info["namespaces"],
|
|
|
|
hs_token=as_info["hs_token"],
|
|
|
|
sender=user_id,
|
|
|
|
id=as_info["id"],
|
2016-08-18 07:56:02 -06:00
|
|
|
protocols=protocols,
|
2016-10-18 10:04:09 -06:00
|
|
|
rate_limited=rate_limited
|
2016-05-17 04:28:58 -06:00
|
|
|
)
|