-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathexecute.ts
83 lines (73 loc) · 2.47 KB
/
execute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import {
ActionTypeExecutorResult,
ActionTypeRegistryContract,
RawAction,
Services,
} from '../types';
import { validateParams, validateConfig, validateSecrets } from './validate_with_schema';
import { EncryptedSavedObjectsPlugin } from '../../../encrypted_saved_objects';
interface ExecuteOptions {
actionId: string;
namespace?: string;
services: Services;
params: Record<string, any>;
encryptedSavedObjectsPlugin: EncryptedSavedObjectsPlugin;
actionTypeRegistry: ActionTypeRegistryContract;
}
export async function execute({
actionId,
namespace,
actionTypeRegistry,
services,
params,
encryptedSavedObjectsPlugin,
}: ExecuteOptions): Promise<ActionTypeExecutorResult> {
// Ensure user can read the action before processing
const {
attributes: { actionTypeId, config, description },
} = await services.savedObjectsClient.get<RawAction>('action', actionId);
// Only get encrypted attributes here, the remaining attributes can be fetched in
// the savedObjectsClient call
const {
attributes: { secrets },
} = await encryptedSavedObjectsPlugin.getDecryptedAsInternalUser<RawAction>('action', actionId, {
namespace,
});
const actionType = actionTypeRegistry.get(actionTypeId);
let validatedParams;
let validatedConfig;
let validatedSecrets;
try {
validatedParams = validateParams(actionType, params);
validatedConfig = validateConfig(actionType, config);
validatedSecrets = validateSecrets(actionType, secrets);
} catch (err) {
return { status: 'error', message: err.message, retry: false };
}
let result: ActionTypeExecutorResult | null = null;
const actionLabel = `${actionId} - ${actionTypeId} - ${description}`;
try {
result = await actionType.executor({
id: actionId,
services,
params: validatedParams,
config: validatedConfig,
secrets: validatedSecrets,
});
} catch (err) {
services.log(
['warning', 'x-pack', 'actions'],
`action executed unsuccessfully: ${actionLabel} - ${err.message}`
);
throw err;
}
services.log(['debug', 'x-pack', 'actions'], `action executed successfully: ${actionLabel}`);
// return basic response if none provided
if (result == null) return { status: 'ok' };
return result;
}