From 03b6cfbd0660c32d0bd03513916b7283cd49436c Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Mon, 13 Jun 2022 17:58:24 +0200 Subject: [PATCH 01/10] Add custom well-known Signed-off-by: Jacek Kusnierz --- synapse/config/server.py | 2 ++ synapse/rest/well_known.py | 6 +++++- tests/rest/test_well_known.py | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/synapse/config/server.py b/synapse/config/server.py index 657322cb1f98..d28654fb0b09 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -302,6 +302,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: "public_baseurl cannot contain query parameters or a #-fragment" ) + self.custom_well_known = config.get("custom_well_known") + # Whether to enable user presence. presence_config = config.get("presence") or {} self.use_presence = presence_config.get("enabled") diff --git a/synapse/rest/well_known.py b/synapse/rest/well_known.py index 04b035a1b10d..5765ed85b14d 100644 --- a/synapse/rest/well_known.py +++ b/synapse/rest/well_known.py @@ -11,7 +11,6 @@ # 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. - import logging from typing import TYPE_CHECKING, Optional @@ -44,6 +43,11 @@ def get_well_known(self) -> Optional[JsonDict]: "base_url": self._config.registration.default_identity_server } + if self._config.server.custom_well_known: + for key, value in self._config.server.custom_well_known.items(): + if key not in result: + result[key] = value + return result diff --git a/tests/rest/test_well_known.py b/tests/rest/test_well_known.py index 11f78f52b87a..f9a857fa3b4e 100644 --- a/tests/rest/test_well_known.py +++ b/tests/rest/test_well_known.py @@ -59,6 +59,28 @@ def test_client_well_known_no_public_baseurl(self) -> None: self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) + @unittest.override_config( + { + "public_baseurl": "https://tesths", + "default_identity_server": "https://testis", + "custom_well_known": {"custom": False}, + } + ) + def test_client_well_known_custom(self) -> None: + channel = self.make_request( + "GET", "/.well-known/matrix/client", shorthand=False + ) + + self.assertEqual(channel.code, HTTPStatus.OK) + self.assertEqual( + channel.json_body, + { + "m.homeserver": {"base_url": "https://tesths/"}, + "m.identity_server": {"base_url": "https://testis"}, + "custom": False + }, + ) + @unittest.override_config({"serve_server_wellknown": True}) def test_server_well_known(self) -> None: channel = self.make_request( From 68ac6b361a61264e6118d11941700ab299e9ccf9 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Mon, 13 Jun 2022 18:26:31 +0200 Subject: [PATCH 02/10] reformat --- tests/rest/test_well_known.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rest/test_well_known.py b/tests/rest/test_well_known.py index f9a857fa3b4e..4fba28104107 100644 --- a/tests/rest/test_well_known.py +++ b/tests/rest/test_well_known.py @@ -77,7 +77,7 @@ def test_client_well_known_custom(self) -> None: { "m.homeserver": {"base_url": "https://tesths/"}, "m.identity_server": {"base_url": "https://testis"}, - "custom": False + "custom": False, }, ) From 33759283b9a3b5114c53a00437fcc502bb353575 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Mon, 13 Jun 2022 18:28:13 +0200 Subject: [PATCH 03/10] Add changelog Signed-off-by: Jacek Kusnierz --- changelog.d/13035.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/13035.feature diff --git a/changelog.d/13035.feature b/changelog.d/13035.feature new file mode 100644 index 000000000000..bd3033c63890 --- /dev/null +++ b/changelog.d/13035.feature @@ -0,0 +1 @@ +Allow adding custom content for /.well-known/matrix/client endpoint. From 177f9f221beaed2ff3c1af0383402a5891927a7f Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Mon, 13 Jun 2022 18:29:59 +0200 Subject: [PATCH 04/10] Reformat Signed-off-by: Jacek Kusnierz --- synapse/config/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/config/server.py b/synapse/config/server.py index d28654fb0b09..ca29dc812de4 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -302,7 +302,7 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: "public_baseurl cannot contain query parameters or a #-fragment" ) - self.custom_well_known = config.get("custom_well_known") + self.custom_well_known = config.get("custom_well_known") # Whether to enable user presence. presence_config = config.get("presence") or {} From 10e563dfe97441b5c03b51cf05030bc5a58d75d5 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Mon, 13 Jun 2022 21:47:45 +0200 Subject: [PATCH 05/10] Add documentation entry Signed-off-by: Jacek Kusnierz --- docs/usage/configuration/config_documentation.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 392ae80a759a..3101bd314297 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -230,6 +230,21 @@ Example configuration: serve_server_wellknown: true ``` --- +Config option: `custom_wellknown` + +This option allows the server to define arbitrary payload in addition to the default wellknown. + +If this option is provided, it parses the given yaml to json and +serves it on `/.well-known/matrix/client` endpoint +alongside the standard properties. + +Example configuration: +```yaml +custom_well_known: + option1: value1 + option2: value2 +``` +--- Config option: `soft_file_limit` Set the soft limit on the number of file descriptors synapse can use. @@ -3578,3 +3593,4 @@ background_updates: min_batch_size: 10 default_batch_size: 50 ``` + From 16b0431c35598412d6a579172d700869574da0be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Ku=C5=9Bnierz?= Date: Tue, 14 Jun 2022 15:39:12 +0200 Subject: [PATCH 06/10] Apply suggestions to documentation Co-authored-by: David Robertson --- changelog.d/13035.feature | 2 +- docs/usage/configuration/config_documentation.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog.d/13035.feature b/changelog.d/13035.feature index bd3033c63890..cfca3ab4b7ad 100644 --- a/changelog.d/13035.feature +++ b/changelog.d/13035.feature @@ -1 +1 @@ -Allow adding custom content for /.well-known/matrix/client endpoint. +Allow server admins to customise the response of the `/.well-known/matrix/client` endpoint. diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 3101bd314297..e13fba53e9c0 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -230,9 +230,9 @@ Example configuration: serve_server_wellknown: true ``` --- -Config option: `custom_wellknown` +Config option: `custom_well_known` -This option allows the server to define arbitrary payload in addition to the default wellknown. +This option allows server runners to add arbitrary key-value pairsto the [client-facing `.well-known` response](https://spec.matrix.org/latest/client-server-api/#well-known-uri). If this option is provided, it parses the given yaml to json and serves it on `/.well-known/matrix/client` endpoint From 3b9b351aa8788a392785185f0173f339b4e54232 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Wed, 15 Jun 2022 13:01:09 +0200 Subject: [PATCH 07/10] Updated according to suggestions --- .../configuration/config_documentation.md | 6 +++--- synapse/config/server.py | 18 +++++++++++++++++- synapse/rest/well_known.py | 4 ++-- tests/rest/test_well_known.py | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index e13fba53e9c0..b58c1a0a13d4 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -230,9 +230,9 @@ Example configuration: serve_server_wellknown: true ``` --- -Config option: `custom_well_known` +Config option: `extra_well_known_content` -This option allows server runners to add arbitrary key-value pairsto the [client-facing `.well-known` response](https://spec.matrix.org/latest/client-server-api/#well-known-uri). +This option allows server runners to add arbitrary key-value pairs to the [client-facing `.well-known` response](https://spec.matrix.org/latest/client-server-api/#well-known-uri). If this option is provided, it parses the given yaml to json and serves it on `/.well-known/matrix/client` endpoint @@ -240,7 +240,7 @@ alongside the standard properties. Example configuration: ```yaml -custom_well_known: +extra_well_known_content: option1: value1 option2: value2 ``` diff --git a/synapse/config/server.py b/synapse/config/server.py index ca29dc812de4..7ab622f35402 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -302,7 +302,23 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: "public_baseurl cannot contain query parameters or a #-fragment" ) - self.custom_well_known = config.get("custom_well_known") + self.extra_well_known_content = config.get("extra_well_known_content") + + if type(self.extra_well_known_content) is not dict: + raise ConfigError( + "extra_well_known_content must be a dictionary of key-value pairs" + ) + + if "m.homeserver" in self.extra_well_known_content.keys(): + raise ConfigError( + "m.homeserver is not supported in extra_well_known_content, " + "use public_baseurl in base config instead." + ) + if "m.identity_server" in self.extra_well_known_content.keys(): + raise ConfigError( + "m.identity_server is not supported in extra_well_known_content, " + "use default_identity_server in base config instead." + ) # Whether to enable user presence. presence_config = config.get("presence") or {} diff --git a/synapse/rest/well_known.py b/synapse/rest/well_known.py index 5765ed85b14d..5c5858585263 100644 --- a/synapse/rest/well_known.py +++ b/synapse/rest/well_known.py @@ -43,8 +43,8 @@ def get_well_known(self) -> Optional[JsonDict]: "base_url": self._config.registration.default_identity_server } - if self._config.server.custom_well_known: - for key, value in self._config.server.custom_well_known.items(): + if self._config.server.extra_well_known_content: + for key, value in self._config.server.extra_well_known_content.items(): if key not in result: result[key] = value diff --git a/tests/rest/test_well_known.py b/tests/rest/test_well_known.py index 4fba28104107..d97c6bd1436d 100644 --- a/tests/rest/test_well_known.py +++ b/tests/rest/test_well_known.py @@ -63,7 +63,7 @@ def test_client_well_known_no_public_baseurl(self) -> None: { "public_baseurl": "https://tesths", "default_identity_server": "https://testis", - "custom_well_known": {"custom": False}, + "extra_well_known_content": {"custom": False}, } ) def test_client_well_known_custom(self) -> None: From e87b7b329a48381b4a0c5309e98cd2548bbb0ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Ku=C5=9Bnierz?= Date: Wed, 15 Jun 2022 13:14:54 +0200 Subject: [PATCH 08/10] Apply suggestions from code review Co-authored-by: David Robertson --- synapse/config/server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse/config/server.py b/synapse/config/server.py index 7ab622f35402..8d7ab24e7acb 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -302,19 +302,19 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: "public_baseurl cannot contain query parameters or a #-fragment" ) - self.extra_well_known_content = config.get("extra_well_known_content") + self.extra_well_known_content = config.get("extra_well_known_content", {}) - if type(self.extra_well_known_content) is not dict: + if not isinstance(self.extra_well_known_content, dict): raise ConfigError( "extra_well_known_content must be a dictionary of key-value pairs" ) - if "m.homeserver" in self.extra_well_known_content.keys(): + if "m.homeserver" in self.extra_well_known_content: raise ConfigError( "m.homeserver is not supported in extra_well_known_content, " "use public_baseurl in base config instead." ) - if "m.identity_server" in self.extra_well_known_content.keys(): + if "m.identity_server" in self.extra_well_known_content: raise ConfigError( "m.identity_server is not supported in extra_well_known_content, " "use default_identity_server in base config instead." From c3356bfca330c4bdfb89fc32e80baff572a9af08 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Wed, 15 Jun 2022 13:27:46 +0200 Subject: [PATCH 09/10] Add more tests to well_known Signed-off-by: Jacek Kusnierz --- tests/rest/test_well_known.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/rest/test_well_known.py b/tests/rest/test_well_known.py index d97c6bd1436d..7aedd75dacf6 100644 --- a/tests/rest/test_well_known.py +++ b/tests/rest/test_well_known.py @@ -59,6 +59,30 @@ def test_client_well_known_no_public_baseurl(self) -> None: self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) + @unittest.override_config( + { + "default_identity_server": "https://testis", + } + ) + def test_client_well_known_only_default_identity_server(self) -> None: + channel = self.make_request( + "GET", "/.well-known/matrix/client", shorthand=False + ) + + self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) + + @unittest.override_config( + { + "extra_well_known_content": {"custom": False}, + } + ) + def test_client_well_known_only_extra_well_known(self) -> None: + channel = self.make_request( + "GET", "/.well-known/matrix/client", shorthand=False + ) + + self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) + @unittest.override_config( { "public_baseurl": "https://tesths", From 36e6adfa9d28339fae68c26647fc70480d53eef0 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Wed, 15 Jun 2022 17:46:45 +0200 Subject: [PATCH 10/10] Remove redundant tests, address comments Signed-off-by: Jacek Kusnierz --- .../configuration/config_documentation.md | 5 ++-- synapse/config/server.py | 10 ++++--- synapse/rest/well_known.py | 7 +++-- tests/rest/test_well_known.py | 26 +------------------ 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index b58c1a0a13d4..2ccc317726cb 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -230,9 +230,10 @@ Example configuration: serve_server_wellknown: true ``` --- -Config option: `extra_well_known_content` +Config option: `extra_well_known_client_content ` This option allows server runners to add arbitrary key-value pairs to the [client-facing `.well-known` response](https://spec.matrix.org/latest/client-server-api/#well-known-uri). +Note that the `public_baseurl` config option must be provided for Synapse to serve a response to `/.well-known/matrix/client` at all. If this option is provided, it parses the given yaml to json and serves it on `/.well-known/matrix/client` endpoint @@ -240,7 +241,7 @@ alongside the standard properties. Example configuration: ```yaml -extra_well_known_content: +extra_well_known_client_content : option1: value1 option2: value2 ``` diff --git a/synapse/config/server.py b/synapse/config/server.py index 8d7ab24e7acb..5ce77dd72a30 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -302,19 +302,21 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: "public_baseurl cannot contain query parameters or a #-fragment" ) - self.extra_well_known_content = config.get("extra_well_known_content", {}) + self.extra_well_known_client_content = config.get( + "extra_well_known_client_content", {} + ) - if not isinstance(self.extra_well_known_content, dict): + if not isinstance(self.extra_well_known_client_content, dict): raise ConfigError( "extra_well_known_content must be a dictionary of key-value pairs" ) - if "m.homeserver" in self.extra_well_known_content: + if "m.homeserver" in self.extra_well_known_client_content: raise ConfigError( "m.homeserver is not supported in extra_well_known_content, " "use public_baseurl in base config instead." ) - if "m.identity_server" in self.extra_well_known_content: + if "m.identity_server" in self.extra_well_known_client_content: raise ConfigError( "m.identity_server is not supported in extra_well_known_content, " "use default_identity_server in base config instead." diff --git a/synapse/rest/well_known.py b/synapse/rest/well_known.py index 5c5858585263..6f7ac54c651a 100644 --- a/synapse/rest/well_known.py +++ b/synapse/rest/well_known.py @@ -43,8 +43,11 @@ def get_well_known(self) -> Optional[JsonDict]: "base_url": self._config.registration.default_identity_server } - if self._config.server.extra_well_known_content: - for key, value in self._config.server.extra_well_known_content.items(): + if self._config.server.extra_well_known_client_content: + for ( + key, + value, + ) in self._config.server.extra_well_known_client_content.items(): if key not in result: result[key] = value diff --git a/tests/rest/test_well_known.py b/tests/rest/test_well_known.py index 7aedd75dacf6..d8faafec7590 100644 --- a/tests/rest/test_well_known.py +++ b/tests/rest/test_well_known.py @@ -59,35 +59,11 @@ def test_client_well_known_no_public_baseurl(self) -> None: self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) - @unittest.override_config( - { - "default_identity_server": "https://testis", - } - ) - def test_client_well_known_only_default_identity_server(self) -> None: - channel = self.make_request( - "GET", "/.well-known/matrix/client", shorthand=False - ) - - self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) - - @unittest.override_config( - { - "extra_well_known_content": {"custom": False}, - } - ) - def test_client_well_known_only_extra_well_known(self) -> None: - channel = self.make_request( - "GET", "/.well-known/matrix/client", shorthand=False - ) - - self.assertEqual(channel.code, HTTPStatus.NOT_FOUND) - @unittest.override_config( { "public_baseurl": "https://tesths", "default_identity_server": "https://testis", - "extra_well_known_content": {"custom": False}, + "extra_well_known_client_content": {"custom": False}, } ) def test_client_well_known_custom(self) -> None: