From d73d5daad3aa5424c390d3cff32cc495ee62148a Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Sun, 26 Sep 2021 12:27:46 +0300 Subject: [PATCH] add debugtoolbar support --- docs/configuration.md | 3 +++ package/archlinux/PKGBUILD | 1 + package/etc/ahriman.ini | 3 +++ setup.py | 1 + src/ahriman/web/web.py | 8 ++++++++ tests/ahriman/web/conftest.py | 17 +++++++++++++++++ tests/ahriman/web/test_web.py | 15 ++++++++++++++- tests/testresources/core/ahriman.ini | 3 +++ 8 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index b03837ea..464cfa9a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -125,6 +125,9 @@ Group name must refer to architecture, e.g. it should be `s3:x86_64` for x86_64 Web server settings. If any of `host`/`port` is not set, web integration will be disabled. Group name must refer to architecture, e.g. it should be `web:x86_64` for x86_64 architecture. * `address` - optional address in form `proto://host:port` (`port` can be omitted in case of default `proto` ports), will be used instead of `http://{host}:{port}` in case if set, string, optional. This option is required in case if `OAuth` provider is used. +* `debug` - enable debug toolbar, boolean, optional, default `no`. +* `debug_check_host` - check hosts to access debug toolbar, boolean, optional, default `no`. +* `debug_allowed_hosts` - allowed hosts to get access to debug toolbar, space separated list of string, optional. * `host` - host to bind, string, optional. * `password` - password to authorize in web service in order to update service status, string, required in case if authorization enabled. * `port` - port to bind, int, optional. diff --git a/package/archlinux/PKGBUILD b/package/archlinux/PKGBUILD index 1f006005..9d859c0b 100644 --- a/package/archlinux/PKGBUILD +++ b/package/archlinux/PKGBUILD @@ -15,6 +15,7 @@ optdepends=('breezy: -bzr packages support' 'mercurial: -hg packages support' 'python-aioauth-client: web server with OAuth2 authorization' 'python-aiohttp: web server' + 'python-aiohttp-debugtoolbar: web server with enabled debug panel' 'python-aiohttp-jinja2: web server' 'python-aiohttp-security: web server with authorization' 'python-aiohttp-session: web server with authorization' diff --git a/package/etc/ahriman.ini b/package/etc/ahriman.ini index 094b9b1f..ec1b3237 100644 --- a/package/etc/ahriman.ini +++ b/package/etc/ahriman.ini @@ -51,6 +51,9 @@ command = rsync --archive --compress --partial --delete chunk_size = 8388608 [web] +debug = no +debug_check_host = no +debug_allowed_hosts = host = 127.0.0.1 static_path = /usr/share/ahriman/static templates = /usr/share/ahriman \ No newline at end of file diff --git a/setup.py b/setup.py index a11d9f86..d25443e8 100644 --- a/setup.py +++ b/setup.py @@ -110,6 +110,7 @@ setup( "aiohttp", "aiohttp_jinja2", "aioauth-client", + "aiohttp_debugtoolbar", "aiohttp_session", "aiohttp_security", "cryptography", diff --git a/src/ahriman/web/web.py b/src/ahriman/web/web.py index d97f65e5..c68ae38c 100644 --- a/src/ahriman/web/web.py +++ b/src/ahriman/web/web.py @@ -99,6 +99,14 @@ def setup_service(architecture: str, configuration: Configuration, spawner: Spaw application.logger.info("setup process spawner") application["spawn"] = spawner + application.logger.info("setup debug panel") + debug_enabled = configuration.getboolean("web", "debug", fallback=False) + if debug_enabled: + import aiohttp_debugtoolbar # type: ignore + aiohttp_debugtoolbar.setup(application, + hosts=configuration.getlist("web", "debug_allowed_hosts", fallback=[]), + check_host=configuration.getboolean("web", "debug_check_host", fallback=False)) + application.logger.info("setup authorization") validator = application["validator"] = Auth.load(configuration) if validator.enabled: diff --git a/tests/ahriman/web/conftest.py b/tests/ahriman/web/conftest.py index fc47d8e8..348cc93c 100644 --- a/tests/ahriman/web/conftest.py +++ b/tests/ahriman/web/conftest.py @@ -64,3 +64,20 @@ def application_with_auth(configuration: Configuration, user: User, spawner: Spa application["validator"]._users[generated.username] = generated return application + + +@pytest.fixture +def application_with_debug(configuration: Configuration, user: User, spawner: Spawn, + mocker: MockerFixture) -> web.Application: + """ + application fixture with debug enabled + :param configuration: configuration fixture + :param user: user descriptor fixture + :param spawner: spawner fixture + :param mocker: mocker object + :return: application test instance + """ + configuration.set_option("web", "debug", "yes") + mocker.patch.object(ahriman.core.auth.helpers, "_has_aiohttp_security", False) + mocker.patch("pathlib.Path.mkdir") + return setup_service("x86_64", configuration, spawner) diff --git a/tests/ahriman/web/test_web.py b/tests/ahriman/web/test_web.py index a3372a54..e2edd8db 100644 --- a/tests/ahriman/web/test_web.py +++ b/tests/ahriman/web/test_web.py @@ -45,7 +45,7 @@ def test_run(application: web.Application, mocker: MockerFixture) -> None: def test_run_with_auth(application_with_auth: web.Application, mocker: MockerFixture) -> None: """ - must run application + must run application with enabled authorization """ port = 8080 application_with_auth["configuration"].set_option("web", "port", str(port)) @@ -54,3 +54,16 @@ def test_run_with_auth(application_with_auth: web.Application, mocker: MockerFix run_server(application_with_auth) run_application_mock.assert_called_with(application_with_auth, host="127.0.0.1", port=port, handle_signals=False, access_log=pytest.helpers.anyvar(int)) + + +def test_run_with_debug(application_with_debug: web.Application, mocker: MockerFixture) -> None: + """ + must run application with enabled debug panel + """ + port = 8080 + application_with_debug["configuration"].set_option("web", "port", str(port)) + run_application_mock = mocker.patch("aiohttp.web.run_app") + + run_server(application_with_debug) + run_application_mock.assert_called_with(application_with_debug, host="127.0.0.1", port=port, + handle_signals=False, access_log=pytest.helpers.anyvar(int)) diff --git a/tests/testresources/core/ahriman.ini b/tests/testresources/core/ahriman.ini index 54ade25c..f7ccf833 100644 --- a/tests/testresources/core/ahriman.ini +++ b/tests/testresources/core/ahriman.ini @@ -62,6 +62,9 @@ region = eu-central-1 secret_key = [web] +debug = no +debug_check_host = no +debug_allowed_hosts = host = 127.0.0.1 static_path = ../web/templates/static templates = ../web/templates \ No newline at end of file