import {z} from 'zod' export type GetApiGroupArgs = {'X-Auth-Token': string, scope?: ('fed' | 'local') | undefined}; export type GetApiGroupOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTCriteriaEntry = z.object({key: z.string(), value: z.string(), op: z.string()}); 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 rESTGroup = z.object({name: z.string(), learned: z.boolean(), reserved: z.boolean(), policy_mode: z.string().optional(), domain: z.string(), creater_domains: z.array(z.string()), kind: z.string(), platform_role: z.string(), cap_change_mode: z.boolean(), criteria: z.array(rESTCriteriaEntry), members: z.array(rESTWorkloadBrief), policy_rules: z.array(z.number().int()), response_rules: z.array(z.number().int())}); export const rESTGroupsData = z.object({groups: z.array(rESTGroup)}); 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 groups * * GET /group */ export const getApiGroup = async (args: GetApiGroupArgs, options: GetApiGroupOptions) => { 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'])) if (args.scope != null) query.append("scope", String(args.scope)) const url = `${baseUrl.replace(/\/+$/, '')}/group${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(rESTGroupsData, await res.json()) };