Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

8.4 KB
Raw Blame
import { AppSettings } from '../../db/entities/AppSettings.entity';
import type { AppSettingsRepository } from '../../db/repositories/AppSettings.repository';
import { UnitOfWork } from '../database/unit-of-work';
import { StorageJobsService } from './storage-jobs.service';
import { getSeedConfigPath } from './storage-paths';
import { ephemeralDriverFor, probeDriver, type ProbeTargetResult } from './storage-probe';
import { StorageReclaimService } from './storage-reclaim.service';
import { BACKENDS_KEY, CATEGORIES_KEY, VERSION_KEY, StorageRegistryService } from './storage-registry.service';
import {
  assertNoMaskSentinels,
  decryptBackendSecrets,
  encryptStorageSecrets,
  maskBackendOptions,
  unmaskStorageConfig,
} from './storage-secrets';
import { StorageStatsService } from './storage-stats.service';
import { StorageService } from './storage.service';
import { StorageBackendError, StorageConflictError, type StorageCategory } from './storage.types';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import type {
  StorageAdminState,
  StorageBackend,
  StorageConfigPut,
  StorageTestResponse,
  StorageUsage,
} from '@trek/shared';

import fs from 'node:fs';

/**
 * Owner of the api/admin/storage read/write pipelines (spec:
 * docs/superpowers/specs/2026-08-19-storage-admin-config-design.md, Server).
 * Reads render from the registry's live snapshot; writes run
 * unmask → preview → encrypt → persist → reload, so an admin save either
 * fully applies or changes nothing — never the boot-time silent fallback.
 */
@Injectable()
export class StorageAdminService {
  constructor(
    @InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository,
    private readonly registry: StorageRegistryService,
    private readonly storage: StorageService,
    private readonly jobs: StorageJobsService,
    private readonly stats: StorageStatsService,
    private readonly uow: UnitOfWork,
    private readonly reclaim: StorageReclaimService,
  ) {}

  /** The effective world — secrets masked, categories cross-referenced per backend. */
  async state(): Promise<StorageAdminState> {
    const snapshot = this.registry.snapshot();
    const assignments = Object.entries(snapshot.categories) as Array<
      [keyof typeof snapshot.categories, { backend: string; source: 'default' | 'settings' }]
    >;
    return {
      backends: snapshot.backends.map((backend) => ({
        name: backend.name,
        type: backend.type,
        source: backend.source,
        options: maskBackendOptions(backend.type, backend.options),
        categories: assignments.filter(([, a]) => a.backend === backend.name).map(([category]) => category),
      })),
      categories: snapshot.categories,
      health: { replicaFailures: this.storage.health().replicaFailures.map((f) => ({ ...f })) },
      seedFilePresent: fs.existsSync(getSeedConfigPath()),
      usage: await this.stats.readUsage(),
      reclaim: await this.reclaim.state(),
      backfills: this.jobs.statuses(),
      migrations: this.jobs.migrationStatuses(),
      version: await this.registry.currentConfigVersion(),
      configError: this.registry.lastLoadError(),
    };
  }

  /**
   * Start a replica catch-up for a routed mirror. Delegates to the one-at-a-time
   * job registry; throws BackfillTargetError (404) / BackfillBusyError (409).
   */
  startBackfill(mirrorName: string): void {
    this.jobs.startBackfill(mirrorName);
  }

  /** True when an active run was cancelled; false when there was nothing to cancel. */
  cancelBackfill(mirrorName: string): boolean {
    return this.jobs.cancelBackfill(mirrorName);
  }

  /**
   * Start a category migration to a new backend. Delegates to the one-at-a-time
   * job registry; throws MigrationRequestError (400) / MigrationTargetError (404) /
   * BackfillBusyError (409).
   */
  async startMigration(category: StorageCategory, to: string): Promise<void> {
    await this.jobs.startMigration(category, to);
  }

  /** True when an active migration was cancelled; false when there was nothing to cancel. */
  cancelMigration(category: string): boolean {
    return this.jobs.cancelMigration(category);
  }

  /** Runs and persists a fresh usage scan. Throws StatsBusyError (409) if one is already running. */
  async refreshStats(): Promise<StorageUsage> {
    return await this.stats.scan();
  }

