Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 10x 10x 10x 2x 8x 27x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 13x 13x 35x 35x 35x 35x 35x 35x 35x 9x 9x 1x 9x 1x 1x 1x 9x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 35x 35x 35x 35x 35x 1x | import type { IConfigRuntime, IGateway, ILog, IRegistry } from '@feasibleone/blong/types';
import { Internal } from '@feasibleone/blong/types';
import type { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';
interface IConfig {
enabled: boolean;
routePrefix: string;
auth: false | 'jwt';
}
interface IGatewayWithPlugins extends IGateway {
registerPlugin(plugin: unknown, options?: unknown): void;
}
interface IRpcServerWithInfo {
info(): object;
}
/** The members a JWK uses for private material — what must never reach an introspection answer. */
const PRIVATE_JWK_MEMBERS = ['d', 'p', 'q', 'dp', 'dq', 'qi', 'k', 'oth'];
/**
* The configuration snapshot with private key material removed.
*
* `/api/sys/config` exists to show what a process was configured with, and the gateway's pair is part
* of that — but the pair the loader resolves carries the private half, and a reader of this endpoint
* must not receive a key that mints the tokens the gateway verifies. Only values that *are* JWKs are
* touched: an object with `kty` loses its private members, and everything else, including a property
* merely named `d`, is served as it is. This endpoint is dev-intent only, and that guardrail stands
* on its own: `systemDebug` must never be enabled in production (D-448).
*/
export const publicConfigSnapshot = (value: unknown): unknown => {
if (Array.isArray(value)) return value.map(publicConfigSnapshot);
if (!value || typeof value !== 'object') return value;
const entries = Object.entries(value as Record<string, unknown>);
const isJwk = typeof (value as {kty?: unknown}).kty === 'string';
const kept = isJwk
? entries.filter(([name]) => !PRIVATE_JWK_MEMBERS.includes(name))
: entries;
return Object.fromEntries(kept.map(([name, member]) => [name, publicConfigSnapshot(member)]));
};
// The api object is captured by reference so that configRuntime — which is set
// on it after infra items are constructed (load.ts) — is visible at request time.
interface IApiRef {
log?: ILog;
gateway?: IGatewayWithPlugins;
registry?: IRegistry;
rpcServer?: IRpcServerWithInfo;
configRuntime?: IConfigRuntime;
}
export default class SystemDebug extends Internal {
#config: IConfig = {
enabled: false,
routePrefix: '/api/sys',
auth: false,
};
#apiRef: IApiRef;
public constructor(config: IConfig, apiRef: IApiRef) {
super({log: apiRef.log});
this.merge(this.#config, config);
this.#apiRef = apiRef;
}
public async init(): Promise<void> {
if (!this.#config.enabled || !this.#apiRef.gateway) return;
this.signal?.frameworkDebugEnabled?.();
const apiRef = this.#apiRef;
const prefix = this.#config.routePrefix;
const authConfig = this.#config.auth;
const plugin = fp(
async (server: FastifyInstance) => {
// GET /api/sys/config — effective runtime configuration snapshot, with the private
// halves of any key pair removed: the resolved pair is configuration, and a reader of
// this endpoint must not be handed a key that signs what the gateway verifies.
server.route({
method: 'GET',
url: `${prefix}/config`,
config: {auth: authConfig},
handler: async () =>
publicConfigSnapshot(apiRef.configRuntime?.rawSnapshot ?? {}),
});
// GET /api/sys/ports — registered adapter/orchestrator port definitions
server.route({
method: 'GET',
url: `${prefix}/ports`,
config: {auth: authConfig},
handler: async () => ({
ports: Array.from(apiRef.registry?.ports.keys() ?? []),
}),
});
// GET /api/sys/methods — registered handler method groups with handler counts
server.route({
method: 'GET',
url: `${prefix}/methods`,
config: {auth: authConfig},
handler: async () => ({
methods: Array.from(apiRef.registry?.methods.entries() ?? []).map(
([name, handlers]) => ({
name,
handlerCount: handlers.length,
}),
),
}),
});
// GET /api/sys/modules — registered realm modules.
// Symbol keys (used internally for system-tagged infrastructure items)
// are excluded because they are not JSON-serialisable.
server.route({
method: 'GET',
url: `${prefix}/modules`,
config: {auth: authConfig},
handler: async () => ({
modules: Array.from(apiRef.registry?.modules.keys() ?? []).filter(
(k): k is string => typeof k === 'string',
),
}),
});
// GET /api/sys/rpc — internal RPC server address info
server.route({
method: 'GET',
url: `${prefix}/rpc`,
config: {auth: authConfig},
handler: async () => apiRef.rpcServer?.info() ?? {},
});
},
{name: 'system-debug'},
);
apiRef.gateway!.registerPlugin(plugin);
}
}
|