kibana/x-pack/plugins/upgrade_assistant/server/plugin.ts
Rudolf Meijering 00a1144ae2
Refactor Plugins to access elasticsearch from CoreStart (#59915)
* x-pack/watcher: use Elasticsearch from CoreStart

* x-pack/upgrade_assistant: use Elasticsearch from CoreStart

* x-pack/actions: use Elasticsearch from CoreStart

* x-pack/alerting: use Elasticsearch from CoreStart

* x-pack/lens: use Elasticsearch from CoreStart

* expressions: use Elasticsearch from CoreStart

* x-pack/remote_clusters: remove unused Elasticsearch dependency on CoreSetup

* x-pack/oss_telemetry: use Elasticsearch from CoreStart

* Cleanup after #59886

* x-pack/watcher: create custom client only once

* Revert "x-pack/watcher: create custom client only once"

This reverts commit 78fc4d2e93.

* Revert "x-pack/watcher: use Elasticsearch from CoreStart"

This reverts commit b621af9388.

* x-pack/task_manager: use Elasticsearch from CoreStart

* x-pack/event_log: use Elasticsearch from CoreStart

* x-pack/alerting: use Elasticsearch from CoreStart

* x-pack/apm: use Elasticsearch from CoreStart

* x-pack/actions: use Elasticsearch from CoreStart

* PR Feedback

* APM review nits

* Remove unused variable

* Remove unused variable

* x-pack/apm: better typesafety

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2020-04-15 17:07:57 +02:00

122 lines
4 KiB
TypeScript

/*
* 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 { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import {
Plugin,
CoreSetup,
CoreStart,
PluginInitializerContext,
Logger,
SavedObjectsClient,
SavedObjectsServiceStart,
} from '../../../../src/core/server';
import { CloudSetup } from '../../cloud/server';
import { LicensingPluginSetup } from '../../licensing/server';
import { CredentialStore, credentialStoreFactory } from './lib/reindexing/credential_store';
import { ReindexWorker } from './lib/reindexing';
import { registerUpgradeAssistantUsageCollector } from './lib/telemetry';
import { registerClusterCheckupRoutes } from './routes/cluster_checkup';
import { registerDeprecationLoggingRoutes } from './routes/deprecation_logging';
import { registerReindexIndicesRoutes, createReindexWorker } from './routes/reindex_indices';
import { registerTelemetryRoutes } from './routes/telemetry';
import { RouteDependencies } from './types';
interface PluginsSetup {
usageCollection: UsageCollectionSetup;
licensing: LicensingPluginSetup;
cloud?: CloudSetup;
}
export class UpgradeAssistantServerPlugin implements Plugin {
private readonly logger: Logger;
private readonly credentialStore: CredentialStore;
// Properties set at setup
private licensing?: LicensingPluginSetup;
// Properties set at start
private savedObjectsServiceStart?: SavedObjectsServiceStart;
private worker?: ReindexWorker;
constructor({ logger }: PluginInitializerContext) {
this.logger = logger.get();
this.credentialStore = credentialStoreFactory();
}
private getWorker() {
if (!this.worker) {
throw new Error('Worker unavailable');
}
return this.worker;
}
setup(
{ http, getStartServices, capabilities }: CoreSetup,
{ usageCollection, cloud, licensing }: PluginsSetup
) {
this.licensing = licensing;
const router = http.createRouter();
const dependencies: RouteDependencies = {
cloud,
router,
credentialStore: this.credentialStore,
log: this.logger,
getSavedObjectsService: () => {
if (!this.savedObjectsServiceStart) {
throw new Error('Saved Objects Start service not available');
}
return this.savedObjectsServiceStart;
},
licensing,
};
registerClusterCheckupRoutes(dependencies);
registerDeprecationLoggingRoutes(dependencies);
registerReindexIndicesRoutes(dependencies, this.getWorker.bind(this));
// Bootstrap the needed routes and the collector for the telemetry
registerTelemetryRoutes(dependencies);
if (usageCollection) {
getStartServices().then(([{ savedObjects, elasticsearch }]) => {
registerUpgradeAssistantUsageCollector({ elasticsearch, usageCollection, savedObjects });
});
}
}
start({ savedObjects, elasticsearch }: CoreStart) {
this.savedObjectsServiceStart = savedObjects;
// The ReindexWorker uses a map of request headers that contain the authentication credentials
// for a given reindex. We cannot currently store these in an the .kibana index b/c we do not
// want to expose these credentials to any unauthenticated users. We also want to avoid any need
// to add a user for a special index just for upgrading. This in-memory cache allows us to
// process jobs without the browser staying on the page, but will require that jobs go into
// a paused state if no Kibana nodes have the required credentials.
this.worker = createReindexWorker({
credentialStore: this.credentialStore,
licensing: this.licensing!,
elasticsearchService: elasticsearch,
logger: this.logger,
savedObjects: new SavedObjectsClient(
this.savedObjectsServiceStart.createInternalRepository()
),
});
this.worker.start();
}
stop(): void {
if (this.worker) {
this.worker.stop();
}
}
}