Files
tessera/web/src/components/vault/SnapshotSheet.tsx
Adnan Zahir d12472ba02 feat: per-orbit access grants and an Access console (auth M5)
Authorization
- one gate for every human endpoint: authenticate, resolve the resource, then
  admin_only / scoped / visible; require_admin* removed so each handler must choose
- grants (user or IdP group; global or one orbit; optional end date) read live on
  every request; only orbit.read, snapshot.read, capture.run and snapshot.download
  can be granted, everything else stays with configured administrators
- an orbit without orbit.read is 404, a visible one without the permission is 403,
  a caller with nothing to see gets 403 on lists; job history is filtered before LIMIT
- jobs and audit record the stable principal id, not the IdP display name
- restore targets and restore history are admin-only until self-service (M6)
- sessions keep the groups verified at sign-in

Administration
- /api/v1/access grants and users (enable, disable, sign out everywhere), validated
  and audited by id; admins cannot disable themselves or the emergency operator
- Access console with the shared Select and a themed DatePicker; non-administrators
  only see Vault and Activity and never call administrator endpoints
- the Window/Radar view is stored per account (/api/v1/me/preferences)
- UI tolerates older hubs and mocks (missing admin/grants) and a console that fails
  to render no longer blanks the app

Tests: a matrix generated from the endpoint contract exercises every human route
against real resources in two orbits for each kind of caller.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 14:45:41 +07:00

136 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { deleteSnapshot, type Snapshot } from "../../api";
import { formatBytes, formatWhen } from "../../lib/format";
import { Button } from "../ui/Button";
import { TelemetryPanel } from "../ui/Telemetry";
/** Moon spots along the little orbit in the hero, nearest the hole last. */
const moons = [[126, 36], [104, 20], [52, 89], [20, 62]] as const;
function signedBytes(delta: number) {
return `${delta < 0 ? "−" : "+"}${formatBytes(Math.abs(delta))}`;
}
export function SnapshotSheet({
snap,
siblings = [],
onReconstitute,
onDeleted,
canManage = false,
canDownload = false,
}: {
snap: Snapshot;
/** Every snapshot of the same orbit, newest first, including this one. */
siblings?: Snapshot[];
onReconstitute: () => void;
onDeleted: () => void;
/** Administrators only: restore and delete. */
canManage?: boolean;
canDownload?: boolean;
}) {
const [confirmDelete, setConfirmDelete] = useState(false);
const [copied, setCopied] = useState(false);
const qc = useQueryClient();
const deletion = useMutation({
mutationFn: () => deleteSnapshot(snap.id),
onSuccess: () => {
void qc.invalidateQueries({ queryKey: ["snapshots"] });
onDeleted();
},
});
const index = siblings.findIndex(({ id }) => id === snap.id);
const previous = index >= 0 ? siblings[index + 1] : undefined;
const shown = Math.min(siblings.length, moons.length);
const lit = Math.min(Math.max(index, 0), moons.length - 1);
const database = !!snap.capture?.restore_ready;
const taken = new Date(snap.taken_at);
async function copy() {
try {
await navigator.clipboard.writeText(snap.id);
setCopied(true);
window.setTimeout(() => setCopied(false), 1600);
} catch {
// Clipboard access can be blocked; the id stays selectable on the page.
}
}
return (
<div className="sheet">
<div className="sheet-hero">
<svg viewBox="0 0 150 110" width="150" height="110" role="img"
aria-label={index >= 0 ? `Snapshot ${index + 1} of ${siblings.length} on its orbit` : "Snapshot on its orbit"}>
<ellipse cx="75" cy="55" rx="64" ry="38" fill="none" stroke="#dfe4ef" strokeOpacity=".14" />
<circle cx="75" cy="55" r="7" fill="#000" stroke="#ffe5c8" strokeOpacity=".7" />
{moons.slice(0, Math.max(shown, 1)).map(([x, y], i) => (
<path key={i} d={`M${x} ${y - 4}l4 4-4 4-4-4z`} fill={i === lit ? "#f1bc9033" : "none"}
stroke={i === lit ? "#f1bc90" : "#dfe4ef"} strokeOpacity={i === lit ? 1 : Math.max(0.25, 0.6 - i * 0.12)} />
))}
</svg>
<div className="min-w-0">
<p className="field-label">Recovery point for</p>
<p className="sheet-name">{snap.orbit_name}</p>
{index === 0 && (
<p className="sheet-note"><span className="register-signal signal-green" aria-hidden="true" />Newest snapshot of this orbit</p>
)}
</div>
</div>
<dl className="sheet-facts">
<div>
<dt>Captured</dt>
<dd>{formatWhen(snap.taken_at)}</dd>
<dd className="sheet-sub">{taken.toISOString().slice(0, 16).replace("T", " ")} UTC</dd>
</div>
<div>
<dt>Payload</dt>
<dd className="sheet-mono">{formatBytes(snap.size_bytes)}</dd>
{previous && <dd className="sheet-sub">{signedBytes(snap.size_bytes - previous.size_bytes)} vs previous</dd>}
</div>
<div>
<dt>Recovery</dt>
<dd>{database ? "Database ready" : "File export"}</dd>
{snap.capture && <dd className="sheet-sub">{snap.capture.engine} · {snap.capture.format}</dd>}
</div>
</dl>
<div className="gate-cmd">
<div className="gate-cmd-head">
<span>Snapshot ID</span>
<button type="button" className="gate-copy" onClick={() => void copy()}>{copied ? "COPIED" : "COPY"}</button>
</div>
<code className="sheet-id">{snap.id}</code>
</div>
<div className="sheet-actions">
{canManage && <Button variant="primary" className="sheet-primary" onClick={onReconstitute}>
{database ? "Database recovery options" : "Export file on hub"}
<span aria-hidden="true">→</span>
</Button>}
{canDownload && <a href={`/api/v1/snapshots/${snap.id}/download`} download className="console-control sheet-link">
Download snapshot
<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M8 2v8M4.5 7 8 10.5 11.5 7M3 13.5h10" />
</svg>
</a>}
{canManage && (confirmDelete ? (
<div className="sheet-confirm" role="alertdialog" aria-label="Confirm deletion">
<p>Remove this recovery point from the hub catalogue? This cannot be undone.</p>
{deletion.isError && <p role="alert" className="text-orbit-red">Snapshot could not be removed. It may be in use.</p>}
<div className="flex gap-2.5">
<Button className="flex-1" disabled={deletion.isPending} onClick={() => setConfirmDelete(false)}>Cancel</Button>
<Button variant="danger" className="flex-1" disabled={deletion.isPending} onClick={() => deletion.mutate()}>
Confirm deletion
</Button>
</div>
</div>
) : (
<button type="button" className="sheet-delete" onClick={() => setConfirmDelete(true)}>Delete snapshot</button>
))}
</div>
<TelemetryPanel label="Snapshot record">{JSON.stringify(snap, null, 2)}</TelemetryPanel>
</div>
);
}