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 { 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(undefined); const [error, setError] = useState(''), [busy, setBusy] = useState(false); const [reviewed, setReviewed] = useState(false), [revision, setRevision] = useState(0); const activeSubmit = useRef(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

Windows-Journalwiederherstellung

{error &&

{error}

} {row === undefined && !error &&

Prüfantrag wird geladen …

} {row === null &&

Für diese Testinstallation liegt noch kein Prüfantrag vor.

} {row === null && allowed(auth, 'operations:submit') && } {row && <>

Antrag {row.id} · Windows-Verbindung {String(row.witness.connection_generation)}

Gültiger historischer Serverstand {String(row.witness.server_head_checkpoint)}. Agent und Helper haben die signierte Ablehnung {String(row.witness.local_head_checkpoint)} gespeichert.

Beleg-SHA-256 {row.hash}

Öffentliche Server- und Gastbelege
{pretty(row.witness)}
{confirmed &&

Bestätigt durch {confirmed.by} am {confirmed.at}. Quittung gültig bis {confirmed.until}.

} {eligible && confirmed && !expired && Signierte Quittung herunterladen} {(!confirmed || expired) && (eligible ? <>

{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.

:

Die Bestätigung erfordert eine persönliche OIDC-Sitzung mit der Berechtigung recovery:confirm.

)} }
; }