forked from mtusi2026/mtusi2026lab6
9 lines
2.1 KiB
TypeScript
9 lines
2.1 KiB
TypeScript
import{mkdir,readFile,writeFile}from'node:fs/promises';import{resolve}from'node:path';import{createGrafanaCredentialBootstrap}from'./lib/grafana-credential';
|
|
if(process.argv.includes('--check')){console.log('Grafana credential entrypoint validated; no credential reads or mutations');process.exit(0);}
|
|
if(process.platform!=='linux')throw new Error('Run the stand credential bootstrap on its owned Linux host');
|
|
const root=resolve(import.meta.dir,'..'),local=resolve(root,'.local'),passwordFile=resolve(local,'grafana_password');await mkdir(local,{recursive:true,mode:0o700});
|
|
function run(args:string[],input?:Uint8Array){const result=Bun.spawnSync(args,{stdin:input,stdout:'pipe',stderr:'pipe'});if(result.exitCode!==0)throw new Error('Grafana encrypted credential archival failed');return result.stdout;}
|
|
let archiveDirectory='';
|
|
await createGrafanaCredentialBootstrap({read:async()=>{try{return(await readFile(passwordFile,'utf8')).trim()}catch(e){if(e.code==='ENOENT')return undefined;throw e}},generate:()=> 'Aa1!'+crypto.randomUUID()+crypto.randomUUID(),archive:async value=>{archiveDirectory=resolve(local,'grafana-owner-archive-'+crypto.randomUUID());await mkdir(archiveDirectory,{mode:0o700});const key=resolve(archiveDirectory,'identity.agekey');run(['/home/rey/.local/bin/age-keygen','-o',key]);const plaintext=Buffer.from(value),encrypted=run(['/home/rey/.local/bin/age','--encrypt','-i',key],plaintext);const target=resolve(archiveDirectory,'grafana-owner.age');await writeFile(target,encrypted,{flag:'wx',mode:0o600});const restored=run(['/home/rey/.local/bin/age','--decrypt','-i',key,target]);if(!Buffer.from(restored).equals(plaintext))throw new Error('Grafana archive roundtrip mismatch');await writeFile(resolve(archiveDirectory,'RESULT.json'),JSON.stringify({checkedAt:new Date().toISOString(),encryptedBeforeRuntimePublish:true,roundtripVerified:true,passwordSHA256:new Bun.CryptoHasher('sha256').update(plaintext).digest('hex')})+'\n',{flag:'wx',mode:0o600});},publish:async value=>{await writeFile(passwordFile,value,{flag:'wx',mode:0o644})}})();
|
|
console.log(JSON.stringify({grafanaOwnerPreservedOrCreated:true,encryptedArchiveVerified:true,archiveDirectory}));
|