dorfteich/apps/api/src/setup/setup.guard.ts
Claude Fable 5 f0a82bad20
Some checks failed
CD / Build and push images (push) Successful in 3m16s
CI / Lint, typecheck, test (push) Successful in 3m5s
CD / Deploy to Test (push) Successful in 13s
CD / Smoke tests against Test (push) Failing after 3m35s
CD / Promote to Int (push) Has been skipped
CI / Auth e2e pack (push) Successful in 5m6s
CI / Import/export fidelity gate (push) Successful in 43s
CI / Build container images (push) Has been skipped
Add the first-run setup wizard API with env-backed secret store (#80)
When the api runs against a database without the setup.completedAt
marker, a global SetupGuard answers every non-exempt route with 503
setup_required; only /setup/*, health probes, and the session routes
stay reachable. The wizard steps (POST /setup/admin|instance|smtp|
registration|complete) write straight to their production homes; the
Site Admin step signs its creator in, later steps require that session.
Completing sets the marker and locks every step permanently (410, also
across restarts, and not reopenable via PATCH /admin/settings).

SMTP entered in the wizard is verified with a live delivery test first
(failure blocks the step with the transport error as detail) and then
persisted to the new env-backed secret store: a mode-600 dotenv file on
the new `secrets` volume (SECRETS_FILE). Explicit container env always
wins over the store; empty compose-passed strings count as unset. The
mail transport now resolves lazily through SmtpConfigService so wizard
changes apply without a restart.

SETUP_ADMIN_* env pre-seeds the whole wizard at boot for automated
deploys; a backfill migration marks instances that already have a Site
Admin as completed, and seed/vitest global-setup do the same for
fixture databases. The setup e2e suite provisions its own fresh
database (CREATE DATABASE + migrate deploy) per run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
2026-07-11 15:10:28 +02:00

47 lines
1.4 KiB
TypeScript

import {
CanActivate,
ExecutionContext,
Injectable,
ServiceUnavailableException,
SetMetadata,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { SetupStateService } from './setup-state.service';
const SETUP_EXEMPT_KEY = 'setupExempt';
/**
* Marks routes that stay reachable while the instance still requires the
* first-run setup: the wizard itself, health probes, and the session
* routes (so a mid-wizard admin who lost the cookie can sign back in).
*/
export const SetupExempt = (): MethodDecorator & ClassDecorator =>
SetMetadata(SETUP_EXEMPT_KEY, true);
/**
* Global first-line guard (registered before AuthGuard via module order):
* while setup is pending every non-exempt route answers 503
* `setup_required`, so clients — including anonymous ones — always learn
* the instance state instead of a misleading 401 (issue #80).
*/
@Injectable()
export class SetupGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
private readonly state: SetupStateService,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const exempt = this.reflector.getAllAndOverride<boolean>(SETUP_EXEMPT_KEY, [
context.getHandler(),
context.getClass(),
]);
if (exempt) return true;
if (await this.state.isPending()) {
throw new ServiceUnavailableException({ code: 'setup_required' });
}
return true;
}
}