From b5b08ecde6ea0e04e06fb401a2b249865b205a36 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Mon, 10 Feb 2020 12:56:44 -0600 Subject: [PATCH] Use default spaces suffix for signals index if spaces disabled Addresses #57221. --- .../lib/detection_engine/routes/utils.test.ts | 33 +++++++++++++++++++ .../lib/detection_engine/routes/utils.ts | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.test.ts index ffd0c791c5bb60..aeb48f58ddfd2a 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.test.ts @@ -11,6 +11,7 @@ import { transformBulkError, BulkError, createSuccessObject, + getIndex, ImportSuccessError, createImportErrorObject, transformImportError, @@ -274,4 +275,36 @@ describe('utils', () => { expect(transformed).toEqual(expected); }); }); + + describe('getIndex', () => { + it('appends the space ID to the configured index if spaces are enabled', () => { + const mockGet = jest.fn(); + const mockGetSpaceId = jest.fn(); + const config = jest.fn(() => ({ get: mockGet, has: jest.fn() })); + const server = { plugins: { spaces: { getSpaceId: mockGetSpaceId } }, config }; + + mockGet.mockReturnValue('mockSignalsIndex'); + mockGetSpaceId.mockReturnValue('myspace'); + // @ts-ignore-next-line TODO these dependencies are simplified on + // https://github.com/elastic/kibana/pull/56814. We're currently mocking + // out what we need. + const index = getIndex(null, server); + + expect(index).toEqual('mockSignalsIndex-myspace'); + }); + + it('appends the default space ID to the configured index if spaces are disabled', () => { + const mockGet = jest.fn(); + const config = jest.fn(() => ({ get: mockGet, has: jest.fn() })); + const server = { plugins: {}, config }; + + mockGet.mockReturnValue('mockSignalsIndex'); + // @ts-ignore-next-line TODO these dependencies are simplified on + // https://github.com/elastic/kibana/pull/56814. We're currently mocking + // out what we need. + const index = getIndex(null, server); + + expect(index).toEqual('mockSignalsIndex-default'); + }); + }); }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.ts index 416c76b5d4eb5d..e61ef94d8510df 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/utils.ts @@ -161,7 +161,7 @@ export const getIndex = ( request: RequestFacade | Omit, server: ServerFacade ): string => { - const spaceId = server.plugins.spaces.getSpaceId(request); + const spaceId = server.plugins.spaces?.getSpaceId?.(request) ?? 'default'; const signalsIndex = server.config().get(`xpack.${APP_ID}.${SIGNALS_INDEX_KEY}`); return `${signalsIndex}-${spaceId}`; };