import {z} from 'zod' export type GetApiCloudAwsLambdaProjectRegionNameArgs = {'X-Auth-Token': string, project: string, region: string, name: string}; export type GetApiCloudAwsLambdaProjectRegionNameOptions = {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 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 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 rESTBenchItem = z.object({catalog: z.string(), type: z.string(), level: z.string(), test_number: z.string(), profile: z.string(), scored: z.boolean(), automated: z.boolean(), description: z.string(), message: z.array(z.string()), remediation: z.string(), group: z.string()}); export const rESTScanSecret = z.object({type: z.string(), evidence: z.string(), path: z.string(), suggestion: z.string()}); export const rESTScanSetIdPerm = z.object({type: z.string(), evidence: z.string(), path: z.string()}); export const rESTScanReport = z.object({vulnerabilities: z.array(rESTVulnerability), modules: z.array(rESTScanModule).optional(), checks: z.array(rESTBenchItem).optional(), secrets: z.array(rESTScanSecret).optional(), setid_perms: z.array(rESTScanSetIdPerm).optional(), envs: z.array(z.string()).optional(), labels: z.object({key: z.array(z.string()).optional(), value: z.string().optional()}).optional(), cmds: z.array(z.string()).optional()}); export const rESTScanAwsFuncDetail = z.object({scan_brief: rESTScanBrief, scan_report: rESTScanReport}); export const rESTAwsResDetail = z.object({permission_level: z.string(), resource: z.string(), permission_state: z.string()}); export const rESTAwsFuncPermission = z.object({aws_attached_policy: z.boolean(), policy_permission_level: z.string(), permission_state: z.array(z.string()), allowed_detail: z.object({key: z.string().optional(), value: z.array(rESTAwsResDetail).optional()})}); export const rESTScanAwsFuncReport = z.object({function_id: z.string(), nv_sec_id: z.string(), version: z.string(), scan_result: rESTScanAwsFuncDetail, allowed_resources: z.object({key: z.string().optional(), value: rESTAwsFuncPermission.optional()}), req_resources: z.object({key: z.string().optional(), value: rESTAwsFuncPermission.optional()})}); export const rESTAwsFunction = z.object({function_name: z.string(), region: z.string(), report: z.array(rESTScanAwsFuncReport).optional()}); 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 AWS Lambda function detail * * GET /cloud/aws/lambda/{project}/{region}/{name} */ export const getApiCloudAwsLambdaProjectRegionName = async (args: GetApiCloudAwsLambdaProjectRegionNameArgs, options: GetApiCloudAwsLambdaProjectRegionNameOptions) => { 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(/\/+$/, '')}/cloud/aws/lambda/${encodeURIComponent(String(args.project))}/${encodeURIComponent(String(args.region))}/${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(rESTAwsFunction, await res.json()) };