From 57f45fdc8964907a7a094acf652c57b454d4f351 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Fri, 17 Sep 2021 19:15:53 +0300 Subject: [PATCH] better reload --- src/ahriman/application/handlers/setup.py | 2 +- src/ahriman/application/handlers/user.py | 2 +- src/ahriman/core/status/web_client.py | 2 +- src/ahriman/web/views/service/reload_auth.py | 10 +++++++++- .../service/test_views_service_reload_auth.py | 18 +++++++++++++++--- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/ahriman/application/handlers/setup.py b/src/ahriman/application/handlers/setup.py index d2ac8d25..9e3d34c6 100644 --- a/src/ahriman/application/handlers/setup.py +++ b/src/ahriman/application/handlers/setup.py @@ -152,7 +152,7 @@ class Setup(Handler): :param architecture: repository architecture """ command = Setup.build_command(prefix, architecture) - Setup.SUDOERS_PATH.write_text(f"ahriman ALL=(ALL) NOPASSWD: {command} *\n") + Setup.SUDOERS_PATH.write_text(f"ahriman ALL=(ALL) NOPASSWD: {command} *\n", encoding="utf8") Setup.SUDOERS_PATH.chmod(0o400) # security! @staticmethod diff --git a/src/ahriman/application/handlers/user.py b/src/ahriman/application/handlers/user.py index 4b4d45fa..50b9b4f0 100644 --- a/src/ahriman/application/handlers/user.py +++ b/src/ahriman/application/handlers/user.py @@ -52,7 +52,7 @@ class User(Handler): User.clear_user(auth_configuration, user) if not args.remove: User.create_configuration(auth_configuration, user, salt, args.as_service) - User.write_configuration(configuration) + User.write_configuration(auth_configuration) if not args.no_reload: client = Application(architecture, configuration, no_report=False).repository.reporter diff --git a/src/ahriman/core/status/web_client.py b/src/ahriman/core/status/web_client.py index 5c75df7e..7c2acbcf 100644 --- a/src/ahriman/core/status/web_client.py +++ b/src/ahriman/core/status/web_client.py @@ -72,7 +72,7 @@ class WebClient(Client): """ :return: full url for web service to reload authentication module """ - return f"{self.address}/status-api/v1/reload-auth" + return f"{self.address}/service-api/v1/reload-auth" @property def _status_url(self) -> str: diff --git a/src/ahriman/web/views/service/reload_auth.py b/src/ahriman/web/views/service/reload_auth.py index c0366cae..fb9cf64d 100644 --- a/src/ahriman/web/views/service/reload_auth.py +++ b/src/ahriman/web/views/service/reload_auth.py @@ -35,6 +35,14 @@ class ReloadAuthView(BaseView): :return: 204 on success """ self.configuration.reload() - self.request.app["validator"] = Auth.load(self.configuration) + + try: + import aiohttp_security # type: ignore + self.request.app[aiohttp_security.api.AUTZ_KEY].validator =\ + self.request.app["validator"] =\ + Auth.load(self.configuration) + except (ImportError, KeyError): + self.request.app.logger.warning("could not update authentication module validator", exc_info=True) + raise return HTTPNoContent() diff --git a/tests/ahriman/web/views/service/test_views_service_reload_auth.py b/tests/ahriman/web/views/service/test_views_service_reload_auth.py index 891bdd97..793570e3 100644 --- a/tests/ahriman/web/views/service/test_views_service_reload_auth.py +++ b/tests/ahriman/web/views/service/test_views_service_reload_auth.py @@ -2,14 +2,26 @@ from aiohttp.test_utils import TestClient from pytest_mock import MockerFixture -async def test_post(client: TestClient, mocker: MockerFixture) -> None: +async def test_post(client_with_auth: TestClient, mocker: MockerFixture) -> None: """ must call post request correctly """ + mocker.patch("aiohttp_security.check_permission", return_value=True) reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload") load_mock = mocker.patch("ahriman.core.auth.auth.Auth.load") - response = await client.post("/service-api/v1/reload-auth") + response = await client_with_auth.post("/service-api/v1/reload-auth") assert response.ok reload_mock.assert_called_once() - load_mock.assert_called_with(client.app["configuration"]) + load_mock.assert_called_with(client_with_auth.app["configuration"]) + + +async def test_post_no_auth(client: TestClient, mocker: MockerFixture) -> None: + """ + must call return 500 if no authorization module loaded + """ + reload_mock = mocker.patch("ahriman.core.configuration.Configuration.reload") + response = await client.post("/service-api/v1/reload-auth") + + assert response.status == 500 + reload_mock.assert_called_once()