Skip to content
File

Blob: ui/operator/waf-api.ts

typescript49 lines
1import { RequestError, requestJSON } from '../shared/http';
2import { WAF_CHANNELS, WAF_DEFAULT_MODES, type WAFChannel, type WAFDefaultMode } from '../shared/waf';
3import type { Identity } from './types';
4 
5export interface WAFConfig {
6 enabled: boolean;
7 default_mode: WAFDefaultMode;
8 channel: WAFChannel;
9 pinned_version: string;
10}
11 
12export interface WAFSnapshot {
13 version: string;
14 config: WAFConfig | null;
15 server: Identity;
16 observed_at: string;
17 error?: string;
18 status: {
19 configured: boolean;
20 enabled: boolean;
21 ready: boolean;
22 active_version: string;
23 previous_version: string;
24 engine_version: string;
25 channel: WAFChannel | 'UNKNOWN_CHANNEL';
26 pinned_version?: string;
27 applied_version: string;
28 last_error?: string;
29 updating: boolean;
30 };
31}
32 
33export async function requestWAFConfig(signal: AbortSignal, body?: string, after?: string): Promise<WAFSnapshot> {
34 const data = await requestJSON<WAFSnapshot>(`/_internal/api/config/waf${after ? `?after=${encodeURIComponent(after)}` : ''}`, {
35 signal, body, method: body === undefined ? 'GET' : 'PUT', timeout: body === undefined ? 12_000 : 15_000,
36 });
37 const validConfig = data?.config === null || (data?.config && typeof data.config.enabled === 'boolean'
38 && WAF_DEFAULT_MODES.includes(data.config.default_mode) && WAF_CHANNELS.includes(data.config.channel)
39 && typeof data.config.pinned_version === 'string');
40 if (!data || typeof data.version !== 'string' || typeof data.server?.address !== 'string'
41 || typeof data.observed_at !== 'string' || !validConfig || !data.status
42 || typeof data.status.enabled !== 'boolean' || typeof data.status.ready !== 'boolean' || typeof data.status.updating !== 'boolean'
43 || typeof data.status.active_version !== 'string' || typeof data.status.previous_version !== 'string'
44 || typeof data.status.engine_version !== 'string' || typeof data.status.applied_version !== 'string') {
45 throw new RequestError('Unexpected server response.', body === undefined ? 'request_failed' : 'outcome_unknown');
46 }
47 return data;
48}