From 66171a74d095f3637236045a3e9f7fd2adb2ae0f Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Wed, 25 Jan 2023 16:22:00 +0500 Subject: [PATCH 1/3] [Console] Update autocomplete proxy configuration to inlcude proxy headers --- .../console/autocomplete_entities/index.ts | 27 +++++++++++++++---- .../api/console/proxy/create_handler.ts | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts b/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts index dae50854e00f8..7f2eefcee8af3 100644 --- a/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts +++ b/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts @@ -16,7 +16,7 @@ import type { SemVer } from 'semver'; import type { RouteDependencies } from '../../..'; import { sanitizeHostname } from '../../../../lib/utils'; import type { ESConfigForProxy } from '../../../../types'; -import { getRequestConfig } from '../proxy/create_handler'; +import { getRequestConfig, getProxyHeaders } from '../proxy/create_handler'; interface SettingsToRetrieve { indices: boolean; @@ -25,7 +25,7 @@ interface SettingsToRetrieve { dataStreams: boolean; } -type Config = ESConfigForProxy & { headers: KibanaRequest['headers'] } & { kibanaVersion: SemVer }; +type Config = ESConfigForProxy & { request: KibanaRequest } & { kibanaVersion: SemVer }; const MAX_RESPONSE_SIZE = 10 * 1024 * 1024; // 10MB // Limit the response size to 10MB, because the response can be very large and sending it to the client @@ -103,15 +103,32 @@ const getEntity = (path: string, config: Config) => { const host = hosts[idx]; const uri = new URL(host + path); const { protocol, hostname, port } = uri; - const { headers } = getRequestConfig(config.headers, config, uri.toString(), kibanaVersion); + const { headers, agent } = getRequestConfig(config.request.headers, config, uri.toString(), kibanaVersion); + const proxyHeaders = getProxyHeaders(config.request); const client = protocol === 'https:' ? https : http; + + const requestHeaders = { + ...headers, + ...proxyHeaders, + } + + const hasHostHeader = Object.keys(requestHeaders).some((key) => key.toLowerCase() === 'host'); + if (!hasHostHeader) { + requestHeaders['host'] = hostname; + } + const options = { method: 'GET', - headers: { ...headers }, + headers: { + ...requestHeaders, + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + }, host: sanitizeHostname(hostname), port: port === '' ? undefined : parseInt(port, 10), protocol, path: `${path}?pretty=false`, // add pretty=false to compress the response by removing whitespace + agent, }; try { @@ -173,7 +190,7 @@ export const registerAutocompleteEntitiesRoute = (deps: RouteDependencies) => { const legacyConfig = await deps.proxy.readLegacyESConfig(); const configWithHeaders = { ...legacyConfig, - headers: request.headers, + request, kibanaVersion: deps.kibanaVersion, }; diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index 70de8d6c59c7f..30e432d6226ef 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -70,7 +70,7 @@ export function getRequestConfig( }; } -function getProxyHeaders(req: KibanaRequest) { +export function getProxyHeaders(req: KibanaRequest) { const headers = Object.create(null); // Scope this proto-unsafe functionality to where it is being used. From 1d88d7fb82aaf72f31215b0f4c4318341ac8d01c Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 25 Jan 2023 11:31:35 +0000 Subject: [PATCH 2/3] [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' --- .../routes/api/console/autocomplete_entities/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts b/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts index 7f2eefcee8af3..070474d8ae867 100644 --- a/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts +++ b/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts @@ -103,18 +103,23 @@ const getEntity = (path: string, config: Config) => { const host = hosts[idx]; const uri = new URL(host + path); const { protocol, hostname, port } = uri; - const { headers, agent } = getRequestConfig(config.request.headers, config, uri.toString(), kibanaVersion); + const { headers, agent } = getRequestConfig( + config.request.headers, + config, + uri.toString(), + kibanaVersion + ); const proxyHeaders = getProxyHeaders(config.request); const client = protocol === 'https:' ? https : http; const requestHeaders = { ...headers, ...proxyHeaders, - } + }; const hasHostHeader = Object.keys(requestHeaders).some((key) => key.toLowerCase() === 'host'); if (!hasHostHeader) { - requestHeaders['host'] = hostname; + requestHeaders.host = hostname; } const options = { From 5e314ca8c62774986524f73543d2b5ea7ed9e89e Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Wed, 25 Jan 2023 17:09:59 +0500 Subject: [PATCH 3/3] Add comments --- .../api/console/autocomplete_entities/index.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts b/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts index 070474d8ae867..17643f1bd7070 100644 --- a/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts +++ b/src/plugins/console/server/routes/api/console/autocomplete_entities/index.ts @@ -118,6 +118,9 @@ const getEntity = (path: string, config: Config) => { }; const hasHostHeader = Object.keys(requestHeaders).some((key) => key.toLowerCase() === 'host'); + // if the host header is not set, then set it to the hostname. This is needed because the + // request will fail if the host header is not set and it is used to determine the node to + // send the request to in a multi-node cluster. if (!hasHostHeader) { requestHeaders.host = hostname; } @@ -193,7 +196,7 @@ export const registerAutocompleteEntitiesRoute = (deps: RouteDependencies) => { } const legacyConfig = await deps.proxy.readLegacyESConfig(); - const configWithHeaders = { + const config = { ...legacyConfig, request, kibanaVersion: deps.kibanaVersion, @@ -201,12 +204,12 @@ export const registerAutocompleteEntitiesRoute = (deps: RouteDependencies) => { // Wait for all requests to complete, in case one of them fails return the successfull ones const results = await Promise.allSettled([ - getMappings(settings, configWithHeaders), - getAliases(settings, configWithHeaders), - getDataStreams(settings, configWithHeaders), - getLegacyTemplates(settings, configWithHeaders), - getIndexTemplates(settings, configWithHeaders), - getComponentTemplates(settings, configWithHeaders), + getMappings(settings, config), + getAliases(settings, config), + getDataStreams(settings, config), + getLegacyTemplates(settings, config), + getIndexTemplates(settings, config), + getComponentTemplates(settings, config), ]); const [mappings, aliases, dataStreams, legacyTemplates, indexTemplates, componentTemplates] =