Skip to content

Commit

Permalink
[7.x] Refactor Plugins to access elasticsearch from CoreStart (#59915) (
Browse files Browse the repository at this point in the history
#64329)

* 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 78fc4d2.

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

This reverts commit b621af9.

* 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 <[email protected]>

* Fix event log tests

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
rudolf and elasticmachine authored Apr 24, 2020
1 parent 742ef2f commit 069d347
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 130 deletions.
17 changes: 12 additions & 5 deletions src/core/server/elasticsearch/elasticsearch_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import { CoreService } from '../../types';
import { merge } from '../../utils';
import { CoreContext } from '../core_context';
import { Logger } from '../logging';
import { ClusterClient, ScopeableRequest } from './cluster_client';
import {
ClusterClient,
ScopeableRequest,
IClusterClient,
ICustomClusterClient,
} from './cluster_client';
import { ElasticsearchClientConfig } from './elasticsearch_client_config';
import { ElasticsearchConfig, ElasticsearchConfigType } from './elasticsearch_config';
import { InternalHttpServiceSetup, GetAuthHeaders } from '../http/';
Expand All @@ -58,12 +63,14 @@ export class ElasticsearchService
implements CoreService<InternalElasticsearchServiceSetup, ElasticsearchServiceStart> {
private readonly log: Logger;
private readonly config$: Observable<ElasticsearchConfig>;
private subscription: Subscription | undefined;
private subscription?: Subscription;
private stop$ = new Subject();
private kibanaVersion: string;
createClient: InternalElasticsearchServiceSetup['createClient'] | undefined;
dataClient: InternalElasticsearchServiceSetup['dataClient'] | undefined;
adminClient: InternalElasticsearchServiceSetup['adminClient'] | undefined;
private createClient?: (
type: string,
clientConfig?: Partial<ElasticsearchClientConfig>
) => ICustomClusterClient;
private adminClient?: IClusterClient;

constructor(private readonly coreContext: CoreContext) {
this.kibanaVersion = coreContext.env.packageInfo.version;
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class Server {
uiSettings: uiSettingsStart,
};

const pluginsStart = await this.plugins.start(this.coreStart!);
const pluginsStart = await this.plugins.start(this.coreStart);

await this.legacy.start({
core: {
Expand Down
23 changes: 11 additions & 12 deletions src/plugins/expressions/server/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { register, registryFactory, Registry, Fn } from '@kbn/interpreter/common

import Boom from 'boom';
import { schema } from '@kbn/config-schema';
import { CoreSetup, Logger } from 'src/core/server';
import { CoreSetup, Logger, APICaller } from 'src/core/server';
import { ExpressionsServerSetupDependencies } from './plugin';
import { typeSpecs, ExpressionType } from '../common';
import { serializeProvider } from '../common';
Expand Down Expand Up @@ -97,7 +97,10 @@ export const createLegacyServerEndpoints = (
* @param {*} handlers - The Canvas handlers
* @param {*} fnCall - Describes the function being run `{ functionName, args, context }`
*/
async function runFunction(handlers: any, fnCall: any) {
async function runFunction(
handlers: { environment: string; elasticsearchClient: APICaller },
fnCall: any
) {
const { functionName, args, context } = fnCall;
const { deserialize } = serializeProvider(registries.types.toJS());
const fnDef = registries.serverFunctions.toJS()[functionName];
Expand All @@ -112,18 +115,14 @@ export const createLegacyServerEndpoints = (
* results back using ND-JSON.
*/
plugins.bfetch.addBatchProcessingRoute(`/api/interpreter/fns`, request => {
const scopedClient = core.elasticsearch.dataClient.asScoped(request);
const handlers = {
environment: 'server',
elasticsearchClient: async (
endpoint: string,
clientParams: Record<string, any> = {},
options?: any
) => scopedClient.callAsCurrentUser(endpoint, clientParams, options),
};

return {
onBatchItem: async (fnCall: any) => {
const [coreStart] = await core.getStartServices();
const handlers = {
environment: 'server',
elasticsearchClient: coreStart.elasticsearch.legacy.client.asScoped(request)
.callAsCurrentUser,
};
const result = await runFunction(handlers, fnCall);
if (typeof result === 'undefined') {
throw new Error(`Function ${fnCall.functionName} did not return anything.`);
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/actions/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ describe('Actions Plugin', () => {
savedObjects: {
client: {},
},
elasticsearch: {
adminClient: jest.fn(),
},
},
} as any,
httpServerMock.createKibanaRequest(),
Expand Down
25 changes: 9 additions & 16 deletions x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
Plugin,
CoreSetup,
CoreStart,
IClusterClient,
KibanaRequest,
Logger,
SharedGlobalConfig,
RequestHandler,
IContextProvider,
SavedObjectsServiceStart,
ElasticsearchServiceStart,
} from '../../../../src/core/server';

import {
Expand Down Expand Up @@ -89,7 +89,6 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi

private readonly logger: Logger;
private serverBasePath?: string;
private adminClient?: IClusterClient;
private taskRunnerFactory?: TaskRunnerFactory;
private actionTypeRegistry?: ActionTypeRegistry;
private actionExecutor?: ActionExecutor;
Expand Down Expand Up @@ -173,7 +172,6 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
this.actionTypeRegistry = actionTypeRegistry;
this.serverBasePath = core.http.basePath.serverBasePath;
this.actionExecutor = actionExecutor;
this.adminClient = core.elasticsearch.adminClient;
this.spaces = plugins.spaces?.spacesService;

registerBuiltInActionTypes({
Expand Down Expand Up @@ -233,7 +231,6 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
actionTypeRegistry,
taskRunnerFactory,
kibanaIndex,
adminClient,
isESOUsingEphemeralEncryptionKey,
preconfiguredActions,
} = this;
Expand All @@ -242,7 +239,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
logger,
eventLogger: this.eventLogger!,
spaces: this.spaces,
getServices: this.getServicesFactory(core.savedObjects),
getServices: this.getServicesFactory(core.savedObjects, core.elasticsearch),
encryptedSavedObjectsPlugin: plugins.encryptedSavedObjects,
actionTypeRegistry: actionTypeRegistry!,
preconfiguredActions,
Expand Down Expand Up @@ -282,7 +279,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
savedObjectsClient: core.savedObjects.getScopedClient(request),
actionTypeRegistry: actionTypeRegistry!,
defaultKibanaIndex: await kibanaIndex,
scopedClusterClient: adminClient!.asScoped(request),
scopedClusterClient: core.elasticsearch.legacy.client.asScoped(request),
preconfiguredActions,
});
},
Expand All @@ -291,24 +288,20 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
}

private getServicesFactory(
savedObjects: SavedObjectsServiceStart
savedObjects: SavedObjectsServiceStart,
elasticsearch: ElasticsearchServiceStart
): (request: KibanaRequest) => Services {
const { adminClient } = this;
return request => ({
callCluster: adminClient!.asScoped(request).callAsCurrentUser,
callCluster: elasticsearch.legacy.client.asScoped(request).callAsCurrentUser,
savedObjectsClient: savedObjects.getScopedClient(request),
});
}

private createRouteHandlerContext = (
defaultKibanaIndex: string
): IContextProvider<RequestHandler<any, any, any>, 'actions'> => {
const {
actionTypeRegistry,
adminClient,
isESOUsingEphemeralEncryptionKey,
preconfiguredActions,
} = this;
const { actionTypeRegistry, isESOUsingEphemeralEncryptionKey, preconfiguredActions } = this;

return async function actionsRouteHandlerContext(context, request) {
return {
getActionsClient: () => {
Expand All @@ -321,7 +314,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
savedObjectsClient: context.core.savedObjects.client,
actionTypeRegistry: actionTypeRegistry!,
defaultKibanaIndex,
scopedClusterClient: adminClient!.asScoped(request),
scopedClusterClient: context.core.elasticsearch.adminClient,
preconfiguredActions,
});
},
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/actions/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Logger, CoreSetup } from 'kibana/server';
import { Logger, CoreSetup, APICaller } from 'kibana/server';
import moment from 'moment';
import {
RunContext,
Expand Down Expand Up @@ -62,7 +62,11 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra
export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex: string) {
return ({ taskInstance }: RunContext) => {
const { state } = taskInstance;
const callCluster = core.elasticsearch.adminClient.callAsInternalUser;
const callCluster = (...args: Parameters<APICaller>) => {
return core.getStartServices().then(([{ elasticsearch: { legacy: { client } } }]) =>
client.callAsInternalUser(...args)
);
};
return {
async run() {
return Promise.all([
Expand Down
12 changes: 5 additions & 7 deletions x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { TaskRunnerFactory } from './task_runner';
import { AlertsClientFactory } from './alerts_client_factory';
import { LicenseState } from './lib/license_state';
import {
IClusterClient,
KibanaRequest,
Logger,
PluginInitializerContext,
Expand All @@ -29,6 +28,7 @@ import {
IContextProvider,
RequestHandler,
SharedGlobalConfig,
ElasticsearchServiceStart,
} from '../../../../src/core/server';

import {
Expand Down Expand Up @@ -94,7 +94,6 @@ export class AlertingPlugin {
private readonly logger: Logger;
private alertTypeRegistry?: AlertTypeRegistry;
private readonly taskRunnerFactory: TaskRunnerFactory;
private adminClient?: IClusterClient;
private serverBasePath?: string;
private licenseState: LicenseState | null = null;
private isESOUsingEphemeralEncryptionKey?: boolean;
Expand All @@ -119,7 +118,6 @@ export class AlertingPlugin {
}

public async setup(core: CoreSetup, plugins: AlertingPluginsSetup): Promise<PluginSetupContract> {
this.adminClient = core.elasticsearch.adminClient;
this.licenseState = new LicenseState(plugins.licensing.license$);
this.spaces = plugins.spaces?.spacesService;
this.security = plugins.security;
Expand Down Expand Up @@ -223,7 +221,7 @@ export class AlertingPlugin {

taskRunnerFactory.initialize({
logger,
getServices: this.getServicesFactory(core.savedObjects),
getServices: this.getServicesFactory(core.savedObjects, core.elasticsearch),
spaceIdToNamespace: this.spaceIdToNamespace,
actionsPlugin: plugins.actions,
encryptedSavedObjectsPlugin: plugins.encryptedSavedObjects,
Expand Down Expand Up @@ -263,11 +261,11 @@ export class AlertingPlugin {
};

private getServicesFactory(
savedObjects: SavedObjectsServiceStart
savedObjects: SavedObjectsServiceStart,
elasticsearch: ElasticsearchServiceStart
): (request: KibanaRequest) => Services {
const { adminClient } = this;
return request => ({
callCluster: adminClient!.asScoped(request).callAsCurrentUser,
callCluster: elasticsearch.legacy.client.asScoped(request).callAsCurrentUser,
savedObjectsClient: savedObjects.getScopedClient(request),
});
}
Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/alerting/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Logger, CoreSetup } from 'kibana/server';
import { Logger, CoreSetup, APICaller } from 'kibana/server';
import moment from 'moment';
import {
RunContext,
Expand Down Expand Up @@ -65,7 +65,12 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra
export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex: string) {
return ({ taskInstance }: RunContext) => {
const { state } = taskInstance;
const callCluster = core.elasticsearch.adminClient.callAsInternalUser;
const callCluster = (...args: Parameters<APICaller>) => {
return core.getStartServices().then(([{ elasticsearch: { legacy: { client } } }]) =>
client.callAsInternalUser(...args)
);
};

return {
async run() {
return Promise.all([
Expand Down
Loading

0 comments on commit 069d347

Please sign in to comment.