Skip to content

Commit

Permalink
VaultDep should not have direct access to Request.
Browse files Browse the repository at this point in the history
  • Loading branch information
aekblad committed Aug 7, 2024
1 parent 22e2c96 commit 4390114
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Response,
status,
)
from fastapi.datastructures import State
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from typing_extensions import Annotated, Concatenate, ParamSpec

Expand Down Expand Up @@ -501,12 +502,20 @@ def start() -> None:
uvicorn.run(app, host=args.host, port=args.port)


def get_vault(request: Request, secrets_path: str) -> Vault:
def get_app_state(request: Request) -> State:
"""Depend explicitly on app state only, without access to the request."""
return cast(FastAPI, request.app).state


AppStateDep = Annotated[State, Depends(get_app_state)]


def get_vault(state: AppStateDep, secrets_path: str) -> Vault:
"""Return vault instance for use as a FastAPI dependency."""
vault = Vault.from_coerced_attrs(
vault_url=request.app.state.vault_url,
mount_point=request.app.state.mount_point,
chunk_size=request.app.state.chunk_size,
vault_url=state.vault_url,
mount_point=state.mount_point,
chunk_size=state.chunk_size,
secrets_path=secrets_path,
)
return vault
Expand Down

0 comments on commit 4390114

Please sign in to comment.