diff --git a/src/core/server/saved_objects/export/get_sorted_objects_for_export.test.ts b/src/core/server/saved_objects/export/get_sorted_objects_for_export.test.ts index 9a3449b65a941..fafa04447ddfe 100644 --- a/src/core/server/saved_objects/export/get_sorted_objects_for_export.test.ts +++ b/src/core/server/saved_objects/export/get_sorted_objects_for_export.test.ts @@ -108,8 +108,6 @@ describe('getSortedObjectsForExport()', () => { "namespace": undefined, "perPage": 500, "search": undefined, - "sortField": "_id", - "sortOrder": "asc", "type": Array [ "index-pattern", "search", @@ -256,8 +254,6 @@ describe('getSortedObjectsForExport()', () => { "namespace": undefined, "perPage": 500, "search": "foo", - "sortField": "_id", - "sortOrder": "asc", "type": Array [ "index-pattern", "search", @@ -345,8 +341,6 @@ describe('getSortedObjectsForExport()', () => { "namespace": "foo", "perPage": 500, "search": undefined, - "sortField": "_id", - "sortOrder": "asc", "type": Array [ "index-pattern", "search", @@ -399,6 +393,79 @@ describe('getSortedObjectsForExport()', () => { ).rejects.toThrowErrorMatchingInlineSnapshot(`"Can't export more than 1 objects"`); }); + test('sorts objects within type', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + total: 3, + per_page: 10000, + page: 1, + saved_objects: [ + { + id: '3', + type: 'index-pattern', + attributes: { + name: 'baz', + }, + references: [], + }, + { + id: '1', + type: 'index-pattern', + attributes: { + name: 'foo', + }, + references: [], + }, + { + id: '2', + type: 'index-pattern', + attributes: { + name: 'bar', + }, + references: [], + }, + ], + }); + const exportStream = await getSortedObjectsForExport({ + exportSizeLimit: 10000, + savedObjectsClient, + types: ['index-pattern'], + }); + const response = await readStreamToCompletion(exportStream); + expect(response).toMatchInlineSnapshot(` + Array [ + Object { + "attributes": Object { + "name": "foo", + }, + "id": "1", + "references": Array [], + "type": "index-pattern", + }, + Object { + "attributes": Object { + "name": "bar", + }, + "id": "2", + "references": Array [], + "type": "index-pattern", + }, + Object { + "attributes": Object { + "name": "baz", + }, + "id": "3", + "references": Array [], + "type": "index-pattern", + }, + Object { + "exportedCount": 3, + "missingRefCount": 0, + "missingReferences": Array [], + }, + ] + `); + }); + test('exports selected objects and sorts them', async () => { savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ diff --git a/src/core/server/saved_objects/export/get_sorted_objects_for_export.ts b/src/core/server/saved_objects/export/get_sorted_objects_for_export.ts index e1a705a36db75..a4dfacfd9e34f 100644 --- a/src/core/server/saved_objects/export/get_sorted_objects_for_export.ts +++ b/src/core/server/saved_objects/export/get_sorted_objects_for_export.ts @@ -19,7 +19,7 @@ import Boom from 'boom'; import { createListStream } from '../../../../legacy/utils/streams'; -import { SavedObjectsClientContract } from '../types'; +import { SavedObjectsClientContract, SavedObject } from '../types'; import { fetchNestedDependencies } from './inject_nested_depdendencies'; import { sortObjects } from './sort_objects'; @@ -105,15 +105,17 @@ async function fetchObjectsToExport({ const findResponse = await savedObjectsClient.find({ type: types, search, - sortField: '_id', - sortOrder: 'asc', perPage: exportSizeLimit, namespace, }); if (findResponse.total > exportSizeLimit) { throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`); } - return findResponse.saved_objects; + + // sorts server-side by _id, since it's only available in fielddata + return findResponse.saved_objects.sort((a: SavedObject, b: SavedObject) => + a.id > b.id ? 1 : -1 + ); } else { throw Boom.badRequest('Either `type` or `objects` are required.'); } @@ -137,14 +139,17 @@ export async function getSortedObjectsForExport({ exportSizeLimit, namespace, }); - let exportedObjects = [...rootObjects]; + let exportedObjects = []; let missingReferences: SavedObjectsExportResultDetails['missingReferences'] = []; + if (includeReferencesDeep) { const fetchResult = await fetchNestedDependencies(rootObjects, savedObjectsClient, namespace); - exportedObjects = fetchResult.objects; + exportedObjects = sortObjects(fetchResult.objects); missingReferences = fetchResult.missingRefs; + } else { + exportedObjects = sortObjects(rootObjects); } - exportedObjects = sortObjects(exportedObjects); + const exportDetails: SavedObjectsExportResultDetails = { exportedCount: exportedObjects.length, missingRefCount: missingReferences.length, diff --git a/src/legacy/server/logging/log_reporter.js b/src/legacy/server/logging/log_reporter.js index 78176e94fd126..b64f08c1cbbb6 100644 --- a/src/legacy/server/logging/log_reporter.js +++ b/src/legacy/server/logging/log_reporter.js @@ -24,6 +24,14 @@ import LogFormatJson from './log_format_json'; import LogFormatString from './log_format_string'; import { LogInterceptor } from './log_interceptor'; +// NOTE: legacy logger creates a new stream for each new access +// In https://github.com/elastic/kibana/pull/55937 we reach the max listeners +// default limit of 10 for process.stdout which starts a long warning/error +// thrown every time we start the server. +// In order to keep using the legacy logger until we remove it I'm just adding +// a new hard limit here. +process.stdout.setMaxListeners(15); + export function getLoggerStream({ events, config }) { const squeeze = new Squeeze(events); const format = config.json ? new LogFormatJson(config) : new LogFormatString(config); diff --git a/x-pack/.i18nrc.json b/x-pack/.i18nrc.json index d2ec34a7ba886..d7d9249a946cb 100644 --- a/x-pack/.i18nrc.json +++ b/x-pack/.i18nrc.json @@ -26,7 +26,7 @@ "xpack.logstash": "legacy/plugins/logstash", "xpack.main": "legacy/plugins/xpack_main", "xpack.monitoring": "legacy/plugins/monitoring", - "xpack.remoteClusters": "legacy/plugins/remote_clusters", + "xpack.remoteClusters": ["plugins/remote_clusters", "legacy/plugins/remote_clusters"], "xpack.reporting": ["plugins/reporting", "legacy/plugins/reporting"], "xpack.rollupJobs": "legacy/plugins/rollup", "xpack.searchProfiler": "plugins/searchprofiler", diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts index f9d1d97a521fe..3d865cdb9a6e1 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.test.ts @@ -79,15 +79,20 @@ function getMockData(overwrites: Record = {}) { } describe('create()', () => { - test('creates an alert', async () => { - const alertsClient = new AlertsClient(alertsClientParams); - const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ + let alertsClient: AlertsClient; + + beforeEach(() => { + alertsClient = new AlertsClient(alertsClientParams); + alertTypeRegistry.get.mockReturnValue({ id: '123', name: 'Test', actionGroups: ['default'], async executor() {}, }); + }); + + test('creates an alert', async () => { + const data = getMockData(); savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ { @@ -263,7 +268,6 @@ describe('create()', () => { }); test('creates an alert with multiple actions', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData({ actions: [ { @@ -289,12 +293,6 @@ describe('create()', () => { }, ], }); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ { @@ -446,14 +444,7 @@ describe('create()', () => { }); test('creates a disabled alert', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData({ enabled: false }); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ { @@ -527,9 +518,8 @@ describe('create()', () => { }); test('should validate params', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ + alertTypeRegistry.get.mockReturnValue({ id: '123', name: 'Test', actionGroups: [], @@ -547,14 +537,7 @@ describe('create()', () => { }); test('throws error if loading actions fails', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); savedObjectsClient.bulkGet.mockRejectedValueOnce(new Error('Test Error')); await expect(alertsClient.create({ data })).rejects.toThrowErrorMatchingInlineSnapshot( `"Test Error"` @@ -564,14 +547,7 @@ describe('create()', () => { }); test('throws error if create saved object fails', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ { @@ -592,14 +568,7 @@ describe('create()', () => { }); test('attempts to remove saved object if scheduling failed', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ { @@ -655,14 +624,7 @@ describe('create()', () => { }); test('returns task manager error if cleanup fails, logs to console', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); savedObjectsClient.bulkGet.mockResolvedValueOnce({ saved_objects: [ { @@ -714,7 +676,6 @@ describe('create()', () => { }); test('throws an error if alert type not registerd', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); alertTypeRegistry.get.mockImplementation(() => { throw new Error('Invalid type'); @@ -725,14 +686,7 @@ describe('create()', () => { }); test('calls the API key function', async () => { - const alertsClient = new AlertsClient(alertsClientParams); const data = getMockData(); - alertTypeRegistry.get.mockReturnValueOnce({ - id: '123', - name: 'Test', - actionGroups: ['default'], - async executor() {}, - }); alertsClientParams.createAPIKey.mockResolvedValueOnce({ apiKeysEnabled: true, result: { id: '123', api_key: 'abc' }, @@ -845,6 +799,117 @@ describe('create()', () => { } ); }); + + test(`doesn't create API key for disabled alerts`, async () => { + const data = getMockData({ enabled: false }); + savedObjectsClient.bulkGet.mockResolvedValueOnce({ + saved_objects: [ + { + id: '1', + type: 'action', + attributes: { + actionTypeId: 'test', + }, + references: [], + }, + ], + }); + savedObjectsClient.create.mockResolvedValueOnce({ + id: '1', + type: 'alert', + attributes: { + alertTypeId: '123', + schedule: { interval: '10s' }, + params: { + bar: true, + }, + actions: [ + { + group: 'default', + actionRef: 'action_0', + actionTypeId: 'test', + params: { + foo: true, + }, + }, + ], + }, + references: [ + { + name: 'action_0', + type: 'action', + id: '1', + }, + ], + }); + taskManager.schedule.mockResolvedValueOnce({ + id: 'task-123', + taskType: 'alerting:123', + scheduledAt: new Date(), + attempts: 1, + status: TaskStatus.Idle, + runAt: new Date(), + startedAt: null, + retryAt: null, + state: {}, + params: {}, + ownerId: null, + }); + savedObjectsClient.update.mockResolvedValueOnce({ + id: '1', + type: 'alert', + attributes: { + scheduledTaskId: 'task-123', + }, + references: [ + { + id: '1', + name: 'action_0', + type: 'action', + }, + ], + }); + await alertsClient.create({ data }); + + expect(alertsClientParams.createAPIKey).not.toHaveBeenCalled(); + expect(savedObjectsClient.create).toHaveBeenCalledWith( + 'alert', + { + actions: [ + { + actionRef: 'action_0', + group: 'default', + actionTypeId: 'test', + params: { foo: true }, + }, + ], + alertTypeId: '123', + consumer: 'bar', + name: 'abc', + params: { bar: true }, + apiKey: null, + apiKeyOwner: null, + createdBy: 'elastic', + createdAt: '2019-02-12T21:01:22.479Z', + updatedBy: 'elastic', + enabled: false, + schedule: { interval: '10s' }, + throttle: null, + muteAll: false, + mutedInstanceIds: [], + tags: ['foo'], + }, + { + references: [ + { + id: '1', + name: 'action_0', + type: 'action', + }, + ], + } + ); + }); }); describe('enable()', () => { @@ -2544,25 +2609,42 @@ describe('update()', () => { }); describe('updateApiKey()', () => { - test('updates the API key for the alert', async () => { - const alertsClient = new AlertsClient(alertsClientParams); - encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({ - id: '1', - type: 'alert', - attributes: { - schedule: { interval: '10s' }, - alertTypeId: '2', - enabled: true, - }, - version: '123', - references: [], - }); + let alertsClient: AlertsClient; + const existingAlert = { + id: '1', + type: 'alert', + attributes: { + schedule: { interval: '10s' }, + alertTypeId: '2', + enabled: true, + }, + version: '123', + references: [], + }; + const existingEncryptedAlert = { + ...existingAlert, + attributes: { + ...existingAlert.attributes, + apiKey: Buffer.from('123:abc').toString('base64'), + }, + }; + + beforeEach(() => { + alertsClient = new AlertsClient(alertsClientParams); + savedObjectsClient.get.mockResolvedValue(existingAlert); + encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValue(existingEncryptedAlert); alertsClientParams.createAPIKey.mockResolvedValueOnce({ apiKeysEnabled: true, - result: { id: '123', api_key: 'abc' }, + result: { id: '234', api_key: 'abc' }, }); + }); + test('updates the API key for the alert', async () => { await alertsClient.updateApiKey({ id: '1' }); + expect(savedObjectsClient.get).not.toHaveBeenCalled(); + expect(encryptedSavedObjects.getDecryptedAsInternalUser).toHaveBeenCalledWith('alert', '1', { + namespace: 'default', + }); expect(savedObjectsClient.update).toHaveBeenCalledWith( 'alert', '1', @@ -2570,37 +2652,66 @@ describe('updateApiKey()', () => { schedule: { interval: '10s' }, alertTypeId: '2', enabled: true, - apiKey: Buffer.from('123:abc').toString('base64'), + apiKey: Buffer.from('234:abc').toString('base64'), apiKeyOwner: 'elastic', updatedBy: 'elastic', }, { version: '123' } ); + expect(alertsClientParams.invalidateAPIKey).toHaveBeenCalledWith({ id: '123' }); }); - test('swallows error when invalidate API key throws', async () => { - const alertsClient = new AlertsClient(alertsClientParams); - alertsClientParams.invalidateAPIKey.mockRejectedValue(new Error('Fail')); - encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({ - id: '1', - type: 'alert', - attributes: { + test('falls back to SOC when getDecryptedAsInternalUser throws an error', async () => { + encryptedSavedObjects.getDecryptedAsInternalUser.mockRejectedValueOnce(new Error('Fail')); + + await alertsClient.updateApiKey({ id: '1' }); + expect(savedObjectsClient.get).toHaveBeenCalledWith('alert', '1'); + expect(encryptedSavedObjects.getDecryptedAsInternalUser).toHaveBeenCalledWith('alert', '1', { + namespace: 'default', + }); + expect(savedObjectsClient.update).toHaveBeenCalledWith( + 'alert', + '1', + { schedule: { interval: '10s' }, alertTypeId: '2', enabled: true, - apiKey: Buffer.from('123:abc').toString('base64'), + apiKey: Buffer.from('234:abc').toString('base64'), + apiKeyOwner: 'elastic', + updatedBy: 'elastic', }, - version: '123', - references: [], - }); - alertsClientParams.createAPIKey.mockResolvedValueOnce({ - apiKeysEnabled: true, - result: { id: '123', api_key: 'abc' }, - }); + { version: '123' } + ); + expect(alertsClientParams.invalidateAPIKey).not.toHaveBeenCalled(); + }); + + test('swallows error when invalidate API key throws', async () => { + alertsClientParams.invalidateAPIKey.mockRejectedValue(new Error('Fail')); await alertsClient.updateApiKey({ id: '1' }); expect(alertsClientParams.logger.error).toHaveBeenCalledWith( 'Failed to invalidate API Key: Fail' ); + expect(savedObjectsClient.update).toHaveBeenCalled(); + }); + + test('swallows error when getting decrypted object throws', async () => { + encryptedSavedObjects.getDecryptedAsInternalUser.mockRejectedValueOnce(new Error('Fail')); + + await alertsClient.updateApiKey({ id: '1' }); + expect(alertsClientParams.logger.error).toHaveBeenCalledWith( + 'updateApiKey(): Failed to load API key to invalidate on alert 1: Fail' + ); + expect(savedObjectsClient.update).toHaveBeenCalled(); + expect(alertsClientParams.invalidateAPIKey).not.toHaveBeenCalled(); + }); + + test('throws when savedObjectsClient update fails', async () => { + savedObjectsClient.update.mockRejectedValueOnce(new Error('Fail')); + + await expect(alertsClient.updateApiKey({ id: '1' })).rejects.toThrowErrorMatchingInlineSnapshot( + `"Fail"` + ); + expect(alertsClientParams.invalidateAPIKey).not.toHaveBeenCalled(); }); }); diff --git a/x-pack/legacy/plugins/alerting/server/alerts_client.ts b/x-pack/legacy/plugins/alerting/server/alerts_client.ts index f6841ed5a0e46..5b0bec7f3eabb 100644 --- a/x-pack/legacy/plugins/alerting/server/alerts_client.ts +++ b/x-pack/legacy/plugins/alerting/server/alerts_client.ts @@ -152,13 +152,14 @@ export class AlertsClient { const alertType = this.alertTypeRegistry.get(data.alertTypeId); const validatedAlertTypeParams = validateAlertTypeParams(alertType, data.params); const username = await this.getUserName(); + const createdAPIKey = data.enabled ? await this.createAPIKey() : null; this.validateActions(alertType, data.actions); const { references, actions } = await this.denormalizeActions(data.actions); const rawAlert: RawAlert = { ...data, - ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), + ...this.apiKeyAsAlertAttributes(createdAPIKey, username), actions, createdBy: username, updatedBy: username, @@ -329,10 +330,10 @@ export class AlertsClient { } private apiKeyAsAlertAttributes( - apiKey: CreateAPIKeyResult, + apiKey: CreateAPIKeyResult | null, username: string | null ): Pick { - return apiKey.apiKeysEnabled + return apiKey && apiKey.apiKeysEnabled ? { apiKeyOwner: username, apiKey: Buffer.from(`${apiKey.result.id}:${apiKey.result.api_key}`).toString('base64'), @@ -344,12 +345,27 @@ export class AlertsClient { } public async updateApiKey({ id }: { id: string }) { - const { - version, - attributes, - } = await this.encryptedSavedObjectsPlugin.getDecryptedAsInternalUser('alert', id, { - namespace: this.namespace, - }); + let apiKeyToInvalidate: string | null = null; + let attributes: RawAlert; + let version: string | undefined; + + try { + const decryptedAlert = await this.encryptedSavedObjectsPlugin.getDecryptedAsInternalUser< + RawAlert + >('alert', id, { namespace: this.namespace }); + apiKeyToInvalidate = decryptedAlert.attributes.apiKey; + attributes = decryptedAlert.attributes; + version = decryptedAlert.version; + } catch (e) { + // We'll skip invalidating the API key since we failed to load the decrypted saved object + this.logger.error( + `updateApiKey(): Failed to load API key to invalidate on alert ${id}: ${e.message}` + ); + // Still attempt to load the attributes and version using SOC + const alert = await this.savedObjectsClient.get('alert', id); + attributes = alert.attributes; + version = alert.version; + } const username = await this.getUserName(); await this.savedObjectsClient.update( @@ -363,7 +379,9 @@ export class AlertsClient { { version } ); - await this.invalidateApiKey({ apiKey: attributes.apiKey }); + if (apiKeyToInvalidate) { + await this.invalidateApiKey({ apiKey: apiKeyToInvalidate }); + } } private async invalidateApiKey({ apiKey }: { apiKey: string | null }): Promise { diff --git a/x-pack/legacy/plugins/remote_clusters/index.ts b/x-pack/legacy/plugins/remote_clusters/index.ts index ed992e3bf1921..5dd823e09eb8b 100644 --- a/x-pack/legacy/plugins/remote_clusters/index.ts +++ b/x-pack/legacy/plugins/remote_clusters/index.ts @@ -7,8 +7,6 @@ import { Legacy } from 'kibana'; import { resolve } from 'path'; import { PLUGIN } from './common'; -import { Plugin as RemoteClustersPlugin } from './plugin'; -import { createShim } from './shim'; export function remoteClusters(kibana: any) { return new kibana.Plugin({ @@ -43,25 +41,6 @@ export function remoteClusters(kibana: any) { config.get('xpack.remote_clusters.enabled') && config.get('xpack.index_management.enabled') ); }, - init(server: Legacy.Server) { - const { - coreSetup, - pluginsSetup: { - license: { registerLicenseChecker }, - }, - } = createShim(server, PLUGIN.ID); - - const remoteClustersPlugin = new RemoteClustersPlugin(); - - // Set up plugin. - remoteClustersPlugin.setup(coreSetup); - - registerLicenseChecker( - server, - PLUGIN.ID, - PLUGIN.getI18nName(), - PLUGIN.MINIMUM_LICENSE_REQUIRED - ); - }, + init(server: any) {}, }); } diff --git a/x-pack/legacy/plugins/remote_clusters/plugin.ts b/x-pack/legacy/plugins/remote_clusters/plugin.ts deleted file mode 100644 index a15ad553c9188..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/plugin.ts +++ /dev/null @@ -1,30 +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 { API_BASE_PATH } from './common'; -import { CoreSetup } from './shim'; -import { - registerGetRoute, - registerAddRoute, - registerUpdateRoute, - registerDeleteRoute, -} from './server/routes/api'; - -export class Plugin { - public setup(core: CoreSetup): void { - const { - http: { createRouter, isEsError }, - } = core; - - const router = createRouter(API_BASE_PATH); - - // Register routes. - registerGetRoute(router); - registerAddRoute(router); - registerUpdateRoute(router); - registerDeleteRoute(router, isEsError); - } -} diff --git a/x-pack/legacy/plugins/remote_clusters/public/app/sections/remote_cluster_edit/remote_cluster_edit.js b/x-pack/legacy/plugins/remote_clusters/public/app/sections/remote_cluster_edit/remote_cluster_edit.js index 42b9eabc8e33e..f48d854da7255 100644 --- a/x-pack/legacy/plugins/remote_clusters/public/app/sections/remote_cluster_edit/remote_cluster_edit.js +++ b/x-pack/legacy/plugins/remote_clusters/public/app/sections/remote_cluster_edit/remote_cluster_edit.js @@ -37,7 +37,7 @@ export class RemoteClusterEdit extends Component { stopEditingCluster: PropTypes.func, editCluster: PropTypes.func, isEditingCluster: PropTypes.bool, - getEditClusterError: PropTypes.string, + getEditClusterError: PropTypes.object, clearEditClusterErrors: PropTypes.func, openDetailPanel: PropTypes.func, }; diff --git a/x-pack/legacy/plugins/remote_clusters/public/app/store/actions/remove_clusters.js b/x-pack/legacy/plugins/remote_clusters/public/app/store/actions/remove_clusters.js index 47eb192714d7a..4086a91e29021 100644 --- a/x-pack/legacy/plugins/remote_clusters/public/app/store/actions/remove_clusters.js +++ b/x-pack/legacy/plugins/remote_clusters/public/app/store/actions/remove_clusters.js @@ -63,9 +63,7 @@ export const removeClusters = names => async (dispatch, getState) => { const { name, error: { - output: { - payload: { message }, - }, + payload: { message }, }, } = errors[0]; diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/add_route.test.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/add_route.test.ts deleted file mode 100644 index 0ed2f85fa904f..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/add_route.test.ts +++ /dev/null @@ -1,112 +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 Boom from 'boom'; -import { Request, ResponseToolkit } from 'hapi'; -import { wrapCustomError } from '../../../../../server/lib/create_router'; -import { addHandler } from './add_route'; - -describe('[API Routes] Remote Clusters addHandler()', () => { - const mockResponseToolkit = {} as ResponseToolkit; - - it('returns success', async () => { - const mockCreateRequest = ({ - payload: { - name: 'test_cluster', - seeds: [], - }, - } as unknown) as Request; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce(null) - .mockReturnValueOnce({ - acknowledged: true, - persistent: { - cluster: { - remote: { - test_cluster: { - cluster: true, - }, - }, - }, - }, - }); - - const response = await addHandler(mockCreateRequest, callWithRequest, mockResponseToolkit); - const expectedResponse = { - acknowledged: true, - }; - expect(response).toEqual(expectedResponse); - }); - - it('throws an error if the response does not contain cluster information', async () => { - const mockCreateRequest = ({ - payload: { - name: 'test_cluster', - seeds: [], - }, - } as unknown) as Request; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce(null) - .mockReturnValueOnce({ - acknowledged: true, - persistent: {}, - }); - - const expectedError = wrapCustomError( - new Error('Unable to add cluster, no response returned from ES.'), - 400 - ); - - await expect( - addHandler(mockCreateRequest, callWithRequest, mockResponseToolkit) - ).rejects.toThrow(expectedError); - }); - - it('throws an error if the cluster already exists', async () => { - const mockCreateRequest = ({ - payload: { - name: 'test_cluster', - seeds: [], - }, - } as unknown) as Request; - - const callWithRequest = jest.fn().mockReturnValueOnce({ test_cluster: true }); - - const expectedError = wrapCustomError( - new Error('There is already a remote cluster with that name.'), - 409 - ); - - await expect( - addHandler(mockCreateRequest, callWithRequest, mockResponseToolkit) - ).rejects.toThrow(expectedError); - }); - - it('throws an ES error when one is received', async () => { - const mockCreateRequest = ({ - payload: { - name: 'test_cluster', - seeds: [], - }, - } as unknown) as Request; - - const mockError = new Error() as any; - mockError.response = JSON.stringify({ error: 'Test error' }); - - const callWithRequest = jest - .fn() - .mockReturnValueOnce(null) - .mockRejectedValueOnce(mockError); - - await expect( - addHandler(mockCreateRequest, callWithRequest, mockResponseToolkit) - ).rejects.toThrow(Boom.boomify(mockError)); - }); -}); diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/add_route.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/add_route.ts deleted file mode 100644 index 36b8d4fe7c3a0..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/add_route.ts +++ /dev/null @@ -1,49 +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 { get } from 'lodash'; - -import { - Router, - RouterRouteHandler, - wrapCustomError, -} from '../../../../../server/lib/create_router'; -import { serializeCluster } from '../../../common/cluster_serialization'; -import { doesClusterExist } from '../../lib/does_cluster_exist'; - -export const register = (router: Router): void => { - router.post('', addHandler); -}; - -export const addHandler: RouterRouteHandler = async (req, callWithRequest): Promise => { - const { name, seeds, skipUnavailable } = req.payload as any; - - // Check if cluster already exists. - const existingCluster = await doesClusterExist(callWithRequest, name); - if (existingCluster) { - const conflictError = wrapCustomError( - new Error('There is already a remote cluster with that name.'), - 409 - ); - - throw conflictError; - } - - const addClusterPayload = serializeCluster({ name, seeds, skipUnavailable }); - const response = await callWithRequest('cluster.putSettings', { body: addClusterPayload }); - const acknowledged = get(response, 'acknowledged'); - const cluster = get(response, `persistent.cluster.remote.${name}`); - - if (acknowledged && cluster) { - return { - acknowledged: true, - }; - } - - // If for some reason the ES response did not acknowledge, - // return an error. This shouldn't happen. - throw wrapCustomError(new Error('Unable to add cluster, no response returned from ES.'), 400); -}; diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/delete_route.test.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/delete_route.test.ts deleted file mode 100644 index b7eeffcb75105..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/delete_route.test.ts +++ /dev/null @@ -1,158 +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 Boom from 'boom'; -import { Request, ResponseToolkit } from 'hapi'; -import { wrapCustomError } from '../../../../../server/lib/create_router'; -import { createDeleteHandler } from './delete_route'; - -describe('[API Routes] Remote Clusters deleteHandler()', () => { - const mockResponseToolkit = {} as ResponseToolkit; - - const isEsError = () => true; - const deleteHandler = createDeleteHandler(isEsError); - - it('returns names of deleted remote cluster', async () => { - const mockCreateRequest = ({ - params: { - nameOrNames: 'test_cluster', - }, - } as unknown) as Request; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce({ test_cluster: true }) - .mockReturnValueOnce({ - acknowledged: true, - persistent: { - cluster: { - remote: {}, - }, - }, - }); - - const response = await deleteHandler(mockCreateRequest, callWithRequest, mockResponseToolkit); - const expectedResponse = { errors: [], itemsDeleted: ['test_cluster'] }; - expect(response).toEqual(expectedResponse); - }); - - it('returns names of multiple deleted remote clusters', async () => { - const mockCreateRequest = ({ - params: { - nameOrNames: 'test_cluster1,test_cluster2', - }, - } as unknown) as Request; - - const clusterExistsEsResponseMock = { test_cluster1: true, test_cluster2: true }; - - const successfulDeletionEsResponseMock = { - acknowledged: true, - persistent: { - cluster: { - remote: {}, - }, - }, - }; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce(clusterExistsEsResponseMock) - .mockReturnValueOnce(clusterExistsEsResponseMock) - .mockReturnValueOnce(successfulDeletionEsResponseMock) - .mockReturnValueOnce(successfulDeletionEsResponseMock); - - const response = await deleteHandler(mockCreateRequest, callWithRequest, mockResponseToolkit); - const expectedResponse = { errors: [], itemsDeleted: ['test_cluster1', 'test_cluster2'] }; - expect(response).toEqual(expectedResponse); - }); - - it('returns an error if the response contains cluster information', async () => { - const mockCreateRequest = ({ - params: { - nameOrNames: 'test_cluster', - }, - } as unknown) as Request; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce({ test_cluster: true }) - .mockReturnValueOnce({ - acknowledged: true, - persistent: { - cluster: { - remote: { - test_cluster: {}, - }, - }, - }, - }); - - const response = await deleteHandler(mockCreateRequest, callWithRequest); - const expectedResponse = { - errors: [ - { - name: 'test_cluster', - error: wrapCustomError( - new Error('Unable to delete cluster, information still returned from ES.'), - 400 - ), - }, - ], - itemsDeleted: [], - }; - expect(response).toEqual(expectedResponse); - }); - - it(`returns an error if the cluster doesn't exist`, async () => { - const mockCreateRequest = ({ - params: { - nameOrNames: 'test_cluster', - }, - } as unknown) as Request; - - const callWithRequest = jest.fn().mockReturnValueOnce({}); - - const response = await deleteHandler(mockCreateRequest, callWithRequest); - const expectedResponse = { - errors: [ - { - name: 'test_cluster', - error: wrapCustomError(new Error('There is no remote cluster with that name.'), 404), - }, - ], - itemsDeleted: [], - }; - expect(response).toEqual(expectedResponse); - }); - - it('forwards an ES error when one is received', async () => { - const mockCreateRequest = ({ - params: { - nameOrNames: 'test_cluster', - }, - } as unknown) as Request; - - const mockError = new Error() as any; - mockError.response = JSON.stringify({ error: 'Test error' }); - - const callWithRequest = jest - .fn() - .mockReturnValueOnce({ test_cluster: true }) - .mockRejectedValueOnce(mockError); - - const response = await deleteHandler(mockCreateRequest, callWithRequest); - const expectedResponse = { - errors: [ - { - name: 'test_cluster', - error: Boom.boomify(mockError), - }, - ], - itemsDeleted: [], - }; - expect(response).toEqual(expectedResponse); - }); -}); diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/delete_route.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/delete_route.ts deleted file mode 100644 index eff7c66b265b8..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/delete_route.ts +++ /dev/null @@ -1,102 +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 { get } from 'lodash'; - -import { - Router, - RouterRouteHandler, - wrapCustomError, - wrapEsError, - wrapUnknownError, -} from '../../../../../server/lib/create_router'; -import { serializeCluster } from '../../../common/cluster_serialization'; -import { doesClusterExist } from '../../lib/does_cluster_exist'; - -export const register = (router: Router, isEsError: any): void => { - router.delete('/{nameOrNames}', createDeleteHandler(isEsError)); -}; - -export const createDeleteHandler: any = (isEsError: any) => { - const deleteHandler: RouterRouteHandler = async ( - req, - callWithRequest - ): Promise<{ - itemsDeleted: any[]; - errors: any[]; - }> => { - const { nameOrNames } = req.params as any; - const names = nameOrNames.split(','); - - const itemsDeleted: any[] = []; - const errors: any[] = []; - - // Validator that returns an error if the remote cluster does not exist. - const validateClusterDoesExist = async (name: string) => { - try { - const existingCluster = await doesClusterExist(callWithRequest, name); - if (!existingCluster) { - return wrapCustomError(new Error('There is no remote cluster with that name.'), 404); - } - } catch (error) { - return wrapCustomError(error, 400); - } - }; - - // Send the request to delete the cluster and return an error if it could not be deleted. - const sendRequestToDeleteCluster = async (name: string) => { - try { - const body = serializeCluster({ name }); - const response = await callWithRequest('cluster.putSettings', { body }); - const acknowledged = get(response, 'acknowledged'); - const cluster = get(response, `persistent.cluster.remote.${name}`); - - if (acknowledged && !cluster) { - return null; - } - - // If for some reason the ES response still returns the cluster information, - // return an error. This shouldn't happen. - return wrapCustomError( - new Error('Unable to delete cluster, information still returned from ES.'), - 400 - ); - } catch (error) { - if (isEsError(error)) { - return wrapEsError(error); - } - - return wrapUnknownError(error); - } - }; - - const deleteCluster = async (clusterName: string) => { - // Validate that the cluster exists. - let error: any = await validateClusterDoesExist(clusterName); - - if (!error) { - // Delete the cluster. - error = await sendRequestToDeleteCluster(clusterName); - } - - if (error) { - errors.push({ name: clusterName, error }); - } else { - itemsDeleted.push(clusterName); - } - }; - - // Delete all our cluster in parallel. - await Promise.all(names.map(deleteCluster)); - - return { - itemsDeleted, - errors, - }; - }; - - return deleteHandler; -}; diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/get_route.test.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/get_route.test.ts deleted file mode 100644 index 4599e1b1e52e1..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/get_route.test.ts +++ /dev/null @@ -1,40 +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 { Request, ResponseToolkit } from 'hapi'; -import { getAllHandler } from './get_route'; - -describe('[API Routes] Remote Clusters getAllHandler()', () => { - const mockResponseToolkit = {} as ResponseToolkit; - - it('converts the ES response object to an array', async () => { - const callWithRequest = jest - .fn() - .mockReturnValueOnce({}) - .mockReturnValueOnce({ - abc: { seeds: ['xyz'] }, - foo: { seeds: ['bar'] }, - }); - - const response = await getAllHandler({} as Request, callWithRequest, mockResponseToolkit); - const expectedResponse: any[] = [ - { name: 'abc', seeds: ['xyz'], isConfiguredByNode: true }, - { name: 'foo', seeds: ['bar'], isConfiguredByNode: true }, - ]; - expect(response).toEqual(expectedResponse); - }); - - it('returns an empty array when ES responds with an empty object', async () => { - const callWithRequest = jest - .fn() - .mockReturnValueOnce({}) - .mockReturnValueOnce({}); - - const response = await getAllHandler({} as Request, callWithRequest, mockResponseToolkit); - const expectedResponse: any[] = []; - expect(response).toEqual(expectedResponse); - }); -}); diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/get_route.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/get_route.ts deleted file mode 100644 index 97bb59de85b89..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/get_route.ts +++ /dev/null @@ -1,40 +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 { get } from 'lodash'; - -import { Router, RouterRouteHandler } from '../../../../../server/lib/create_router'; -import { deserializeCluster } from '../../../common/cluster_serialization'; - -export const register = (router: Router): void => { - router.get('', getAllHandler); -}; - -// GET '/api/remote_clusters' -export const getAllHandler: RouterRouteHandler = async (req, callWithRequest): Promise => { - const clusterSettings = await callWithRequest('cluster.getSettings'); - const transientClusterNames = Object.keys(get(clusterSettings, `transient.cluster.remote`) || {}); - const persistentClusterNames = Object.keys( - get(clusterSettings, `persistent.cluster.remote`) || {} - ); - - const clustersByName = await callWithRequest('cluster.remoteInfo'); - const clusterNames = (clustersByName && Object.keys(clustersByName)) || []; - - return clusterNames.map((clusterName: string): any => { - const cluster = clustersByName[clusterName]; - const isTransient = transientClusterNames.includes(clusterName); - const isPersistent = persistentClusterNames.includes(clusterName); - // If the cluster hasn't been stored in the cluster state, then it's defined by the - // node's config file. - const isConfiguredByNode = !isTransient && !isPersistent; - - return { - ...deserializeCluster(clusterName, cluster), - isConfiguredByNode, - }; - }); -}; diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/update_route.test.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/update_route.test.ts deleted file mode 100644 index 4de92aef78357..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/update_route.test.ts +++ /dev/null @@ -1,120 +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 { Request, ResponseToolkit } from 'hapi'; -import { wrapCustomError } from '../../../../../server/lib/create_router'; -import { updateHandler } from './update_route'; - -describe('[API Routes] Remote Clusters updateHandler()', () => { - const mockResponseToolkit = {} as ResponseToolkit; - - it('returns the cluster information from Elasticsearch', async () => { - const mockCreateRequest = ({ - payload: { - seeds: [], - }, - params: { - name: 'test_cluster', - }, - } as unknown) as Request; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce({ test_cluster: true }) - .mockReturnValueOnce(null) - .mockReturnValueOnce({ - acknowledged: true, - persistent: { - cluster: { - remote: { - test_cluster: { - seeds: [], - }, - }, - }, - }, - }); - - const response = await updateHandler(mockCreateRequest, callWithRequest, mockResponseToolkit); - const expectedResponse = { - name: 'test_cluster', - seeds: [], - isConfiguredByNode: false, - }; - expect(response).toEqual(expectedResponse); - }); - - it(`throws an error if the response doesn't contain cluster information`, async () => { - const mockCreateRequest = ({ - payload: { - seeds: [], - }, - params: { - name: 'test_cluster', - }, - } as unknown) as Request; - - const callWithRequest = jest - .fn() - .mockReturnValueOnce({ test_cluster: true }) - .mockReturnValueOnce({ - acknowledged: true, - persistent: {}, - }); - - const expectedError = wrapCustomError( - new Error('Unable to update cluster, no response returned from ES.'), - 400 - ); - await expect( - updateHandler(mockCreateRequest, callWithRequest, mockResponseToolkit) - ).rejects.toThrow(expectedError); - }); - - it('throws an error if the cluster does not exist', async () => { - const mockCreateRequest = ({ - payload: { - seeds: [], - }, - params: { - name: 'test_cluster', - }, - } as unknown) as Request; - - const callWithRequest = jest.fn().mockReturnValueOnce({}); - - const expectedError = wrapCustomError( - new Error('There is no remote cluster with that name.'), - 404 - ); - await expect( - updateHandler(mockCreateRequest, callWithRequest, mockResponseToolkit) - ).rejects.toThrow(expectedError); - }); - - it('throws an ES error when one is received', async () => { - const mockCreateRequest = ({ - payload: { - seeds: [], - }, - params: { - name: 'test_cluster', - }, - } as unknown) as Request; - - const mockError = new Error() as any; - mockError.response = JSON.stringify({ error: 'Test error' }); - - const callWithRequest = jest - .fn() - .mockReturnValueOnce({ test_cluster: true }) - .mockRejectedValueOnce(mockError); - - await expect( - updateHandler(mockCreateRequest, callWithRequest, mockResponseToolkit) - ).rejects.toThrow(mockError); - }); -}); diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/update_route.ts b/x-pack/legacy/plugins/remote_clusters/server/routes/api/update_route.ts deleted file mode 100644 index d6eedf7924ca3..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/server/routes/api/update_route.ts +++ /dev/null @@ -1,52 +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 { get } from 'lodash'; - -import { - Router, - RouterRouteHandler, - wrapCustomError, -} from '../../../../../server/lib/create_router'; -import { serializeCluster, deserializeCluster } from '../../../common/cluster_serialization'; -import { doesClusterExist } from '../../lib/does_cluster_exist'; - -export const register = (router: Router): void => { - router.put('/{name}', updateHandler); -}; - -export const updateHandler: RouterRouteHandler = async (req, callWithRequest): Promise => { - const { name } = req.params as any; - const { seeds, skipUnavailable } = req.payload as any; - - // Check if cluster does exist. - const existingCluster = await doesClusterExist(callWithRequest, name); - if (!existingCluster) { - throw wrapCustomError(new Error('There is no remote cluster with that name.'), 404); - } - - // Delete existing cluster settings. - // This is a workaround for: https://github.com/elastic/elasticsearch/issues/37799 - const deleteClusterPayload = serializeCluster({ name }); - await callWithRequest('cluster.putSettings', { body: deleteClusterPayload }); - - // Update cluster as new settings - const updateClusterPayload = serializeCluster({ name, seeds, skipUnavailable }); - const response = await callWithRequest('cluster.putSettings', { body: updateClusterPayload }); - const acknowledged = get(response, 'acknowledged'); - const cluster = get(response, `persistent.cluster.remote.${name}`); - - if (acknowledged && cluster) { - return { - ...deserializeCluster(name, cluster), - isConfiguredByNode: false, - }; - } - - // If for some reason the ES response did not acknowledge, - // return an error. This shouldn't happen. - throw wrapCustomError(new Error('Unable to update cluster, no response returned from ES.'), 400); -}; diff --git a/x-pack/legacy/plugins/remote_clusters/shim.ts b/x-pack/legacy/plugins/remote_clusters/shim.ts deleted file mode 100644 index d81f685992156..0000000000000 --- a/x-pack/legacy/plugins/remote_clusters/shim.ts +++ /dev/null @@ -1,41 +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 { Legacy } from 'kibana'; -import { createRouter, isEsErrorFactory, Router } from '../../server/lib/create_router'; -import { registerLicenseChecker } from '../../server/lib/register_license_checker'; - -export interface CoreSetup { - http: { - createRouter(basePath: string): Router; - isEsError(error: any): boolean; - }; -} - -export interface Plugins { - license: { - registerLicenseChecker: typeof registerLicenseChecker; - }; -} - -export function createShim( - server: Legacy.Server, - pluginId: string -): { coreSetup: CoreSetup; pluginsSetup: Plugins } { - return { - coreSetup: { - http: { - createRouter: (basePath: string) => createRouter(server, pluginId, basePath), - isEsError: isEsErrorFactory(server), - }, - }, - pluginsSetup: { - license: { - registerLicenseChecker, - }, - }, - }; -} diff --git a/x-pack/plugins/actions/server/lib/action_executor.test.ts b/x-pack/plugins/actions/server/lib/action_executor.test.ts index 7d4233db0f8d9..8301a13c82469 100644 --- a/x-pack/plugins/actions/server/lib/action_executor.test.ts +++ b/x-pack/plugins/actions/server/lib/action_executor.test.ts @@ -10,7 +10,7 @@ import { ActionExecutor } from './action_executor'; import { actionTypeRegistryMock } from '../action_type_registry.mock'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; import { savedObjectsClientMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; -import { createEventLoggerMock } from '../../../event_log/server/event_logger.mock'; +import { eventLoggerMock } from '../../../event_log/server/mocks'; const actionExecutor = new ActionExecutor(); const savedObjectsClient = savedObjectsClientMock.create(); @@ -41,7 +41,7 @@ actionExecutor.initialize({ getServices, actionTypeRegistry, encryptedSavedObjectsPlugin, - eventLogger: createEventLoggerMock(), + eventLogger: eventLoggerMock.create(), }); beforeEach(() => jest.resetAllMocks()); diff --git a/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts b/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts index 8890de2483290..fda1e2f5d2456 100644 --- a/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts +++ b/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts @@ -13,7 +13,7 @@ import { actionTypeRegistryMock } from '../action_type_registry.mock'; import { actionExecutorMock } from './action_executor.mock'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; import { savedObjectsClientMock, loggingServiceMock } from 'src/core/server/mocks'; -import { createEventLoggerMock } from '../../../event_log/server/event_logger.mock'; +import { eventLoggerMock } from '../../../event_log/server/mocks'; const spaceIdToNamespace = jest.fn(); const actionTypeRegistry = actionTypeRegistryMock.create(); @@ -59,7 +59,7 @@ const actionExecutorInitializerParams = { getServices: jest.fn().mockReturnValue(services), actionTypeRegistry, encryptedSavedObjectsPlugin: mockedEncryptedSavedObjectsPlugin, - eventLogger: createEventLoggerMock(), + eventLogger: eventLoggerMock.create(), }; const taskRunnerFactoryInitializerParams = { spaceIdToNamespace, diff --git a/x-pack/plugins/event_log/server/event_log_service.mock.ts b/x-pack/plugins/event_log/server/event_log_service.mock.ts new file mode 100644 index 0000000000000..805c241414a2e --- /dev/null +++ b/x-pack/plugins/event_log/server/event_log_service.mock.ts @@ -0,0 +1,25 @@ +/* + * 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 { IEventLogService } from './types'; +import { eventLoggerMock } from './event_logger.mock'; + +const createEventLogServiceMock = () => { + const mock: jest.Mocked = { + isEnabled: jest.fn(), + isLoggingEntries: jest.fn(), + isIndexingEntries: jest.fn(), + registerProviderActions: jest.fn(), + isProviderActionRegistered: jest.fn(), + getProviderActions: jest.fn(), + getLogger: jest.fn().mockReturnValue(eventLoggerMock.create()), + }; + return mock; +}; + +export const eventLogServiceMock = { + create: createEventLogServiceMock, +}; diff --git a/x-pack/plugins/event_log/server/event_logger.mock.ts b/x-pack/plugins/event_log/server/event_logger.mock.ts index 97c2b9f980dcd..6a2c10b625b8e 100644 --- a/x-pack/plugins/event_log/server/event_logger.mock.ts +++ b/x-pack/plugins/event_log/server/event_logger.mock.ts @@ -4,12 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ -import { IEvent, IEventLogger } from './types'; +import { IEventLogger } from './types'; -export function createEventLoggerMock(): IEventLogger { - return { - logEvent(eventProperties: IEvent): void {}, - startTiming(event: IEvent): void {}, - stopTiming(event: IEvent): void {}, +const createEventLoggerMock = () => { + const mock: jest.Mocked = { + logEvent: jest.fn(), + startTiming: jest.fn(), + stopTiming: jest.fn(), }; -} + return mock; +}; + +export const eventLoggerMock = { + create: createEventLoggerMock, +}; diff --git a/x-pack/plugins/event_log/server/mocks.ts b/x-pack/plugins/event_log/server/mocks.ts new file mode 100644 index 0000000000000..aad6cf3e24561 --- /dev/null +++ b/x-pack/plugins/event_log/server/mocks.ts @@ -0,0 +1,23 @@ +/* + * 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 { eventLogServiceMock } from './event_log_service.mock'; + +export { eventLogServiceMock }; +export { eventLoggerMock } from './event_logger.mock'; + +const createSetupMock = () => { + return eventLogServiceMock.create(); +}; + +const createStartMock = () => { + return undefined; +}; + +export const eventLogMock = { + createSetup: createSetupMock, + createStart: createStartMock, +}; diff --git a/x-pack/plugins/remote_clusters/common/constants.ts b/x-pack/plugins/remote_clusters/common/constants.ts new file mode 100644 index 0000000000000..3521b7f662fc9 --- /dev/null +++ b/x-pack/plugins/remote_clusters/common/constants.ts @@ -0,0 +1,23 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { LicenseType } from '../../licensing/common/types'; + +const basicLicense: LicenseType = 'basic'; + +export const PLUGIN = { + id: 'remote_clusters', + // Remote Clusters are used in both CCS and CCR, and CCS is available for all licenses. + minimumLicenseType: basicLicense, + getI18nName: (): string => { + return i18n.translate('xpack.remoteClusters.appName', { + defaultMessage: 'Remote Clusters', + }); + }, +}; + +export const API_BASE_PATH = '/api/remote_clusters'; diff --git a/x-pack/plugins/remote_clusters/common/lib/cluster_serialization.test.ts b/x-pack/plugins/remote_clusters/common/lib/cluster_serialization.test.ts new file mode 100644 index 0000000000000..476fbee7fb6a0 --- /dev/null +++ b/x-pack/plugins/remote_clusters/common/lib/cluster_serialization.test.ts @@ -0,0 +1,137 @@ +/* + * 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 { deserializeCluster, serializeCluster } from './cluster_serialization'; + +describe('cluster_serialization', () => { + describe('deserializeCluster()', () => { + it('should throw an error for invalid arguments', () => { + expect(() => deserializeCluster('foo', 'bar')).toThrowError(); + }); + + it('should deserialize a complete cluster object', () => { + expect( + deserializeCluster('test_cluster', { + seeds: ['localhost:9300'], + connected: true, + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + transport: { + ping_schedule: '-1', + compress: false, + }, + }) + ).toEqual({ + name: 'test_cluster', + seeds: ['localhost:9300'], + isConnected: true, + connectedNodesCount: 1, + maxConnectionsPerCluster: 3, + initialConnectTimeout: '30s', + skipUnavailable: false, + transportPingSchedule: '-1', + transportCompress: false, + }); + }); + + it('should deserialize a cluster object without transport information', () => { + expect( + deserializeCluster('test_cluster', { + seeds: ['localhost:9300'], + connected: true, + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }) + ).toEqual({ + name: 'test_cluster', + seeds: ['localhost:9300'], + isConnected: true, + connectedNodesCount: 1, + maxConnectionsPerCluster: 3, + initialConnectTimeout: '30s', + skipUnavailable: false, + }); + }); + + it('should deserialize a cluster object with arbitrary missing properties', () => { + expect( + deserializeCluster('test_cluster', { + seeds: ['localhost:9300'], + connected: true, + num_nodes_connected: 1, + initial_connect_timeout: '30s', + transport: { + compress: false, + }, + }) + ).toEqual({ + name: 'test_cluster', + seeds: ['localhost:9300'], + isConnected: true, + connectedNodesCount: 1, + initialConnectTimeout: '30s', + transportCompress: false, + }); + }); + }); + + describe('serializeCluster()', () => { + it('should throw an error for invalid arguments', () => { + expect(() => serializeCluster('foo')).toThrowError(); + }); + + it('should serialize a complete cluster object to only dynamic properties', () => { + expect( + serializeCluster({ + name: 'test_cluster', + seeds: ['localhost:9300'], + isConnected: true, + connectedNodesCount: 1, + maxConnectionsPerCluster: 3, + initialConnectTimeout: '30s', + skipUnavailable: false, + transportPingSchedule: '-1', + transportCompress: false, + }) + ).toEqual({ + persistent: { + cluster: { + remote: { + test_cluster: { + seeds: ['localhost:9300'], + skip_unavailable: false, + }, + }, + }, + }, + }); + }); + + it('should serialize a cluster object with missing properties', () => { + expect( + serializeCluster({ + name: 'test_cluster', + seeds: ['localhost:9300'], + }) + ).toEqual({ + persistent: { + cluster: { + remote: { + test_cluster: { + seeds: ['localhost:9300'], + skip_unavailable: null, + }, + }, + }, + }, + }); + }); + }); +}); diff --git a/x-pack/plugins/remote_clusters/common/lib/cluster_serialization.ts b/x-pack/plugins/remote_clusters/common/lib/cluster_serialization.ts new file mode 100644 index 0000000000000..07ea79d42b800 --- /dev/null +++ b/x-pack/plugins/remote_clusters/common/lib/cluster_serialization.ts @@ -0,0 +1,71 @@ +/* + * 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 function deserializeCluster(name: string, esClusterObject: any): any { + if (!name || !esClusterObject || typeof esClusterObject !== 'object') { + throw new Error('Unable to deserialize cluster'); + } + + const { + seeds, + connected: isConnected, + num_nodes_connected: connectedNodesCount, + max_connections_per_cluster: maxConnectionsPerCluster, + initial_connect_timeout: initialConnectTimeout, + skip_unavailable: skipUnavailable, + transport, + } = esClusterObject; + + let deserializedClusterObject: any = { + name, + seeds, + isConnected, + connectedNodesCount, + maxConnectionsPerCluster, + initialConnectTimeout, + skipUnavailable, + }; + + if (transport) { + const { ping_schedule: transportPingSchedule, compress: transportCompress } = transport; + + deserializedClusterObject = { + ...deserializedClusterObject, + transportPingSchedule, + transportCompress, + }; + } + + // It's unnecessary to send undefined values back to the client, so we can remove them. + Object.keys(deserializedClusterObject).forEach(key => { + if (deserializedClusterObject[key] === undefined) { + delete deserializedClusterObject[key]; + } + }); + + return deserializedClusterObject; +} + +export function serializeCluster(deserializedClusterObject: any): any { + if (!deserializedClusterObject || typeof deserializedClusterObject !== 'object') { + throw new Error('Unable to serialize cluster'); + } + + const { name, seeds, skipUnavailable } = deserializedClusterObject; + + return { + persistent: { + cluster: { + remote: { + [name]: { + seeds: seeds ? seeds : null, + skip_unavailable: skipUnavailable !== undefined ? skipUnavailable : null, + }, + }, + }, + }, + }; +} diff --git a/x-pack/plugins/remote_clusters/common/lib/index.ts b/x-pack/plugins/remote_clusters/common/lib/index.ts new file mode 100644 index 0000000000000..bc67bf21af038 --- /dev/null +++ b/x-pack/plugins/remote_clusters/common/lib/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { deserializeCluster, serializeCluster } from './cluster_serialization'; diff --git a/x-pack/plugins/remote_clusters/kibana.json b/x-pack/plugins/remote_clusters/kibana.json new file mode 100644 index 0000000000000..de1e3d1e26865 --- /dev/null +++ b/x-pack/plugins/remote_clusters/kibana.json @@ -0,0 +1,9 @@ +{ + "id": "remote_clusters", + "version": "kibana", + "requiredPlugins": [ + "licensing" + ], + "server": true, + "ui": false +} diff --git a/x-pack/plugins/remote_clusters/server/index.ts b/x-pack/plugins/remote_clusters/server/index.ts new file mode 100644 index 0000000000000..896161d82919b --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/index.ts @@ -0,0 +1,9 @@ +/* + * 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 'kibana/server'; +import { RemoteClustersServerPlugin } from './plugin'; + +export const plugin = (ctx: PluginInitializerContext) => new RemoteClustersServerPlugin(ctx); diff --git a/x-pack/legacy/plugins/remote_clusters/server/lib/does_cluster_exist.ts b/x-pack/plugins/remote_clusters/server/lib/does_cluster_exist.ts similarity index 70% rename from x-pack/legacy/plugins/remote_clusters/server/lib/does_cluster_exist.ts rename to x-pack/plugins/remote_clusters/server/lib/does_cluster_exist.ts index 1e450cf4ae920..8f3e828f79086 100644 --- a/x-pack/legacy/plugins/remote_clusters/server/lib/does_cluster_exist.ts +++ b/x-pack/plugins/remote_clusters/server/lib/does_cluster_exist.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -export async function doesClusterExist(callWithRequest: any, clusterName: string): Promise { +export async function doesClusterExist(callAsCurrentUser: any, clusterName: string): Promise { try { - const clusterInfoByName = await callWithRequest('cluster.remoteInfo'); + const clusterInfoByName = await callAsCurrentUser('cluster.remoteInfo'); return Boolean(clusterInfoByName && clusterInfoByName[clusterName]); } catch (err) { throw new Error('Unable to check if cluster already exists.'); diff --git a/x-pack/plugins/remote_clusters/server/lib/is_es_error/index.ts b/x-pack/plugins/remote_clusters/server/lib/is_es_error/index.ts new file mode 100644 index 0000000000000..a9a3c61472d8c --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/lib/is_es_error/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { isEsError } from './is_es_error'; diff --git a/x-pack/plugins/remote_clusters/server/lib/is_es_error/is_es_error.ts b/x-pack/plugins/remote_clusters/server/lib/is_es_error/is_es_error.ts new file mode 100644 index 0000000000000..4137293cf39c0 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/lib/is_es_error/is_es_error.ts @@ -0,0 +1,13 @@ +/* + * 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 * as legacyElasticsearch from 'elasticsearch'; + +const esErrorsParent = legacyElasticsearch.errors._Abstract; + +export function isEsError(err: Error) { + return err instanceof esErrorsParent; +} diff --git a/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/index.ts b/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/index.ts new file mode 100644 index 0000000000000..0743e443955f4 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { licensePreRoutingFactory } from './license_pre_routing_factory'; diff --git a/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/license_pre_routing_factory.test.ts b/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/license_pre_routing_factory.test.ts new file mode 100644 index 0000000000000..ff777698599cf --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/license_pre_routing_factory.test.ts @@ -0,0 +1,47 @@ +/* + * 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 { kibanaResponseFactory } from '../../../../../../src/core/server'; +import { licensePreRoutingFactory } from '../license_pre_routing_factory'; +import { LicenseStatus } from '../../types'; + +describe('licensePreRoutingFactory()', () => { + let mockDeps: any; + let mockContext: any; + let licenseStatus: LicenseStatus; + + beforeEach(() => { + mockDeps = { getLicenseStatus: () => licenseStatus }; + mockContext = { + core: {}, + actions: {}, + licensing: {}, + }; + }); + + describe('status is not valid', () => { + it('replies with 403', () => { + licenseStatus = { valid: false }; + const stubRequest: any = {}; + const stubHandler: any = () => {}; + const routeWithLicenseCheck = licensePreRoutingFactory(mockDeps, stubHandler); + const response: any = routeWithLicenseCheck(mockContext, stubRequest, kibanaResponseFactory); + expect(response.status).to.be(403); + }); + }); + + describe('status is valid', () => { + it('replies with nothing', () => { + licenseStatus = { valid: true }; + const stubRequest: any = {}; + const stubHandler: any = () => null; + const routeWithLicenseCheck = licensePreRoutingFactory(mockDeps, stubHandler); + const response = routeWithLicenseCheck(mockContext, stubRequest, kibanaResponseFactory); + expect(response).to.be(null); + }); + }); +}); diff --git a/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/license_pre_routing_factory.ts b/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/license_pre_routing_factory.ts new file mode 100644 index 0000000000000..09d78302a7e76 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/lib/license_pre_routing_factory/license_pre_routing_factory.ts @@ -0,0 +1,35 @@ +/* + * 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, + KibanaResponseFactory, + RequestHandler, + RequestHandlerContext, +} from 'kibana/server'; +import { RouteDependencies } from '../../types'; + +export const licensePreRoutingFactory = ( + { getLicenseStatus }: RouteDependencies, + handler: RequestHandler +) => { + return function licenseCheck( + ctx: RequestHandlerContext, + request: KibanaRequest, + response: KibanaResponseFactory + ) { + const licenseStatus = getLicenseStatus(); + if (!licenseStatus.valid) { + return response.forbidden({ + body: { + message: licenseStatus.message || '', + }, + }); + } + + return handler(ctx, request, response); + }; +}; diff --git a/x-pack/plugins/remote_clusters/server/plugin.ts b/x-pack/plugins/remote_clusters/server/plugin.ts new file mode 100644 index 0000000000000..dd0bb536d2695 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/plugin.ts @@ -0,0 +1,72 @@ +/* + * 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 { i18n } from '@kbn/i18n'; + +import { CoreSetup, Logger, Plugin, PluginInitializerContext } from 'src/core/server'; +import { PLUGIN } from '../common/constants'; +import { LICENSE_CHECK_STATE } from '../../licensing/common/types'; +import { Dependencies, LicenseStatus, RouteDependencies } from './types'; + +import { + registerGetRoute, + registerAddRoute, + registerUpdateRoute, + registerDeleteRoute, +} from './routes/api'; + +export class RemoteClustersServerPlugin implements Plugin { + licenseStatus: LicenseStatus; + log: Logger; + + constructor({ logger }: PluginInitializerContext) { + this.log = logger.get(); + this.licenseStatus = { valid: false }; + } + + async setup( + { http, elasticsearch: elasticsearchService }: CoreSetup, + { licensing }: Dependencies + ) { + const elasticsearch = await elasticsearchService.adminClient; + const router = http.createRouter(); + const routeDependencies: RouteDependencies = { + elasticsearch, + elasticsearchService, + router, + getLicenseStatus: () => this.licenseStatus, + }; + + // Register routes + registerGetRoute(routeDependencies); + registerAddRoute(routeDependencies); + registerUpdateRoute(routeDependencies); + registerDeleteRoute(routeDependencies); + + licensing.license$.subscribe(license => { + const { state, message } = license.check(PLUGIN.id, PLUGIN.minimumLicenseType); + const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; + if (hasRequiredLicense) { + this.licenseStatus = { valid: true }; + } else { + this.licenseStatus = { + valid: false, + message: + message || + i18n.translate('xpack.remoteClusters.licenseCheckErrorMessage', { + defaultMessage: 'License check failed', + }), + }; + if (message) { + this.log.info(message); + } + } + }); + } + + start() {} + + stop() {} +} diff --git a/x-pack/plugins/remote_clusters/server/routes/api/add_route.test.ts b/x-pack/plugins/remote_clusters/server/routes/api/add_route.test.ts new file mode 100644 index 0000000000000..a6edd15995d72 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/add_route.test.ts @@ -0,0 +1,194 @@ +/* + * 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 { kibanaResponseFactory, RequestHandlerContext } from '../../../../../../src/core/server'; +import { register } from './add_route'; +import { API_BASE_PATH } from '../../../common/constants'; +import { LicenseStatus } from '../../types'; + +import { + elasticsearchServiceMock, + httpServerMock, + httpServiceMock, +} from '../../../../../../src/core/server/mocks'; + +interface TestOptions { + licenseCheckResult?: LicenseStatus; + apiResponses?: Array<() => Promise>; + asserts: { statusCode: number; result?: Record; apiArguments?: unknown[][] }; + payload?: Record; +} + +describe('ADD remote clusters', () => { + const addRemoteClustersTest = ( + description: string, + { licenseCheckResult = { valid: true }, apiResponses = [], asserts, payload }: TestOptions + ) => { + test(description, async () => { + const { adminClient: elasticsearchMock } = elasticsearchServiceMock.createSetup(); + + const mockRouteDependencies = { + router: httpServiceMock.createRouter(), + getLicenseStatus: () => licenseCheckResult, + elasticsearchService: elasticsearchServiceMock.createInternalSetup(), + elasticsearch: elasticsearchMock, + }; + + const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + + elasticsearchServiceMock + .createClusterClient() + .asScoped.mockReturnValue(mockScopedClusterClient); + + for (const apiResponse of apiResponses) { + mockScopedClusterClient.callAsCurrentUser.mockImplementationOnce(apiResponse); + } + + register(mockRouteDependencies); + const [[{ validate }, handler]] = mockRouteDependencies.router.post.mock.calls; + + const mockRequest = httpServerMock.createKibanaRequest({ + method: 'post', + path: API_BASE_PATH, + body: payload !== undefined ? (validate as any).body.validate(payload) : undefined, + headers: { authorization: 'foo' }, + }); + + const mockContext = ({ + core: { + elasticsearch: { + dataClient: mockScopedClusterClient, + }, + }, + } as unknown) as RequestHandlerContext; + + const response = await handler(mockContext, mockRequest, kibanaResponseFactory); + + expect(response.status).toBe(asserts.statusCode); + expect(response.payload).toEqual(asserts.result); + + if (Array.isArray(asserts.apiArguments)) { + for (const apiArguments of asserts.apiArguments) { + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledWith(...apiArguments); + } + } else { + expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled(); + } + }); + }; + + describe('success', () => { + addRemoteClustersTest('adds remote cluster', { + apiResponses: [ + async () => ({}), + async () => ({ + acknowledged: true, + persistent: { + cluster: { + remote: { + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }, + }, + }, + transient: {}, + }), + ], + payload: { + name: 'test', + seeds: ['127.0.0.1:9300'], + skipUnavailable: false, + }, + asserts: { + apiArguments: [ + ['cluster.remoteInfo'], + [ + 'cluster.putSettings', + { + body: { + persistent: { + cluster: { + remote: { test: { seeds: ['127.0.0.1:9300'], skip_unavailable: false } }, + }, + }, + }, + }, + ], + ], + statusCode: 200, + result: { + acknowledged: true, + }, + }, + }); + }); + + describe('failure', () => { + addRemoteClustersTest('returns 409 if remote cluster already exists', { + apiResponses: [ + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + ], + payload: { + name: 'test', + seeds: ['127.0.0.1:9300'], + skipUnavailable: false, + }, + asserts: { + apiArguments: [['cluster.remoteInfo']], + statusCode: 409, + result: { + message: 'There is already a remote cluster with that name.', + }, + }, + }); + + addRemoteClustersTest('returns 400 ES did not acknowledge remote cluster', { + apiResponses: [async () => ({}), async () => ({})], + payload: { + name: 'test', + seeds: ['127.0.0.1:9300'], + skipUnavailable: false, + }, + asserts: { + apiArguments: [ + ['cluster.remoteInfo'], + [ + 'cluster.putSettings', + { + body: { + persistent: { + cluster: { + remote: { test: { seeds: ['127.0.0.1:9300'], skip_unavailable: false } }, + }, + }, + }, + }, + ], + ], + statusCode: 400, + result: { + message: 'Unable to add cluster, no response returned from ES.', + }, + }, + }); + }); +}); diff --git a/x-pack/plugins/remote_clusters/server/routes/api/add_route.ts b/x-pack/plugins/remote_clusters/server/routes/api/add_route.ts new file mode 100644 index 0000000000000..aa09b6bf45667 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/add_route.ts @@ -0,0 +1,98 @@ +/* + * 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 { get } from 'lodash'; +import { schema, TypeOf } from '@kbn/config-schema'; +import { i18n } from '@kbn/i18n'; +import { RequestHandler } from 'src/core/server'; + +import { serializeCluster } from '../../../common/lib'; +import { doesClusterExist } from '../../lib/does_cluster_exist'; +import { API_BASE_PATH } from '../../../common/constants'; +import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'; +import { isEsError } from '../../lib/is_es_error'; +import { RouteDependencies } from '../../types'; + +const bodyValidation = schema.object({ + name: schema.string(), + seeds: schema.arrayOf(schema.string()), + skipUnavailable: schema.boolean(), +}); + +type RouteBody = TypeOf; + +export const register = (deps: RouteDependencies): void => { + const addHandler: RequestHandler = async ( + ctx, + request, + response + ) => { + try { + const callAsCurrentUser = ctx.core.elasticsearch.dataClient.callAsCurrentUser; + + const { name, seeds, skipUnavailable } = request.body; + + // Check if cluster already exists. + const existingCluster = await doesClusterExist(callAsCurrentUser, name); + if (existingCluster) { + return response.customError({ + statusCode: 409, + body: { + message: i18n.translate( + 'xpack.remoteClusters.addRemoteCluster.existingRemoteClusterErrorMessage', + { + defaultMessage: 'There is already a remote cluster with that name.', + } + ), + }, + }); + } + + const addClusterPayload = serializeCluster({ name, seeds, skipUnavailable }); + const updateClusterResponse = await callAsCurrentUser('cluster.putSettings', { + body: addClusterPayload, + }); + const acknowledged = get(updateClusterResponse, 'acknowledged'); + const cluster = get(updateClusterResponse, `persistent.cluster.remote.${name}`); + + if (acknowledged && cluster) { + return response.ok({ + body: { + acknowledged: true, + }, + }); + } + + // If for some reason the ES response did not acknowledge, + // return an error. This shouldn't happen. + return response.customError({ + statusCode: 400, + body: { + message: i18n.translate( + 'xpack.remoteClusters.addRemoteCluster.unknownRemoteClusterErrorMessage', + { + defaultMessage: 'Unable to add cluster, no response returned from ES.', + } + ), + }, + }); + } catch (error) { + if (isEsError(error)) { + return response.customError({ statusCode: error.statusCode, body: error }); + } + return response.internalError({ body: error }); + } + }; + deps.router.post( + { + path: API_BASE_PATH, + validate: { + body: bodyValidation, + }, + }, + licensePreRoutingFactory(deps, addHandler) + ); +}; diff --git a/x-pack/plugins/remote_clusters/server/routes/api/delete_route.test.ts b/x-pack/plugins/remote_clusters/server/routes/api/delete_route.test.ts new file mode 100644 index 0000000000000..04deb62d2c2d2 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/delete_route.test.ts @@ -0,0 +1,246 @@ +/* + * 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 { kibanaResponseFactory, RequestHandlerContext } from '../../../../../../src/core/server'; +import { register } from './delete_route'; +import { API_BASE_PATH } from '../../../common/constants'; +import { LicenseStatus } from '../../types'; + +import { + elasticsearchServiceMock, + httpServerMock, + httpServiceMock, +} from '../../../../../../src/core/server/mocks'; + +interface TestOptions { + licenseCheckResult?: LicenseStatus; + apiResponses?: Array<() => Promise>; + asserts: { statusCode: number; result?: Record; apiArguments?: unknown[][] }; + params: { + nameOrNames: string; + }; +} + +describe('DELETE remote clusters', () => { + const deleteRemoteClustersTest = ( + description: string, + { licenseCheckResult = { valid: true }, apiResponses = [], asserts, params }: TestOptions + ) => { + test(description, async () => { + const { adminClient: elasticsearchMock } = elasticsearchServiceMock.createSetup(); + + const mockRouteDependencies = { + router: httpServiceMock.createRouter(), + getLicenseStatus: () => licenseCheckResult, + elasticsearchService: elasticsearchServiceMock.createInternalSetup(), + elasticsearch: elasticsearchMock, + }; + + const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + + elasticsearchServiceMock + .createClusterClient() + .asScoped.mockReturnValue(mockScopedClusterClient); + + for (const apiResponse of apiResponses) { + mockScopedClusterClient.callAsCurrentUser.mockImplementationOnce(apiResponse); + } + + register(mockRouteDependencies); + const [[{ validate }, handler]] = mockRouteDependencies.router.delete.mock.calls; + + const mockRequest = httpServerMock.createKibanaRequest({ + method: 'delete', + path: `${API_BASE_PATH}/{nameOrNames}`, + params: (validate as any).params.validate(params), + headers: { authorization: 'foo' }, + }); + + const mockContext = ({ + core: { + elasticsearch: { + dataClient: mockScopedClusterClient, + }, + }, + } as unknown) as RequestHandlerContext; + + const response = await handler(mockContext, mockRequest, kibanaResponseFactory); + + expect(response.status).toBe(asserts.statusCode); + expect(response.payload).toEqual(asserts.result); + + if (Array.isArray(asserts.apiArguments)) { + for (const apiArguments of asserts.apiArguments) { + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledWith(...apiArguments); + } + } else { + expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled(); + } + }); + }; + + describe('success', () => { + deleteRemoteClustersTest('deletes remote cluster', { + apiResponses: [ + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + async () => ({ + acknowledged: true, + persistent: {}, + transient: {}, + }), + ], + params: { + nameOrNames: 'test', + }, + asserts: { + apiArguments: [ + ['cluster.remoteInfo'], + [ + 'cluster.putSettings', + { + body: { + persistent: { + cluster: { + remote: { test: { seeds: null, skip_unavailable: null } }, + }, + }, + }, + }, + ], + ], + statusCode: 200, + result: { + itemsDeleted: ['test'], + errors: [], + }, + }, + }); + }); + + describe('failure', () => { + deleteRemoteClustersTest( + 'returns errors array with 404 error if remote cluster does not exist', + { + apiResponses: [async () => ({})], + params: { + nameOrNames: 'test', + }, + asserts: { + apiArguments: [['cluster.remoteInfo']], + statusCode: 200, + result: { + errors: [ + { + error: { + options: { + body: { + message: 'There is no remote cluster with that name.', + }, + statusCode: 404, + }, + payload: { + message: 'There is no remote cluster with that name.', + }, + status: 404, + }, + name: 'test', + }, + ], + itemsDeleted: [], + }, + }, + } + ); + + deleteRemoteClustersTest( + 'returns errors array with 400 error if ES still returns cluster information', + { + apiResponses: [ + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + async () => ({ + acknowledged: true, + persistent: { + cluster: { + remote: { + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: true, + }, + }, + }, + }, + transient: {}, + }), + ], + params: { + nameOrNames: 'test', + }, + asserts: { + apiArguments: [ + ['cluster.remoteInfo'], + [ + 'cluster.putSettings', + { + body: { + persistent: { + cluster: { + remote: { test: { seeds: null, skip_unavailable: null } }, + }, + }, + }, + }, + ], + ], + statusCode: 200, + result: { + errors: [ + { + error: { + options: { + body: { + message: 'Unable to delete cluster, information still returned from ES.', + }, + statusCode: 400, + }, + payload: { + message: 'Unable to delete cluster, information still returned from ES.', + }, + status: 400, + }, + name: 'test', + }, + ], + itemsDeleted: [], + }, + }, + } + ); + }); +}); diff --git a/x-pack/plugins/remote_clusters/server/routes/api/delete_route.ts b/x-pack/plugins/remote_clusters/server/routes/api/delete_route.ts new file mode 100644 index 0000000000000..742780ffed309 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/delete_route.ts @@ -0,0 +1,138 @@ +/* + * 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 { get } from 'lodash'; +import { schema, TypeOf } from '@kbn/config-schema'; +import { i18n } from '@kbn/i18n'; +import { RequestHandler } from 'src/core/server'; + +import { RouteDependencies } from '../../types'; +import { serializeCluster } from '../../../common/lib'; +import { API_BASE_PATH } from '../../../common/constants'; +import { doesClusterExist } from '../../lib/does_cluster_exist'; +import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'; +import { isEsError } from '../../lib/is_es_error'; + +const paramsValidation = schema.object({ + nameOrNames: schema.string(), +}); + +type RouteParams = TypeOf; + +export const register = (deps: RouteDependencies): void => { + const deleteHandler: RequestHandler = async ( + ctx, + request, + response + ) => { + try { + const callAsCurrentUser = ctx.core.elasticsearch.dataClient.callAsCurrentUser; + + const { nameOrNames } = request.params; + const names = nameOrNames.split(','); + + const itemsDeleted: any[] = []; + const errors: any[] = []; + + // Validator that returns an error if the remote cluster does not exist. + const validateClusterDoesExist = async (name: string) => { + try { + const existingCluster = await doesClusterExist(callAsCurrentUser, name); + if (!existingCluster) { + return response.customError({ + statusCode: 404, + body: { + message: i18n.translate( + 'xpack.remoteClusters.deleteRemoteCluster.noRemoteClusterErrorMessage', + { + defaultMessage: 'There is no remote cluster with that name.', + } + ), + }, + }); + } + } catch (error) { + return response.customError({ statusCode: 400, body: error }); + } + }; + + // Send the request to delete the cluster and return an error if it could not be deleted. + const sendRequestToDeleteCluster = async (name: string) => { + try { + const body = serializeCluster({ name }); + const updateClusterResponse = await callAsCurrentUser('cluster.putSettings', { body }); + const acknowledged = get(updateClusterResponse, 'acknowledged'); + const cluster = get(updateClusterResponse, `persistent.cluster.remote.${name}`); + + // Deletion was successful + if (acknowledged && !cluster) { + return null; + } + + // If for some reason the ES response still returns the cluster information, + // return an error. This shouldn't happen. + return response.customError({ + statusCode: 400, + body: { + message: i18n.translate( + 'xpack.remoteClusters.deleteRemoteCluster.unknownRemoteClusterErrorMessage', + { + defaultMessage: 'Unable to delete cluster, information still returned from ES.', + } + ), + }, + }); + } catch (error) { + if (isEsError(error)) { + return response.customError({ statusCode: error.statusCode, body: error }); + } + return response.internalError({ body: error }); + } + }; + + const deleteCluster = async (clusterName: string) => { + // Validate that the cluster exists. + let error: any = await validateClusterDoesExist(clusterName); + + if (!error) { + // Delete the cluster. + error = await sendRequestToDeleteCluster(clusterName); + } + + if (error) { + errors.push({ name: clusterName, error }); + } else { + itemsDeleted.push(clusterName); + } + }; + + // Delete all our cluster in parallel. + await Promise.all(names.map(deleteCluster)); + + return response.ok({ + body: { + itemsDeleted, + errors, + }, + }); + } catch (error) { + if (isEsError(error)) { + return response.customError({ statusCode: error.statusCode, body: error }); + } + return response.internalError({ body: error }); + } + }; + + deps.router.delete( + { + path: `${API_BASE_PATH}/{nameOrNames}`, + validate: { + params: paramsValidation, + }, + }, + licensePreRoutingFactory(deps, deleteHandler) + ); +}; diff --git a/x-pack/plugins/remote_clusters/server/routes/api/get_route.test.ts b/x-pack/plugins/remote_clusters/server/routes/api/get_route.test.ts new file mode 100644 index 0000000000000..90955be85859d --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/get_route.test.ts @@ -0,0 +1,190 @@ +/* + * 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 Boom from 'boom'; + +import { kibanaResponseFactory, RequestHandlerContext } from '../../../../../../src/core/server'; +import { register } from './get_route'; +import { API_BASE_PATH } from '../../../common/constants'; +import { LicenseStatus } from '../../types'; + +import { + elasticsearchServiceMock, + httpServerMock, + httpServiceMock, +} from '../../../../../../src/core/server/mocks'; + +interface TestOptions { + licenseCheckResult?: LicenseStatus; + apiResponses?: Array<() => Promise>; + asserts: { statusCode: number; result?: Record; apiArguments?: unknown[][] }; +} + +describe('GET remote clusters', () => { + const getRemoteClustersTest = ( + description: string, + { licenseCheckResult = { valid: true }, apiResponses = [], asserts }: TestOptions + ) => { + test(description, async () => { + const { adminClient: elasticsearchMock } = elasticsearchServiceMock.createSetup(); + + const mockRouteDependencies = { + router: httpServiceMock.createRouter(), + getLicenseStatus: () => licenseCheckResult, + elasticsearchService: elasticsearchServiceMock.createInternalSetup(), + elasticsearch: elasticsearchMock, + }; + + const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + + elasticsearchServiceMock + .createClusterClient() + .asScoped.mockReturnValue(mockScopedClusterClient); + + for (const apiResponse of apiResponses) { + mockScopedClusterClient.callAsCurrentUser.mockImplementationOnce(apiResponse); + } + + register(mockRouteDependencies); + const [[, handler]] = mockRouteDependencies.router.get.mock.calls; + + const mockRequest = httpServerMock.createKibanaRequest({ + method: 'get', + path: API_BASE_PATH, + headers: { authorization: 'foo' }, + }); + + const mockContext = ({ + core: { + elasticsearch: { + dataClient: mockScopedClusterClient, + }, + }, + } as unknown) as RequestHandlerContext; + + const response = await handler(mockContext, mockRequest, kibanaResponseFactory); + + expect(response.status).toBe(asserts.statusCode); + expect(response.payload).toEqual(asserts.result); + + if (Array.isArray(asserts.apiArguments)) { + for (const apiArguments of asserts.apiArguments) { + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledWith(...apiArguments); + } + } else { + expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled(); + } + }); + }; + + describe('success', () => { + getRemoteClustersTest('returns remote clusters', { + apiResponses: [ + async () => ({ + persistent: { + cluster: { + remote: { + test: { + seeds: ['127.0.0.1:9300'], + skip_unavailable: false, + }, + }, + }, + }, + transient: {}, + }), + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + ], + asserts: { + apiArguments: [['cluster.getSettings'], ['cluster.remoteInfo']], + statusCode: 200, + result: [ + { + name: 'test', + seeds: ['127.0.0.1:9300'], + isConnected: true, + connectedNodesCount: 1, + maxConnectionsPerCluster: 3, + initialConnectTimeout: '30s', + skipUnavailable: false, + isConfiguredByNode: false, + }, + ], + }, + }); + getRemoteClustersTest('returns an empty array when ES responds with an empty object', { + apiResponses: [async () => ({}), async () => ({})], + asserts: { + apiArguments: [['cluster.getSettings'], ['cluster.remoteInfo']], + statusCode: 200, + result: [], + }, + }); + }); + + describe('failure', () => { + const error = Boom.notAcceptable('test error'); + + getRemoteClustersTest('returns an error if failure to get cluster settings', { + apiResponses: [ + async () => { + throw error; + }, + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + ], + asserts: { + apiArguments: [['cluster.getSettings']], + statusCode: 500, + result: error, + }, + }); + + getRemoteClustersTest('returns an error if failure to get cluster remote info', { + apiResponses: [ + async () => ({ + persistent: { + cluster: { + remote: { + test: { + seeds: ['127.0.0.1:9300'], + skip_unavailable: false, + }, + }, + }, + }, + transient: {}, + }), + async () => { + throw error; + }, + ], + asserts: { + apiArguments: [['cluster.getSettings'], ['cluster.remoteInfo']], + statusCode: 500, + result: error, + }, + }); + }); +}); diff --git a/x-pack/plugins/remote_clusters/server/routes/api/get_route.ts b/x-pack/plugins/remote_clusters/server/routes/api/get_route.ts new file mode 100644 index 0000000000000..44b6284109ac5 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/get_route.ts @@ -0,0 +1,62 @@ +/* + * 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 { get } from 'lodash'; + +import { RequestHandler } from 'src/core/server'; +import { deserializeCluster } from '../../../common/lib'; +import { API_BASE_PATH } from '../../../common/constants'; +import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'; +import { isEsError } from '../../lib/is_es_error'; +import { RouteDependencies } from '../../types'; + +export const register = (deps: RouteDependencies): void => { + const allHandler: RequestHandler = async (ctx, request, response) => { + try { + const callAsCurrentUser = await ctx.core.elasticsearch.dataClient.callAsCurrentUser; + const clusterSettings = await callAsCurrentUser('cluster.getSettings'); + + const transientClusterNames = Object.keys( + get(clusterSettings, 'transient.cluster.remote') || {} + ); + const persistentClusterNames = Object.keys( + get(clusterSettings, 'persistent.cluster.remote') || {} + ); + + const clustersByName = await callAsCurrentUser('cluster.remoteInfo'); + const clusterNames = (clustersByName && Object.keys(clustersByName)) || []; + + const body = clusterNames.map((clusterName: string): any => { + const cluster = clustersByName[clusterName]; + const isTransient = transientClusterNames.includes(clusterName); + const isPersistent = persistentClusterNames.includes(clusterName); + // If the cluster hasn't been stored in the cluster state, then it's defined by the + // node's config file. + const isConfiguredByNode = !isTransient && !isPersistent; + + return { + ...deserializeCluster(clusterName, cluster), + isConfiguredByNode, + }; + }); + + return response.ok({ body }); + } catch (error) { + if (isEsError(error)) { + return response.customError({ statusCode: error.statusCode, body: error }); + } + return response.internalError({ body: error }); + } + }; + + deps.router.get( + { + path: API_BASE_PATH, + validate: false, + }, + licensePreRoutingFactory(deps, allHandler) + ); +}; diff --git a/x-pack/legacy/plugins/remote_clusters/server/routes/api/index.ts b/x-pack/plugins/remote_clusters/server/routes/api/index.ts similarity index 100% rename from x-pack/legacy/plugins/remote_clusters/server/routes/api/index.ts rename to x-pack/plugins/remote_clusters/server/routes/api/index.ts diff --git a/x-pack/plugins/remote_clusters/server/routes/api/update_route.test.ts b/x-pack/plugins/remote_clusters/server/routes/api/update_route.test.ts new file mode 100644 index 0000000000000..9ba239c3ff661 --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/update_route.test.ts @@ -0,0 +1,228 @@ +/* + * 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 { kibanaResponseFactory, RequestHandlerContext } from '../../../../../../src/core/server'; +import { register } from './update_route'; +import { API_BASE_PATH } from '../../../common/constants'; +import { LicenseStatus } from '../../types'; + +import { + elasticsearchServiceMock, + httpServerMock, + httpServiceMock, +} from '../../../../../../src/core/server/mocks'; + +interface TestOptions { + licenseCheckResult?: LicenseStatus; + apiResponses?: Array<() => Promise>; + asserts: { statusCode: number; result?: Record; apiArguments?: unknown[][] }; + payload?: Record; + params: { + name: string; + }; +} + +describe('UPDATE remote clusters', () => { + const updateRemoteClustersTest = ( + description: string, + { + licenseCheckResult = { valid: true }, + apiResponses = [], + asserts, + payload, + params, + }: TestOptions + ) => { + test(description, async () => { + const { adminClient: elasticsearchMock } = elasticsearchServiceMock.createSetup(); + + const mockRouteDependencies = { + router: httpServiceMock.createRouter(), + getLicenseStatus: () => licenseCheckResult, + elasticsearchService: elasticsearchServiceMock.createInternalSetup(), + elasticsearch: elasticsearchMock, + }; + + const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + + elasticsearchServiceMock + .createClusterClient() + .asScoped.mockReturnValue(mockScopedClusterClient); + + for (const apiResponse of apiResponses) { + mockScopedClusterClient.callAsCurrentUser.mockImplementationOnce(apiResponse); + } + + register(mockRouteDependencies); + const [[{ validate }, handler]] = mockRouteDependencies.router.put.mock.calls; + + const mockRequest = httpServerMock.createKibanaRequest({ + method: 'put', + path: `${API_BASE_PATH}/{name}`, + params: (validate as any).params.validate(params), + body: payload !== undefined ? (validate as any).body.validate(payload) : undefined, + headers: { authorization: 'foo' }, + }); + + const mockContext = ({ + core: { + elasticsearch: { + dataClient: mockScopedClusterClient, + }, + }, + } as unknown) as RequestHandlerContext; + + const response = await handler(mockContext, mockRequest, kibanaResponseFactory); + + expect(response.status).toBe(asserts.statusCode); + expect(response.payload).toEqual(asserts.result); + + if (Array.isArray(asserts.apiArguments)) { + for (const apiArguments of asserts.apiArguments) { + expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledWith(...apiArguments); + } + } else { + expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled(); + } + }); + }; + + describe('success', () => { + updateRemoteClustersTest('updates remote cluster', { + apiResponses: [ + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + async () => ({ + acknowledged: true, + persistent: { + cluster: { + remote: { + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: true, + }, + }, + }, + }, + transient: {}, + }), + ], + params: { + name: 'test', + }, + payload: { + seeds: ['127.0.0.1:9300'], + skipUnavailable: true, + }, + asserts: { + apiArguments: [ + ['cluster.remoteInfo'], + [ + 'cluster.putSettings', + { + body: { + persistent: { + cluster: { + remote: { test: { seeds: ['127.0.0.1:9300'], skip_unavailable: true } }, + }, + }, + }, + }, + ], + ], + statusCode: 200, + result: { + connectedNodesCount: 1, + initialConnectTimeout: '30s', + isConfiguredByNode: false, + isConnected: true, + maxConnectionsPerCluster: 3, + name: 'test', + seeds: ['127.0.0.1:9300'], + skipUnavailable: true, + }, + }, + }); + }); + + describe('failure', () => { + updateRemoteClustersTest('returns 404 if remote cluster does not exist', { + apiResponses: [async () => ({})], + payload: { + seeds: ['127.0.0.1:9300'], + skipUnavailable: false, + }, + params: { + name: 'test', + }, + asserts: { + apiArguments: [['cluster.remoteInfo']], + statusCode: 404, + result: { + message: 'There is no remote cluster with that name.', + }, + }, + }); + + updateRemoteClustersTest('returns 400 if ES did not acknowledge remote cluster', { + apiResponses: [ + async () => ({ + test: { + connected: true, + mode: 'sniff', + seeds: ['127.0.0.1:9300'], + num_nodes_connected: 1, + max_connections_per_cluster: 3, + initial_connect_timeout: '30s', + skip_unavailable: false, + }, + }), + async () => ({}), + ], + payload: { + seeds: ['127.0.0.1:9300'], + skipUnavailable: false, + }, + params: { + name: 'test', + }, + asserts: { + apiArguments: [ + ['cluster.remoteInfo'], + [ + 'cluster.putSettings', + { + body: { + persistent: { + cluster: { + remote: { test: { seeds: ['127.0.0.1:9300'], skip_unavailable: false } }, + }, + }, + }, + }, + ], + ], + statusCode: 400, + result: { + message: 'Unable to edit cluster, no response returned from ES.', + }, + }, + }); + }); +}); diff --git a/x-pack/plugins/remote_clusters/server/routes/api/update_route.ts b/x-pack/plugins/remote_clusters/server/routes/api/update_route.ts new file mode 100644 index 0000000000000..fd707f15ad11e --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/routes/api/update_route.ts @@ -0,0 +1,108 @@ +/* + * 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 { get } from 'lodash'; +import { schema, TypeOf } from '@kbn/config-schema'; +import { i18n } from '@kbn/i18n'; +import { RequestHandler } from 'src/core/server'; + +import { API_BASE_PATH } from '../../../common/constants'; +import { serializeCluster, deserializeCluster } from '../../../common/lib'; +import { doesClusterExist } from '../../lib/does_cluster_exist'; +import { RouteDependencies } from '../../types'; +import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'; +import { isEsError } from '../../lib/is_es_error'; + +const bodyValidation = schema.object({ + seeds: schema.arrayOf(schema.string()), + skipUnavailable: schema.boolean(), +}); + +const paramsValidation = schema.object({ + name: schema.string(), +}); + +type RouteParams = TypeOf; + +type RouteBody = TypeOf; + +export const register = (deps: RouteDependencies): void => { + const updateHandler: RequestHandler = async ( + ctx, + request, + response + ) => { + try { + const callAsCurrentUser = ctx.core.elasticsearch.dataClient.callAsCurrentUser; + + const { name } = request.params; + const { seeds, skipUnavailable } = request.body; + + // Check if cluster does exist. + const existingCluster = await doesClusterExist(callAsCurrentUser, name); + if (!existingCluster) { + return response.customError({ + statusCode: 404, + body: { + message: i18n.translate( + 'xpack.remoteClusters.updateRemoteCluster.noRemoteClusterErrorMessage', + { + defaultMessage: 'There is no remote cluster with that name.', + } + ), + }, + }); + } + + // Update cluster as new settings + const updateClusterPayload = serializeCluster({ name, seeds, skipUnavailable }); + const updateClusterResponse = await callAsCurrentUser('cluster.putSettings', { + body: updateClusterPayload, + }); + + const acknowledged = get(updateClusterResponse, 'acknowledged'); + const cluster = get(updateClusterResponse, `persistent.cluster.remote.${name}`); + + if (acknowledged && cluster) { + const body = { + ...deserializeCluster(name, cluster), + isConfiguredByNode: false, + }; + return response.ok({ body }); + } + + // If for some reason the ES response did not acknowledge, + // return an error. This shouldn't happen. + return response.customError({ + statusCode: 400, + body: { + message: i18n.translate( + 'xpack.remoteClusters.updateRemoteCluster.unknownRemoteClusterErrorMessage', + { + defaultMessage: 'Unable to edit cluster, no response returned from ES.', + } + ), + }, + }); + } catch (error) { + if (isEsError(error)) { + return response.customError({ statusCode: error.statusCode, body: error }); + } + return response.internalError({ body: error }); + } + }; + + deps.router.put( + { + path: `${API_BASE_PATH}/{name}`, + validate: { + params: paramsValidation, + body: bodyValidation, + }, + }, + licensePreRoutingFactory(deps, updateHandler) + ); +}; diff --git a/x-pack/plugins/remote_clusters/server/types.ts b/x-pack/plugins/remote_clusters/server/types.ts new file mode 100644 index 0000000000000..708b1daf4bbad --- /dev/null +++ b/x-pack/plugins/remote_clusters/server/types.ts @@ -0,0 +1,24 @@ +/* + * 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 { IRouter, ElasticsearchServiceSetup, IClusterClient } from 'kibana/server'; +import { LicensingPluginSetup } from '../../licensing/server'; + +export interface Dependencies { + licensing: LicensingPluginSetup; +} + +export interface RouteDependencies { + router: IRouter; + getLicenseStatus: () => LicenseStatus; + elasticsearchService: ElasticsearchServiceSetup; + elasticsearch: IClusterClient; +} + +export interface LicenseStatus { + valid: boolean; + message?: string; +} diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts index d99ab794cd28f..65ffa9ebe9dfa 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/find.ts @@ -158,7 +158,7 @@ export default function createFindTests({ getService }: FtrProviderContext) { createdBy: 'elastic', throttle: '1m', updatedBy: 'elastic', - apiKeyOwner: 'elastic', + apiKeyOwner: null, muteAll: false, mutedInstanceIds: [], createdAt: match.createdAt, diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts index b54147348d9a3..cd821a739a9eb 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/update_api_key.ts @@ -74,6 +74,60 @@ export default function createUpdateApiKeyTests({ getService }: FtrProviderConte } }); + it('should still be able to update API key when AAD is broken', async () => { + const { body: createdAlert } = await supertest + .post(`${getUrlPrefix(space.id)}/api/alert`) + .set('kbn-xsrf', 'foo') + .send(getTestAlertData()) + .expect(200); + objectRemover.add(space.id, createdAlert.id, 'alert'); + + await supertest + .put(`${getUrlPrefix(space.id)}/api/saved_objects/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .send({ + attributes: { + name: 'bar', + }, + }) + .expect(200); + + const response = await alertUtils.getUpdateApiKeyRequest(createdAlert.id); + + switch (scenario.id) { + case 'no_kibana_privileges at space1': + case 'space_1_all at space2': + case 'global_read at space1': + expect(response.statusCode).to.eql(404); + expect(response.body).to.eql({ + statusCode: 404, + error: 'Not Found', + message: 'Not Found', + }); + break; + case 'superuser at space1': + case 'space_1_all at space1': + expect(response.statusCode).to.eql(204); + expect(response.body).to.eql(''); + const { body: updatedAlert } = await supertestWithoutAuth + .get(`${getUrlPrefix(space.id)}/api/alert/${createdAlert.id}`) + .set('kbn-xsrf', 'foo') + .auth(user.username, user.password) + .expect(200); + expect(updatedAlert.apiKeyOwner).to.eql(user.username); + // Ensure AAD isn't broken + await checkAAD({ + supertest, + spaceId: space.id, + type: 'alert', + id: createdAlert.id, + }); + break; + default: + throw new Error(`Scenario untested: ${JSON.stringify(scenario)}`); + } + }); + it(`shouldn't update alert api key from another space`, async () => { const { body: createdAlert } = await supertest .post(`${getUrlPrefix('other')}/api/alert`) diff --git a/x-pack/test/api_integration/apis/management/remote_clusters/remote_clusters.js b/x-pack/test/api_integration/apis/management/remote_clusters/remote_clusters.js index 947e28cf11153..677d22ff74984 100644 --- a/x-pack/test/api_integration/apis/management/remote_clusters/remote_clusters.js +++ b/x-pack/test/api_integration/apis/management/remote_clusters/remote_clusters.js @@ -57,6 +57,7 @@ export default function({ getService }) { .send({ name: 'test_cluster', seeds: [NODE_SEED], + skipUnavailable: false, }) .expect(409); @@ -183,17 +184,11 @@ export default function({ getService }) { { name: 'test_cluster_doesnt_exist', error: { - isBoom: true, - isServer: false, - data: null, - output: { + status: 404, + payload: { message: 'There is no remote cluster with that name.' }, + options: { statusCode: 404, - payload: { - statusCode: 404, - error: 'Not Found', - message: 'There is no remote cluster with that name.', - }, - headers: {}, + body: { message: 'There is no remote cluster with that name.' }, }, }, },