import {z} from 'zod' export type GetApiLogThreatIdArgs = {'X-Auth-Token': string, id: string}; export type GetApiLogThreatIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const threat = 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(), threat_id: z.number().int(), client_workload_id: z.string(), client_workload_name: z.string(), client_workload_domain: z.string().optional(), client_workload_image: z.string().optional(), client_workload_service: z.string().optional(), server_workload_id: z.string(), server_workload_name: z.string(), server_workload_domain: z.string().optional(), server_workload_image: z.string().optional(), server_workload_service: z.string().optional(), severity: z.string(), action: z.string(), count: z.number().int(), ether_type: z.number().int(), client_port: z.number().int(), server_port: z.number().int(), server_conn_port: z.number().int(), icmp_code: z.number().int(), icmp_type: z.number().int(), ip_proto: z.number().int(), client_ip: z.string(), server_ip: z.string(), application: z.string(), sensor: z.string(), group: z.string(), target: z.string(), monitor: z.boolean(), cap_len: z.number().int().optional(), packet: z.string().optional(), message: z.string()}); export const rESTThreatData = z.object({threat: threat}); 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 }; /** * Show threat * * GET /log/threat/{id} */ export const getApiLogThreatId = async (args: GetApiLogThreatIdArgs, options: GetApiLogThreatIdOptions) => { 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/threat/${encodeURIComponent(String(args.id))}${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(rESTThreatData, await res.json()) };