Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update docs and wrap pebble.ExecError with custom error #653

Open
wants to merge 2 commits into
base: 2/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/explanation/moderation-and-spam-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ For details and implementation, visit the module’s repository: [Synapse Invite
---

## Mjolnir

With the arrival of MAS mjolnir has been temporary disabled.
With the arrival of MAS, mjolnir has been temporary disabled.
48 changes: 39 additions & 9 deletions src/auth/mas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
logger = logging.getLogger()

MAS_TEMPLATE_FILE_NAME = "mas_config.yaml.j2"

MAS_SERVICE_NAME = "synapse-mas"
MAS_EXECUTABLE_PATH = "/usr/bin/mas-cli"
MAS_WORKING_DIR = "/mas"
Expand Down Expand Up @@ -46,6 +45,10 @@ class MASConfigInvalidError(Exception):
"""Exception raised when validation of the MAS config failed."""


class MASConfigSyncError(Exception):
"""Exception raised when synchronisation of the MAS config failed."""


class MASRegisterUserFailedError(Exception):
"""Exception raised when validation of the MAS config failed."""

Expand All @@ -54,6 +57,10 @@ class MASVerifyUserEmailFailedError(Exception):
"""Exception raised when validation of the MAS config failed."""


class MASDeactivateUserFailedError(Exception):
"""Exception raised when deactivation of a MAS user failed."""


class MASGenerateAdminAccessTokenError(Exception):
"""Exception raised when generation of admin token failed."""

Expand All @@ -63,21 +70,37 @@ def validate_mas_config(container: ops.model.Container) -> None:

Args:
container: Synapse container.

Raises:
MASConfigInvalidError: when validation of the MAS config failed.
"""
command = [MAS_EXECUTABLE_PATH, "config", "check", "-c", MAS_CONFIGURATION_PATH]
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait_output()

try:
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait_output()
except ops.pebble.ExecError as exc:
logger.exception("Validation of the MAS config failed.")
raise MASConfigInvalidError("Validation of the MAS config failed.") from exc


def sync_mas_config(container: ops.model.Container) -> None:
"""Sync the MAS configuration with the database.

Args:
container: Synapse container.

Raises:
MASConfigSyncError: when synchronisation of the MAS config failed.
"""
command = [MAS_EXECUTABLE_PATH, "config", "sync", "--prune", "-c", MAS_CONFIGURATION_PATH]
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait()

try:
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait()
except ops.pebble.ExecError as exc:
logger.exception("Error syncing MAS config with the database.")
raise MASConfigSyncError("Error validating MAS configuration.") from exc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this exception be catched somewhere? Otherwise I think the unit will go into error state (and currently synapse does not go into error state, but into blocked state I think)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that if this command fails then there's something wrong with the postgresql database which should put the charm into an error state as there's usually no specific action for the operator to take



def register_user(
Expand Down Expand Up @@ -116,7 +139,7 @@ def register_user(
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait_output()
except ops.pebble.ExecError as exc:
logger.error("Error registering new user: %s", exc.stderr)
logger.exception("Error registering new user.")
raise MASRegisterUserFailedError("Error validating MAS configuration.") from exc

return password
Expand Down Expand Up @@ -151,7 +174,7 @@ def verify_user_email(
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait_output()
except ops.pebble.ExecError as exc:
logger.error("Error verifying the user email: %s", exc.stderr)
logger.exception("Error verifying the user email.")
raise MASVerifyUserEmailFailedError("Error verifying the user email.") from exc


Expand All @@ -161,6 +184,9 @@ def deactivate_user(container: ops.model.Container, username: str) -> None:
Args:
container: Synapse container.
username: Username to create the access token.

Raises:
MASDeactivateUserFailedError: when deactivation of the user fails
"""
command = [
MAS_EXECUTABLE_PATH,
Expand All @@ -172,8 +198,12 @@ def deactivate_user(container: ops.model.Container, username: str) -> None:
username,
]

process = container.exec(command=command, working_dir=MAS_WORKING_DIR, combine_stderr=True)
process.wait_output()
try:
process = container.exec(command=command, working_dir=MAS_WORKING_DIR)
process.wait_output()
except ops.pebble.ExecError as exc:
logger.error("Error registering new user: %s", exc.stderr)
raise MASDeactivateUserFailedError("Error deactivating user.") from exc


def generate_mas_config(
Expand Down
4 changes: 2 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pebble
import synapse
from auth.mas import (
MASDeactivateUserFailedError,
MASRegisterUserFailedError,
MASVerifyUserEmailFailedError,
deactivate_user,
Expand Down Expand Up @@ -537,7 +538,6 @@ def _on_verify_user_email_action(self, event: ActionEvent) -> None:
results = {"verify-user-email": True}
event.set_results(results)

@validate_charm_state
def _on_anonymize_user_action(self, event: ActionEvent) -> None:
"""Anonymize user and report action result.

Expand All @@ -551,7 +551,7 @@ def _on_anonymize_user_action(self, event: ActionEvent) -> None:

try:
deactivate_user(container=container, username=event.params["username"])
except ops.pebble.ExecError as exc:
except MASDeactivateUserFailedError as exc:
logger.exception("Error deactivating user.")
event.fail(str(exc))

Expand Down
Loading