mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-06 07:01:52 +09:00
A table gets exactly one row of chrome:
[View name v] ....................... [search] [filter] [...]
and nothing else. The row count is gone from it entirely: GridViewPicker's
`total` prop is deleted, not deprecated. It printed the number DataTableFooter
already prints under the rows it counts, and on Servers it appeared twice --
once next to the view name, once on a second toolbar -- plus an "N online" that
no filter, sort or view ever referred to.
Placement is decided by who owns the top bar. A top-level page in a tab group
hoists search, filter and the overflow menu up there next to its own actions;
an inner table (the Docker, security, settings and server-detail tabs) keeps
them inline on the view line, because that route's bar belongs to the page
around it.
useTopbarChrome PORTALS into a slot TabGroupLayout renders rather than handing
the node up through state. That is not a style choice: an effect that publishes
a node upward must name everything the node closes over in its dependency list,
and useTableChrome rebuilds toolsProps on every render -- so it fired every
render, set state on the layout, re-rendered the child, and looped
("Maximum update depth exceeded"). A portal has no dependency list. Using
display:contents on the slot keeps the portaled children as flex items of the
real container, so the top bar's slot ordering still sorts them into fixed
positions -- the order rules name both depths for that reason.
A ListToolbar now survives only if it carries something real: a quick-filter
segment, a scope select, a checkbox. Its `count` prop is deprecated with zero
core render sites; it stays accepted because ListToolbar is SDK surface.
The list/cards layout toggle is removed from core. A layout switch is a third
way to change what the table shows, next to views and column filters, and
unlike those two nothing can save or share it. renderCard keeps working for
extensions since ResourceListPage is SDK surface -- no core page passes it.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
446 lines
18 KiB
React
446 lines
18 KiB
React
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
import api from '../../services/api';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Switch } from '@/components/ui/switch';
|
|
import { DataTable, DataTableFooter, ListToolbar } from '@/components/ds';
|
|
import {
|
|
useTableChrome, GridViewPicker, GridChips, GridFilterButton,
|
|
GridToolsMenu, GridFilterDrawer, applyFilters,
|
|
} from '@/components/ds/grid';
|
|
import { useTableSort } from '@/hooks/useTableSort';
|
|
import { useColumnVisibility } from '@/hooks/useColumnVisibility';
|
|
|
|
// Scoped file-integrity monitoring: baseline-and-diff over the paths
|
|
// ServerKit manages (nginx configs, serverkit-owned systemd units, and
|
|
// app docroots on explicit per-app opt-in).
|
|
|
|
const SCOPE_META = {
|
|
nginx: {
|
|
label: 'Nginx configuration',
|
|
hint: 'sites-enabled + conf.d',
|
|
},
|
|
systemd: {
|
|
label: 'Systemd units',
|
|
hint: 'serverkit-* units in /etc/systemd/system',
|
|
},
|
|
};
|
|
|
|
// Matches the service's own listing cap — a check that touched thousands of
|
|
// files reports counts, not a scrollback.
|
|
const MAX_LISTED_CHANGES = 200;
|
|
|
|
function formatAge(iso) {
|
|
if (!iso) return null;
|
|
const ms = Date.now() - new Date(iso).getTime();
|
|
if (Number.isNaN(ms)) return null;
|
|
const mins = Math.floor(ms / 60000);
|
|
if (mins < 1) return 'just now';
|
|
if (mins < 60) return `${mins}m ago`;
|
|
const hours = Math.floor(mins / 60);
|
|
if (hours < 48) return `${hours}h ago`;
|
|
return `${Math.floor(hours / 24)}d ago`;
|
|
}
|
|
|
|
function scopeLabel(scope) {
|
|
if (SCOPE_META[scope.scope]) return SCOPE_META[scope.scope].label;
|
|
return scope.app_name ? `App: ${scope.app_name}` : `App #${scope.app_id}`;
|
|
}
|
|
|
|
// A check result is three separate lists; the table is one. `tone` rides along
|
|
// so each cell keeps the colour the hand-rolled table gave it.
|
|
function toChangeRows(check) {
|
|
if (!check || !check.total_changes) return [];
|
|
return [
|
|
...(check.added || []).map((p) => ({ kind: 'added', tone: 'green', path: p, what: null })),
|
|
...(check.removed || []).map((p) => ({ kind: 'removed', tone: 'red', path: p, what: null })),
|
|
...(check.modified || []).map((m) => ({
|
|
kind: 'modified', tone: 'amber', path: m.path, what: m.what,
|
|
})),
|
|
];
|
|
}
|
|
|
|
// Cell markup/classNames identical to the hand-rolled table they replace. Two
|
|
// accessors per column on purpose: `value` is what the column menu, the filter
|
|
// rules and the export read, `sortValue` is what DataTable's sorter reads — it
|
|
// has no fallback to `value`, so a sortable column needs both.
|
|
const CHANGE_COLUMNS = [
|
|
{
|
|
key: 'change',
|
|
header: 'Change',
|
|
sortable: true,
|
|
// Declared, not inferred: a check that turned up a single change gives
|
|
// the inferrer one sample, which fails its "values repeat" test and
|
|
// types the column `text` — and then every preset that picks a kind
|
|
// matches zero rows on exactly the tables that are easiest to read.
|
|
type: 'enum',
|
|
enumOrder: ['added', 'removed', 'modified'],
|
|
value: (row) => row.kind,
|
|
sortValue: (row) => row.kind,
|
|
render: (row) => <span className={`sec-state sec-state--${row.tone}`}>{row.kind}</span>,
|
|
},
|
|
{
|
|
key: 'path',
|
|
header: 'Path',
|
|
sortable: true,
|
|
type: 'text',
|
|
value: (row) => row.path,
|
|
sortValue: (row) => row.path,
|
|
cellClassName: 'sk-cell-mono sec-path',
|
|
render: (row) => row.path,
|
|
},
|
|
{
|
|
key: 'what',
|
|
header: 'What changed',
|
|
// The service reports exactly hash | size | mode, in that order, and
|
|
// only for modified rows. Joined into one string so a rule can ask for
|
|
// "the bytes moved" (contains hash) or "only the bits moved"
|
|
// (is mode); added/removed rows carry nothing and read ''.
|
|
type: 'text',
|
|
value: (row) => (row.what || []).join(', '),
|
|
render: (row) => (
|
|
row.what ? (
|
|
<span className="sec-what">
|
|
{row.what.map((w) => (
|
|
<span key={w} className="sec-state sec-state--gray">{w}</span>
|
|
))}
|
|
</span>
|
|
) : (
|
|
<span className="sec-dash">—</span>
|
|
)
|
|
),
|
|
},
|
|
];
|
|
|
|
// Built-in saved views. A check result is a pile of diffs of three very
|
|
// different kinds, and the whole job of reading one is separating "something
|
|
// touched the box" from "a deploy chmod'd a file".
|
|
const INTEGRITY_VIEWS = [
|
|
{
|
|
// Nothing ServerKit does adds a file to sites-enabled or drops a unit
|
|
// in /etc/systemd/system without going through the panel, so a file
|
|
// appearing or vanishing is the loudest signal on this table.
|
|
name: 'Added or removed',
|
|
state: {
|
|
sorts: [{ key: 'path', direction: 'asc' }],
|
|
hiddenKeys: [],
|
|
columnFilters: {
|
|
match: 'all',
|
|
rules: [{ id: 'fi1', field: 'change', op: 'any', value: ['added', 'removed'] }],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
// The bytes moved. A size- or mode-only diff is not tampering; a
|
|
// changed sha256 on a managed config is the thing you came here for.
|
|
name: 'Content changed',
|
|
state: {
|
|
sorts: [{ key: 'path', direction: 'asc' }],
|
|
hiddenKeys: [],
|
|
columnFilters: {
|
|
match: 'all',
|
|
rules: [{ id: 'fi2', field: 'what', op: 'contains', value: 'hash' }],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
// Permission bits moved and the content did not — nearly always a
|
|
// chmod from a deploy or a hand-run command, so it triages apart from
|
|
// the content diffs. `is` rather than `contains`: a row whose hash
|
|
// moved as well belongs in the view above, not here.
|
|
name: 'Permissions only',
|
|
state: {
|
|
sorts: [{ key: 'path', direction: 'asc' }],
|
|
hiddenKeys: [],
|
|
columnFilters: {
|
|
match: 'all',
|
|
rules: [{ id: 'fi3', field: 'what', op: 'is', value: 'mode' }],
|
|
},
|
|
},
|
|
},
|
|
];
|
|
|
|
const IntegrityTab = () => {
|
|
const [status, setStatus] = useState(null);
|
|
const [apps, setApps] = useState([]);
|
|
const [busy, setBusy] = useState(null); // `${scope}:${action}` while a call runs
|
|
const [savingOptins, setSavingOptins] = useState(false);
|
|
const [message, setMessage] = useState(null);
|
|
const [expanded, setExpanded] = useState(null); // scope whose changes table is open
|
|
|
|
// Lifted out of <DataTable storageKey> so the chrome can capture and
|
|
// restore them. The keys keep DataTable's own `-sort` / `-cols` suffixes so
|
|
// a sort or a hidden column persisted by an earlier build still loads.
|
|
const { sorts, setSorts } = useTableSort({
|
|
storageKey: 'serverkit-table-integrity-changes-sort',
|
|
});
|
|
const { hiddenKeys, setHiddenKeys } = useColumnVisibility({
|
|
storageKey: 'serverkit-table-integrity-changes-cols',
|
|
});
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
const data = await api.request('/security/fim');
|
|
setStatus(data);
|
|
} catch (err) {
|
|
setMessage({ type: 'error', text: err.message });
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
api.getApps().then(
|
|
(data) => setApps(data.apps || []),
|
|
() => {} // apps list is optional chrome; FIM still works without it
|
|
);
|
|
}, [load]);
|
|
|
|
async function runAction(scope, action) {
|
|
setBusy(`${scope}:${action}`);
|
|
setMessage(null);
|
|
try {
|
|
const result = await api.request(`/security/fim/${scope}/${action}`, { method: 'POST' });
|
|
if (action === 'check') {
|
|
setExpanded(result.total_changes > 0 ? scope : null);
|
|
setMessage(result.total_changes === 0
|
|
? { type: 'success', text: `${scope}: no changes since baseline` }
|
|
: { type: 'error', text: `${scope}: ${result.total_changes} change(s) detected` });
|
|
} else {
|
|
setExpanded(null);
|
|
setMessage({
|
|
type: 'success',
|
|
text: `${scope}: baseline saved (${result.file_count} files)`,
|
|
});
|
|
}
|
|
await load();
|
|
} catch (err) {
|
|
setMessage({ type: 'error', text: err.message });
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
}
|
|
|
|
async function toggleApp(appId, enabled) {
|
|
const current = status?.app_optins || [];
|
|
const next = enabled
|
|
? [...new Set([...current, appId])]
|
|
: current.filter((id) => id !== appId);
|
|
setSavingOptins(true);
|
|
setMessage(null);
|
|
try {
|
|
await api.request('/security/fim/apps', { method: 'PUT', body: { app_ids: next } });
|
|
await load();
|
|
} catch (err) {
|
|
setMessage({ type: 'error', text: err.message });
|
|
} finally {
|
|
setSavingOptins(false);
|
|
}
|
|
}
|
|
|
|
const scopes = status?.scopes || [];
|
|
const optins = status?.app_optins || [];
|
|
|
|
// `expanded` holds ONE scope key, so at most one changes table is mounted —
|
|
// which is why this tab has a single chrome instance and passes no
|
|
// urlScope: there is no second table to fight it over ?view= / ?sort=.
|
|
const openScope = scopes.find((s) => s.scope === expanded) || null;
|
|
const changeRows = useMemo(() => toChangeRows(openScope?.last_check), [openScope]);
|
|
const shownRows = useMemo(
|
|
() => changeRows.slice(0, MAX_LISTED_CHANGES),
|
|
[changeRows],
|
|
);
|
|
|
|
// No `pageState`: a check result is whatever the last run produced, and the
|
|
// tab owns no search or filter of its own, so there is nothing
|
|
// page-private for a saved view to carry.
|
|
const chrome = useTableChrome({
|
|
columns: CHANGE_COLUMNS,
|
|
rows: shownRows,
|
|
viewPageKey: 'security-integrity',
|
|
builtinViews: INTEGRITY_VIEWS,
|
|
noun: 'changes',
|
|
sorts,
|
|
setSorts,
|
|
hiddenKeys,
|
|
setHiddenKeys,
|
|
});
|
|
|
|
// DataTable applies the column rules itself, so the tab re-runs them to say
|
|
// how many rows survived them.
|
|
const shownCount = useMemo(
|
|
() => applyFilters(shownRows, chrome.cfg.filters, chrome.columns).length,
|
|
[shownRows, chrome.cfg.filters, chrome.columns],
|
|
);
|
|
|
|
function renderScopeCard(scope) {
|
|
const key = scope.scope;
|
|
const baseline = scope.baseline;
|
|
const check = scope.last_check;
|
|
const changed = check && check.total_changes > 0;
|
|
// Same guard the old renderChanges() applied: a scope with nothing to
|
|
// show never opens, even if it is the expanded one.
|
|
const isOpen = expanded === key && changeRows.length > 0;
|
|
return (
|
|
<div className="card sec-flush" key={key}>
|
|
<div className="card-header">
|
|
<h3>
|
|
{scopeLabel(scope)}{' '}
|
|
<span className="sec-count">
|
|
· {SCOPE_META[key]?.hint || (scope.roots[0] || 'no docroot')}
|
|
</span>
|
|
</h3>
|
|
{!scope.available && <span className="sec-state sec-state--gray">not present</span>}
|
|
{scope.available && !baseline && <span className="sec-state sec-state--gray">no baseline</span>}
|
|
{baseline && !check && <span className="sec-state sec-state--cyan">baselined</span>}
|
|
{check && (changed
|
|
? <span className="sec-state sec-state--amber">{check.total_changes} changes</span>
|
|
: <span className="sec-state sec-state--green">clean</span>)}
|
|
</div>
|
|
<div className="card-body">
|
|
<p className="sec-hint--lead sec-hint">
|
|
{baseline
|
|
? `Baseline: ${baseline.file_count} files, ${formatAge(baseline.created_at) || 'unknown age'}`
|
|
: 'No baseline yet — create one to start tracking changes.'}
|
|
{check && ` · Last check ${formatAge(check.checked_at) || ''}`}
|
|
</p>
|
|
{/* These act on the SCOPE and are there whether or not the
|
|
changes list is open, so they keep their own bar; the
|
|
table's chrome rides the view line below. */}
|
|
<ListToolbar>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => runAction(key, 'baseline')}
|
|
disabled={!scope.available || busy !== null}
|
|
>
|
|
{busy === `${key}:baseline` ? 'Baselining…' : 'Baseline'}
|
|
</Button>
|
|
<Button
|
|
variant="default"
|
|
size="sm"
|
|
onClick={() => runAction(key, 'check')}
|
|
disabled={!baseline || busy !== null}
|
|
>
|
|
{busy === `${key}:check` ? 'Checking…' : 'Check now'}
|
|
</Button>
|
|
{changed && (
|
|
<>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => runAction(key, 'accept')}
|
|
disabled={busy !== null}
|
|
>
|
|
{busy === `${key}:accept` ? 'Accepting…' : 'Accept changes'}
|
|
</Button>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={() => setExpanded(expanded === key ? null : key)}
|
|
>
|
|
{expanded === key ? 'Hide changes' : 'View changes'}
|
|
</Button>
|
|
</>
|
|
)}
|
|
</ListToolbar>
|
|
{/* The view name is the changes table's heading — the card's
|
|
own h3 names the SCOPE, which is a different thing. */}
|
|
{isOpen && (
|
|
<>
|
|
<GridViewPicker
|
|
views={chrome.views}
|
|
label="changes"
|
|
onCreate={chrome.createView}
|
|
actions={(
|
|
<>
|
|
<GridFilterButton
|
|
count={chrome.filterCount}
|
|
onClick={() => chrome.setDrawerOpen(true)}
|
|
/>
|
|
<GridToolsMenu {...chrome.toolsProps} onRefresh={load} />
|
|
</>
|
|
)}
|
|
/>
|
|
<GridChips {...chrome.chipProps} />
|
|
</>
|
|
)}
|
|
</div>
|
|
{isOpen && (
|
|
<DataTable
|
|
columns={chrome.columns}
|
|
data={shownRows}
|
|
keyField={(row) => `${row.kind}:${row.path}`}
|
|
sorts={sorts}
|
|
onSortsChange={setSorts}
|
|
{...chrome.tableProps}
|
|
footer={(
|
|
<DataTableFooter
|
|
shown={shownCount}
|
|
total={changeRows.length}
|
|
noun="change"
|
|
/>
|
|
)}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const managedScopes = scopes.filter((s) => !s.scope.startsWith('app:'));
|
|
const appScopes = scopes.filter((s) => s.scope.startsWith('app:'));
|
|
|
|
return (
|
|
<div className="integrity-tab">
|
|
{message && (
|
|
<div className={`alert alert-${message.type === 'success' ? 'success' : 'danger'}`}>
|
|
{message.text}
|
|
</div>
|
|
)}
|
|
|
|
<p className="sec-hint sec-hint--lead">
|
|
Baseline-and-diff monitoring over the paths ServerKit manages. Baseline a
|
|
scope, then check it (or let the scheduled sweep do it) — any added,
|
|
removed or modified files are flagged and admins are notified. Accepting
|
|
changes re-baselines the scope.
|
|
</p>
|
|
|
|
{managedScopes.map(renderScopeCard)}
|
|
{appScopes.map(renderScopeCard)}
|
|
|
|
<div className="card">
|
|
<div className="card-header">
|
|
<h3>Application docroots <span className="sec-count">· opt-in</span></h3>
|
|
</div>
|
|
<div className="card-body">
|
|
<p className="sec-hint sec-hint--lead">
|
|
Watching a docroot hashes every file outside upload/cache
|
|
directories, so it is opt-in per application.
|
|
</p>
|
|
{apps.length === 0 ? (
|
|
<p className="sec-faint">No applications found.</p>
|
|
) : (
|
|
<div className="sec-finding-list">
|
|
{apps.map((appItem) => (
|
|
<div className="sec-finding" key={appItem.id}>
|
|
<Switch
|
|
checked={optins.includes(appItem.id)}
|
|
disabled={savingOptins}
|
|
onCheckedChange={(checked) => toggleApp(appItem.id, checked)}
|
|
/>
|
|
<div className="sec-finding__msg">
|
|
{appItem.name}{' '}
|
|
<span className="sec-mono">{appItem.root_path || 'no docroot'}</span>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<GridFilterDrawer {...chrome.drawerProps} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default IntegrityTab;
|