Files
mtusi2026lab6/student-tests/openobserve-network.ts
T

7 lines
2.3 KiB
TypeScript

import{writeFile}from'node:fs/promises';
const phase=process.env.VP_NETWORK_PHASE;if(!['before','after'].includes(phase??''))throw new Error('explicit owned network phase required');const user=process.env.O2_QUERY_USER,password=process.env.O2_QUERY_PASSWORD,rawIP=process.env.VP_RAW_OO_IP,gateway=process.env.VP_HOST_GATEWAY;if(!user||!password||!/^\d+\.\d+\.\d+\.\d+$/.test(rawIP??'')||!/^\d+\.\d+\.\d+\.\d+$/.test(gateway??''))throw new Error('owned query principal/network metadata missing');const auth='Basic '+Buffer.from(user+':'+password).toString('base64');
async function request(name:string,url:string,body?:unknown){let status=0,rows=false;try{const r=await fetch(url,{method:body?'POST':'GET',headers:{Authorization:auth,'Content-Type':'application/json'},body:body?JSON.stringify(body):undefined,signal:AbortSignal.timeout(2000)});status=r.status;const text=await r.text();try{const parsed=JSON.parse(text);rows=Array.isArray(parsed.hits)&&parsed.hits.length>0}catch{}}catch{}return{name,httpStatus:status,hasRows:rows};}
const now=Date.now()*1000;const search={query:{sql:'SELECT event FROM "identity_probe" LIMIT 1',start_time:now-1_800_000_000,end_time:now+1_000_000,size:1}};
const results=[await request('query gateway positive','http://observe-query:5082/api/default/_search?type=logs',search),await request('raw OpenObserve read by IP','http://'+rawIP+':5080/api/default/streams'),await request('raw OpenObserve write by IP','http://'+rawIP+':5080/api/default/identity_probe/_json',[{event:'owned_network_boundary_control'}]),await request('host gateway cannot use loopback-published raw port','http://'+gateway+':28406/api/default/streams'),await request('Collector cannot be used as ingest bypass','http://otel-collector:4318/v1/traces',{})];
const passed=results[0].httpStatus===200&&results[0].hasRows&&(phase==='before'?results[1].httpStatus===0&&results[2].httpStatus===0:results.slice(1).every(r=>r.httpStatus===0));const result={checkedAt:new Date().toISOString(),phase,scope:'unprivileged cached client on the actual configured Grafana network using actual query SA; Grafana binary not run',results,passed};await writeFile('/out/network-'+phase+'.json',JSON.stringify(result,null,2)+'\n',{flag:'wx',mode:0o600});console.log(JSON.stringify(result));if(!passed)process.exitCode=1;