Manage shared ARNavi VPS contours and application keys from infrastructure
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
// Bounded SQL qualification, not a fleet simulator or a production DB connection.
|
||||
// npm install --prefix /private/tmp/ndc-b2-sql-check --ignore-scripts --save-exact @electric-sql/pglite@0.5.8
|
||||
// node scripts/qualify-b2-foundation.mjs /private/tmp/ndc-b2-sql-check/node_modules/@electric-sql/pglite/dist/index.js /private/tmp/b2-schema-proof.json
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile, readdir, writeFile, mkdtemp, rm } from 'node:fs/promises';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
import { resolve } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { B2Infrastructure } from '../services/device-control-core/src/b2/infrastructure.mjs';
|
||||
import { B2Secrets } from '../services/device-control-core/src/b2/secrets.mjs';
|
||||
const root = fileURLToPath(new URL('../', import.meta.url));
|
||||
const { PGlite } = await import(pathToFileURL(resolve(process.argv[2])));
|
||||
const db = new PGlite();
|
||||
try {
|
||||
const directory = resolve(root, 'services/device-control-core/migrations');
|
||||
const migrations = (await readdir(directory)).filter((name) => name.endsWith('.sql')).sort();
|
||||
for (const name of migrations) await db.exec(await readFile(resolve(directory, name), 'utf8'));
|
||||
assert.equal(migrations.length, 17);
|
||||
await db.exec(await readFile(resolve(root, 'deployment/device-b2-foundation-v1.sql'), 'utf8'));
|
||||
const ownerA = '11111111-1111-4111-8111-111111111111'; const ownerB = '22222222-2222-4222-8222-222222222222';
|
||||
const projectA = '33333333-3333-4333-8333-333333333333'; const projectB = '44444444-4444-4444-8444-444444444444';
|
||||
const source = '55555555-5555-4555-8555-555555555555'; const host = '66666666-6666-4666-8666-666666666666';
|
||||
for (const [owner, project] of [[ownerA, projectA], [ownerB, projectB]]) {
|
||||
await db.query('insert into device_owner_scopes(id,scope_kind,owner_ref,display_name,created_by_ref) values($1,$2,$3,$4,$5)', [owner,'company','company:'+owner,'SQL fixture','user:fixture']);
|
||||
await db.query('insert into device_projects(id,owner_scope_id,project_key,name,created_by_ref) values($1,$2,$3,$4,$5)', [project,owner,'fixture-project','SQL fixture','user:fixture']);
|
||||
}
|
||||
await db.query(`insert into device_b2_hosts(id,owner_scope_id,remote_host_id,endpoint,fingerprint,integration_id,authority,secret_ref)
|
||||
values($1,$2,$3,$4,$5,$6,$7,$8)`, [source,ownerA,'fixture-vps','https://vps.invalid','a'.repeat(64),'b'.repeat(32),'host','b2-secret:fixture']);
|
||||
await db.query(`insert into device_infrastructure_hosts(id,owner_scope_id,project_id,host_key,display_name,created_by_ref)
|
||||
values($1,$2,$3,$4,$5,$6)`, [host,ownerA,projectA,'fixture-host','SQL fixture','user:fixture']);
|
||||
await db.query('insert into device_b2_host_views(host_id,project_id,owner_scope_id,source_id) values($1,$2,$3,$4)', [host,projectA,ownerA,source]);
|
||||
await assert.rejects(db.query('insert into device_b2_contour_links values($1,$2,$3,$4,$5,now())', [source,'c'.repeat(32),projectB,ownerA,'user:fixture']), (error) => error.code === '23503');
|
||||
await db.query('insert into device_b2_contour_links values($1,$2,$3,$4,$5,now())', [source,'c'.repeat(32),projectA,ownerA,'user:fixture']);
|
||||
await assert.rejects(db.query('insert into device_b2_contour_links values($1,$2,$3,$4,$5,now())', [source,'c'.repeat(32),projectA,ownerA,'user:fixture']), (error) => error.code === '23505');
|
||||
// Execute the actual domain repository against PostgreSQL, not mocked SQL.
|
||||
const projectA2 = '77777777-7777-4777-8777-777777777777';
|
||||
await db.query('insert into device_projects(id,owner_scope_id,project_key,name,created_by_ref) values($1,$2,$3,$4,$5)', [projectA2,ownerA,'second-project','Second SQL fixture','user:fixture']);
|
||||
for (const project of [projectA,projectA2,projectB]) await db.query(
|
||||
'insert into device_project_grants(id,project_id,principal_kind,principal_ref,project_role,created_by_ref) values($1,$2,$3,$4,$5,$6)',
|
||||
[randomUUID(),project,'user','user:fixture','owner','user:fixture']);
|
||||
const secretRoot = await mkdtemp(resolve(tmpdir(),'b2-sql-secrets-'));
|
||||
try {
|
||||
const packageValue = { schema:'nodedc.b2-connection/v2',endpoint:'https://vps.invalid',host_id:'fixture-vps',
|
||||
fingerprint:'a'.repeat(64),integration_id:'b'.repeat(32),authority:'host',issuer:'https://vps.invalid/',
|
||||
audience:'https://vps.invalid/v2',scope:'b2',client_id:'b2_fixture',client_secret:'synthetic-sql-test-secret' };
|
||||
const remote = { async call(path) {
|
||||
if (path==='/v2/identity') return {host_id:packageValue.host_id};
|
||||
if (path==='/v2/management') return {host_id:packageValue.host_id,contours:[],applications:[]};
|
||||
if (path==='/v2/contours') return {items:[{id:'d'.repeat(32)}]};
|
||||
if (path.endsWith('/devices')) return {items:[]};
|
||||
throw new Error('unexpected test remote operation');
|
||||
} };
|
||||
const service = new B2Infrastructure({ pool:{connect:async()=>({query:(...args)=>db.query(...args),release(){}})},
|
||||
secrets:new B2Secrets(secretRoot),clientFactory:()=>remote });
|
||||
const actor = {userRef:'user:fixture',hubRole:'owner',groupRefs:[],ownerScopes:[{scopeKind:'company',ownerRef:'company:'+ownerA}]};
|
||||
const common = {projectRef:'project:'+projectA,hostRef:'host:'+host};
|
||||
const connect = await service.execute(actor,{...common,action:'connect',connection:packageValue},'a'.repeat(32));
|
||||
assert.equal(connect.connected,true);
|
||||
await service.execute(actor,{...common,action:'link',contourId:'d'.repeat(32),targetProjectRef:'project:'+projectA2},'b'.repeat(32));
|
||||
const linkedViews = (await db.query('select * from device_b2_host_views where source_id=$1',[source])).rows;
|
||||
assert.equal(linkedViews.length,2);
|
||||
assert.equal(linkedViews.filter((view)=>view.project_id===projectA2).length,1);
|
||||
await assert.rejects(service.execute(actor,{...common,action:'link',contourId:'d'.repeat(32),targetProjectRef:'project:'+projectB},'c'.repeat(32)), /b2_company_mismatch/);
|
||||
const stored = (await db.query('select secret_ref from device_b2_hosts where id=$1',[source])).rows[0];
|
||||
assert.match(stored.secret_ref,/^b2-secret:/);
|
||||
const audit = JSON.stringify((await db.query('select payload from device_audit_events')).rows);
|
||||
assert.equal(audit.includes(packageValue.client_secret),false);
|
||||
} finally { await rm(secretRoot,{recursive:true,force:true}); }
|
||||
const schema = (await db.query(`select c.relname as name,obj_description(c.oid) as marker,
|
||||
(select count(*)::int from pg_attribute a where a.attrelid=c.oid and a.attnum>0 and not a.attisdropped) as columns,
|
||||
(select count(*)::int from pg_constraint k where k.conrelid=c.oid and k.contype <> 'n') as constraints,
|
||||
(select bool_and(k.convalidated) from pg_constraint k where k.conrelid=c.oid and k.contype <> 'n') as validated,
|
||||
(select json_agg(json_build_array(a.attname,format_type(a.atttypid,a.atttypmod),a.attnotnull) order by a.attnum)
|
||||
from pg_attribute a where a.attrelid=c.oid and a.attnum>0 and not a.attisdropped) as fields,
|
||||
(select json_agg(pg_get_constraintdef(k.oid) order by pg_get_constraintdef(k.oid)) from pg_constraint k where k.conrelid=c.oid and k.contype <> 'n') as definitions
|
||||
from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname='public'
|
||||
and c.relname in ('device_b2_hosts','device_b2_host_views','device_b2_contour_links') order by c.relname`)).rows;
|
||||
if (process.argv[3]) await writeFile(process.argv[3],JSON.stringify(schema,null,2)+'\n');
|
||||
console.log(JSON.stringify({ ok:true,engine:'PGlite 0.5.8 PostgreSQL WASM',migrations:migrations.length,
|
||||
checks:['existing schema compatibility','new tables','same-company link','cross-company FK denial','one-contour-one-project uniqueness','actual connect/link repository','private key storage','shared host view in second project','audit excludes secrets'],
|
||||
productionAcceptance:false,rows:schema.map(({name,columns,constraints})=>({name,columns,constraints})) }));
|
||||
} finally { await db.close(); }
|
||||
Reference in New Issue
Block a user