Add the independently reviewed Windows recovery interface
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { canonicalJson, pretty, record, RecordValue, request, Session, tenantPath } from './api';
|
||||
import { allowed, sha256 } from './safety';
|
||||
|
||||
export const recoveryTenant = '11111111-1111-4111-8111-111111111111';
|
||||
export const recoveryAsset = 'a176520e-8a91-4e93-a78b-8e7620f430e0';
|
||||
const agent = '63ea15b2-0643-403d-b5b4-cba1288dbea3';
|
||||
// Canonical SHA-256 of the single historical case in rmm_domain::windows_sync_history.
|
||||
const historicalWitnessHash = 'f29b5afadaf2debab9610282725c2493cb7405784640a4616e1998c304309cab';
|
||||
const path = tenantPath(recoveryTenant, '/windows-checkpoint-recovery');
|
||||
const humanOidc = /^oidc:[0-9a-f]{64}$/;
|
||||
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
|
||||
interface Review {
|
||||
id: string;
|
||||
hash: string;
|
||||
witness: RecordValue;
|
||||
confirmed: { by: string; at: string; receipt: string; until: string } | null;
|
||||
}
|
||||
|
||||
export async function readReview(value: unknown): Promise<Review | null> {
|
||||
const row = record(value).request;
|
||||
if (row === null) return null;
|
||||
const r = record(row), w = record(r.witness, 'Wiederherstellungsbeleg');
|
||||
const id = r.request_id, hash = r.witness_sha256;
|
||||
if (typeof id !== 'string' || !uuid.test(id) || typeof hash !== 'string' || hash !== historicalWitnessHash ||
|
||||
r.tenant_id !== recoveryTenant || w.tenant_id !== r.tenant_id || r.asset_id !== recoveryAsset ||
|
||||
w.asset_id !== r.asset_id || r.agent_id !== agent || w.agent_id !== agent ||
|
||||
w.schema_version !== 1 || w.connection_generation !== 150 ||
|
||||
w.server_head_checkpoint !== 6755 || w.local_head_checkpoint !== 6756 ||
|
||||
w.server_head_decision_id !== 'b6a3101f-68ad-41c8-be4e-4c7c2bc963d6' ||
|
||||
w.local_head_decision_id !== '0633a4de-7e52-4e5d-a2a1-59fb134817fe' ||
|
||||
await sha256(new TextEncoder().encode(canonicalJson(w))) !== hash) {
|
||||
throw new Error('Gerätebindung, Journalstände oder SHA-256 des gespeicherten Belegs stimmen nicht überein.');
|
||||
}
|
||||
if (r.status === 'pending_review' && r.confirmed_by === null && r.confirmed_at === null &&
|
||||
r.receipt_id === null && r.receipt_valid_until === null) return { id, hash, witness: w, confirmed: null };
|
||||
if (r.status !== 'confirmed' || typeof r.confirmed_by !== 'string' || !humanOidc.test(r.confirmed_by) ||
|
||||
typeof r.confirmed_at !== 'string' || !Number.isFinite(Date.parse(r.confirmed_at)) ||
|
||||
typeof r.receipt_id !== 'string' || !uuid.test(r.receipt_id) ||
|
||||
typeof r.receipt_valid_until !== 'string' || !Number.isFinite(Date.parse(r.receipt_valid_until)) ||
|
||||
Date.parse(r.receipt_valid_until) <= Date.parse(r.confirmed_at)) {
|
||||
throw new Error('Unvollständiger Bestätigungsstand.');
|
||||
}
|
||||
return { id, hash, witness: w, confirmed: { by: r.confirmed_by, at: r.confirmed_at,
|
||||
receipt: r.receipt_id, until: r.receipt_valid_until } };
|
||||
}
|
||||
|
||||
export function mayConfirmRecovery(auth: Session): boolean {
|
||||
return auth.principal.simulated === false && humanOidc.test(auth.principal.id) &&
|
||||
auth.tenants.some(t => t.tenant_id === recoveryTenant) && !!auth.csrf_token && allowed(auth, 'recovery:confirm');
|
||||
}
|
||||
|
||||
export function RecoveryReview({ auth }: { auth: Session }) {
|
||||
const [row, setRow] = useState<Review | null | undefined>(undefined);
|
||||
const [error, setError] = useState(''), [busy, setBusy] = useState(false);
|
||||
const [reviewed, setReviewed] = useState(false), [revision, setRevision] = useState(0);
|
||||
const activeSubmit = useRef<AbortController | null>(null);
|
||||
const [elapsedReceipt, setElapsedReceipt] = useState('');
|
||||
useEffect(() => () => activeSubmit.current?.abort(), []);
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
setRow(undefined); setReviewed(false); setError('');
|
||||
request(path, { signal: controller.signal }).then(readReview).then(response => {
|
||||
if (!controller.signal.aborted) setRow(response);
|
||||
}).catch(e => { if (!controller.signal.aborted) setError(String(e)); });
|
||||
return () => controller.abort();
|
||||
}, [revision]);
|
||||
|
||||
const eligible = mayConfirmRecovery(auth);
|
||||
const confirmed = row?.confirmed;
|
||||
const expired = !!confirmed && (confirmed.receipt === elapsedReceipt || Date.parse(confirmed.until) <= Date.now());
|
||||
useEffect(() => {
|
||||
if (!confirmed) return;
|
||||
const timer = window.setTimeout(() => {
|
||||
setElapsedReceipt(confirmed.receipt);
|
||||
setReviewed(false);
|
||||
}, Math.max(0, Date.parse(confirmed.until) - Date.now()));
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [confirmed]);
|
||||
async function submit() {
|
||||
if (activeSubmit.current || row === undefined) return;
|
||||
if (row && (!eligible || !reviewed || (row.confirmed && !expired))) return;
|
||||
if (!row && !allowed(auth, 'operations:submit')) return;
|
||||
const controller = new AbortController();
|
||||
activeSubmit.current = controller;
|
||||
setBusy(true); setReviewed(false); setError('');
|
||||
try {
|
||||
const url = row ? path + '/' + row.id + '/confirm' : path;
|
||||
const body = row ? { witness_sha256: row.hash, reviewed: true,
|
||||
...(row.confirmed ? { previous_receipt_id: row.confirmed.receipt } : {}) } : {};
|
||||
const reply = await readReview(await request(url, { body, csrf: auth.csrf_token, signal: controller.signal }));
|
||||
if (!reply || (row && (reply.id !== row.id || reply.hash !== row.hash || !reply.confirmed))) {
|
||||
throw new Error('Antwort passt nicht zum geprüften Antrag. Bitte den gespeicherten Stand abrufen.');
|
||||
}
|
||||
if (!controller.signal.aborted) setRow(reply);
|
||||
} catch (e) {
|
||||
if (!controller.signal.aborted) { setRow(undefined); setError(String(e)); }
|
||||
} finally {
|
||||
activeSubmit.current = null;
|
||||
if (!controller.signal.aborted) setBusy(false);
|
||||
}
|
||||
}
|
||||
return <section aria-label="Windows-Journalwiederherstellung">
|
||||
<h3>Windows-Journalwiederherstellung</h3>
|
||||
{error && <p role="alert">{error}</p>}
|
||||
{row === undefined && !error && <p>Prüfantrag wird geladen …</p>}
|
||||
{row === null && <p>Für diese Testinstallation liegt noch kein Prüfantrag vor.</p>}
|
||||
{row === null && allowed(auth, 'operations:submit') &&
|
||||
<button disabled={busy} onClick={submit}>Prüfantrag aus gespeicherten Belegen anlegen</button>}
|
||||
{row && <>
|
||||
<p>Antrag <code>{row.id}</code> · Windows-Verbindung {String(row.witness.connection_generation)}</p>
|
||||
<p>Gültiger historischer Serverstand <strong>{String(row.witness.server_head_checkpoint)}</strong>.
|
||||
Agent und Helper haben die signierte Ablehnung <strong>{String(row.witness.local_head_checkpoint)}</strong> gespeichert.</p>
|
||||
<p>Beleg-SHA-256 <code>{row.hash}</code></p>
|
||||
<details open><summary>Öffentliche Server- und Gastbelege</summary><pre>{pretty(row.witness)}</pre></details>
|
||||
{confirmed && <p>Bestätigt durch {confirmed.by} am {confirmed.at}. Quittung gültig bis {confirmed.until}.</p>}
|
||||
{eligible && confirmed && !expired &&
|
||||
<a href={path + '/' + row.id + '/receipt'} download="windows-checkpoint-recovery.json">Signierte Quittung herunterladen</a>}
|
||||
{(!confirmed || expired) && (eligible ? <>
|
||||
<p>{confirmed ? 'Die erneute Prüfung stellt eine neue befristete Quittung aus.' :
|
||||
'Mit der Bestätigung übernimmt der Server die bereits signierte Ablehnung 6756 als Kontrollstand und stellt eine befristete Quittung für den Gast aus.'}
|
||||
{' '}Vor einem späteren Job sind der reguläre signierte Abgleich und die gesonderte Plangenehmigung erforderlich.</p>
|
||||
<label><input type="checkbox" checked={reviewed} disabled={busy}
|
||||
onChange={e => setReviewed(e.currentTarget.checked)}/> Ich habe beide Journalstände, die Gerätebindung und die öffentlichen Belege geprüft.</label>
|
||||
<button disabled={busy || !reviewed} onClick={submit}>{confirmed ? 'Neue Quittung ausdrücklich bestätigen' : 'Übernahme des dokumentierten Kontrollstands bestätigen'}</button>
|
||||
</> : <p>Die Bestätigung erfordert eine persönliche OIDC-Sitzung mit der Berechtigung <code>recovery:confirm</code>.</p>)}
|
||||
</>}
|
||||
<button disabled={busy || (row === undefined && !error)} onClick={() => setRevision(n => n + 1)}>Gespeicherten Stand aktualisieren</button>
|
||||
</section>;
|
||||
}
|
||||
Reference in New Issue
Block a user