Skip to content

Commit

Permalink
Merge branch 'main' into discourse-gatekeeper/migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
amandahla authored Dec 5, 2023
2 parents 6c4d6ea + db97e4b commit b2102c2
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 16 deletions.
6 changes: 6 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# See LICENSE file for licensing details.

options:
allow_public_rooms_over_federation:
type: boolean
default: false
description: |
Allows any other homeserver to fetch the server's public rooms directory
via federation.
enable_mjolnir:
type: boolean
default: false
Expand Down
13 changes: 7 additions & 6 deletions src-docs/charm_state.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Get charm proxy information from juju charm environment.

---

<a href="../src/charm_state.py#L179"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L181"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `from_charm`

Expand Down Expand Up @@ -124,12 +124,13 @@ Represent Synapse builtin configuration values.

**Attributes:**

- <b>`server_name`</b>: server_name config.
- <b>`report_stats`</b>: report_stats config.
- <b>`public_baseurl`</b>: public_baseurl config.
- <b>`allow_public_rooms_over_federation`</b>: allow_public_rooms_over_federation config.
- <b>`enable_mjolnir`</b>: enable_mjolnir config.
- <b>`enable_password_config`</b>: enable_password_config config.
- <b>`federation_domain_whitelist`</b>: federation_domain_whitelist config.
- <b>`public_baseurl`</b>: public_baseurl config.
- <b>`report_stats`</b>: report_stats config.
- <b>`server_name`</b>: server_name config.
- <b>`smtp_enable_tls`</b>: enable tls while connecting to SMTP server.
- <b>`smtp_host`</b>: SMTP host.
- <b>`smtp_notif_from`</b>: defines the "From" address to use when sending emails.
Expand All @@ -142,7 +143,7 @@ Represent Synapse builtin configuration values.

---

<a href="../src/charm_state.py#L113"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L115"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `set_default_smtp_notif_from`

Expand All @@ -169,7 +170,7 @@ Set server_name as default value to smtp_notif_from.

---

<a href="../src/charm_state.py#L132"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L134"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `to_yes_or_no`

Expand Down
4 changes: 2 additions & 2 deletions src-docs/pebble.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Change the configuration.

---

<a href="../src/pebble.py#L104"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/pebble.py#L106"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `enable_saml`

Expand Down Expand Up @@ -117,7 +117,7 @@ Replan Synapse NGINX service.

---

<a href="../src/pebble.py#L120"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/pebble.py#L122"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `reset_instance`

Expand Down
14 changes: 8 additions & 6 deletions src/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ class SynapseConfig(BaseModel): # pylint: disable=too-few-public-methods
"""Represent Synapse builtin configuration values.
Attributes:
server_name: server_name config.
report_stats: report_stats config.
public_baseurl: public_baseurl config.
allow_public_rooms_over_federation: allow_public_rooms_over_federation config.
enable_mjolnir: enable_mjolnir config.
enable_password_config: enable_password_config config.
federation_domain_whitelist: federation_domain_whitelist config.
public_baseurl: public_baseurl config.
report_stats: report_stats config.
server_name: server_name config.
smtp_enable_tls: enable tls while connecting to SMTP server.
smtp_host: SMTP host.
smtp_notif_from: defines the "From" address to use when sending emails.
Expand All @@ -88,12 +89,13 @@ class SynapseConfig(BaseModel): # pylint: disable=too-few-public-methods
smtp_user: username to authenticate to SMTP host.
"""

