All files / src/app/api/admin/spe route.ts

94.44% Statements 68/72
86.44% Branches 51/59
95% Functions 19/20
100% Lines 62/62

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256                                            2x   2x     2x         2x           8x 8x 8x                   15x                                                   11x 11x                     11x 11x                                       18x 18x 36x   18x 18x 18x   24x                                 2x   2x 8x 2x 8x       2x 2x 3x 3x   2x                   18x                     18x 18x   18x 18x 17x 17x 3x 2x                     1x   18x 18x                           20x 20x 18x     18x               20x 20x 18x 36x   18x     20x 20x         17x 8x 8x 7x         1x 9x 8x   1x     1x     11x          
import { NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { isAzureDeployment } from "@/lib/azure-arm";
import { runQuery } from "@/lib/neo4j";
import { userToParticipantId } from "@/lib/odrl-engine";
import { NEO4J_PROXY_URL, callerHeaders } from "@/lib/proxy";
import {
  SPE_LABEL_SIMULATED,
  SPE_POWER_REASONS,
  spePowerState,
  type SpeArmPower,
  type SpeEntry,
  type SpePermitRecord,
  type SpeStatusResponse,
} from "@/lib/spe";
import {
  SPE_IDS,
  SPE_POWER_ROLES,
  readArmPower,
  spePowerAvailable,
} from "@/lib/spe-power";
 
export const dynamic = "force-dynamic";
 
const PROXY_TIMEOUT_MS = 5_000;
 
/** The holders of the two demo SPEs (#774): fictional, as everywhere. */
const SPE_HOLDERS: Record<string, string> = {
  "SPE-1": "AlphaKlinik Berlin",
  "SPE-2": "Limburg Medical Centre",
};
 
const SPE_RUNS_ON: Record<string, string> = {
  "SPE-1": "Hub graph Neo4j, an ordinary container",
  "SPE-2": "Second Neo4j (NEO4J_SPE2_URI), an ordinary container",
};
 
function isSpeStatusResponse(v: unknown): v is SpeStatusResponse {
  Iif (!v || typeof v !== "object") return false;
  const o = v as Record<string, unknown>;
  return typeof o.environment === "string" && Array.isArray(o.spes);
}
 
/**
 * An SPE as far as the UI knows without the proxy's `/spe/status`: no
 * attestation, no key release, no permit, no patient count, labelled
 * "Simulated attestation", status "unreachable" (unknown). Nothing here may
 * claim more than the proxy said.
 */
function unattested(id: string): SpeEntry {
  return {
    id,
    holder: SPE_HOLDERS[id] ?? "Unknown holder",
    runsOn: SPE_RUNS_ON[id] ?? "Unknown",
    teeType: "none",
    attestationScope: "none",
    simulated: true,
    label: SPE_LABEL_SIMULATED,
    attestedAt: null,
    keyReleasedAt: null,
    permitId: null,
    policyHash: null,
    policyCheckedByHardware: false,
    patients: null,
    status: "unreachable",
    attestation: null,
  };
}
 
/**
 * True when the deployment says a second SPE exists. `NEO4J_SPE2_CONFIGURED`
 * ("true" or "1") is set on the UI where the proxy has `NEO4J_SPE2_URI`; the
 * UI never sees the proxy's environment, and asking the proxy would cost a
 * query.
 */
function spe2Configured(): boolean {
  const v = (process.env.NEO4J_SPE2_CONFIGURED ?? "").trim().toLowerCase();
  return v === "true" || v === "1";
}
 
/**
 * Without the proxy's `/spe/status` (#774 WP1 not deployed, or the proxy
 * down): SPE-1 always, SPE-2 only when `NEO4J_SPE2_CONFIGURED` says it
 * exists. It asks the proxy nothing else: `/federated/stats` would run the
 * statistics on every SPE and record a TransferEvent, so every visit to an
 * admin page would write access-log rows.
 */
function fallbackStatus(reason: string): SpeStatusResponse {
  const ids = spe2Configured() ? ["SPE-1", "SPE-2"] : ["SPE-1"];
  return {
    environment: isAzureDeployment() ? "azure" : "local",
    trustee: null,
    spes: ids.map(unattested),
    residualTrust: [],
    fallback: { reason },
  };
}
 
/**
 * Each SPE's power state (ADR-072): ARM's view of its ACI group on Azure,
 * combined with the proxy's read of the workspace. A group ARM reports and
 * the proxy did not list (SPE-2 in the fallback without
 * NEO4J_SPE2_CONFIGURED) is added, so a stopped workspace still shows.
 */
function withPower(
  status: SpeStatusResponse,
  arm: Record<string, SpeArmPower | null>,
  roles: string[],
): SpeStatusResponse {
  const spes = [...status.spes];
  for (const id of SPE_IDS) {
    Iif (arm[id] && !spes.some((s) => s.id === id)) spes.push(unattested(id));
  }
  const available = spePowerAvailable();
  const allowed = roles.some((r) => (SPE_POWER_ROLES as string[]).includes(r));
  return {
    ...status,
    spes: spes.map((spe) => ({
      ...spe,
      powerState: spePowerState(arm[spe.id] ?? null, spe.status),
      armState: arm[spe.id]?.state ?? null,
      lastQueriedAt: spe.lastQueriedAt ?? null,
    })),
    power: {
      available: available && allowed,
      reason: !available
        ? SPE_POWER_REASONS.notAzure
        : allowed
          ? null
          : SPE_POWER_REASONS.notAllowed,
    },
  };
}
 
const PERMIT_TIMEOUT_MS = 3_000;
 
const strOrNull = (v: unknown): string | null =>
  typeof v === "string" && v !== "" ? v : null;
const strings = (v: unknown): string[] =>
  Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
 
/** "SPE-1=sha256:..." entries, as `spe-loader record-permit` writes them. */
function hashBySpe(v: unknown): Record<string, string> {
  const out: Record<string, string> = {};
  for (const entry of strings(v)) {
    const at = entry.indexOf("=");
    if (at > 0) out[entry.slice(0, at)] = entry.slice(at + 1);
  }
  return out;
}
 
/**
 * The data permits the hub holds a policy hash or Trustee resource for
 * (#833), so the policies page shows a stopped workspace's permit too. Best
 * effort: an unreachable hub, or one slower than three seconds, gives none,
 * and the page shows what the workspaces reported.
 */
async function readPermits(): Promise<SpePermitRecord[]> {
  const query = runQuery<Record<string, unknown>>(
    `MATCH (a:HDABApproval)
     WHERE a.policyHash IS NOT NULL OR size(coalesce(a.trusteeResources, [])) > 0
     RETURN a.approvalId AS permitId, a.name AS name, a.status AS status,
            a.policyHash AS policyHash, a.policyHashBySpe AS policyHashBySpe,
            coalesce(a.speWorkspaces, []) AS speWorkspaces,
            coalesce(a.trusteeResources, []) AS trusteeResources,
            toString(a.policyHashRecordedAt) AS recordedAt
     ORDER BY permitId`,
  );
  let timer: ReturnType<typeof setTimeout> | undefined;
  const timeout = new Promise<null>((resolve) => {
    timer = setTimeout(() => resolve(null), PERMIT_TIMEOUT_MS);
  });
  try {
    const rows = await Promise.race([query, timeout]);
    Iif (!rows) return [];
    return rows
      .filter((r) => typeof r.permitId === "string")
      .map((r) => ({
        permitId: r.permitId as string,
        name: strOrNull(r.name),
        status: strOrNull(r.status),
        policyHash: strOrNull(r.policyHash),
        policyHashBySpe: hashBySpe(r.policyHashBySpe),
        speWorkspaces: strings(r.speWorkspaces),
        trusteeResources: strings(r.trusteeResources),
        recordedAt: strOrNull(r.recordedAt),
      }));
  } catch {
    return [];
  } finally {
    clearTimeout(timer);
    query.catch(() => undefined);
  }
}
 
/**
 * GET /api/admin/spe: the confidential SPEs of ADR-068 (#774), for the
 * operator and the access body. Passes the proxy's `/spe/status` through;
 * when the proxy does not have it yet or cannot be reached, answers with the
 * fallback above, labelled "Simulated attestation", and the reason. Each SPE
 * carries its power state (ADR-072). A workspace ARM reports stopped is
 * named in `skip`, so the proxy does not wait for it. `permits` are the data
 * permits the hub records a policy hash for (#833).
 */
export async function GET(): Promise<NextResponse> {
  const auth = await requireAuth(["EDC_ADMIN", "HDAB_AUTHORITY"]);
  if (isAuthError(auth)) return auth;
  const { session } = auth;
 
  // Names who asked, should the proxy record the status call (issue #205).
  const headers: Record<string, string> = {
    "X-Participant": userToParticipantId(
      session.user.email ?? session.user.name ?? session.user.id,
      session.roles,
    ),
    ...callerHeaders(session),
  };
 
  const permitsRead = readPermits();
  const arm = await readArmPower();
  const stopped = SPE_IDS.filter(
    (id) => arm[id] && spePowerState(arm[id], undefined) === "stopped",
  );
  const query = stopped.length > 0 ? `?skip=${stopped.join(",")}` : "";
 
  let reason: string;
  try {
    const resp = await fetch(`${NEO4J_PROXY_URL}/spe/status${query}`, {
      headers,
      cache: "no-store",
      signal: AbortSignal.timeout(PROXY_TIMEOUT_MS),
    });
    if (resp.ok) {
      const data: unknown = await resp.json();
      if (isSpeStatusResponse(data)) {
        return NextResponse.json({
          ...withPower(data, arm, session.roles),
          permits: await permitsRead,
        });
      }
      reason = "The query proxy's /spe/status answered in an unknown shape";
    } else if (resp.status === 404) {
      reason = "The query proxy has no /spe/status yet";
    } else {
      reason = `The query proxy's /spe/status answered ${resp.status}`;
    }
  } catch {
    reason = "The query proxy is unreachable";
  }
 
  return NextResponse.json({
    ...withPower(fallbackStatus(reason), arm, session.roles),
    permits: await permitsRead,
  });
}