refactor: use AppKey's instead of string identifiers for web application

This commit is contained in:
2023-12-27 13:41:07 +02:00
parent b4fa10781b
commit dd8d5d130b
13 changed files with 89 additions and 37 deletions

View File

@ -23,7 +23,7 @@ from aiohttp.web import Application
from typing import Any
from ahriman import __version__
from ahriman.core.configuration import Configuration
from ahriman.web.keys import ConfigurationKey
__all__ = ["setup_apispec"]
@ -89,7 +89,7 @@ def _servers(application: Application) -> list[dict[str, Any]]:
Returns:
list[dict[str, Any]]: list (actually only one) of defined web urls
"""
configuration: Configuration = application["configuration"]
configuration = application[ConfigurationKey]
address = configuration.get("web", "address", fallback=None)
if not address:
host = configuration.get("web", "host")

40
src/ahriman/web/keys.py Normal file
View File

@ -0,0 +1,40 @@
#
# Copyright (c) 2021-2023 ahriman team.
#
# This file is part of ahriman
# (see https://github.com/arcan1s/ahriman).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from aiohttp.web import AppKey
from ahriman.core.auth import Auth
from ahriman.core.configuration import Configuration
from ahriman.core.spawn import Spawn
from ahriman.core.status.watcher import Watcher
from ahriman.models.repository_id import RepositoryId
__all__ = [
"AuthKey",
"ConfigurationKey",
"SpawnKey",
"WatcherKey",
]
AuthKey = AppKey("validator", Auth)
ConfigurationKey = AppKey("configuration", Configuration)
SpawnKey = AppKey("spawn", Spawn)
WatcherKey = AppKey("watcher", dict[RepositoryId, Watcher])

View File

@ -154,7 +154,7 @@ def setup_auth(application: Application, configuration: Configuration, validator
setup_session(application, storage)
authorization_policy = _AuthorizationPolicy(validator)
identity_policy = application["identity"] = aiohttp_security.SessionIdentityPolicy()
identity_policy = aiohttp_security.SessionIdentityPolicy()
aiohttp_security.setup(application, identity_policy, authorization_policy)
application.middlewares.append(_auth_handler(validator.allow_read_only))

View File

@ -29,6 +29,7 @@ from ahriman.core.spawn import Spawn
from ahriman.core.status.watcher import Watcher
from ahriman.models.repository_id import RepositoryId
from ahriman.models.user_access import UserAccess
from ahriman.web.keys import AuthKey, ConfigurationKey, SpawnKey, WatcherKey
T = TypeVar("T", str, list[str])
@ -54,8 +55,7 @@ class BaseView(View, CorsViewMixin):
Returns:
Configuration: configuration instance
"""
configuration: Configuration = self.request.app["configuration"]
return configuration
return self.request.app[ConfigurationKey]
@property
def services(self) -> dict[RepositoryId, Watcher]:
@ -65,8 +65,7 @@ class BaseView(View, CorsViewMixin):
Returns:
dict[RepositoryId, Watcher]: map of loaded watchers per known repository
"""
watchers: dict[RepositoryId, Watcher] = self.request.app["watcher"]
return watchers
return self.request.app[WatcherKey]
@property
def sign(self) -> GPG:
@ -86,8 +85,7 @@ class BaseView(View, CorsViewMixin):
Returns:
Spawn: external process spawner instance
"""
spawner: Spawn = self.request.app["spawn"]
return spawner
return self.request.app[SpawnKey]
@property
def validator(self) -> Auth:
@ -97,8 +95,7 @@ class BaseView(View, CorsViewMixin):
Returns:
Auth: authorization service instance
"""
validator: Auth = self.request.app["validator"]
return validator
return self.request.app[AuthKey]
@classmethod
async def get_permission(cls, request: Request) -> UserAccess:

View File

@ -34,6 +34,7 @@ from ahriman.core.status.watcher import Watcher
from ahriman.models.repository_id import RepositoryId
from ahriman.web.apispec import setup_apispec
from ahriman.web.cors import setup_cors
from ahriman.web.keys import AuthKey, ConfigurationKey, SpawnKey, WatcherKey
from ahriman.web.middlewares.exception_handler import exception_handler
from ahriman.web.routes import setup_routes
@ -97,7 +98,7 @@ async def _on_startup(application: Application) -> None:
application.logger.info("server started")
try:
for watcher in application["watcher"].values():
for watcher in application[WatcherKey].values():
watcher.load()
except Exception:
message = "could not load packages"
@ -114,7 +115,7 @@ def run_server(application: Application) -> None:
"""
application.logger.info("start server")
configuration: Configuration = application["configuration"]
configuration = application[ConfigurationKey]
host = configuration.get("web", "host")
port = configuration.getint("web", "port")
unix_socket = _create_socket(configuration, application)
@ -156,7 +157,7 @@ def setup_server(configuration: Configuration, spawner: Spawn, repositories: lis
aiohttp_jinja2.setup(application, trim_blocks=True, lstrip_blocks=True, autoescape=True, loader=loader)
application.logger.info("setup configuration")
application["configuration"] = configuration
application[ConfigurationKey] = configuration
application.logger.info("setup watchers")
if not repositories:
@ -166,13 +167,13 @@ def setup_server(configuration: Configuration, spawner: Spawn, repositories: lis
for repository_id in repositories:
application.logger.info("load repository %s", repository_id)
watchers[repository_id] = Watcher(repository_id, database)
application["watcher"] = watchers
application[WatcherKey] = watchers
application.logger.info("setup process spawner")
application["spawn"] = spawner
application[SpawnKey] = spawner
application.logger.info("setup authorization")
validator = application["validator"] = Auth.load(configuration, database)
validator = application[AuthKey] = Auth.load(configuration, database)
if validator.enabled:
from ahriman.web.middlewares.auth_handler import setup_auth
setup_auth(application, configuration, validator)