Files
mtusi2026lab6/scripts/lib/openobserve-bootstrap.ts
T

36 lines
3.7 KiB
TypeScript

export type AccountKey = 'ingest' | 'query';
export type Account = { email: string; token: string };
export type Intent = { email: string; rawFile: string; captureId: string };
export type Capability = { role: string; searchHTTP: number; searchHasRows: boolean; ingestHTTP: number; adminRotateHTTP: number };
export type CreateResult = { status: number | null; body: any };
export type BootstrapPorts = {
readIndex(): Promise<Record<AccountKey, Account> | undefined>;
readAccount(key: AccountKey): Promise<Account | undefined>;
readIntent(key: AccountKey): Promise<Intent | undefined>;
makeIntent(key: AccountKey): Intent;
claimIntent(key: AccountKey, intent: Intent): Promise<void>;
readCaptured(intent: Intent): Promise<CreateResult | undefined>;
create(key: AccountKey, intent: Intent): Promise<CreateResult>;
saveAccount(key: AccountKey, account: Account): Promise<void>;
verify(key: AccountKey, account: Account): Promise<Capability>;
publishIndex(accounts: Record<AccountKey, Account>): Promise<void>;
publishDerived(accounts: Record<AccountKey, Account>): Promise<void>;
};
export const accountEmail = (key: AccountKey) => 'vp6-' + key + '@lab.invalid';
function candidate(key:AccountKey,response:CreateResult):Account{if(![200,null].includes(response.status)||response.body?.code!==200||response.body?.user!==accountEmail(key)||typeof response.body?.token!=="string"||!/^[A-Za-z0-9_-]{16,256}$/.test(response.body.token))throw new Error("Captured native account response is not recoverable; no POST retry");return{email:response.body.user,token:response.body.token};}
function validateAccount(key:AccountKey,account:Account){if(account?.email!==accountEmail(key)||typeof account?.token!=="string"||!/^[A-Za-z0-9_-]{16,256}$/.test(account.token))throw new Error("Private account checkpoint invalid; no POST retry");}
export function createAccountBootstrap(ports:BootstrapPorts){
const names:(keyof BootstrapPorts)[]=["readIndex","readAccount","readIntent","makeIntent","claimIntent","readCaptured","create","saveAccount","verify","publishIndex","publishDerived"];if(names.some(name=>typeof ports?.[name]!=="function"))throw new Error("All bootstrap ports are required");
return async()=>{const previous=await ports.readIndex(),accounts={} as Record<AccountKey,Account>,proofs=[];
for(const key of ["ingest","query"] as const){let account=await ports.readAccount(key)??previous?.[key];
if(!account){const retained=await ports.readIntent(key);if(retained){if(retained.email!==accountEmail(key))throw new Error("Private account intent identity mismatch");const captured=await ports.readCaptured(retained);if(!captured)throw new Error("Uncertain account creation retained; inspect captured bytes, never repeat POST automatically");account=candidate(key,captured);}else{const intent=ports.makeIntent(key);if(intent.email!==accountEmail(key))throw new Error("New account intent identity mismatch");await ports.claimIntent(key,intent);const created=await ports.create(key,intent);if(created.status!==200)throw new Error("Native account create was not accepted; retained intent prevents automatic retry");account=candidate(key,created);}}
validateAccount(key,account);
// A received credential is durable before the next create, including when its live probe fails.
await ports.saveAccount(key,account);
const proof=await ports.verify(key,account);if(proof.role!=="service_account"||proof.searchHTTP!==200||!proof.searchHasRows||proof.ingestHTTP!==200||proof.adminRotateHTTP!==403)throw new Error("Native account capability verification failed; derived credentials/start are not published");
accounts[key]=account;proofs.push({principal:key,...proof});
}
await ports.publishIndex(accounts);await ports.publishDerived(accounts);return{accounts,proofs};
};
}