feat(plugins): activation, HTTP route proxy + instance settings (M2)

Wires the isolated runtime into TREK. Admins can now activate a plugin from the
panel and its HTTP routes work end to end, still behind the kill switch.

- PluginRuntimeService owns the supervisor: activate spawns the child with its
  granted permissions + decrypted instance config, deactivate kills it, status
  and errors are persisted to the plugins / plugin_error_log tables, and active
  plugins are booted on startup (OnModuleInit).
- Bidirectional RPC: the child now handles host→child invokes (routes/jobs) and
  reports its declared routes on load; the supervisor gained invoke()/routesOf().
- /api/plugins/:id/* proxy controller — a single static route that matches the
  plugin's declared routes, enforces per-route auth (auth:false routes are public
  for OAuth callbacks/webhooks), forwards only a whitelisted request view (never
  the session cookie), and strips unsafe response headers.
- Admin endpoints: POST :id/activate, POST :id/deactivate, GET/PUT :id/config —
  instance settings with secret fields encrypted (apiKeyCrypto) and masked.
- Kill switch moved to its own module so it never collides with test config mocks.

Photo/calendar hook consumers are deferred to a later slice.
This commit is contained in:
mauriceboe
2026-07-03 16:50:23 +02:00
parent 14b348581a
commit d823ac443b
13 changed files with 775 additions and 29 deletions
-11
View File
@@ -174,14 +174,3 @@ export const SESSION_DURATION_REMEMBER =
export const SESSION_DURATION_REMEMBER_MS = parsedRememberMs ?? parseDurationMs(DEFAULT_SESSION_DURATION_REMEMBER)!;
/** "Remember me" session length in seconds — passed to `jwt.sign({ expiresIn })`. */
export const SESSION_DURATION_REMEMBER_SECONDS = Math.floor(SESSION_DURATION_REMEMBER_MS / 1000);
/**
* Plugin-system kill switch (#plugins). Off by default — a brand-new, high-risk
* surface an instance owner must deliberately opt into. When false, the plugin
* module serves its admin listing (so the tab isn't a dead end) but never spawns
* a plugin process and refuses install/activate. Read at call time so tests and
* runtime env changes take effect without re-import.
*/
export function pluginsEnabled(): boolean {
return (process.env.TREK_PLUGINS_ENABLED || '').trim().toLowerCase() === 'true';
}
+10
View File
@@ -0,0 +1,10 @@
/**
* Plugin-system kill switch (#plugins). Off by default — a brand-new, high-risk
* surface an instance owner must deliberately opt into. Lives in its own module
* (not config.ts) so the many tests that mock config with a partial export set
* don't have to know about it: the plugin runtime reads the env directly here.
* Read at call time so tests and runtime env changes take effect immediately.
*/
export function pluginsEnabled(): boolean {
return (process.env.TREK_PLUGINS_ENABLED || '').trim().toLowerCase() === 'true';
}
@@ -0,0 +1,108 @@
import { Injectable, type OnModuleInit, type OnModuleDestroy } from '@nestjs/common';
import { db } from '../../db/database';
import { pluginsEnabled } from './kill-switch';
import { decrypt_api_key } from '../../services/apiKeyCrypto';
import { PluginSupervisor, type PluginRouteInfo } from './supervisor/plugin-supervisor';
import { createRealRpcHost, closePluginDataDb } from './host/create-rpc-host';
import { isKnownPermission } from './protocol/envelope';
/**
* Owns the isolated-plugin runtime lifecycle inside NestJS (#plugins, M2).
* Bridges the DB registry (`plugins` rows) to the process supervisor: activate
* spawns the child with its granted permissions + decrypted instance config,
* deactivate kills it, and status/errors are persisted back to the DB. Boots all
* `active` plugins on startup when the runtime is enabled.
*/
interface PluginRow {
id: string;
status: string;
permissions: string;
granted_permissions: string;
config: string;
}
@Injectable()
export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy {
private readonly supervisor = new PluginSupervisor(createRealRpcHost, {
onStatus: (id, status, error) => {
db.prepare('UPDATE plugins SET status = ?, last_error = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?').run(
status,
error ?? null,
id,
);
},
onLog: (id, level, msg) => {
if (level === 'error' || level === 'warn') {
db.prepare('INSERT INTO plugin_error_log (plugin_id, level, message) VALUES (?, ?, ?)').run(id, level, msg);
}
},
});
onModuleInit(): void {
if (!pluginsEnabled()) return;
const active = db.prepare("SELECT id FROM plugins WHERE status = 'active'").all() as Array<{ id: string }>;
for (const { id } of active) {
this.activate(id).catch(() => {
/* status is persisted as error by the supervisor hook */
});
}
}
async onModuleDestroy(): Promise<void> {
await this.supervisor.shutdownAll();
}
/** Spawn a plugin from its DB row (granted permissions + decrypted config). */
async activate(id: string): Promise<void> {
const row = db.prepare('SELECT id, status, permissions, granted_permissions, config FROM plugins WHERE id = ?').get(id) as
| PluginRow
| undefined;
if (!row) throw new Error(`plugin ${id} not found`);
const granted = new Set(parseArray(row.granted_permissions).filter(isKnownPermission));
const config = decryptConfig(parseObject(row.config));
await this.supervisor.activate(id, granted, config);
}
async deactivate(id: string): Promise<void> {
await this.supervisor.disable(id);
closePluginDataDb(id);
db.prepare("UPDATE plugins SET status = 'inactive', updated_at = CURRENT_TIMESTAMP WHERE id = ?").run(id);
}
isActive(id: string): boolean {
return this.supervisor.isActive(id);
}
routesOf(id: string): PluginRouteInfo[] {
return this.supervisor.routesOf(id);
}
invoke(id: string, method: string, params: Record<string, unknown>): Promise<unknown> {
return this.supervisor.invoke(id, method, params);
}
}
function parseArray(json: string): string[] {
try {
const v = JSON.parse(json || '[]');
return Array.isArray(v) ? v.map(String) : [];
} catch {
return [];
}
}
function parseObject(json: string): Record<string, unknown> {
try {
const v = JSON.parse(json || '{}');
return v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : {};
} catch {
return {};
}
}
/** Decrypt secret config values transparently (decrypt_api_key passes plaintext through). */
function decryptConfig(config: Record<string, unknown>): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(config)) {
out[k] = typeof v === 'string' ? decrypt_api_key(v) : v;
}
return out;
}
@@ -0,0 +1,71 @@
import { All, Controller, Param, Req, Res } from '@nestjs/common';
import type { Request, Response } from 'express';
import { extractToken, verifyJwtAndLoadUser } from '../../middleware/auth';
import { pluginsEnabled } from './kill-switch';
import { PluginRuntimeService } from './plugin-runtime.service';
/**
* Proxies a plugin's own HTTP routes at /api/plugins/:id/* (#plugins, M2).
*
* This is a SINGLE static Nest route — it never registers per-plugin routes at
* runtime. It matches the request against the plugin's declared routes, enforces
* per-route auth (routes with `auth:false` — OAuth callbacks/webhooks — skip the
* session check), and forwards a minimal, whitelisted request view to the
* isolated child over RPC. The plugin never sees raw headers or the session
* cookie, so it cannot replay the user's credentials.
*/
const SAFE_RESPONSE_HEADERS = new Set(['content-type', 'cache-control', 'content-disposition', 'location']);
@Controller('api/plugins/:pluginId')
export class PluginsProxyController {
constructor(private readonly runtime: PluginRuntimeService) {}
@All('*path')
async proxy(@Param('pluginId') pluginId: string, @Req() req: Request, @Res() res: Response): Promise<void> {
if (!pluginsEnabled() || !this.runtime.isActive(pluginId)) {
res.status(404).json({ error: 'Plugin not found' });
return;
}
const rest = (req.params as Record<string, unknown>).path ?? (req.params as Record<string, unknown>)[0] ?? '';
const sub = '/' + (Array.isArray(rest) ? rest.join('/') : String(rest)).replace(/^\/+/, '');
const route = this.runtime.routesOf(pluginId).find((r) => r.method === req.method && r.path === sub);
if (!route) {
res.status(404).json({ error: 'Not found' });
return;
}
// Per-route auth: default-on; `auth:false` routes are public (OAuth cb/webhook).
let user: { id: number; username: string; is_admin?: boolean } | null = null;
if (route.auth) {
const token = extractToken(req);
const loaded = token ? verifyJwtAndLoadUser(token) : null;
if (!loaded) {
res.status(401).json({ error: 'Access token required', code: 'AUTH_REQUIRED' });
return;
}
user = loaded;
}
try {
const reply = (await this.runtime.invoke(pluginId, 'invoke.route', {
routeId: route.i,
req: {
method: req.method,
path: sub,
query: req.query,
body: req.body ?? null,
user: user ? { id: user.id, username: user.username, isAdmin: !!user.is_admin } : null,
},
})) as { status?: number; headers?: Record<string, string>; body?: unknown };
res.status(reply?.status ?? 200);
for (const [k, v] of Object.entries(reply?.headers ?? {})) {
if (SAFE_RESPONSE_HEADERS.has(k.toLowerCase())) res.setHeader(k, v);
}
res.send(reply?.body ?? '');
} catch (e) {
res.status(502).json({ error: 'Plugin error', detail: e instanceof Error ? e.message : 'unknown' });
}
}
}
+39 -6
View File
@@ -1,24 +1,57 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, HttpCode, HttpException, Param, Post, Put, UseGuards } from '@nestjs/common';
import { PluginsService } from './plugins.service';
import { PluginRuntimeService } from './plugin-runtime.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { AdminGuard } from '../auth/admin.guard';
import { pluginsEnabled } from './kill-switch';
/**
* /api/admin/plugins — admin-only plugin control surface (#plugins).
*
* M0 scaffold: read-only listing + the runtime-enabled flag. Install, activate,
* disable, uninstall and the registry browser are added in later milestones.
* M0: read-only listing + the runtime-enabled flag.
* M2: activate / deactivate (spawns/kills the isolated child) + instance config.
* Admin-gated like the rest of /api/admin. The proxy namespace /api/plugins/:id
* is reserved for the (future) runtime and is intentionally NOT part of this
* admin controller.
* is a separate controller.
*/
@Controller('api/admin/plugins')
@UseGuards(JwtAuthGuard, AdminGuard)
export class PluginsController {
constructor(private readonly plugins: PluginsService) {}
constructor(
private readonly plugins: PluginsService,
private readonly runtime: PluginRuntimeService,
) {}
@Get()
list() {
return this.plugins.list();
}
@Get(':id/config')
getConfig(@Param('id') id: string) {
return { config: this.plugins.getInstanceConfig(id) };
}
@Put(':id/config')
updateConfig(@Param('id') id: string, @Body() body: Record<string, unknown>) {
return { config: this.plugins.updateInstanceConfig(id, body || {}) };
}
@Post(':id/activate')
@HttpCode(200)
async activate(@Param('id') id: string) {
if (!pluginsEnabled()) throw new HttpException({ error: 'Plugins are disabled by server configuration' }, 503);
try {
await this.runtime.activate(id);
} catch (e) {
throw new HttpException({ error: e instanceof Error ? e.message : 'activation failed' }, 400);
}
return { status: this.runtime.isActive(id) ? 'active' : 'error' };
}
@Post(':id/deactivate')
@HttpCode(200)
async deactivate(@Param('id') id: string) {
await this.runtime.deactivate(id);
return { status: 'inactive' };
}
}
+7 -5
View File
@@ -1,14 +1,16 @@
import { Module } from '@nestjs/common';
import { PluginsController } from './plugins.controller';
import { PluginsProxyController } from './plugins-proxy.controller';
import { PluginsService } from './plugins.service';
import { PluginRuntimeService } from './plugin-runtime.service';
/**
* Plugin system (#plugins). M0 registers only the read side; the isolated
* runtime supervisor, install pipeline and registry service are added in later
* milestones under this same module.
* Plugin system (#plugins). M0 read side + M2 isolated runtime: the runtime
* service owns the process supervisor and boots active plugins on startup; the
* proxy controller forwards /api/plugins/:id/* to the isolated child.
*/
@Module({
controllers: [PluginsController],
providers: [PluginsService],
controllers: [PluginsController, PluginsProxyController],
providers: [PluginsService, PluginRuntimeService],
})
export class PluginsModule {}
+64 -1
View File
@@ -1,6 +1,9 @@
import { Injectable } from '@nestjs/common';
import { db } from '../../db/database';
import { pluginsEnabled } from '../../config';
import { pluginsEnabled } from './kill-switch';
import { maybe_encrypt_api_key } from '../../services/apiKeyCrypto';
const SECRET_MASK = '••••••••';
/**
* Read side of the plugin system (#plugins), M0 scaffold. Lists installed
@@ -33,4 +36,64 @@ export class PluginsService {
.all() as PluginListItem[];
return { enabled: pluginsEnabled(), plugins };
}
/**
* Merge instance-scope settings into the plugin's config, encrypting fields
* declared secret (unless the value is the unchanged mask sentinel). Returns
* the config with secrets masked for the client.
*/
updateInstanceConfig(id: string, patch: Record<string, unknown>): Record<string, unknown> {
const row = db.prepare('SELECT config FROM plugins WHERE id = ?').get(id) as { config: string } | undefined;
if (!row) throw new Error(`plugin ${id} not found`);
const secretKeys = new Set(
(
db
.prepare("SELECT field_key FROM plugin_settings_fields WHERE plugin_id = ? AND scope = 'instance' AND secret = 1")
.all(id) as Array<{ field_key: string }>
).map((r) => r.field_key),
);
const config = safeParse(row.config);
for (const [k, v] of Object.entries(patch)) {
if (secretKeys.has(k)) {
if (v === SECRET_MASK) continue; // unchanged secret — keep stored ciphertext
config[k] = maybe_encrypt_api_key(v);
} else {
config[k] = v;
}
}
db.prepare('UPDATE plugins SET config = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?').run(JSON.stringify(config), id);
return maskSecrets(config, secretKeys);
}
/** Read the instance config with secret fields masked. */
getInstanceConfig(id: string): Record<string, unknown> {
const row = db.prepare('SELECT config FROM plugins WHERE id = ?').get(id) as { config: string } | undefined;
if (!row) throw new Error(`plugin ${id} not found`);
const secretKeys = new Set(
(
db
.prepare("SELECT field_key FROM plugin_settings_fields WHERE plugin_id = ? AND scope = 'instance' AND secret = 1")
.all(id) as Array<{ field_key: string }>
).map((r) => r.field_key),
);
return maskSecrets(safeParse(row.config), secretKeys);
}
}
function safeParse(json: string): Record<string, unknown> {
try {
const v = JSON.parse(json || '{}');
return v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : {};
} catch {
return {};
}
}
function maskSecrets(config: Record<string, unknown>, secretKeys: Set<string>): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(config)) {
out[k] = secretKeys.has(k) && v ? SECRET_MASK : v;
}
return out;
}
@@ -51,7 +51,11 @@ async function boot(config: Record<string, unknown>): Promise<void> {
def = mod && mod.default ? (mod.default as PluginDefinition) : (mod as PluginDefinition);
ctx = createPluginContext(pluginId, config, transport);
if (typeof def.onLoad === 'function') await def.onLoad(ctx);
send({ k: 'evt', topic: 'loaded', data: {} });
// Report the declared routes (with their index = routeId) and job ids so the
// host can proxy HTTP and schedule jobs without re-parsing the manifest.
const routes = (def.routes ?? []).map((r, i) => ({ i, method: r.method, path: r.path, auth: r.auth !== false }));
const jobs = (def.jobs ?? []).map((j) => j.id);
send({ k: 'evt', topic: 'loaded', data: { routes, jobs } });
// An immediate first heartbeat confirms liveness without waiting a full interval.
send({ k: 'evt', topic: 'heartbeat', data: { rss: process.memoryUsage().rss } });
} catch (e) {
@@ -59,6 +63,37 @@ async function boot(config: Record<string, unknown>): Promise<void> {
}
}
/** Handle a host→child request: run a declared route or job with the plugin ctx. */
async function handleInvoke(req: { id: string; method: string; params: Record<string, unknown> }): Promise<void> {
const respond = (ok: boolean, payload: unknown) =>
send(
ok
? { k: 'res', id: req.id, ok: true, result: payload }
: { k: 'res', id: req.id, ok: false, error: { code: 'PLUGIN_ERROR', message: String(payload) } },
);
try {
if (!def || !ctx) throw new Error('plugin not loaded');
if (req.method === 'invoke.route') {
const routeId = req.params.routeId as number;
const route = def.routes?.[routeId];
if (!route) throw new Error(`no route ${routeId}`);
const pluginReq = req.params.req as Parameters<NonNullable<typeof route.handler>>[0];
const result = await route.handler(pluginReq, ctx);
respond(true, result);
} else if (req.method === 'invoke.job') {
const jobId = req.params.jobId as string;
const job = def.jobs?.find((j) => j.id === jobId);
if (!job) throw new Error(`no job ${jobId}`);
await job.handler(ctx);
respond(true, { ok: true });
} else {
respond(false, `unknown invoke ${req.method}`);
}
} catch (e) {
respond(false, errMsg(e));
}
}
async function shutdown(): Promise<void> {
try {
if (def && typeof def.onUnload === 'function' && ctx) await def.onUnload(ctx);
@@ -72,6 +107,11 @@ async function shutdown(): Promise<void> {
process.on('message', (raw: unknown) => {
const msg = raw as Envelope;
if (!msg || typeof msg !== 'object') return;
if (msg.k === 'req') {
// A host→child invoke (route / job).
void handleInvoke({ id: msg.id, method: msg.method, params: (msg.params ?? {}) as Record<string, unknown> });
return;
}
if (msg.k === 'res') {
const p = pending.get(msg.id);
if (!p) return;
@@ -1,8 +1,16 @@
import { randomUUID } from 'node:crypto';
import { fork, type ChildProcess } from 'node:child_process';
import { resolveChildEntry, pluginCodeDir } from '../paths';
import type { Envelope, RpcRequest } from '../protocol/envelope';
import type { Envelope, RpcError, RpcRequest } from '../protocol/envelope';
import type { PluginRpcHost } from '../host/rpc-host';
export interface PluginRouteInfo {
i: number;
method: string;
path: string;
auth: boolean;
}
/**
* Owns the lifecycle of every running plugin child (#plugins, M1): spawn on
* activate, route RPC between the child and its capability host, watch
@@ -21,6 +29,12 @@ export interface SupervisorHooks {
onEvent?(id: string, topic: string, data: unknown): void;
}
interface Pending {
resolve: (v: unknown) => void;
reject: (e: Error) => void;
timer: ReturnType<typeof setTimeout>;
}
interface Supervised {
id: string;
granted: ReadonlySet<string>;
@@ -30,6 +44,9 @@ interface Supervised {
status: PluginStatus;
crashes: number[]; // crash timestamps (ms)
lastBeat: number;
routes: PluginRouteInfo[];
jobs: string[];
pending: Map<string, Pending>; // host→child invokes awaiting a response
activation?: { resolve: () => void; reject: (e: Error) => void };
}
@@ -74,6 +91,9 @@ export class PluginSupervisor {
status: 'starting',
crashes: [],
lastBeat: Date.now(),
routes: [],
jobs: [],
pending: new Map(),
};
this.running.set(id, sup);
this.ensureSweep();
@@ -99,6 +119,28 @@ export class PluginSupervisor {
statusOf(id: string): PluginStatus | null {
return this.running.get(id)?.status ?? null;
}
/** The plugin's declared HTTP routes (populated once it reports `loaded`). */
routesOf(id: string): PluginRouteInfo[] {
return this.running.get(id)?.routes ?? [];
}
/** Send a host→child request (invoke a route or job) and await its response. */
invoke(id: string, method: string, params: Record<string, unknown>, timeoutMs = 30_000): Promise<unknown> {
const sup = this.running.get(id);
if (!sup || sup.status !== 'active' || !sup.child) {
return Promise.reject(new Error(`plugin ${id} is not active`));
}
return new Promise((resolve, reject) => {
const reqId = randomUUID();
const timer = setTimeout(() => {
sup.pending.delete(reqId);
reject(new Error('plugin invoke timed out'));
}, timeoutMs);
timer.unref?.();
sup.pending.set(reqId, { resolve, reject, timer });
sup.child!.send({ k: 'req', id: reqId, method, params } satisfies Envelope);
});
}
async shutdownAll(): Promise<void> {
if (this.sweep) clearInterval(this.sweep);
@@ -145,6 +187,17 @@ export class PluginSupervisor {
return;
}
if (msg.k === 'res') {
// A response to a host→child invoke (route/job).
const p = sup.pending.get(msg.id);
if (!p) return;
sup.pending.delete(msg.id);
clearTimeout(p.timer);
if (msg.ok) p.resolve(msg.result);
else p.reject(new Error((msg as RpcError).error.message));
return;
}
if (msg.k === 'evt') {
switch (msg.topic) {
case 'hello':
@@ -153,12 +206,16 @@ export class PluginSupervisor {
case 'heartbeat':
sup.lastBeat = Date.now();
break;
case 'loaded':
case 'loaded': {
sup.lastBeat = Date.now();
const d = msg.data as { routes?: PluginRouteInfo[]; jobs?: string[] };
sup.routes = d.routes ?? [];
sup.jobs = d.jobs ?? [];
this.setStatus(sup, 'active');
sup.activation?.resolve();
sup.activation = undefined;
break;
}
case 'load-error': {
const message = (msg.data as { message?: string })?.message || 'plugin load failed';
this.setStatus(sup, 'error', message);
@@ -179,9 +236,18 @@ export class PluginSupervisor {
}
}
private rejectPending(sup: Supervised, reason: string): void {
for (const p of sup.pending.values()) {
clearTimeout(p.timer);
p.reject(new Error(reason));
}
sup.pending.clear();
}
private onExit(sup: Supervised, code: number | null, signal: string | null): void {
// Reject any pending ctx calls implicitly by killing the child; the child is gone.
sup.child = null;
// In-flight host→child invokes can never complete now.
this.rejectPending(sup, 'plugin exited');
// A clean stop we asked for isn't a crash.
if (sup.status === 'stopped' || sup.status === 'error') return;
if (!this.running.has(sup.id)) return;