550 lines
36 KiB
JavaScript
550 lines
36 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import {execFile, execFileSync} from 'node:child_process'
|
|
import {randomBytes, randomUUID, X509Certificate} from 'node:crypto'
|
|
import {setDefaultResultOrder} from 'node:dns'
|
|
import {mkdtemp, mkdir, cp, readFile, writeFile, rename, rm, chmod, access} from 'node:fs/promises'
|
|
import {createServer} from 'node:net'
|
|
import {tmpdir} from 'node:os'
|
|
import {dirname, join, resolve} from 'node:path'
|
|
import {fileURLToPath} from 'node:url'
|
|
import {promisify} from 'node:util'
|
|
import EmbeddedPostgres from 'embedded-postgres'
|
|
import pg from 'pg'
|
|
import {tsImport} from 'tsx/esm/api'
|
|
import {poolOptions, retainPublisherTlsPeer} from './writer-profile/operator-contract.mjs'
|
|
import {sha} from './writer-profile/operator-files.mjs'
|
|
|
|
const openssl = process.env.MMAPP_TLS_FIXTURE_OPENSSL || 'openssl'
|
|
const activationOnly = process.argv.includes('--activation-only')
|
|
const schemaOnly = process.argv.includes('--schema-only')
|
|
const runtimeRoleOnly = process.argv.includes('--runtime-role-only')
|
|
const registryOnly = process.argv.includes('--registry-only')
|
|
const run = promisify(execFile)
|
|
setDefaultResultOrder('ipv4first')
|
|
const host = 'localhost'
|
|
const reservePort = async () => {
|
|
const listener = createServer()
|
|
await new Promise((ready, reject) => listener.once('error', reject).listen(0, '127.0.0.1', ready))
|
|
return listener
|
|
}
|
|
const releasePort = listener => new Promise((ready, reject) => listener.close(error => error ? reject(error) : ready()))
|
|
const sourcePort = await reservePort()
|
|
const clonePort = await reservePort()
|
|
const port = sourcePort.address().port, clonePortNumber = clonePort.address().port
|
|
await Promise.all([releasePort(sourcePort), releasePort(clonePort)])
|
|
const transport = {host, port, user: 'mcp_test', password: randomBytes(24).toString('hex'), database: 'postgres'}
|
|
const runtime = 'tls_fixture_runtime', runtimePassword = randomBytes(24).toString('hex')
|
|
const runtimeUrl = targetPort => `postgresql://${runtime}:${runtimePassword}@${host}:${targetPort}/${transport.database}`
|
|
const runtimeRefused = {code: 'DATABASE_RUNTIME_SOURCE_REFUSED'}
|
|
const certFile = 'server.pem', keyFile = 'server-key.pem'
|
|
const root = await mkdtemp(join(tmpdir(), 'mm-writer-tls-owned-'))
|
|
const databaseDir = join(root, 'pg'), cloneDir = join(root, 'pg-clone')
|
|
const sourceTlsDir = join(root, 'source-tls'), cloneTlsDir = join(root, 'clone-tls')
|
|
const postgresLog = []
|
|
const cluster = (databaseDir, port, tlsDir) => new EmbeddedPostgres({databaseDir, port, user: transport.user, password: transport.password,
|
|
persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'],
|
|
postgresFlags: ['-h', '127.0.0.1', '-c', 'ssl=on', '-c', `ssl_cert_file=${join(tlsDir, certFile)}`, '-c', `ssl_key_file=${join(tlsDir, keyFile)}`, '-c', 'log_line_prefix=[%u] '],
|
|
onLog: message => { postgresLog.push(message) }, onError: () => {}})
|
|
const postgres = cluster(databaseDir, port, sourceTlsDir), clone = cluster(cloneDir, clonePortNumber, cloneTlsDir)
|
|
let sourceStarted = false, cloneStarted = false
|
|
const fingerprint = ca => sha(new X509Certificate(ca).publicKey.export({format:'der', type:'spki'}))
|
|
const issue = async tlsDir => {
|
|
await mkdir(tlsDir, {mode:0o700})
|
|
execFileSync(openssl, ['req', '-new', '-newkey', 'rsa:2048', '-x509', '-nodes', '-days', '1', '-subj', '/CN=localhost',
|
|
'-addext', 'subjectAltName=DNS:localhost', '-keyout', keyFile, '-out', certFile], {cwd: tlsDir, stdio: 'ignore', windowsHide: true})
|
|
await chmod(join(tlsDir, keyFile), 0o600)
|
|
return readFile(join(tlsDir, certFile))
|
|
}
|
|
const withClient = async (config, work) => {
|
|
const pool = new pg.Pool({connectionTimeoutMillis:5000, ...config})
|
|
let client
|
|
try { client = await pool.connect(); return await work(client) }
|
|
finally { client?.release(true); await pool.end() }
|
|
}
|
|
const query = "SELECT (pg_control_system()).system_identifier::text AS system, oid::text AS oid FROM pg_database WHERE datname=current_database()"
|
|
const createRuntime = async client => {
|
|
await client.query(`CREATE ROLE ${runtime} LOGIN PASSWORD '${runtimePassword}'; GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${runtime}`)
|
|
const {rows: [{oid}]} = await client.query('SELECT oid::text FROM pg_catalog.pg_roles WHERE rolname=$1', [runtime])
|
|
return oid
|
|
}
|
|
const runtimeFiles = async (request, ca, targetPort = port) => {
|
|
const requestFile = join(root, 'runtime-request.json'), caFile = join(root, 'runtime-ca.pem')
|
|
const bytes = Buffer.from(JSON.stringify(request))
|
|
await Promise.all([writeFile(requestFile, bytes), writeFile(caFile, ca)])
|
|
return {databaseUrl: runtimeUrl(targetPort), requestFile, requestSha256: sha(bytes), caFile}
|
|
}
|
|
const unexpectedConnection = () => assert.fail('Unverified runtime connection became available to application SQL')
|
|
|
|
async function verifyRegistryEnrollment(owner, runtimeRequest, ca) {
|
|
const {mock} = await import('node:test')
|
|
const filesUrl = new URL('./writer-profile/operator-files.mjs', import.meta.url)
|
|
const files = await import(filesUrl.href)
|
|
const {MIGRATION_MANIFEST, migrationManifestDigest} = await tsImport('../src/db/migration-manifest.ts', import.meta.url)
|
|
const {FIXED56_ROLE_TEMPLATE} = await tsImport('./writer-profile/fixed56-build-definition.ts', import.meta.url)
|
|
const operator = runtimeRequest.roles.operator.name, capture = runtimeRequest.roles.capture.name
|
|
const publisher = runtimeRequest.roles.publisher.name, control = runtimeRequest.roles.control.name
|
|
const password = randomBytes(24).toString('hex')
|
|
const guard = name => FIXED56_ROLE_TEMPLATE.functions.find(row => row.namespace === 'mmapp_control' && row.proname === name).definition
|
|
await owner.query(`CREATE ROLE ${operator} LOGIN; CREATE ROLE ${capture} LOGIN; CREATE ROLE ${publisher} LOGIN;
|
|
CREATE ROLE ${control} LOGIN PASSWORD '${password}'; ALTER SCHEMA mmapp_control OWNER TO ${control};
|
|
REVOKE ALL ON SCHEMA mmapp_control FROM PUBLIC;
|
|
GRANT USAGE ON SCHEMA mmapp_control TO ${operator}, ${publisher};
|
|
CREATE TABLE public.schema_migrations (id text PRIMARY KEY);
|
|
GRANT SELECT ON TABLE public.schema_migrations TO ${control};
|
|
GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${operator}, ${publisher}, ${control};
|
|
${guard('candidate_require_preadmission')}; ${guard('candidate_truncate_preadmitted')};
|
|
ALTER FUNCTION mmapp_control.candidate_require_preadmission() OWNER TO ${control};
|
|
ALTER FUNCTION mmapp_control.candidate_truncate_preadmitted() OWNER TO ${control};
|
|
REVOKE ALL ON FUNCTION mmapp_control.candidate_require_preadmission(), mmapp_control.candidate_truncate_preadmitted() FROM PUBLIC;
|
|
GRANT EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() TO ${operator}`)
|
|
await owner.query('INSERT INTO public.schema_migrations SELECT unnest($1::text[])', [MIGRATION_MANIFEST.map(pin => pin.file.slice(0, -4))])
|
|
const keys = Object.keys(runtimeRequest.roles)
|
|
const names = keys.map(key => runtimeRequest.roles[key].name)
|
|
const {rows} = await owner.query('SELECT rolname AS name, oid::text AS oid FROM pg_catalog.pg_roles WHERE rolname=ANY($1)', [names])
|
|
const roles = Object.fromEntries(keys.map((key, index) => [key, {name:names[index], signedOid:rows.find(row => row.name === names[index]).oid}]))
|
|
const manifestSha256 = migrationManifestDigest()
|
|
const bytes = new Map([
|
|
['owned-approval', Buffer.from(JSON.stringify({format:'application-writer-registry-enrollment-v1', manifestSha256,
|
|
deploymentRequest:{...runtimeRequest, roles}}))],
|
|
['owned-transport', Buffer.from(JSON.stringify({...transport, user:control, password}))],
|
|
['owned-ca', ca],
|
|
])
|
|
// Windows fixture supplies protected bytes. The real TLS, SQL and catalog verifier run unchanged.
|
|
const fileFixture = mock.module(filesUrl, {namedExports:{...files, async readProtected(name, max, expected) {
|
|
const value = bytes.get(name)
|
|
assert(value && value.length <= max && sha(value) === expected)
|
|
return value
|
|
}}})
|
|
try {
|
|
const {registryEnrollmentMain} = await import('./writer-profile/registry-enrollment.mts')
|
|
const args = ['registry-enroll', '--approval', 'owned-approval', '--approval-sha256', sha(bytes.get('owned-approval')),
|
|
'--transport', 'owned-transport', '--transport-sha256', sha(bytes.get('owned-transport')), '--ca', 'owned-ca']
|
|
const inspectArgs = ['registry-inspect', ...args.slice(1)]
|
|
const count = async () => (await owner.query(`SELECT count(*)::int AS n FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n
|
|
ON n.oid=c.relnamespace WHERE n.nspname='mmapp_control' AND c.relname IN ('application_writer_profile_history','application_writer_profile_current')`)).rows[0].n
|
|
assert.equal((await registryEnrollmentMain(inspectArgs)).outcome, 'absent')
|
|
assert.equal(await count(), 0)
|
|
await owner.query('CREATE TABLE mmapp_control.application_writer_profile_history (id int)')
|
|
try { await assert.rejects(registryEnrollmentMain(inspectArgs), /relations are incomplete/) }
|
|
finally { await owner.query('DROP TABLE mmapp_control.application_writer_profile_history') }
|
|
await owner.query(`GRANT pg_read_all_data TO ${runtime}`)
|
|
try {
|
|
await assert.rejects(registryEnrollmentMain(args), /Role ownership\/membership/)
|
|
assert.equal(await count(), 0)
|
|
} finally { await owner.query(`REVOKE pg_read_all_data FROM ${runtime}`) }
|
|
const report = await registryEnrollmentMain(args)
|
|
assert.equal(report.outcome, 'committed')
|
|
assert.equal(report.readyAuthorized, false)
|
|
assert.equal(report.cleanupUnconfirmed, false)
|
|
assert.equal(report.manifestSha256, manifestSha256)
|
|
assert.match(report.registryStructureSha256, /^[a-f0-9]{64}$/)
|
|
assert.equal(await count(), 2)
|
|
const observed = await registryEnrollmentMain(inspectArgs)
|
|
assert.equal(observed.outcome, 'installed')
|
|
assert.equal(observed.registryStructureSha256, report.registryStructureSha256)
|
|
await owner.query(`GRANT UPDATE ON mmapp_control.application_writer_profile_history TO ${runtime}`)
|
|
try { await assert.rejects(registryEnrollmentMain(inspectArgs), /effective grant boundary/) }
|
|
finally { await owner.query(`REVOKE UPDATE ON mmapp_control.application_writer_profile_history FROM ${runtime}`) }
|
|
await assert.rejects(registryEnrollmentMain(args), /Registry target\/owner\/absence refused/)
|
|
assert.equal(await count(), 2)
|
|
console.log('Owned registry enrollment and inspection: live TLS, strict catalog readback, rollback on role drift, ACL drift and repeat enrollment refused')
|
|
} finally { fileFixture.restore() }
|
|
}
|
|
|
|
async function verifyRuntimeRolePrivileges(owner, createPool) {
|
|
const settable = 'tls_fixture_settable'
|
|
await owner.query(`CREATE ROLE ${settable} NOLOGIN;
|
|
CREATE TABLE public.runtime_privilege_fixture (value int);
|
|
INSERT INTO public.runtime_privilege_fixture VALUES (1);
|
|
GRANT SELECT, UPDATE ON public.runtime_privilege_fixture TO ${runtime}`)
|
|
const withRuntime = async work => {
|
|
const fresh = createPool(1)
|
|
try { await work(fresh) } finally { await fresh.end() }
|
|
}
|
|
await withRuntime(async fresh => {
|
|
const {rows} = await fresh.query('UPDATE public.runtime_privilege_fixture SET value=2 RETURNING value')
|
|
assert.deepEqual(rows, [{value:2}])
|
|
})
|
|
for (const [grant, revoke] of [
|
|
[`GRANT pg_write_all_data TO ${runtime} WITH INHERIT TRUE, SET FALSE`, `REVOKE pg_write_all_data FROM ${runtime}`],
|
|
[`GRANT ${settable} TO ${runtime} WITH INHERIT FALSE, SET TRUE`, `REVOKE ${settable} FROM ${runtime}`],
|
|
[`GRANT ${runtime} TO ${settable}`, `REVOKE ${runtime} FROM ${settable}`],
|
|
[`GRANT TEMPORARY ON DATABASE postgres TO ${runtime}`, `REVOKE TEMPORARY ON DATABASE postgres FROM ${runtime}`],
|
|
[`GRANT CREATE ON SCHEMA mmapp_control TO ${runtime}`, `REVOKE CREATE ON SCHEMA mmapp_control FROM ${runtime}`],
|
|
[`GRANT SET ON PARAMETER session_replication_role TO ${runtime}`, `REVOKE SET ON PARAMETER session_replication_role FROM ${runtime}`],
|
|
[`ALTER TABLE public.runtime_privilege_fixture OWNER TO ${runtime}`,
|
|
`ALTER TABLE public.runtime_privilege_fixture OWNER TO ${transport.user};
|
|
GRANT SELECT, UPDATE ON public.runtime_privilege_fixture TO ${runtime}`],
|
|
]) {
|
|
await owner.query(grant)
|
|
try {
|
|
await withRuntime(async fresh => assert.rejects(
|
|
fresh.query('UPDATE public.runtime_privilege_fixture SET value=3'), runtimeRefused, grant,
|
|
))
|
|
assert.deepEqual((await owner.query('SELECT value FROM public.runtime_privilege_fixture')).rows, [{value:2}])
|
|
} finally { await owner.query(revoke) }
|
|
}
|
|
await withRuntime(async fresh => assert.deepEqual(
|
|
(await fresh.query('SELECT value FROM public.runtime_privilege_fixture')).rows, [{value:2}],
|
|
))
|
|
await withRuntime(async retained => {
|
|
await retained.query('SELECT 1')
|
|
assert.equal(retained.idleCount, 1)
|
|
await owner.query(`GRANT ${settable} TO ${runtime}`)
|
|
try {
|
|
await assert.rejects(retained.query('UPDATE public.runtime_privilege_fixture SET value=3'), runtimeRefused)
|
|
assert.equal(retained.idleCount, 0)
|
|
} finally { await owner.query(`REVOKE ${settable} FROM ${runtime}`) }
|
|
await retained.query('SELECT 1')
|
|
assert.equal(retained.idleCount, 1)
|
|
await owner.query(`GRANT TEMPORARY ON DATABASE postgres TO ${runtime}`)
|
|
try {
|
|
await assert.rejects(retained.connect(), runtimeRefused)
|
|
assert.equal(retained.idleCount, 0)
|
|
} finally { await owner.query(`REVOKE TEMPORARY ON DATABASE postgres FROM ${runtime}`) }
|
|
assert.deepEqual((await retained.query('SELECT value FROM public.runtime_privilege_fixture')).rows, [{value:2}])
|
|
})
|
|
console.log('Runtime roles: direct DML accepted; unsafe privilege drift refused on fresh and retained TLS connections before application SQL')
|
|
}
|
|
|
|
async function verifyRuntimeActivation(createPool, ownerPool, request) {
|
|
const {sourceBindingBytes} = await tsImport('../src/db/database-source-binding.ts', import.meta.url)
|
|
const {DATABASE_SOURCE_ACTIVATION_FILENAME} = await tsImport('../src/db/database-source-activation-record.ts', import.meta.url)
|
|
const directory = join(root, 'activation')
|
|
const file = join(directory, DATABASE_SOURCE_ACTIVATION_FILENAME), pending = `${file}.pending`
|
|
const state = {
|
|
format: 'database-source-activation-v1', activationDirectory: directory, installationId: request.installationId,
|
|
sourceIdentityId: randomUUID(), publicKeySpkiSha256: 'c'.repeat(64), status: 'active',
|
|
generations: [{generation: randomUUID(), headSha256: request.sourceHeadSha256, bindingSha256: 'b'.repeat(64),
|
|
tlsSpkiSha256: request.tls.spkiSha256}],
|
|
}
|
|
const active = sourceBindingBytes(state)
|
|
const revoked = sourceBindingBytes({...state, status:'revoked'})
|
|
const publish = async bytes => {
|
|
await writeFile(pending, bytes, {flag:'wx', mode:0o600})
|
|
await rename(pending, file)
|
|
}
|
|
await mkdir(directory, {mode:0o700})
|
|
await publish(active)
|
|
await ownerPool.query(`CREATE SEQUENCE mmapp_control.activation_query; GRANT USAGE ON mmapp_control.activation_query TO ${runtime}`)
|
|
const blockedSql = "SELECT nextval('mmapp_control.activation_query')"
|
|
const pidSql = 'SELECT pg_backend_pid() AS pid'
|
|
const directoryKey = 'DATABASE_RUNTIME_ACTIVATION_DIRECTORY', digestKey = 'DATABASE_RUNTIME_ACTIVATION_SHA256'
|
|
const previousDirectory = process.env[directoryKey], previousDigest = process.env[digestKey]
|
|
process.env[directoryKey] = directory
|
|
process.env[digestKey] = sha(active)
|
|
let pool
|
|
try {
|
|
pool = createPool(1)
|
|
const {rows:[{pid}]} = await pool.query(pidSql)
|
|
const retained = await pool.connect()
|
|
let queued
|
|
try {
|
|
assert.equal((await retained.query(pidSql)).rows[0].pid, pid)
|
|
queued = assert.rejects(pool.connect().then(client => {
|
|
client.release()
|
|
unexpectedConnection()
|
|
}), runtimeRefused)
|
|
await writeFile(pending, revoked, {flag:'wx', mode:0o600})
|
|
} finally { retained.release() }
|
|
await queued
|
|
await assert.rejects(pool.query(blockedSql), runtimeRefused)
|
|
await rm(pending)
|
|
assert.equal((await pool.query(pidSql)).rowCount, 1)
|
|
assert.equal(pool.idleCount, 1)
|
|
await publish(revoked)
|
|
await assert.rejects(pool.query(blockedSql), runtimeRefused)
|
|
await assert.rejects(pool.connect().then(client => {
|
|
client.release()
|
|
unexpectedConnection()
|
|
}), runtimeRefused)
|
|
const refuseFresh = async expectedDigest => {
|
|
process.env[digestKey] = expectedDigest
|
|
await assert.rejects(async () => {
|
|
const fresh = createPool(1)
|
|
try { await fresh.query(blockedSql) }
|
|
finally { await fresh.end() }
|
|
}, runtimeRefused)
|
|
}
|
|
await refuseFresh(sha(revoked))
|
|
await publish(active)
|
|
await refuseFresh('0'.repeat(64))
|
|
assert.equal((await pool.query(pidSql)).rowCount, 1)
|
|
const [generation] = state.generations
|
|
const newHead = 'd'.repeat(64), newSpki = 'e'.repeat(64)
|
|
for (const wrongState of [
|
|
{...state, installationId:randomUUID()},
|
|
{...state, generations:[{...generation, headSha256:newHead}]},
|
|
{...state, generations:[{...generation, tlsSpkiSha256:newSpki}]},
|
|
{...state, generations:[generation, {...generation, generation:randomUUID(), headSha256:newHead, tlsSpkiSha256:newSpki}]},
|
|
]) {
|
|
const wrong = sourceBindingBytes(wrongState)
|
|
await publish(wrong)
|
|
await refuseFresh(sha(wrong))
|
|
}
|
|
const {rows:[{is_called}]} = await ownerPool.query('SELECT is_called FROM mmapp_control.activation_query')
|
|
assert.equal(is_called, false, 'Refused application SQL reached PostgreSQL')
|
|
} finally {
|
|
if (previousDirectory === undefined) delete process.env[directoryKey]
|
|
else process.env[directoryKey] = previousDirectory
|
|
if (previousDigest === undefined) delete process.env[digestKey]
|
|
else process.env[digestKey] = previousDigest
|
|
await pool?.end()
|
|
}
|
|
console.log('Runtime activation: retained PostgreSQL client admitted under active pin; pending, revoked, digest and latest generation mismatches refused before application SQL')
|
|
}
|
|
|
|
async function verifyRestrictedStartup(ownerPool, validFiles) {
|
|
const sqlitePath = join(root, 'fixture.sqlite')
|
|
const runBackend = (args, env) => run(process.execPath, ['--import', import.meta.resolve('tsx'), ...args], {
|
|
cwd: root, timeout: 120_000, windowsHide: true, env: {
|
|
SystemRoot: process.env.SystemRoot, PATH: process.env.PATH, TEMP: process.env.TEMP, TMP: process.env.TMP,
|
|
NODE_ENV: 'test', DATABASE_PATH: sqlitePath, MCP_STATE_DIR: join(root, 'startup-state'), ...env,
|
|
},
|
|
})
|
|
const prepare = fileURLToPath(new URL('../src/scripts/prepare-database.ts', import.meta.url))
|
|
const ownerUrl = `postgresql://${transport.user}:${transport.password}@127.0.0.1:${transport.port}/${transport.database}`
|
|
await runBackend([prepare], {DATABASE_URL: ownerUrl})
|
|
await ownerPool.query(`REVOKE CREATE ON SCHEMA public FROM PUBLIC;
|
|
GRANT SELECT ON public.schema_migrations, public.traffic_settings, public.uptime_settings,
|
|
public.internet_path_settings, public.data_migration, public.servers TO ${runtime};
|
|
ALTER ROLE ${runtime} SET log_statement = 'all'`)
|
|
const {FIXED56_ROLE_TEMPLATE} = await tsImport('./writer-profile/fixed56-build-definition.ts', import.meta.url)
|
|
const guardFunction = name => FIXED56_ROLE_TEMPLATE.functions.find(row => row.namespace === 'mmapp_control' && row.proname === name)
|
|
const admissionGuard = guardFunction('candidate_require_preadmission')
|
|
const truncateGuard = guardFunction('candidate_truncate_preadmitted')
|
|
const operatorGuard = guardFunction('candidate_operator_ddl')
|
|
const trigger = FIXED56_ROLE_TEMPLATE.triggers.find(row => row.relname === 'mcp_operation_replay_tombstones' && row.tgname === 'candidate_truncate_preadmitted')
|
|
const eventTrigger = FIXED56_ROLE_TEMPLATE.events.find(row => row.evtname === 'candidate_operator_ddl')
|
|
assert.equal(trigger.tgenabled, 'A')
|
|
assert.equal(eventTrigger.evtenabled, 'O')
|
|
assert.equal(eventTrigger.evttags, null)
|
|
await ownerPool.query(`${admissionGuard.definition}; ${truncateGuard.definition}; ${trigger.definition};
|
|
ALTER TABLE public.mcp_operation_replay_tombstones ENABLE ALWAYS TRIGGER candidate_truncate_preadmitted;
|
|
${operatorGuard.definition.replace('@mm-role:operator@', 'tls_operator')};
|
|
CREATE EVENT TRIGGER ${eventTrigger.evtname} ON ${eventTrigger.evtevent} EXECUTE FUNCTION ${eventTrigger.function};
|
|
REVOKE ALL ON FUNCTION mmapp_control.candidate_require_preadmission(), mmapp_control.candidate_truncate_preadmitted(),
|
|
mmapp_control.candidate_operator_ddl() FROM PUBLIC`)
|
|
|
|
const databaseModule = new URL('../src/db/index.ts', import.meta.url).href
|
|
const bootstrapModule = new URL('../src/db/bootstrap.ts', import.meta.url).href
|
|
const runtimeEnv = {
|
|
DATABASE_URL: validFiles.databaseUrl, DATABASE_RUNTIME_REQUEST_FILE: validFiles.requestFile,
|
|
DATABASE_RUNTIME_REQUEST_SHA256: validFiles.requestSha256, DATABASE_RUNTIME_CA_FILE: validFiles.caFile,
|
|
}
|
|
const schemaRefused = 'DATABASE_RUNTIME_SCHEMA_REFUSED'
|
|
const startup = async (expectedError, schemaSha256) => {
|
|
const start = postgresLog.length
|
|
await runBackend(['--input-type=module', '--eval', `
|
|
import assert from 'node:assert/strict';
|
|
import {closePool, pool} from ${JSON.stringify(databaseModule)};
|
|
import {initDatabase} from ${JSON.stringify(bootstrapModule)};
|
|
try {
|
|
const {rows: [role]} = await pool.query(\`SELECT session_user::text AS login,
|
|
current_user::text AS principal, has_schema_privilege('public', 'CREATE') AS create_schema,
|
|
has_database_privilege(current_database(), 'CREATE,TEMP') AS create_database\`);
|
|
assert.deepEqual(role, {login:${JSON.stringify(runtime)}, principal:${JSON.stringify(runtime)}, create_schema:false, create_database:false});
|
|
const expected = process.argv[1];
|
|
if (expected === ${JSON.stringify(schemaRefused)}) await assert.rejects(initDatabase(), {code: expected});
|
|
else if (expected) await assert.rejects(initDatabase(), new RegExp(expected));
|
|
else await initDatabase();
|
|
} finally { await closePool(); }
|
|
`, expectedError ?? ''], {
|
|
...runtimeEnv,
|
|
...(schemaSha256 === undefined ? {} : {DATABASE_RUNTIME_SCHEMA_SHA256: schemaSha256}),
|
|
})
|
|
const statements = postgresLog.slice(start).join('').split(new RegExp(`\\[${runtime}\\] (?:LOG: +(?:statement|execute [^:]+)|STATEMENT):`, 'g')).slice(1)
|
|
const readsMigrationHistory = statements.some(statement => /^\s*SELECT id FROM public\.schema_migrations\b/i.test(statement))
|
|
assert.equal(readsMigrationHistory, expectedError !== schemaRefused, 'Migration history must be read only after the schema guard passes')
|
|
if (schemaSha256 !== undefined) {
|
|
assert(statements.some(statement => /^\s*BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY\b/i.test(statement)))
|
|
assert(statements.some(statement => /^\s*\/\* migration-schema:columns \*\//i.test(statement)))
|
|
}
|
|
if (expectedError === schemaRefused) {
|
|
assert(!statements.some(statement => /\bFROM\s+(?:public\.)?(?:data_migration|servers|traffic_settings|uptime_settings|internet_path_settings)\b/i.test(statement)), 'Refused startup must stop before application reads')
|
|
}
|
|
assert(!statements.some(statement => /^\s*(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|TRUNCATE|COPY|CALL|DO)\b/i.test(statement)), 'PostgreSQL observed no runtime DDL or data writes during startup')
|
|
}
|
|
|
|
const {captureRuntimeDatabaseSchema} = await tsImport('../src/db/database-runtime-schema.ts', import.meta.url)
|
|
const {withSystemDatabaseOperationLock} = await tsImport('../src/db/operation-lock.ts', import.meta.url)
|
|
const capture = () => withSystemDatabaseOperationLock(() => ownerPool.connect(), captureRuntimeDatabaseSchema)
|
|
const baseline = await capture()
|
|
const {format, serverVersion, sha256} = baseline
|
|
const inspect = fileURLToPath(new URL('../src/scripts/inspect-database-schema.ts', import.meta.url))
|
|
const {stdout} = await runBackend([inspect], runtimeEnv)
|
|
assert.deepEqual(JSON.parse(stdout), {format, schemaSha256: sha256, serverVersion})
|
|
await startup(undefined, sha256)
|
|
const migrations = () => ownerPool.query('SELECT id, applied_at FROM public.schema_migrations ORDER BY id').then(result => result.rows)
|
|
const before = await migrations()
|
|
await ownerPool.query('ALTER TABLE public.traffic_settings ALTER COLUMN interval_sec SET DEFAULT 31')
|
|
await startup(schemaRefused, sha256)
|
|
assert.deepEqual(await migrations(), before)
|
|
const {rows: [{column_default}]} = await ownerPool.query(`SELECT column_default FROM information_schema.columns
|
|
WHERE table_schema = 'public' AND table_name = 'traffic_settings' AND column_name = 'interval_sec'`)
|
|
assert.equal(column_default, '31')
|
|
await ownerPool.query('ALTER TABLE public.traffic_settings ALTER COLUMN interval_sec SET DEFAULT 30')
|
|
console.log('Restricted application startup: accepted the operator schema digest over TLS and refused changed column default before application reads without runtime DDL or data writes')
|
|
|
|
const publicRows = ({sections}) => Object.values(sections).map(rows => rows.filter(row => row.namespace === 'public'))
|
|
const assertControlDrift = async () => {
|
|
const changed = await capture()
|
|
assert.notEqual(changed.sha256, sha256)
|
|
assert.deepEqual(publicRows(changed), publicRows(baseline))
|
|
await startup(schemaRefused, sha256)
|
|
assert.deepEqual(await migrations(), before)
|
|
}
|
|
const driftedBody = admissionGuard.definition.replace('pg_advisory_xact_lock_shared(8723110::bigint)', 'pg_advisory_xact_lock_shared(8723111::bigint)')
|
|
assert.notEqual(driftedBody, admissionGuard.definition)
|
|
await ownerPool.query(driftedBody)
|
|
await assertControlDrift()
|
|
await ownerPool.query(admissionGuard.definition)
|
|
await ownerPool.query(`GRANT EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() TO ${runtime}`)
|
|
await assertControlDrift()
|
|
await ownerPool.query(`REVOKE EXECUTE ON FUNCTION mmapp_control.candidate_truncate_preadmitted() FROM ${runtime}`)
|
|
await ownerPool.query(`ALTER EVENT TRIGGER ${eventTrigger.evtname} DISABLE`)
|
|
await assertControlDrift()
|
|
await ownerPool.query(`ALTER EVENT TRIGGER ${eventTrigger.evtname} ENABLE`)
|
|
await startup(undefined, sha256)
|
|
console.log('Restricted application startup: refused control guard body, ACL and event trigger state drift with the public catalog and migration markers intact')
|
|
await startup()
|
|
const currentPartition = `flow_buckets_d_${new Date().toISOString().slice(0, 10).replaceAll('-', '')}`
|
|
const expiredPartition = 'flow_buckets_d_20000101'
|
|
const partitionExists = async name => (await ownerPool.query('SELECT to_regclass($1) IS NOT NULL AS present', [`public.${name}`])).rows[0].present
|
|
await ownerPool.query(`DROP TABLE public.${currentPartition}; CREATE TABLE public.${expiredPartition}
|
|
PARTITION OF public.flow_buckets FOR VALUES FROM ('2000-01-01T00:00:00Z') TO ('2000-01-02T00:00:00Z')`)
|
|
await startup(`Required PostgreSQL partition ${currentPartition} is absent or incompatible`)
|
|
assert.equal(await partitionExists(currentPartition), false)
|
|
|
|
const {rows: [migration]} = await ownerPool.query('DELETE FROM public.schema_migrations WHERE id = (SELECT max(id) FROM public.schema_migrations) RETURNING id, applied_at')
|
|
await startup('Версия схемы PostgreSQL не совпадает')
|
|
const maintain = () => runBackend([prepare, '--partitions-only'], {DATABASE_URL: ownerUrl})
|
|
await assert.rejects(maintain(), /Версия схемы PostgreSQL не совпадает/)
|
|
assert.equal(await partitionExists(currentPartition), false)
|
|
assert.equal(await partitionExists(expiredPartition), true)
|
|
assert.equal((await ownerPool.query('SELECT id FROM public.schema_migrations WHERE id=$1', [migration.id])).rowCount, 0)
|
|
await ownerPool.query('INSERT INTO public.schema_migrations (id, applied_at) VALUES ($1, $2)', [migration.id, migration.applied_at])
|
|
|
|
await writeFile(sqlitePath, 'SQLite format 3\0')
|
|
await maintain()
|
|
assert.equal(await partitionExists(currentPartition), true)
|
|
assert.equal(await partitionExists(expiredPartition), false)
|
|
assert.deepEqual(await migrations(), before)
|
|
await startup('оператор должен завершить импорт SQLite')
|
|
await rm(sqlitePath)
|
|
await startup()
|
|
console.log('Operator partition maintenance: missing migration refused before partition DDL; missing and expired partitions handled without migrations or SQLite import')
|
|
}
|
|
|
|
try {
|
|
await postgres.initialise()
|
|
const first = await issue(sourceTlsDir)
|
|
await postgres.start(); sourceStarted = true
|
|
const pin = {serverName:host, caSha256:sha(first), spkiSha256:fingerprint(first)}
|
|
const request = {tls:pin, roles:{publisher:{name:transport.user}}, source:{databaseName:transport.database}}
|
|
const baseline = await withClient(poolOptions(transport, request, first), async client => {
|
|
const retained = retainPublisherTlsPeer(client, pin)
|
|
retained()
|
|
const {rows:[source]} = await client.query(query)
|
|
const {rows:[status]} = await client.query('SELECT ssl FROM pg_stat_ssl WHERE pid=pg_backend_pid()')
|
|
assert.equal(status.ssl, true)
|
|
await client.query(`CREATE SCHEMA mmapp_control; GRANT USAGE ON SCHEMA mmapp_control TO PUBLIC;
|
|
CREATE TABLE mmapp_control.notes (note text PRIMARY KEY); INSERT INTO mmapp_control.notes VALUES ('original')`)
|
|
await client.query('REVOKE CREATE, TEMP ON DATABASE postgres FROM PUBLIC; REVOKE EXECUTE ON FUNCTION pg_catalog.pg_control_system() FROM PUBLIC')
|
|
source.runtimeOid = await createRuntime(client)
|
|
const {rows: [{server_version_num}]} = await client.query('SHOW server_version_num')
|
|
source.serverVersionNum = Number(server_version_num)
|
|
const connection = client.connection, socket = connection.stream
|
|
connection.stream = undefined
|
|
try { assert.throws(retained, /retained TLS peer unavailable/) } finally { connection.stream = socket }
|
|
return source
|
|
})
|
|
await withClient({...transport, ssl:false}, async client => assert.throws(() => retainPublisherTlsPeer(client,pin), /retained TLS peer unavailable/))
|
|
|
|
const runtimeRequest = {
|
|
format: 'application-writer-bootstrap-request-v1', installationId: randomUUID(), sourceHeadSha256: 'a'.repeat(64),
|
|
source: {systemIdentifier: baseline.system, databaseOid: baseline.oid, databaseName: transport.database,
|
|
serverVersionNum: baseline.serverVersionNum, inRecovery: false},
|
|
roles: {runtime: {name: runtime, signedOid: baseline.runtimeOid}, operator: {name: 'tls_operator', signedOid: '30000'},
|
|
capture: {name: 'tls_capture', signedOid: '30001'}, publisher: {name: 'tls_publisher', signedOid: '30002'},
|
|
control: {name: 'tls_control', signedOid: '30003'}},
|
|
tls: pin,
|
|
}
|
|
const validFiles = await runtimeFiles(runtimeRequest, first)
|
|
Object.assign(process.env, {DATABASE_URL: validFiles.databaseUrl, DATABASE_RUNTIME_REQUEST_FILE: validFiles.requestFile,
|
|
DATABASE_RUNTIME_REQUEST_SHA256: validFiles.requestSha256, DATABASE_RUNTIME_CA_FILE: validFiles.caFile})
|
|
const {pool: appPool, createPool} = await tsImport('../src/db/index.ts', import.meta.url)
|
|
const {readRuntimeDatabasePoolOptions} = await tsImport('../src/db/database-runtime-source.ts', import.meta.url)
|
|
try {
|
|
const {rows: [identity]} = await appPool.query(`SELECT session_user::text AS login, current_user::text AS principal,
|
|
(SELECT ssl FROM pg_catalog.pg_stat_ssl WHERE pid=pg_backend_pid()) AS tls,
|
|
pg_catalog.has_database_privilege(current_database(), 'CREATE,TEMP') AS can_create_or_temp`)
|
|
assert.deepEqual(identity, {login: runtime, principal: runtime, tls: true, can_create_or_temp: false})
|
|
await assert.rejects(appPool.query('SELECT * FROM mmapp_control.notes'), {code: '42501'})
|
|
} finally { await appPool.end() }
|
|
const ownerPool = new pg.Pool(poolOptions(transport, request, first))
|
|
try {
|
|
if (registryOnly) await verifyRegistryEnrollment(ownerPool, runtimeRequest, first)
|
|
else {
|
|
if (!schemaOnly && !activationOnly) await verifyRuntimeRolePrivileges(ownerPool, createPool)
|
|
if (!schemaOnly && !runtimeRoleOnly) await verifyRuntimeActivation(createPool, ownerPool, runtimeRequest)
|
|
if (!activationOnly && !runtimeRoleOnly) await verifyRestrictedStartup(ownerPool, validFiles)
|
|
}
|
|
} finally { await ownerPool.end() }
|
|
if (!activationOnly && !schemaOnly && !runtimeRoleOnly && !registryOnly) {
|
|
assert.throws(() => readRuntimeDatabasePoolOptions({...validFiles, databaseUrl: `${validFiles.databaseUrl}?sslmode=disable`}), runtimeRefused)
|
|
assert.throws(() => readRuntimeDatabasePoolOptions({...validFiles, requestSha256: '0'.repeat(64)}), runtimeRefused)
|
|
await withClient(poolOptions(transport, request, first), client => client.query(`REVOKE EXECUTE ON FUNCTION pg_catalog.pg_control_system() FROM ${runtime}`))
|
|
await assert.rejects(withClient(readRuntimeDatabasePoolOptions(validFiles), unexpectedConnection), runtimeRefused)
|
|
await withClient(poolOptions(transport, request, first), client => client.query(`GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${runtime}`))
|
|
const wrongDb = await runtimeFiles({...runtimeRequest, source: {...runtimeRequest.source, databaseOid: String(Number(baseline.oid) + 1)}}, first)
|
|
await assert.rejects(withClient(readRuntimeDatabasePoolOptions(wrongDb), unexpectedConnection), runtimeRefused)
|
|
await runtimeFiles(runtimeRequest, first)
|
|
await postgres.stop(); sourceStarted = false
|
|
await cp(databaseDir, cloneDir, {recursive:true, force:false, errorOnExist:true})
|
|
await assert.rejects(access(join(cloneDir, keyFile)), {code:'ENOENT'})
|
|
const second = await issue(cloneTlsDir)
|
|
assert.notEqual(fingerprint(second), pin.spkiSha256)
|
|
await postgres.start(); sourceStarted = true
|
|
await clone.start(); cloneStarted = true
|
|
const cloneTransport = {...transport, port:clonePortNumber}
|
|
const bothTrusted = Buffer.concat([first,second])
|
|
await assert.rejects(withClient(poolOptions(cloneTransport, {...request,tls:{...pin,caSha256:sha(bothTrusted)}}, bothTrusted),
|
|
() => assert.fail('Wrong physical cluster must not reach application SQL')), /TLS SPKI refused/)
|
|
await withClient({...cloneTransport, ssl:{ca:bothTrusted, servername:host, rejectUnauthorized:true}}, async client => {
|
|
assert.equal(client.connection.stream.authorized, true)
|
|
const {rows:[source]} = await client.query(query)
|
|
assert.deepEqual(source, {system: baseline.system, oid: baseline.oid})
|
|
assert.throws(() => retainPublisherTlsPeer(client,pin), /TLS SPKI refused/)
|
|
retainPublisherTlsPeer(client,{...pin,spkiSha256:fingerprint(second)})()
|
|
assert.deepEqual((await client.query('SELECT note FROM mmapp_control.notes')).rows, [{note:'original'}])
|
|
await client.query("INSERT INTO mmapp_control.notes VALUES ('clone-only')")
|
|
})
|
|
const cloneFiles = await runtimeFiles({...runtimeRequest, tls: {...pin, caSha256: sha(bothTrusted)}}, bothTrusted, clonePortNumber)
|
|
await assert.rejects(withClient(readRuntimeDatabasePoolOptions(cloneFiles), unexpectedConnection), runtimeRefused)
|
|
await runtimeFiles(runtimeRequest, first)
|
|
await withClient(poolOptions(transport, request, first), async client => {
|
|
retainPublisherTlsPeer(client,pin)()
|
|
assert.deepEqual((await client.query('SELECT note FROM mmapp_control.notes')).rows, [{note:'original'}])
|
|
await client.query(`DROP OWNED BY ${runtime}; DROP ROLE ${runtime}`)
|
|
assert.notEqual(await createRuntime(client), baseline.runtimeOid)
|
|
})
|
|
const freshAppPool = createPool(1)
|
|
try { await assert.rejects(freshAppPool.query('SELECT 1'), runtimeRefused) }
|
|
finally { await freshAppPool.end() }
|
|
console.log('Genuine PostgreSQL TLS: original accepted; independently writable physical clone with identical DB coordinates and a different CA-valid TLS key refused')
|
|
console.log('Runtime pool: restricted login accepted; TLS override, incorrect request digest, missing observation grant, wrong database OID, clone key and recreated role refused')
|
|
}
|
|
} finally {
|
|
const stops = await Promise.allSettled([
|
|
cloneStarted ? clone.stop() : undefined,
|
|
sourceStarted ? postgres.stop() : undefined,
|
|
])
|
|
assert(stops.every(result => result.status === 'fulfilled'), 'Owned cluster stop unconfirmed')
|
|
assert.equal(dirname(resolve(root)), resolve(tmpdir()))
|
|
assert.match(root, /mm-writer-tls-owned-/)
|
|
await rm(root, {recursive:true, force:true, maxRetries:20, retryDelay:100})
|
|
await assert.rejects(access(root), {code:'ENOENT'})
|
|
console.log('Owned PostgreSQL and temporary TLS material removed')
|
|
}
|