[glm-grade=B] feat(monitoring): host journald log viewer (DC-055)

Adds a dedicated dashboard surface for host journald logs (caddy, docker,
dashcaddy-api, ssh, ...) via a read-only bind-mount of /var/log/journal +
journalctl. Closes queue item #2: the only way to see the recurring
'100.120.159.34:5000 i/o timeout' spam in Caddy's health_checker logs was
SSH into DNS2.

Backend (dashcaddy-api/):
- src/monitoring/journald-reader.js (NEW, ~320 lines) wraps journalctl
  with allow-listed unit names (caddy, docker, dashcaddy-api, ssh,
  systemd-journald, tailscaled, networkd-dispatcher), validates
  since/until/search before argv assembly, and uses spawn() with an argv
  array (no shell). Clamps tail at MAX_TAIL_LINES=5000 and stdout at
  MAX_OUTPUT_BUFFER=2MB; streaming also caps at MAX_STREAM_LINES=5000
  via a closure-scoped counter. Maps ENOENT cleanly to 'journalctl
  unavailable'.
- routes/logs.js (+102 lines): three new routes mounted under the
  existing auth-gated apiRouter: GET /api/v1/logs/journal/units,
  GET /api/v1/logs/journal (bounded tail read), and GET
  /api/v1/logs/journal/stream (SSE). Stream route pre-validates unit
  with assertUnitAllowed BEFORE writing SSE headers so an invalid unit
  returns 400 JSON instead of an open stream with an error frame.
- 41 new tests across 2 files covering allow-list enforcement, shell-meta
  rejection in unit/since/until/search, MAX_OUTPUT_BUFFER cap, ENOENT
  mapping, non-zero exit stderr surfacing, and route-level 400-on-bad-unit.
  Full local suite 1831/1831 (+41 net).

Container plumbing (start.sh):
- Two new bind mounts:
    -v /var/log/journal:/var/log/journal:ro
    -v /usr/bin/journalctl:/usr/bin/journalctl:ro
  Bind-mount chosen over privileged systemd-journal remote to keep the
  container unprivileged and the journal access read-only.

Frontend (status/js/):
- journald.js (NEW, ~285 lines) self-contained modal mirroring the
  existing Container Logs modal. SSE via EventSource, debounced search
  (200ms), overflow hint when stream cap is hit, unit dropdown from a
  fixed allow-list that mirrors the backend. Hooked via the new
  '#view-journald-logs' button in the Tools dropdown (next to Container
  Logs).
- build.js (+4 lines) adds journald.js to the features bundle. Bundle
  rebuild succeeded (features.js 27 files, 466 KB raw / 1229 KB min).
  CSP hash unchanged (no inline script changes).

GLM judge (round 1, 178s, 14 tool calls, cold diff + 8 file reads):
GRADE=B. Shell injection fully defended (all four attacker inputs
rejected before spawn). Route-level allow-list holds (streamEntries not
called for bad unit). SSE cleanup correct. Round-2 fix-first applied
same commit: the round-1 stream's 5000-line cap was dead code (counter
on function object never incremented) moved to closure scope and now
actually fires. Also dropped deprecated req.on('aborted') listener
(Node 18+ fires 'close' for both clean and abort).

