90 lines
2.8 KiB
TypeScript
90 lines
2.8 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
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
* 2.0.
|
|
*/
|
|
|
|
import {
|
|
PluginInitializerContext,
|
|
CoreSetup,
|
|
CoreStart,
|
|
Plugin,
|
|
Logger,
|
|
} from '../../../../src/core/server';
|
|
|
|
import { createConfig } from './create_config';
|
|
import { OsqueryPluginSetup, OsqueryPluginStart, SetupPlugins, StartPlugins } from './types';
|
|
import { defineRoutes } from './routes';
|
|
import { osquerySearchStrategyProvider } from './search_strategy/osquery';
|
|
import { initSavedObjects } from './saved_objects';
|
|
import { initUsageCollectors } from './usage';
|
|
import { OsqueryAppContext, OsqueryAppContextService } from './lib/osquery_app_context_services';
|
|
import { ConfigType } from './config';
|
|
|
|
export class OsqueryPlugin implements Plugin<OsqueryPluginSetup, OsqueryPluginStart> {
|
|
private readonly logger: Logger;
|
|
private context: PluginInitializerContext;
|
|
private readonly osqueryAppContextService = new OsqueryAppContextService();
|
|
|
|
constructor(private readonly initializerContext: PluginInitializerContext) {
|
|
this.context = initializerContext;
|
|
this.logger = initializerContext.logger.get();
|
|
}
|
|
|
|
public setup(core: CoreSetup<StartPlugins, OsqueryPluginStart>, plugins: SetupPlugins) {
|
|
this.logger.debug('osquery: Setup');
|
|
const config = createConfig(this.initializerContext);
|
|
|
|
if (!config.enabled) {
|
|
return {};
|
|
}
|
|
|
|
const router = core.http.createRouter();
|
|
|
|
const osqueryContext: OsqueryAppContext = {
|
|
logFactory: this.context.logger,
|
|
service: this.osqueryAppContextService,
|
|
config: (): ConfigType => config,
|
|
security: plugins.security,
|
|
};
|
|
|
|
initSavedObjects(core.savedObjects, osqueryContext);
|
|
initUsageCollectors({
|
|
core,
|
|
osqueryContext,
|
|
usageCollection: plugins.usageCollection,
|
|
});
|
|
defineRoutes(router, osqueryContext);
|
|
|
|
core.getStartServices().then(([, depsStart]) => {
|
|
const osquerySearchStrategy = osquerySearchStrategyProvider(depsStart.data);
|
|
|
|
plugins.data.search.registerSearchStrategy('osquerySearchStrategy', osquerySearchStrategy);
|
|
});
|
|
|
|
return {};
|
|
}
|
|
|
|
public start(core: CoreStart, plugins: StartPlugins) {
|
|
this.logger.debug('osquery: Started');
|
|
const registerIngestCallback = plugins.fleet?.registerExternalCallback;
|
|
|
|
this.osqueryAppContextService.start({
|
|
...plugins.fleet,
|
|
// @ts-expect-error update types
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
config: this.config!,
|
|
logger: this.logger,
|
|
registerIngestCallback,
|
|
});
|
|
|
|
return {};
|
|
}
|
|
|
|
public stop() {
|
|
this.logger.debug('osquery: Stopped');
|
|
this.osqueryAppContextService.stop();
|
|
}
|
|
}
|