import {z} from 'zod' export type STIXContentEncoding = 'gzip'; export type STIXMetadataObject = {[key: string]: unknown}; export type STIXPatternType = 'stix'; export type STIXObject = {confidence?: number | undefined, created?: string | undefined, external_references?: Array | undefined, id: string, indicator_types?: Array | undefined, kill_chain_phases?: Array | undefined, labels?: Array | undefined, modified?: string | undefined, object_marking_refs?: Array | undefined, pattern?: string | undefined, pattern_type?: STIXPatternType | undefined, revoked?: boolean | undefined, spec_version?: string | undefined, type: string, valid_from?: string | undefined, valid_until?: string | undefined} | {[key: string]: unknown}; export type STIXSpecVersion = '2.1'; export type STIXBundleType = 'bundle'; export type STIXBundleRequest = {id: string, objects: Array, spec_version?: STIXSpecVersion | undefined, type: STIXBundleType} | {[key: string]: unknown}; export type CreateApiApiV2SecurityThreatIntelStixArgs = {ti_vendor: string, 'Content-Encoding'?: STIXContentEncoding | undefined, body: STIXBundleRequest}; export type CreateApiApiV2SecurityThreatIntelStixOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sTIXIngestResponseAttributes = z.object({accepted: z.number().int().gte(0), invalid: z.number().int().gte(0), unsupported: z.number().int().gte(0)}); export const sTIXIngestResponseType = z.literal("threat-intel-stix-ingest"); export const sTIXIngestResponseData = z.object({attributes: sTIXIngestResponseAttributes, id: z.string(), type: sTIXIngestResponseType}); export const sTIXIngestResponse = z.object({data: sTIXIngestResponseData}); 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 }; /** * Ingest STIX threat intelligence * * POST /api/v2/security/threat-intel/stix * * Also accepts auth: AuthZ */ export const createApiApiV2SecurityThreatIntelStix = async (args: CreateApiApiV2SecurityThreatIntelStixArgs, options?: CreateApiApiV2SecurityThreatIntelStixOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.ti_vendor != null) headers.set("ti_vendor", String(args.ti_vendor)) if (args['Content-Encoding'] != null) headers.set("Content-Encoding", String(args['Content-Encoding'])) 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/threat-intel/stix${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(sTIXIngestResponse, await res.json()) };