diff --git a/x-pack/plugins/ingest_manager/public/plugin.ts b/x-pack/plugins/ingest_manager/public/plugin.ts index 3eb2fad339b7d..5b78ce05f925e 100644 --- a/x-pack/plugins/ingest_manager/public/plugin.ts +++ b/x-pack/plugins/ingest_manager/public/plugin.ts @@ -28,7 +28,7 @@ export type IngestManagerSetup = void; */ export interface IngestManagerStart { registerDatasource: typeof registerDatasource; - success: boolean; + success?: boolean; error?: { message: string; }; @@ -81,8 +81,8 @@ export class IngestManagerPlugin try { const permissionsResponse = await core.http.get(appRoutesService.getCheckPermissionsPath()); if (permissionsResponse.success) { - const { isInitialized: success } = await core.http.post(setupRouteService.getSetupPath()); - return { success, registerDatasource }; + core.http.post(setupRouteService.getSetupPath()); + return { registerDatasource }; } else { throw new Error(permissionsResponse.error); } diff --git a/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts index eaf0e1a104b3e..11dea25f1bbb4 100644 --- a/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts @@ -28,6 +28,8 @@ import { removeInstallation, } from '../../services/epm/packages'; +import { appContextService } from '../../services'; + export const getCategoriesHandler: RequestHandler = async (context, request, response) => { try { const res = await getCategories(); @@ -125,6 +127,20 @@ export const installPackageHandler: RequestHandler { diff --git a/x-pack/plugins/ingest_manager/server/services/index.ts b/x-pack/plugins/ingest_manager/server/services/index.ts index 1a0fb262eeb7f..b3e04b1b3ef52 100644 --- a/x-pack/plugins/ingest_manager/server/services/index.ts +++ b/x-pack/plugins/ingest_manager/server/services/index.ts @@ -5,7 +5,7 @@ */ import { SavedObjectsClientContract } from 'kibana/server'; -import { AgentStatus } from '../types'; +import { AgentStatus, CallESAsCurrentUser } from '../types'; import * as settingsService from './settings'; export { ESIndexPatternSavedObjectService } from './es_index_pattern'; @@ -15,6 +15,7 @@ export { ESIndexPatternSavedObjectService } from './es_index_pattern'; export interface ESIndexPatternService { getESIndexPattern( savedObjectsClient: SavedObjectsClientContract, + callCluster: CallESAsCurrentUser, pkgName: string, datasetPath: string ): Promise; diff --git a/x-pack/plugins/ingest_manager/server/services/setup.ts b/x-pack/plugins/ingest_manager/server/services/setup.ts index b6e1bca7b9d05..d37db1cf6e250 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -34,67 +34,74 @@ export async function setupIngestManager( soClient: SavedObjectsClientContract, callCluster: CallESAsCurrentUser ) { - const [installedPackages, defaultOutput, config] = await Promise.all([ - // packages installed by default - ensureInstalledDefaultPackages(soClient, callCluster), - outputService.ensureDefaultOutput(soClient), - agentConfigService.ensureDefaultAgentConfig(soClient), - ensureDefaultIndices(callCluster), - settingsService.getSettings(soClient).catch((e: any) => { - if (e.isBoom && e.output.statusCode === 404) { - const http = appContextService.getHttpSetup(); - const serverInfo = http.getServerInfo(); - const basePath = http.basePath; - - const cloud = appContextService.getCloud(); - const cloudId = cloud?.isCloudEnabled && cloud.cloudId; - const cloudUrl = cloudId && decodeCloudId(cloudId)?.kibanaUrl; - const flagsUrl = appContextService.getConfig()?.fleet?.kibana?.host; - const defaultUrl = url.format({ - protocol: serverInfo.protocol, - hostname: serverInfo.host, - port: serverInfo.port, - pathname: basePath.serverBasePath, - }); - - return settingsService.saveSettings(soClient, { - agent_auto_upgrade: true, - package_auto_upgrade: true, - kibana_url: cloudUrl || flagsUrl || defaultUrl, - }); - } - - return Promise.reject(e); - }), - ]); - - // ensure default packages are added to the default conifg - const configWithDatasource = await agentConfigService.get(soClient, config.id, true); - if (!configWithDatasource) { - throw new Error('Config not found'); - } - if ( - configWithDatasource.datasources.length && - typeof configWithDatasource.datasources[0] === 'string' - ) { - throw new Error('Config not found'); - } - for (const installedPackage of installedPackages) { - const packageShouldBeInstalled = DEFAULT_AGENT_CONFIGS_PACKAGES.some( - (packageName) => installedPackage.name === packageName - ); - if (!packageShouldBeInstalled) { - continue; + try { + const [installedPackages, defaultOutput, config] = await Promise.all([ + // packages installed by default + ensureInstalledDefaultPackages(soClient, callCluster), + outputService.ensureDefaultOutput(soClient), + agentConfigService.ensureDefaultAgentConfig(soClient), + ensureDefaultIndices(callCluster), + settingsService.getSettings(soClient).catch((e: any) => { + if (e.isBoom && e.output.statusCode === 404) { + const http = appContextService.getHttpSetup(); + const serverInfo = http.getServerInfo(); + const basePath = http.basePath; + + const cloud = appContextService.getCloud(); + const cloudId = cloud?.isCloudEnabled && cloud.cloudId; + const cloudUrl = cloudId && decodeCloudId(cloudId)?.kibanaUrl; + const flagsUrl = appContextService.getConfig()?.fleet?.kibana?.host; + const defaultUrl = url.format({ + protocol: serverInfo.protocol, + hostname: serverInfo.host, + port: serverInfo.port, + pathname: basePath.serverBasePath, + }); + + return settingsService.saveSettings(soClient, { + agent_auto_upgrade: true, + package_auto_upgrade: true, + kibana_url: cloudUrl || flagsUrl || defaultUrl, + }); + } + + return Promise.reject(e); + }), + ]); + + // ensure default packages are added to the default conifg + const configWithDatasource = await agentConfigService.get(soClient, config.id, true); + if (!configWithDatasource) { + throw new Error('Config not found'); } + if ( + configWithDatasource.datasources.length && + typeof configWithDatasource.datasources[0] === 'string' + ) { + throw new Error('Config not found'); + } + for (const installedPackage of installedPackages) { + const packageShouldBeInstalled = DEFAULT_AGENT_CONFIGS_PACKAGES.some( + (packageName) => installedPackage.name === packageName + ); + if (!packageShouldBeInstalled) { + continue; + } - const isInstalled = configWithDatasource.datasources.some((d: Datasource | string) => { - return typeof d !== 'string' && d.package?.name === installedPackage.name; - }); + const isInstalled = configWithDatasource.datasources.some((d: Datasource | string) => { + return typeof d !== 'string' && d.package?.name === installedPackage.name; + }); - if (!isInstalled) { - await addPackageToConfig(soClient, installedPackage, configWithDatasource, defaultOutput); + if (!isInstalled) { + await addPackageToConfig(soClient, installedPackage, configWithDatasource, defaultOutput); + } } + } catch (e) { + appContextService.setIsInitialized({ status: 'error', error: e }); } + + appContextService.setIsInitialized({ status: 'success' }); + return; } export async function setupFleet( diff --git a/x-pack/plugins/security_solution/server/endpoint/alerts/index_pattern.ts b/x-pack/plugins/security_solution/server/endpoint/alerts/index_pattern.ts index 391aedecdd099..b3725422da6c9 100644 --- a/x-pack/plugins/security_solution/server/endpoint/alerts/index_pattern.ts +++ b/x-pack/plugins/security_solution/server/endpoint/alerts/index_pattern.ts @@ -60,6 +60,7 @@ export class IngestIndexPatternRetriever implements IndexPatternRetriever { try { const pattern = await this.service.getESIndexPattern( ctx.core.savedObjects.client, + ctx.core.elasticsearch.legacy.client.callAsCurrentUser, IngestIndexPatternRetriever.endpointPackageName, datasetPath );