import {z} from 'zod' export type GetApiScanRegistryArgs = {'X-Auth-Token': string}; export type GetApiScanRegistryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTScanSchedule = z.object({schedule: z.string(), interval: z.number().int()}); export const rESTAWSAccountKey = z.object({id: z.string(), access_key_id: z.string().optional(), secret_access_key: z.string().optional(), region: z.string()}); export const rESTJfrogXray = z.object({url: z.string(), enable: z.boolean(), username: z.string(), password: z.string().optional()}); export const rESTGCRKey = z.object({json_key: z.string().optional()}); export const rESTRegistrySummary = z.object({name: z.string(), registry_type: z.string(), registry: z.string(), username: z.string(), password: z.string().optional(), auth_token: z.string().optional(), auth_with_token: z.boolean(), filters: z.array(z.string()), rescan_after_db_update: z.boolean(), scan_layers: z.boolean(), repo_limit: z.number().int(), tag_limit: z.number().int(), schedule: rESTScanSchedule, aws_key: rESTAWSAccountKey.optional(), jfrog_xray: rESTJfrogXray.optional(), gcr_key: rESTGCRKey.optional(), jfrog_mode: z.string(), gitlab_external_url: z.string(), gitlab_private_token: z.string().optional(), ibm_cloud_token_url: z.string(), ibm_cloud_account: z.string(), status: z.string(), error_message: z.string(), error_detail: z.string(), started_at: z.string(), scanned: z.number().int(), scheduled: z.number().int(), scanning: z.number().int(), failed: z.number().int(), cvedb_version: z.string(), cvedb_create_time: z.string()}); export const rESTRegistrySummaryListData = z.object({summarys: z.array(rESTRegistrySummary)}); 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 registries * * GET /scan/registry */ export const getApiScanRegistry = async (args: GetApiScanRegistryArgs, options: GetApiScanRegistryOptions) => { 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(/\/+$/, '')}/scan/registry${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(rESTRegistrySummaryListData, await res.json()) };