Files
MikrotikManager/backend/scripts/legacy-probe-0037-owned.mjs
T

197 lines
18 KiB
JavaScript

import assert from 'node:assert/strict'
import {createPublicKey,generateKeyPairSync,randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto'
import {readFile,mkdir,writeFile} from 'node:fs/promises'
import {basename,dirname,join,resolve} from 'node:path'
import {tmpdir} from 'node:os'
export async function verifyOwnedLegacy0037Handoff(pg,options,leafBytes,client,directory,inspectOnly=false){
assert.equal(dirname(directory),resolve(tmpdir()))
assert.match(basename(directory),/^mm-archive-leaf-tls-/)
const fixtureState=join(directory,'mm-mcp-test-legacy-0037','state')
await mkdir(fixtureState,{recursive:true,mode:0o700})
const tail=inspectOnly==='tail'?{user:'owned_commit_operator',password:randomBytes(24).toString('hex')}:null
const localLogin=tail??options
Object.assign(process.env,{MMAPP_OWN_PROBE_LEGACY_0037:'true',MMAPP_OWN_PROBE_LEGACY_ORPHANS:'true',
DATABASE_URL:`postgres://${localLogin.user}:${localLogin.password}@127.0.0.1:${options.port}/postgres`,MCP_STATE_DIR:fixtureState,AUTH_REQUIRED:'false'})
const [{applyLegacyProbeFixtureMigrations},{initialDatabaseSchemaControlState},{runOwnedLegacyProbe0037Handoff},
{captureLegacyProbeLineage,captureLegacyProbeStorage,LEGACY_PROBE_CUSTODY_LOCK,LEGACY_PROBE_RELATIONS},
{runPinnedDatabaseSnapshotDump,runPinnedLegacyProbeSnapshotDump},{LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN},
{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},
{provisionDatabaseSourceBindingOffline},{retainPublisherTlsPeer},security,{collectLegacyServerProbeSnapshot}]=await Promise.all([
import('../src/db/legacy-probe-fixture.ts'),import('../src/db/database-schema-control-model.ts'),import('../src/db/proposals/server-probe-legacy-0037-owned.ts'),
import('../src/services/server-probe-legacy-source-inventory.ts'),import('../src/services/database-dump-connection.ts'),import('../src/services/server-probe-legacy-archive-approval.ts'),
import('../src/db/database-source-binding.ts'),import('./database-source-binding-operator.ts'),import('./writer-profile/operator-contract.mjs'),
import('../src/modules/mcp/security.ts'),import('../src/services/server-probe-legacy-snapshot.ts'),
])
const live=new pg.Pool({...options,max:4}),coldDatabase='mm_owned_legacy_cold_'+randomUUID().replaceAll('-','')
let cold,coldAdmin
const store=async(name,value)=>{const file=join(directory,name);await writeFile(file,value,{flag:'wx',mode:0o600});return file}
const inspect=async(sql)=>{
await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
try {
let sqlState
const summary=await collectLegacyServerProbeSnapshot({query:async(text,values)=>{
try { return await sql.query(text,values) } catch(error) { sqlState=error.code;throw error }
}},{deadlineAt:Date.now()+30000})
assert.equal(summary.complete,true,JSON.stringify({reasons:summary.reasons,sqlState}))
const snapshot=await captureLegacyProbeStorage(sql),lineage=await captureLegacyProbeLineage(sql)
return {summary,snapshot,lineage}
} finally { await sql.query('ROLLBACK') }
}
try{
if(tail){
// Use the same canonical application owner from the first byte. The
// signed reference compares live owners and ACLs as part of its digest.
await live.query(`CREATE ROLE ${tail.user} LOGIN CREATEDB PASSWORD '${tail.password}';
ALTER DATABASE postgres OWNER TO ${tail.user}; COMMENT ON DATABASE postgres IS NULL`)
const application=new pg.Pool({...options,...tail,max:4})
try{await applyLegacyProbeFixtureMigrations(application)}finally{await application.end()}
}else await applyLegacyProbeFixtureMigrations(live)
await live.query(`CREATE SCHEMA mmapp_control; CREATE TABLE mmapp_control.database_schema_state(id smallint PRIMARY KEY CHECK(id=1),revision bigint NOT NULL CHECK(revision>=0),state jsonb NOT NULL);
CREATE TABLE mmapp_control.database_schema_intents(id uuid PRIMARY KEY,intent jsonb NOT NULL,terminal jsonb)`)
await live.query('INSERT INTO mmapp_control.database_schema_state VALUES(1,0,$1::jsonb)',[JSON.stringify(initialDatabaseSchemaControlState())])
const owner='owned-legacy-history-owner',generation=randomUUID(),connection=randomUUID(),credential=randomUUID(),plan=randomUUID(),operation=randomUUID()
const restReceipt=randomUUID(),restExecution=randomUUID(),mcpReceipt=randomUUID(),quarantine=randomUUID(),quarantinedExecution=randomUUID()
const expiresAt=Date.UTC(2000,0,1),issuedAt=expiresAt-3600000,expiry=new Date(expiresAt).toISOString(),bindingProof='a'.repeat(64)
await live.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) VALUES($1,'owned history',$2,'[]','{}',$3)",[connection,owner,generation])
await live.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)",[credential,connection,randomUUID(),expiry])
const binding={version:1,operation:'servers.probes.run',principal:{connectionId:connection,credentialId:credential,credentialExpiresAt:expiry},
ownerId:owner,issuedAt,authorityExpiresAt:expiresAt,prepared:{receiptId:mcpReceipt,serverId:1,tool:'ping',expiresAt:expiry,
bindingCiphertext:await security.encryptSecret('owned original provider request')},bindingProof,stageBudget:2}
const step={kind:'service',service:'servers.probes.run',input:{receiptId:mcpReceipt,serverId:1,
inputDigest:await security.privateSecretFingerprint(security.fingerprint(binding),'server-probe-mcp-binding-v1'),
bindingCiphertext:await security.encryptSecret(JSON.stringify(binding))}}
const evidence={sealed:await security.encryptSecret(JSON.stringify({steps:[step],target:{scope:'servers',serverIds:[1]},requiresHuman:true,
verificationScope:'acknowledgement',mutationCost:2,validUntil:expiry}))}
await live.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by)
VALUES($1,$2,'servers.probes.run',1,1,$3,'{}',$4::jsonb,'completed',$5,$6)`,[plan,connection,generation,JSON.stringify(evidence),expiry,owner])
await live.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,finished_at) VALUES($1,$2,$3,$4,'applied',clock_timestamp())",[operation,plan,connection,randomUUID()])
await live.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)",[operation,JSON.stringify(security.redact(step))])
await live.query('INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)',[operation,connection,owner])
const context={identity:{kind:'rest',ownerId:owner,executionId:restExecution,requestId:'owned-original-request',authentication:'jwt',issuedAt,expiresAt},sourceServerId:1,tool:'ping'}
await live.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool)
VALUES($1,$2,'rest',$3,$4,'jwt','owned-original-request',$5,$6,1,'ping')`,[restReceipt,bindingProof,restExecution,owner,issuedAt,expiry])
await live.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,received_at) VALUES($1,1,'native-ping',1000,'received','received',clock_timestamp())",[restReceipt])
const receipt={receiptId:restReceipt,tool:'ping',serverId:1,state:'completed',httpStatus:200,output:'owned original result',data:{kind:'ping'},
attemptedStages:1,completedStages:1,automaticReplay:false,forwardingVerified:false}
const plaintext=JSON.stringify({version:1,context,bindingProof,receipt})
await live.query(`UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,finished_at=clock_timestamp(),receipt_ciphertext=$2,receipt_proof=$3
WHERE receipt_id=$1`,[restReceipt,await security.encryptSecret(plaintext),await security.privateSecretFingerprint(security.fingerprint(plaintext),'server-probe-journal-v1:receipt')])
await live.query(`INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages)
VALUES($1,repeat('b',64),'rest',$2,repeat('c',64),'JOURNAL_UNKNOWN',0,0)`,[quarantine,quarantinedExecution])
const dependencySql=`SELECT jsonb_build_object('operation',to_jsonb(o),'plan',to_jsonb(p),'connection',to_jsonb(c),'step',to_jsonb(s),'credential',to_jsonb(k)) AS original
FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id JOIN public.mcp_connections c ON c.id=o.connection_id
JOIN public.mcp_operation_steps s ON s.operation_id=o.id JOIN public.mcp_credentials k ON k.connection_id=c.id WHERE o.id=$1 AND k.id=$2`
const dependencies=(await live.query(dependencySql,[operation,credential])).rows
assert.equal(dependencies.length,1)
const dependencyHash=sourceBindingSha256(sourceBindingBytes(dependencies))
const beforeClient=await live.connect()
let before
try { before=await inspect(beforeClient) } finally { beforeClient.release() }
assert.equal(before.snapshot.rowCount,4);assert.equal(before.lineage.rowCount,4)
assert.equal(before.summary.counts.verified,2,JSON.stringify(before.summary.reasons));assert.equal(before.summary.counts.unresolved,1)
const privateKey=generateKeyPairSync('ed25519').privateKey, signer=await store('owned-source-signer.pem',privateKey.export({type:'pkcs8',format:'pem'}))
const privatePassword=await store('owned-archive-password',options.password+'\n')
const peerLeaf=new X509Certificate(leafBytes),host='localhost',exporter=await live.connect()
let originalPin,archive,fullArchive,signedApproval
try{
retainPublisherTlsPeer(exporter,{serverName:host,spkiSha256:sourceBindingSha256(peerLeaf.publicKey.export({type:'spki',format:'der'}))})()
await exporter.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
await exporter.query(LEGACY_PROBE_CUSTODY_LOCK)
const {rows:[identity]}=await exporter.query(`SELECT current_database() AS "databaseName",current_user::text AS "currentUser",session_user::text AS "sessionUser",
(pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier",pg_catalog.pg_is_in_recovery() AS "inRecovery",
current_setting('server_version_num')::int AS "serverVersionNum",d.oid::int AS "databaseOid",r.oid::text AS "signedOid"
FROM pg_catalog.pg_database d,pg_catalog.pg_roles r WHERE d.datname=current_database() AND r.rolname=current_user`)
const {signedOid,...source}=identity
const installationId=randomUUID(),sourceIdentityId=randomUUID(),request={installationId,sourceIdentityId,generation,source}
;({deploymentPin:originalPin}=await provisionDatabaseSourceBindingOffline({request,privateKeyFile:signer,outputDirectory:join(directory,'owned-original-registry')}))
const registry=await readDatabaseSourceBindingRegistry(originalPin)
const storage=await captureLegacyProbeStorage(exporter),lineage=await captureLegacyProbeLineage(exporter)
assert.deepEqual(storage,before.snapshot);assert.deepEqual(lineage,before.lineage)
const {rows:[{snapshot}]}=await exporter.query('SELECT pg_catalog.pg_export_snapshot() AS snapshot')
const peer={host,port:options.port,database:'postgres',capture:{name:options.user,signedOid},leaf:peerLeaf.toString()}
archive=await runPinnedLegacyProbeSnapshotDump(peer,options.password,snapshot,directory,{})
fullArchive=await runPinnedDatabaseSnapshotDump(peer,privatePassword,snapshot,{})
const body={format:'legacy-probe-custody-approval-v1',installationId,sourceIdentityId,generation,ordinal:1,
headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,archiveSha256:sourceBindingSha256(archive),
storageSha256:storage.sha256,lineageSha256:lineage.sha256}
signedApproval=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN+sourceBindingCanonical(body)),privateKey).toString('base64url')})
} finally { try { await exporter.query('ROLLBACK') } finally { exporter.release() } }
await store('owned-legacy-probe.dump',archive);await store('owned-legacy-probe-approval.json',signedApproval)
const archiveFile=await store('owned-full-cold.dump',fullArchive)
const {stdout:toc}=await client('pg_restore',['--list',archiveFile],'pinned')
assert.doesNotMatch(toc,/mmapp_control/)
await live.query(`CREATE DATABASE ${coldDatabase} TEMPLATE template0`)
coldAdmin=new pg.Client({...options,database:coldDatabase});await coldAdmin.connect()
if(/\bSCHEMA - public\b/.test(toc))await coldAdmin.query('DROP SCHEMA public RESTRICT')
try { await client('pg_restore',['--dbname='+coldDatabase,'--exit-on-error','--no-owner',archiveFile],'pinned') }
catch { throw Error('Owned full legacy cold restore failed') }
const coldPassword=randomUUID()
await coldAdmin.query(`CREATE ROLE owned_legacy_reader LOGIN PASSWORD '${coldPassword}'; GRANT USAGE ON SCHEMA public TO owned_legacy_reader; GRANT SELECT ON TABLE
${['schema_migrations','mcp_operations','mcp_operation_steps','mcp_plans','mcp_connections','mcp_credentials',...LEGACY_PROBE_RELATIONS].map(name=>`public.${name}`).join(', ')} TO owned_legacy_reader`)
cold=new pg.Client({...options,database:coldDatabase,user:'owned_legacy_reader',password:coldPassword});await cold.connect()
const coldState=join(directory,'owned-cold-key-state')
await mkdir(coldState,{mode:0o700})
const archivedKey=await readFile(join(fixtureState,'encryption-key'))
try { await writeFile(join(coldState,'encryption-key'),archivedKey,{flag:'wx',mode:0o600}) } finally { archivedKey.fill(0) }
const assertCold=async()=>{
let read
process.env.MCP_STATE_DIR=coldState
try { read=await inspect(cold) } finally { process.env.MCP_STATE_DIR=fixtureState }
assert.deepEqual(read,before)
assert.equal(sourceBindingSha256(sourceBindingBytes((await cold.query(dependencySql,[operation,credential])).rows)),dependencyHash)
assert((await cold.query('SELECT execution_id FROM public.server_probe_executions WHERE receipt_id=$1',[restReceipt])).rows[0]?.execution_id===restExecution,'Original receipt ID drift')
assert((await cold.query('SELECT execution_id FROM public.server_probe_execution_quarantines WHERE receipt_id=$1',[quarantine])).rows[0]?.execution_id===quarantinedExecution,'Original unresolved ID drift')
await assert.rejects(cold.query('DELETE FROM public.server_probe_execution_quarantines WHERE receipt_id=$1',[quarantine]),{code:'42501'})
}
await assertCold()
if(['cold','execute','adopt','release','tail'].includes(inspectOnly)){
const {verifyOwnedLegacyColdCommand}=await import('./legacy-probe-cold-verify-owned.mjs')
return await verifyOwnedLegacyColdCommand({live,coldAdmin,cold,options,directory,coldState,leafBytes,originalPin,archive,fullArchive,signedApproval,
execute:inspectOnly!=='cold',adopt:['adopt','release','tail'].includes(inspectOnly),release:['release','tail'].includes(inspectOnly),
tail,sourcePrivateKey:privateKey})
}
if(inspectOnly){
const {verifyOwnedHandoffInspector}=await import('./legacy-probe-handoff-inspect-owned.mjs')
return await verifyOwnedHandoffInspector({live,directory,options,leafBytes,originalPin,archive,fullArchive,signedApproval,privateKey,intentOnly:inspectOnly==='intent'})
}
const operatorPublicKeyPem=Buffer.from(createPublicKey(privateKey).export({type:'spki',format:'pem'}))
const archiveApproval={archive,signedApproval,operatorPublicKeyPem}, input={live,cold,coldDatabase,fixtureDirectory:directory,originalPin,archiveApproval}
const expectPre0037=async()=>{
const sql=await live.connect()
try { assert.deepEqual(await inspect(sql),before) } finally { sql.release() }
const {rows:[row]}=await live.query(`SELECT count(*)::int AS markers, to_regclass('public.server_probe_storage_admissions')::text AS v2
FROM public.schema_migrations`)
assert.deepEqual(row,{markers:36,v2:null})
}
await assert.rejects(runOwnedLegacyProbe0037Handoff({...input,archiveApproval:{...archiveApproval,archive:Buffer.concat([archive,Buffer.from('tamper')])}}),/approval refused/)
await coldAdmin.query("UPDATE public.mcp_operation_steps SET intended_state='{\"drift\":true}' WHERE operation_id=$1",[operation])
await assert.rejects(runOwnedLegacyProbe0037Handoff(input),{code:'ERR_ASSERTION'})
await coldAdmin.query('UPDATE public.mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1',
[operation,JSON.stringify(dependencies[0].original.step.intended_state)])
await expectPre0037()
const competingWriter=await live.connect()
try{
await competingWriter.query('BEGIN')
await competingWriter.query('LOCK TABLE public.mcp_plans IN ROW EXCLUSIVE MODE')
await assert.rejects(runOwnedLegacyProbe0037Handoff(input),{code:'55P03'})
} finally { try { await competingWriter.query('ROLLBACK') } finally { competingWriter.release() } }
await assert.rejects(runOwnedLegacyProbe0037Handoff({...input,failAfterBody:true}),{code:'22012'})
await expectPre0037()
const outcome=await runOwnedLegacyProbe0037Handoff(input)
assert.deepEqual(outcome,{archivedRows:4,lineageRows:4,readyAuthorized:false})
await assertCold()
assert.equal(sourceBindingSha256(sourceBindingBytes((await live.query(dependencySql,[operation,credential])).rows)),dependencyHash)
const {rows:history}=await live.query("SELECT terminal->>'outcome' AS outcome FROM mmapp_control.database_schema_intents ORDER BY (intent->>'epoch')::int")
assert.deepEqual(history,[{outcome:'observed-failure'},{outcome:'observed-success'}])
return {ownedAtomicLegacy0037Handoff:true,coldArchiveRows:4,verifiedHistoricalIdentities:2,unresolvedHistoricalIdentities:1,readyAuthorized:false}
} finally {
await Promise.allSettled([cold?.end(),coldAdmin?.end(),live.end()])
if(inspectOnly){
const {closePool}=await import('../src/db/index.ts')
await closePool()
}
}
}