Plan an exact service-account OPNsense kernel probe through normal approval
This commit is contained in:
@@ -18,6 +18,7 @@ import { CatalogCheckNotice } from './catalog-check';
|
||||
import { devicePlatform } from './script-catalog';
|
||||
import { DeviceInventoryPanels, systemDetails } from './device-inventory';
|
||||
import { OPNsenseDetails } from './opnsense';
|
||||
import { OPNsenseProbe } from './opnsense-probe';
|
||||
import { DeviceCustomerTransfer } from './device-customer-transfer';
|
||||
import { HypervisorAssignment, HypervisorLabel } from './device-hypervisor';
|
||||
import { ErrorBox, errorText, useResource } from './resource';
|
||||
@@ -302,6 +303,7 @@ function DeviceDetail({
|
||||
onSaved={() => { setPurposeSaved(true); detail.reload(); }}
|
||||
/>
|
||||
<OPNsenseDetails device={d} />
|
||||
{scoped && <OPNsenseProbe key={`${customer}/${asset}/${agent}`} device={d} auth={scoped}/>}
|
||||
<DeviceInventoryPanels device={d} />
|
||||
{volumes.length > 0 && (
|
||||
<section>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { useState } from 'react';
|
||||
import { objectId, record, request, tenantPath, type RecordValue, type Session } from './api';
|
||||
import { allowed } from './safety';
|
||||
import { verifiedScriptOrder } from './order-composer';
|
||||
import { ErrorBox, errorText } from './resource';
|
||||
|
||||
export function OPNsenseProbe({ device, auth }: {device: RecordValue; auth: Session}) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [ticket, setTicket] = useState<{id:string; hash:string} | null>(null);
|
||||
const [operation, setOperation] = useState<string | null>(null);
|
||||
if (device.source_id !== 'opnsense.base' || device.lifecycle !== 'current' ||
|
||||
!Array.isArray(device.capabilities) || !device.capabilities.includes('opnsense.uname/1') ||
|
||||
!allowed(auth,'operations:plan') || !allowed(auth,'scripts:execute')) return null;
|
||||
const customer = objectId(device,'tenant'), asset = objectId(device,'asset'), agent = objectId(device,'agent');
|
||||
const path = tenantPath(customer,'/operation-plans');
|
||||
async function submit() {
|
||||
setBusy(true); setError(null);
|
||||
try {
|
||||
let pending = ticket;
|
||||
if (!pending) {
|
||||
const now = Date.now(), start = new Date(now).toISOString(), end = new Date(now+86400000).toISOString();
|
||||
const reason = 'OPNsense: Kernelname auf dem Dienstkonto prüfen';
|
||||
const raw = await request(path, {csrf:auth.csrf_token,body:{tenant_id:customer,action:'run_script',
|
||||
targets:{asset_ids:[asset]},parameters:{execution_identity:'agent_service',timeout_seconds:10,
|
||||
max_output_bytes:1024,arguments:[],expected_stdout:'FreeBSD\n',
|
||||
variants:[{os_family:'opnsense',interpreter:'posix_sh',encoding:'utf-8',script_utf8:'uname -s\n'}]},
|
||||
schedule:{not_before:start,start_before:end,offline_policy:'wait_until_deadline'},
|
||||
rollout:{max_parallel:1,halt_after_failures:1},reason}});
|
||||
const checked = await verifiedScriptOrder(raw,customer,auth.principal.id), j = checked.jobs[0];
|
||||
if (!j || checked.jobs.length!==1 || j.assetId!==asset || j.agentId!==agent || j.identity!=='agent_service' ||
|
||||
j.script!=='uname -s\n' || j.expectedStdout!=='FreeBSD\n' || j.interpreter!=='posix_sh' ||
|
||||
j.arguments.length || j.runtimeSeconds!==10 || j.outputBytes!==1024 || checked.reason!==reason ||
|
||||
Date.parse(j.notBefore)!==now || Date.parse(j.startBefore)!==Date.parse(end) ||
|
||||
checked.maxParallel!==1 || checked.halt!==1) throw Error('Plan weicht von der Kernelabfrage ab.');
|
||||
pending = {id:objectId(checked.plan,'plan'),hash:String(checked.plan.plan_hash)};
|
||||
setTicket(pending);
|
||||
}
|
||||
const r = record(await request(path+'/'+pending.id+'/submit',{csrf:auth.csrf_token,body:{idempotency_key:pending.id}}));
|
||||
if (r.plan_id!==pending.id || r.plan_hash!==pending.hash) throw Error('Planbindung stimmt nicht.');
|
||||
setOperation(objectId(r,'operation'));
|
||||
} catch (e) { setError(errorText(e)); } finally { setBusy(false); }
|
||||
}
|
||||
return <section><h2>OPNsense-Kernelabfrage</h2><p><code>uname -s</code> als Dienstkonto · ein Versuch · 10 Sekunden · 1 KiB Ausgabe.</p>
|
||||
{operation && ticket ? <p>Auftrag eingereicht. <a href={`#genehmigungen/pruefen/${customer}/${ticket.id}`}>Zur Genehmigung</a></p>
|
||||
: <button disabled={busy} onClick={submit}>Kernelabfrage zur Genehmigung einreichen</button>}
|
||||
<ErrorBox error={error}/></section>;
|
||||
}
|
||||
@@ -81,7 +81,7 @@ function resumeTicket(key: string): Ticket | null {
|
||||
}
|
||||
return data;
|
||||
}
|
||||
async function verifiedScriptOrder(value: unknown, tenantId: string, principal: string) {
|
||||
export async function verifiedScriptOrder(value: unknown, tenantId: string, principal: string) {
|
||||
const plan = record(value, 'Auftragsplan');
|
||||
await verifyPlanIntegrity(plan);
|
||||
await verifyScriptPlan(plan);
|
||||
|
||||
Reference in New Issue
Block a user