import {z} from 'zod' export type RESTScanMeta = {source: string, user: string, job: string, workspace: string, function: string, region: string}; export type RESTScanRepoReq = {metadata: RESTScanMeta, registry: string, username?: string | undefined, password?: string | undefined, repository: string, tag: string, scan_layers: boolean, base_image: string}; export type RESTScanRepoReqData = {request: RESTScanRepoReq}; export type CreateApiScanRepositoryArgs = {'X-Auth-Token': string, body: RESTScanRepoReqData}; export type CreateApiScanRepositoryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTVulnerability = z.object({name: z.string(), score: z.number(), severity: z.string(), vectors: z.string(), description: z.string(), package_name: z.string(), package_version: z.string(), fixed_version: z.string(), link: z.string(), score_v3: z.number(), vectors_v3: z.string(), published_timestamp: z.number().int(), last_modified_timestamp: z.number().int(), cpes: z.array(z.string()).optional(), cves: z.array(z.string()).optional(), feed_rating: z.string(), in_base_image: z.boolean(), tags: z.array(z.string()).optional()}); export const rESTScanLayer = z.object({digest: z.string(), cmds: z.string(), vulnerabilities: z.array(rESTVulnerability), size: z.number().int()}); export const rESTModuleCve = z.object({name: z.string(), status: z.string()}); export const rESTScanModule = z.object({name: z.string(), version: z.string(), source: z.string(), cves: z.array(rESTModuleCve).optional(), cpes: z.array(z.string()).optional()}); export const rESTScanRepoReport = z.object({verdict: z.string().optional(), image_id: z.string(), registry: z.string(), repository: z.string(), tag: z.string(), digest: z.string(), size: z.number().int(), author: z.string(), base_os: z.string(), cvedb_version: z.string(), cvedb_create_time: z.string(), layers: z.array(rESTScanLayer), vulnerabilities: z.array(rESTVulnerability), modules: z.array(rESTScanModule), envs: z.array(z.string()), labels: z.object({key: z.array(z.string()).optional(), value: z.string().optional()})}); export const rESTScanRepoReportData = z.object({report: rESTScanRepoReport}); 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 }; /** * Scan repository * * POST /scan/repository */ export const createApiScanRepository = async (args: CreateApiScanRepositoryArgs, options: CreateApiScanRepositoryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/repository${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rESTScanRepoReportData, await res.json()) };