Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Update autocomplete route configuration to include proxy headers #149498

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -103,15 +103,40 @@ 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 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add some comments here on why this replacement needs to be made?

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 {
Expand Down Expand Up @@ -171,20 +196,20 @@ export const registerAutocompleteEntitiesRoute = (deps: RouteDependencies) => {
}

const legacyConfig = await deps.proxy.readLegacyESConfig();
const configWithHeaders = {
const config = {
...legacyConfig,
headers: request.headers,
request,
kibanaVersion: deps.kibanaVersion,
};

// 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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down