-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
plugin.ts
177 lines (157 loc) · 6.14 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* 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 { ServerRoute } from 'hapi';
import { Observable } from 'rxjs';
import { KibanaConfig } from 'src/legacy/server/kbn_server';
import { SavedObjectsService } from 'src/core/server';
import {
Logger,
HttpServiceSetup,
PluginInitializerContext,
ElasticsearchServiceSetup,
} from 'src/core/server';
import { CapabilitiesModifier } from 'src/legacy/server/capabilities';
import { OptionalPlugin } from '../../../../server/lib/optional_plugin';
import { XPackMainPlugin } from '../../../xpack_main/xpack_main';
import { createDefaultSpace } from '../lib/create_default_space';
// @ts-ignore
import { AuditLogger } from '../../../../server/lib/audit_logger';
// @ts-ignore
import { watchStatusAndLicenseToInitialize } from '../../../../server/lib/watch_status_and_license_to_initialize';
import { checkLicense } from '../lib/check_license';
import { spacesSavedObjectsClientWrapperFactory } from '../lib/saved_objects_client/saved_objects_client_wrapper_factory';
import { SpacesAuditLogger } from '../lib/audit_logger';
import { createSpacesTutorialContextFactory } from '../lib/spaces_tutorial_context_factory';
import { initInternalApis } from '../routes/api/v1';
import { initExternalSpacesApi } from '../routes/api/external';
import { getSpacesUsageCollector } from '../lib/get_spaces_usage_collector';
import { SpacesService } from './spaces_service';
import { SecurityPlugin } from '../../../security';
import { SpacesServiceSetup } from './spaces_service/spaces_service';
import { SpacesConfigType } from './config';
import { getActiveSpace } from '../lib/get_active_space';
import { toggleUICapabilities } from '../lib/toggle_ui_capabilities';
export interface SpacesHttpServiceSetup extends HttpServiceSetup {
route(route: ServerRoute | ServerRoute[]): void;
}
export interface SpacesCoreSetup {
http: SpacesHttpServiceSetup;
savedObjects: SavedObjectsService;
elasticsearch: ElasticsearchServiceSetup;
usage: {
collectorSet: {
register: (collector: any) => void;
};
};
tutorial: {
addScopedTutorialContextFactory: (factory: any) => void;
};
capabilities: {
registerCapabilitiesModifier: (provider: CapabilitiesModifier) => void;
};
auditLogger: {
create: (pluginId: string) => AuditLogger;
};
}
export interface PluginsSetup {
// TODO: Spaces has a circular dependency with Security right now.
// Security is not yet available when init runs, so this is wrapped in an optional plugin for the time being.
security: OptionalPlugin<SecurityPlugin>;
xpackMain: XPackMainPlugin;
// TODO: this is temporary for `watchLicenseAndStatusToInitialize`
spaces: any;
}
export interface SpacesPluginSetup {
spacesService: SpacesServiceSetup;
// TODO: this is temporary, required by request interceptors which are initialized in legacy plugin
log: Logger;
}
export interface SpacesInitializerContext extends PluginInitializerContext {
legacyConfig: KibanaConfig;
}
export class Plugin {
private readonly pluginId = 'spaces';
private config$: Observable<SpacesConfigType>;
private log: Logger;
constructor(private readonly initializerContext: SpacesInitializerContext) {
this.config$ = initializerContext.config.create<SpacesConfigType>();
this.log = initializerContext.logger.get();
}
public async setup(core: SpacesCoreSetup, plugins: PluginsSetup): Promise<SpacesPluginSetup> {
const xpackMainPlugin: XPackMainPlugin = plugins.xpackMain;
watchStatusAndLicenseToInitialize(xpackMainPlugin, plugins.spaces, async () => {
await createDefaultSpace({
elasticsearch: core.elasticsearch,
savedObjects: core.savedObjects,
});
});
// Register a function that is called whenever the xpack info changes,
// to re-compute the license check results for this plugin.
xpackMainPlugin.info.feature(this.pluginId).registerLicenseCheckResultsGenerator(checkLicense);
const spacesAuditLogger = new SpacesAuditLogger(core.auditLogger.create(this.pluginId));
const service = new SpacesService(
this.log,
this.initializerContext.legacyConfig.get('server.basePath')
);
const spacesService = await service.setup({
http: core.http,
elasticsearch: core.elasticsearch,
savedObjects: core.savedObjects,
security: plugins.security,
spacesAuditLogger,
config$: this.config$,
});
const { addScopedSavedObjectsClientWrapperFactory, types } = core.savedObjects;
addScopedSavedObjectsClientWrapperFactory(
Number.MIN_SAFE_INTEGER,
'spaces',
spacesSavedObjectsClientWrapperFactory(spacesService, types)
);
core.tutorial.addScopedTutorialContextFactory(
createSpacesTutorialContextFactory(spacesService)
);
core.capabilities.registerCapabilitiesModifier(async (request, uiCapabilities) => {
const spacesClient = await spacesService.scopedClient(request);
try {
const activeSpace = await getActiveSpace(
spacesClient,
core.http.basePath.get(request),
this.initializerContext.legacyConfig.get('server.basePath')
);
const features = plugins.xpackMain.getFeatures();
return toggleUICapabilities(features, uiCapabilities, activeSpace);
} catch (e) {
return uiCapabilities;
}
});
initInternalApis({
http: core.http,
config: this.initializerContext.legacyConfig,
savedObjects: core.savedObjects,
spacesService,
xpackMain: xpackMainPlugin,
});
initExternalSpacesApi({
http: core.http,
log: this.log,
savedObjects: core.savedObjects,
spacesService,
xpackMain: xpackMainPlugin,
});
// Register a function with server to manage the collection of usage stats
core.usage.collectorSet.register(
getSpacesUsageCollector({
config: this.initializerContext.legacyConfig,
usage: core.usage,
xpackMain: xpackMainPlugin,
})
);
return {
spacesService,
log: this.log,
};
}
}