~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
FastAPI authentication dependencies.
Replaces Flask's @login_required decorator and g.db_session / g.current_user
with explicit dependency injection.
"""
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Annotated, Generator
from fastapi import Depends, HTTPException, Request
from loguru import logger
from sqlalchemy.orm import Session
from ...database.encrypted_db import (
UserConnectionStatus,
db_manager,
sidecar_wait_budget,
)
from ...database.rekey_lock import RekeyOperationalError
from ...database.session_context import get_user_db_session
from ...database.session_passwords import session_password_store
from ...utilities.db_utils import get_settings_manager
from ..auth.session_manager import (
SESSION_VALIDATION_UNAVAILABLE_DETAIL,
VALIDATED_SESSION_STATE_KEY,
SessionValidationUnavailableError,
session_manager,
)
def get_session_username(request: Request) -> str | None:
"""Get the username from the session, or None if not authenticated."""
return request.session.get("username")
# Type names of the rekey operational faults that DatabaseMiddleware's reopen
# of the user's database met in the current request (ensure_user_database);
# None outside a request_database_scope. A list, so the reopen that runs on a
# worker thread (with a copy of the request's context) can append to the one
# require_auth reads later on another.
#
# Request-scoped state: contexts copied from the request (research, RAG and
# post-login threads) keep this list after the request ends, so it must only
# be read by require_auth (through _database_connection_status), in the
# request that wrote it, never from background work.
_reopen_faults: ContextVar[list[str] | None] = ContextVar(
"ldr_request_reopen_faults", default=None
)
@contextmanager
def request_database_scope() -> Generator[None, None, None]:
"""Scope one HTTP request's database bookkeeping (``DatabaseMiddleware``).
* The request's waits for the user's rekey sidecar share a give-up flag
(``sidecar_wait_budget``): DatabaseMiddleware's probe and reopen,
``require_auth``'s probes and the route's own database opens. A wait
that gives up sets it, and each later claim then tries once without
waiting, until one of them gets the sidecar again. The flag is not a
running total of the time waited.
* A rekey operational fault that the reopen in ``ensure_user_database``
met is kept for ``require_auth``, which answers the missing engine
with the retryable 503 instead of the 401 that ``/`` turns into a
logout.
"""
token = _reopen_faults.set([])
try:
with sidecar_wait_budget():
yield
finally:
_reopen_faults.reset(token)
def _record_reopen_fault(fault: RekeyOperationalError) -> None:
faults = _reopen_faults.get()
if faults is not None:
faults.append(type(fault).__name__)
def clear_session_if_unrecoverable(request: Request, username: str) -> bool:
"""Drop a stale session that has no database recovery credential.
Ports ``cleanup_stale_sessions()``, which Flask ran as a before_request
handler on every request (``web/auth/session_cleanup.py`` on main, deleted
by this migration with no successor). Without it, a session whose password
has been evicted keeps a valid ``username`` cookie that 401s on every
protected route: the browser still believes it is logged in and nothing
tells it otherwise. An API or XHR client polling endpoints would never
recover.
Only clears when recovery is genuinely impossible, matching the original:
a temp auth token or a stored session password means
``ensure_user_database()`` can still reopen the connection, so the session
is stale rather than dead and must be left alone.
This also runs when a server-side session is still valid and its shared
connection is open. Background work or another device can keep that
connection alive after this session loses its required credential.
When it clears the cookie it also destroys the matching server-side
session and disconnects that session's sockets
(``_revoke_server_session``), because the cleared browser can no longer
reach logout to do it.
Returns True if the session was cleared.
"""
if request.session.get("temp_auth_token"):
# Post-login bootstrap credential — ensure_user_database() consumes it
# and opens the database.
return False
if not db_manager.has_encryption:
# Unencrypted databases open with the dummy password; a missing
# connection here is not a credential problem.
return False
session_id = request.session.get("session_id")
if session_id and session_password_store.get_session_password(
username, session_id
):
# The store can still reopen it.
return False
logger.info(
"Clearing stale session for {} — no recovery credential", username
)
if session_id:
_revoke_server_session(session_id, username)
request.session.clear()
return True
def _revoke_server_session(session_id: str, username: str) -> None:
"""Destroy the server-side session behind a cookie that is being cleared.
Clearing only the cookie would leave the ``session_manager`` record alive
for the rest of its idle timeout (up to ``session_remember_me_days``).
Once the cookie is gone the browser cannot reach logout, the path that
normally destroys it. A replayed copy of the cookie would keep renewing
that record, and checks that rely on ``validate_session`` alone (the
Socket.IO connect gate, ``has_active_sessions_for`` in the idle sweeper)
would keep treating the session as live. So destroy the record and
disconnect its sockets, the same per-session teardown logout performs.
Only this session is affected. The user's other sessions keep working.
A session id owned by a different user is left alone. Teardown is
best-effort so a failure here cannot turn the 401 into a 500.
The owner is looked up with ``session_owner``, which reads no epoch.
``validate_session`` re-projects the durable epoch and raises
``SessionValidationUnavailableError`` while the epoch files cannot be
read; looking the owner up through it skipped the teardown below while
the cookie was still cleared, leaving exactly the orphaned record this
function exists to remove. A stale-epoch or expired record of this
user is destroyed too: the cookie bound to it is being cleared.
"""
try:
owner = session_manager.session_owner(session_id)
if owner is not None and owner != username:
return
if owner is not None:
session_manager.destroy_session(session_id)
from ..services.socketio_asgi import disconnect_session
disconnect_session(session_id)
except Exception as exc:
# Type name only: a rendered traceback would include session_id.
failure = type(exc).__name__
else:
return
logger.warning(
"Failed to revoke an unrecoverable server-side session: {}", failure
)
def end_rejected_session(request: Request) -> None:
"""Log out a browser whose session ``require_auth`` rejected but kept.
``require_auth`` keeps the cookie on one 401 path on purpose: the user's
database is not connected, but ``clear_session_if_unrecoverable`` still
sees a way to recover (this session's stored password, a temp auth token,
or an unencrypted install). If the reopen then keeps failing (missing or
corrupt database file, a failed migration, the close/rekey window of a
password change), every request 401s with ``username`` still in the
cookie. ``/auth/login`` sends any cookie with a username back to ``/``,
so a page that redirects that 401 to the login page without clearing the
cookie loops between the two until the browser gives up. A cached engine
that merely cannot be checked right now (a rekey sidecar fault), or a
reopen that failed with a rekey operational fault (another holder
keeping the sidecar past the wait bound, for one), is not such a
failure: ``require_auth`` answers it with a 503, which never reaches
this function.
Call this before redirecting such a 401 to the login page. It destroys
the server-side session, disconnects its sockets, drops this session's
stored password, and clears the cookie, so the browser lands on the
login form. Other sessions of the same user are not touched. Does
nothing when ``require_auth`` already cleared the session.
"""
username = request.session.get("username")
if not username:
return
session_id = request.session.get("session_id")
if session_id:
_revoke_server_session(session_id, username)
try:
session_password_store.clear_session(username, session_id)
except Exception as exc:
# Type name only: a rendered traceback would include session_id.
logger.warning(
"Failed to drop a rejected session's stored password: {}",
type(exc).__name__,
)
request.session.clear()
# Seconds a client is asked to wait before retrying a request answered with
# ``database_unavailable_exception``'s 503: the user's database could not be
# checked or opened because of a rekey operational fault (a transient
# sidecar fault such as an fd-exhaustion spike).
DATABASE_UNAVAILABLE_RETRY_AFTER_SECONDS = 5
def database_unavailable_exception() -> HTTPException:
"""The retryable 503 for a rekey operational fault after authentication.
Every moment that meets such a fault answers the same way:
* ``require_auth``'s own probe cannot check the cached engine, or no
engine is cached because this request's reopen of the database met
such a fault (``UserConnectionStatus.UNAVAILABLE``);
* the probe succeeded, but opening the request's database session a
moment later failed on the same kind of fault
(``DatabaseSessionUnavailableError`` from ``get_user_db_session``),
which an fd-exhaustion spike that comes and goes produces: ``/`` and
``get_db_session_dep`` raise this for it (``/`` answers a browser,
for this and for ``require_auth``'s own 503, with the branded error
page instead: same status and ``Retry-After``, and a button that
retries ``/``);
* a route left that error, or a ``RekeyOperationalError`` raised by a
direct ``db_manager`` call, unhandled: the app's exception handler
for both (``fastapi_app._register_exception_handlers``) answers this.
That includes a settings read through ``get_settings_manager``
without a session (``db_manager.get_session`` raises the fault rather
than letting it read as "no database", i.e. every setting at its
default); ``POST /metrics/api/journal-data/download``, whose own
catch-all would answer 500, passes it on to that handler.
None is a verdict on the session, so whoever raises this leaves the
cookie, the server-side session and the stored password alone. A fresh
instance each call: ``HTTPException`` carries mutable headers.
"""
return HTTPException(
status_code=503,
detail="Your database is temporarily unavailable. "
"Please try again in a moment.",
headers={"Retry-After": str(DATABASE_UNAVAILABLE_RETRY_AFTER_SECONDS)},
)
def _database_connection_status(username: str) -> UserConnectionStatus:
"""Whether ``username``'s database can serve this request right now.
``is_user_connected`` stays the single probe on the common path. Its
False is ambiguous, though: either no usable engine is cached (none, or
a stale one the probe just detached), or a cached engine could not be
checked because the rekey sidecar could not be opened or locked, or the
epoch read failed (an fd-exhaustion spike, EACCES, EIO). The manager
keeps the engine in the second case and re-checks it on the next
probe. So a False is followed by ``probe_user_connection``, which tells
the two apart; its answer is the more recent one, so it decides.
No cached engine is not a credential verdict either when this
request's reopen of the database (``ensure_user_database``) failed with
a rekey operational fault: its cold open could not take the sidecar
(another holder kept it past the wait bound, an fd-exhaustion spike),
could not read or publish the epoch, or found durable rekey state that
needs recovery. None of these says anything against the password, so
that is ``UNAVAILABLE`` too.
"""
try:
if db_manager.is_user_connected(username):
return UserConnectionStatus.CONNECTED
except RekeyOperationalError:
# The probe takes the rekey sidecar (filesystem + a lock it waits
# for at most a few seconds). Opening, locking (a holder that keeps
# it past that bound included) and reading are reported as a plain
# False, but a failure to release the sidecar afterwards
# (unlock/close) raises RekeySidecarIOError, which is not an
# OSError. It is the same transient sidecar I/O fault, not a verdict
# on the session.
return UserConnectionStatus.UNAVAILABLE
connection = db_manager.probe_user_connection(username)
if (
connection is UserConnectionStatus.NOT_CONNECTED
and _reopen_faults.get()
):
return UserConnectionStatus.UNAVAILABLE
return connection
def require_auth(request: Request) -> str:
"""Require authentication. Returns username or raises 401 (or 503).
Replaces Flask's @login_required decorator.
For API routes, returns JSON 401. For HTML routes, could redirect
(but callers handle that distinction).
A session that would otherwise be accepted gets a 503 with
``Retry-After`` instead while its cached database engine cannot be
checked because of a rekey sidecar fault, or while no engine is cached
because this request's reopen failed with a rekey operational fault
(another holder keeping the sidecar past the wait bound, for one). The
session, its
server-side record and its stored password are all kept: a 401 there
would be turned into a logout by ``/`` (``end_rejected_session``)
after a transient fault such as an fd-exhaustion spike. A fault that
only shows when the route then opens its database session surfaces as
``DatabaseSessionUnavailableError`` and gets the same 503
(``database_unavailable_exception``).
"""
username = request.session.get("username")
if not username:
raise HTTPException(status_code=401, detail="Authentication required")
connection = _database_connection_status(username)
if connection is not UserConnectionStatus.CONNECTED and (
connection is not UserConnectionStatus.UNAVAILABLE
):
# Stale session with no recoverable credential: clear the cookie (and
# revoke the server-side session) so the client is sent back to login
# instead of 401-ing on every request until someone loads "/".
clear_session_if_unrecoverable(request, username)
raise HTTPException(
status_code=401, detail="Database connection required"
)
# Validate the SERVER-SIDE session, not just the username claim inside the
# signed cookie.
#
# Without this, revocation does not work. The checks below it are both
# username-scoped: `is_user_connected` is true whenever ANY session for that
# user has the database open, and the password resolver
# (`get_user_db_session` -> `get_any_session_password`) hands back whichever
# session's password is live. So a cookie captured before logout is rejected
# only while the user stays logged out, and is accepted again the moment
# they — or anyone on any device — logs in again. Demonstrated: log in,
# capture cookie, log out (401 as expected), log in again, replay the
# original cookie -> 200 with real data.
#
# Flask did not need this: `get_user_db_session` resolved the password from
# `flask_session["session_id"]`, so a replayed cookie whose session had been
# destroyed found no password and failed at the database layer.
# `get_any_session_password` (added on this branch, absent from main) removed
# that incidental protection, because 154 router call sites invoke
# `get_user_db_session(username)` without threading a session_id through.
#
# Validating the session id here restores revocation for every route at one
# chokepoint. It also gives `session_timeout_hours` / `remember_me_days`
# real effect, since `validate_session` enforces the timeouts and refreshes
# last-access on use.
try:
session_valid = _server_session_valid(request, username)
except SessionValidationUnavailableError:
# The durable epoch could not be read (e.g. EMFILE). Refuse THIS
# request, but keep the session and its cookie: a transient fault
# proves nothing about the session and must not log users out.
raise HTTPException(
status_code=503,
detail=SESSION_VALIDATION_UNAVAILABLE_DETAIL,
headers={"Retry-After": "5"},
) from None
if not session_valid:
# Destroyed by logout or password change, expired, or never valid.
# Clear the cookie so the client stops presenting a dead session.
request.session.clear()
raise HTTPException(status_code=401, detail="Authentication required")
if connection is UserConnectionStatus.UNAVAILABLE:
# The session is valid, but its database cannot be checked: the
# user's engine is still cached and its rekey epoch could not be
# checked (the sidecar could not be opened, locked or released, or
# the epoch read failed), or no engine is cached and the reopen
# DatabaseMiddleware attempted failed with a rekey operational
# fault (_database_connection_status). Answer a
# retryable 503 and touch nothing: a 401 here reaches "/", whose
# end_rejected_session destroys the session and its stored
# password, so an fd-exhaustion spike or a held sidecar would log
# the user out. With an engine cached, that reopen failed fast too:
# open_user_database raises the same fault before deriving a key.
raise database_unavailable_exception()
return username
def _server_session_valid(request: Request, username: str) -> bool:
"""Whether the login still owns a usable authenticated session.
An open shared connection alone cannot make an encrypted database usable:
request handlers still need the session's in-memory credential. Require
re-authentication when it is gone, even if background work keeps the
connection open.
Split out of ``require_auth`` as a named seam rather than inlined, so a
test suite can relax the server-side-session gate without relaxing
authentication itself. Many route tests authenticate with the legacy
idiom — a bare ``username`` in the session plus a mocked ``db_manager``,
never creating a server-side session — which this gate correctly
rejects. ``tests/conftest.py``'s autouse ``_legacy_bare_username_auth``
patches this one function so those tests keep working, while tests that
must prove a destroyed session IS rejected opt out with
``@pytest.mark.real_session_check`` and exercise the real check.
Only ever called after a username has been confirmed present, so
"accept unconditionally" is exactly the pre-revocation contract.
Per-request memo: ``DatabaseMiddleware`` validates the same session
(``_enforce_session_revocation``) earlier in this request and records
the ``(session_id, username)`` it proved in ``request.state``. When
both still match the cookie, that result is reused instead of
re-projecting the durable epoch a second time. Anything else (no
memo, a different id or user) validates for real.
Raises ``SessionValidationUnavailableError`` when the epoch cannot be
read (``require_auth`` answers 503 and keeps the session).
"""
session_id = request.session.get("session_id")
if not session_id:
return False
scope = getattr(request, "scope", None)
state = scope.get("state") if isinstance(scope, dict) else None
if isinstance(state, dict) and state.get(VALIDATED_SESSION_STATE_KEY) == (
session_id,
username,
):
valid_owner = True
else:
valid_owner = session_manager.validate_session(session_id) == username
# The memo only caches the epoch check. A credential lost later in this
# request must still retire the session, even with a shared DB open.
return valid_owner and not clear_session_if_unrecoverable(request, username)
def get_db_session_dep(
request: Request,
username: Annotated[str, Depends(require_auth)],
) -> Generator[Session, None, None]:
"""Yield a database session for the authenticated user.
Passes the current request's session_id through to
get_user_db_session so the password lookup is bound to this
request's session rather than falling back to "any active
session's password" (cross-session leak risk).
NOT WIRED TO ANY ROUTE, AND DO NOT WIRE IT TO ONE AS-IS.
Its only consumer is ``get_settings_manager_dep`` below, which is itself
referenced only from ``tests/web/routers/test_thread_safety.py``.
The hazard: FastAPI drives a *sync generator* dependency through
``contextmanager_in_threadpool``, which dispatches ``__enter__`` and
``__exit__`` as two SEPARATE ``anyio.to_thread.run_sync`` calls. anyio
picks a worker with ``idle_workers.pop()`` and gives no task affinity, so
the two halves can land on different pooled threads. ``enter_scope()`` /
``exit_scope()`` (database/session_context.py) write to a
``threading.local()``, so a straddle leaves the entering worker's
``scope_depth`` stuck at >=1 forever — and ``ThreadLocalSessionManager.
get_session`` then permanently skips its stale-transaction rollback,
letting one request's uncommitted ORM state be autoflushed and committed
by the next request served on that thread.
The same hazard is documented for the streaming generators in
``web/routers/library.py``, which handle it deliberately. An attempt to
reproduce the straddle here (120 requests, 24-way concurrency, 44 distinct
workers) produced no mismatch on the pinned fastapi/starlette/anyio — but
same-thread dispatch is a scheduling accident, not a guarantee.
Before using this: either make scope enter/exit a token API that asserts
``threading.get_ident()`` matches, or use ``run_db_sync`` (which keeps the
whole unit of work on one worker), as the routes actually do.
A ``DatabaseSessionUnavailableError`` (a rekey sidecar or epoch fault,
not a credential problem) is answered with the retryable 503 of
``database_unavailable_exception`` and leaves the session alone; any
other ``DatabaseSessionError`` clears it and answers 401.
"""
from ...database.session_context import (
DatabaseSessionError,
DatabaseSessionUnavailableError,
)
session_id = request.session.get("session_id")
try:
with get_user_db_session(username, session_id=session_id) as session:
if session is None:
raise HTTPException(
status_code=500, detail="Failed to get database session"
)
yield session
except DatabaseSessionUnavailableError:
# require_auth's probe passed, then opening the session (or a
# session the handler opened) met a transient rekey fault. Clearing
# the session here would log the user out over an fd spike.
raise database_unavailable_exception() from None
except DatabaseSessionError:
# Password not available (e.g. server restarted, session expired).
# Clear session to force re-login.
request.session.clear()
raise HTTPException(
status_code=401,
detail="Session expired — please log in again",
)
def get_settings_manager_dep(
db_session: Annotated[Session, Depends(get_db_session_dep)],
username: Annotated[str, Depends(require_auth)],
):
"""Yield a SettingsManager bound to the current user's DB session."""
return get_settings_manager(db_session, username)
def ensure_user_database(request: Request) -> None:
"""Ensure the user's encrypted database is open for this request.
Ports Flask's ensure_user_database() before_request handler.
Uses the same 3-source password fallback:
1. Temporary auth token (post-login/register, 10s TTL)
2. Session password store (persistent, 24h TTL)
3. Dummy password for unencrypted databases
Never raises. Any fault while resolving the password or opening the
connection degrades to "no connection opened", and the auth gate then
rejects the request with a 401 — matching Flask's handler, whose
``try/except Exception`` covered both ``is_user_connected()`` and
``open_user_database()`` (``web/auth/database_middleware.py`` on main).
That is not cosmetic here: this runs from ``DatabaseMiddleware``, i.e.
before any route, so an escaping exception turns EVERY authenticated
request into a 500 for as long as the fault lasts. One exception to the
401: a rekey operational fault (``RekeyOperationalError``) from the
reopen is recorded for the request (``request_database_scope``), and
the auth gate answers it with a retryable 503 that keeps the session
(see ``require_auth``). That includes a cached engine that cannot be
checked, which ``open_user_database`` raises before deriving a key, and
a cold open that could not take the sidecar (another holder keeping it
past the wait bound, an fd-exhaustion spike) or read the epoch.
"""
username = request.session.get("username")
if not username:
return
password = None
# One guard around the whole body rather than main's narrower one.
#
# main's try wrapped `is_user_connected()` + `open_user_database()`
# together, both inside `if password:`. This port hoists the
# `is_user_connected()` fast path above the token block (see below),
# so a try around `open_user_database()` alone would leave it — and
# the token/password-store lookups — unguarded. Covering the lot
# keeps main's contract ("a db_manager fault degrades to 401") intact
# regardless of that reordering.
try:
# Source 1: Temporary auth token (post-login/register).
#
# Consumed BEFORE the is_user_connected() fast path below, and that
# ordering is load-bearing. The token is a one-time bootstrap
# credential that retrieve_auth() deletes from the store — but only
# if we actually call it. Login already opens the connection, so an
# early return on is_user_connected() would skip this block on every
# subsequent request and the token would never be consumed: it stays
# live in the store for its full 10s TTL *and* stays in the client's
# cookie. A cookie captured in that window then re-authenticates
# after logout, because logout clears session_password_store but
# cannot reach into an already-issued cookie. Worse, this block would
# then write the recovered password into session_password_store,
# promoting a 10s window into a 24h session.
#
# Flask consumed the token unconditionally here and tested
# is_user_connected() only at the point of opening the DB
# (web/auth/database_middleware.py).
temp_auth_token = request.session.get("temp_auth_token")
if temp_auth_token:
from ...database.temp_auth import temp_auth_store
auth_data = temp_auth_store.retrieve_auth(temp_auth_token)
if auth_data:
stored_username, password = auth_data
if stored_username == username:
# Remove token from session after use
request.session.pop("temp_auth_token", None)
# Store in session password store for future requests
session_id = request.session.get("session_id")
if session_id:
session_password_store.store_session_password(
username, session_id, password
)
else:
# An expired/consumed marker is not a recovery credential.
# A concurrent response can retain this old cookie after
# another request consumed the token. Leaving it in place
# would bypass clear_session_if_unrecoverable indefinitely.
request.session.pop("temp_auth_token", None)
# Fast path: the connection is already open, so there is nothing left
# to do. Placed after the token block above rather than at the top of
# the function — see the comment there. In steady state (no token in
# the session) reaching here costs one is_user_connected() probe,
# which is no longer a bare dict lookup: it takes the per-user
# lifecycle lock and the rekey sidecar flock and re-reads the
# durable rekey epoch to validate the cached engine.
if db_manager.is_user_connected(username):
return
# Source 2: Session password store
if not password:
session_id = request.session.get("session_id")
if session_id:
password = session_password_store.get_session_password(
username, session_id
)
# Source 3: Dummy password for unencrypted databases
if not password and not db_manager.has_encryption:
# nosemgrep: semgrep.rules.hardcoded-secret-detection, reason: The placeholder is used only when SQLCipher encryption is unavailable
password = "dummy" # noqa: S105 — not a real password; placeholder for unencrypted DBs
if password:
engine = db_manager.open_user_database(username, password)
if not engine:
logger.warning(
f"open_user_database returned None for user {username}"
)
except RekeyOperationalError as exc:
# Not a verdict on the session: the sidecar or an epoch file could
# not be read, another holder kept the sidecar past the wait bound,
# or the durable rekey state needs recovery first. None of these
# says anything against the password. Recorded for require_auth,
# which answers it with the retryable 503 and keeps the session
# instead of reading the missing engine as a credential failure (a
# 401 that "/" turns into a logout). Type name only, as below.
_record_reopen_fault(exc)
logger.warning(
f"Database for user {username} is temporarily unavailable "
f"({type(exc).__name__})"
)
except Exception as exc:
# Deliberately no traceback and no exception message: the frames
# under this call hold the plaintext password in their locals, and
# loguru renders frame locals when `diagnose` is on (its default).
# `limits`-style exceptions that echo their input back would leak it
# into the message too. main logged a bare warning here for the same
# reason; the exception TYPE is added because it costs nothing and
# is the one piece of a traceback that cannot carry a credential.
logger.warning(
f"Failed to open database for user {username} "
f"({type(exc).__name__})"
)