Skip to content

Commit

Permalink
[Console] Fix Kibana DevTool Copy as CURL does not url encode special…
Browse files Browse the repository at this point in the history
… chars in indice date math. (#130970)

* Fix cURL encoding for ES and Kibana requests. Add unit tests

* move changing global object in beforeEach

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
hro-maker and kibanamachine authored May 5, 2022
1 parent fe92ccf commit 880c321
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ POST /_sql?format=txt
"query": "SELECT prenom FROM claude_index WHERE prenom = 'claude' ",
"fetch_size": 1
}

GET <index_1-{now/d-2d}>,<index_1-{now/d-1d}>,<index_1-{now/d}>/_search?pretty

GET kbn:/api/spaces/space
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './sense_editor.test.mocks';

import $ from 'jquery';
import _ from 'lodash';
import { URL } from 'url';

import { create } from './create';
import { XJson } from '@kbn/es-ui-shared-plugin/public';
Expand All @@ -19,6 +20,8 @@ const { collapseLiteralStrings } = XJson;

describe('Editor', () => {
let input;
let oldUrl;
let olldWindow;

beforeEach(function () {
// Set up our document body
Expand All @@ -31,8 +34,19 @@ describe('Editor', () => {
input = create(document.querySelector('#ConAppEditor'));
$(input.getCoreEditor().getContainer()).show();
input.autocomplete._test.removeChangeListener();
oldUrl = global.URL;
olldWindow = { ...global.window };
global.URL = URL;
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
origin: 'http://localhost:5620',
},
});
});
afterEach(function () {
global.URL = oldUrl;
global.window = olldWindow;
$(input.getCoreEditor().getContainer()).hide();
input.autocomplete._test.addChangeListener();
});
Expand Down Expand Up @@ -476,4 +490,20 @@ curl -XPOST "http://localhost:9200/_sql?format=txt" -H "kbn-xsrf: reporting" -H
"fetch_size": 1
}'`.trim()
);

multiReqCopyAsCurlTest(
'with date math index',
editorInput1,
{ start: { lineNumber: 35 }, end: { lineNumber: 35 } },
`
curl -XGET "http://localhost:9200/%3Cindex_1-%7Bnow%2Fd-2d%7D%3E%2C%3Cindex_1-%7Bnow%2Fd-1d%7D%3E%2C%3Cindex_1-%7Bnow%2Fd%7D%3E%2F_search?pretty" -H "kbn-xsrf: reporting"`.trim()
);

multiReqCopyAsCurlTest(
'with Kibana API request',
editorInput1,
{ start: { lineNumber: 37 }, end: { lineNumber: 37 } },
`
curl -XGET "http://localhost:5620/api/spaces/space" -H \"kbn-xsrf: reporting\"`.trim()
);
});
22 changes: 17 additions & 5 deletions src/plugins/console/public/lib/es/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { HttpResponse, HttpSetup } from '@kbn/core/public';
import { trimStart } from 'lodash';
import { trimStart, trimEnd } from 'lodash';
import { API_BASE_PATH, KIBANA_API_PREFIX } from '../../../common/constants';

const esVersion: string[] = [];
Expand Down Expand Up @@ -79,11 +79,23 @@ function getKibanaRequestUrl(path: string) {

export function constructUrl(baseUri: string, path: string) {
const kibanaRequestUrl = getKibanaRequestUrl(path);
let url = `${trimEnd(baseUri, '/')}/${trimStart(path, '/')}`;

if (kibanaRequestUrl) {
return kibanaRequestUrl;
url = kibanaRequestUrl;
}
baseUri = baseUri.replace(/\/+$/, '');
path = path.replace(/^\/+/, '');
return baseUri + '/' + path;

const { origin, pathname, search } = new URL(url);
return `${origin}${encodePathname(pathname)}${search ?? ''}`;
}

const encodePathname = (path: string) => {
const decodedPath = new URLSearchParams(`path=${path}`).get('path') ?? '';

// Skip if it is valid
if (path === decodedPath) {
return path;
}

return `/${encodeURIComponent(trimStart(decodedPath, '/'))}`;
};

0 comments on commit 880c321

Please sign in to comment.