Publish minimal Guild report and court calendar storage grants

This commit is contained in:
Dev 2026-11-25 02:31:19 +01:00
commit a596b604d9
2 changed files with 19 additions and 0 deletions

View file

@ -0,0 +1,13 @@
import {fileURLToPath} from 'node:url';import {resolve} from 'node:path';import {execFileSync} from 'node:child_process';
import {openDatabase} from '../node/mysql.mjs';import {openPostbankDatabase} from '../node/postbank-db.mjs';
export async function politicalServiceGrantPlan(db,bank){
const sql='SELECT DATABASE() db,@@hostname host,@@port port,@@server_id server_id',a=(await db.query(sql))[0],b=await bank?.first(sql);
if(!a||!b||['host','port','server_id'].some(k=>String(a[k])!==String(b[k]))||!/^[a-z][a-z0-9_]{2,63}$/.test(b.db||''))throw Error('One transactional MySQL server required');
const identity=(await db.query('SELECT CURRENT_USER() identity'))[0]?.identity?.split('@');if(identity?.length!==2||!identity.every(v=>/^[a-zA-Z0-9_.%:-]+$/.test(v)))throw Error('Unexpected central identity');
const target="'"+identity[0]+"'@'"+identity[1]+"'";
return [['SELECT','pb_guild_passages'],['SELECT,INSERT','jarvis_events'],['SELECT,INSERT','jarvis_event_history']].map(([rights,table])=>'GRANT '+rights+' ON `'+b.db+'`.`'+table+'` TO '+target+';\n').join('');
}
if(process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url)){
const apply=process.argv.slice(2).join(' ')==='--apply';if(process.argv.length>2&&!apply)throw Error('Use no arguments to review, or --apply');
const db=await openDatabase(process.env),bank=await openPostbankDatabase(process.env);try{const sql=await politicalServiceGrantPlan(db,bank);if(apply){if(process.getuid?.()!==0)throw Error('Apply through the privileged deployment operator');execFileSync('mysql',['--batch'],{input:sql,stdio:['pipe','pipe','pipe']});console.log('Applied three scoped political service grants. No business records or balances changed.');}else process.stdout.write(sql);}finally{await Promise.allSettled([db.close(),bank?.close()]);}
}

View file

@ -0,0 +1,6 @@
import test from 'node:test';import assert from 'node:assert/strict';import{politicalServiceGrantPlan}from'../scripts/political-service-grants.mjs';
test('political services request only Guild read and calendar read/insert on the verified bank server',async()=>{
const identity={db:'dune_site',host:'central',port:3306,server_id:1},db={query:async sql=>[sql.includes('CURRENT_USER')?{identity:'dune_app@localhost'}:identity]},bank={first:async()=>({...identity,db:'dune_bank'})};
const plan=await politicalServiceGrantPlan(db,bank);assert.equal(plan,"GRANT SELECT ON `dune_bank`.`pb_guild_passages` TO 'dune_app'@'localhost';\nGRANT SELECT,INSERT ON `dune_bank`.`jarvis_events` TO 'dune_app'@'localhost';\nGRANT SELECT,INSERT ON `dune_bank`.`jarvis_event_history` TO 'dune_app'@'localhost';\n");assert.doesNotMatch(plan,/UPDATE|DELETE|DROP|ALL PRIVILEGES|pb_accounts/);
await assert.rejects(politicalServiceGrantPlan(db,{first:async()=>({...identity,host:'foreign',db:'dune_bank'})}),/One transactional/);await assert.rejects(politicalServiceGrantPlan(db,{first:async()=>({...identity,db:'bank`;DROP'})}),/One transactional/);await assert.rejects(politicalServiceGrantPlan({query:async sql=>[sql.includes('CURRENT_USER')?{identity:"app'@localhost"}:identity]},bank),/identity/);
});