forked from mtusi2026/mtusi2026lab7
ЛР 7 — БПИ2304 — Кирюпина — на 3
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
// Certificates are bootstrap identity. KV credentials stay only in process memory.
|
||||
const https=require('node:https');
|
||||
const tls=require('./tls');
|
||||
async function bao(path,data,token) {
|
||||
return new Promise((resolve,reject)=>{
|
||||
const req=https.request('https://bao:8200/v1/'+path,{...tls,agent:tls.agent,method:data?'POST':'GET',headers:{'Content-Type':'application/json',...(token?{'X-Vault-Token':token}:{})},timeout:5000},res=>{
|
||||
let body='';res.on('data',b=>body+=b);res.on('end',()=>{if(res.statusCode!==200)return reject(Error('Secret service unavailable'));try{resolve(JSON.parse(body));}catch{reject(Error('Secret response invalid'));}});
|
||||
});req.on('timeout',()=>req.destroy(Error('Secret timeout')));req.on('error',()=>reject(Error('Secret connection failed')));req.end(data?JSON.stringify(data):undefined);
|
||||
});
|
||||
}
|
||||
async function main(){
|
||||
const role=process.env.SERVICE_ROLE;
|
||||
const auth=(await bao('auth/cert/login',{name:role})).auth;
|
||||
if(auth.policies.includes('root'))throw Error('Root policy forbidden');
|
||||
const credentials=(await bao('secret/data/'+role,null,auth.client_token)).data.data;
|
||||
process.env.DATABASE_URL=credentials.database_url;
|
||||
process.env.BROKER_URL=credentials.broker_url;
|
||||
process.env.S3_ACCESS_KEY_ID=credentials.s3_access_key;
|
||||
process.env.S3_SECRET_ACCESS_KEY=credentials.s3_secret_key;
|
||||
// No token needed after startup; explicitly revoke the short-lived token.
|
||||
await bao('auth/token/revoke-self',{},auth.client_token).catch(()=>{});
|
||||
require(role==='worker'?'./worker':'./server');
|
||||
}
|
||||
if(require.main===module)main().catch(()=>{console.error('Service bootstrap failed: check OpenBao readiness and service policy');process.exit(1);});
|
||||
module.exports={bao};
|
||||
Reference in New Issue
Block a user