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

29 lines
4.6 KiB
TypeScript

import{mkdir,readFile,writeFile}from'node:fs/promises';import{resolve}from'node:path';
const root=resolve(import.meta.dir,'..'),baseline=process.argv.includes('--baseline'),base=baseline?'http://127.0.0.1:28406':'http://127.0.0.1:28906',output=resolve(root,'.local/openobserve-gateway-'+crypto.randomUUID());await mkdir(output,{mode:0o700});const accounts=JSON.parse(await readFile(resolve(root,'.local/openobserve-accounts.json'),'utf8'));const auth=(label:string)=>'Basic '+Buffer.from(accounts[label].email+':'+accounts[label].token).toString('base64');
const now=Date.now()*1000;const query=(size:number,start=now-300_000_000,end=now)=>({query:{sql:'SELECT trace_id FROM "messenger" LIMIT 1',start_time:start,end_time:end,size}});
const cases=baseline?[{name:'query principal cannot ingest',path:'/api/default/identity_probe/_json',method:'POST',body:[{event:'owned_query_baseline_red'}],expected:403}]:[
{name:'query native search',path:'/api/default/_search?type=traces',method:'POST',body:query(100),expected:200},
{name:'query native PromQL',path:'/api/default/prometheus/api/v1/query?query=max(database_observer_up)',expected:200},
{name:'editor organizations',path:'/api/organizations?page_num=0&page_size=1000',expected:200},
{name:'editor streams',path:'/api/default/streams?type=traces&fetchSchema=false',expected:200},
{name:"editor values",path:"/api/default/messenger/_values?"+new URLSearchParams({fields:"service_name",start_time:String(now-300_000_000),end_time:String(now),size:"50",type:"logs"}),expected:200},
{name:"values over1000 limit denied",path:"/api/default/messenger/_values?"+new URLSearchParams({fields:"service_name",start_time:String(now-300_000_000),end_time:String(now),size:"1001",type:"logs"}),expected:400},
{name:"values over1hour window denied",path:"/api/default/messenger/_values?"+new URLSearchParams({fields:"service_name",start_time:String(now-3_600_000_001),end_time:String(now),size:"50",type:"logs"}),expected:400},
{name:"PromQL bounded range with fractional epoch",path:"/api/default/prometheus/api/v1/query_range?"+new URLSearchParams({query:"max(database_observer_up)",start:String(now/1e6-60),end:String(now/1e6),step:"1"}),expected:200},
{name:"PromQL over1hour window denied",path:"/api/default/prometheus/api/v1/query_range?"+new URLSearchParams({query:"max(database_observer_up)",start:String(now/1e6-7200),end:String(now/1e6),step:"60"}),expected:400},
{name:'editor schema',path:'/api/default/streams/messenger/schema?type=traces',expected:200},
{name:'query principal cannot ingest',path:'/api/default/identity_probe/_json',method:'POST',body:[{event:'owned_query_gateway_negative'}],expected:403},
{name:'query principal cannot read admin users',path:'/api/default/users',expected:403},
{name:'query principal cannot select another organization',path:'/api/other/streams',expected:403},
{name:'query principal cannot write stream settings',path:'/api/default/streams/messenger/settings',method:'PUT',body:{},expected:403},
{name:'ingest principal cannot use query gateway',path:'/api/default/streams',principal:'ingest',expected:401},
{name:'anonymous cannot use query gateway',path:'/api/default/streams',principal:'anonymous',expected:401},
{name:'native zero size allowed within finite response and query budgets',path:'/api/default/_search?type=traces',method:'POST',body:query(0),expected:200},
{name:'over1000 result limit denied',path:'/api/default/_search?type=traces',method:'POST',body:query(1001),expected:400},
{name:'inverted time window denied',path:'/api/default/_search?type=traces',method:'POST',body:query(10,now,now-1),expected:400},
{name:'over1hour time window denied',path:'/api/default/_search?type=traces',method:'POST',body:query(10,now-3_600_000_001),expected:400},
{name:'oversized search body denied',path:'/api/default/_search?type=traces',method:'POST',body:{...query(10),padding:'x'.repeat(70_000)},expected:413}
];
const results=[];for(const c of cases){const principal=(c as any).principal??'query';const r=await fetch(base+c.path,{method:c.method??'GET',headers:{'Content-Type':'application/json',...(principal==='anonymous'?{}:{Authorization:auth(principal)})},body:c.body===undefined?undefined:JSON.stringify(c.body),redirect:'manual',signal:AbortSignal.timeout(12_000)});await r.arrayBuffer();results.push({name:c.name,expected:c.expected,actual:r.status,pass:r.status===c.expected});}
const result={checkedAt:new Date().toISOString(),base,baseline,nativeRole:'service_account',nativeReadOnly:false,results,output};await writeFile(resolve(output,'RESULT.json'),JSON.stringify(result,null,2)+'\n',{flag:'wx',mode:0o600});console.log(JSON.stringify({receipt:resolve(output,'RESULT.json'),baseline,results}));if(results.some(r=>!r.pass))process.exitCode=1;