diff --git a/.buildkite/ftr_oblt_stateful_configs.yml b/.buildkite/ftr_oblt_stateful_configs.yml index 55f3c1b603fca..eb8382eacce89 100644 --- a/.buildkite/ftr_oblt_stateful_configs.yml +++ b/.buildkite/ftr_oblt_stateful_configs.yml @@ -25,6 +25,7 @@ defaultQueue: 'n2-4-spot' enabled: - x-pack/test/alerting_api_integration/observability/config.ts - x-pack/test/api_integration/apis/logs_ui/config.ts + - x-pack/test/api_integration/apis/logs_shared/config.ts - x-pack/test/api_integration/apis/metrics_ui/config.ts - x-pack/test/api_integration/apis/osquery/config.ts - x-pack/test/api_integration/apis/synthetics/config.ts diff --git a/x-pack/plugins/observability_solution/logs_shared/server/routes/deprecations/migrate_log_view_settings.ts b/x-pack/plugins/observability_solution/logs_shared/server/routes/deprecations/migrate_log_view_settings.ts index e94faf208a4ed..08152eb850e51 100644 --- a/x-pack/plugins/observability_solution/logs_shared/server/routes/deprecations/migrate_log_view_settings.ts +++ b/x-pack/plugins/observability_solution/logs_shared/server/routes/deprecations/migrate_log_view_settings.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { defaultLogViewId } from '../../../common/log_views'; import { MIGRATE_LOG_VIEW_SETTINGS_URL } from '../../../common/http_api/deprecations'; import { logSourcesKibanaAdvancedSettingRT } from '../../../common'; import { LogsSharedBackendLibs } from '../../lib/logs_shared_types'; @@ -21,34 +22,41 @@ export const initMigrateLogViewSettingsRoute = ({ framework.router.put( { path: MIGRATE_LOG_VIEW_SETTINGS_URL, validate: false }, async (context, request, response) => { - const [_, pluginStartDeps, pluginStart] = await getStartServices(); + try { + const [_, pluginStartDeps, pluginStart] = await getStartServices(); - const logSourcesService = - await pluginStartDeps.logsDataAccess.services.logSourcesServiceFactory.getScopedLogSourcesService( - request - ); - const logViewsClient = pluginStart.logViews.getScopedClient(request); + const logSourcesService = + await pluginStartDeps.logsDataAccess.services.logSourcesServiceFactory.getScopedLogSourcesService( + request + ); + const logViewsClient = pluginStart.logViews.getScopedClient(request); - const logView = await logViewsClient.getLogView('default'); + const logView = await logViewsClient.getLogView('default'); - if (!logView || logSourcesKibanaAdvancedSettingRT.is(logView.attributes.logIndices)) { - throw new Error( - "Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting." - ); - } + if (!logView || logSourcesKibanaAdvancedSettingRT.is(logView.attributes.logIndices)) { + return response.customError({ + body: new Error( + "Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting." + ), + statusCode: 400, + }); + } - const indices = ( - await logViewsClient.getResolvedLogView({ - type: 'log-view-reference', - logViewId: 'default', - }) - ).indices; + const indices = ( + await logViewsClient.getResolvedLogView({ + type: 'log-view-reference', + logViewId: defaultLogViewId, + }) + ).indices; - await logSourcesService.setLogSources([{ indexPattern: indices }]); - await logViewsClient.putLogView('default', { - logIndices: { type: 'kibana_advanced_setting' }, - }); - return response.ok(); + await logSourcesService.setLogSources([{ indexPattern: indices }]); + await logViewsClient.putLogView(defaultLogViewId, { + logIndices: { type: 'kibana_advanced_setting' }, + }); + return response.ok(); + } catch (error) { + throw error; + } } ); }; diff --git a/x-pack/test/api_integration/apis/logs_shared/config.ts b/x-pack/test/api_integration/apis/logs_shared/config.ts new file mode 100644 index 0000000000000..5f335f116fefe --- /dev/null +++ b/x-pack/test/api_integration/apis/logs_shared/config.ts @@ -0,0 +1,17 @@ +/* + * 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 { FtrConfigProviderContext } from '@kbn/test'; + +export default async function ({ readConfigFile }: FtrConfigProviderContext) { + const baseIntegrationTestsConfig = await readConfigFile(require.resolve('../../config.ts')); + + return { + ...baseIntegrationTestsConfig.getAll(), + testFiles: [require.resolve('.')], + }; +} diff --git a/x-pack/test/api_integration/apis/logs_shared/index.ts b/x-pack/test/api_integration/apis/logs_shared/index.ts new file mode 100644 index 0000000000000..157526c418754 --- /dev/null +++ b/x-pack/test/api_integration/apis/logs_shared/index.ts @@ -0,0 +1,14 @@ +/* + * 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 { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ loadTestFile }: FtrProviderContext) { + describe('Logs shared routes', () => { + loadTestFile(require.resolve('./migrate_log_view_settings')); + }); +} diff --git a/x-pack/test/api_integration/apis/logs_shared/migrate_log_view_settings.ts b/x-pack/test/api_integration/apis/logs_shared/migrate_log_view_settings.ts new file mode 100644 index 0000000000000..e3185fc82dacf --- /dev/null +++ b/x-pack/test/api_integration/apis/logs_shared/migrate_log_view_settings.ts @@ -0,0 +1,95 @@ +/* + * 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 expect from '@kbn/expect'; +import { LogViewAttributes } from '@kbn/logs-shared-plugin/common/log_views'; +import { infraSourceConfigurationSavedObjectName } from '@kbn/infra-plugin/server/lib/sources'; +import { logViewSavedObjectName } from '@kbn/logs-shared-plugin/server'; +import { defaultLogViewId } from '@kbn/logs-shared-plugin/common/log_views'; +import { MIGRATE_LOG_VIEW_SETTINGS_URL } from '@kbn/logs-shared-plugin/common/http_api/deprecations'; +import { OBSERVABILITY_LOGS_DATA_ACCESS_LOG_SOURCES_ID } from '@kbn/management-settings-ids'; +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ getService }: FtrProviderContext) { + const supertest = getService('supertest'); + const logViewsService = getService('infraLogViews'); + const kibanaServer = getService('kibanaServer'); + const retry = getService('retry'); + + const INDICES = 'logs-*,something-else-*,test-*'; + + describe('Log view settings migration', () => { + describe('Migration API', () => { + before(async () => { + await kibanaServer.savedObjects.clean({ + types: [infraSourceConfigurationSavedObjectName, logViewSavedObjectName], + }); + }); + + afterEach(async () => { + await kibanaServer.savedObjects.clean({ + types: [infraSourceConfigurationSavedObjectName, logViewSavedObjectName], + }); + }); + + it('performs a migration when the log view is not using the Kibana advanced setting', async () => { + const logViewAttributes: Partial = { + name: 'Test Log View 1', + description: 'Test Description 1', + logIndices: { type: 'index_name', indexName: INDICES }, + logColumns: [], + }; + + await logViewsService.putLogView(defaultLogViewId, { + attributes: logViewAttributes, + }); + + await supertest + .put(MIGRATE_LOG_VIEW_SETTINGS_URL) + .set({ + 'kbn-xsrf': 'some-xsrf-token', + }) + .send() + .expect(200); + + await retry.try(async () => { + const migratedLogView = await logViewsService.getLogView(defaultLogViewId); + expect(migratedLogView.data.attributes.logIndices.type).to.eql('kibana_advanced_setting'); + const uiSetting = await kibanaServer.uiSettings.get( + OBSERVABILITY_LOGS_DATA_ACCESS_LOG_SOURCES_ID + ); + expect(uiSetting).to.eql([INDICES]); + }); + }); + + it('should error when the log view is already using the Kibana advanced setting', async () => { + const logViewAttributes: Partial = { + name: 'Test Log View 1', + description: 'Test Description 1', + logIndices: { type: 'kibana_advanced_setting' }, + logColumns: [], + }; + + await logViewsService.putLogView(defaultLogViewId, { + attributes: logViewAttributes, + }); + + const response = await supertest + .put(MIGRATE_LOG_VIEW_SETTINGS_URL) + .set({ + 'kbn-xsrf': 'some-xsrf-token', + }) + .send() + .expect(400); + + expect(response.body.message).to.eql( + "Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting." + ); + }); + }); + }); +} diff --git a/x-pack/test/tsconfig.json b/x-pack/test/tsconfig.json index 3449df8d6f23c..fe82748f6e6ae 100644 --- a/x-pack/test/tsconfig.json +++ b/x-pack/test/tsconfig.json @@ -176,6 +176,7 @@ "@kbn/osquery-plugin", "@kbn/entities-schema", "@kbn/actions-simulators-plugin", - "@kbn/cases-api-integration-test-plugin" + "@kbn/cases-api-integration-test-plugin", + "@kbn/management-settings-ids" ] }