import {z} from 'zod' export type GetApiWorkloadArgs = {'X-Auth-Token': string}; export type GetApiWorkloadOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTScanBrief = z.object({status: z.string(), high: z.number().int(), medium: z.number().int(), result: z.string(), scanned_timestamp: z.number().int(), scanned_at: z.string(), base_os: z.string(), scanner_version: z.string(), cvedb_create_time: z.string()}); export const rESTWorkloadBrief: z.ZodType = z.object({id: z.string(), name: z.string(), display_name: z.string(), pod_name: z.string(), image: z.string(), image_id: z.string(), platform_role: z.string(), domain: z.string(), state: z.string(), service: z.string(), author: z.string(), service_group: z.string(), share_ns_with: z.string().optional(), cap_sniff: z.boolean(), cap_quarantine: z.boolean(), cap_change_mode: z.boolean(), policy_mode: z.string(), profile_mode: z.string(), scan_summary: rESTScanBrief, children: z.array(z.lazy(() => rESTWorkloadBrief)), quarantine_reason: z.string().optional(), service_mesh: z.boolean(), service_mesh_sidecar: z.boolean(), privileged: z.boolean(), run_as_root: z.boolean()}); export const rESTIPAddr = z.object({ip: z.string(), ip_prefix: z.number().int(), gateway: z.string()}); export const rESTWorkloadPorts = z.object({ip_proto: z.number().int(), port: z.number().int(), host_ip: z.string(), host_port: z.number().int()}); export const rESTWorkload = z.object({id: z.string(), name: z.string(), display_name: z.string(), pod_name: z.string(), image: z.string(), image_id: z.string(), platform_role: z.string(), domain: z.string(), state: z.string(), service: z.string(), author: z.string(), service_group: z.string(), share_ns_with: z.string().optional(), cap_sniff: z.boolean(), cap_quarantine: z.boolean(), cap_change_mode: z.boolean(), policy_mode: z.string().optional(), scan_summary: rESTScanBrief, children: z.array(rESTWorkloadBrief), quarantine_reason: z.string().optional(), service_mesh: z.boolean(), service_mesh_sidecar: z.boolean(), privileged: z.boolean(), run_as_root: z.boolean(), host_name: z.string(), host_id: z.string(), enforcer_id: z.string(), network_mode: z.string(), created_at: z.string(), started_at: z.string(), finished_at: z.string(), running: z.boolean(), secured_at: z.string(), exit_code: z.number().int(), interfaces: z.object({key: z.string().optional(), value: z.array(rESTIPAddr).optional()}), ports: z.array(rESTWorkloadPorts), labels: z.object({key: z.string().optional(), value: z.string().optional()}), applications: z.array(z.string()), memory_limit: z.number().int(), cpus: z.string()}); export const rESTWorkloadsData = z.object({workloads: z.array(rESTWorkload)}); 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 workload * * GET /workload */ export const getApiWorkload = async (args: GetApiWorkloadArgs, options: GetApiWorkloadOptions) => { 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(/\/+$/, '')}/workload${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(rESTWorkloadsData, await res.json()) };