feat(dashboard): AUD1T — privileged-actions audit trail as a LOGS sub-tab

Adds an AUDIT sub-tab to /logs.html (alongside BUILD / AGENT / SYSTEM),
consuming GET /api/audit-log ({ entries, total }). A read-only filterable
table: when / agent / action / target / outcome / detail, newest-first,
with a 'latest 500 of N' header from total and a client-side substring
filter. Outcome badges colour ok green / err red, with an err whose detail
starts 'denied:' rendered amber + labelled 'denied' (capability refusal
reads distinct from an execution failure). Lazy-fetched on tab show (like
SYSTEM); a 30s ticker keeps the relative timestamps honest.

The audit_log store + endpoint landed in the prior audit-log backend work;
this is the operator-visible surface for it. Resolves #1647.
This commit is contained in:
iris 2026-06-13 14:08:27 +02:00
commit 8b991b2cc5
4 changed files with 212 additions and 4 deletions

View file

@ -1,9 +1,11 @@
// /logs.html entry point: log viewer with three sub-tabs (BUILD, AGENT, SYSTEM).
// /logs.html entry point: log viewer with four sub-tabs (BUILD, AGENT,
// SYSTEM, AUDIT).
// BUILD — all-agents build log history, backed by GET /api/build-logs
// AGENT — per-container journald viewer, backed by GET /api/journal/{name}
// SYSTEM — host service logs, backed by GET /api/journal-host
// AUDIT — agent-initiated privileged-action trail, GET /api/audit-log
//
// Tab routing via URL hash (#build, #agent, #system). Default: #build.
// Tab routing via URL hash (#build, #agent, #system, #audit). Default: #build.
//
// Live improvements:
// - Live build output auto-scrolls to the bottom (sticky-bottom) unless
@ -368,6 +370,125 @@ import { createTabStrip } from '@hive/shared/tabs.js';
if (systemUnitSelect) systemUnitSelect.addEventListener('change', fetchSystem);
if (systemRefresh) systemRefresh.addEventListener('click', fetchSystem);
// ─── AUDIT tab ──────────────────────────────────────────────────────
// Operator-visible trail of agent-initiated privileged actions, backed
// by GET /api/audit-log → { entries: [...], total: N } (entries
// newest-first, server-clamped to 500; `total` drives "latest 500 of N").
// Per-entry: { id, ts_unix (secs), agent, action, target, outcome, detail }.
// outcome is 'ok' | 'err'; a capability denial is 'err' with detail
// starting "denied:" — coloured amber to read apart from an execution
// failure. Lazy-fetched on tab show (like SYSTEM); filter is a
// client-side substring on the cached rows.
const auditList = $('audit-list');
const auditFilter = $('audit-filter');
const auditRefresh = $('audit-refresh');
const auditCount = $('audit-count');
let auditEntries = [];
let auditTotal = 0;
let auditFetching = false;
// ts_unix is unix seconds — fmtAgeSecs wants an age in seconds.
function auditFmtWhen(tsUnix) {
if (!tsUnix) return '';
const age = Math.floor(Date.now() / 1000) - tsUnix;
return fmtAgeSecs(Math.max(0, age)) + ' ago';
}
// outcome → badge. 'ok' green; an 'err' whose detail starts "denied:" is a
// capability refusal (amber, labelled "denied"); other 'err' red. The
// literal outcome is the fallback label so a new value still renders.
function auditOutcomeBadge(outcome, detail) {
const denied = outcome === 'err'
&& typeof detail === 'string' && detail.startsWith('denied:');
const cls = outcome === 'ok'
? 'audit-outcome audit-outcome-ok'
: denied
? 'audit-outcome audit-outcome-denied'
: 'audit-outcome audit-outcome-err';
return el('span', { class: cls }, denied ? 'denied' : (outcome || '?'));
}
function auditMatches(e, q) {
if (!q) return true;
return `${e.agent || ''} ${e.action || ''} ${e.target || ''} ${e.detail || ''}`
.toLowerCase().includes(q);
}
function renderAudit() {
if (!auditList) return;
const q = (auditFilter ? auditFilter.value : '').trim().toLowerCase();
const rows = auditEntries.filter((e) => auditMatches(e, q));
if (auditCount) {
const shown = auditEntries.length;
const clamped = auditTotal > shown;
let txt = clamped
? `latest ${shown} of ${auditTotal}`
: `${shown} entr${shown === 1 ? 'y' : 'ies'}`;
if (q) txt += ` · ${rows.length} match${rows.length === 1 ? '' : 'es'}`;
auditCount.textContent = txt;
}
auditList.replaceChildren();
if (rows.length === 0) {
auditList.append(el('p', { class: 'meta' },
q ? '(no matching entries)' : '(no privileged actions recorded yet)'));
return;
}
const table = el('table', { class: 'audit-table' });
table.append(el('thead', {},
el('tr', {},
el('th', { class: 'audit-when-th' }, 'when'),
el('th', {}, 'agent'),
el('th', {}, 'action'),
el('th', {}, 'target'),
el('th', { class: 'audit-outcome-th' }, 'outcome'),
el('th', {}, 'detail'),
)));
const tbody = el('tbody', {});
for (const e of rows) {
tbody.append(el('tr', {},
el('td', {
class: 'audit-when meta',
title: e.ts_unix ? new Date(e.ts_unix * 1000).toISOString() : '',
}, auditFmtWhen(e.ts_unix)),
el('td', { class: 'audit-agent' }, e.agent || ''),
el('td', { class: 'audit-action' }, e.action || ''),
el('td', { class: 'audit-target' }, e.target || ''),
el('td', { class: 'audit-outcome-td' }, auditOutcomeBadge(e.outcome, e.detail)),
el('td', { class: 'audit-detail meta' }, e.detail || ''),
));
}
table.append(tbody);
const wrap = el('div', { class: 'audit-table-wrap' });
wrap.append(table);
auditList.append(wrap);
}
async function fetchAudit() {
if (!auditList || auditFetching) return;
auditFetching = true;
try {
const resp = await fetch('/api/audit-log');
if (!resp.ok) throw new Error('http ' + resp.status);
const data = await resp.json();
auditEntries = Array.isArray(data.entries) ? data.entries : [];
auditTotal = typeof data.total === 'number' ? data.total : auditEntries.length;
renderAudit();
} catch (err) {
auditList.replaceChildren();
auditList.append(el('p', { class: 'meta' }, 'fetch failed: ' + err));
} finally {
auditFetching = false;
}
}
if (auditRefresh) auditRefresh.addEventListener('click', fetchAudit);
if (auditFilter) auditFilter.addEventListener('input', renderAudit);
// ─── init ─────────────────────────────────────────────────────────────
// Wire the shared tab strip now that fetchSystem + the element refs it
@ -375,7 +496,10 @@ import { createTabStrip } from '@hive/shared/tabs.js';
// the deep-linked tab is SYSTEM, kicks off the lazy fetch via onShow.
logTabs = createTabStrip(document.getElementById('logs-tabbar'), {
defaultId: 'build',
onShow: (id) => { if (id === 'system') fetchSystem(); },
onShow: (id) => {
if (id === 'system') fetchSystem();
else if (id === 'audit') fetchAudit();
},
});
loadAgentList();
fetchBuild();
@ -389,6 +513,8 @@ import { createTabStrip } from '@hive/shared/tabs.js';
if (systemLastFetch && systemFetchTs && !systemFetchTs.hidden) {
systemFetchTs.textContent = fmtFetchTs(systemLastFetch);
}
// Keep the audit "ago" column honest while that tab is in view.
if (auditEntries.length && logTabs.active() === 'audit') renderAudit();
}, 30_000);
})();