import {z} from 'zod' export type GetApiApiV2SecurityOwnershipResourceIdArgs = {resource_id: string}; export type GetApiApiV2SecurityOwnershipResourceIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ownershipEvidenceVersion = z.record(z.string(), z.unknown()); export const ownershipEvidenceVersions = z.array(ownershipEvidenceVersion).nullable(); export const ownershipOwnerType = z.enum(["user", "team", "service", "unknown"]); export const ownershipInferenceSource = z.record(z.string(), z.unknown()); export const ownershipInferenceSources = z.array(ownershipInferenceSource); export const ownershipInferenceStatus = z.enum(["suggested", "persisted", "overridden", "failed", "unknown"]); export const ownershipInferenceItem = z.object({checksum: z.string(), confidence: z.string(), created_at: z.string(), evidence_versions: ownershipEvidenceVersions, explanation: z.string(), id: z.string(), owner_type: ownershipOwnerType, primary_contact_ref: z.string().nullable().optional(), sources: ownershipInferenceSources, status: ownershipInferenceStatus, updated_at: z.string()}); export const ownershipInferenceItems = z.array(ownershipInferenceItem); export const ownershipInferenceListAttributes = z.object({items: ownershipInferenceItems}); export const ownershipInferencesType = z.literal("ownership_inferences"); export const ownershipInferenceListData = z.object({attributes: ownershipInferenceListAttributes, id: z.string(), type: ownershipInferencesType}); export const ownershipInferenceListResponse = z.object({data: ownershipInferenceListData}); 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 }; /** * List ownership inferences for a resource * * GET /api/v2/security/ownership/{resource_id} * * Also accepts auth: AuthZ */ export const getApiApiV2SecurityOwnershipResourceId = async (args: GetApiApiV2SecurityOwnershipResourceIdArgs, options?: GetApiApiV2SecurityOwnershipResourceIdOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/security/ownership/${encodeURIComponent(String(args.resource_id))}${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(ownershipInferenceListResponse, await res.json()) };