import assert from 'node:assert/strict' import {generateKeyPairSync,randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto' import {mkdir,writeFile} from 'node:fs/promises' import {basename,dirname,join,resolve} from 'node:path' import {tmpdir} from 'node:os' import {modelOwnedOperatorFiles} from './legacy-probe-cold-verify-owned.mjs' export async function verifyOwnedSource58Foundation(pg,options,leaf,directory,admission=false){ assert.equal(options.host,'127.0.0.1');assert.equal(options.database,'postgres');assert.equal(options.user,'archive_leaf_fixture') assert.equal(dirname(directory),resolve(tmpdir()));assert.match(basename(directory),/^mm-archive-leaf-tls-/) process.env.DATABASE_URL=`postgres://${options.user}:${options.password}@127.0.0.1:${options.port}/postgres` const [{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},{provisionDatabaseSourceBindingOffline}, {migrationManifestDigest},{RESTORE_WRITER_PROFILE_DEFINITION_SHA256},{captureMigrationSchema},{applySqlMigrations},{createBootstrapMaterial}]=await Promise.all([ import('../src/db/database-source-binding.ts'),import('./database-source-binding-operator.ts'),import('../src/db/migration-manifest.ts'), import('../src/db/database-restore-quarantine-profile.ts'),import('../src/db/migration-schema-inventory.ts'),import('../src/db/migrate.ts'), import('./database-writer-bootstrap-operator.mjs'), ]) const control=new pg.Pool(options),passwords={},names={control:options.user,operator:'owned_source58_operator',publisher:'owned_source58_publisher', runtime:'owned_source58_runtime',capture:'owned_source58_capture'} const sha=sourceBindingSha256,store=async(name,bytes)=>{const file=join(directory,name);await writeFile(file,bytes,{flag:'wx',mode:0o600});return file} const restoreProfile=admission==='quarantine'||admission==='upgrade' const publishes=admission==='publication'||restoreProfile let publisher,operator,modeled try{ for(const scope of ['operator','publisher','runtime','capture']){ passwords[scope]=randomBytes(24).toString('hex') await control.query(`CREATE ROLE ${names[scope]} LOGIN PASSWORD '${passwords[scope]}'`) } await control.query(`REVOKE ALL ON DATABASE postgres FROM PUBLIC; GRANT CONNECT ON DATABASE postgres TO ${[names.operator,names.publisher,names.runtime,names.capture].join(', ')}; ALTER SCHEMA public OWNER TO ${names.operator}; CREATE SCHEMA mmapp_control; GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${names.operator}, ${names.publisher}`) const actor=scope=>new pg.Pool({...options,user:names[scope],password:passwords[scope]}) publisher=actor('publisher');operator=actor('operator') await applySqlMigrations(operator) const {rows:[source]}=await publisher.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" FROM pg_catalog.pg_database d WHERE d.datname=current_database()`) const {rows:liveRoles}=await control.query('SELECT rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1::text[])',[Object.values(names)]) const roles=Object.fromEntries(Object.entries(names).map(([scope,name])=>[scope,liveRoles.find(role=>role.name===name)])) const sourceKey=generateKeyPairSync('ed25519'),approver=generateKeyPairSync('ed25519') const signer=await store('foundation-source-key.pem',sourceKey.privateKey.export({format:'pem',type:'pkcs8'})) const {deploymentPin:pin}=await provisionDatabaseSourceBindingOffline({request:{installationId:randomUUID(),sourceIdentityId:randomUUID(),generation:randomUUID(),source}, privateKeyFile:signer,outputDirectory:join(directory,'foundation-source-registry')}) const registry=await readDatabaseSourceBindingRegistry(pin) const tls={serverName:'localhost',caSha256:sha(leaf),spkiSha256:sha(new X509Certificate(leaf).publicKey.export({format:'der',type:'spki'}))} const request={format:'application-writer-bootstrap-request-v1',installationId:pin.installationId,sourceHeadSha256:registry.headSha256,roles,tls, source:{systemIdentifier:source.systemIdentifier,databaseOid:String(source.databaseOid),databaseName:source.databaseName,serverVersionNum:source.serverVersionNum,inRecovery:false}} const bootstrapMaterial=createBootstrapMaterial(request,sha(sourceBindingBytes(request))) const bootstrap=sourceBindingBytes(bootstrapMaterial.bootstrap) const activationDirectory=join(directory,'foundation-activation');await mkdir(activationDirectory,{mode:0o700}) const activation=sourceBindingBytes({format:'database-source-activation-v1',activationDirectory,installationId:pin.installationId,sourceIdentityId:registry.body.sourceIdentityId, status:'active',publicKeySpkiSha256:pin.publicKeySpkiSha256,generations:[{generation:registry.body.generation,headSha256:registry.headSha256, bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]}) await store('foundation-activation/source-activation.json',activation) const states=async(includeControlEventTriggers=false)=>{ const client=await control.connect() try{ await client.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') await client.query("SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'; SET LOCAL statement_timeout='5s'") return await captureMigrationSchema(client,{scope:{applicationNamespaces:['public','mmapp_control']},includeControlEventTriggers}) }finally{await client.query('ROLLBACK');client.release(true)} } const baseline=await states() modeled=await modelOwnedOperatorFiles(directory,sha) const [{main},{SOURCE58_FOUNDATION_DOMAIN,source58FoundationSqlSha256}]=await Promise.all([ import('./database-writer-profile-operator.mts'),import('./writer-profile/source58-foundation.mts'), ]) const manifestSha256=migrationManifestDigest(),foundationSha256=source58FoundationSqlSha256(roles) const approved={format:'source58-foundation-approval-v1',decision:'install-foundations',bootstrapSha256:sha(bootstrap), sourceHeadSha256:registry.headSha256,sourceTlsSpkiSha256:tls.spkiSha256,manifestSha256,definitionSha256:RESTORE_WRITER_PROFILE_DEFINITION_SHA256, preSchemaSha256:baseline.sha256,foundationSha256,expiresAt:Date.now()+90000} const args=['source58-foundation-enroll','--bootstrap',await store('foundation-bootstrap.json',bootstrap),'--bootstrap-sha256',sha(bootstrap), '--source-registry',pin.registryDirectory,'--source-spki-sha256',pin.publicKeySpkiSha256,'--activation-directory',activationDirectory, '--activation-sha256',sha(activation),'--ca',await store('foundation-ca.pem',leaf), '--approval-public-key',await store('foundation-approval-pub.pem',approver.publicKey.export({format:'pem',type:'spki'})), '--approval-spki-sha256',sha(approver.publicKey.export({format:'der',type:'spki'}))] for(const [scope,password] of [['publisher',passwords.publisher],['control',options.password]]){ const transport=sourceBindingBytes({host:'localhost',port:options.port,user:names[scope],password,database:options.database}) args.push('--'+scope+'-transport',await store('foundation-'+scope+'-transport.json',transport),'--'+scope+'-transport-sha256',sha(transport)) } const approve=async(body,name)=>{ const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(SOURCE58_FOUNDATION_DOMAIN+sourceBindingCanonical(body)),approver.privateKey).toString('base64url')}) return [...args,'--approval',await store(name,bytes),'--approval-sha256',sha(bytes)] } if(!publishes){ await assert.rejects(main(await approve({...approved,preSchemaSha256:'0'.repeat(64)},'foundation-wrong-base.json')),/Exact signed pre-foundation catalogue required/) assert.equal((await states()).sha256,baseline.sha256) } const install=await approve(approved,'foundation-approval.json'),result=await main(install) assert.equal(result.event,'database-source58-foundation');assert.equal(result.outcome,'committed');assert.equal(result.foundationSha256,foundationSha256) assert.equal(result.readyAuthorized,false);assert.equal(result.cleanupUnconfirmed,false) if(!publishes){ await assert.rejects(main(install),/Unenrolled staging control namespace and marker required/) await assert.rejects(operator.query('CREATE TABLE public.owned_foundation_ddl(id int)'),{code:'55000'}) const connection=await operator.connect() try{ await connection.query('BEGIN') await connection.query('SELECT pg_catalog.pg_advisory_xact_lock_shared(8723110::bigint)') await connection.query('CREATE TABLE public.owned_foundation_ddl(id int)') }finally{await connection.query('ROLLBACK');connection.release(true)} } const {rows:[state]}=await control.query(`SELECT (SELECT count(*)::int FROM mmapp_control.application_writer_profile_current) AS heads, (SELECT count(*)::int FROM mmapp_control.application_writer_profile_history) AS history, pg_catalog.to_regclass('mmapp_control.database_restore_quarantine') IS NULL AS quarantine_absent, (SELECT count(*)::int FROM pg_catalog.pg_trigger WHERE tgrelid='public.schema_migrations'::regclass AND NOT tgisinternal) AS marker_triggers`) assert.deepEqual(state,{heads:0,history:0,quarantine_absent:true,marker_triggers:0}) if(admission){ const [{SOURCE57_ADMISSION_DOMAIN},{source57AdmissionSqlSha256},{BUILD57_PROVENANCE}, {assertReviewedMigrationMarkerGuard},{collectSource57Catalogue},{readProtectedRegistryStructureData},{canonical}]=await Promise.all([ import('./writer-profile/source58-foundation.mts'),import('./writer-profile/source57-admission-plan.mts'),import('./writer-profile/fixed57-build-definition.ts'), import('../src/db/partition-guard-inheritance.ts'),import('./writer-profile/catalogue.mts'), import('../src/db/application-writer-profile-registry-structure.ts'),import('./writer-profile/application-writer-profile-canonical.ts'), ]) const predecessor=await states(true),admissionSha256=source57AdmissionSqlSha256(roles) const {foundationSha256:_foundation,...common}=approved const body={...common,format:'source57-admission-approval-v1',decision:'install-admission', definitionSha256:BUILD57_PROVENANCE.templateSha256,preSchemaSha256:predecessor.sha256,admissionSha256,expiresAt:Date.now()+90000} const issue=async(body,name)=>{ const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(SOURCE57_ADMISSION_DOMAIN+sourceBindingCanonical(body)),approver.privateKey).toString('base64url')}) return ['source57-admission-enroll',...args.slice(1),'--approval',await store(name,bytes),'--approval-sha256',sha(bytes)] } if(!publishes){ await assert.rejects(main(await issue({...body,preSchemaSha256:'0'.repeat(64)},'admission-wrong-base.json')),/Exact signed pre-admission catalogue required/) assert.equal((await states(true)).sha256,predecessor.sha256) } const admissionArgs=await issue(body,'admission-approval.json'),result=await main(admissionArgs) assert.equal(result.admissionSha256,admissionSha256);assert.equal(result.readyAuthorized,false) if(publishes){ const {verifyOwnedSource57Publication}=await import('./source57-publication-owned.mjs') return await verifyOwnedSource57Publication({pg,control,operator,actor,main,directory,store,sha,bootstrapMaterial,bootstrap,request,pin,registry,tls, activation,activationDirectory,publisherTransport:args[args.indexOf('--publisher-transport')+1],publisherTransportSha256:args[args.indexOf('--publisher-transport-sha256')+1], controlTransport:args[args.indexOf('--control-transport')+1],controlTransportSha256:args[args.indexOf('--control-transport-sha256')+1], ca:args[args.indexOf('--ca')+1],quarantine:restoreProfile,publishSource58:admission==='upgrade'}) } await assert.rejects(main(admissionArgs),/Unenrolled public relations and empty Source57 writer registry required/) const markerReader=await operator.connect() try{ await markerReader.query('BEGIN') await markerReader.query('SELECT pg_catalog.pg_advisory_xact_lock_shared(8723110::bigint)') await assertReviewedMigrationMarkerGuard(markerReader) }finally{await markerReader.query('ROLLBACK');markerReader.release(true)} await assert.rejects(operator.query('TRUNCATE public.schema_migrations'),{code:'55000'}) const runtime=actor('runtime') try{ assert.equal((await runtime.query('SELECT count(*)::int AS n FROM public.schema_migrations')).rows[0].n,57) await assert.rejects(runtime.query('UPDATE public.schema_migrations SET applied_at=now()'),{code:'42501'}) }finally{await runtime.end()} const verified=await publisher.connect() try{ await verified.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY') const [catalogue,security]=[await collectSource57Catalogue(verified,{roleBindings:roles}),await readProtectedRegistryStructureData(verified,roles)] assert.equal(sha(canonical({base:catalogue.catalogSha256,security:security.sha256})),result.catalogueSha256) }finally{await verified.query('ROLLBACK');verified.release(true)} return {source57AdmissionInstalled:true,exactSource57CatalogueVerified:true,admissionSha256,catalogueSha256:result.catalogueSha256, realOperatorReviewedMarkerAccepted:true,unadmittedMarkerTruncateRefused:true,runtimeMarkerUpdateRefused:true,existing57MarkersPreserved:true, signedWrongPredecessorRefused:true,replayRefused:true,publishedWriterHead:false,restoreQuarantineInstalled:false,readyAuthorized:false} } return {source58FoundationInstalled:true,signedWrongPredecessorRefused:true,replayRefused:true,operatorDdlWithoutAttemptLockRefused:true, operatorDdlUnderAttemptLockVerified:true,registryEmpty:true,markerCohortInstalled:false,readyAuthorized:false, foundationSha256,registryStructureSha256:result.registryStructureSha256} }finally{ modeled?.restore() await Promise.all([control.end(),publisher?.end(),operator?.end()]) await (await import('../src/db/index.ts')).closePool() } }