import {z} from 'zod' export type GetApiLogIncidentArgs = {'X-Auth-Token': string}; export type GetApiLogIncidentOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const incident = z.object({name: z.string().optional(), level: z.string(), reported_timestamp: z.number().int(), reported_at: z.string(), cluster_name: z.string(), response_rule_id: z.number().int().optional(), host_id: z.string(), host_name: z.string(), enforcer_id: z.string(), enforcer_name: z.string(), id: z.string(), workload_id: z.string().optional(), workload_name: z.string().optional(), workload_domain: z.string().optional(), workload_image: z.string().optional(), workload_service: z.string().optional(), remote_workload_id: z.string().optional(), remote_workload_name: z.string().optional(), remote_workload_domain: z.string().optional(), remote_workload_image: z.string().optional(), remote_workload_service: z.string().optional(), proc_name: z.string().optional(), proc_path: z.string().optional(), proc_cmd: z.string().optional(), proc_real_uid: z.number().int().optional(), proc_effective_uid: z.number().int().optional(), proc_real_user: z.string().optional(), proc_effective_user: z.string().optional(), file_path: z.string().optional(), file_name: z.array(z.string()).optional(), client_ip: z.string(), server_ip: z.string(), client_port: z.number().int(), server_port: z.number().int(), server_conn_port: z.number().int(), ether_type: z.number().int(), ip_proto: z.number().int(), conn_ingress: z.boolean(), proc_parent_name: z.string().optional(), proc_parent_path: z.string().optional(), action: z.string(), group: z.string(), rule_id: z.string(), aggregation_from: z.number().int(), count: z.number().int().optional(), message: z.string()}); export const rESTIncidentsData = z.object({incidents: z.array(incident)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get a list of incidents * * GET /log/incident */ export const getApiLogIncident = async (args: GetApiLogIncidentArgs, options: GetApiLogIncidentOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Auth-Token'] != null) headers.set("X-Auth-Token", String(args['X-Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/log/incident${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rESTIncidentsData, await res.json()) };