From 9fed654b421e7dc8ee8e473c0cafd9686e3cac70 Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Tue, 1 Sep 2020 19:24:12 +0200 Subject: [PATCH] [7.x] xpack_main legacy plugin pre-removal cleanup (#76257) (#76358) * xpack_main legacy plugin pre-removal cleanup (#76257) * cleanup xpack_main legacy plugin, remove capabilities mixin * fix test env * delete injectXpackSignature and related tests # Conflicts: # x-pack/legacy/plugins/xpack_main/index.js # x-pack/scripts/functional_tests.js * remove unused keys * create the xpackLegacy plugin * update plugin list * fix type --- docs/developer/plugin-list.asciidoc | 4 + .../capabilities/capabilities_mixin.test.ts | 65 ----- .../server/capabilities/capabilities_mixin.ts | 42 ---- src/legacy/server/capabilities/index.ts | 20 -- src/legacy/server/kbn_server.js | 4 - x-pack/.i18nrc.json | 2 +- x-pack/legacy/plugins/xpack_main/index.js | 51 ---- .../lib/__tests__/call_cluster_factory.js | 77 ------ .../__tests__/inject_xpack_info_signature.js | 125 ---------- .../lib/__tests__/replace_injected_vars.js | 225 ------------------ .../server/lib/__tests__/setup_xpack_main.js | 22 -- .../server/lib/call_cluster_factory.js | 45 ---- .../server/lib/inject_xpack_info_signature.js | 25 -- .../server/lib/replace_injected_vars.js | 38 --- .../xpack_main/server/lib/setup_xpack_main.js | 6 - .../plugins/xpack_main/server/xpack_main.d.ts | 1 - x-pack/plugins/features/server/plugin.test.ts | 43 ++-- x-pack/plugins/features/server/plugin.ts | 7 +- x-pack/plugins/xpack_legacy/kibana.json | 8 + x-pack/plugins/xpack_legacy/server/index.ts | 11 + x-pack/plugins/xpack_legacy/server/plugin.ts | 46 ++++ x-pack/scripts/functional_tests.js | 1 - x-pack/test/licensing_plugin/config.legacy.ts | 15 -- x-pack/test/licensing_plugin/legacy/index.ts | 16 -- .../test/licensing_plugin/legacy/updates.ts | 58 ----- 25 files changed, 105 insertions(+), 852 deletions(-) delete mode 100644 src/legacy/server/capabilities/capabilities_mixin.test.ts delete mode 100644 src/legacy/server/capabilities/capabilities_mixin.ts delete mode 100644 src/legacy/server/capabilities/index.ts delete mode 100644 x-pack/legacy/plugins/xpack_main/server/lib/__tests__/call_cluster_factory.js delete mode 100644 x-pack/legacy/plugins/xpack_main/server/lib/__tests__/inject_xpack_info_signature.js delete mode 100644 x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js delete mode 100644 x-pack/legacy/plugins/xpack_main/server/lib/call_cluster_factory.js delete mode 100644 x-pack/legacy/plugins/xpack_main/server/lib/inject_xpack_info_signature.js delete mode 100644 x-pack/legacy/plugins/xpack_main/server/lib/replace_injected_vars.js create mode 100644 x-pack/plugins/xpack_legacy/kibana.json create mode 100644 x-pack/plugins/xpack_legacy/server/index.ts create mode 100644 x-pack/plugins/xpack_legacy/server/plugin.ts delete mode 100644 x-pack/test/licensing_plugin/config.legacy.ts delete mode 100644 x-pack/test/licensing_plugin/legacy/index.ts delete mode 100644 x-pack/test/licensing_plugin/legacy/updates.ts diff --git a/docs/developer/plugin-list.asciidoc b/docs/developer/plugin-list.asciidoc index 85dd8ff3b16109..751d4a5e6bfee0 100644 --- a/docs/developer/plugin-list.asciidoc +++ b/docs/developer/plugin-list.asciidoc @@ -490,6 +490,10 @@ in their infrastructure. |This plugins adopts some conventions in addition to or in place of conventions in Kibana (at the time of the plugin's creation): +|{kib-repo}blob/{branch}/x-pack/plugins/xpack_legacy[xpackLegacy] +|WARNING: Missing README. + + |=== include::{kibana-root}/src/plugins/dashboard/README.asciidoc[leveloffset=+1] diff --git a/src/legacy/server/capabilities/capabilities_mixin.test.ts b/src/legacy/server/capabilities/capabilities_mixin.test.ts deleted file mode 100644 index 3422d6a8cbb346..00000000000000 --- a/src/legacy/server/capabilities/capabilities_mixin.test.ts +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { Server } from 'hapi'; -import KbnServer from '../kbn_server'; - -import { capabilitiesMixin } from './capabilities_mixin'; - -describe('capabilitiesMixin', () => { - let registerMock: jest.Mock; - - const getKbnServer = (pluginSpecs: any[] = []) => { - return ({ - afterPluginsInit: (callback: () => void) => callback(), - pluginSpecs, - newPlatform: { - setup: { - core: { - capabilities: { - registerProvider: registerMock, - }, - }, - }, - }, - } as unknown) as KbnServer; - }; - - let server: Server; - beforeEach(() => { - server = new Server(); - server.getUiNavLinks = () => []; - registerMock = jest.fn(); - }); - - it('calls capabilities#registerCapabilitiesProvider for each legacy plugin specs', async () => { - const getPluginSpec = (provider: () => any) => ({ - getUiCapabilitiesProvider: () => provider, - }); - - const capaA = { catalogue: { A: true } }; - const capaB = { catalogue: { B: true } }; - const kbnServer = getKbnServer([getPluginSpec(() => capaA), getPluginSpec(() => capaB)]); - await capabilitiesMixin(kbnServer, server); - - expect(registerMock).toHaveBeenCalledTimes(2); - expect(registerMock.mock.calls[0][0]()).toEqual(capaA); - expect(registerMock.mock.calls[1][0]()).toEqual(capaB); - }); -}); diff --git a/src/legacy/server/capabilities/capabilities_mixin.ts b/src/legacy/server/capabilities/capabilities_mixin.ts deleted file mode 100644 index 1f8c869f17f663..00000000000000 --- a/src/legacy/server/capabilities/capabilities_mixin.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { Server } from 'hapi'; -import KbnServer from '../kbn_server'; - -export async function capabilitiesMixin(kbnServer: KbnServer, server: Server) { - const registerLegacyCapabilities = async () => { - const capabilitiesList = await Promise.all( - kbnServer.pluginSpecs - .map((spec) => spec.getUiCapabilitiesProvider()) - .filter((provider) => !!provider) - .map((provider) => provider(server)) - ); - - capabilitiesList.forEach((capabilities) => { - kbnServer.newPlatform.setup.core.capabilities.registerProvider(() => capabilities); - }); - }; - - // Some plugin capabilities are derived from data provided by other plugins, - // so we need to wait until after all plugins have been init'd to fetch uiCapabilities. - kbnServer.afterPluginsInit(async () => { - await registerLegacyCapabilities(); - }); -} diff --git a/src/legacy/server/capabilities/index.ts b/src/legacy/server/capabilities/index.ts deleted file mode 100644 index 8c5dea1226f2b6..00000000000000 --- a/src/legacy/server/capabilities/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -export { capabilitiesMixin } from './capabilities_mixin'; diff --git a/src/legacy/server/kbn_server.js b/src/legacy/server/kbn_server.js index 1084521235ea03..320086b6d65310 100644 --- a/src/legacy/server/kbn_server.js +++ b/src/legacy/server/kbn_server.js @@ -34,7 +34,6 @@ import configCompleteMixin from './config/complete'; import { optimizeMixin } from '../../optimize'; import * as Plugins from './plugins'; import { savedObjectsMixin } from './saved_objects/saved_objects_mixin'; -import { capabilitiesMixin } from './capabilities'; import { serverExtensionsMixin } from './server_extensions'; import { uiMixin } from '../ui'; import { i18nMixin } from './i18n'; @@ -115,9 +114,6 @@ export default class KbnServer { // setup saved object routes savedObjectsMixin, - // setup capabilities routes - capabilitiesMixin, - // setup routes that serve the @kbn/optimizer output optimizeMixin, diff --git a/x-pack/.i18nrc.json b/x-pack/.i18nrc.json index 7d21f958bb80b4..90e36734770620 100644 --- a/x-pack/.i18nrc.json +++ b/x-pack/.i18nrc.json @@ -32,7 +32,7 @@ "xpack.licenseMgmt": "plugins/license_management", "xpack.licensing": "plugins/licensing", "xpack.logstash": ["plugins/logstash", "legacy/plugins/logstash"], - "xpack.main": "legacy/plugins/xpack_main", + "xpack.main": ["legacy/plugins/xpack_main", "plugins/xpack_legacy"], "xpack.maps": ["plugins/maps", "legacy/plugins/maps"], "xpack.ml": ["plugins/ml", "legacy/plugins/ml"], "xpack.monitoring": ["plugins/monitoring"], diff --git a/x-pack/legacy/plugins/xpack_main/index.js b/x-pack/legacy/plugins/xpack_main/index.js index ee4bb9721d0f7d..ccb0a386f2b430 100644 --- a/x-pack/legacy/plugins/xpack_main/index.js +++ b/x-pack/legacy/plugins/xpack_main/index.js @@ -5,14 +5,9 @@ */ import { resolve } from 'path'; -import { XPACK_DEFAULT_ADMIN_EMAIL_UI_SETTING } from '../../server/lib/constants'; import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status'; -import { replaceInjectedVars } from './server/lib/replace_injected_vars'; import { setupXPackMain } from './server/lib/setup_xpack_main'; import { xpackInfoRoute, settingsRoute } from './server/routes/api/v1'; -import { i18n } from '@kbn/i18n'; - -export { callClusterFactory } from './server/lib/call_cluster_factory'; export const xpackMain = (kibana) => { return new kibana.Plugin({ @@ -32,53 +27,7 @@ export const xpackMain = (kibana) => { }).default(); }, - uiCapabilities(server) { - const featuresPlugin = server.newPlatform.setup.plugins.features; - if (!featuresPlugin) { - throw new Error('New Platform XPack Features plugin is not available.'); - } - return featuresPlugin.getFeaturesUICapabilities(); - }, - - uiExports: { - uiSettingDefaults: { - [XPACK_DEFAULT_ADMIN_EMAIL_UI_SETTING]: { - name: i18n.translate('xpack.main.uiSettings.adminEmailTitle', { - defaultMessage: 'Admin email', - }), - // TODO: change the description when email address is used for more things? - description: i18n.translate('xpack.main.uiSettings.adminEmailDescription', { - defaultMessage: - 'Recipient email address for X-Pack admin operations, such as Cluster Alert email notifications from Monitoring.', - }), - deprecation: { - message: i18n.translate('xpack.main.uiSettings.adminEmailDeprecation', { - defaultMessage: - 'This setting is deprecated and will not be supported in Kibana 8.0. Please configure `monitoring.cluster_alerts.email_notifications.email_address` in your kibana.yml settings.', - }), - docLinksKey: 'kibanaGeneralSettings', - }, - type: 'string', // TODO: Any way of ensuring this is a valid email address? - value: null, - }, - }, - replaceInjectedVars, - injectDefaultVars(server) { - const config = server.config(); - - return { - activeSpace: null, - spacesEnabled: config.get('xpack.spaces.enabled'), - }; - }, - }, - init(server) { - const featuresPlugin = server.newPlatform.setup.plugins.features; - if (!featuresPlugin) { - throw new Error('New Platform XPack Features plugin is not available.'); - } - mirrorPluginStatus(server.plugins.elasticsearch, this, 'yellow', 'red'); setupXPackMain(server); diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/call_cluster_factory.js b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/call_cluster_factory.js deleted file mode 100644 index abe0d327d7b5cb..00000000000000 --- a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/call_cluster_factory.js +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import expect from '@kbn/expect'; -import sinon from 'sinon'; -import { callClusterFactory } from '../call_cluster_factory'; - -describe('callClusterFactory', () => { - let mockServer; - let mockCluster; - - beforeEach(() => { - mockCluster = { - callWithRequest: sinon.stub().returns(Promise.resolve({ hits: { total: 0 } })), - callWithInternalUser: sinon.stub().returns(Promise.resolve({ hits: { total: 0 } })), - }; - mockServer = { - plugins: { - elasticsearch: { - getCluster: sinon.stub().withArgs('admin').returns(mockCluster), - }, - }, - log() {}, - }; - }); - - it('returns an object with getter methods', () => { - const _callClusterFactory = callClusterFactory(mockServer); - expect(_callClusterFactory).to.be.an('object'); - expect(_callClusterFactory.getCallClusterWithReq).to.be.an('function'); - expect(_callClusterFactory.getCallClusterInternal).to.be.an('function'); - expect(mockCluster.callWithRequest.called).to.be(false); - expect(mockCluster.callWithInternalUser.called).to.be(false); - }); - - describe('getCallClusterWithReq', () => { - it('throws an error if req is not passed', async () => { - const runCallCluster = () => callClusterFactory(mockServer).getCallClusterWithReq(); - expect(runCallCluster).to.throwException(); - }); - - it('returns a method that wraps callWithRequest', async () => { - const mockReq = { - headers: { - authorization: 'Basic dSQzcm5AbTM6cEAkJHcwcmQ=', // u$3rn@m3:p@$$w0rd - }, - }; - const callCluster = callClusterFactory(mockServer).getCallClusterWithReq(mockReq); - - const result = await callCluster('search', { body: { match: { match_all: {} } } }); - expect(result).to.eql({ hits: { total: 0 } }); - - expect(mockCluster.callWithInternalUser.called).to.be(false); - expect(mockCluster.callWithRequest.calledOnce).to.be(true); - const [req, method] = mockCluster.callWithRequest.getCall(0).args; - expect(req).to.be(mockReq); - expect(method).to.be('search'); - }); - }); - - describe('getCallClusterInternal', () => { - it('returns a method that wraps callWithInternalUser', async () => { - const callCluster = callClusterFactory(mockServer).getCallClusterInternal(); - - const result = await callCluster('search', { body: { match: { match_all: {} } } }); - expect(result).to.eql({ hits: { total: 0 } }); - - expect(mockCluster.callWithRequest.called).to.be(false); - expect(mockCluster.callWithInternalUser.calledOnce).to.be(true); - const [method] = mockCluster.callWithInternalUser.getCall(0).args; - expect(method).to.eql('search'); - }); - }); -}); diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/inject_xpack_info_signature.js b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/inject_xpack_info_signature.js deleted file mode 100644 index 420f3b2d6631cc..00000000000000 --- a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/inject_xpack_info_signature.js +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import sinon from 'sinon'; -import expect from '@kbn/expect'; -import { injectXPackInfoSignature } from '../inject_xpack_info_signature'; - -describe('injectXPackInfoSignature()', () => { - class MockErrorResponse extends Error { - constructor() { - super(); - this.output = { - headers: {}, - }; - - this.headers = {}; - } - } - - const fakeH = { continue: 'blah' }; - - let mockXPackInfo; - beforeEach(() => { - mockXPackInfo = sinon.stub({ - isAvailable() {}, - getSignature() {}, - refreshNow() {}, - }); - }); - - describe('error response', () => { - it('refreshes `xpackInfo` and do not inject signature if it is not available.', async () => { - mockXPackInfo.isAvailable.returns(true); - mockXPackInfo.getSignature.returns('this-should-never-be-set'); - - // We need this to make sure the code waits for `refreshNow` to complete before it tries - // to access its properties. - mockXPackInfo.refreshNow = () => { - return new Promise((resolve) => { - mockXPackInfo.isAvailable.returns(false); - resolve(); - }); - }; - - const mockResponse = new MockErrorResponse(); - const response = await injectXPackInfoSignature( - mockXPackInfo, - { response: mockResponse }, - fakeH - ); - - expect(mockResponse.headers).to.eql({}); - expect(mockResponse.output.headers).to.eql({}); - expect(response).to.be(fakeH.continue); - }); - - it('refreshes `xpackInfo` and injects its updated signature.', async () => { - mockXPackInfo.isAvailable.returns(true); - mockXPackInfo.getSignature.returns('old-signature'); - - // We need this to make sure the code waits for `refreshNow` to complete before it tries - // to access its properties. - mockXPackInfo.refreshNow = () => { - return new Promise((resolve) => { - mockXPackInfo.getSignature.returns('new-signature'); - resolve(); - }); - }; - - const mockResponse = new MockErrorResponse(); - const response = await injectXPackInfoSignature( - mockXPackInfo, - { response: mockResponse }, - fakeH - ); - - expect(mockResponse.headers).to.eql({}); - expect(mockResponse.output.headers).to.eql({ - 'kbn-xpack-sig': 'new-signature', - }); - expect(response).to.be(fakeH.continue); - }); - }); - - describe('non-error response', () => { - it('do not inject signature if `xpackInfo` is not available.', async () => { - mockXPackInfo.isAvailable.returns(false); - mockXPackInfo.getSignature.returns('this-should-never-be-set'); - - const mockResponse = { headers: {}, output: { headers: {} } }; - const response = await injectXPackInfoSignature( - mockXPackInfo, - { response: mockResponse }, - fakeH - ); - - expect(mockResponse.headers).to.eql({}); - expect(mockResponse.output.headers).to.eql({}); - sinon.assert.notCalled(mockXPackInfo.refreshNow); - expect(response).to.be(fakeH.continue); - }); - - it('injects signature if `xpackInfo` is available.', async () => { - mockXPackInfo.isAvailable.returns(true); - mockXPackInfo.getSignature.returns('available-signature'); - - const mockResponse = { headers: {}, output: { headers: {} } }; - const response = await injectXPackInfoSignature( - mockXPackInfo, - { response: mockResponse }, - fakeH - ); - - expect(mockResponse.headers).to.eql({ - 'kbn-xpack-sig': 'available-signature', - }); - expect(mockResponse.output.headers).to.eql({}); - sinon.assert.notCalled(mockXPackInfo.refreshNow); - expect(response).to.be(fakeH.continue); - }); - }); -}); diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js deleted file mode 100644 index ce6e20bd874b29..00000000000000 --- a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/replace_injected_vars.js +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import sinon from 'sinon'; -import expect from '@kbn/expect'; - -import { replaceInjectedVars } from '../replace_injected_vars'; -import { KibanaRequest } from '../../../../../../../src/core/server'; - -const buildRequest = (path = '/app/kibana') => { - const get = sinon.stub(); - - return { - app: {}, - path, - route: { settings: {} }, - headers: {}, - raw: { - req: { - socket: {}, - }, - }, - getSavedObjectsClient: () => { - return { - get, - create: sinon.stub(), - - errors: { - isNotFoundError: (error) => { - return error.message === 'not found exception'; - }, - }, - }; - }, - }; -}; - -describe('replaceInjectedVars uiExport', () => { - it('sends xpack info if request is authenticated and license is not basic', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: { - b: 1, - }, - }); - - sinon.assert.calledOnce(server.newPlatform.setup.plugins.security.authc.isAuthenticated); - sinon.assert.calledWithExactly( - server.newPlatform.setup.plugins.security.authc.isAuthenticated, - sinon.match.instanceOf(KibanaRequest) - ); - }); - - it('sends the xpack info if security plugin is disabled', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - delete server.plugins.security; - delete server.newPlatform.setup.plugins.security; - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: { - b: 1, - }, - }); - }); - - it('sends the xpack info if xpack license is basic', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - server.plugins.xpack_main.info.license.isOneOf.returns(true); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: { - b: 1, - }, - }); - }); - - it('respects the telemetry opt-in document when opted-out', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - server.plugins.xpack_main.info.license.isOneOf.returns(true); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: { - b: 1, - }, - }); - }); - - it('respects the telemetry opt-in document when opted-in', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - server.plugins.xpack_main.info.license.isOneOf.returns(true); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: { - b: 1, - }, - }); - }); - - it('indicates that telemetry is opted-out when not loading an application', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(true, '/'); - const server = mockServer(); - server.plugins.xpack_main.info.license.isOneOf.returns(true); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: { - b: 1, - }, - }); - }); - - it('sends the originalInjectedVars if not authenticated', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - server.newPlatform.setup.plugins.security.authc.isAuthenticated.returns(false); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql(originalInjectedVars); - }); - - it('sends the originalInjectedVars if xpack info is unavailable', async () => { - const originalInjectedVars = { a: 1 }; - const request = buildRequest(); - const server = mockServer(); - server.plugins.xpack_main.info.isAvailable.returns(false); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql(originalInjectedVars); - }); - - it('sends the originalInjectedVars (with xpackInitialInfo = undefined) if security is disabled, xpack info is unavailable', async () => { - const originalInjectedVars = { - a: 1, - uiCapabilities: { navLinks: { foo: true }, bar: { baz: true }, catalogue: { cfoo: true } }, - }; - const request = buildRequest(); - const server = mockServer(); - delete server.plugins.security; - server.plugins.xpack_main.info.isAvailable.returns(false); - - const newVars = await replaceInjectedVars(originalInjectedVars, request, server); - expect(newVars).to.eql({ - a: 1, - xpackInitialInfo: undefined, - uiCapabilities: { - navLinks: { foo: true }, - bar: { baz: true }, - catalogue: { - cfoo: true, - }, - }, - }); - }); -}); - -// creates a mock server object that defaults to being authenticated with a -// non-basic license -function mockServer() { - const getLicenseCheckResults = sinon.stub().returns({}); - return { - newPlatform: { - setup: { - plugins: { security: { authc: { isAuthenticated: sinon.stub().returns(true) } } }, - }, - }, - plugins: { - security: {}, - xpack_main: { - getFeatures: () => [ - { - id: 'mockFeature', - name: 'Mock Feature', - privileges: { - all: { - app: [], - savedObject: { - all: [], - read: [], - }, - ui: ['mockFeatureCapability'], - }, - }, - }, - ], - info: { - isAvailable: sinon.stub().returns(true), - feature: () => ({ - getLicenseCheckResults, - }), - license: { - isOneOf: sinon.stub().returns(false), - }, - toJSON: () => ({ b: 1 }), - }, - }, - }, - }; -} diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/setup_xpack_main.js b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/setup_xpack_main.js index b4a2c090d63092..c34e27642d2ceb 100644 --- a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/setup_xpack_main.js +++ b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/setup_xpack_main.js @@ -8,7 +8,6 @@ import { BehaviorSubject } from 'rxjs'; import sinon from 'sinon'; import { XPackInfo } from '../xpack_info'; import { setupXPackMain } from '../setup_xpack_main'; -import * as InjectXPackInfoSignatureNS from '../inject_xpack_info_signature'; describe('setupXPackMain()', () => { const sandbox = sinon.createSandbox(); @@ -19,7 +18,6 @@ describe('setupXPackMain()', () => { beforeEach(() => { sandbox.useFakeTimers(); - sandbox.stub(InjectXPackInfoSignatureNS, 'injectXPackInfoSignature'); mockElasticsearchPlugin = { getCluster: sinon.stub(), @@ -63,29 +61,9 @@ describe('setupXPackMain()', () => { setupXPackMain(mockServer); sinon.assert.calledWithExactly(mockServer.expose, 'info', sinon.match.instanceOf(XPackInfo)); - sinon.assert.calledWithExactly(mockServer.ext, 'onPreResponse', sinon.match.func); sinon.assert.calledWithExactly(mockElasticsearchPlugin.status.on, 'change', sinon.match.func); }); - it('onPreResponse hook calls `injectXPackInfoSignature` for every request.', () => { - setupXPackMain(mockServer); - - const xPackInfo = mockServer.expose.firstCall.args[1]; - const onPreResponse = mockServer.ext.firstCall.args[1]; - - const mockRequest = {}; - const mockReply = sinon.stub(); - - onPreResponse(mockRequest, mockReply); - - sinon.assert.calledWithExactly( - InjectXPackInfoSignatureNS.injectXPackInfoSignature, - xPackInfo, - sinon.match.same(mockRequest), - sinon.match.same(mockReply) - ); - }); - describe('Elasticsearch plugin state changes cause XPackMain plugin state change.', () => { let xPackInfo; let onElasticsearchPluginStatusChange; diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/call_cluster_factory.js b/x-pack/legacy/plugins/xpack_main/server/lib/call_cluster_factory.js deleted file mode 100644 index f946725e017ffd..00000000000000 --- a/x-pack/legacy/plugins/xpack_main/server/lib/call_cluster_factory.js +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -/* - * Factory for acquiring a method that can send an authenticated query to Elasticsearch. - * - * The method can either: - * - take authentication from an HTTP request object, which should be used when - * the caller is an HTTP API - * - fabricate authentication using the system username and password from the - * Kibana config, which should be used when the caller is an internal process - * - * @param {Object} server: the Kibana server object - * @return {Function}: callCluster function - */ -export function callClusterFactory(server) { - const { callWithRequest, callWithInternalUser } = server.plugins.elasticsearch.getCluster( - 'admin' - ); - - return { - /* - * caller is coming from a request, so use callWithRequest with actual - * Authorization header credentials - * @param {Object} req: HTTP request object - */ - getCallClusterWithReq(req) { - if (req === undefined) { - throw new Error('request object is required'); - } - return (...args) => callWithRequest(req, ...args); - }, - - getCallClusterInternal() { - /* - * caller is an internal function of the stats collection system, so use - * internal system user - */ - return callWithInternalUser; - }, - }; -} diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/inject_xpack_info_signature.js b/x-pack/legacy/plugins/xpack_main/server/lib/inject_xpack_info_signature.js deleted file mode 100644 index 166bd2b4755f05..00000000000000 --- a/x-pack/legacy/plugins/xpack_main/server/lib/inject_xpack_info_signature.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -export async function injectXPackInfoSignature(info, request, h) { - // If we're returning an error response, refresh xpack info from - // Elasticsearch in case the error is due to a change in license information - // in Elasticsearch. - const isErrorResponse = request.response instanceof Error; - if (isErrorResponse) { - await info.refreshNow(); - } - - if (info.isAvailable()) { - // Note: request.response.output is used instead of request.response because - // evidently HAPI does not allow headers to be set on the latter in case of - // error responses. - const response = isErrorResponse ? request.response.output : request.response; - response.headers['kbn-xpack-sig'] = info.getSignature(); - } - - return h.continue; -} diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/replace_injected_vars.js b/x-pack/legacy/plugins/xpack_main/server/lib/replace_injected_vars.js deleted file mode 100644 index f09f97d44bfe8b..00000000000000 --- a/x-pack/legacy/plugins/xpack_main/server/lib/replace_injected_vars.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { KibanaRequest } from '../../../../../../src/core/server'; - -export async function replaceInjectedVars(originalInjectedVars, request, server) { - const xpackInfo = server.plugins.xpack_main.info; - - const withXpackInfo = async () => ({ - ...originalInjectedVars, - xpackInitialInfo: xpackInfo.isAvailable() ? xpackInfo.toJSON() : undefined, - }); - - // security feature is disabled - if (!server.plugins.security || !server.newPlatform.setup.plugins.security) { - return await withXpackInfo(); - } - - // not enough license info to make decision one way or another - if (!xpackInfo.isAvailable()) { - return originalInjectedVars; - } - - // request is not authenticated - if ( - !(await server.newPlatform.setup.plugins.security.authc.isAuthenticated( - KibanaRequest.from(request) - )) - ) { - return originalInjectedVars; - } - - // plugin enabled, license is appropriate, request is authenticated - return await withXpackInfo(); -} diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/setup_xpack_main.js b/x-pack/legacy/plugins/xpack_main/server/lib/setup_xpack_main.js index 9196b210bba20a..33b551bbe864ff 100644 --- a/x-pack/legacy/plugins/xpack_main/server/lib/setup_xpack_main.js +++ b/x-pack/legacy/plugins/xpack_main/server/lib/setup_xpack_main.js @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { injectXPackInfoSignature } from './inject_xpack_info_signature'; import { XPackInfo } from './xpack_info'; /** @@ -20,11 +19,6 @@ export function setupXPackMain(server) { server.expose('info', info); - server.ext('onPreResponse', (request, h) => injectXPackInfoSignature(info, request, h)); - - const { getFeatures } = server.newPlatform.setup.plugins.features; - server.expose('getFeatures', getFeatures); - const setPluginStatus = () => { if (info.isAvailable()) { server.plugins.xpack_main.status.green('Ready'); diff --git a/x-pack/legacy/plugins/xpack_main/server/xpack_main.d.ts b/x-pack/legacy/plugins/xpack_main/server/xpack_main.d.ts index 2a59c2f1366d4a..f4363a8e57b37c 100644 --- a/x-pack/legacy/plugins/xpack_main/server/xpack_main.d.ts +++ b/x-pack/legacy/plugins/xpack_main/server/xpack_main.d.ts @@ -11,5 +11,4 @@ export { XPackFeature } from './lib/xpack_info'; export interface XPackMainPlugin { info: XPackInfo; - getFeatures(): Feature[]; } diff --git a/x-pack/plugins/features/server/plugin.test.ts b/x-pack/plugins/features/server/plugin.test.ts index 3d85c2e9eb751c..00d578f5ca8664 100644 --- a/x-pack/plugins/features/server/plugin.test.ts +++ b/x-pack/plugins/features/server/plugin.test.ts @@ -4,23 +4,30 @@ * you may not use this file except in compliance with the Elastic License. */ import { coreMock, savedObjectsServiceMock } from 'src/core/server/mocks'; - import { Plugin } from './plugin'; -const initContext = coreMock.createPluginInitializerContext(); -const coreSetup = coreMock.createSetup(); -const coreStart = coreMock.createStart(); -const typeRegistry = savedObjectsServiceMock.createTypeRegistryMock(); -typeRegistry.getVisibleTypes.mockReturnValue([ - { - name: 'foo', - hidden: false, - mappings: { properties: {} }, - namespaceType: 'single' as 'single', - }, -]); -coreStart.savedObjects.getTypeRegistry.mockReturnValue(typeRegistry); describe('Features Plugin', () => { + let initContext: ReturnType; + let coreSetup: ReturnType; + let coreStart: ReturnType; + let typeRegistry: ReturnType; + + beforeEach(() => { + initContext = coreMock.createPluginInitializerContext(); + coreSetup = coreMock.createSetup(); + coreStart = coreMock.createStart(); + typeRegistry = savedObjectsServiceMock.createTypeRegistryMock(); + typeRegistry.getVisibleTypes.mockReturnValue([ + { + name: 'foo', + hidden: false, + mappings: { properties: {} }, + namespaceType: 'single' as 'single', + }, + ]); + coreStart.savedObjects.getTypeRegistry.mockReturnValue(typeRegistry); + }); + it('returns OSS + registered features', async () => { const plugin = new Plugin(initContext); const { registerFeature } = await plugin.setup(coreSetup, {}); @@ -88,4 +95,12 @@ describe('Features Plugin', () => { expect(soTypes.includes('foo')).toBe(true); expect(soTypes.includes('bar')).toBe(false); }); + + it('registers a capabilities provider', async () => { + const plugin = new Plugin(initContext); + await plugin.setup(coreSetup, {}); + + expect(coreSetup.capabilities.registerProvider).toHaveBeenCalledTimes(1); + expect(coreSetup.capabilities.registerProvider).toHaveBeenCalledWith(expect.any(Function)); + }); }); diff --git a/x-pack/plugins/features/server/plugin.ts b/x-pack/plugins/features/server/plugin.ts index 5783b20eae6484..61b66d95ca44f3 100644 --- a/x-pack/plugins/features/server/plugin.ts +++ b/x-pack/plugins/features/server/plugin.ts @@ -61,10 +61,15 @@ export class Plugin { featureRegistry: this.featureRegistry, }); + const getFeaturesUICapabilities = () => + uiCapabilitiesForFeatures(this.featureRegistry.getAll()); + + core.capabilities.registerProvider(getFeaturesUICapabilities); + return deepFreeze({ registerFeature: this.featureRegistry.register.bind(this.featureRegistry), getFeatures: this.featureRegistry.getAll.bind(this.featureRegistry), - getFeaturesUICapabilities: () => uiCapabilitiesForFeatures(this.featureRegistry.getAll()), + getFeaturesUICapabilities, }); } diff --git a/x-pack/plugins/xpack_legacy/kibana.json b/x-pack/plugins/xpack_legacy/kibana.json new file mode 100644 index 00000000000000..d1ad5e74a7a699 --- /dev/null +++ b/x-pack/plugins/xpack_legacy/kibana.json @@ -0,0 +1,8 @@ +{ + "id": "xpackLegacy", + "version": "8.0.0", + "kibanaVersion": "kibana", + "server": true, + "ui": false, + "requiredPlugins": [] +} diff --git a/x-pack/plugins/xpack_legacy/server/index.ts b/x-pack/plugins/xpack_legacy/server/index.ts new file mode 100644 index 00000000000000..ecdee0692fc9d5 --- /dev/null +++ b/x-pack/plugins/xpack_legacy/server/index.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { PluginInitializerContext } from '../../../../src/core/server'; +import { XpackLegacyPlugin } from './plugin'; + +export const plugin = (initializerContext: PluginInitializerContext) => + new XpackLegacyPlugin(initializerContext); diff --git a/x-pack/plugins/xpack_legacy/server/plugin.ts b/x-pack/plugins/xpack_legacy/server/plugin.ts new file mode 100644 index 00000000000000..10bac4e66f9dcc --- /dev/null +++ b/x-pack/plugins/xpack_legacy/server/plugin.ts @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import { i18n } from '@kbn/i18n'; +import { + PluginInitializerContext, + CoreStart, + CoreSetup, + Plugin, +} from '../../../../src/core/server'; + +export class XpackLegacyPlugin implements Plugin { + constructor(_initializerContext: PluginInitializerContext) {} + + public setup(core: CoreSetup) { + core.uiSettings.register({ + 'xPack:defaultAdminEmail': { + name: i18n.translate('xpack.main.uiSettings.adminEmailTitle', { + defaultMessage: 'Admin email', + }), + description: i18n.translate('xpack.main.uiSettings.adminEmailDescription', { + defaultMessage: + 'Recipient email address for X-Pack admin operations, such as Cluster Alert email notifications from Monitoring.', + }), + deprecation: { + message: i18n.translate('xpack.main.uiSettings.adminEmailDeprecation', { + defaultMessage: + 'This setting is deprecated and will not be supported in Kibana 8.0. Please configure `monitoring.cluster_alerts.email_notifications.email_address` in your kibana.yml settings.', + }), + docLinksKey: 'kibanaGeneralSettings', + }, + type: 'string', + value: '', + schema: schema.maybe(schema.string()), + }, + }); + } + + public start(core: CoreStart) {} + + public stop() {} +} diff --git a/x-pack/scripts/functional_tests.js b/x-pack/scripts/functional_tests.js index df02660c76b647..3494282382eac9 100644 --- a/x-pack/scripts/functional_tests.js +++ b/x-pack/scripts/functional_tests.js @@ -54,7 +54,6 @@ require('@kbn/test').runTestsCli([ require.resolve('../test/upgrade_assistant_integration/config'), require.resolve('../test/licensing_plugin/config'), require.resolve('../test/licensing_plugin/config.public'), - require.resolve('../test/licensing_plugin/config.legacy'), require.resolve('../test/endpoint_api_integration_no_ingest/config.ts'), require.resolve('../test/functional_embedded/config.ts'), require.resolve('../test/reporting_api_integration/reporting_and_security.config.ts'), diff --git a/x-pack/test/licensing_plugin/config.legacy.ts b/x-pack/test/licensing_plugin/config.legacy.ts deleted file mode 100644 index 14eadc3194f9e3..00000000000000 --- a/x-pack/test/licensing_plugin/config.legacy.ts +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -import { FtrConfigProviderContext } from '@kbn/test/types/ftr'; - -export default async function ({ readConfigFile }: FtrConfigProviderContext) { - const commonConfig = await readConfigFile(require.resolve('./config')); - - return { - ...commonConfig.getAll(), - testFiles: [require.resolve('./legacy')], - }; -} diff --git a/x-pack/test/licensing_plugin/legacy/index.ts b/x-pack/test/licensing_plugin/legacy/index.ts deleted file mode 100644 index 6274bd39690420..00000000000000 --- a/x-pack/test/licensing_plugin/legacy/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { FtrProviderContext } from '../services'; - -// eslint-disable-next-line import/no-default-export -export default function ({ loadTestFile }: FtrProviderContext) { - describe('Legacy licensing plugin', function () { - this.tags('ciGroup2'); - // MUST BE LAST! CHANGES LICENSE TYPE! - loadTestFile(require.resolve('./updates')); - }); -} diff --git a/x-pack/test/licensing_plugin/legacy/updates.ts b/x-pack/test/licensing_plugin/legacy/updates.ts deleted file mode 100644 index 1de8659672d2f5..00000000000000 --- a/x-pack/test/licensing_plugin/legacy/updates.ts +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -import expect from '@kbn/expect'; -import { FtrProviderContext } from '../services'; -import { createScenario } from '../scenario'; -import '../../../../test/plugin_functional/plugins/core_provider_plugin/types'; - -// eslint-disable-next-line import/no-default-export -export default function (ftrContext: FtrProviderContext) { - const { getService } = ftrContext; - const supertest = getService('supertest'); - const testSubjects = getService('testSubjects'); - - const scenario = createScenario(ftrContext); - - describe('changes in license types', () => { - after(async () => { - await scenario.teardown(); - }); - - it('provides changes in license types', async () => { - await scenario.setup(); - await scenario.waitForPluginToDetectLicenseUpdate(); - - const { - body: legacyInitialLicense, - header: legacyInitialLicenseHeaders, - } = await supertest.get('/api/xpack/v1/info').expect(200); - - expect(legacyInitialLicense.license?.type).to.be('basic'); - expect(legacyInitialLicenseHeaders['kbn-xpack-sig']).to.be.a('string'); - - await scenario.startTrial(); - await scenario.waitForPluginToDetectLicenseUpdate(); - - const { body: legacyTrialLicense, header: legacyTrialLicenseHeaders } = await supertest - .get('/api/xpack/v1/info') - .expect(200); - - expect(legacyTrialLicense.license?.type).to.be('trial'); - expect(legacyTrialLicenseHeaders['kbn-xpack-sig']).to.not.be( - legacyInitialLicenseHeaders['kbn-xpack-sig'] - ); - - await scenario.startBasic(); - await scenario.waitForPluginToDetectLicenseUpdate(); - - const { body: legacyBasicLicense } = await supertest.get('/api/xpack/v1/info').expect(200); - expect(legacyBasicLicense.license?.type).to.be('basic'); - - // banner shown only when license expired not just deleted - await testSubjects.missingOrFail('licenseExpiredBanner'); - }); - }); -}