89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
import { DynamicModule, Module } from '@nestjs/common';
|
|
import { open, lstat } from 'node:fs/promises';
|
|
import { constants } from 'node:fs';
|
|
import { dirname, resolve } from 'node:path';
|
|
import { HermesManagementModule } from './hermes-management.module';
|
|
import {
|
|
RuntimeReadAccess,
|
|
RuntimeReadController,
|
|
} from './runtime-read.controller';
|
|
import { HermesError } from './hermes.adapter';
|
|
export async function runtimeSecret(path: string): Promise<string> {
|
|
if (!/^\/run\/secrets\/[a-zA-Z0-9_-]+$/.test(path))
|
|
throw new HermesError('CONFIGURATION');
|
|
try {
|
|
const file = await open(path, 'r');
|
|
try {
|
|
const stat = await file.stat();
|
|
if (!stat.isFile() || stat.size > 4096) throw new Error();
|
|
const value = (await file.readFile('utf8')).trim();
|
|
if (!value || /[\r\n]/.test(value)) throw new Error();
|
|
return value;
|
|
} finally {
|
|
await file.close();
|
|
}
|
|
} catch {
|
|
throw new HermesError('AUTH_REQUIRED');
|
|
}
|
|
}
|
|
/** Exact fixed mount roots only; startup fails closed, never browser/JWT grants. */
|
|
export async function loadRuntimeGrants(
|
|
path = '/run/secrets/hermes_runtime_grants',
|
|
reviewedRoot?: string,
|
|
): Promise<Readonly<Record<string, readonly string[]>>> {
|
|
const roots = reviewedRoot
|
|
? [resolve(reviewedRoot)]
|
|
: ['/run/secrets', '/etc/canina'];
|
|
if (!roots.includes(dirname(path)) || resolve(path) !== path)
|
|
throw new HermesError('CONFIGURATION');
|
|
try {
|
|
const stat = await lstat(path);
|
|
if (
|
|
!stat.isFile() ||
|
|
stat.isSymbolicLink() ||
|
|
stat.size > 65536 ||
|
|
stat.mode & 0o022
|
|
)
|
|
throw new Error();
|
|
const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
try {
|
|
const data: unknown = JSON.parse(await file.readFile('utf8'));
|
|
if (!data || typeof data !== 'object' || Array.isArray(data))
|
|
throw new Error();
|
|
new RuntimeReadAccess(data as Record<string, readonly string[]>);
|
|
return data as Record<string, readonly string[]>;
|
|
} finally {
|
|
await file.close();
|
|
}
|
|
} catch {
|
|
throw new HermesError('CONFIGURATION');
|
|
}
|
|
}
|
|
@Module({})
|
|
export class RuntimeReadModule {
|
|
static async registerFromFile(
|
|
path = '/run/secrets/hermes_runtime_grants',
|
|
): Promise<DynamicModule> {
|
|
return this.register(await loadRuntimeGrants(path));
|
|
}
|
|
static register(
|
|
grants: Readonly<Record<string, readonly string[]>>,
|
|
): DynamicModule {
|
|
const access = new RuntimeReadAccess(grants);
|
|
return {
|
|
module: RuntimeReadModule,
|
|
imports: [
|
|
HermesManagementModule.registerAuthenticated(
|
|
async () => ({
|
|
username: await runtimeSecret('/run/secrets/hermes_username'),
|
|
password: await runtimeSecret('/run/secrets/hermes_password'),
|
|
}),
|
|
access,
|
|
),
|
|
],
|
|
controllers: [RuntimeReadController],
|
|
providers: [{ provide: RuntimeReadAccess, useValue: access }],
|
|
};
|
|
}
|
|
}
|