Skip to content

Commit

Permalink
Merge pull request #7 from volvo-cars/vaultdep-explicit-secrets-path
Browse files Browse the repository at this point in the history
Have VaultDep depend explicitly on secrets_path.
  • Loading branch information
aekblad authored Aug 7, 2024
2 parents fc0bb1d + 4390114 commit 9422175
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 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,13 +502,21 @@ def start() -> None:
uvicorn.run(app, host=args.host, port=args.port)


def get_vault(request: Request) -> 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,
secrets_path=request.path_params["secrets_path"],
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 9422175

Please sign in to comment.