import {z} from 'zod' export type GetApiServiceNameArgs = {'X-Auth-Token': string, name: string}; export type GetApiServiceNameOptions = {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 rESTPolicyRule = z.object({id: z.number().int(), comment: z.string(), from: z.string(), to: z.string(), ports: z.string(), action: z.string(), applications: z.array(z.string()), learned: z.boolean(), disable: z.boolean(), created_timestamp: z.number().int(), last_modified_timestamp: z.number().int(), cfg_type: z.string(), priority: z.number().int()}); export const rESTCLUSEventCondition = z.object({type: z.string().optional(), value: z.string().optional()}); export const rESTResponseRule = z.object({id: z.number().int(), event: z.string(), comment: z.string(), group: z.string(), conditions: z.array(rESTCLUSEventCondition), actions: z.array(z.string()), webhooks: z.array(z.string()), disable: z.boolean(), cfg_type: z.string()}); export const rESTIPPort = z.object({ip: z.string(), port: z.number().int()}); export const rESTService = z.object({name: z.string(), policy_mode: z.string(), profile_mode: z.string(), domain: z.string(), platform_role: z.string(), members: z.array(rESTWorkloadBrief), policy_rules: z.array(rESTPolicyRule), response_rules: z.array(rESTResponseRule), service_addr: rESTIPPort.optional(), ingress_exposure: z.boolean(), egress_exposure: z.boolean(), cap_change_mode: z.boolean().optional(), cap_scorable: z.boolean().optional()}); export const rESTServiceData = z.object({service: rESTService}); 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 service * * GET /service/{name} */ export const getApiServiceName = async (args: GetApiServiceNameArgs, options: GetApiServiceNameOptions) => { 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(/\/+$/, '')}/service/${encodeURIComponent(String(args.name))}${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(rESTServiceData, await res.json()) };