import {z} from 'zod' export type ExecutionPolicyIntegration = 'INTEGRATION_KUBERNETES' | 'INTEGRATION_SCRIPT' | 'INTEGRATION_REMOTE_ACTION' | 'INTEGRATION_SLACK'; export type ExecutionPolicyActionPattern = {action_fqns: Array, integration: ExecutionPolicyIntegration}; export type ExecutionPolicyEffect = 'allow' | 'deny'; export type ExecutionPolicyKubernetesScopeRule = {target_namespaces: Array}; export type ExecutionPolicyKubernetesScope = {rules: Array}; export type ExecutionPolicyRemoteActionRshellAccess = 'read_only' | 'read_write'; export type ExecutionPolicyRemoteActionRshellScopeRule = {access: ExecutionPolicyRemoteActionRshellAccess, target_paths: Array}; export type ExecutionPolicyRemoteActionRshellScope = {rules: Array}; export type ExecutionPolicyScriptScopeRule = {target_script_names: Array}; export type ExecutionPolicyScriptScope = {rules: Array}; export type ExecutionPolicySlackScopeRule = {channel_name_selectors: Array}; export type ExecutionPolicySlackScope = {rules: Array}; export type ExecutionPolicyScope = {kubernetes?: ExecutionPolicyKubernetesScope | undefined, remote_action_rshell?: ExecutionPolicyRemoteActionRshellScope | undefined, scripts?: ExecutionPolicyScriptScope | undefined, slack?: ExecutionPolicySlackScope | undefined}; export type ExecutionPolicyTarget = {agent_tags: Array, name?: (string | null) | undefined}; export type ExecutionPolicyWriteAttributes = {action_pattern: ExecutionPolicyActionPattern, effect: ExecutionPolicyEffect, name: string, scope?: ExecutionPolicyScope | undefined, targets?: Array | undefined}; export type ExecutionPolicyType = 'execution_policy'; export type ExecutionPolicyCreateRequestData = {attributes: ExecutionPolicyWriteAttributes, type: ExecutionPolicyType}; export type ExecutionPolicyCreateRequest = {data: ExecutionPolicyCreateRequestData}; export type CreateApiApiV2ActionsExecutionPoliciesArgs = {body: ExecutionPolicyCreateRequest}; export type CreateApiApiV2ActionsExecutionPoliciesOptions = {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 }; /** * Create an execution policy * * POST /api/v2/actions/execution-policies * * Also accepts auth: AuthZ */ export const createApiApiV2ActionsExecutionPolicies = async (args: CreateApiApiV2ActionsExecutionPoliciesArgs, options?: CreateApiApiV2ActionsExecutionPoliciesOptions) => { 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 (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: "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(executionPolicyResponse, await res.json()) };