Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cloud_pipelines_backend/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .instrumentation import contextual_logging

if typing.TYPE_CHECKING:
from . import secret_value_store
from .launchers import interfaces as launcher_interfaces


Expand All @@ -40,6 +41,7 @@ def setup_routes(
container_launcher_for_log_streaming: "launcher_interfaces.ContainerTaskLauncher[launcher_interfaces.LaunchedContainer] | None" = None,
default_component_library_owner_username: str = "admin",
do_skip_backfill: bool = False,
secret_value_store: "secret_value_store.SecretValueStore | None" = None,
):
def get_session():
with orm.Session(autocommit=False, autoflush=False, bind=db_engine) as session:
Expand Down Expand Up @@ -76,6 +78,7 @@ async def lifespan(app: fastapi.FastAPI):
user_details_getter=user_details_getter,
get_launcher=get_launcher,
lifespan=lifespan,
secret_value_store=secret_value_store,
)


Expand All @@ -93,6 +96,7 @@ def _setup_routes_internal(
| typing.Callable[..., abc.Iterator[typing.Any]]
| None
) = None,
secret_value_store: "secret_value_store.SecretValueStore | None" = None,
):
# We request `app: fastapi.FastAPI` instead of just returning the router
# because we want to add exception handler which is only supported for `FastAPI`.
Expand Down Expand Up @@ -150,6 +154,15 @@ def handle_api_validation_error(
content={"detail": str(exc)},
)

@app.exception_handler(errors.SecretStorageTypeMismatchError)
def handle_secret_storage_type_mismatch_error(
request: fastapi.Request, exc: errors.SecretStorageTypeMismatchError
):
return fastapi.responses.JSONResponse(
status_code=fastapi.status.HTTP_503_SERVICE_UNAVAILABLE,
content={"message": str(exc)},
)

