Files
mtusi2026lab6/scripts/render-openobserve-credentials.ts

6 lines
1.7 KiB
TypeScript

import {readFile,writeFile} from 'node:fs/promises';import {resolve} from 'node:path';
const root=resolve(import.meta.dir,'..');if(process.argv.includes('--dry-run')){console.log('Validated dedicated native service-account renderer; no credential reads or writes.');process.exit(0);}
const accounts=JSON.parse(await readFile(resolve(root,'.local/openobserve-accounts.json'),'utf8'));for(const label of ['query','ingest']){const a=accounts[label];if(a.email!=='vp6-'+label+'@lab.invalid'||typeof a.token!=='string'||!/^[A-Za-z0-9_-]{16,256}$/.test(a.token))throw new Error('native service-account shape outside supported credentials');}
async function preserveOrCreate(name:string,content:string,mode:number){const path=resolve(root,".local",name);let previous:string|undefined;try{previous=await readFile(path,"utf8")}catch(error){if((error as any).code!=="ENOENT")throw error;}if(previous!==undefined){if(previous!==content)throw new Error("Existing dedicated credential differs; preserve and perform an explicit rotation");return;}await writeFile(path,content,{flag:"wx",mode});}
const ingest='Basic '+Buffer.from(accounts.ingest.email+':'+accounts.ingest.token).toString('base64');await preserveOrCreate('openobserve_sa_ingest_auth',ingest,0o644);await preserveOrCreate('openobserve_query_users.cfg','userlist query_users\n user '+accounts.query.email+' insecure-password '+accounts.query.token+'\n',0o644);await preserveOrCreate("grafana-openobserve.env","O2_QUERY_USER="+accounts.query.email+"\nO2_QUERY_PASSWORD="+accounts.query.token+"\n",0o600);console.log(JSON.stringify({rendered:['openobserve_sa_ingest_auth','openobserve_query_users.cfg'],credentialValuesPrinted:false,ingestHeaderHasCRLF:false,queryRole:'service_account',ownerCredentialUsed:false}));