fix: the web service starts once libpq can be found.
Release / release (push) Successful in 12s

This commit is contained in:
slawek committed 2026-10-24 11:07:45 +02:00
1 parent 732cc8cb54
commit b1e6d9cd0b
9 files changed
+55 -30

No files matched your search

@@ -5,9 +5,7 @@ router and that never leaves it. /auth issues an access token naming the user in
a refresh token valid REFRESH_SECONDS; vpn-router-token issues tokens with the admin claim and
any lifetime. A token without the admin claim must expire within MAX_LIFETIME seconds.
psycopg is imported by driver() when the database is used, not when the module is loaded, so the
module loads while libpq cannot be found yet. A driver that cannot be imported and every
failure of the database raise DatabaseError, the only database exception callers see.
Every failure of the database raises DatabaseError, the only database exception callers see.
Refresh tokens are random strings kept in PostgreSQL as their SHA-256, in the database of
web.database, by default the vpn-router database on this host that provision() creates on the
@@ -24,6 +22,8 @@ import secrets
import subprocess
import time
import psycopg
WEB_KEY = pathlib.Path('/etc/vpn-router/web.key')
DATABASE_SECRET = pathlib.Path('/etc/vpn-router/database.secret')
@@ -44,7 +44,7 @@ SCHEMA = 'CREATE TABLE IF NOT EXISTS refresh_tokens (hash text PRIMARY KEY, name
class DatabaseError(Exception):
"""The PostgreSQL driver cannot be loaded, or the database of the refresh tokens failed."""
"""The database of the refresh tokens failed."""
def key():
@@ -117,15 +117,6 @@ def admin_token(seconds):
return {'token': sign({'admin': True, 'iat': now, 'exp': now + seconds}, secret), 'expires': now + seconds}
def driver():
"""The psycopg module; DatabaseError while libpq cannot be found."""
try:
import psycopg
except ImportError as exc:
raise DatabaseError(f'the PostgreSQL driver cannot be loaded: {exc}') from exc
return psycopg
def connection_string(cfg):
"""web.database, or the database provision() creates; its password, of URL-safe characters, is written unquoted."""
configured = cfg['web']['database'].strip()
@@ -138,7 +129,6 @@ def connection_string(cfg):
@contextlib.contextmanager
def database(cfg):
"""A connection to the database of the refresh tokens, with their table, closed at the end; DatabaseError on any failure."""
psycopg = driver()
try:
with psycopg.connect(connection_string(cfg), autocommit=True) as connection:
connection.execute(SCHEMA)