federation.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 typing import Optional
  16. from synapse.config._base import Config
  17. from synapse.config._util import validate_config
  18. class FederationConfig(Config):
  19. section = "federation"
  20. def read_config(self, config, **kwargs):
  21. # FIXME: federation_domain_whitelist needs sytests
  22. self.federation_domain_whitelist = None # type: Optional[dict]
  23. federation_domain_whitelist = config.get("federation_domain_whitelist", None)
  24. if federation_domain_whitelist is not None:
  25. # turn the whitelist into a hash for speed of lookup
  26. self.federation_domain_whitelist = {}
  27. for domain in federation_domain_whitelist:
  28. self.federation_domain_whitelist[domain] = True
  29. federation_metrics_domains = config.get("federation_metrics_domains") or []
  30. validate_config(
  31. _METRICS_FOR_DOMAINS_SCHEMA,
  32. federation_metrics_domains,
  33. ("federation_metrics_domains",),
  34. )
  35. self.federation_metrics_domains = set(federation_metrics_domains)
  36. self.allow_profile_lookup_over_federation = config.get(
  37. "allow_profile_lookup_over_federation", True
  38. )
  39. def generate_config_section(self, config_dir_path, server_name, **kwargs):
  40. return """\
  41. ## Federation ##
  42. # Restrict federation to the following whitelist of domains.
  43. # N.B. we recommend also firewalling your federation listener to limit
  44. # inbound federation traffic as early as possible, rather than relying
  45. # purely on this application-layer restriction. If not specified, the
  46. # default is to whitelist everything.
  47. #
  48. #federation_domain_whitelist:
  49. # - lon.example.com
  50. # - nyc.example.com
  51. # - syd.example.com
  52. # Report prometheus metrics on the age of PDUs being sent to and received from
  53. # the following domains. This can be used to give an idea of "delay" on inbound
  54. # and outbound federation, though be aware that any delay can be due to problems
  55. # at either end or with the intermediate network.
  56. #
  57. # By default, no domains are monitored in this way.
  58. #
  59. #federation_metrics_domains:
  60. # - matrix.org
  61. # - example.com
  62. # Uncomment to disable profile lookup over federation. By default, the
  63. # Federation API allows other homeservers to obtain profile data of any user
  64. # on this homeserver. Defaults to 'true'.
  65. #
  66. #allow_profile_lookup_over_federation: false
  67. """
  68. _METRICS_FOR_DOMAINS_SCHEMA = {"type": "array", "items": {"type": "string"}}