forked from mtusi2026/mtusi2026lab6
37 lines
2.4 KiB
TypeScript
37 lines
2.4 KiB
TypeScript
import { mkdir, writeFile, chmod, access } from 'node:fs/promises';
|
|
import { resolve } from 'node:path';
|
|
|
|
const root = resolve(import.meta.dir, '..');
|
|
const local = resolve(root, '.local');
|
|
const files = ['pg_password', 'database_url', 'cache_url', 'valkey.conf'];
|
|
const present = await Promise.all(files.map(async name => {try {await access(resolve(local, name));return true;} catch {return false;}}));
|
|
if (present.some(Boolean)) {
|
|
if (!present.every(Boolean)) throw new Error('Incomplete local configuration; preserve it and investigate before bootstrap');
|
|
console.log('Local configuration exists');
|
|
} else {
|
|
await mkdir(local, { recursive: true, mode: 0o700 });
|
|
const pg = crypto.randomUUID().replaceAll('-', '') + crypto.randomUUID().replaceAll('-', '');
|
|
const cache = crypto.randomUUID().replaceAll('-', '');
|
|
const values = [pg, `postgres://messenger:${pg}@postgres:5432/messenger?sslmode=disable`, `redis://:${cache}@valkey:6379/0`, `bind 0.0.0.0\nprotected-mode yes\nrequirepass ${cache}\nappendonly yes\nappendfsync always\ndir /data\n`];
|
|
for (let i = 0; i < files.length; i++) {
|
|
const destination = resolve(local, files[i]);
|
|
await writeFile(destination, values[i] + '\n', { mode: 0o600, flag: 'wx' });
|
|
await chmod(destination, 0o644); // Compose secrets must be readable by nonroot containers; parent is 0700.
|
|
}
|
|
console.log('Created local synthetic stand credentials');
|
|
}
|
|
|
|
const s3Files=['s3_access','s3_secret','s3_root','s3_credentials'];
|
|
const existing=await Promise.all(s3Files.map(async name=>{try{await access(resolve(local,name));return true;}catch{return false;}}));
|
|
if(existing.some(Boolean)&&!existing.every(Boolean))throw new Error('Incomplete S3 configuration; preserve it before investigation');
|
|
if(!existing.some(Boolean)){
|
|
const accessKey='root_'+crypto.randomUUID().replaceAll('-','');
|
|
const rootSecret=crypto.randomUUID().replaceAll('-','')+crypto.randomUUID().replaceAll('-','');
|
|
const appSecret=crypto.randomUUID().replaceAll('-','')+crypto.randomUUID().replaceAll('-','');
|
|
const common={Endpoint:'s3:9000',Bucket:'messenger'};
|
|
const values=[accessKey,rootSecret,JSON.stringify({...common,Access:accessKey,Secret:rootSecret}),JSON.stringify({...common,Access:'messenger-api',Secret:appSecret})];
|
|
for(let i=0;i<s3Files.length;i++)await writeFile(resolve(local,s3Files[i]),values[i]+'\n',{mode:0o644,flag:'wx'});
|
|
console.log('Created separate S3 root and scoped application credentials');
|
|
}
|
|
|