From 1d570a5b971bc4f92a1391319557cad98c2f823f Mon Sep 17 00:00:00 2001 From: John Schulz Date: Mon, 8 Jun 2020 21:15:12 -0400 Subject: [PATCH 1/9] Quick pass at different approach --- x-pack/plugins/ingest_manager/public/plugin.ts | 6 +++--- .../ingest_manager/server/routes/setup/handlers.ts | 7 +++++-- .../ingest_manager/server/services/app_context.ts | 10 ++++++++++ x-pack/plugins/ingest_manager/server/services/setup.ts | 2 ++ 4 files changed, 20 insertions(+), 5 deletions(-) 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/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index 30eb6c0ae8caa..fac3d74590d29 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -77,9 +77,12 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser; const logger = appContextService.getLogger(); try { - await setupIngestManager(soClient, callCluster); + if (!appContextService.getIsInitialized()) { + await setupIngestManager(soClient, callCluster); + } + return response.ok({ - body: { isInitialized: true }, + body: { isInitialized: appContextService.getIsInitialized() }, }); } catch (e) { if (e instanceof IngestManagerError) { diff --git a/x-pack/plugins/ingest_manager/server/services/app_context.ts b/x-pack/plugins/ingest_manager/server/services/app_context.ts index 9e6220b6958f1..3ff8463b8e0a6 100644 --- a/x-pack/plugins/ingest_manager/server/services/app_context.ts +++ b/x-pack/plugins/ingest_manager/server/services/app_context.ts @@ -23,6 +23,7 @@ class AppContextService { private cloud?: CloudSetup; private logger: Logger | undefined; private httpSetup?: HttpServiceSetup; + private isInitialized?: boolean = false; public async start(appContext: IngestManagerAppContext) { this.encryptedSavedObjects = appContext.encryptedSavedObjects?.getClient(); @@ -33,6 +34,7 @@ class AppContextService { this.logger = appContext.logger; this.kibanaVersion = appContext.kibanaVersion; this.httpSetup = appContext.httpSetup; + this.isInitialized = false; if (appContext.config$) { this.config$ = appContext.config$; @@ -84,6 +86,14 @@ class AppContextService { return this.savedObjects; } + public getIsInitialized() { + return this.isInitialized; + } + + public setIsInitialized(value: boolean) { + this.isInitialized = value; + } + public getIsProductionMode() { return this.isProductionMode; } diff --git a/x-pack/plugins/ingest_manager/server/services/setup.ts b/x-pack/plugins/ingest_manager/server/services/setup.ts index b6e1bca7b9d05..e1673ef968cfb 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -95,6 +95,8 @@ export async function setupIngestManager( await addPackageToConfig(soClient, installedPackage, configWithDatasource, defaultOutput); } } + + appContextService.setIsInitialized(true); } export async function setupFleet( From 819341815d846948631602e33ef710f86ea6d1d6 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Tue, 9 Jun 2020 05:21:16 -0400 Subject: [PATCH 2/9] Use isInitalized getter --- x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index fac3d74590d29..6a825ef0c1ec8 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -62,7 +62,7 @@ export const createFleetSetupHandler: RequestHandler< }); return response.ok({ - body: { isInitialized: true }, + body: { isInitialized: appContextService.getIsInitialized() }, }); } catch (e) { return response.customError({ From 6393d81b940c469db5fb71acd3a3f5debf386a90 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Tue, 9 Jun 2020 16:43:38 -0400 Subject: [PATCH 3/9] Track isInitialized as a Promise. The tests in test/api_integration/apis/endpoint/alerts/index_pattern.ts which failed in CI pass locally. Pushing with logging so I can see if it behaves differently there. Will revert them when CI is passing --- .../ingest_manager/server/routes/setup/handlers.ts | 8 +++----- .../ingest_manager/server/services/app_context.ts | 5 ++--- .../ingest_manager/server/services/es_index_pattern.ts | 5 +++++ x-pack/plugins/ingest_manager/server/services/setup.ts | 9 ++++++++- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index 6a825ef0c1ec8..2bf9d326ee0af 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -62,7 +62,7 @@ export const createFleetSetupHandler: RequestHandler< }); return response.ok({ - body: { isInitialized: appContextService.getIsInitialized() }, + body: { isInitialized: true }, }); } catch (e) { return response.customError({ @@ -77,12 +77,10 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser; const logger = appContextService.getLogger(); try { - if (!appContextService.getIsInitialized()) { - await setupIngestManager(soClient, callCluster); - } + await setupIngestManager(soClient, callCluster); return response.ok({ - body: { isInitialized: appContextService.getIsInitialized() }, + body: { isInitialized: true }, }); } catch (e) { if (e instanceof IngestManagerError) { diff --git a/x-pack/plugins/ingest_manager/server/services/app_context.ts b/x-pack/plugins/ingest_manager/server/services/app_context.ts index 3ff8463b8e0a6..baf25ed719fc6 100644 --- a/x-pack/plugins/ingest_manager/server/services/app_context.ts +++ b/x-pack/plugins/ingest_manager/server/services/app_context.ts @@ -23,7 +23,7 @@ class AppContextService { private cloud?: CloudSetup; private logger: Logger | undefined; private httpSetup?: HttpServiceSetup; - private isInitialized?: boolean = false; + private isInitialized?: Promise; public async start(appContext: IngestManagerAppContext) { this.encryptedSavedObjects = appContext.encryptedSavedObjects?.getClient(); @@ -34,7 +34,6 @@ class AppContextService { this.logger = appContext.logger; this.kibanaVersion = appContext.kibanaVersion; this.httpSetup = appContext.httpSetup; - this.isInitialized = false; if (appContext.config$) { this.config$ = appContext.config$; @@ -91,7 +90,7 @@ class AppContextService { } public setIsInitialized(value: boolean) { - this.isInitialized = value; + this.isInitialized = Promise.resolve(value); } public getIsProductionMode() { diff --git a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts index fc2fe6d1c40e8..fb34832528066 100644 --- a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts +++ b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts @@ -6,6 +6,7 @@ import { SavedObjectsClientContract } from 'kibana/server'; import { getInstallation } from './epm/packages'; import { ESIndexPatternService } from '../../server'; +import { appContextService } from './index'; export class ESIndexPatternSavedObjectService implements ESIndexPatternService { public async getESIndexPattern( @@ -13,6 +14,10 @@ export class ESIndexPatternSavedObjectService implements ESIndexPatternService { pkgName: string, datasetPath: string ): Promise { + console.log( + 'ESIndexPatternSavedObjectService#getESIndexPattern()', + appContextService.getIsInitialized() + ); const installation = await getInstallation({ savedObjectsClient, pkgName }); return installation?.es_index_patterns[datasetPath]; } diff --git a/x-pack/plugins/ingest_manager/server/services/setup.ts b/x-pack/plugins/ingest_manager/server/services/setup.ts index e1673ef968cfb..547db702e22a3 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -34,6 +34,12 @@ export async function setupIngestManager( soClient: SavedObjectsClientContract, callCluster: CallESAsCurrentUser ) { + console.log('setupIngestManager()'); + if (appContextService.getIsInitialized()) { + console.log('setupIngestManager early exit'); + return; + } + console.log('setupIngestManager set it up'); const [installedPackages, defaultOutput, config] = await Promise.all([ // packages installed by default ensureInstalledDefaultPackages(soClient, callCluster), @@ -95,8 +101,9 @@ export async function setupIngestManager( await addPackageToConfig(soClient, installedPackage, configWithDatasource, defaultOutput); } } - + console.log('setupIngestManager WORKED'); appContextService.setIsInitialized(true); + return; } export async function setupFleet( From 9b4af9c67884628f56714aeb9e78c2c46f855093 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Tue, 9 Jun 2020 17:24:11 -0400 Subject: [PATCH 4/9] Disable lint errors for console.log for now --- .../ingest_manager/server/services/es_index_pattern.ts | 1 + x-pack/plugins/ingest_manager/server/services/setup.ts | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts index fb34832528066..2d55b14baf009 100644 --- a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts +++ b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts @@ -14,6 +14,7 @@ export class ESIndexPatternSavedObjectService implements ESIndexPatternService { pkgName: string, datasetPath: string ): Promise { + // eslint-disable-next-line no-console console.log( 'ESIndexPatternSavedObjectService#getESIndexPattern()', appContextService.getIsInitialized() diff --git a/x-pack/plugins/ingest_manager/server/services/setup.ts b/x-pack/plugins/ingest_manager/server/services/setup.ts index 547db702e22a3..33cbb5f25b99d 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -34,11 +34,14 @@ export async function setupIngestManager( soClient: SavedObjectsClientContract, callCluster: CallESAsCurrentUser ) { + // eslint-disable-next-line no-console console.log('setupIngestManager()'); if (appContextService.getIsInitialized()) { + // eslint-disable-next-line no-console console.log('setupIngestManager early exit'); return; } + // eslint-disable-next-line no-console console.log('setupIngestManager set it up'); const [installedPackages, defaultOutput, config] = await Promise.all([ // packages installed by default @@ -101,6 +104,7 @@ export async function setupIngestManager( await addPackageToConfig(soClient, installedPackage, configWithDatasource, defaultOutput); } } + // eslint-disable-next-line no-console console.log('setupIngestManager WORKED'); appContextService.setIsInitialized(true); return; From 9b66d22382191fe013663e868e5087e0f5a38d4a Mon Sep 17 00:00:00 2001 From: John Schulz Date: Wed, 10 Jun 2020 07:57:53 -0400 Subject: [PATCH 5/9] New isInitialized shape & error response in handler --- .../ingest_manager/server/routes/epm/handlers.ts | 16 ++++++++++++++++ .../server/services/app_context.ts | 13 +++++++++---- .../ingest_manager/server/services/setup.ts | 4 ++-- 3 files changed, 27 insertions(+), 6 deletions(-) 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; + private initializationState?: InitializationState; public async start(appContext: IngestManagerAppContext) { this.encryptedSavedObjects = appContext.encryptedSavedObjects?.getClient(); @@ -34,6 +38,7 @@ class AppContextService { this.logger = appContext.logger; this.kibanaVersion = appContext.kibanaVersion; this.httpSetup = appContext.httpSetup; + this.initializationState = { status: 'not_started' }; if (appContext.config$) { this.config$ = appContext.config$; @@ -86,11 +91,11 @@ class AppContextService { } public getIsInitialized() { - return this.isInitialized; + return this.initializationState; } - public setIsInitialized(value: boolean) { - this.isInitialized = Promise.resolve(value); + public setIsInitialized(vals: InitializationState) { + this.initializationState = vals; } public getIsProductionMode() { diff --git a/x-pack/plugins/ingest_manager/server/services/setup.ts b/x-pack/plugins/ingest_manager/server/services/setup.ts index 33cbb5f25b99d..d59b67708a8d7 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -36,7 +36,7 @@ export async function setupIngestManager( ) { // eslint-disable-next-line no-console console.log('setupIngestManager()'); - if (appContextService.getIsInitialized()) { + if (appContextService.getIsInitialized()?.status === 'success') { // eslint-disable-next-line no-console console.log('setupIngestManager early exit'); return; @@ -106,7 +106,7 @@ export async function setupIngestManager( } // eslint-disable-next-line no-console console.log('setupIngestManager WORKED'); - appContextService.setIsInitialized(true); + appContextService.setIsInitialized({ status: 'success' }); return; } From bde0cb9523cccbf965ac617b22a5bbe90fee1960 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Wed, 10 Jun 2020 11:22:55 -0400 Subject: [PATCH 6/9] Debugging. Only run failing suite --- x-pack/test/api_integration/apis/fleet/agents/enroll.ts | 3 ++- x-pack/test/api_integration/apis/fleet/agents/services.ts | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/x-pack/test/api_integration/apis/fleet/agents/enroll.ts b/x-pack/test/api_integration/apis/fleet/agents/enroll.ts index ca9a0d57a3ea6..26e402e1e0a4d 100644 --- a/x-pack/test/api_integration/apis/fleet/agents/enroll.ts +++ b/x-pack/test/api_integration/apis/fleet/agents/enroll.ts @@ -19,7 +19,8 @@ export default function (providerContext: FtrProviderContext) { const supertest = getSupertestWithoutAuth(providerContext); let apiKey: { id: string; api_key: string }; - describe('fleet_agents_enroll', () => { + // eslint-disable-next-line ban/ban + describe.only('fleet_agents_enroll', () => { before(async () => { await esArchiver.loadIfNeeded('fleet/agents'); diff --git a/x-pack/test/api_integration/apis/fleet/agents/services.ts b/x-pack/test/api_integration/apis/fleet/agents/services.ts index 86c5fb5032c7f..957e2907a762e 100644 --- a/x-pack/test/api_integration/apis/fleet/agents/services.ts +++ b/x-pack/test/api_integration/apis/fleet/agents/services.ts @@ -33,10 +33,16 @@ export function getEsClientForAPIKey({ getService }: FtrProviderContext, esApiKe export function setupIngest({ getService }: FtrProviderContext) { before(async () => { + // eslint-disable-next-line no-console + console.log('test fleet agents#setupIngest'); await getService('supertest').post(`/api/ingest_manager/setup`).set('kbn-xsrf', 'xxx').send(); + // eslint-disable-next-line no-console + console.log('test fleet agents#setupIngest ingest setup'); await getService('supertest') .post(`/api/ingest_manager/fleet/setup`) .set('kbn-xsrf', 'xxx') .send({ forceRecreate: true }); + // eslint-disable-next-line no-console + console.log('test fleet agents#setupIngest fleet setup'); }); } From f1c5902b362d3c092d81f38278a5d820c01949f2 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Wed, 10 Jun 2020 14:19:45 -0400 Subject: [PATCH 7/9] Add try/catch to setupIngestManager. Drop logging --- .../server/services/es_index_pattern.ts | 10 +- .../ingest_manager/server/services/index.ts | 3 +- .../ingest_manager/server/services/setup.ts | 124 +++++++++--------- .../server/endpoint/alerts/index_pattern.ts | 1 + .../apis/fleet/agents/enroll.ts | 3 +- .../apis/fleet/agents/services.ts | 6 - 6 files changed, 69 insertions(+), 78 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts index 2d55b14baf009..04e9fc75cabfe 100644 --- a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts +++ b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts @@ -6,19 +6,17 @@ import { SavedObjectsClientContract } from 'kibana/server'; import { getInstallation } from './epm/packages'; import { ESIndexPatternService } from '../../server'; -import { appContextService } from './index'; +import { CallESAsCurrentUser } from '../types'; +import { setupIngestManager } from './setup'; export class ESIndexPatternSavedObjectService implements ESIndexPatternService { public async getESIndexPattern( savedObjectsClient: SavedObjectsClientContract, + callCluster: CallESAsCurrentUser, pkgName: string, datasetPath: string ): Promise { - // eslint-disable-next-line no-console - console.log( - 'ESIndexPatternSavedObjectService#getESIndexPattern()', - appContextService.getIsInitialized() - ); + await setupIngestManager(savedObjectsClient, callCluster); const installation = await getInstallation({ savedObjectsClient, pkgName }); return installation?.es_index_patterns[datasetPath]; } 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 d59b67708a8d7..0e7e250380b8c 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -34,78 +34,76 @@ export async function setupIngestManager( soClient: SavedObjectsClientContract, callCluster: CallESAsCurrentUser ) { - // eslint-disable-next-line no-console - console.log('setupIngestManager()'); if (appContextService.getIsInitialized()?.status === 'success') { - // eslint-disable-next-line no-console - console.log('setupIngestManager early exit'); return; } - // eslint-disable-next-line no-console - console.log('setupIngestManager set it up'); - 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 }); } - // eslint-disable-next-line no-console - console.log('setupIngestManager WORKED'); + appContextService.setIsInitialized({ status: 'success' }); return; } 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 ); diff --git a/x-pack/test/api_integration/apis/fleet/agents/enroll.ts b/x-pack/test/api_integration/apis/fleet/agents/enroll.ts index 26e402e1e0a4d..ca9a0d57a3ea6 100644 --- a/x-pack/test/api_integration/apis/fleet/agents/enroll.ts +++ b/x-pack/test/api_integration/apis/fleet/agents/enroll.ts @@ -19,8 +19,7 @@ export default function (providerContext: FtrProviderContext) { const supertest = getSupertestWithoutAuth(providerContext); let apiKey: { id: string; api_key: string }; - // eslint-disable-next-line ban/ban - describe.only('fleet_agents_enroll', () => { + describe('fleet_agents_enroll', () => { before(async () => { await esArchiver.loadIfNeeded('fleet/agents'); diff --git a/x-pack/test/api_integration/apis/fleet/agents/services.ts b/x-pack/test/api_integration/apis/fleet/agents/services.ts index 957e2907a762e..86c5fb5032c7f 100644 --- a/x-pack/test/api_integration/apis/fleet/agents/services.ts +++ b/x-pack/test/api_integration/apis/fleet/agents/services.ts @@ -33,16 +33,10 @@ export function getEsClientForAPIKey({ getService }: FtrProviderContext, esApiKe export function setupIngest({ getService }: FtrProviderContext) { before(async () => { - // eslint-disable-next-line no-console - console.log('test fleet agents#setupIngest'); await getService('supertest').post(`/api/ingest_manager/setup`).set('kbn-xsrf', 'xxx').send(); - // eslint-disable-next-line no-console - console.log('test fleet agents#setupIngest ingest setup'); await getService('supertest') .post(`/api/ingest_manager/fleet/setup`) .set('kbn-xsrf', 'xxx') .send({ forceRecreate: true }); - // eslint-disable-next-line no-console - console.log('test fleet agents#setupIngest fleet setup'); }); } From d4ff7b1053c04f1d5898450283ec7eaaa980e154 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Thu, 11 Jun 2020 07:42:53 -0400 Subject: [PATCH 8/9] Remove early return. Maintain current behavior --- x-pack/plugins/ingest_manager/server/services/setup.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/setup.ts b/x-pack/plugins/ingest_manager/server/services/setup.ts index 0e7e250380b8c..d37db1cf6e250 100644 --- a/x-pack/plugins/ingest_manager/server/services/setup.ts +++ b/x-pack/plugins/ingest_manager/server/services/setup.ts @@ -34,10 +34,6 @@ export async function setupIngestManager( soClient: SavedObjectsClientContract, callCluster: CallESAsCurrentUser ) { - if (appContextService.getIsInitialized()?.status === 'success') { - return; - } - try { const [installedPackages, defaultOutput, config] = await Promise.all([ // packages installed by default From b4b0691cdb59eca81a0135450f0d69028523fbf9 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Thu, 11 Jun 2020 10:16:41 -0400 Subject: [PATCH 9/9] Don't await setupIngestManager --- .../plugins/ingest_manager/server/services/es_index_pattern.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts index 04e9fc75cabfe..ab8656a70cf7e 100644 --- a/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts +++ b/x-pack/plugins/ingest_manager/server/services/es_index_pattern.ts @@ -7,7 +7,6 @@ import { SavedObjectsClientContract } from 'kibana/server'; import { getInstallation } from './epm/packages'; import { ESIndexPatternService } from '../../server'; import { CallESAsCurrentUser } from '../types'; -import { setupIngestManager } from './setup'; export class ESIndexPatternSavedObjectService implements ESIndexPatternService { public async getESIndexPattern( @@ -16,7 +15,6 @@ export class ESIndexPatternSavedObjectService implements ESIndexPatternService { pkgName: string, datasetPath: string ): Promise { - await setupIngestManager(savedObjectsClient, callCluster); const installation = await getInstallation({ savedObjectsClient, pkgName }); return installation?.es_index_patterns[datasetPath]; }