157 lines
18 KiB
TypeScript
157 lines
18 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import { basename, dirname, resolve } from "node:path"
|
|
import type { QueryConfig } from "pg"
|
|
import type { ServerPollJournalContext } from "./server-poll-journal.js"
|
|
import type { ServerPollReceipt } from "./server-poll-execution.js"
|
|
import type { ProbeJournalConnections } from "./server-probe-journal.js"
|
|
|
|
/** Explicitly called by the disposable runner only. No runtime import, SQL,
|
|
* migration, native request, deletion or registry mutation occurs on import. */
|
|
export async function acceptServerPollJournalSql(){
|
|
assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL,"true")
|
|
const url=new URL(process.env.DATABASE_URL??"")
|
|
assert.equal(url.hostname,"127.0.0.1");assert.equal(url.pathname,"/postgres")
|
|
assert(["postgres:","postgresql:"].includes(url.protocol))
|
|
assert(process.env.MCP_STATE_DIR&&basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-"))
|
|
const {pool}=await import("../db/index.js")
|
|
const security=await import("../modules/mcp/security.js")
|
|
const {createSqlServerPollJournal,SERVER_POLL_JOURNAL_DDL,ServerPollJournalConflict,ServerPollJournalOutcomeUnknown}=await import("./server-poll-journal.js")
|
|
assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name,"postgres")
|
|
assert.equal((await pool.query("SELECT to_regclass('public.server_poll_executions') AS name")).rows[0].name,null,"Poll journal proposal must not already be installed")
|
|
const previousMcpEnabled=(await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean
|
|
const ids:number[]=[],proof=(value:unknown)=>security.privateSecretFingerprint(security.fingerprint(value),"server-poll-production-v1")
|
|
const statements:string[]=[],releases:boolean[]=[]
|
|
const handledClients=new WeakSet<object>()
|
|
let cases=0,loseCommit=false,lostCommits=0
|
|
const connections:ProbeJournalConnections={async connect(){
|
|
const client=await pool.connect();if(!handledClients.has(client)){handledClients.add(client);client.on("error",()=>{})}
|
|
return {query:async(text,values)=>{statements.push(text);const query:QueryConfig&{query_timeout:number}={text,values,query_timeout:5000};const result=await client.query<Record<string,unknown>>(query);if(text==="COMMIT"&&loseCommit){loseCommit=false;lostCommits++;throw Error("Synthetic lost ACK AFTER actual COMMIT")};return result},release:destroy=>{releases.push(!!destroy);client.release(destroy)}}
|
|
}}
|
|
const reject=async(promise:Promise<unknown>)=>{await assert.rejects(promise);cases++}
|
|
try{
|
|
await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1")
|
|
await pool.query(SERVER_POLL_JOURNAL_DDL)
|
|
ids.push(...(await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('poll-journal-owned-source','127.0.0.1',9,true),('poll-journal-owned-peer','127.0.0.1',9,true) RETURNING id")).rows.map(row=>Number(row.id)))
|
|
const allServerIds=(await pool.query("SELECT id FROM servers ORDER BY id LIMIT 1001")).rows.map(row=>Number(row.id));assert(allServerIds.length<=1000)
|
|
const fixture=async(kind:"mcp"|"jwt"|"local"="local")=>{
|
|
const issuedAt=Date.now()-1000,expiresAt=Date.now()+600000,executionId=randomUUID(),ownerId="poll-owned-"+randomUUID()
|
|
const identity:ServerPollJournalContext["identity"]=kind==="mcp"?{kind,ownerId,executionId,connectionId:randomUUID(),credentialId:randomUUID(),operationName:"servers.poll.run",issuedAt,expiresAt}:{kind:"rest",authentication:kind,ownerId,executionId,requestId:"accepted-owned-request-"+randomUUID(),issuedAt,expiresAt}
|
|
const around=new Date().toISOString(),beforeProof=await proof({ownedSource:ids,version:1})
|
|
const plan:ServerPollJournalContext["plan"]={version:1,requiresHuman:true,scope:"global",request:{serverId:ids[0],receiptId:randomUUID()},around,beforeProof,authorityProof:await proof({identity,scope:ids}),bindingProof:"a".repeat(64),maintenance:[
|
|
{kind:"partition.ensure",parent:"server_snapshots",name:"server_snapshots_w_20990101",from:"2099-01-01T00:00:00.000Z",to:"2099-01-08T00:00:00.000Z"},
|
|
{kind:"partition.storage",parent:"flow_daily_facts",name:"flow_daily_facts_m_209901",settings:{fillfactor:"70",autovacuum_vacuum_scale_factor:"0.05",autovacuum_vacuum_cost_limit:"2000"}},
|
|
{kind:"partition.drop",parent:"traffic_samples",name:"traffic_samples_w_20000101"},
|
|
],affectedServerIds:[...allServerIds],mutationCost:11}
|
|
const {bindingProof:_initial,...core}=plan;plan.bindingProof=await proof(core)
|
|
let planId:string|undefined,live=true
|
|
if(identity.kind==="mcp"){
|
|
const permissions=["mm:servers:write"],policy={id:identity.connectionId,version:1,status:"active",mode:"approval",permissions,expiresAt:new Date(expiresAt).toISOString(),scope:{serverIds:allServerIds,operationIds:[identity.operationName],allowGlobal:true}}
|
|
await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned poll journal SQL',$2,$3::jsonb,$4::jsonb,$5,$6,$7)",[identity.connectionId,ownerId,JSON.stringify(permissions),JSON.stringify(policy),await security.accessGeneration(),new Date(expiresAt),new Date(issuedAt)])
|
|
await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)",[identity.credentialId,identity.connectionId,randomUUID(),new Date(expiresAt)])
|
|
planId=randomUUID()
|
|
await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,$3,1,1,$4,$5::jsonb,$6::jsonb,'executing',$7)",[planId,identity.connectionId,identity.operationName,await security.accessGeneration(),JSON.stringify({sealed:await security.encryptSecret(JSON.stringify(plan))}),JSON.stringify({sealed:await security.encryptSecret(JSON.stringify({authorityProof:plan.authorityProof}))}),new Date(expiresAt)])
|
|
await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')",[executionId,planId,identity.connectionId,randomUUID()])
|
|
}
|
|
const context:ServerPollJournalContext={identity,plan}
|
|
const deps={connections,executionProof:proof,assertAuthority:async(actual:Readonly<ServerPollJournalContext>)=>{
|
|
assert(live,"Owned trusted JWT/local/MCP request revoked");assert.deepEqual(actual,context);assert(Date.now()<identity.expiresAt)
|
|
const servers=(await pool.query("SELECT id,enabled FROM servers ORDER BY id LIMIT 1001")).rows
|
|
assert.deepEqual(servers.map(r=>Number(r.id)),allServerIds);assert.equal(servers.find(r=>Number(r.id)===ids[0])?.enabled,true)
|
|
if(identity.kind==="mcp"){
|
|
const rows=(await pool.query("SELECT c.generation,c.policy,p.payload,p.evidence FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id JOIN mcp_plans p ON p.id=o.plan_id WHERE o.id=$1 AND p.id=$2",[identity.executionId,planId])).rows
|
|
assert.equal(rows.length,1);assert.equal(rows[0].generation,await security.accessGeneration());assert.equal(rows[0].policy.scope.allowGlobal,true)
|
|
assert.deepEqual(JSON.parse(await security.decryptSecret(rows[0].payload.sealed)),plan);assert.deepEqual(JSON.parse(await security.decryptSecret(rows[0].evidence.sealed)),{authorityProof:plan.authorityProof})
|
|
}
|
|
}}
|
|
const journal=createSqlServerPollJournal(context,deps),receiptId=plan.request.receiptId
|
|
const claim=(instance=journal)=>instance.claim(plan.request,plan.bindingProof)
|
|
const native=async(instance=journal)=>{
|
|
const results=await Promise.allSettled(["identity","resource","interfaces","addresses"].map(async name=>{const stage="native."+name;await instance.intent({receiptId,stage,beforeProof,effectProof:await proof({read:name,source:ids[0]})});await instance.acknowledge({receiptId,stage,resultProof:await proof({name,synthetic:true}),outcome:"confirmed"})}))
|
|
const failed=results.find(result=>result.status==="rejected");if(failed?.status==="rejected")throw failed.reason
|
|
}
|
|
const effects=[{kind:"server.name",serverId:ids[0],name:"Owned synthetic name",updatedAt:around},{kind:"flow-cache.invalidate",serverId:ids[0]},{kind:"snapshot.insert",snapshot:{serverId:ids[0],polledAt:around,status:"online"}},{kind:"snapshot.raw.clear",serverId:ids[0],beforeAt:around,ids:[]},...plan.maintenance]
|
|
let afterProof=beforeProof
|
|
const publications=async(instance=journal,wrongMaintenance=false)=>{for(const [index,effect]of effects.entries()){
|
|
const stage="publish."+index,effectProof=await proof(wrongMaintenance&&index===effects.length-1?{kind:"wrong-maintenance"}:effect)
|
|
await instance.intent({receiptId,stage,beforeProof:afterProof,effectProof});afterProof=await proof({prior:afterProof,effect,index})
|
|
await instance.acknowledge({receiptId,stage,resultProof:afterProof,outcome:"confirmed"})
|
|
}}
|
|
const receipt=():ServerPollReceipt=>({receiptId,configurationVerified:true,complete:true,maintenanceComplete:true,nativeReads:4,confirmedPublications:effects.length,completedPublications:effects.length,afterProof,observation:"online",snapshot:{id:1,serverId:ids[0],polledAt:around,status:"online",identityName:"PUBLIC_SYNTHETIC_POLL_OBSERVATION"}})
|
|
return {context,deps,journal,claim,native,publications,receipt,revoke:()=>{live=false},restore:()=>{live=true}}
|
|
}
|
|
for(const kind of ["jwt","local","mcp"] as const){
|
|
const f=await fixture(kind);assert.equal((await f.claim()).state,"new");await f.native();await f.publications();await f.journal.complete(f.receipt(),f.context.plan.bindingProof)
|
|
const restarted=createSqlServerPollJournal(f.context,f.deps),stored=await f.claim(restarted);assert.equal(stored.state,"known");if(stored.state==="known")assert.deepEqual(stored.receipt,f.receipt())
|
|
const anchor=(await pool.query("SELECT * FROM server_poll_executions WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0]
|
|
assert.equal(anchor.identity_kind,f.context.identity.kind);assert.equal(anchor.execution_id,f.context.identity.executionId);assert(!JSON.stringify(anchor).includes(f.context.identity.ownerId));const row=(await pool.query("SELECT * FROM server_poll_receipts WHERE receipt_id=$1",[anchor.receipt_id])).rows[0];assert(!JSON.stringify(row).includes("PUBLIC_SYNTHETIC_POLL_OBSERVATION"))
|
|
await reject(f.journal.complete(f.receipt(),f.context.plan.bindingProof));await reject(f.journal.intent({receiptId:anchor.receipt_id,stage:"publish.7",beforeProof:f.receipt().afterProof,effectProof:"a".repeat(64)}));cases++
|
|
}
|
|
{
|
|
const f=await fixture("mcp"),other=createSqlServerPollJournal(f.context,f.deps)
|
|
assert.deepEqual((await Promise.all([f.claim(),f.claim(other)])).map(v=>v.state).sort(),["new","unresolved"])
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE execution_id=$1",[f.context.identity.executionId])).rows[0].n,1);cases++
|
|
}
|
|
{
|
|
const f=await fixture();await f.claim();await reject(f.journal.complete({...f.receipt(),confirmedPublications:1,completedPublications:1},f.context.plan.bindingProof));await f.native();await f.publications(f.journal,true);await reject(f.journal.complete(f.receipt(),f.context.plan.bindingProof));cases++
|
|
}
|
|
{
|
|
const f=await fixture();await f.claim();const stage={receiptId:f.context.plan.request.receiptId,stage:"native.identity",beforeProof:f.context.plan.beforeProof,effectProof:"a".repeat(64)};await f.journal.intent(stage);await reject(f.journal.intent(stage));const ack={receiptId:stage.receiptId,stage:stage.stage,resultProof:"b".repeat(64),outcome:"confirmed" as const};await f.journal.acknowledge(ack);await reject(f.journal.acknowledge(ack));cases++
|
|
}
|
|
for(const mutation of ["scope","global","owner","credential","credential-expiry","operation","operation-state"] as const){
|
|
const f=await fixture("mcp"),identity=f.context.identity;assert(identity.kind==="mcp")
|
|
if(mutation==="scope")await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1",[identity.connectionId])
|
|
if(mutation==="global")await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,allowGlobal}','false') WHERE id=$1",[identity.connectionId])
|
|
if(mutation==="owner")await pool.query("UPDATE mcp_connections SET owner_id='foreign' WHERE id=$1",[identity.connectionId])
|
|
if(mutation==="credential")await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1",[identity.credentialId])
|
|
if(mutation==="credential-expiry")await pool.query("UPDATE mcp_credentials SET expires_at=clock_timestamp()-interval '1 second' WHERE id=$1",[identity.credentialId])
|
|
if(mutation==="operation")await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}','[]') WHERE id=$1",[identity.connectionId])
|
|
if(mutation==="operation-state")await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1",[identity.executionId])
|
|
// Deliberately permissive trusted callback isolates actual SQL fences.
|
|
await reject(createSqlServerPollJournal(f.context,{...f.deps,assertAuthority:async()=>{}}).claim(f.context.plan.request,f.context.plan.bindingProof))
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0)
|
|
}
|
|
{
|
|
const f=await fixture("mcp");await f.claim();const identity=f.context.identity;assert(identity.kind==="mcp");await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1",[identity.credentialId]);await reject(f.native());assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_intents WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0)
|
|
}
|
|
{
|
|
const f=await fixture("jwt");await f.claim();f.revoke();await reject(f.native());f.restore();assert.equal((await f.claim()).state,"unresolved");cases++
|
|
}
|
|
{
|
|
const f=await fixture("local");await pool.query("UPDATE servers SET enabled=false WHERE id=$1",[ids[0]]);try{await reject(f.claim())}finally{await pool.query("UPDATE servers SET enabled=true WHERE id=$1",[ids[0]])}
|
|
}
|
|
{
|
|
const f=await fixture();await f.claim();await f.native();await f.publications();await f.journal.complete(f.receipt(),f.context.plan.bindingProof)
|
|
for(const [table,column]of [["server_poll_executions","binding_proof"],["server_poll_intents","before_proof"],["server_poll_acks","result_proof"],["server_poll_receipts","receipt_proof"]])await reject(pool.query(`UPDATE ${table} SET ${column}=$2 WHERE receipt_id=$1`,[f.context.plan.request.receiptId,"c".repeat(64)]))
|
|
const other={...f.context,identity:{...f.context.identity,ownerId:"foreign-owner"}};await reject(createSqlServerPollJournal(other,{...f.deps,assertAuthority:async()=>{}}).claim(other.plan.request,other.plan.bindingProof));cases++
|
|
}
|
|
for(const boundary of ["claim","intent","complete"] as const){
|
|
const f=await fixture(),start=statements.length
|
|
if(boundary!=="claim")await f.claim()
|
|
if(boundary==="complete"){await f.native();await f.publications()}
|
|
const beforeLoss=statements.length;loseCommit=true
|
|
const work=boundary==="claim"?f.claim():boundary==="intent"?f.journal.intent({receiptId:f.context.plan.request.receiptId,stage:"native.identity",beforeProof:f.context.plan.beforeProof,effectProof:"a".repeat(64)}):f.journal.complete(f.receipt(),f.context.plan.bindingProof)
|
|
await assert.rejects(work,(error:unknown)=>error instanceof ServerPollJournalOutcomeUnknown&&!error.requiresDurableQuarantine)
|
|
assert(!statements.slice(beforeLoss).includes("ROLLBACK"));assert.equal(statements.slice(beforeLoss).filter(s=>s==="COMMIT").length,2)
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_quarantines WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,1)
|
|
await assert.rejects(f.claim(createSqlServerPollJournal(f.context,f.deps)),ServerPollJournalConflict)
|
|
assert(statements.length>start);assert(releases.includes(true));await reject(pool.query("UPDATE server_poll_quarantines SET context_proof=$2 WHERE receipt_id=$1",[f.context.plan.request.receiptId,"d".repeat(64)]));cases++
|
|
}
|
|
assert.equal(lostCommits,3)
|
|
{
|
|
const f=await fixture(),extra=Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('poll-journal-unbound-catalogue','127.0.0.1',9,false) RETURNING id")).rows[0].id);ids.push(extra)
|
|
await reject(createSqlServerPollJournal(f.context,{...f.deps,assertAuthority:async()=>{}}).claim(f.context.plan.request,f.context.plan.bindingProof))
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_poll_executions WHERE receipt_id=$1",[f.context.plan.request.receiptId])).rows[0].n,0)
|
|
}
|
|
assert(!statements.some(sql=>/^\s*(UPDATE|DELETE|DROP|ALTER)\b/i.test(sql)),"Journal must never issue inverse/configuration mutation statements")
|
|
// Observe append-only protection without issuing any diagnostic DELETE.
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM pg_trigger WHERE tgname=ANY($1::text[]) AND (tgtype::int & 8)=8",[["server_poll_execution_immutable","server_poll_intent_immutable","server_poll_ack_immutable","server_poll_receipt_immutable","server_poll_quarantine_immutable"]])).rows[0].n,5)
|
|
assert(cases>=25&&cases<=60);console.log(`Server poll journal SQL proposal: ${cases} scenarios passed; no routes or production migration activated`)
|
|
return cases
|
|
}finally{
|
|
loseCommit=false
|
|
if(ids.length)await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])",[ids])
|
|
await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1",[previousMcpEnabled])
|
|
}
|
|
}
|