This was on the gap list as a missing feature, but it was a bug first. The
update checker asks the registry for a tag's digest over HTTP itself, and could
only do it anonymously. A private repository answers 401, remote_digest returned
None, and None already meant "could not reach registry" — so a private image was
indistinguishable from a network blip. The Images page showed nothing and a
stack pinned to a six-month-old image looked up to date indefinitely.
So AuthRequired is now its own exception, separate from unreachable, and the
error names the registry and which of the two problems it is: "ghcr.io needs
credentials" when there are none, "ghcr.io rejected the stored credentials" when
there are and they are wrong. Those are different fixes, and the message should
say which one you need. The plain unreachable message survives unchanged, with a
test pinning it, because not every failure is an auth failure.
Two consumers need the credentials and they need them in completely different
shapes, which is why this is its own service rather than a field on something
else. StackPilot's own checker wants (user, password) inside async code that has
no database session, so the rows are mirrored into an in-memory cache that
reload() refills on startup and after every write. The Docker CLI wants a
config.json, so reload() writes one into ${DATA_DIR}/docker and compose runs with
DOCKER_CONFIG pointed at it. Generating it from the database every time is what
makes deletion real: removing a registry in the UI revokes the CLI's login
instead of leaving a stale one in ~/.docker.
Host normalization is the join that makes any of it work, and it is easy to
underestimate. parse_ref only ever produces registry-1.docker.io, nobody types
that, and the CLI wants the whole thing under https://index.docker.io/v1/ — three
spellings of one registry across three layers. canonical_host settles on what
parse_ref produces, the config writer translates on the way out, and a bare
nginx:alpine finds credentials entered as "docker.io". Verified end to end:
typed as the v1 URL, stored as registry-1.docker.io, written as the v1 URL.
The password is encrypted at rest with the same key as backup destinations and
never leaves the server, not even masked — the API returns has_password, which
is all the form needs to offer "leave blank to keep". A row that cannot be
decrypted after a SECRET_KEY change is skipped with a warning rather than taking
every other registry down with it. Everything here is admin-only including the
reads, because even masked the rows say which registries this install talks to
and under what account.
The Test button asks the registry rather than validating a string, following the
Bearer challenge with credentials attached the way a real client does. Only an
outright 401 counts as wrong credentials; anything else means reachable and
talking, which is as much as a credentials check can honestly claim. Checked
against the live Docker Hub token endpoint with deliberately wrong credentials.
33 tests: the normalization table, the cache, the generated config.json down to
its 0600 mode and the Docker Hub key, encryption at rest, that no password field
appears in any response, and the 401-is-reported behaviour that started this.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
158 lines
5.0 KiB
Python
158 lines
5.0 KiB
Python
"""StackPilot backend — FastAPI application entry point."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
from sqlmodel import Session
|
|
|
|
from config import settings
|
|
from version import APP_VERSION
|
|
from database import engine, init_db
|
|
from docker_client import DockerError
|
|
from routers import (
|
|
audit,
|
|
auth,
|
|
backups,
|
|
containers,
|
|
dashboard,
|
|
destinations,
|
|
editor,
|
|
files,
|
|
images,
|
|
networks,
|
|
ports,
|
|
registries,
|
|
schedules,
|
|
secrets,
|
|
settings as settings_router,
|
|
stacks,
|
|
system,
|
|
templates,
|
|
volumes,
|
|
ws,
|
|
)
|
|
from services import (
|
|
backup_destination_service,
|
|
image_status_store,
|
|
logo_service,
|
|
registry_service,
|
|
schedule_service,
|
|
stack_lock_service,
|
|
template_service,
|
|
update_service,
|
|
)
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger("stackpilot")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
init_db()
|
|
# Register stacks that already exist on disk.
|
|
try:
|
|
with Session(engine) as session:
|
|
stacks.sync_discovered_stacks(session)
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Stack discovery failed: %s", exc)
|
|
# One-off: encrypt backup-destination credentials written before they were
|
|
# stored encrypted (see services/crypto_service.py).
|
|
try:
|
|
with Session(engine) as session:
|
|
encrypted = backup_destination_service.migrate_plaintext_configs(session)
|
|
if encrypted:
|
|
logger.info("Encrypted %d backup destination config(s) at rest", encrypted)
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Destination config encryption migration failed: %s", exc)
|
|
try:
|
|
moved = template_service.migrate_legacy_db_templates()
|
|
if moved:
|
|
logger.info("Migrated %d custom template(s) from the database to folders", moved)
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Legacy template migration failed: %s", exc)
|
|
# Runtime state that used to live in module dicts and was lost on restart.
|
|
try:
|
|
with Session(engine) as session:
|
|
stale = stack_lock_service.prune_expired(session)
|
|
if stale:
|
|
logger.info("Cleared %d stale stack lock(s) from a previous run", stale)
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Could not prune stack locks: %s", exc)
|
|
try:
|
|
restored = image_status_store.install()
|
|
logger.info("Restored %d cached image update status(es)", restored)
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Could not restore the image update cache: %s", exc)
|
|
# Private registry credentials: into the in-memory cache the update checker
|
|
# reads, and into the config.json the Docker CLI reads.
|
|
try:
|
|
with Session(engine) as session:
|
|
known = registry_service.reload(session)
|
|
if known:
|
|
logger.info("Loaded credentials for %d registr%s", known, "y" if known == 1 else "ies")
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Could not load registry credentials: %s", exc)
|
|
|
|
update_task = asyncio.create_task(update_service.background_loop())
|
|
schedule_task = asyncio.create_task(schedule_service.scheduler_loop())
|
|
# App logos. Deliberately a task and not awaited: the catalog is a network
|
|
# download, and a box with no outbound internet must still start instantly
|
|
# (it just keeps the built-in glyphs).
|
|
logo_task = asyncio.create_task(logo_service.catalog_loop())
|
|
logger.info("StackPilot backend ready on port %s", settings.PORT)
|
|
yield
|
|
update_task.cancel()
|
|
schedule_task.cancel()
|
|
logo_task.cancel()
|
|
|
|
|
|
app = FastAPI(title="StackPilot", version=APP_VERSION, lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
@app.exception_handler(DockerError)
|
|
async def docker_error_handler(_request: Request, exc: DockerError):
|
|
return JSONResponse(
|
|
status_code=502,
|
|
content={"error": exc.error, "detail": exc.detail},
|
|
)
|
|
|
|
|
|
app.include_router(auth.router)
|
|
app.include_router(stacks.router)
|
|
app.include_router(registries.router)
|
|
app.include_router(secrets.router)
|
|
app.include_router(containers.router)
|
|
app.include_router(dashboard.router)
|
|
app.include_router(system.router)
|
|
app.include_router(volumes.router)
|
|
app.include_router(editor.router)
|
|
app.include_router(files.router)
|
|
app.include_router(images.router)
|
|
app.include_router(ports.router)
|
|
app.include_router(templates.router)
|
|
app.include_router(audit.router)
|
|
app.include_router(settings_router.router)
|
|
app.include_router(backups.router)
|
|
app.include_router(destinations.router)
|
|
app.include_router(schedules.router)
|
|
app.include_router(networks.router)
|
|
app.include_router(ws.router)
|
|
|
|
|
|
@app.get("/api/health")
|
|
def health() -> dict:
|
|
return {"status": "ok", "version": APP_VERSION}
|