Skip to content

Commit

Permalink
Add enable_password_config (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandahla authored Oct 25, 2023
1 parent 0c6c8f9 commit 3716d4e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ options:
description: |
Configures whether to enable Mjolnir - moderation tool for Matrix.
Reference: https://github.com/matrix-org/mjolnir
enable_password_config:
type: boolean
default: true
description: |
Defaults to true. If Synapse uses a single sign-on provider for
authentication, this option can be used to disable the regular login
flow.
public_baseurl:
type: string
description: |
Expand Down
9 changes: 5 additions & 4 deletions src-docs/charm_state.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Exception raised when a charm configuration is found to be invalid.

Attrs: msg (str): Explanation of the error.

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

### <kbd>function</kbd> `__init__`

Expand Down Expand Up @@ -67,7 +67,7 @@ Get charm proxy information from juju charm environment.

---

<a href="../src/charm_state.py#L173"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L176"><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 @@ -128,6 +128,7 @@ Represent Synapse builtin configuration values.
- <b>`report_stats`</b>: report_stats config.
- <b>`public_baseurl`</b>: public_baseurl config.
- <b>`enable_mjolnir`</b>: enable_mjolnir config.
- <b>`enable_password_config`</b>: enable_password_config 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 @@ -140,7 +141,7 @@ Represent Synapse builtin configuration values.

---

<a href="../src/charm_state.py#L107"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L110"><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 @@ -167,7 +168,7 @@ Set server_name as default value to smtp_notif_from.

---

<a href="../src/charm_state.py#L126"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L129"><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#L98"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/pebble.py#L100"><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#L114"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/pebble.py#L116"><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
3 changes: 3 additions & 0 deletions src/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

KNOWN_CHARM_CONFIG = (
"enable_mjolnir",
"enable_password_config",
"public_baseurl",
"report_stats",
"server_name",
Expand Down Expand Up @@ -76,6 +77,7 @@ class SynapseConfig(BaseModel): # pylint: disable=too-few-public-methods
report_stats: report_stats config.
public_baseurl: public_baseurl config.
enable_mjolnir: enable_mjolnir config.
enable_password_config: enable_password_config 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,6 +90,7 @@ class SynapseConfig(BaseModel): # pylint: disable=too-few-public-methods
report_stats: str | None = Field(None)
public_baseurl: str | None = Field(None)
enable_mjolnir: bool = False
enable_password_config: bool = True
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 @@ -91,6 +91,8 @@ def change_config(self, container: ops.model.Container) -> None:
synapse.enable_saml(container=container, charm_state=self._charm_state)
if self._charm_state.synapse_config.smtp_host:
synapse.enable_smtp(container=container, charm_state=self._charm_state)
if not self._charm_state.synapse_config.enable_password_config:
synapse.disable_password_config(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 @@ -53,6 +53,7 @@
check_nginx_ready,
check_ready,
create_mjolnir_config,
disable_password_config,
enable_metrics,
enable_saml,
enable_serve_server_wellknown,
Expand Down
18 changes: 18 additions & 0 deletions src/synapse/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ def enable_metrics(container: ops.Container) -> None:
raise EnableMetricsError(str(exc)) from exc


def disable_password_config(container: ops.Container) -> None:
"""Change the Synapse configuration to disable password config.
Args:
container: Container of the charm.
Raises:
WorkloadError: something went wrong disabling password config.
"""
try:
config = container.pull(SYNAPSE_CONFIG_PATH).read()
current_yaml = yaml.safe_load(config)
current_yaml["password_config"] = {"enabled": False}
container.push(SYNAPSE_CONFIG_PATH, yaml.safe_dump(current_yaml))
except ops.pebble.PathError as exc:
raise WorkloadError(str(exc)) from exc


def enable_serve_server_wellknown(container: ops.Container) -> None:
"""Change the Synapse configuration to enable server wellknown file.
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test_synapse_workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,51 @@ def test_enable_serve_server_wellknown_error(monkeypatch: pytest.MonkeyPatch):
synapse.enable_serve_server_wellknown(container_mock)


def test_disable_password_config_success(monkeypatch: pytest.MonkeyPatch):
"""
arrange: set mock container with file.
act: call disable_password_config.
assert: new configuration file is pushed and password_config is disabled.
"""
config_content = """
password_config:
enabled: true
"""
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.disable_password_config(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 = {
"password_config": {
"enabled": False,
},
}
assert push_mock.call_args[0][1] == yaml.safe_dump(expected_config_content)


def test_disable_password_config_error(monkeypatch: pytest.MonkeyPatch):
"""
arrange: set mock container with file.
act: call disable_password_config.
assert: raise WorkloadError.
"""
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.disable_password_config(container_mock)


def test_get_registration_shared_secret_success(monkeypatch: pytest.MonkeyPatch):
"""
arrange: set mock container with file.
Expand Down

0 comments on commit 3716d4e

Please sign in to comment.