Merge pull request 'Fix character provisioning with production database handles and release links' (#432) from fix/system-character-cli-20261120 into main
All checks were successful
DuneRealms Web CI / web (push) Successful in 3m11s

This commit is contained in:
unimatrix 2026-11-20 18:49:46 +00:00
commit 01164bbdf9
2 changed files with 19 additions and 4 deletions

View file

@ -1,13 +1,13 @@
// Server operator only. Uses the ordinary JARVIS identity adapter; no browser session.
import {fileURLToPath} from 'node:url';
import {resolve} from 'node:path';
import {realpathSync} from 'node:fs';
import {openDatabase} from '../node/mysql.mjs';
import {openOrganisationKeyService} from '../node/organisation-key-service.mjs';
import {createSystemCharacters} from '../node/system-characters.mjs';
import {SYSTEM_CHARACTERS} from '../node/system-character-catalogue.mjs';
export async function currentSystemCharacterOperator(actor,db){
const row=await db.first("SELECT p.principal_kind FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND a.password_hash NOT LIKE '!%'",[actor?.id||'']);
return row?.principal_kind==='owner';
const rows=await db.query("SELECT p.principal_kind FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND a.password_hash NOT LIKE '!%'",[actor?.id||'']);
return rows[0]?.principal_kind==='owner';
}
export async function provisionCharacters(db,keys,{actorId,apply=false}={}){
const actor={id:actorId};if(!await currentSystemCharacterOperator(actor,db))throw Error('Current owner authority required');
@ -17,7 +17,7 @@ export async function provisionCharacters(db,keys,{actorId,apply=false}={}){
}
return {applied:apply,count:results.length,accounts:results};
}
if(process.argv[1]&&fileURLToPath(import.meta.url)===resolve(process.argv[1])){
if(process.argv[1]&&fileURLToPath(import.meta.url)===realpathSync(process.argv[1])){
if(process.getuid?.()!==0)throw Error('Server administrator required');
const db=await openDatabase(process.env);
try{const keys=await openOrganisationKeyService(db,process.env);console.log(JSON.stringify(await provisionCharacters(db,keys,{actorId:process.argv.find(a=>a.startsWith('--actor='))?.slice(8),apply:process.argv.includes('--apply')})));}finally{await db.close();}

View file

@ -42,3 +42,18 @@ test('revoked or modified identity keys fail closed without restoring capabiliti
}finally{f.db.close();}
});
// Production exposes query/prepare on its root handle, first only on transactions.
test('operator provisioning works with the production root database interface',async()=>{
const f=await fixture();try{
f.db.sqlite.exec('CREATE TABLE org_control_principals(user_id TEXT PRIMARY KEY,principal_kind TEXT)');
await f.db.query("INSERT INTO org_control_principals VALUES(?,'owner')",[f.people.Owner.id]);
const {provisionCharacters}=await import('../scripts/provision-system-characters.mjs');
const root={query:f.db.query.bind(f.db),transaction:f.db.transaction.bind(f.db)};
const result=await provisionCharacters(root,f.keys,{actorId:f.people.Owner.id,apply:true});
assert.equal(result.count,SYSTEM_CHARACTERS.length);assert.ok(result.accounts.every(a=>a.created));
assert.ok((await provisionCharacters(root,f.keys,{actorId:f.people.Owner.id})).accounts.every(a=>a.existing));
await f.db.query('DELETE FROM org_control_principals');
await assert.rejects(provisionCharacters(root,f.keys,{actorId:f.people.Owner.id,apply:true}),/authority/);
}finally{f.db.close();}
});