Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 10x 10x 5x 5x 5x 1x 10x 10x 10x 2x 2x 9x 9x 10x 4x 4x 1x 1x 3x 71x 41x 41x 3x 2x 1x 1x 1x 1x 6x 3x 3x 2x 10x 10x 10x 37x 37x 39x 10x 10x 6x | import type { SpeAuditEvent } from "@/lib/spe";
/**
* Reads an SPE event of #774 off an (:AuditEvent) of the ADR-045 chain.
*
* The proxy writes the events (WP1); #774 fixes their types and fields
* (`speId`, `permitId`, `policyHash`, `teeType`, `attestationScope`,
* `simulated`) but not where on the node they sit. This takes a field from
* the node when it is a property there, else from the FHIR AuditEvent in
* `resource`: an `entity[].detail[]` entry of that name, and the event type
* from a `subtype` code or an `eventType` detail.
*/
type Row = Record<string, unknown>;
interface FhirDetail {
type?: string;
valueString?: string;
valueBoolean?: boolean;
}
function parseResource(raw: unknown): Row | null {
Iif (raw && typeof raw === "object") return raw as Row;
if (typeof raw !== "string" || !raw) return null;
try {
const parsed: unknown = JSON.parse(raw);
return parsed && typeof parsed === "object" ? (parsed as Row) : null;
} catch {
return null;
}
}
function resourceDetails(resource: Row | null): Map<string, FhirDetail> {
const out = new Map<string, FhirDetail>();
const entities = Array.isArray(resource?.entity) ? resource.entity : [];
for (const entity of entities as Row[]) {
const details = Array.isArray(entity?.detail) ? entity.detail : [];
for (const d of details as FhirDetail[]) {
Eif (d && typeof d.type === "string" && !out.has(d.type)) {
out.set(d.type, d);
}
}
}
return out;
}
function resourceEventType(resource: Row | null): string | null {
const subtypes = Array.isArray(resource?.subtype) ? resource.subtype : [];
for (const s of subtypes as Row[]) {
Eif (typeof s?.code === "string" && s.code.startsWith("spe.")) {
return s.code;
}
}
return null;
}
function str(v: unknown): string | null {
if (typeof v === "string" && v !== "") return v;
Iif (typeof v === "number") return String(v);
return null;
}
function num(v: unknown): number | null {
if (typeof v === "number" && Number.isFinite(v)) return v;
if (typeof v === "string" && v.trim() !== "" && Number.isFinite(Number(v))) {
return Number(v);
}
// A Neo4j integer that reached JSON as { low, high }.
Eif (v && typeof v === "object" && "low" in v) {
const low = (v as { low: unknown }).low;
return typeof low === "number" ? low : null;
}
return null;
}
function bool(v: unknown): boolean | null {
if (typeof v === "boolean") return v;
Iif (v === "true") return true;
if (v === "false") return false;
return null;
}
export function normaliseSpeEvent(row: Row): SpeAuditEvent | null {
const resource = parseResource(row.resource);
const details = resourceDetails(resource);
const detail = (name: string): unknown => {
const d = details.get(name);
return d?.valueBoolean ?? d?.valueString;
};
const field = (name: string): unknown => row[name] ?? detail(name);
const eventType =
str(row.eventType) ??
str(detail("eventType")) ??
resourceEventType(resource);
if (!eventType || !eventType.startsWith("spe.")) return null;
return {
id: str(row.id) ?? str(resource?.id) ?? `${eventType}-${str(row.recorded)}`,
eventType,
recorded: str(row.recorded) ?? str(resource?.recorded),
speId: str(field("speId")),
permitId: str(field("permitId")),
policyHash: str(field("policyHash")),
teeType: str(field("teeType")),
attestationScope: str(field("attestationScope")),
simulated: bool(field("simulated")),
outcome: str(row.outcome) ?? str(detail("outcome")),
optOut:
eventType === "spe.optout_synced"
? {
added: num(field("optedOutAdded")),
removed: num(field("optedOutRemoved")),
inHub: num(field("optedOutInHub")),
}
: null,
};
}
|