From 554e4186cac384033e8a5ebcd4a6a2311c335040 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Thu, 24 Mar 2022 16:25:56 +0500 Subject: [PATCH 01/16] Remove unused imports --- src/plugins/console/public/application/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/console/public/application/index.tsx b/src/plugins/console/public/application/index.tsx index 1950ab0c37951..82497a8cfafc0 100644 --- a/src/plugins/console/public/application/index.tsx +++ b/src/plugins/console/public/application/index.tsx @@ -75,7 +75,6 @@ export function renderApp({ notifications, trackUiMetric, objectStorageClient, - http, }, theme$, }} From 12382b2c64c434faa59913d04df9060f49188545 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Fri, 25 Mar 2022 08:56:40 -0700 Subject: [PATCH 02/16] Add support for Kibana API requests --- .../console_editor/editor.test.mock.tsx | 2 +- .../legacy/console_editor/editor.test.tsx | 6 ++-- .../editor/legacy/console_editor/editor.tsx | 4 +-- .../console/public/application/hooks/index.ts | 2 +- .../index.ts | 2 +- .../send_request.ts} | 12 +++---- .../track.ts | 0 .../use_send_current_request.test.tsx} | 28 ++++++++--------- .../use_send_current_request.ts} | 6 ++-- .../models/sense_editor/sense_editor.ts | 26 ++++++++++------ .../public/application/stores/request.ts | 10 +++--- src/plugins/console/public/lib/es/es.ts | 31 +++++++++++++++---- src/plugins/console/public/lib/es/index.ts | 2 +- 13 files changed, 79 insertions(+), 52 deletions(-) rename src/plugins/console/public/application/hooks/{use_send_current_request_to_es => use_send_current_request}/index.ts (81%) rename src/plugins/console/public/application/hooks/{use_send_current_request_to_es/send_request_to_es.ts => use_send_current_request/send_request.ts} (94%) rename src/plugins/console/public/application/hooks/{use_send_current_request_to_es => use_send_current_request}/track.ts (100%) rename src/plugins/console/public/application/hooks/{use_send_current_request_to_es/use_send_current_request_to_es.test.tsx => use_send_current_request/use_send_current_request.test.tsx} (80%) rename src/plugins/console/public/application/hooks/{use_send_current_request_to_es/use_send_current_request_to_es.ts => use_send_current_request/use_send_current_request.ts} (96%) diff --git a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx index dfed86a643627..6733b38e297eb 100644 --- a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx +++ b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx @@ -39,7 +39,7 @@ jest.mock('../../../../models/sense_editor', () => { }; }); -jest.mock('../../../../hooks/use_send_current_request_to_es/send_request_to_es', () => ({ +jest.mock('../../../../hooks/use_send_current_request/send_request_to_es', () => ({ sendRequestToES: jest.fn(), })); jest.mock('../../../../../lib/autocomplete/get_endpoint_from_position', () => ({ diff --git a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx index b942a6d830217..9e8301e71f986 100644 --- a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx +++ b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx @@ -25,7 +25,7 @@ import { } from '../../../../contexts'; // Mocked functions -import { sendRequestToES } from '../../../../hooks/use_send_current_request_to_es/send_request_to_es'; +import { sendRequest } from '../../../../hooks/use_send_current_request/send_request'; import { getEndpointFromPosition } from '../../../../../lib/autocomplete/get_endpoint_from_position'; import type { DevToolsSettings } from '../../../../../services'; import * as consoleMenuActions from '../console_menu_actions'; @@ -60,13 +60,13 @@ describe('Legacy (Ace) Console Editor Component Smoke Test', () => { it('calls send current request to ES', async () => { (getEndpointFromPosition as jest.Mock).mockReturnValue({ patterns: [] }); - (sendRequestToES as jest.Mock).mockRejectedValue({}); + (sendRequest as jest.Mock).mockRejectedValue({}); const editor = doMount(); act(() => { editor.find('button[data-test-subj~="sendRequestButton"]').simulate('click'); }); await nextTick(); - expect(sendRequestToES).toBeCalledTimes(1); + expect(sendRequest).toBeCalledTimes(1); }); it('opens docs', () => { diff --git a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx index bafe9ee6ca156..ca2faadb23cbb 100644 --- a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx +++ b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx @@ -26,7 +26,7 @@ import { ConsoleMenu } from '../../../../components'; import { useEditorReadContext, useServicesContext } from '../../../../contexts'; import { useSaveCurrentTextObject, - useSendCurrentRequestToES, + useSendCurrentRequest, useSetInputEditor, } from '../../../../hooks'; import * as senseEditor from '../../../../models/sense_editor'; @@ -72,7 +72,7 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) { const { settings } = useEditorReadContext(); const setInputEditor = useSetInputEditor(); - const sendCurrentRequestToES = useSendCurrentRequestToES(); + const sendCurrentRequestToES = useSendCurrentRequest(); const saveCurrentTextObject = useSaveCurrentTextObject(); const editorRef = useRef(null); diff --git a/src/plugins/console/public/application/hooks/index.ts b/src/plugins/console/public/application/hooks/index.ts index 1a9b4e5c472bf..1996330bef66b 100644 --- a/src/plugins/console/public/application/hooks/index.ts +++ b/src/plugins/console/public/application/hooks/index.ts @@ -8,6 +8,6 @@ export { useSetInputEditor } from './use_set_input_editor'; export { useRestoreRequestFromHistory } from './use_restore_request_from_history'; -export { useSendCurrentRequestToES } from './use_send_current_request_to_es'; +export { useSendCurrentRequest } from './use_send_current_request'; export { useSaveCurrentTextObject } from './use_save_current_text_object'; export { useDataInit } from './use_data_init'; diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/index.ts b/src/plugins/console/public/application/hooks/use_send_current_request/index.ts similarity index 81% rename from src/plugins/console/public/application/hooks/use_send_current_request_to_es/index.ts rename to src/plugins/console/public/application/hooks/use_send_current_request/index.ts index df2431f1f6f43..33bdbef87f2ef 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/index.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/index.ts @@ -6,4 +6,4 @@ * Side Public License, v 1. */ -export { useSendCurrentRequestToES } from './use_send_current_request_to_es'; +export { useSendCurrentRequest } from './use_send_current_request'; diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts similarity index 94% rename from src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts rename to src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts index 451198aaf2d2b..1856dfef6837a 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts @@ -15,12 +15,12 @@ import { BaseResponseType } from '../../../types'; const { collapseLiteralStrings } = XJson; -export interface EsRequestArgs { +export interface RequestArgs { http: HttpSetup; requests: Array<{ url: string; method: string; data: string[] }>; } -export interface ESResponseObject { +export interface ResponseObject { statusCode: number; statusText: string; timeMs: number; @@ -28,17 +28,17 @@ export interface ESResponseObject { value: V; } -export interface ESRequestResult { +export interface RequestResult { request: { data: string; method: string; path: string }; - response: ESResponseObject; + response: ResponseObject; } let CURRENT_REQ_ID = 0; -export function sendRequestToES(args: EsRequestArgs): Promise { +export function sendRequest(args: RequestArgs): Promise { const requests = args.requests.slice(); return new Promise((resolve, reject) => { const reqId = ++CURRENT_REQ_ID; - const results: ESRequestResult[] = []; + const results: RequestResult[] = []; if (reqId !== CURRENT_REQ_ID) { return; } diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/track.ts b/src/plugins/console/public/application/hooks/use_send_current_request/track.ts similarity index 100% rename from src/plugins/console/public/application/hooks/use_send_current_request_to_es/track.ts rename to src/plugins/console/public/application/hooks/use_send_current_request/track.ts diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.test.tsx b/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx similarity index 80% rename from src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.test.tsx rename to src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx index e0131dc116a34..e92dfef159351 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.test.tsx +++ b/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -jest.mock('./send_request_to_es', () => ({ sendRequestToES: jest.fn() })); +jest.mock('./send_request', () => ({ sendRequestToES: jest.fn() })); jest.mock('../../contexts/editor_context/editor_registry', () => ({ instance: { getInputEditor: jest.fn() }, })); @@ -21,10 +21,10 @@ import { serviceContextMock } from '../../contexts/services_context.mock'; import { useRequestActionContext } from '../../contexts/request_context'; import { instance as editorRegistry } from '../../contexts/editor_context/editor_registry'; -import { sendRequestToES } from './send_request_to_es'; -import { useSendCurrentRequestToES } from './use_send_current_request_to_es'; +import { sendRequest } from './send_request'; +import { useSendCurrentRequest } from './use_send_current_request'; -describe('useSendCurrentRequestToES', () => { +describe('useSendCurrentRequest', () => { let mockContextValue: ContextValue; let dispatch: (...args: unknown[]) => void; const contexts = ({ children }: { children: JSX.Element }) => ( @@ -41,18 +41,18 @@ describe('useSendCurrentRequestToES', () => { jest.resetAllMocks(); }); - it('calls send request to ES', async () => { + it('calls send request', async () => { // Set up mocks (mockContextValue.services.settings.toJSON as jest.Mock).mockReturnValue({}); // This request should succeed - (sendRequestToES as jest.Mock).mockResolvedValue([]); + (sendRequest as jest.Mock).mockResolvedValue([]); (editorRegistry.getInputEditor as jest.Mock).mockImplementation(() => ({ getRequestsInRange: () => ['test'], })); - const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); + const { result } = renderHook(() => useSendCurrentRequest(), { wrapper: contexts }); await act(() => result.current()); - expect(sendRequestToES).toHaveBeenCalledWith({ + expect(sendRequest).toHaveBeenCalledWith({ http: mockContextValue.services.http, requests: ['test'], }); @@ -64,12 +64,12 @@ describe('useSendCurrentRequestToES', () => { it('handles known errors', async () => { // Set up mocks - (sendRequestToES as jest.Mock).mockRejectedValue({ response: 'nada' }); + (sendRequest as jest.Mock).mockRejectedValue({ response: 'nada' }); (editorRegistry.getInputEditor as jest.Mock).mockImplementation(() => ({ getRequestsInRange: () => ['test'], })); - const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); + const { result } = renderHook(() => useSendCurrentRequest(), { wrapper: contexts }); await act(() => result.current()); // Second call should be the request failure const [, [requestFailedCall]] = (dispatch as jest.Mock).mock.calls; @@ -80,12 +80,12 @@ describe('useSendCurrentRequestToES', () => { it('handles unknown errors', async () => { // Set up mocks - (sendRequestToES as jest.Mock).mockRejectedValue(NaN /* unexpected error value */); + (sendRequest as jest.Mock).mockRejectedValue(NaN /* unexpected error value */); (editorRegistry.getInputEditor as jest.Mock).mockImplementation(() => ({ getRequestsInRange: () => ['test'], })); - const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); + const { result } = renderHook(() => useSendCurrentRequest(), { wrapper: contexts }); await act(() => result.current()); // Second call should be the request failure const [, [requestFailedCall]] = (dispatch as jest.Mock).mock.calls; @@ -100,7 +100,7 @@ describe('useSendCurrentRequestToES', () => { it('notifies the user about save to history errors once only', async () => { // Set up mocks - (sendRequestToES as jest.Mock).mockReturnValue( + (sendRequest as jest.Mock).mockReturnValue( [{ request: {} }, { request: {} }] /* two responses to save history */ ); (mockContextValue.services.settings.toJSON as jest.Mock).mockReturnValue({}); @@ -112,7 +112,7 @@ describe('useSendCurrentRequestToES', () => { getRequestsInRange: () => ['test', 'test'], })); - const { result } = renderHook(() => useSendCurrentRequestToES(), { wrapper: contexts }); + const { result } = renderHook(() => useSendCurrentRequest(), { wrapper: contexts }); await act(() => result.current()); expect(dispatch).toHaveBeenCalledTimes(2); diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts b/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.ts similarity index 96% rename from src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts rename to src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.ts index e7c436c9806b3..ed08304d8d660 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/use_send_current_request_to_es.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.ts @@ -16,10 +16,10 @@ import { retrieveAutoCompleteInfo } from '../../../lib/mappings/mappings'; import { instance as registry } from '../../contexts/editor_context/editor_registry'; import { useRequestActionContext, useServicesContext } from '../../contexts'; import { StorageQuotaError } from '../../components/storage_quota_error'; -import { sendRequestToES } from './send_request_to_es'; +import { sendRequest } from './send_request'; import { track } from './track'; -export const useSendCurrentRequestToES = () => { +export const useSendCurrentRequest = () => { const { services: { history, settings, notifications, trackUiMetric, http }, theme$, @@ -46,7 +46,7 @@ export const useSendCurrentRequestToES = () => { // Fire and forget setTimeout(() => track(requests, editor, trackUiMetric), 0); - const results = await sendRequestToES({ http, requests }); + const results = await sendRequest({ http, requests }); let saveToHistoryError: undefined | Error; const { isHistoryDisabled } = settings.toJSON(); diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts index 5e8ca35f287b7..218227a6b9d9d 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts @@ -19,6 +19,7 @@ import * as es from '../../../lib/es/es'; import { CoreEditor, Position, Range } from '../../../types'; import { createTokenIterator } from '../../factories'; import createAutocompleter from '../../../lib/autocomplete/autocomplete'; +import { KIBANA_API_KEYWORD } from '../../../lib/es/es'; const { collapseLiteralStrings } = XJson; @@ -461,27 +462,34 @@ export class SenseEditor { getRequestsAsCURL = async (elasticsearchBaseUrl: string, range?: Range): Promise => { const requests = await this.getRequestsInRange(range, true); + // There should be another way of getting kibana base path?? + const kibanaBaseUrl = window.location.origin; const result = _.map(requests, (req) => { if (typeof req === 'string') { // no request block return req; } - const esPath = req.url; - const esMethod = req.method; - const esData = req.data; + let path = req.url; + const method = req.method; + const data = req.data; // this is the first url defined in elasticsearch.hosts - const url = es.constructESUrl(elasticsearchBaseUrl, esPath); + let url = es.constructUrl(elasticsearchBaseUrl, path); - let ret = 'curl -X' + esMethod + ' "' + url + '"'; - if (esData && esData.length) { - ret += " -H 'Content-Type: application/json' -d'\n"; - const dataAsString = collapseLiteralStrings(esData.join('\n')); + if (path.includes(KIBANA_API_KEYWORD)) { + path = path.replace(KIBANA_API_KEYWORD, ''); + url = es.constructUrl(kibanaBaseUrl, path); + } + + let ret = 'curl -X' + method + ' "' + url + '"'; + if (data && data.length) { + ret += " -H 'Content-Type: application/json' -H 'kbn-xsrf: reporting' -d'\n"; + const dataAsString = collapseLiteralStrings(data.join('\n')); // We escape single quoted strings that that are wrapped in single quoted strings ret += dataAsString.replace(/'/g, "'\\''"); - if (esData.length > 1) { + if (data.length > 1) { ret += '\n'; } // end with a new line ret += "'"; diff --git a/src/plugins/console/public/application/stores/request.ts b/src/plugins/console/public/application/stores/request.ts index 099ab24326d31..8056ab5a7987f 100644 --- a/src/plugins/console/public/application/stores/request.ts +++ b/src/plugins/console/public/application/stores/request.ts @@ -10,18 +10,18 @@ import { Reducer } from 'react'; import { produce } from 'immer'; import { identity } from 'fp-ts/lib/function'; import { BaseResponseType } from '../../types/common'; -import { ESRequestResult } from '../hooks/use_send_current_request_to_es/send_request_to_es'; +import { RequestResult } from '../hooks/use_send_current_request/send_request'; export type Actions = | { type: 'sendRequest'; payload: undefined } - | { type: 'requestSuccess'; payload: { data: ESRequestResult[] } } - | { type: 'requestFail'; payload: ESRequestResult | undefined }; + | { type: 'requestSuccess'; payload: { data: RequestResult[] } } + | { type: 'requestFail'; payload: RequestResult | undefined }; export interface Store { requestInFlight: boolean; lastResult: { - data: ESRequestResult[] | null; - error?: ESRequestResult; + data: RequestResult[] | null; + error?: RequestResult; }; } diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index 2a4059d664e6c..ecd2491ea6bd2 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -6,10 +6,12 @@ * Side Public License, v 1. */ -import type { HttpFetchOptions, HttpResponse, HttpSetup } from '@kbn/core/public'; +import type { HttpResponse, HttpSetup } from 'kibana/public'; +import { parse } from 'query-string'; import { API_BASE_PATH } from '../../../common/constants'; const esVersion: string[] = []; +export const KIBANA_API_KEYWORD = 'kbn:'; export function getVersion() { return esVersion; @@ -39,17 +41,34 @@ export async function send({ withProductOrigin = false, asResponse = false, }: SendProps) { - const options: HttpFetchOptions = { + const isKibanaApiRequest = path.includes(KIBANA_API_KEYWORD); + + if (isKibanaApiRequest) { + const _method = method.toLowerCase() as 'get' | 'post'; + const hasQueryParams = path.includes('?'); + const [pathname, queryString] = path.split('?'); + const url = hasQueryParams + ? `/${pathname.split(KIBANA_API_KEYWORD)[1]}` + : `/${path.split(KIBANA_API_KEYWORD)[1]}`; + const query = hasQueryParams ? parse(queryString) : {}; + + return await http[_method](url, { + ...(data && { body: data }), + query, + asResponse, + asSystemRequest, + }); + } + + return await http.post(`${API_BASE_PATH}/proxy`, { query: { path, method, ...(withProductOrigin && { withProductOrigin }) }, body: data, asResponse, asSystemRequest, - }; - - return await http.post(`${API_BASE_PATH}/proxy`, options); + }); } -export function constructESUrl(baseUri: string, path: string) { +export function constructUrl(baseUri: string, path: string) { baseUri = baseUri.replace(/\/+$/, ''); path = path.replace(/^\/+/, ''); return baseUri + '/' + path; diff --git a/src/plugins/console/public/lib/es/index.ts b/src/plugins/console/public/lib/es/index.ts index 61d34ba96ec05..f83893e93713e 100644 --- a/src/plugins/console/public/lib/es/index.ts +++ b/src/plugins/console/public/lib/es/index.ts @@ -6,4 +6,4 @@ * Side Public License, v 1. */ -export { send, constructESUrl, getContentType, getVersion } from './es'; +export { send, constructUrl, getContentType, getVersion } from './es'; From bf15c3d6bfd4ae4bbb9cf4174e80ffbffdcaeec4 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Fri, 25 Mar 2022 23:31:00 -0700 Subject: [PATCH 03/16] Fix failing tests --- .../use_send_current_request.test.tsx | 2 +- .../application/models/sense_editor/sense_editor.test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx b/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx index e92dfef159351..d16dc3f832d3a 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx +++ b/src/plugins/console/public/application/hooks/use_send_current_request/use_send_current_request.test.tsx @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -jest.mock('./send_request', () => ({ sendRequestToES: jest.fn() })); +jest.mock('./send_request', () => ({ sendRequest: jest.fn() })); jest.mock('../../contexts/editor_context/editor_registry', () => ({ instance: { getInputEditor: jest.fn() }, })); diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js index 0889b98c69388..f1c6e34843378 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js @@ -459,7 +459,7 @@ curl -XGET "http://localhost:9200/_stats?level=shards" #in between comment -curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'Content-Type: application/json' -d' +curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'Content-Type: application/json' -H 'kbn-xsrf: reporting' -d' { "f": 1 }'`.trim() @@ -470,7 +470,7 @@ curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'Content-Type: application editorInput1, { start: { lineNumber: 29 }, end: { lineNumber: 33 } }, ` -curl -XPOST "http://localhost:9200/_sql?format=txt" -H 'Content-Type: application/json' -d' +curl -XPOST "http://localhost:9200/_sql?format=txt" -H 'Content-Type: application/json' -H 'kbn-xsrf: reporting' -d' { "query": "SELECT prenom FROM claude_index WHERE prenom = '\\''claude'\\'' ", "fetch_size": 1 From 3c7bf47047c8820aa2484e3ffcbc30080f5799c6 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Sat, 26 Mar 2022 02:34:13 -0700 Subject: [PATCH 04/16] Fix failing tests --- .../editor/legacy/console_editor/editor.test.mock.tsx | 4 ++-- .../containers/editor/legacy/console_editor/editor.test.tsx | 2 +- src/plugins/console/public/lib/es/es.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx index 6733b38e297eb..b410e240151d7 100644 --- a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx +++ b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.mock.tsx @@ -39,8 +39,8 @@ jest.mock('../../../../models/sense_editor', () => { }; }); -jest.mock('../../../../hooks/use_send_current_request/send_request_to_es', () => ({ - sendRequestToES: jest.fn(), +jest.mock('../../../../hooks/use_send_current_request/send_request', () => ({ + sendRequest: jest.fn(), })); jest.mock('../../../../../lib/autocomplete/get_endpoint_from_position', () => ({ getEndpointFromPosition: jest.fn(), diff --git a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx index 9e8301e71f986..ba5f1e78d5f01 100644 --- a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx +++ b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.test.tsx @@ -58,7 +58,7 @@ describe('Legacy (Ace) Console Editor Component Smoke Test', () => { sandbox.restore(); }); - it('calls send current request to ES', async () => { + it('calls send current request', async () => { (getEndpointFromPosition as jest.Mock).mockReturnValue({ patterns: [] }); (sendRequest as jest.Mock).mockRejectedValue({}); const editor = doMount(); diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index ecd2491ea6bd2..ff34c3dd915d7 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -44,7 +44,7 @@ export async function send({ const isKibanaApiRequest = path.includes(KIBANA_API_KEYWORD); if (isKibanaApiRequest) { - const _method = method.toLowerCase() as 'get' | 'post'; + const _method = method.toLowerCase() as 'get' | 'post' | 'delete' | 'put' | 'head'; const hasQueryParams = path.includes('?'); const [pathname, queryString] = path.split('?'); const url = hasQueryParams From 51a67193983aa0f671fff043594f33fbe32223ed Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Tue, 29 Mar 2022 05:30:37 -0700 Subject: [PATCH 05/16] Support leading / and minor refactor --- .../containers/editor/legacy/console_editor/editor.tsx | 8 ++++---- .../editor/legacy/console_editor/keyboard_shortcuts.ts | 10 +++------- .../models/sense_editor/sense_editor.test.js | 6 +++--- .../application/models/sense_editor/sense_editor.ts | 5 +++-- src/plugins/console/public/lib/es/es.ts | 9 ++++++--- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx index ca2faadb23cbb..d01a40bdd44b3 100644 --- a/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx +++ b/src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx @@ -72,7 +72,7 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) { const { settings } = useEditorReadContext(); const setInputEditor = useSetInputEditor(); - const sendCurrentRequestToES = useSendCurrentRequest(); + const sendCurrentRequest = useSendCurrentRequest(); const saveCurrentTextObject = useSaveCurrentTextObject(); const editorRef = useRef(null); @@ -231,11 +231,11 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) { if (!isKeyboardShortcutsDisabled) { registerCommands({ senseEditor: editorInstanceRef.current!, - sendCurrentRequestToES, + sendCurrentRequest, openDocumentation, }); } - }, [sendCurrentRequestToES, openDocumentation, settings]); + }, [openDocumentation, settings, sendCurrentRequest]); useEffect(() => { const { current: editor } = editorInstanceRef; @@ -262,7 +262,7 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) { > void; + sendCurrentRequest: () => void; openDocumentation: () => void; } @@ -24,11 +24,7 @@ const COMMANDS = { GO_TO_LINE: 'gotoline', }; -export function registerCommands({ - senseEditor, - sendCurrentRequestToES, - openDocumentation, -}: Actions) { +export function registerCommands({ senseEditor, sendCurrentRequest, openDocumentation }: Actions) { const throttledAutoIndent = throttle(() => senseEditor.autoIndent(), 500, { leading: true, trailing: true, @@ -39,7 +35,7 @@ export function registerCommands({ keys: { win: 'Ctrl-Enter', mac: 'Command-Enter' }, name: COMMANDS.SEND_TO_ELASTICSEARCH, fn: () => { - sendCurrentRequestToES(); + sendCurrentRequest(); }, }); diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js index f1c6e34843378..3d61558e6a962 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js @@ -455,11 +455,11 @@ describe('Editor', () => { editorInput1, { start: { lineNumber: 7 }, end: { lineNumber: 14 } }, ` -curl -XGET "http://localhost:9200/_stats?level=shards" +curl -XGET "http://localhost:9200/_stats?level=shards" -H 'kbn-xsrf: reporting' #in between comment -curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'Content-Type: application/json' -H 'kbn-xsrf: reporting' -d' +curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' { "f": 1 }'`.trim() @@ -470,7 +470,7 @@ curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'Content-Type: application editorInput1, { start: { lineNumber: 29 }, end: { lineNumber: 33 } }, ` -curl -XPOST "http://localhost:9200/_sql?format=txt" -H 'Content-Type: application/json' -H 'kbn-xsrf: reporting' -d' +curl -XPOST "http://localhost:9200/_sql?format=txt" -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' { "query": "SELECT prenom FROM claude_index WHERE prenom = '\\''claude'\\'' ", "fetch_size": 1 diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts index 218227a6b9d9d..893b183b44a43 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts @@ -482,9 +482,10 @@ export class SenseEditor { url = es.constructUrl(kibanaBaseUrl, path); } - let ret = 'curl -X' + method + ' "' + url + '"'; + // let ret = 'curl -X' + method + ' "' + url + '"'; + let ret = `curl -X${method} "${url}" -H 'kbn-xsrf: reporting'`; if (data && data.length) { - ret += " -H 'Content-Type: application/json' -H 'kbn-xsrf: reporting' -d'\n"; + ret += " -H 'Content-Type: application/json' -d'\n"; const dataAsString = collapseLiteralStrings(data.join('\n')); // We escape single quoted strings that that are wrapped in single quoted strings diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index ff34c3dd915d7..5ed44db078508 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -8,6 +8,7 @@ import type { HttpResponse, HttpSetup } from 'kibana/public'; import { parse } from 'query-string'; +import { trimStart } from 'lodash'; import { API_BASE_PATH } from '../../../common/constants'; const esVersion: string[] = []; @@ -32,6 +33,8 @@ interface SendProps { asResponse?: boolean; } +type Method = 'get' | 'post' | 'delete' | 'put' | 'head'; + export async function send({ http, method, @@ -44,12 +47,12 @@ export async function send({ const isKibanaApiRequest = path.includes(KIBANA_API_KEYWORD); if (isKibanaApiRequest) { - const _method = method.toLowerCase() as 'get' | 'post' | 'delete' | 'put' | 'head'; + const _method = method.toLowerCase() as Method; const hasQueryParams = path.includes('?'); const [pathname, queryString] = path.split('?'); const url = hasQueryParams - ? `/${pathname.split(KIBANA_API_KEYWORD)[1]}` - : `/${path.split(KIBANA_API_KEYWORD)[1]}`; + ? `/${trimStart(pathname.split(KIBANA_API_KEYWORD)[1], '/')}` + : `/${trimStart(path.split(KIBANA_API_KEYWORD)[1], '/')}`; const query = hasQueryParams ? parse(queryString) : {}; return await http[_method](url, { From 813c8a03f3b436586048e8150e91ffd314fba02c Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Fri, 8 Apr 2022 00:42:08 -0700 Subject: [PATCH 06/16] Resolve conflicts --- src/plugins/console/common/constants/api.ts | 1 + src/plugins/console/common/constants/index.ts | 2 +- .../send_request_to_es.test.ts | 133 ------------------ .../console/public/application/index.tsx | 1 + src/plugins/console/public/lib/es/es.ts | 3 +- 5 files changed, 4 insertions(+), 136 deletions(-) delete mode 100644 src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts diff --git a/src/plugins/console/common/constants/api.ts b/src/plugins/console/common/constants/api.ts index aa0fad1fe4424..4258d81c8ce7f 100644 --- a/src/plugins/console/common/constants/api.ts +++ b/src/plugins/console/common/constants/api.ts @@ -7,3 +7,4 @@ */ export const API_BASE_PATH = '/api/console'; +export const KIBANA_API_KEYWORD = 'kbn:'; diff --git a/src/plugins/console/common/constants/index.ts b/src/plugins/console/common/constants/index.ts index d8768af8fc8d8..3dd6a62ec25e4 100644 --- a/src/plugins/console/common/constants/index.ts +++ b/src/plugins/console/common/constants/index.ts @@ -7,4 +7,4 @@ */ export { MAJOR_VERSION } from './plugin'; -export { API_BASE_PATH } from './api'; +export { API_BASE_PATH, KIBANA_API_KEYWORD } from './api'; diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts deleted file mode 100644 index 8578e271f37b3..0000000000000 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts +++ /dev/null @@ -1,133 +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 - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import type { ContextValue } from '../../contexts'; - -jest.mock('./send_request_to_es', () => ({ sendRequestToES: jest.fn(() => Promise.resolve()) })); - -import { sendRequestToES } from './send_request_to_es'; -import { serviceContextMock } from '../../contexts/services_context.mock'; - -const mockedSendRequestToES = sendRequestToES as jest.Mock; - -describe('sendRequestToES', () => { - let mockContextValue: ContextValue; - - beforeEach(() => { - mockContextValue = serviceContextMock.create(); - }); - - afterEach(() => { - jest.resetAllMocks(); - }); - - it('should send request to ES', async () => { - mockedSendRequestToES.mockResolvedValue([ - { - response: { - statusCode: 200, - value: '{\n "acknowledged": true \n}', - }, - }, - ]); - - const args = { - http: mockContextValue.services.http, - requests: [{ method: 'PUT', url: 'test', data: [] }], - }; - const results = await sendRequestToES(args); - - const [request] = results; - expect(request.response.statusCode).toEqual(200); - expect(request.response.value).toContain('"acknowledged": true'); - expect(mockedSendRequestToES).toHaveBeenCalledWith(args); - expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); - }); - - it('should send multiple requests to ES', async () => { - mockedSendRequestToES.mockResolvedValue([ - { - response: { - statusCode: 200, - }, - }, - { - response: { - statusCode: 200, - }, - }, - ]); - - const args = { - http: mockContextValue.services.http, - requests: [ - { method: 'GET', url: 'test-1', data: [] }, - { method: 'GET', url: 'test-2', data: [] }, - ], - }; - const results = await sendRequestToES(args); - - const [firstRequest, secondRequest] = results; - expect(firstRequest.response.statusCode).toEqual(200); - expect(secondRequest.response.statusCode).toEqual(200); - expect(mockedSendRequestToES).toHaveBeenCalledWith(args); - expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); - }); - - it('should handle errors', async () => { - mockedSendRequestToES.mockRejectedValue({ - response: { - statusCode: 500, - statusText: 'error', - }, - }); - - try { - await sendRequestToES({ - http: mockContextValue.services.http, - requests: [{ method: 'GET', url: 'test', data: [] }], - }); - } catch (error) { - expect(error.response.statusCode).toEqual(500); - expect(error.response.statusText).toEqual('error'); - expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); - } - }); - describe('successful response value', () => { - describe('with text', () => { - it('should return value with lines separated', async () => { - mockedSendRequestToES.mockResolvedValue('\ntest_index-1 [] \ntest_index-2 []\n'); - const response = await sendRequestToES({ - http: mockContextValue.services.http, - requests: [{ method: 'GET', url: 'test-1', data: [] }], - }); - - expect(response).toMatchInlineSnapshot(` - " - test_index-1 [] - test_index-2 [] - " - `); - expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); - }); - }); - - describe('with parsed json', () => { - it('should stringify value', async () => { - mockedSendRequestToES.mockResolvedValue(JSON.stringify({ test: 'some value' })); - const response = await sendRequestToES({ - http: mockContextValue.services.http, - requests: [{ method: 'GET', url: 'test-2', data: [] }], - }); - - expect(typeof response).toBe('string'); - expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); - }); - }); - }); -}); diff --git a/src/plugins/console/public/application/index.tsx b/src/plugins/console/public/application/index.tsx index 82497a8cfafc0..1950ab0c37951 100644 --- a/src/plugins/console/public/application/index.tsx +++ b/src/plugins/console/public/application/index.tsx @@ -75,6 +75,7 @@ export function renderApp({ notifications, trackUiMetric, objectStorageClient, + http, }, theme$, }} diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index 5ed44db078508..bfb9adc2c5381 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -9,10 +9,9 @@ import type { HttpResponse, HttpSetup } from 'kibana/public'; import { parse } from 'query-string'; import { trimStart } from 'lodash'; -import { API_BASE_PATH } from '../../../common/constants'; +import { API_BASE_PATH, KIBANA_API_KEYWORD } from '../../../common/constants'; const esVersion: string[] = []; -export const KIBANA_API_KEYWORD = 'kbn:'; export function getVersion() { return esVersion; From ada7ea85f502fb002c26cd64279f4c980b110f16 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Fri, 8 Apr 2022 02:07:59 -0700 Subject: [PATCH 07/16] Update send_request.test file --- .../send_request.test.ts | 101 ++++++++++++++++++ .../models/sense_editor/sense_editor.ts | 4 +- 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts new file mode 100644 index 0000000000000..d4d711baf435d --- /dev/null +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts @@ -0,0 +1,101 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { ContextValue } from '../../contexts'; + +jest.mock('./send_request', () => ({ sendRequest: jest.fn(() => Promise.resolve()) })); + +import { sendRequest } from './send_request'; +import { serviceContextMock } from '../../contexts/services_context.mock'; + +const mockedSendRequest = sendRequest as jest.Mock; + +describe('sendRequest', () => { + let mockContextValue: ContextValue; + + beforeEach(() => { + mockContextValue = serviceContextMock.create(); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('should send request', async () => { + mockedSendRequest.mockResolvedValue([ + { + response: { + statusCode: 200, + value: '{\n "acknowledged": true \n}', + }, + }, + ]); + + const args = { + http: mockContextValue.services.http, + requests: [{ method: 'PUT', url: 'test', data: [] }], + }; + const results = await sendRequest(args); + + const [request] = results; + expect(request.response.statusCode).toEqual(200); + expect(request.response.value).toContain('"acknowledged": true'); + expect(mockedSendRequest).toHaveBeenCalledWith(args); + expect(mockedSendRequest).toHaveBeenCalledTimes(1); + }); + + it('should send multiple requests', async () => { + mockedSendRequest.mockResolvedValue([ + { + response: { + statusCode: 200, + }, + }, + { + response: { + statusCode: 200, + }, + }, + ]); + + const args = { + http: mockContextValue.services.http, + requests: [ + { method: 'GET', url: 'test-1', data: [] }, + { method: 'GET', url: 'test-2', data: [] }, + ], + }; + const results = await sendRequest(args); + + const [firstRequest, secondRequest] = results; + expect(firstRequest.response.statusCode).toEqual(200); + expect(secondRequest.response.statusCode).toEqual(200); + expect(mockedSendRequest).toHaveBeenCalledWith(args); + expect(mockedSendRequest).toHaveBeenCalledTimes(1); + }); + + it('should handle errors', async () => { + mockedSendRequest.mockRejectedValue({ + response: { + statusCode: 500, + statusText: 'error', + }, + }); + + try { + await sendRequest({ + http: mockContextValue.services.http, + requests: [{ method: 'GET', url: 'test', data: [] }], + }); + } catch (error) { + expect(error.response.statusCode).toEqual(500); + expect(error.response.statusText).toEqual('error'); + expect(mockedSendRequest).toHaveBeenCalledTimes(1); + } + }); +}); diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts index 893b183b44a43..05f012929c6f3 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts @@ -8,7 +8,8 @@ import _ from 'lodash'; -import { XJson } from '@kbn/es-ui-shared-plugin/public'; +import { KIBANA_API_KEYWORD } from '../../../../common/constants'; +import { XJson } from '../../../../../es_ui_shared/public'; import RowParser from '../../../lib/row_parser'; import * as utils from '../../../lib/utils'; @@ -19,7 +20,6 @@ import * as es from '../../../lib/es/es'; import { CoreEditor, Position, Range } from '../../../types'; import { createTokenIterator } from '../../factories'; import createAutocompleter from '../../../lib/autocomplete/autocomplete'; -import { KIBANA_API_KEYWORD } from '../../../lib/es/es'; const { collapseLiteralStrings } = XJson; From f09d4d3ee44ecc8f5629a157344fe2ce917c61cc Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Mon, 11 Apr 2022 01:54:14 -0700 Subject: [PATCH 08/16] Refactor --- .../use_send_current_request/send_request.ts | 28 +++++++++---------- .../models/sense_editor/sense_editor.test.js | 6 ++-- .../models/sense_editor/sense_editor.ts | 10 +++---- src/plugins/console/public/lib/es/es.ts | 19 ++++++------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts index 1856dfef6837a..7c4fbda063ece 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts @@ -59,11 +59,11 @@ export function sendRequest(args: RequestArgs): Promise { return; } const req = requests.shift()!; - const esPath = req.url; - const esMethod = req.method; - let esData = collapseLiteralStrings(req.data.join('\n')); - if (esData) { - esData += '\n'; + const path = req.url; + const method = req.method; + let data = collapseLiteralStrings(req.data.join('\n')); + if (data) { + data += '\n'; } // append a new line for bulk requests. const startTime = Date.now(); @@ -71,9 +71,9 @@ export function sendRequest(args: RequestArgs): Promise { try { const { response, body } = await es.send({ http: args.http, - method: esMethod, - path: esPath, - data: esData, + method, + path, + data, asResponse: true, }); @@ -115,9 +115,9 @@ export function sendRequest(args: RequestArgs): Promise { value, }, request: { - data: esData, - method: esMethod, - path: esPath, + data, + method, + path, }, }); @@ -157,9 +157,9 @@ export function sendRequest(args: RequestArgs): Promise { statusText: error?.response?.statusText ?? 'error', }, request: { - data: esData, - method: esMethod, - path: esPath, + data, + method, + path, }, }); } diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js index 3d61558e6a962..94ed72dfa865e 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js @@ -455,11 +455,11 @@ describe('Editor', () => { editorInput1, { start: { lineNumber: 7 }, end: { lineNumber: 14 } }, ` -curl -XGET "http://localhost:9200/_stats?level=shards" -H 'kbn-xsrf: reporting' +curl -XGET 'http://localhost:9200/_stats?level=shards' -H 'kbn-xsrf: reporting' #in between comment -curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' +curl -XPUT 'http://localhost:9200/index_1/type1/1' -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' { "f": 1 }'`.trim() @@ -470,7 +470,7 @@ curl -XPUT "http://localhost:9200/index_1/type1/1" -H 'kbn-xsrf: reporting' -H ' editorInput1, { start: { lineNumber: 29 }, end: { lineNumber: 33 } }, ` -curl -XPOST "http://localhost:9200/_sql?format=txt" -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' +curl -XPOST 'http://localhost:9200/_sql?format=txt' -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' { "query": "SELECT prenom FROM claude_index WHERE prenom = '\\''claude'\\'' ", "fetch_size": 1 diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts index 05f012929c6f3..357f4eb17980f 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts @@ -15,7 +15,7 @@ import RowParser from '../../../lib/row_parser'; import * as utils from '../../../lib/utils'; // @ts-ignore -import * as es from '../../../lib/es/es'; +import { constructUrl } from '../../../lib/es/es'; import { CoreEditor, Position, Range } from '../../../types'; import { createTokenIterator } from '../../factories'; @@ -462,7 +462,6 @@ export class SenseEditor { getRequestsAsCURL = async (elasticsearchBaseUrl: string, range?: Range): Promise => { const requests = await this.getRequestsInRange(range, true); - // There should be another way of getting kibana base path?? const kibanaBaseUrl = window.location.origin; const result = _.map(requests, (req) => { if (typeof req === 'string') { @@ -475,15 +474,14 @@ export class SenseEditor { const data = req.data; // this is the first url defined in elasticsearch.hosts - let url = es.constructUrl(elasticsearchBaseUrl, path); + let url = constructUrl(elasticsearchBaseUrl, path); if (path.includes(KIBANA_API_KEYWORD)) { path = path.replace(KIBANA_API_KEYWORD, ''); - url = es.constructUrl(kibanaBaseUrl, path); + url = constructUrl(kibanaBaseUrl, path); } - // let ret = 'curl -X' + method + ' "' + url + '"'; - let ret = `curl -X${method} "${url}" -H 'kbn-xsrf: reporting'`; + let ret = `curl -X${method} '${url}' -H 'kbn-xsrf: reporting'`; if (data && data.length) { ret += " -H 'Content-Type: application/json' -d'\n"; const dataAsString = collapseLiteralStrings(data.join('\n')); diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index bfb9adc2c5381..a74855d53f84a 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -7,7 +7,6 @@ */ import type { HttpResponse, HttpSetup } from 'kibana/public'; -import { parse } from 'query-string'; import { trimStart } from 'lodash'; import { API_BASE_PATH, KIBANA_API_KEYWORD } from '../../../common/constants'; @@ -46,16 +45,16 @@ export async function send({ const isKibanaApiRequest = path.includes(KIBANA_API_KEYWORD); if (isKibanaApiRequest) { - const _method = method.toLowerCase() as Method; - const hasQueryParams = path.includes('?'); - const [pathname, queryString] = path.split('?'); - const url = hasQueryParams - ? `/${trimStart(pathname.split(KIBANA_API_KEYWORD)[1], '/')}` - : `/${trimStart(path.split(KIBANA_API_KEYWORD)[1], '/')}`; - const query = hasQueryParams ? parse(queryString) : {}; + const httpMethod = method.toLowerCase() as Method; + const uri = new URL( + `${window.location.origin}/${trimStart(path.replace(KIBANA_API_KEYWORD, ''), '/')}` + ); + const { pathname, searchParams } = uri; + const query = Object.fromEntries(searchParams.entries()); + const body = ['post', 'put'].includes(httpMethod) ? data : null; - return await http[_method](url, { - ...(data && { body: data }), + return await http[httpMethod](pathname, { + body, query, asResponse, asSystemRequest, From ddad9ac521614c2fbc983583a1a65c6f43047135 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Tue, 12 Apr 2022 14:26:03 +0500 Subject: [PATCH 09/16] Add functional test --- test/functional/apps/console/_autocomplete.ts | 2 ++ test/functional/apps/console/_console.ts | 16 ++++++++++++++++ test/functional/page_objects/console_page.ts | 1 - 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/test/functional/apps/console/_autocomplete.ts b/test/functional/apps/console/_autocomplete.ts index 4b424b2a79c66..c4dc7a05df612 100644 --- a/test/functional/apps/console/_autocomplete.ts +++ b/test/functional/apps/console/_autocomplete.ts @@ -25,6 +25,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { it('should provide basic auto-complete functionality', async () => { await PageObjects.console.enterRequest(); + await PageObjects.console.pressEnter(); await PageObjects.console.enterText(`{\n\t"query": {`); await PageObjects.console.pressEnter(); await PageObjects.console.promptAutocomplete(); @@ -37,6 +38,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { beforeEach(async () => { await PageObjects.console.clearTextArea(); await PageObjects.console.enterRequest(); + await PageObjects.console.pressEnter(); }); it('should add a comma after previous non empty line', async () => { await PageObjects.console.enterText(`{\n\t"query": {\n\t\t"match": {}`); diff --git a/test/functional/apps/console/_console.ts b/test/functional/apps/console/_console.ts index 367f1ccb56256..52218b88be60d 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -106,5 +106,21 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); }); }); + + describe('with kbn: prefix in request', () => { + before(async () => { + await PageObjects.console.clearTextArea(); + }); + it('it should send successful request to Kibana API', async () => { + const expectedResponseContains = 'default space'; + await PageObjects.console.enterRequest('\n GET kbn:/api/spaces/space'); + await PageObjects.console.clickPlay(); + await retry.try(async () => { + const actualResponse = await PageObjects.console.getResponse(); + log.debug(actualResponse); + expect(actualResponse).to.contain(expectedResponseContains); + }); + }); + }); }); } diff --git a/test/functional/page_objects/console_page.ts b/test/functional/page_objects/console_page.ts index 32c859cc1aed9..28c6390b431d1 100644 --- a/test/functional/page_objects/console_page.ts +++ b/test/functional/page_objects/console_page.ts @@ -101,7 +101,6 @@ export class ConsolePageObject extends FtrService { public async enterRequest(request: string = '\nGET _search') { const textArea = await this.getEditorTextArea(); await textArea.pressKeys(request); - await textArea.pressKeys(Key.ENTER); } public async enterText(text: string) { From 2b6fd30b5a1cbec933b4d5add2c7f196badfeed8 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Fri, 15 Apr 2022 01:35:36 -0700 Subject: [PATCH 10/16] Address comments --- .../models/sense_editor/sense_editor.test.js | 6 ++-- .../models/sense_editor/sense_editor.ts | 16 +++------ src/plugins/console/public/lib/es/es.ts | 35 +++++++++++++------ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js index 94ed72dfa865e..ff9d245f61275 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js @@ -455,11 +455,11 @@ describe('Editor', () => { editorInput1, { start: { lineNumber: 7 }, end: { lineNumber: 14 } }, ` -curl -XGET 'http://localhost:9200/_stats?level=shards' -H 'kbn-xsrf: reporting' +curl -XGET "http://localhost:9200/_stats?level=shards" -H "kbn-xsrf: reporting" #in between comment -curl -XPUT 'http://localhost:9200/index_1/type1/1' -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' +curl -XPUT "http://localhost:9200/index_1/type1/1" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d' { "f": 1 }'`.trim() @@ -470,7 +470,7 @@ curl -XPUT 'http://localhost:9200/index_1/type1/1' -H 'kbn-xsrf: reporting' -H ' editorInput1, { start: { lineNumber: 29 }, end: { lineNumber: 33 } }, ` -curl -XPOST 'http://localhost:9200/_sql?format=txt' -H 'kbn-xsrf: reporting' -H 'Content-Type: application/json' -d' +curl -XPOST "http://localhost:9200/_sql?format=txt" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d' { "query": "SELECT prenom FROM claude_index WHERE prenom = '\\''claude'\\'' ", "fetch_size": 1 diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts index 357f4eb17980f..c141f9939bbdd 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts @@ -8,7 +8,6 @@ import _ from 'lodash'; -import { KIBANA_API_KEYWORD } from '../../../../common/constants'; import { XJson } from '../../../../../es_ui_shared/public'; import RowParser from '../../../lib/row_parser'; @@ -462,28 +461,23 @@ export class SenseEditor { getRequestsAsCURL = async (elasticsearchBaseUrl: string, range?: Range): Promise => { const requests = await this.getRequestsInRange(range, true); - const kibanaBaseUrl = window.location.origin; const result = _.map(requests, (req) => { if (typeof req === 'string') { // no request block return req; } - let path = req.url; + const path = req.url; const method = req.method; const data = req.data; // this is the first url defined in elasticsearch.hosts - let url = constructUrl(elasticsearchBaseUrl, path); + const url = constructUrl(elasticsearchBaseUrl, path); - if (path.includes(KIBANA_API_KEYWORD)) { - path = path.replace(KIBANA_API_KEYWORD, ''); - url = constructUrl(kibanaBaseUrl, path); - } - - let ret = `curl -X${method} '${url}' -H 'kbn-xsrf: reporting'`; + // Append 'kbn-xsrf' header to bypass (XSRF/CSRF) protections + let ret = `curl -X${method.toUpperCase()} "${url}" -H "kbn-xsrf: reporting"`; if (data && data.length) { - ret += " -H 'Content-Type: application/json' -d'\n"; + ret += ` -H "Content-Type: application/json" -d'\n`; const dataAsString = collapseLiteralStrings(data.join('\n')); // We escape single quoted strings that that are wrapped in single quoted strings diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index a74855d53f84a..5f3e534c340a4 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -21,7 +21,7 @@ export function getContentType(body: unknown) { return 'application/json'; } -interface SendProps { +interface SendConfig { http: HttpSetup; method: string; path: string; @@ -31,7 +31,7 @@ interface SendProps { asResponse?: boolean; } -type Method = 'get' | 'post' | 'delete' | 'put' | 'head'; +type Method = 'get' | 'post' | 'delete' | 'put' | 'patch' | 'head'; export async function send({ http, @@ -41,17 +41,16 @@ export async function send({ asSystemRequest = false, withProductOrigin = false, asResponse = false, -}: SendProps) { - const isKibanaApiRequest = path.includes(KIBANA_API_KEYWORD); +}: SendConfig) { + const kibanaRequestUrl = getKibanaRequestUrl(path); - if (isKibanaApiRequest) { + if (kibanaRequestUrl) { const httpMethod = method.toLowerCase() as Method; - const uri = new URL( - `${window.location.origin}/${trimStart(path.replace(KIBANA_API_KEYWORD, ''), '/')}` - ); - const { pathname, searchParams } = uri; + // construct URL object, so we can extract out pathname, searchParams, etc... + const url = new URL(kibanaRequestUrl); + const { pathname, searchParams } = url; const query = Object.fromEntries(searchParams.entries()); - const body = ['post', 'put'].includes(httpMethod) ? data : null; + const body = ['post', 'put', 'patch'].includes(httpMethod) ? data : null; return await http[httpMethod](pathname, { body, @@ -69,7 +68,23 @@ export async function send({ }); } +function getKibanaRequestUrl(path: string) { + const isKibanaApiRequest = path.startsWith(KIBANA_API_KEYWORD); + const kibanaBasePath = window.location.origin; + + if (isKibanaApiRequest) { + // window.location.origin is used as a Kibana public base path for sending requests in cURL commands. E.g. "Copy as cURL". + return `${kibanaBasePath}/${trimStart(path.replace(KIBANA_API_KEYWORD, '/'))}`; + } +} + export function constructUrl(baseUri: string, path: string) { + const kibanaRequestUrl = getKibanaRequestUrl(path); + + if (kibanaRequestUrl) { + return kibanaRequestUrl; + } + baseUri = baseUri.replace(/\/+$/, ''); path = path.replace(/^\/+/, ''); return baseUri + '/' + path; From 1f5e3176492be78282993cfc3b569d23d16f0d8e Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Fri, 15 Apr 2022 14:38:17 +0500 Subject: [PATCH 11/16] Fix typo --- src/plugins/console/public/lib/es/es.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index 5f3e534c340a4..1fe6f80166708 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -74,7 +74,7 @@ function getKibanaRequestUrl(path: string) { if (isKibanaApiRequest) { // window.location.origin is used as a Kibana public base path for sending requests in cURL commands. E.g. "Copy as cURL". - return `${kibanaBasePath}/${trimStart(path.replace(KIBANA_API_KEYWORD, '/'))}`; + return `${kibanaBasePath}/${trimStart(path.replace(KIBANA_API_KEYWORD, ''), '/')}`; } } @@ -84,7 +84,6 @@ export function constructUrl(baseUri: string, path: string) { if (kibanaRequestUrl) { return kibanaRequestUrl; } - baseUri = baseUri.replace(/\/+$/, ''); path = path.replace(/^\/+/, ''); return baseUri + '/' + path; From b43ad579be628c847b983e6112be3270ccb5e1f1 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Mon, 18 Apr 2022 22:21:18 +0500 Subject: [PATCH 12/16] Resolve conflicts and refactor error handling --- .../use_send_current_request/send_request.ts | 27 +++++++------------ src/plugins/console/public/lib/es/es.ts | 1 - 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts index 7c4fbda063ece..35033a09c22bf 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts @@ -127,25 +127,18 @@ export function sendRequest(args: RequestArgs): Promise { } } catch (error) { let value; - let contentType: string | null = ''; + const { response, body } = error as IHttpFetchError; + const contentType = response?.headers.get('Content-Type') ?? 'unknown'; + const statusCode = response?.status ?? 500; - const { response, body = {} } = error as IHttpFetchError; - if (response) { - const { status, headers } = response; - if (body) { - value = JSON.stringify(body, null, 2); // ES error should be shown - contentType = headers.get('Content-Type'); - } else { - value = 'Request failed to get to the server (status code: ' + status + ')'; - contentType = headers.get('Content-Type'); - } - - if (isMultiRequest) { - value = '# ' + req.method + ' ' + req.url + '\n' + value; - } + if (body) { + value = JSON.stringify(body, null, 2); } else { - value = - "\n\nFailed to connect to Console's backend.\nPlease check the Kibana server is up and running"; + value = 'Request failed to get to the server (status code: ' + statusCode + ')'; + } + + if (isMultiRequest) { + value = '# ' + req.method + ' ' + req.url + '\n' + value; } reject({ diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index 1fe6f80166708..e5774526886fe 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -46,7 +46,6 @@ export async function send({ if (kibanaRequestUrl) { const httpMethod = method.toLowerCase() as Method; - // construct URL object, so we can extract out pathname, searchParams, etc... const url = new URL(kibanaRequestUrl); const { pathname, searchParams } = url; const query = Object.fromEntries(searchParams.entries()); From 4b0e4e634ae74a6ecae7c259ee48717136c3317e Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 18 Apr 2022 18:11:49 +0000 Subject: [PATCH 13/16] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../public/application/models/sense_editor/sense_editor.ts | 2 +- src/plugins/console/public/lib/es/es.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts index c141f9939bbdd..ac65afce2c18a 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.ts +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.ts @@ -8,7 +8,7 @@ import _ from 'lodash'; -import { XJson } from '../../../../../es_ui_shared/public'; +import { XJson } from '@kbn/es-ui-shared-plugin/public'; import RowParser from '../../../lib/row_parser'; import * as utils from '../../../lib/utils'; diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index e5774526886fe..fde2be5791f7e 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import type { HttpResponse, HttpSetup } from 'kibana/public'; +import type { HttpResponse, HttpSetup } from '@kbn/core/public'; import { trimStart } from 'lodash'; import { API_BASE_PATH, KIBANA_API_KEYWORD } from '../../../common/constants'; From c5e1bb4b4287a04724eefea64a7916c8116ad40e Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Wed, 20 Apr 2022 06:01:58 -0700 Subject: [PATCH 14/16] Address comments --- .../hooks/use_send_current_request/send_request.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts index 35033a09c22bf..1247f3f78aa68 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts @@ -128,8 +128,9 @@ export function sendRequest(args: RequestArgs): Promise { } catch (error) { let value; const { response, body } = error as IHttpFetchError; - const contentType = response?.headers.get('Content-Type') ?? 'unknown'; + const contentType = response?.headers.get('Content-Type') ?? ''; const statusCode = response?.status ?? 500; + const statusText = error?.response?.statusText ?? 'error'; if (body) { value = JSON.stringify(body, null, 2); @@ -146,8 +147,8 @@ export function sendRequest(args: RequestArgs): Promise { value, contentType, timeMs: Date.now() - startTime, - statusCode: error?.response?.status ?? 500, - statusText: error?.response?.statusText ?? 'error', + statusCode, + statusText, }, request: { data, From e8c74f76b4ca9523927571ffa2071bc9dd35c71e Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Thu, 21 Apr 2022 03:30:44 -0700 Subject: [PATCH 15/16] Resolve merge conflicts --- .../send_request.test.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts index d4d711baf435d..60ced085c6891 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.test.ts @@ -98,4 +98,37 @@ describe('sendRequest', () => { expect(mockedSendRequest).toHaveBeenCalledTimes(1); } }); + + describe('successful response value', () => { + describe('with text', () => { + it('should return value with lines separated', async () => { + mockedSendRequest.mockResolvedValue('\ntest_index-1 []\ntest_index-2 []\n'); + const response = await sendRequest({ + http: mockContextValue.services.http, + requests: [{ method: 'GET', url: 'test-1', data: [] }], + }); + + expect(response).toMatchInlineSnapshot(` + " + test_index-1 [] + test_index-2 [] + " + `); + expect(mockedSendRequest).toHaveBeenCalledTimes(1); + }); + }); + + describe('with parsed json', () => { + it('should stringify value', async () => { + mockedSendRequest.mockResolvedValue(JSON.stringify({ test: 'some value' })); + const response = await sendRequest({ + http: mockContextValue.services.http, + requests: [{ method: 'GET', url: 'test-2', data: [] }], + }); + + expect(typeof response).toBe('string'); + expect(mockedSendRequest).toHaveBeenCalledTimes(1); + }); + }); + }); }); From 58348e1bcec9d4c3ca4ec98880552f5690b5cae3 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Thu, 21 Apr 2022 05:58:21 -0700 Subject: [PATCH 16/16] Rename KIBANA_API_KEYWORD --- src/plugins/console/common/constants/api.ts | 2 +- src/plugins/console/common/constants/index.ts | 2 +- src/plugins/console/public/lib/es/es.ts | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/plugins/console/common/constants/api.ts b/src/plugins/console/common/constants/api.ts index 4258d81c8ce7f..3c5aa11519350 100644 --- a/src/plugins/console/common/constants/api.ts +++ b/src/plugins/console/common/constants/api.ts @@ -7,4 +7,4 @@ */ export const API_BASE_PATH = '/api/console'; -export const KIBANA_API_KEYWORD = 'kbn:'; +export const KIBANA_API_PREFIX = 'kbn:'; diff --git a/src/plugins/console/common/constants/index.ts b/src/plugins/console/common/constants/index.ts index 3dd6a62ec25e4..756a79883cbdb 100644 --- a/src/plugins/console/common/constants/index.ts +++ b/src/plugins/console/common/constants/index.ts @@ -7,4 +7,4 @@ */ export { MAJOR_VERSION } from './plugin'; -export { API_BASE_PATH, KIBANA_API_KEYWORD } from './api'; +export { API_BASE_PATH, KIBANA_API_PREFIX } from './api'; diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index fde2be5791f7e..10d0ad95b0496 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -8,7 +8,7 @@ import type { HttpResponse, HttpSetup } from '@kbn/core/public'; import { trimStart } from 'lodash'; -import { API_BASE_PATH, KIBANA_API_KEYWORD } from '../../../common/constants'; +import { API_BASE_PATH, KIBANA_API_PREFIX } from '../../../common/constants'; const esVersion: string[] = []; @@ -68,12 +68,12 @@ export async function send({ } function getKibanaRequestUrl(path: string) { - const isKibanaApiRequest = path.startsWith(KIBANA_API_KEYWORD); + const isKibanaApiRequest = path.startsWith(KIBANA_API_PREFIX); const kibanaBasePath = window.location.origin; if (isKibanaApiRequest) { // window.location.origin is used as a Kibana public base path for sending requests in cURL commands. E.g. "Copy as cURL". - return `${kibanaBasePath}/${trimStart(path.replace(KIBANA_API_KEYWORD, ''), '/')}`; + return `${kibanaBasePath}/${trimStart(path.replace(KIBANA_API_PREFIX, ''), '/')}`; } }