@app.exception_handler(NotImplementedError)
def handle_not_implemented_error(
request: fastapi.Request, exc: NotImplementedError
Expand Down Expand Up @@ -462,7 +475,9 @@ def get_current_user(
)

### Secrets routes
secrets_service = api_server_sql.SecretsApiService()
secrets_service = api_server_sql.SecretsApiService(
secret_value_store=secret_value_store
)

router.get("/api/secrets/", tags=["secrets"], **default_config)(
inject_session_dependency(
Expand Down
38 changes: 36 additions & 2 deletions cloud_pipelines_backend/api_server_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .access_control import checks as access_control_checks
from .access_control import runs as access_control_runs
from . import filter_query_sql
from . import secret_value_store

if typing.TYPE_CHECKING:
from cloud_pipelines.orchestration.storage_providers import (
Expand Down Expand Up @@ -1256,6 +1257,13 @@ class ListSecretsResponse:


class SecretsApiService:
def __init__(
self,
*,
secret_value_store: secret_value_store.SecretValueStore | None = None,
):
self._secret_value_store = secret_value_store

def create_secret(
self,
*,
Expand Down Expand Up @@ -1318,7 +1326,9 @@ def _create_or_update_secret(
raise errors.ItemAlreadyExistsError(
f"Secret with name '{secret_name}' already exists."
)
secret.secret_value = secret_value
secret_value_store.raise_if_storage_type_mismatch(
secret=secret, store=self._secret_value_store
)
secret.updated_at = current_time
else:
if raise_if_not_exists:
Expand All @@ -1328,17 +1338,30 @@ def _create_or_update_secret(
secret = bts.Secret(
user_id=user_id,
secret_name=secret_name,
secret_value=secret_value,
# Set below, through the store when there is one.
secret_value="",
created_at=current_time,
updated_at=current_time,
)
session.add(secret)
# The new value is saved before the row commits, and the one it replaces is retired after,
# so the row never names a value that is already gone.
previous_storage_details = secret_value_store.store_secret_value(
secret=secret, value=secret_value, store=self._secret_value_store
)
if description:
secret.description = description
if expires_at:
secret.expires_at = expires_at
response = SecretInfoResponse.from_db(secret)
session.commit()
if previous_storage_details is not None:
secret_value_store.retire_secret_value(
store=self._secret_value_store,
storage_details=previous_storage_details,
user_id=user_id,
secret_name=secret_name,
)
return response

def delete_secret(
Expand All @@ -1353,8 +1376,19 @@ def delete_secret(
raise errors.ItemNotFoundError(
f"Secret with name '{secret_name}' does not exist."
)
secret_value_store.raise_if_storage_type_mismatch(
secret=secret, store=self._secret_value_store
)
storage_details = secret_value_store.get_storage_details(secret=secret)
session.delete(secret)
session.commit()
if storage_details is not None:
secret_value_store.retire_secret_value(
store=self._secret_value_store,
storage_details=storage_details,
user_id=user_id,
secret_name=secret_name,
)

def list_secrets(
self,
Expand Down
4 changes: 4 additions & 0 deletions cloud_pipelines_backend/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class PermissionError(Exception):
pass


class SecretStorageTypeMismatchError(Exception):
"""A secret's value is in a store of another type than this server's, or the server has none, so it cannot be changed."""


class ApiValidationError(Exception):
"""Base for all filter/annotation validation errors -> 422."""

Expand Down
126 changes: 126 additions & 0 deletions cloud_pipelines_backend/secret_value_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import logging
import typing
from typing import Any, Final

from . import backend_types_sql as bts
from . import errors

_logger = logging.getLogger(__name__)

EXTRA_DATA_STORAGE_KEY: Final[str] = "storage"
STORAGE_TYPE_KEY: Final[str] = "type"
STORAGE_DETAILS_KEY: Final[str] = "details"


class SecretValueStore(typing.Protocol):
"""Keeps secret values outside the database row, for example in a cloud secret manager."""

# Recorded in the row's `extra_data`, so later reads and writes know which store holds the value.
type: str

def save(
self, *, user_id: str, secret_name: str, value: str
) -> tuple[str, dict[str, Any]]:
"""Saves the value; returns what the row's `secret_value` keeps, and the storage details for `get` and `delete`.

Example, a store that keeps numbered versions: `save(user_id="alice", secret_name="HF_TOKEN",
value="hf_FAKE123")` returns `("", {"version": "4"})` — the row keeps `""`, and `{"version": "4"}`
is what `get` and `delete` are later given as `storage_details`.
"""
...

def get(
self, *, storage_details: dict[str, Any], user_id: str, secret_name: str
) -> str:
"""Returns the value that `save` saved with these storage details."""
...

def delete(
self, *, storage_details: dict[str, Any], user_id: str, secret_name: str
) -> None:
"""Retires the value that `save` saved with these storage details."""
...


def get_storage_type(*, secret: bts.Secret) -> str | None:
"""Returns the type of the store holding the row's value, or None when the value is plain in the row.

Example: with `extra_data = {"storage": {"type": "gsm", "details": {"version": "4"}}}` it returns `"gsm"`.
"""
storage = (secret.extra_data or {}).get(EXTRA_DATA_STORAGE_KEY)
return storage[STORAGE_TYPE_KEY] if storage else None


def get_storage_details(*, secret: bts.Secret) -> dict[str, Any] | None:
"""Returns what the store needs to find the row's value, or None when the value is plain in the row.

Example: with `extra_data = {"storage": {"type": "gsm", "details": {"version": "4"}}}` it returns
`{"version": "4"}`.
"""
storage = (secret.extra_data or {}).get(EXTRA_DATA_STORAGE_KEY)
return storage[STORAGE_DETAILS_KEY] if storage else None


def store_secret_value(
*, secret: bts.Secret, value: str, store: SecretValueStore | None
) -> dict[str, Any] | None:
"""Sets the row's value, through the store if one is given.

Returns the storage details of the value it replaces in the store, or None when there was none.
"""
if store is None:
secret.secret_value = value
return None
previous_storage_details = get_storage_details(secret=secret)
secret.secret_value, storage_details = store.save(
user_id=secret.user_id, secret_name=secret.secret_name, value=value
)
# A merge: the copy keeps every other key, so only the storage entry changes.
secret.extra_data = {
**(secret.extra_data or {}),
EXTRA_DATA_STORAGE_KEY: {
STORAGE_TYPE_KEY: store.type,
STORAGE_DETAILS_KEY: storage_details,
},
}
return previous_storage_details


def raise_if_storage_type_mismatch(
*, secret: bts.Secret, store: SecretValueStore | None
) -> None:
"""Raises when the row's value is in a store other than this server's, leaving the row unchanged.

Writing plain text over it, or deleting the row, would leave the stored value behind and still readable.
"""
storage_type = get_storage_type(secret=secret)
if storage_type is None:
return
configured = store.type if store else None
if storage_type != configured:
raise errors.SecretStorageTypeMismatchError(
f"Secret '{secret.secret_name}' is kept in the {storage_type!r} store, "
f"but this server's secret store is {configured!r}; it cannot be changed or deleted here."
)


def retire_secret_value(
*,
store: SecretValueStore,
storage_details: dict[str, Any],
user_id: str,
secret_name: str,
) -> None:
"""Retires a stored value the row no longer names, logging a failure instead of raising it.

A helper for after the row commits: the change is already saved, so raising would report a successful save
as a failure, and the only harm of a failed retire is an old value left enabled in the store.
"""
try:
store.delete(
storage_details=storage_details, user_id=user_id, secret_name=secret_name
)
except Exception:
_logger.exception(
f"Could not retire the previous stored value of secret '{secret_name}'."
Comment thread
yuechao-qin marked this conversation as resolved.
Dismissed
)
Loading
Loading