Container live HEAD 901df86 [glm-grade=B]; deploy via start.sh atomic
swap. Live verify: status.sami=200, container Up + healthy, the new
bundle and index.html served.
This commit is contained in:
Hermes
2026-08-18 01:29:19 -07:00
parent 901df8608b
commit 3a74cc423a
10 changed files with 1556 additions and 183 deletions
@@ -0,0 +1,196 @@
/**
* DC-055: Host journald route smoke tests.
*
* Mounts the routes/logs.js journald endpoints into a tiny express app
* with a mocked journald reader. The mock mirrors the real module's
* validation pipeline (assertUnitAllowed, parseTail, parseTimestamp) so
* bad inputs still throw ValidationError -> 400 at the route boundary,
* but the actual journalctl spawn is short-circuited.
*/
const express = require('express');
const request = require('supertest');
const path = require('path');
const realJournaldPath = require.resolve('../../src/monitoring/journald-reader.js');
// Pull the real module's validators so the mock's readEntries can
// reproduce the same 400-on-bad-input behaviour as production.
const realReader = jest.requireActual(realJournaldPath);
// Mocked journald reader. Variable name MUST start with "mock" so
// jest.mock hoisting doesn't reject the factory closure.
const mockJournald = {
ALLOWED_UNITS: realReader.ALLOWED_UNITS,
MAX_TAIL_LINES: realReader.MAX_TAIL_LINES,
MAX_OUTPUT_BUFFER: realReader.MAX_OUTPUT_BUFFER,
isAvailable: jest.fn().mockResolvedValue(true),
// Validation pipeline runs through the real assert/parse functions so
// bad unit/tail/since/until still surface as ValidationError. The
// journalctl spawn itself is short-circuited — return canned entries.
readEntries: jest.fn(async (opts) => {
const unit = realReader.assertUnitAllowed(opts.unit);
realReader.parseTail(opts.tail); // throws on bad tail
realReader.parseTimestamp(opts.since, 'since');
realReader.parseTimestamp(opts.until, 'until');
return [
{ timestamp: 'Aug 18 00:42:46', hostname: 'host', unit, text: 'mock-line-1' },
];
}),
// Default stream mock: invokes onData with one synthetic entry then
// returns a no-op handle. Tests override per-case.
streamEntries: jest.fn((opts, hooks = {}) => {
if (hooks.onData) {
hooks.onData({ timestamp: 'Aug 18 00:42:46', unit: opts.unit, text: 'stream-line-1' });
}
return { kill: jest.fn(), child: {} };
}),
listUnits: jest.fn(async () => [
{ unit: 'caddy', hasEntries: true },
{ unit: 'docker', hasEntries: true },
]),
assertUnitAllowed: realReader.assertUnitAllowed,
parseTail: realReader.parseTail,
parseTimestamp: realReader.parseTimestamp,
parseShortLine: realReader.parseShortLine,
buildArgv: realReader.buildArgv,
};
jest.mock('../../src/monitoring/journald-reader.js', () => mockJournald);
// Force journaldAvailable = true in routes/logs.js. The route checks
// /var/log/journal + /usr/bin/journalctl at module-load time, so we stub
// fs.existsSync to lie about those paths.
const realFs = require('fs');
const realExists = realFs.existsSync;
realFs.existsSync = function(p) {
if (p === '/var/log/journal' || p === '/usr/bin/journalctl') return true;
return realExists.apply(this, arguments);
};
const logsRoutes = require('../../routes/logs.js');
function buildApp() {
const app = express();
const asyncHandler = (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
const ok = (res, data) => res.json({ success: true, ...data });
const errorHandler = (err, req, res, next) => {
const status = err.statusCode || (err.name === 'ValidationError' ? 400 : 500);
res.status(status).json({ success: false, error: err.message });
};
app.use('/api/v1', logsRoutes({ asyncHandler, ok }));
app.use(errorHandler);
return app;
}
describe('routes /logs/journal', () => {
let app;
beforeEach(async () => {
mockJournald.readEntries.mockClear();
mockJournald.streamEntries.mockClear();
mockJournald.listUnits.mockClear();
app = buildApp();
// Let any keep-alive socket from the prior test close before we
// bind a new express app.
await new Promise(r => setTimeout(r, 10));
});
describe('GET /logs/journal/units', () => {
test('returns unit list when journald is mounted', async () => {
const res = await request(app).get('/api/v1/logs/journal/units');
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.available).toBe(true);
expect(res.body.units.length).toBeGreaterThanOrEqual(1);
});
});
describe('GET /logs/journal', () => {
test('returns entries for caddy', async () => {
const res = await request(app)
.get('/api/v1/logs/journal')
.query({ unit: 'caddy', tail: 50 });
expect(res.status).toBe(200);
expect(res.body.entries.length).toBeGreaterThanOrEqual(1);
expect(res.body.entries[0].unit).toBe('caddy');
expect(mockJournald.readEntries).toHaveBeenCalled();
const call = mockJournald.readEntries.mock.calls[0][0];
expect(call.unit).toBe('caddy');
expect(call.tail).toBe('50');
});
test('forwards since/until/search verbatim', async () => {
await request(app).get('/api/v1/logs/journal').query({
unit: 'caddy', tail: 100,
since: '2026-08-18T00:00:00Z',
until: '2026-08-18T23:59:59Z',
search: 'health',
});
const call = mockJournald.readEntries.mock.calls[0][0];
expect(call.since).toBe('2026-08-18T00:00:00Z');
expect(call.until).toBe('2026-08-18T23:59:59Z');
expect(call.search).toBe('health');
});
test('returns 400 when unit not in allow-list', async () => {
const res = await request(app).get('/api/v1/logs/journal').query({ unit: 'nginx' });
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/not in allow-list/);
// The reader is called and rejects; the route layer maps the
// ValidationError to 400 without doing any spawn.
expect(mockJournald.readEntries).toHaveBeenCalled();
});
test('returns 400 when unit contains shell metacharacters', async () => {
const res = await request(app).get('/api/v1/logs/journal').query({ unit: 'caddy; rm -rf /' });
expect(res.status).toBe(400);
expect(mockJournald.readEntries).toHaveBeenCalled();
});
test('returns 400 when tail is invalid', async () => {
const res = await request(app).get('/api/v1/logs/journal').query({ unit: 'caddy', tail: 'oops' });
expect(res.status).toBe(400);
});
test('returns 500 when reader throws non-validation error', async () => {
mockJournald.readEntries.mockRejectedValueOnce(new Error('journalctl exited 1: bad dir'));
const res = await request(app).get('/api/v1/logs/journal').query({ unit: 'caddy' });
expect(res.status).toBe(500);
expect(res.body.error).toMatch(/journalctl exited 1/);
});
});
describe('GET /logs/journal/stream', () => {
test('opens SSE with correct content-type for a valid unit', async () => {
// Stub the mock to immediately call onError so the route ends
// the response and supertest can collect it. Production SSE
// streams stay open until the client disconnects — covered by
// the journald-reader.streamEntries unit tests.
mockJournald.streamEntries.mockImplementationOnce((opts, hooks) => {
setTimeout(() => hooks.onError && hooks.onError(new Error('synthetic-EOF')), 5);
return { kill: jest.fn(), child: {} };
});
const res = await request(app)
.get('/api/v1/logs/journal/stream')
.query({ unit: 'caddy' })
.timeout(2000);
expect(res.status).toBe(200);
expect(res.headers['content-type']).toMatch(/text\/event-stream/);
});
test('400 when unit not in allow-list', async () => {
// The route pre-validates with journald.assertUnitAllowed BEFORE
// opening SSE — invalid unit returns a 400 JSON response without
// touching the stream.
const res = await request(app)
.get('/api/v1/logs/journal/stream')
.query({ unit: 'nginx' });
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/not in allow-list/);
// streamEntries must NOT have been called for a bad unit.
expect(mockJournald.streamEntries).not.toHaveBeenCalled();
});
});
});