Skip to content

Commit ddd7ff6

Browse files
committed
Prevent updating matrix-auth data more than once
1 parent e7b73b2 commit ddd7ff6

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

lib/charms/synapse/v1/matrix_auth.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _on_config_changed(self, _) -> None:
6767

6868
# Increment this PATCH version before using `charmcraft publish-lib` or reset
6969
# to 0 if you are raising the major API version
70-
LIBPATCH = 4
70+
LIBPATCH = 5
7171

7272
# pylint: disable=wrong-import-position
7373
import json
@@ -442,15 +442,24 @@ def _on_relation_changed(self, event: ops.RelationChangedEvent) -> None:
442442
def update_relation_data(
443443
self, relation: ops.Relation, matrix_auth_provider_data: MatrixAuthProviderData
444444
) -> None:
445-
"""Update the relation data.
445+
"""Update the relation data. Since provider values should not be changed
446+
while instance exists, this method updates relation data only if
447+
invalid or empty.
446448
447449
Args:
448450
relation: the relation for which to update the data.
449451
matrix_auth_provider_data: a MatrixAuthProviderData instance wrapping the data to be
450452
updated.
451453
"""
452-
relation_data = matrix_auth_provider_data.to_relation_data(self.model, relation)
453-
relation.data[self.model.app].update(relation_data)
454+
try:
455+
logger.warning(relation)
456+
MatrixAuthProviderData.from_relation(self.model, relation=relation)
457+
logger.warning("Matrix Provider relation data is already set, skipping")
458+
except ValueError as e:
459+
logger.warning(e)
460+
logger.warning("Matrix Provider relation data is invalid or empty, updating")
461+
relation_data = matrix_auth_provider_data.to_relation_data(self.model, relation)
462+
relation.data[self.model.app].update(relation_data)
454463

455464

456465
class MatrixAuthRequires(ops.Object):

src/matrix_auth_observer.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ def update_matrix_auth_integration(self, charm_state: CharmState) -> None:
6262
logger.info("%d matrix-auth relations found", len(matrix_auth_relations))
6363
for relation in matrix_auth_relations:
6464
provider_data = self._get_matrix_auth_provider_data(charm_state)
65-
if self._matrix_auth_relation_updated(relation, provider_data):
66-
return
67-
logger.info("updating matrix-auth relation %d", relation.id)
6865
self.matrix_auth.update_relation_data(relation, provider_data)
6966

7067
def get_requirer_registration_secrets(self) -> Optional[List]:
@@ -121,30 +118,6 @@ def _get_matrix_auth_provider_data(
121118
shared_secret = synapse.get_registration_shared_secret(container=container)
122119
return MatrixAuthProviderData(homeserver=homeserver, shared_secret=shared_secret)
123120

124-
def _matrix_auth_relation_updated(
125-
self, relation: ops.Relation, provider_data: MatrixAuthProviderData
126-
) -> bool:
127-
"""Compare current information with the one in the relation.
128-
129-
This check is done to prevent triggering relation-changed.
130-
131-
Args:
132-
relation: The matrix-auth relation.
133-
provider_data: current Synapse configuration as MatrixAuthProviderData.
134-
135-
Returns:
136-
True if current configuration and relation data are the same.
137-
"""
138-
relation_homeserver = relation.data[self._charm.app].get("homeserver", "")
139-
relation_shared_secret = relation.data[self._charm.app].get("shared_secret", "")
140-
if (
141-
provider_data.homeserver != relation_homeserver
142-
or provider_data.shared_secret != relation_shared_secret
143-
):
144-
logger.info("matrix-auth relation ID %s is outdated and will be updated", relation.id)
145-
return False
146-
return True
147-
148121
@validate_charm_state
149122
def _on_matrix_auth_relation_changed(self, _: ops.EventBase) -> None:
150123
"""Handle matrix-auth request received event."""

tests/unit/test_matrix_auth_integration.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55

66
# pylint: disable=protected-access
77

8+
import logging
89
from unittest.mock import ANY, MagicMock
910

1011
import pytest
1112
import yaml
12-
from charms.synapse.v1.matrix_auth import MatrixAuthRequirerData, encrypt_string
13+
from charms.synapse.v1.matrix_auth import (
14+
MatrixAuthProviderData,
15+
MatrixAuthRequirerData,
16+
encrypt_string,
17+
)
1318
from ops.testing import Harness
1419
from pydantic import SecretStr
1520

@@ -47,6 +52,40 @@ def test_matrix_auth_update_success(harness: Harness, monkeypatch: pytest.Monkey
4752
assert update_relation_data.call_args[0][1].homeserver == f"https://{TEST_SERVER_NAME}"
4853

4954

55+
def test_matrix_auth_update_only_once(harness: Harness, monkeypatch: pytest.MonkeyPatch, caplog):
56+
"""
57+
arrange: start the Synapse charm.
58+
act: integrate via matrix-auth.
59+
assert: update_relation_data is called and homeserver has same value as
60+
server_name.
61+
"""
62+
harness.update_config({"server_name": TEST_SERVER_NAME})
63+
harness.set_can_connect(synapse.SYNAPSE_CONTAINER_NAME, True)
64+
harness.set_leader(True)
65+
harness.begin()
66+
monkeypatch.setattr(
67+
MatrixAuthProviderData, "get_shared_secret", MagicMock(return_value="shared-secret")
68+
)
69+
monkeypatch.setattr(
70+
synapse, "get_registration_shared_secret", MagicMock(return_value="shared_secret")
71+
)
72+
rel_id = harness.add_relation(
73+
"matrix-auth",
74+
"maubot",
75+
app_data={
76+
"homeserver": "example.com",
77+
"shared_secret_id": "test-secret-id",
78+
"encryption_key_secret_id": "test-secret-id",
79+
},
80+
)
81+
harness.add_relation_unit(rel_id, "maubot/0")
82+
83+
with caplog.at_level(logging.WARNING):
84+
harness.charm.on.config_changed.emit()
85+
86+
assert "Matrix Provider relation data is invalid or empty, updating" not in caplog.text
87+
88+
5089
def test_matrix_auth_update_public_baseurl_success(
5190
harness: Harness, monkeypatch: pytest.MonkeyPatch
5291
):

0 commit comments

Comments
 (0)