Skip to content

Commit 8f24344

Browse files
authored
feat(logger-json): JSON logger with a fail-closed secret scrubber (#22)
* feat(logger-json): JSON logger with a fail-closed secret scrubber * fix(logger-json): redact quoted keys and judge key=value pairs by the structured key rule * fix(logger-json): skip escaped quotes in quoted values; only real binary values count as binary * docs(logger-json): state the scrubber's limits and pin them with tests
1 parent 7bde3da commit 8f24344

11 files changed

Lines changed: 1003 additions & 1 deletion

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ stand in this document. There is no project OMP configuration.
8484
authoriser in `src/authorize`, tests in `test/`.
8585
- Adapters (database, auth, storage, and so on) live in their own packages
8686
under `packages/adapters/*`; `config-env` (reads the environment into the typed
87-
config, the only place that reads `process.env`) exists, the others arrive later. Apps live in `apps/*`: `apps/web`
87+
config, the only place that reads `process.env`) and `logger-json` (JSON logs with a
88+
secret scrubber that fails closed) exist, the others arrive later. Apps live in `apps/*`: `apps/web`
8889
(the web client) and `packages/api-client` exist today as empty placeholders.
8990
- `tools/ci`: repository checks, with their own tests (`bun run test:tools`).
9091
- `.github/workflows`: CI. Change it only on purpose.

‎bun.lock‎

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@tektum/grimoire-adapter-logger-json",
3+
"version": "0.0.0",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"type": "module",
7+
"main": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"types": "./dist/index.d.ts",
12+
"default": "./dist/index.js"
13+
}
14+
},
15+
"dependencies": {
16+
"@tektum/grimoire-core": "*",
17+
"pino": "10.4.0"
18+
}
19+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { pino } from 'pino';
2+
import type { Logger as PinoLogger } from 'pino';
3+
import type { Clock } from '@tektum/grimoire-core/ports/clock';
4+
import type { Logger, LogLevel } from '@tektum/grimoire-core/ports/logger';
5+
import { redactString, scrubFields } from './scrub.js';
6+
7+
export { redactString } from './scrub.js';
8+
9+
export interface JsonLoggerOptions {
10+
readonly level?: LogLevel;
11+
/** Receives pino's output (one JSON line ending in a newline). Defaults to standard output. */
12+
readonly destination?: { write(line: string): unknown };
13+
readonly clock?: Clock;
14+
readonly fields?: Record<string, unknown>;
15+
}
16+
17+
// First layer: pino's own path redaction for the known secret names (one level deep).
18+
const SECRET_NAMES = ['token', 'password', 'passwd', 'pwd', 'secret', 'authorization', 'cookie', 'apikey', 'api_key', 'access_key', 'secret_key', 'session', 'credential', 'credentials'];
19+
const REDACT_PATHS = SECRET_NAMES.flatMap((n) => [n, `*.${n}`]);
20+
21+
interface TsState { value: string }
22+
23+
function wrap(p: PinoLogger, ts: TsState, now: () => string | undefined): Logger {
24+
const emit = (level: LogLevel, event: string, fields?: Record<string, unknown>): void => {
25+
try {
26+
const t = now();
27+
ts.value = t ?? new Date().toISOString();
28+
const clean = scrubFields(fields);
29+
if (t === undefined) clean['clockFallback'] = true;
30+
p[level](clean, redactString(String(event)));
31+
} catch {
32+
// The logger never throws into its caller.
33+
}
34+
};
35+
return {
36+
debug: (e, f) => emit('debug', e, f),
37+
info: (e, f) => emit('info', e, f),
38+
warn: (e, f) => emit('warn', e, f),
39+
error: (e, f) => emit('error', e, f),
40+
child: (fields) => {
41+
try {
42+
return wrap(p.child(scrubFields(fields)), ts, now);
43+
} catch {
44+
return wrap(p, ts, now);
45+
}
46+
},
47+
};
48+
}
49+
50+
export function createJsonLogger(opts: JsonLoggerOptions = {}): Logger {
51+
const clock = opts.clock;
52+
// The clock is read once per event; an unusable reading falls back to real time and the event
53+
// carries `clockFallback: true`.
54+
const now = (): string | undefined => {
55+
try {
56+
return new Date(clock ? clock.now() : Date.now()).toISOString();
57+
} catch {
58+
return undefined;
59+
}
60+
};
61+
const ts: TsState = { value: new Date().toISOString() };
62+
const options = {
63+
level: opts.level ?? 'info',
64+
messageKey: 'event',
65+
base: null,
66+
timestamp: (): string => `,"ts":"${ts.value}"`,
67+
formatters: { level: (label: string) => ({ level: label }) },
68+
redact: { paths: REDACT_PATHS, censor: '[redacted]' },
69+
};
70+
const root = opts.destination ? pino(options, opts.destination) : pino(options);
71+
return wrap(opts.fields ? root.child(scrubFields(opts.fields)) : root, ts, now);
72+
}

0 commit comments

Comments
 (0)