server_name: str = Field(..., min_length=2)
report_stats: str | None = Field(None)
public_baseurl: str | None = Field(None)
allow_public_rooms_over_federation: bool = False
enable_mjolnir: bool = False
enable_password_config: bool = True
federation_domain_whitelist: str | None = Field(None)
public_baseurl: str | None = Field(None)
report_stats: str | None = Field(None)
server_name: str = Field(..., min_length=2)
smtp_enable_tls: bool = True
smtp_host: str | None = Field(None)
smtp_notif_from: str | None = Field(None)
Expand Down
2 changes: 2 additions & 0 deletions src/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def change_config(self, container: ops.model.Container) -> None:
synapse.enable_federation_domain_whitelist(
container=container, charm_state=self._charm_state
)
if self._charm_state.synapse_config.allow_public_rooms_over_federation:
synapse.enable_allow_public_rooms_over_federation(container=container)
self.restart_synapse(container)
except (synapse.WorkloadError, ops.pebble.PathError) as exc:
raise PebbleServiceError(str(exc)) from exc
Expand Down
1 change: 1 addition & 0 deletions src/synapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
check_ready,
create_mjolnir_config,
disable_password_config,
enable_allow_public_rooms_over_federation,
enable_federation_domain_whitelist,
enable_metrics,
enable_saml,
Expand Down
18 changes: 18 additions & 0 deletions src/synapse/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,24 @@ def enable_federation_domain_whitelist(container: ops.Container, charm_state: Ch
raise WorkloadError(str(exc)) from exc


def enable_allow_public_rooms_over_federation(container: ops.Container) -> None:
"""Change the Synapse configuration to allow public rooms in federation.
Args:
container: Container of the charm.
Raises:
WorkloadError: something went wrong enabling configuration.
"""
try:
config = container.pull(SYNAPSE_CONFIG_PATH).read()
current_yaml = yaml.safe_load(config)
current_yaml["allow_public_rooms_over_federation"] = True
container.push(SYNAPSE_CONFIG_PATH, yaml.safe_dump(current_yaml))
except ops.pebble.PathError as exc:
raise WorkloadError(str(exc)) from exc


def _get_mjolnir_config(access_token: str, room_id: str) -> typing.Dict:
"""Create config as expected by mjolnir.
Expand Down
4 changes: 2 additions & 2 deletions synapse_rock/rockcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ summary: Synapse rock
description: Synapse OCI image for the Synapse charm
version: "1.0"
base: [email protected]
# renovate: base: ubuntu:22.04@sha256:2b7412e6465c3c7fc5bb21d3e6f1917c167358449fecac8176c6e496e5c1f05f
# renovate: base: ubuntu:22.04@sha256:90f3997e3e5f82097fa3046152944772876ac338365954f693b9e1bd3dd3c280
build-base: [email protected]
# renovate: build-base: ubuntu:22.04@sha256:2b7412e6465c3c7fc5bb21d3e6f1917c167358449fecac8176c6e496e5c1f05f
# renovate: build-base: ubuntu:22.04@sha256:90f3997e3e5f82097fa3046152944772876ac338365954f693b9e1bd3dd3c280
license: Apache-2.0
platforms:
amd64:
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,22 @@ def test_nginx_replan(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None
harness.container_pebble_ready(synapse.SYNAPSE_NGINX_CONTAINER_NAME)

replan_nginx_mock.assert_called_once()


def test_nginx_replan_failure(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None:
"""
arrange: start the Synapse charm, mock replan_nginx call and set the container as down.
act: fire that NGINX container is ready.
assert: Pebble Service replan NGINX is not called.
"""
harness.begin()
replan_nginx_mock = MagicMock()
monkeypatch.setattr(harness.charm.pebble_service, "replan_nginx", replan_nginx_mock)

harness.set_can_connect(
harness.model.unit.containers[synapse.SYNAPSE_NGINX_CONTAINER_NAME], False
)
harness.container_pebble_ready(synapse.SYNAPSE_NGINX_CONTAINER_NAME)

replan_nginx_mock.assert_called_once()
assert isinstance(harness.model.unit.status, ops.MaintenanceStatus)
24 changes: 24 additions & 0 deletions tests/unit/test_mjolnir.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ def test_on_collect_status_api_error(harness: Harness, monkeypatch: pytest.Monke
enable_mjolnir_mock.assert_not_called()


def test_on_collect_status_admin_none(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None:
"""
arrange: start the Synapse charm, set server_name, mock container, mock _admin_access_token
to be None.
act: call _on_collect_status.
assert: mjolnir is not enabled and the model status is Maintenance.
"""
harness.update_config({"enable_mjolnir": True})
harness.begin_with_initial_hooks()
harness.set_leader(True)
monkeypatch.setattr(Mjolnir, "_admin_access_token", None)
enable_mjolnir_mock = MagicMock(return_value=None)
monkeypatch.setattr(Mjolnir, "enable_mjolnir", enable_mjolnir_mock)
charm_state_mock = MagicMock()
charm_state_mock.enable_mjolnir = True
harness.charm._mjolnir._charm_state = charm_state_mock

event_mock = MagicMock()
harness.charm._mjolnir._on_collect_status(event_mock)

enable_mjolnir_mock.assert_not_called()
assert isinstance(harness.model.unit.status, ops.MaintenanceStatus)


def test_enable_mjolnir(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None:
"""
arrange: start the Synapse charm, set server_name, mock calls to validate args.
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/test_synapse_workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,56 @@
from .conftest import TEST_SERVER_NAME


def test_allow_public_rooms_over_federation_sucess(monkeypatch: pytest.MonkeyPatch):
"""
arrange: set mock container with file.
act: call enable_allow_public_rooms_over_federation.
assert: new configuration file is pushed and
allow_public_rooms_over_federation is enabled.
"""
config_content = """
listeners:
- type: http
port: 8080
bind_addresses:
- "::"
"""
text_io_mock = io.StringIO(config_content)
pull_mock = Mock(return_value=text_io_mock)
push_mock = MagicMock()
container_mock = MagicMock()
monkeypatch.setattr(container_mock, "pull", pull_mock)
monkeypatch.setattr(container_mock, "push", push_mock)

synapse.enable_allow_public_rooms_over_federation(container_mock)

assert pull_mock.call_args[0][0] == synapse.SYNAPSE_CONFIG_PATH
assert push_mock.call_args[0][0] == synapse.SYNAPSE_CONFIG_PATH
expected_config_content = {
"listeners": [
{"type": "http", "port": 8080, "bind_addresses": ["::"]},
],
"allow_public_rooms_over_federation": True,
}
assert push_mock.call_args[0][1] == yaml.safe_dump(expected_config_content)


def test_allow_public_rooms_over_federation_error(monkeypatch: pytest.MonkeyPatch):
"""
arrange: set mock failing container with file.
act: call enable_allow_public_rooms_over_federation.
assert: raise WorkloadError in case of error.
"""
error_message = "Error pulling file"
path_error = ops.pebble.PathError(kind="fake", message=error_message)
pull_mock = MagicMock(side_effect=path_error)
container_mock = MagicMock()
monkeypatch.setattr(container_mock, "pull", pull_mock)

with pytest.raises(synapse.WorkloadError, match=error_message):
synapse.enable_allow_public_rooms_over_federation(container_mock)


def test_enable_federation_domain_whitelist_success(
harness: Harness, monkeypatch: pytest.MonkeyPatch
):
Expand Down

0 comments on commit b2102c2

Please sign in to comment.