Skip to content

Commit

Permalink
Merge pull request #1991 from DSD-DBS/restructure-hooks
Browse files Browse the repository at this point in the history
refactor: Consolidate session hook request arguments into one
  • Loading branch information
MoritzWeber0 authored Nov 13, 2024
2 parents 90bb353 + 53f9559 commit 1b940f4
Show file tree
Hide file tree
Showing 29 changed files with 582 additions and 664 deletions.
24 changes: 12 additions & 12 deletions backend/capellacollab/sessions/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
"pure_variants": pure_variants.PureVariantsIntegration(),
}

REGISTER_HOOKS_AUTO_USE: dict[str, interface.HookRegistration] = {
"persistent_workspace": persistent_workspace.PersistentWorkspaceHook(),
"guacamole": guacamole.GuacamoleIntegration(),
"http": http.HTTPIntegration(),
"read_only_hook": read_only_workspace.ReadOnlyWorkspaceHook(),
"provisioning": provisioning.ProvisionWorkspaceHook(),
"session_preparation": session_preparation.GitRepositoryCloningHook(),
"networking": networking.NetworkingIntegration(),
"authentication": authentication.PreAuthenticationHook(),
"log_collector": log_collector.LogCollectorIntegration(),
}
REGISTER_HOOKS_AUTO_USE: list[interface.HookRegistration] = [
persistent_workspace.PersistentWorkspaceHook(),
guacamole.GuacamoleIntegration(),
http.HTTPIntegration(),
read_only_workspace.ReadOnlyWorkspaceHook(),
provisioning.ProvisionWorkspaceHook(),
session_preparation.GitRepositoryCloningHook(),
networking.NetworkingIntegration(),
authentication.PreAuthenticationHook(),
log_collector.LogCollectorIntegration(),
]


def get_activated_integration_hooks(
Expand All @@ -46,4 +46,4 @@ def get_activated_integration_hooks(
hook
for integration, hook in REGISTERED_HOOKS.items()
if getattr(tool.integrations, integration, False)
] + list(REGISTER_HOOKS_AUTO_USE.values())
] + REGISTER_HOOKS_AUTO_USE
8 changes: 3 additions & 5 deletions backend/capellacollab/sessions/hooks/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@


class PreAuthenticationHook(interface.HookRegistration):
def session_connection_hook( # type: ignore[override]
def session_connection_hook(
self,
db_session: sessions_models.DatabaseSession,
user: users_models.DatabaseUser,
**kwargs,
request: interface.SessionConnectionHookRequest,
) -> interface.SessionConnectionHookResult:
"""Issue pre-authentication tokens for sessions"""

return interface.SessionConnectionHookResult(
cookies={
"ccm_session_token": self._issue_session_token(
user, db_session
request.user, request.db_session
)
}
)
Expand Down
39 changes: 14 additions & 25 deletions backend/capellacollab/sessions/hooks/guacamole.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

from capellacollab.config import config
from capellacollab.core import credentials
from capellacollab.sessions import models as sessions_models
from capellacollab.sessions.operators import k8s
from capellacollab.tools import models as tools_models

from . import interface

Expand All @@ -40,14 +37,11 @@ class GuacamoleIntegration(interface.HookRegistration):
"https": None,
}

def post_session_creation_hook( # type: ignore[override]
def post_session_creation_hook(
self,
session: k8s.Session,
db_session: sessions_models.DatabaseSession,
connection_method: tools_models.ToolSessionConnectionMethod,
**kwargs,
request: interface.PostSessionCreationHookRequest,
) -> interface.PostSessionCreationHookResult:
if connection_method.type != "guacamole":
if request.connection_method.type != "guacamole":
return interface.PostSessionCreationHookResult()

guacamole_username = credentials.generate_password()
Expand All @@ -60,9 +54,9 @@ def post_session_creation_hook( # type: ignore[override]

guacamole_identifier = self._create_connection(
guacamole_token,
db_session.environment["CAPELLACOLLAB_SESSION_TOKEN"],
session["host"],
session["port"],
request.db_session.environment["CAPELLACOLLAB_SESSION_TOKEN"],
request.session["host"],
request.session["port"],
)["identifier"]

self._assign_user_to_connection(
Expand All @@ -79,16 +73,14 @@ def post_session_creation_hook( # type: ignore[override]
config=guacamole_config,
)

def session_connection_hook( # type: ignore[override]
def session_connection_hook(
self,
db_session: sessions_models.DatabaseSession,
connection_method: tools_models.ToolSessionConnectionMethod,
**kwargs,
request: interface.SessionConnectionHookRequest,
) -> interface.SessionConnectionHookResult:
if connection_method.type != "guacamole":
if request.connection_method.type != "guacamole":
return interface.SessionConnectionHookResult()

session_config = db_session.config
session_config = request.db_session.config

if not session_config or not session_config.get("guacamole_username"):
return interface.SessionConnectionHookResult()
Expand All @@ -102,16 +94,13 @@ def session_connection_hook( # type: ignore[override]
redirect_url=config.extensions.guacamole.public_uri + "/#/",
)

def pre_session_termination_hook( # type: ignore[override]
self,
session: sessions_models.DatabaseSession,
connection_method: tools_models.ToolSessionConnectionMethod,
**kwargs,
def pre_session_termination_hook(
self, request: interface.PreSessionTerminationHookRequest
) -> interface.PreSessionTerminationHookResult:
if connection_method.type != "guacamole":
if request.connection_method.type != "guacamole":
return interface.SessionConnectionHookResult()

session_config = session.config
session_config = request.session.config

if session_config and session_config.get("guacamole_username"):
guacamole_token = self._get_admin_token()
Expand Down
23 changes: 9 additions & 14 deletions backend/capellacollab/sessions/hooks/http.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import logging

from capellacollab.core import models as core_models
from capellacollab.tools import models as tools_models

from .. import models as sessions_models
from .. import util as sessions_util
from . import interface


class HTTPIntegration(interface.HookRegistration):
def session_connection_hook( # type: ignore[override]
self,
db_session: sessions_models.DatabaseSession,
connection_method: tools_models.ToolSessionConnectionMethod,
logger: logging.LoggerAdapter,
**kwargs,
def session_connection_hook(
self, request: interface.SessionConnectionHookRequest
) -> interface.SessionConnectionHookResult:
if not isinstance(
connection_method, tools_models.HTTPConnectionMethod
request.connection_method, tools_models.HTTPConnectionMethod
):
return interface.SessionConnectionHookResult()

try:
redirect_url = connection_method.redirect_url.format(
**db_session.environment
redirect_url = request.connection_method.redirect_url.format(
**request.db_session.environment
)
except Exception:
logger.error(
request.logger.error(
"Error while formatting the redirect URL", exc_info=True
)
return interface.SessionConnectionHookResult(
Expand All @@ -43,7 +36,9 @@ def session_connection_hook( # type: ignore[override]
)

cookies, warnings = sessions_util.resolve_environment_variables(
logger, db_session.environment, connection_method.cookies
request.logger,
request.db_session.environment,
request.connection_method.cookies,
)

return interface.SessionConnectionHookResult(
Expand Down
Loading

0 comments on commit 1b940f4

Please sign in to comment.