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 120 121 122 123 124 125 126 127 | 7x 7x 7x 7x 7x 7x 5x 2x 5x 1x 1x 1x 1x 50x 50x 4x 2x | import { NextResponse } from "next/server";
import { NEO4J_PROXY_URL } from "@/lib/proxy";
/**
* Contract and transfer audit, the hub's side (ADR-045 plane 2, #418).
*
* The neo4j-proxy keeps the hash-chained trail ("dsp" chain). The hub writes
* what only it knows: the request it is about to send to the connector, a
* transfer it refused for want of a permit, a demo record no connector saw.
* Everything after the request, the connector reports itself through the
* callback addresses below.
*
* A request is written before the connector is asked, and if it cannot be
* written the negotiation or transfer does not start (fail closed): under
* Regulation (EU) 2025/327 the access body must be able to show every data
* user's access, and an access with no record cannot be shown.
*/
export interface DspAuditRecord {
process: "contract-negotiation" | "transfer-process";
event: string;
outcome: "success" | "failure" | "refused";
processId?: string;
agreementId?: string;
assetId?: string;
datasetId?: string;
permitId?: string;
consumerId?: string;
providerId?: string;
counterPartyId?: string;
participantContext?: string;
reason?: string;
demo?: boolean;
/**
* The k6 run the request belongs to (loadTestId(req)). Sent as X-Load-Test,
* and the proxy stamps it on the record, so an auditor can tell a load
* test's contracts and transfers from real ones (#571).
*/
loadTest?: string;
}
export class AuditUnavailableError extends Error {}
/** Writes one record; rejects unless the proxy committed it (201). */
export async function recordDspEvent(record: DspAuditRecord): Promise<void> {
const token = process.env.AUDIT_CALLBACK_TOKEN;
const { loadTest, ...fields } = record;
// Validated by loadTestId() where the route read it, and again by the proxy.
const run: Record<string, string> = loadTest
? { "X-Load-Test": loadTest }
: {};
let status = 0;
try {
const res = await fetch(`${NEO4J_PROXY_URL}/audit/dsp`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(token ? { "x-audit-token": token } : {}),
...run,
},
body: JSON.stringify({ source: "hub-ui", ...fields }),
cache: "no-store",
signal: AbortSignal.timeout(5000),
});
status = res.status;
} catch (err) {
throw new AuditUnavailableError(
`audit trail unreachable: ${
err instanceof Error ? err.message : String(err)
}`,
);
}
if (status !== 201) {
throw new AuditUnavailableError(`audit trail answered ${status}`);
}
}
/**
* Writes a record whose loss must not undo what already happened (the
* connector accepted the request, or the hub already refused). A failure is
* logged; the request record written before it still stands.
*/
export async function recordDspEventAfter(
record: DspAuditRecord,
): Promise<void> {
try {
await recordDspEvent(record);
} catch (err) {
console.error(
`audit: ${record.process} ${record.event} not recorded:`,
err instanceof Error ? err.message : err,
);
}
}
/**
* The connector's callback to the audit trail, for every state change of
* the negotiation or transfer it is asked to start. Empty unless
* AUDIT_CALLBACK_URL says where the proxy is, as the connector reaches it
* (inside the container network).
*/
export function auditCallbackAddresses(
events: "contract.negotiation" | "transfer.process",
): Record<string, unknown>[] {
const uri = process.env.AUDIT_CALLBACK_URL;
if (!uri) return [];
return [
{
"@type": "CallbackAddress",
uri,
events: [events],
transactional: false,
},
];
}
export function auditUnavailableResponse(err: unknown): NextResponse {
return NextResponse.json(
{
error:
"Not started: the audit record could not be written. Every contract negotiation and data transfer must be on the audit trail (ADR-045).",
detail: err instanceof Error ? err.message : String(err),
},
{ status: 503 },
);
}
|