  /**
   * Full-document replace of the two settings rows. Throws StorageBackendError
   * on any refusal, or StorageConflictError (audit #7) when `config.version`
   * no longer matches the stored counter — checked FIRST, before unmask/preview,
   * so a stale submit never even previews against (let alone overwrites) a
   * config that moved on since the form was loaded (e.g. a category
   * migration's flip, which bumps the same counter).
   */
  async applyConfig(config: StorageConfigPut): Promise<void> {
    const currentVersion = await this.registry.currentConfigVersion();
    if (config.version !== currentVersion) {
      throw new StorageConflictError(currentVersion, config.version);
    }
    const unmasked = unmaskStorageConfig(config, await this.storedBackendsRow());
    // unmask only resolves the secret fields it knows about; a mask sentinel
    // submitted in a non-secret field would otherwise pass through untouched
    // and get persisted verbatim as garbage-in.
    assertNoMaskSentinels(unmasked);
    this.registry.preview({ backends: unmasked.backends, categories: unmasked.categories });
    const encrypted = encryptStorageSecrets(unmasked);
    await this.uow.transactional(async () => {
      await this.appSettings.setValue(BACKENDS_KEY, JSON.stringify(encrypted.backends));
      await this.appSettings.setValue(CATEGORIES_KEY, JSON.stringify(encrypted.categories));
      await this.appSettings.setValue(VERSION_KEY, String(currentVersion + 1));
    });
    await this.registry.reload();
    // Any running job whose backend the reloaded config no longer has ends
    // cancelled rather than running invisibly against a stale driver ref
    // (polish item 3) — see StorageJobsService.cancelJobsForMissingBackends.
    this.jobs.cancelJobsForMissingBackends();
  }

  /**
   * Probe an (unsaved or stored) candidate with ephemeral drivers. Mirrors are
   * expanded and probed per target — the probe bypasses MirrorDriver, which
   * hides replica failures by design. Registry state is never touched.
   */
  async testBackend(candidate: StorageBackend): Promise<StorageTestResponse> {
    const { backends } = unmaskStorageConfig({ backends: [candidate], categories: {} }, await this.storedBackendsRow());
    const backend = backends[0]!;
    const targets = this.probeTargetsFor(backend).map(decryptBackendSecrets) as Array<
      Extract<StorageBackend, { type: 'local' | 's3' }>
    >;
    const results: ProbeTargetResult[] = [];
    for (const target of targets) {
      results.push(await this.probeTarget(target));
    }
    return { ok: results.every((r) => r.ok), targets: results };
  }

  /** Expand a mirror candidate into its concrete targets via the live snapshot. */
  private probeTargetsFor(backend: StorageBackend): StorageBackend[] {
    if (backend.type !== 'mirror') return [backend];
    const byName = new Map(this.registry.snapshot().backends.map((b) => [b.name, b]));
    const names = [backend.options.primary, ...backend.options.replicas];
    return names.map((name) => {
      const resolved = byName.get(name);
      if (!resolved) {
        throw new StorageBackendError(`mirror '${backend.name}' references unknown backend '${name}'`);
      }
      if (resolved.type === 'mirror') {
        throw new StorageBackendError(`mirror '${backend.name}' nests mirror '${name}' — nesting is rejected`);
      }
      return { name: resolved.name, type: resolved.type, options: resolved.options } as StorageBackend;
    });
  }

  private async probeTarget(target: Extract<StorageBackend, { type: 'local' | 's3' }>): Promise<ProbeTargetResult> {
    let driver;
    try {
      driver = ephemeralDriverFor(target);
    } catch (err) {
      return { name: target.name, ok: false, error: err instanceof Error ? err.message : String(err) };
    }
    return probeDriver(target.name, driver);
  }

  /** The raw stored backends row — the unmask source (tolerates absent/garbage rows). */
  private async storedBackendsRow(): Promise<unknown> {
    const value = await this.appSettings.getValue(BACKENDS_KEY);
    if (!value) return [];
    try {
      return JSON.parse(value) as unknown;
    } catch {
      return [];
    }
  }
}