import {z} from 'zod' export type ExecutionPolicyIntegration = 'INTEGRATION_KUBERNETES' | 'INTEGRATION_SCRIPT' | 'INTEGRATION_REMOTE_ACTION' | 'INTEGRATION_SLACK'; export type ExecutionPolicyEffect = 'allow' | 'deny'; export type GetApiApiV2ActionsExecutionPoliciesArgs = {'page[size]'?: number | undefined, 'page[number]'?: number | undefined, 'filter[name]'?: string | undefined, 'filter[ids]'?: Array | undefined, 'filter[integration]'?: Array | undefined, 'filter[effects]'?: Array | undefined, 'filter[creator_ids]'?: Array | undefined, sort?: Array | undefined}; export type GetApiApiV2ActionsExecutionPoliciesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const executionPolicyIntegration = z.enum(["INTEGRATION_KUBERNETES", "INTEGRATION_SCRIPT", "INTEGRATION_REMOTE_ACTION", "INTEGRATION_SLACK"]); export const executionPolicyActionPattern = z.object({action_fqns: z.array(z.string()), integration: executionPolicyIntegration}); export const executionPolicyEffect = z.enum(["allow", "deny"]); export const executionPolicyKubernetesScopeRule = z.object({target_namespaces: z.array(z.string())}); export const executionPolicyKubernetesScope = z.object({rules: z.array(executionPolicyKubernetesScopeRule)}); export const executionPolicyRemoteActionRshellAccess = z.enum(["read_only", "read_write"]); export const executionPolicyRemoteActionRshellScopeRule = z.object({access: executionPolicyRemoteActionRshellAccess, target_paths: z.array(z.string())}); export const executionPolicyRemoteActionRshellScope = z.object({rules: z.array(executionPolicyRemoteActionRshellScopeRule)}); export const executionPolicyScriptScopeRule = z.object({target_script_names: z.array(z.string())}); export const executionPolicyScriptScope = z.object({rules: z.array(executionPolicyScriptScopeRule)}); export const executionPolicySlackScopeRule = z.object({channel_name_selectors: z.array(z.string().min(1).max(80))}); export const executionPolicySlackScope = z.object({rules: z.array(executionPolicySlackScopeRule)}); export const executionPolicyScope = z.object({kubernetes: executionPolicyKubernetesScope.optional(), remote_action_rshell: executionPolicyRemoteActionRshellScope.optional(), scripts: executionPolicyScriptScope.optional(), slack: executionPolicySlackScope.optional()}); export const executionPolicyTarget = z.object({agent_tags: z.array(z.string()), name: z.string().nullable().optional()}); export const executionPolicyAttributes = z.object({action_pattern: executionPolicyActionPattern, created_at: z.string(), created_by: z.string(), effect: executionPolicyEffect, name: z.string(), scope: executionPolicyScope.optional(), targets: z.array(executionPolicyTarget), updated_at: z.string(), updated_by: z.string(), version: z.number().int().lte(2147483647)}); export const executionPolicyType = z.literal("execution_policy"); export const executionPolicyResponseData = z.object({attributes: executionPolicyAttributes, id: z.string(), type: executionPolicyType}); export const executionPolicyListResponsePage = z.object({total: z.number().int().lte(2147483647)}); export const executionPolicyListResponseMeta = z.object({page: executionPolicyListResponsePage}); export const executionPolicyListResponse = z.object({data: z.array(executionPolicyResponseData), meta: executionPolicyListResponseMeta}); 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 execution policies * * GET /api/v2/actions/execution-policies * * Also accepts auth: AuthZ */ export const getApiApiV2ActionsExecutionPolicies = async (args: GetApiApiV2ActionsExecutionPoliciesArgs, options?: GetApiApiV2ActionsExecutionPoliciesOptions) => { 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 (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['filter[name]'] != null) query.append("filter[name]", String(args['filter[name]'])) for (const item of args['filter[ids]'] ?? []) query.append("filter[ids]", String(item)) for (const item of args['filter[integration]'] ?? []) query.append("filter[integration]", String(item)) for (const item of args['filter[effects]'] ?? []) query.append("filter[effects]", String(item)) for (const item of args['filter[creator_ids]'] ?? []) query.append("filter[creator_ids]", String(item)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/actions/execution-policies${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(executionPolicyListResponse, await res.json()) };