-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
index.ts
137 lines (131 loc) · 4.6 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type {
SavedObject,
SavedObjectsExportTransformContext,
SavedObjectsServiceSetup,
} from '@kbn/core/server';
import { EncryptedSavedObjectsPluginSetup } from '@kbn/encrypted-saved-objects-plugin/server';
import { getOldestIdleActionTask } from '@kbn/task-manager-plugin/server';
import { ALERTING_CASES_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { actionTaskParamsModelVersions } from './model_versions';
import { actionMappings, actionTaskParamsMappings, connectorTokenMappings } from './mappings';
import { getActionsMigrations } from './actions_migrations';
import { getActionTaskParamsMigrations } from './action_task_params_migrations';
import { InMemoryConnector, RawAction } from '../types';
import { getImportWarnings } from './get_import_warnings';
import { transformConnectorsForExport } from './transform_connectors_for_export';
import { ActionTypeRegistry } from '../action_type_registry';
import {
ACTION_SAVED_OBJECT_TYPE,
ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE,
CONNECTOR_TOKEN_SAVED_OBJECT_TYPE,
} from '../constants/saved_objects';
import { connectorModelVersions } from './model_versions';
export function setupSavedObjects(
savedObjects: SavedObjectsServiceSetup,
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup,
actionTypeRegistry: ActionTypeRegistry,
taskManagerIndex: string,
inMemoryConnectors: InMemoryConnector[]
) {
savedObjects.registerType({
name: ACTION_SAVED_OBJECT_TYPE,
indexPattern: ALERTING_CASES_SAVED_OBJECT_INDEX,
hidden: true,
namespaceType: 'multiple-isolated',
convertToMultiNamespaceTypeVersion: '8.0.0',
mappings: actionMappings,
migrations: getActionsMigrations(encryptedSavedObjects),
management: {
displayName: 'connector',
defaultSearchField: 'name',
importableAndExportable: true,
getTitle(savedObject: SavedObject<RawAction>) {
return `Connector: [${savedObject.attributes.name}]`;
},
onExport<RawAction>(
context: SavedObjectsExportTransformContext,
objects: Array<SavedObject<RawAction>>
) {
return transformConnectorsForExport(objects, actionTypeRegistry);
},
onImport(connectors) {
return {
warnings: getImportWarnings(connectors as Array<SavedObject<RawAction>>),
};
},
},
modelVersions: connectorModelVersions,
});
// Encrypted attributes
// - `secrets` properties will be encrypted
// - `config` will be included in AAD
// - everything else excluded from AAD
encryptedSavedObjects.registerType({
type: ACTION_SAVED_OBJECT_TYPE,
attributesToEncrypt: new Set(['secrets']),
attributesToIncludeInAAD: new Set(['actionTypeId', 'isMissingSecrets', 'config']),
});
savedObjects.registerType({
name: ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE,
indexPattern: ALERTING_CASES_SAVED_OBJECT_INDEX,
hidden: true,
namespaceType: 'multiple-isolated',
convertToMultiNamespaceTypeVersion: '8.0.0',
mappings: actionTaskParamsMappings,
migrations: getActionTaskParamsMigrations(encryptedSavedObjects, inMemoryConnectors),
excludeOnUpgrade: async ({ readonlyEsClient }) => {
const oldestIdleActionTask = await getOldestIdleActionTask(
readonlyEsClient,
taskManagerIndex
);
return {
bool: {
must: [
{ term: { type: 'action_task_params' } },
{ range: { updated_at: { lt: oldestIdleActionTask } } },
],
},
};
},
modelVersions: actionTaskParamsModelVersions,
});
encryptedSavedObjects.registerType({
type: ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE,
attributesToEncrypt: new Set(['apiKey']),
attributesToIncludeInAAD: new Set([
'actionId',
'consumer',
'params',
'executionId',
'relatedSavedObjects',
'source',
]),
});
savedObjects.registerType({
name: CONNECTOR_TOKEN_SAVED_OBJECT_TYPE,
indexPattern: ALERTING_CASES_SAVED_OBJECT_INDEX,
hidden: true,
namespaceType: 'agnostic',
mappings: connectorTokenMappings,
management: {
importableAndExportable: false,
},
});
encryptedSavedObjects.registerType({
type: CONNECTOR_TOKEN_SAVED_OBJECT_TYPE,
attributesToEncrypt: new Set(['token']),
attributesToIncludeInAAD: new Set([
'connectorId',
'tokenType',
'expiresAt',
'createdAt',
'updatedAt',
]),
});
}