Skip to content

Commit

Permalink
fix: do not call dataservice through network from itself (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Panaetius authored Jan 10, 2025
1 parent d3329d7 commit 2ee5f5d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
1 change: 1 addition & 0 deletions bases/renku_data_services/data_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def register_all_handlers(app: Sanic, config: Config) -> Sanic:
session_repo=config.session_repo,
storage_repo=config.storage_repo,
rp_repo=config.rp_repo,
user_repo=config.kc_user_repo,
data_connector_repo=config.data_connector_repo,
data_connector_project_link_repo=config.data_connector_to_project_link_repo,
data_connector_secret_repo=config.data_connector_secret_repo,
Expand Down
7 changes: 5 additions & 2 deletions components/renku_data_services/notebooks/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
renku_2_make_server_name,
)
from renku_data_services.notebooks.utils import (
get_user_secret,
merge_node_affinities,
node_affinity_from_resource_class,
tolerations_from_resource_class,
Expand All @@ -78,6 +77,7 @@
from renku_data_services.session.db import SessionRepository
from renku_data_services.storage.db import StorageRepository
from renku_data_services.users.db import UserRepo
from renku_data_services.utils.cryptography import get_encryption_key


@dataclass(kw_only=True)
Expand Down Expand Up @@ -241,6 +241,7 @@ class NotebooksNewBP(CustomBlueprint):
session_repo: SessionRepository
rp_repo: ResourcePoolRepository
storage_repo: StorageRepository
user_repo: UserRepo
data_connector_repo: DataConnectorRepository
data_connector_project_link_repo: DataConnectorProjectLinkRepository
data_connector_secret_repo: DataConnectorSecretRepository
Expand Down Expand Up @@ -340,7 +341,9 @@ async def _handler(
data_sources: list[DataSource] = []
user_secret_key: str | None = None
if isinstance(user, AuthenticatedAPIUser) and len(dcs_secrets) > 0:
user_secret_key = await get_user_secret(self.nb_config.data_service_url, user)
secret_key = await self.user_repo.get_or_create_user_secret_key(requested_by=user)
user_secret_key = get_encryption_key(secret_key.encode(), user.id.encode()).decode("utf-8")

for cs_id, cs in dcs.items():
secret_name = f"{server_name}-ds-{cs_id.lower()}"
secret_key_needed = len(dcs_secrets.get(cs_id, [])) > 0
Expand Down
19 changes: 0 additions & 19 deletions components/renku_data_services/notebooks/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"""Utilities for notebooks."""

import httpx

import renku_data_services.crc.models as crc_models
from renku_data_services.base_models.core import AuthenticatedAPIUser
from renku_data_services.notebooks.crs import (
MatchExpression,
NodeAffinity,
Expand All @@ -13,7 +10,6 @@
RequiredDuringSchedulingIgnoredDuringExecution,
Toleration,
)
from renku_data_services.utils.cryptography import get_encryption_key


def merge_node_affinities(
Expand Down Expand Up @@ -99,18 +95,3 @@ def tolerations_from_resource_class(resource_class: crc_models.ResourceClass) ->
for tol in resource_class.tolerations:
output.append(Toleration(key=tol, operator="Exists"))
return output


async def get_user_secret(data_svc_url: str, user: AuthenticatedAPIUser) -> str | None:
"""Get the user secret key from the secret service."""

async with httpx.AsyncClient(timeout=5) as client:
response = await client.get(
f"{data_svc_url}/user/secret_key",
headers={"Authorization": f"Bearer {user.access_token}"},
)
if response.status_code != 200:
return None
user_key = response.json()

return get_encryption_key(user_key["secret_key"].encode(), user.id.encode()).decode("utf-8")

0 comments on commit 2ee5f5d

Please sign in to comment.