Refactor backend views, update secret model #24

Merged
eising merged 4 commits from feature/expanded-secrets into main 2025-06-08 15:45:00 +00:00
5 changed files with 13 additions and 8 deletions
Showing only changes of commit a7a09f7784 - Show all commits

View File

@ -4,7 +4,7 @@
import logging import logging
from typing import Any from typing import Any
from fastapi import APIRouter, Depends, Request from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field, TypeAdapter from pydantic import BaseModel, Field, TypeAdapter
from sqlalchemy import select, func, and_ from sqlalchemy import select, func, and_
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession

View File

@ -22,7 +22,7 @@ from sshecret_backend.view_models import (
) )
from sshecret_backend import audit from sshecret_backend import audit
from sshecret_backend.types import AsyncDBSessionDep from sshecret_backend.types import AsyncDBSessionDep
from .common import get_client_by_id_or_name from .common import get_client_by_id_or_name, get_client_by_name
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -146,7 +146,7 @@ def get_secrets_api(get_db_session: AsyncDBSessionDep) -> APIRouter:
session: Annotated[AsyncSession, Depends(get_db_session)], session: Annotated[AsyncSession, Depends(get_db_session)],
) -> None: ) -> None:
"""Delete a secret.""" """Delete a secret."""
client = await get_client_by_id_or_name(session, name) client = await get_client_by_name(session, name)
if not client: if not client:
raise HTTPException( raise HTTPException(
status_code=404, detail="Cannot find a client with the given name." status_code=404, detail="Cannot find a client with the given name."
@ -217,8 +217,10 @@ def get_secrets_api(get_db_session: AsyncDBSessionDep) -> APIRouter:
clients: list[str] = [] clients: list[str] = []
client_secrets = await session.scalars( client_secrets = await session.scalars(
select(ClientSecret) select(ClientSecret)
.join(ClientSecret.client)
.options(selectinload(ClientSecret.client)) .options(selectinload(ClientSecret.client))
.where(ClientSecret.name == name) .where(ClientSecret.name == name)
.where(Client.is_active.is_(True))
) )
for client_secret in client_secrets.all(): for client_secret in client_secrets.all():
if not client_secret.client: if not client_secret.client:
@ -235,7 +237,10 @@ def get_secrets_api(get_db_session: AsyncDBSessionDep) -> APIRouter:
"""Get a list of which clients has a named secret.""" """Get a list of which clients has a named secret."""
detail_list = ClientSecretDetailList(name=name) detail_list = ClientSecretDetailList(name=name)
client_secrets = await session.scalars( client_secrets = await session.scalars(
select(ClientSecret).where(ClientSecret.name == name) select(ClientSecret)
.options(selectinload(ClientSecret.client))
.where(ClientSecret.name == name)
.where(ClientSecret.client.is_(Client.is_active))
) )
for client_secret in client_secrets.all(): for client_secret in client_secrets.all():
if not client_secret.client: if not client_secret.client:

View File

@ -6,7 +6,7 @@ import sqlite3
from collections.abc import AsyncIterator, Generator, Callable from collections.abc import AsyncIterator, Generator, Callable
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import Any, Literal from typing import Literal
from sqlalchemy import create_engine, Engine, event, select from sqlalchemy import create_engine, Engine, event, select
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession, async_sessionmaker, create_async_engine, AsyncEngine from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession, async_sessionmaker, create_async_engine, AsyncEngine

View File

@ -2,7 +2,7 @@
from collections.abc import AsyncGenerator, Callable, Generator from collections.abc import AsyncGenerator, Callable, Generator
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session from sqlalchemy.orm import Session