-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
plugin.ts
160 lines (143 loc) · 5.92 KB
/
plugin.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* 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 Hapi from 'hapi';
import { Services } from './types';
import { AlertsClient } from './alerts_client';
import { AlertTypeRegistry } from './alert_type_registry';
import { TaskRunnerFactory } from './task_runner';
import { AlertsClientFactory } from './alerts_client_factory';
import { LicenseState } from './lib/license_state';
import { IClusterClient, KibanaRequest, Logger } from '../../../../../src/core/server';
import {
AlertingPluginInitializerContext,
AlertingCoreSetup,
AlertingCoreStart,
AlertingPluginsSetup,
AlertingPluginsStart,
} from './shim';
import {
createAlertRoute,
deleteAlertRoute,
findAlertRoute,
getAlertRoute,
listAlertTypesRoute,
updateAlertRoute,
enableAlertRoute,
disableAlertRoute,
updateApiKeyRoute,
muteAllAlertRoute,
unmuteAllAlertRoute,
muteAlertInstanceRoute,
unmuteAlertInstanceRoute,
} from './routes';
import { extendRouteWithLicenseCheck } from './extend_route_with_license_check';
export interface PluginSetupContract {
registerType: AlertTypeRegistry['register'];
}
export interface PluginStartContract {
listTypes: AlertTypeRegistry['list'];
getAlertsClientWithRequest(request: Hapi.Request): AlertsClient;
}
export class Plugin {
private readonly logger: Logger;
private alertTypeRegistry?: AlertTypeRegistry;
private readonly taskRunnerFactory: TaskRunnerFactory;
private adminClient?: IClusterClient;
private serverBasePath?: string;
private licenseState: LicenseState | null = null;
constructor(initializerContext: AlertingPluginInitializerContext) {
this.logger = initializerContext.logger.get('plugins', 'alerting');
this.taskRunnerFactory = new TaskRunnerFactory();
}
public async setup(
core: AlertingCoreSetup,
plugins: AlertingPluginsSetup
): Promise<PluginSetupContract> {
this.adminClient = core.elasticsearch.adminClient;
this.licenseState = new LicenseState(plugins.licensing.license$);
// Encrypted attributes
plugins.encryptedSavedObjects.registerType({
type: 'alert',
attributesToEncrypt: new Set(['apiKey']),
attributesToExcludeFromAAD: new Set([
'scheduledTaskId',
'muteAll',
'mutedInstanceIds',
'updatedBy',
]),
});
const alertTypeRegistry = new AlertTypeRegistry({
taskManager: plugins.taskManager,
taskRunnerFactory: this.taskRunnerFactory,
});
this.alertTypeRegistry = alertTypeRegistry;
this.serverBasePath = core.http.basePath.serverBasePath;
// Register routes
core.http.route(extendRouteWithLicenseCheck(createAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(deleteAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(findAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(getAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(listAlertTypesRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(updateAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(enableAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(disableAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(updateApiKeyRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(muteAllAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(unmuteAllAlertRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(muteAlertInstanceRoute, this.licenseState));
core.http.route(extendRouteWithLicenseCheck(unmuteAlertInstanceRoute, this.licenseState));
return {
registerType: alertTypeRegistry.register.bind(alertTypeRegistry),
};
}
public start(core: AlertingCoreStart, plugins: AlertingPluginsStart): PluginStartContract {
const { adminClient, serverBasePath } = this;
function spaceIdToNamespace(spaceId?: string): string | undefined {
const spacesPlugin = plugins.spaces();
return spacesPlugin && spaceId ? spacesPlugin.spaceIdToNamespace(spaceId) : undefined;
}
const alertsClientFactory = new AlertsClientFactory({
alertTypeRegistry: this.alertTypeRegistry!,
logger: this.logger,
taskManager: plugins.taskManager,
securityPluginSetup: plugins.security,
encryptedSavedObjectsPlugin: plugins.encryptedSavedObjects,
spaceIdToNamespace,
getSpaceId(request: Hapi.Request) {
const spacesPlugin = plugins.spaces();
return spacesPlugin ? spacesPlugin.getSpaceId(request) : undefined;
},
});
this.taskRunnerFactory.initialize({
logger: this.logger,
getServices(rawRequest: Hapi.Request): Services {
const request = KibanaRequest.from(rawRequest);
return {
callCluster: (...args) => adminClient!.asScoped(request).callAsCurrentUser(...args),
// rawRequest is actually a fake request, converting it to KibanaRequest causes issue in SO access
savedObjectsClient: core.savedObjects.getScopedSavedObjectsClient(rawRequest as any),
};
},
spaceIdToNamespace,
executeAction: plugins.actions.execute,
encryptedSavedObjectsPlugin: plugins.encryptedSavedObjects,
getBasePath(spaceId?: string): string {
const spacesPlugin = plugins.spaces();
return spacesPlugin && spaceId ? spacesPlugin.getBasePath(spaceId) : serverBasePath!;
},
});
return {
listTypes: this.alertTypeRegistry!.list.bind(this.alertTypeRegistry!),
getAlertsClientWithRequest: (request: Hapi.Request) =>
alertsClientFactory!.create(KibanaRequest.from(request), request),
};
}
public stop() {
if (this.licenseState) {
this.licenseState.clean();
}
}
}