import assert from 'node:assert/strict' import {execFile} from 'node:child_process' import {createHash, generateKeyPairSync, randomBytes, randomUUID, X509Certificate} from 'node:crypto' import {chmod, copyFile, mkdir, mkdtemp, open, readFile, readdir, realpath, rm, unlink, writeFile} from 'node:fs/promises' import {createServer} from 'node:net' import {tmpdir} from 'node:os' import {basename, dirname, join} from 'node:path' import {promisify} from 'node:util' const mode = process.argv[2] assert(process.argv.length === 3 && ['--openssl-only', '--owned-postgres', '--owned-legacy', '--owned-legacy-custody', '--owned-legacy-custody-shape', '--owned-legacy-0037-handoff', '--owned-legacy-handoff-inspect', '--owned-legacy-handoff-intent', '--owned-legacy-cold-verify', '--owned-legacy-handoff-execute','--owned-legacy-handoff-adopt','--owned-legacy-handoff-release','--owned-legacy-handoff-tail','--owned-source58-foundation','--owned-source57-admission','--owned-source57-publication','--owned-source58-restore-enrollment','--owned-source58-publication'].includes(mode)) const openssl = process.env.MMAPP_TLS_FIXTURE_OPENSSL || 'openssl' const clientBinary = name => process.env.MMAPP_PG_CLIENT_BIN ? join(process.env.MMAPP_PG_CLIENT_BIN, name + (process.platform === 'win32' ? '.exe' : '')) : name const run = promisify(execFile) const temporary = await realpath(tmpdir()) const directory = await realpath(await mkdtemp(join(temporary, 'mm-archive-leaf-tls-'))) const execute = (command, args, options = {}) => run(command, args, { cwd: directory, timeout: 15000, maxBuffer: 1024 * 1024, windowsHide: true, ...options, }) const certificate = name => join(directory, `${name}.pem`) const key = name => join(directory, `${name}.key`) const host = 'localhost' const leafExtensions = ['basicConstraints=critical,CA:FALSE', 'keyUsage=critical,digitalSignature,keyEncipherment', 'extendedKeyUsage=serverAuth', 'subjectAltName=DNS:localhost'] const request = name => ['req', '-new', '-newkey', 'rsa:2048', '-nodes', '-subj', `/CN=${name === 'delegated' ? 'Owned delegated fixture' : host}`, '-keyout', key(name)] async function issueCertificates() { await execute(openssl, [...request('pinned'), '-x509', '-days', '2', '-out', certificate('pinned'), ...leafExtensions.flatMap(extension => ['-addext', extension])]) await execute(openssl, ['req', '-new', '-x509', '-newkey', 'rsa:2048', '-nodes', '-subj', '/CN=Owned fixture CA', '-keyout', key('ca'), '-out', certificate('ca'), '-days', '2', '-addext', 'basicConstraints=critical,CA:TRUE', '-addext', 'keyUsage=critical,keyCertSign,cRLSign']) const extensions = join(directory, 'leaf.cnf') await writeFile(extensions, leafExtensions.join('\n') + '\n', {flag:'wx'}) for (const [name, issuer] of [['substitute', 'ca'], ['delegated', 'pinned']]) { const csr = join(directory, `${name}.csr`) await execute(openssl, [...request(name), '-out', csr]) await execute(openssl, ['x509', '-req', '-in', csr, '-CA', certificate(issuer), '-CAkey', key(issuer), '-set_serial', '3', '-days', '2', '-extfile', extensions, '-out', certificate(name)]) } const [pinned, substitute, delegated] = await Promise.all(['pinned', 'substitute', 'delegated'].map(async name => new X509Certificate(await readFile(certificate(name))))) const spki = cert => createHash('sha256').update(cert.publicKey.export({type:'spki', format:'der'})).digest('hex') assert.equal(pinned.ca, false) assert.equal(pinned.subject, pinned.issuer) assert(pinned.verify(pinned.publicKey)) assert(delegated.verify(pinned.publicKey)) assert.equal(new Set([spki(pinned), spki(substitute), spki(delegated)]).size, 3) const verify = (trust, name) => execute(openssl, ['verify', '-purpose', 'sslserver', '-verify_hostname', host, '-no-CApath', '-no-CAstore', '-CAfile', certificate(trust), certificate(name)]) await verify('pinned', 'pinned') await verify('ca', 'substitute') await assert.rejects(verify('pinned', 'substitute'), {stderr: /unable to get local issuer certificate/i}) await assert.rejects(verify('pinned', 'delegated'), {stderr: /invalid CA certificate|key usage does not include certificate signing/i}) } async function verifyLegacyExport(pg, options, leafBytes, client) { const holder = new pg.Client(options) const {port,database} = options const hash = value => createHash('sha256').update(value).digest('hex') const pinned = new X509Certificate(leafBytes) const custody=mode==='--owned-legacy-custody' if(custody)Object.assign(process.env,{DATABASE_URL:`postgres://${options.user}:${options.password}@127.0.0.1:${port}/${database}`,MCP_STATE_DIR:join(directory,'legacy-app-state'),AUTH_REQUIRED:'false'}) let app await holder.connect() try { const {rows:[source]} = await holder.query(`SELECT d.oid::text AS "databaseOid", d.datname AS "databaseName", (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier", pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum",false AS "inRecovery" FROM pg_catalog.pg_database d WHERE d.datname=pg_catalog.current_database()`) const [{createConnectedPublisher}, {poolOptions}, {MIGRATION_MANIFEST}, {captureLegacyProbeStorage, captureLegacyProbeLineage, LEGACY_PROBE_RELATIONS}] = await Promise.all([ import('./writer-profile/publisher.mjs'), import('./writer-profile/operator-contract.mjs'), import('../src/db/migration-manifest.ts'), import('../src/services/server-probe-legacy-source-inventory.ts'), ]) const fixtureSchema = `CREATE TABLE public.schema_migrations(id text PRIMARY KEY); CREATE TABLE public.mcp_plans(id uuid PRIMARY KEY, connection_id uuid, operation_id text, operation_version integer, state text, approved_by text, evidence jsonb); CREATE TABLE public.mcp_connections(id uuid PRIMARY KEY, owner_id text); CREATE TABLE public.mcp_operations(id uuid PRIMARY KEY, plan_id uuid, connection_id uuid, state text); CREATE TABLE public.mcp_operation_steps(operation_id uuid, step_index integer, state text, intended_state jsonb); CREATE TABLE public.mcp_owner_revocations(owner_id text PRIMARY KEY, invalid_before timestamptz); CREATE TABLE public.mcp_credentials(id uuid PRIMARY KEY)` const diagnosticDdl = await Promise.all(MIGRATION_MANIFEST.slice(34,36).map(pin => readFile(new URL(`../drizzle/${pin.file}`, import.meta.url), 'utf8'))) const historicalMarkers = MIGRATION_MANIFEST.slice(0,34).map(pin => pin.file.slice(0,-4)) const prepareLegacy = async sql => { await sql.query(fixtureSchema) await sql.query('INSERT INTO public.schema_migrations SELECT unnest($1::text[])', [historicalMarkers]) for (const migration of diagnosticDdl) await sql.query(migration) } await prepareLegacy(holder) if(mode==='--owned-legacy-custody-shape'){ const inspectShape=async()=>{ await holder.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') try { await captureLegacyProbeStorage(holder);return await captureLegacyProbeLineage(holder) } finally { await holder.query('ROLLBACK') } } assert.equal((await inspectShape()).rowCount,0) await holder.query('CREATE TABLE public.owned_inherited_mcp_operations() INHERITS (public.mcp_operations)') await assert.rejects(inspectShape(),/Exact ordinary legacy custody tables required/) return } // The raw archive also carries unresolved identities. No marker or proof is produced by export. const anchor = randomUUID(), quarantine = randomUUID(), operation = randomUUID(), connection = randomUUID(), plan = randomUUID(), executionId = randomUUID() const sourceRows = async sql => { await sql.query("INSERT INTO public.mcp_operations VALUES($1,$2,$3,'applied')", [operation,plan,connection]) await sql.query("INSERT INTO public.mcp_connections VALUES($1,'owned-actor')", [connection]) await sql.query("INSERT INTO public.mcp_plans VALUES($1,$2,'servers.probes.run',1,'completed','owned-actor','{}')", [plan,connection]) await sql.query("INSERT INTO public.mcp_operation_steps VALUES($1,0,'intent','{}')", [operation]) } await sourceRows(holder) const issuedAt = Date.UTC(2000,0,1)-3600000, expiresAt = Date.UTC(2000,0,1), bindingProof='a'.repeat(64) await holder.query(`INSERT INTO public.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,$3,'rest',$2,'owned-actor','jwt','owned-request',$4,$5,1,'ping')`, [anchor,executionId,bindingProof,issuedAt,new Date(expiresAt)]) await holder.query("INSERT INTO public.server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,1,'native-ping',1000)", [anchor]) if(custody){ const {encryptSecret, fingerprint, privateSecretFingerprint}=await import('../src/modules/mcp/security.ts') const context={identity:{kind:'rest',ownerId:'owned-actor',executionId,requestId:'owned-request',authentication:'jwt',issuedAt,expiresAt},sourceServerId:1,tool:'ping'} const receipt={receiptId:anchor,tool:'ping',serverId:1,state:'completed',httpStatus:200,output:'owned historical receipt',data:{kind:'ping'},attemptedStages:1,completedStages:1,automaticReplay:false,forwardingVerified:false} const plaintext=JSON.stringify({version:1,context,bindingProof,receipt}) await holder.query("UPDATE public.server_probe_execution_stages SET state='received',outcome='received',received_at=clock_timestamp() WHERE receipt_id=$1", [anchor]) await holder.query(`UPDATE public.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`, [anchor,await encryptSecret(plaintext),await privateSecretFingerprint(fingerprint(plaintext),'server-probe-journal-v1:receipt')]) } await holder.query(`INSERT INTO public.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, randomUUID()]) await holder.query("INSERT INTO public.mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,'owned-actor')", [operation, connection]) const publisherPassword = randomBytes(24).toString('hex'), publisher = 'archive_legacy_publisher' const roleNames = {publisher,runtime:'archive_legacy_runtime',capture:'archive_legacy_capture',operator:'archive_legacy_operator',control:'archive_legacy_control'} await holder.query(`CREATE ROLE ${publisher} LOGIN PASSWORD '${publisherPassword}'; ${Object.values(roleNames).filter(role => role !== publisher).map(role => `CREATE ROLE ${role} NOLOGIN`).join('; ')}; GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${publisher}; GRANT SELECT ON public.schema_migrations, ${LEGACY_PROBE_RELATIONS.map(name => `public.${name}`).join(', ')} TO ${publisher}`) const {rows:roleRows} = await holder.query('SELECT rolname AS name, oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1)', [Object.values(roleNames)]) const roles = Object.fromEntries(Object.entries(roleNames).map(([scope,name])=>[scope,roleRows.find(row=>row.name===name)])) const tls = {serverName:host,caSha256:hash(leafBytes),spkiSha256:hash(pinned.publicKey.export({format:'der', type:'spki'}))} const transport = {host,port,user:publisher,password:publisherPassword,database} const installation = {roleBindings:roles,expectedSource:source,tls} const [{createBootstrapMaterial}, {provisionDatabaseSourceBindingOffline}, {sourceBindingBytes, readDatabaseSourceBindingRegistry}, {verifyLegacyProbeArchiveApproval}] = await Promise.all([ import('./database-writer-bootstrap-operator.mjs'), import('./database-source-binding-operator.ts'), import('../src/db/database-source-binding.ts'), import('../src/services/server-probe-legacy-archive-approval.ts'), ]) const fixtureFile = async (name, bytes) => { const file=join(directory,name); await writeFile(file,bytes,{flag:'wx',mode:0o600});return file } const keyFile = await fixtureFile('legacy-source-signer.pem',generateKeyPairSync('ed25519').privateKey.export({type:'pkcs8',format:'pem'})) const installationId = randomUUID(), sourceIdentityId = randomUUID(), generation = randomUUID() const registryDirectory = join(directory,'legacy-registry') const {deploymentPin} = await provisionDatabaseSourceBindingOffline({privateKeyFile:keyFile,outputDirectory:registryDirectory, request:{installationId,sourceIdentityId,generation,source:{...source,databaseOid:Number(source.databaseOid),sessionUser:publisher,currentUser:publisher}}}) const boot = {format:'application-writer-bootstrap-request-v1',installationId,sourceHeadSha256:deploymentPin.currentHeadSha256,source,roles,tls} const bootstrap = sourceBindingBytes(createBootstrapMaterial(boot,hash(sourceBindingBytes(boot))).bootstrap) const wire = sourceBindingBytes(transport), activationDirectory=join(directory,'legacy-activation'), archiveDirectory=join(directory,'legacy-export') const registry = await readDatabaseSourceBindingRegistry(deploymentPin) const activation = sourceBindingBytes({format:'database-source-activation-v1',activationDirectory,installationId,sourceIdentityId,status:'active', publicKeySpkiSha256:deploymentPin.publicKeySpkiSha256,generations:[{generation,headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]}) await mkdir(activationDirectory,{mode:0o700});await mkdir(archiveDirectory,{mode:0o700}) await fixtureFile('legacy-activation/source-activation.json',activation) const argv=[custody?'legacy-custody':'legacy-export','--bootstrap',await fixtureFile('legacy-bootstrap.json',bootstrap),'--bootstrap-sha256',hash(bootstrap), '--transport',await fixtureFile('legacy-transport.json',wire),'--transport-sha256',hash(wire),'--ca',certificate('pinned'), '--source-registry',registryDirectory,'--source-spki-sha256',deploymentPin.publicKeySpkiSha256, '--activation-directory',activationDirectory,'--activation-sha256',hash(activation),'--archive-directory',archiveDirectory,'--source-signing-key', keyFile] let modeledFiles if(process.platform==='win32'){ const {mock}=await import('node:test'), filesUrl=new URL('./writer-profile/operator-files.mjs',import.meta.url) const files=await import(filesUrl.href) const protectedDirectory=async path=>{assert(path===directory||path.startsWith(directory+'\\'));assert.equal(await realpath(path),path)} modeledFiles=mock.module(filesUrl,{namedExports:{...files,protectedDirectory,async readProtected(path,limit,pin){ await protectedDirectory(dirname(path));const bytes=await readFile(path);assert(bytes.length>0&&bytes.length<=limit); if(pin!==undefined)assert.equal(hash(bytes),pin);return bytes },writeFile,async withJournalLease(path,work){ await protectedDirectory(path);const lock=join(path,'publisher-lease.lock'),handle=await open(lock,'wx',0o600) try{return await work()}finally{await handle.close();await unlink(lock)} }}}) } try { const {main}=await import('./database-writer-profile-operator.mts') if(custody){ await assert.rejects(main(argv),error=>error.cause?.code==='42501') const lockedTables=['schema_migrations','mcp_plans','mcp_connections','mcp_operations','mcp_operation_steps',...LEGACY_PROBE_RELATIONS].map(name=>`public.${name}`).join(', ') await holder.query(`GRANT SELECT, MAINTAIN ON TABLE ${lockedTables} TO ${publisher}`) await holder.query('BEGIN') try { await holder.query("UPDATE public.mcp_operations SET state='failed' WHERE id=$1",[operation]) await assert.rejects(main(argv),error=>error.cause?.code==='55P03') } finally { await holder.query('ROLLBACK') } assert.deepEqual(await readdir(archiveDirectory),[]) } const legacy=await main(argv) assert.equal(legacy.event,custody?'database-legacy-custody':'database-legacy-export');assert.equal(legacy.conversionAllowed,false);assert.equal(legacy.readyAuthorized,false) assert.equal(legacy.snapshot.rowCount,4);assert.equal(JSON.stringify(legacy).includes('owned-actor'),false) assert.deepEqual((await readdir(archiveDirectory)).sort(),[legacy.approvalFile,legacy.dumpFile].sort()) const legacyFile=join(archiveDirectory,legacy.dumpFile),archive=await readFile(legacyFile) const signedApproval=await readFile(join(archiveDirectory,legacy.approvalFile)),operatorPublicKeyPem=await readFile(join(registryDirectory,'operator-public.pem')) const verified=await verifyLegacyProbeArchiveApproval(deploymentPin,{archive,signedApproval,operatorPublicKeyPem}) assert.equal(verified.storageSha256,legacy.snapshot.sha256);assert.equal(verified.archiveSha256,legacy.archiveSha256) await assert.rejects(verifyLegacyProbeArchiveApproval(deploymentPin,{archive:Buffer.concat([archive,Buffer.from('tamper')]),signedApproval,operatorPublicKeyPem})) await assert.rejects(main(argv),/Empty protected archive directory required/) const {stdout:legacyToc} = await client('pg_restore',['--list',legacyFile],'pinned') const entries = legacyToc.split(/\r?\n/).filter(line => line && !line.startsWith(';')) assert.equal(entries.length,4) assert.deepEqual(entries.map(line => line.match(/ TABLE DATA public (\S+) /)?.[1]).sort(), [...LEGACY_PROBE_RELATIONS].sort()) await holder.query('CREATE DATABASE archive_legacy_restored') const restored = new pg.Client({...options,database:'archive_legacy_restored'}) await restored.connect() try { await prepareLegacy(restored) await sourceRows(restored) // A sterile verification target keeps the exact column shapes and // foreign keys. Production lifecycle triggers have no role in this readback. await restored.query(`DROP TRIGGER server_probe_execution_immutable ON public.server_probe_executions; DROP TRIGGER server_probe_stage_immutable ON public.server_probe_execution_stages; DROP TRIGGER server_probe_quarantine_immutable ON public.server_probe_execution_quarantines; DROP TRIGGER mcp_diagnostic_cancellation_immutable ON public.mcp_diagnostic_cancellations`) try { await client('pg_restore',['--dbname=archive_legacy_restored','--data-only','--exit-on-error','--no-owner',legacyFile],'pinned') } catch { throw Error('Legacy archive restore failed on the isolated verification database') } await restored.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') assert.deepEqual(await captureLegacyProbeStorage(restored),legacy.snapshot) await restored.query('ROLLBACK') } finally { await restored.end() } let checks=0 const legacyPool = new pg.Pool(poolOptions(transport, {roles,source,tls}, leafBytes)) const operator = createConnectedPublisher(installation, undefined, {async verify() { if (++checks === 2) throw Error('owned terminal source refusal') if(custody){ await holder.query("SET lock_timeout='200ms'") try { await assert.rejects(holder.query("UPDATE public.mcp_operations SET state='failed' WHERE id=$1",[operation]),{code:'55P03'}) } finally { await holder.query('RESET lock_timeout') } } return {platformProtectionVerified:true} }}) try { await assert.rejects(operator.inspectLegacyStorage(legacyPool,{deadlineAt:Date.now()+30000,archiveDirectory:directory,custody}), error=>error.cause?.message==='owned terminal source refusal') assert.equal(checks,2) } finally { await legacyPool.end() } if(custody){ app=await import('../src/db/index.ts') const {inspectLegacyServerProbeSource}=await import('../src/services/server-probe-legacy-inspection.ts') const now=Date.now(),actor={id:'owned-historical-settings-admin',email:'',name:'',apps:['mm'],permissions:['mm:settings:admin'],issuedAt:now-1000,expiresAt:now+60000} const expectation={archive,signedApproval,operatorPublicKeyPem} const inspect=evidence=>inspectLegacyServerProbeSource(actor,{deadlineAt:Date.now()+30000},undefined,evidence,deploymentPin) const inspected=await inspect(expectation) assert.equal(verified.format,'legacy-probe-custody-approval-v1');assert.equal(verified.lineageSha256,legacy.lineageSnapshot.sha256) assert.equal(legacy.lineageSnapshot.rowCount,4);assert.deepEqual(inspected.lineageSnapshot,legacy.lineageSnapshot) assert.equal(inspected.summary.counts.verified,1);assert.equal(inspected.summary.counts.unresolved,2) assert.equal(inspected.summary.automaticReplay,false);assert.equal(inspected.conversionAllowed,false) await holder.query("UPDATE public.mcp_operation_steps SET intended_state='{\"drift\":true}' WHERE operation_id=$1",[operation]) await assert.rejects(inspect(expectation),{code:'STATE_UNAVAILABLE'}) await holder.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') try { assert.deepEqual(await captureLegacyProbeStorage(holder),legacy.snapshot) } finally { await holder.query('ROLLBACK') } } } finally { modeledFiles?.restore() } } finally { await app?.pool.end();await holder.end() } } async function verifyOwnedPostgres() { const [{default:EmbeddedPostgres}, {default:pg}] = await Promise.all([import('embedded-postgres'), import('pg')]) const listener = createServer() await new Promise((resolve, reject) => listener.once('error', reject).listen(0, '127.0.0.1', resolve)) const port = listener.address().port await new Promise((resolve, reject) => listener.close(error => error ? reject(error) : resolve())) const user = 'archive_leaf_fixture', database = 'postgres', password = randomBytes(24).toString('hex') const serverCertificate = certificate('server'), serverKey = key('server') const postgres = new EmbeddedPostgres({databaseDir: join(directory, 'pg'), port, user, password, persistent:true, createPostgresUser:false, initdbFlags:['--encoding=UTF8'], postgresFlags:['-h', '127.0.0.1', '-c', 'ssl=on', '-c', `ssl_cert_file=${serverCertificate}`, '-c', `ssl_key_file=${serverKey}`], onLog:()=>{}, onError:()=>{}}) const childEnvironment = Object.fromEntries(['PATH', 'SystemRoot', 'WINDIR', 'TEMP', 'TMP'].filter(name => process.env[name]) .map(name => [name, process.env[name]])) const serviceFiles = new Map() for (const trust of ['pinned', 'ca']) { const file = join(directory, `pg-service-${trust}.conf`) await writeFile(file, `[owned_archive]\nhost=${host}\nhostaddr=127.0.0.1\nport=${port}\nuser=${user}\npassword=${password}\ndbname=${database}\nsslmode=verify-full\nsslrootcert=${certificate(trust)}\ngssencmode=disable\nconnect_timeout=5\n`, {flag:'wx', mode:0o600}) serviceFiles.set(trust, file) } const client = (name, args, trust, options = {}) => execute(clientBinary(name), args, { env:{...childEnvironment, PGSERVICE:'owned_archive', PGSERVICEFILE:serviceFiles.get(trust)}, ...options, }) const check = trust => client('psql', ['--no-psqlrc', '--no-password', '--tuples-only', '--no-align', '--command', 'SELECT ssl FROM pg_catalog.pg_stat_ssl WHERE pid=pg_catalog.pg_backend_pid()'], trust) const dump = (trust, extra = []) => client('pg_dump', ['--no-password', '--format=custom', '--data-only', '--strict-names', '--table=public.archive_probe', ...extra], trust, {encoding:'buffer'}) const badCertificate = async promise => assert.rejects(promise, error => /certificate verify failed|certificate verification failed/i.test(String(error.stderr))) let started = false async function startWith(name) { await copyFile(certificate(name), serverCertificate) await copyFile(key(name), serverKey) await chmod(serverKey, 0o600) await postgres.start() started = true } async function stop() { await postgres.stop(); started = false } try { await postgres.initialise() if(process.env.MMAPP_PG_CLIENT_BIN) process.env.PATH = process.env.MMAPP_PG_CLIENT_BIN + (process.platform === 'win32' ? ';' : ':') + process.env.PATH await startWith('pinned') assert.equal((await check('pinned')).stdout.trim(), 't') const options = {host:'127.0.0.1', port, user, password, database, connectionTimeoutMillis:5000, statement_timeout:5000, ssl:{ca:await readFile(certificate('pinned')), servername:host, rejectUnauthorized:true}} if(['--owned-source58-foundation','--owned-source57-admission','--owned-source57-publication','--owned-source58-restore-enrollment','--owned-source58-publication'].includes(mode)){ const {verifyOwnedSource58Foundation}=await import('./source58-foundation-owned.mjs') return await verifyOwnedSource58Foundation(pg,options,await readFile(certificate('pinned')),directory, mode==='--owned-source58-publication'?'upgrade':mode==='--owned-source58-restore-enrollment'?'quarantine': mode==='--owned-source57-publication'?'publication':mode==='--owned-source57-admission') } if(['--owned-legacy-0037-handoff','--owned-legacy-handoff-inspect','--owned-legacy-handoff-intent','--owned-legacy-cold-verify','--owned-legacy-handoff-execute','--owned-legacy-handoff-adopt','--owned-legacy-handoff-release','--owned-legacy-handoff-tail'].includes(mode)){ const {verifyOwnedLegacy0037Handoff}=await import('./legacy-probe-0037-owned.mjs') return await verifyOwnedLegacy0037Handoff(pg,options,await readFile(certificate('pinned')),client,directory, mode==='--owned-legacy-handoff-tail'?'tail':mode==='--owned-legacy-handoff-release'?'release':mode==='--owned-legacy-handoff-adopt'?'adopt':mode==='--owned-legacy-handoff-execute'?'execute':mode==='--owned-legacy-cold-verify'?'cold':mode==='--owned-legacy-handoff-intent'?'intent':mode==='--owned-legacy-handoff-inspect') } if (mode.startsWith('--owned-legacy')) return await verifyLegacyExport(pg, options, await readFile(certificate('pinned')), client) const holder = new pg.Client(options), writer = new pg.Client(options) let snapshot, productionPeer, archivePasswordFile, runPinnedDatabaseSnapshotDump try { await holder.connect() await writer.connect() await holder.query('CREATE TABLE public.archive_probe(id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY); INSERT INTO public.archive_probe DEFAULT VALUES') await holder.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') snapshot = (await holder.query('SELECT pg_catalog.pg_export_snapshot() AS snapshot')).rows[0].snapshot await writer.query('INSERT INTO public.archive_probe DEFAULT VALUES') const {stdout:archive} = await dump('pinned', [`--snapshot=${snapshot}`]) assert.equal(archive.subarray(0, 5).toString(), 'PGDMP') const file = join(directory, 'archive-probe.dump') await writeFile(file, archive, {flag:'wx', mode:0o600}) const {stdout:decoded} = await client('pg_restore', ['--data-only', '--file=-', file], 'pinned') assert.match(decoded.replaceAll('\r\n', '\n'), /COPY public\.archive_probe \(id\) FROM stdin;\n1\n\\\.\n/) await holder.query('ROLLBACK') const runtimePassword = randomBytes(24).toString('hex'), capturePassword = randomBytes(24).toString('hex') archivePasswordFile = join(directory, 'archive-password') await writeFile(archivePasswordFile, capturePassword, {flag:'wx',mode:0o600}) await holder.query(`CREATE ROLE archive_runtime LOGIN PASSWORD '${runtimePassword}'; CREATE ROLE archive_capture LOGIN PASSWORD '${capturePassword}'; REVOKE TEMP ON DATABASE postgres FROM PUBLIC; GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO archive_runtime; GRANT SELECT ON TABLE public.archive_probe TO archive_runtime, archive_capture; GRANT SELECT ON SEQUENCE public.archive_probe_id_seq TO archive_capture; CREATE SCHEMA mmapp_control; CREATE TABLE mmapp_control.hidden_marker(value text)`) const roles = Object.fromEntries((await holder.query(`SELECT CASE WHEN rolname='archive_runtime' THEN 'runtime' ELSE 'capture' END AS scope, rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname IN ('archive_runtime','archive_capture')`)) .rows.map(({scope,...role})=>[scope,role])) const {rows:[source]} = await holder.query(`SELECT d.oid::text AS "databaseOid", d.datname AS "databaseName", (pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier", pg_catalog.current_setting('server_version_num')::int AS "serverVersionNum",false AS "inRecovery" FROM pg_catalog.pg_database d WHERE d.datname=pg_catalog.current_database()`) const hash = value => createHash('sha256').update(value).digest('hex') const leafBytes = await readFile(certificate('pinned')), pinned = new X509Certificate(leafBytes) const requestBytes = Buffer.from(JSON.stringify({format:'application-writer-bootstrap-request-v1',installationId:'92ae9f9b-b324-425f-9ed5-77be7c000947', sourceHeadSha256:'f'.repeat(64),source,roles,tls:{serverName:host,caSha256:hash(leafBytes), spkiSha256:hash(pinned.publicKey.export({format:'der', type:'spki'}))}})) const requestFile = join(directory, 'runtime-request.json') await writeFile(requestFile,requestBytes,{flag:'wx',mode:0o600}) Object.assign(process.env,{DATABASE_URL:`postgres://archive_runtime:${runtimePassword}@${host}:${port}/${database}`, DATABASE_RUNTIME_REQUEST_FILE:requestFile,DATABASE_RUNTIME_REQUEST_SHA256:hash(requestBytes),DATABASE_RUNTIME_CA_FILE:certificate('pinned'), MCP_STATE_DIR:join(directory,'state')}) const [{exportProtectedDatabaseSnapshot},{pool,readRuntimeDatabaseArchivePeer},{withSystemDatabaseOperationLock},dumpConnection] = await Promise.all([ import('../src/services/database-protected-archive.ts'), import('../src/db/index.ts'), import('../src/db/operation-lock.ts'), import('../src/services/database-dump-connection.ts'), ]) runPinnedDatabaseSnapshotDump = dumpConnection.runPinnedDatabaseSnapshotDump try { const protectedArchive = await withSystemDatabaseOperationLock(()=>pool.connect(),async (runtime,discard)=>{ productionPeer=readRuntimeDatabaseArchivePeer(runtime) return exportProtectedDatabaseSnapshot(runtime,discard,archivePasswordFile) }) const actualArchive = join(directory,'protected.dump') await writeFile(actualArchive,protectedArchive,{flag:'wx',mode:0o600}) const productionToc = (await client('pg_restore',['--list',actualArchive],'pinned')).stdout assert.match(productionToc,/TABLE DATA public archive_probe /) assert.match(productionToc,/SEQUENCE SET public archive_probe_id_seq /) assert.doesNotMatch(productionToc,/mmapp_control|hidden_marker/) } finally { await pool.end() } } finally { await Promise.all([holder.end(), writer.end()]) } await stop() await startWith('substitute') assert.equal((await check('ca')).stdout.trim(), 't') assert.equal((await dump('ca')).stdout.subarray(0, 5).toString(), 'PGDMP') await badCertificate(check('pinned')) await badCertificate(dump('pinned')) const assertPinnedDumpRefused = () => assert.rejects(runPinnedDatabaseSnapshotDump(productionPeer, archivePasswordFile, snapshot, {}), error=>error.code==='DATABASE_DUMP_EXIT'&&error.diagnosticKinds.includes('tls')) await assertPinnedDumpRefused() await stop() await startWith('delegated') await badCertificate(check('pinned')) await badCertificate(dump('pinned')) await assertPinnedDumpRefused() } finally { if (started) await stop() } } try { await issueCertificates() const result=mode==='--openssl-only'?undefined:await verifyOwnedPostgres() console.log(JSON.stringify({mode, exactLeafAccepted:true, ordinaryCaSubstitutionRefused:true, nonCaDelegationRefused:true, realSnapshotDump:mode === '--owned-postgres', protectedProductDump:mode === '--owned-postgres', legacyArchiveRoundTrip:mode === '--owned-legacy'||mode === '--owned-legacy-custody',legacyCustodyLockedAndVerified:mode === '--owned-legacy-custody', inheritedCustodyTablesRefused:mode === '--owned-legacy-custody-shape',...result})) } finally { assert.equal(dirname(directory), temporary) assert(basename(directory).startsWith('mm-archive-leaf-tls-')) await rm(directory, {recursive:true, force:true, maxRetries:10, retryDelay:250}) }