import {z} from 'zod' export type GetApiApiV2ActionsExecutionPoliciesPolicyIdArgs = {policy_id: string}; export type GetApiApiV2ActionsExecutionPoliciesPolicyIdOptions = {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 executionPolicyResponse = z.object({data: executionPolicyResponseData}); 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 an execution policy * * GET /api/v2/actions/execution-policies/{policy_id} * * Also accepts auth: AuthZ */ export const getApiApiV2ActionsExecutionPoliciesPolicyId = async (args: GetApiApiV2ActionsExecutionPoliciesPolicyIdArgs, options?: GetApiApiV2ActionsExecutionPoliciesPolicyIdOptions) => { 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/actions/execution-policies/${encodeURIComponent(String(args.policy_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(executionPolicyResponse, await res.json()) };