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] Proxy fallback #50185

Merged
merged 10 commits into from
Nov 27, 2019
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"@kbn/ui-framework": "1.0.0",
"@types/json-stable-stringify": "^1.0.32",
"@types/lodash.clonedeep": "^4.5.4",
"@types/node-fetch": "2.5.3",
"@types/react-grid-layout": "^0.16.7",
"@types/recompose": "^0.30.5",
"JSONStream": "1.3.5",
Expand Down
4 changes: 3 additions & 1 deletion src/legacy/core_plugins/console/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { first } from 'rxjs/operators';
import { resolve, join, sep } from 'path';
import url from 'url';
import { has, isEmpty, head, pick } from 'lodash';
import { LiveHostsManager } from './server/live_hosts_manager';

// @ts-ignore
import { addProcessorDefinition } from './server/api_server/es_6_0/ingest';
Expand Down Expand Up @@ -142,7 +143,8 @@ export default function(kibana: any) {

server.route(
createProxyRoute({
baseUrl: head(legacyEsConfig.hosts),
// baseUrl: head(legacyEsConfig.hosts),
liveHostsManager: new LiveHostsManager(legacyEsConfig.hosts),
pathFilters: proxyPathFilters,
getConfigForReq(req: any, uri: any) {
const filteredHeaders = filterHeaders(
Expand Down
20 changes: 20 additions & 0 deletions src/legacy/core_plugins/console/server/live_hosts_manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { LiveHostsManager } from './live_hosts_manager';
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { findLiveHostIdx } from './utils';

export class LiveHostsManager {
// Assume first host is live
private pointer = 0;

constructor(private readonly hosts: string[]) {}

async getLiveHost(): Promise<string> {
this.pointer = await findLiveHostIdx(this.pointer, this.hosts);
return this.hosts[this.pointer];
}
}
57 changes: 57 additions & 0 deletions src/legacy/core_plugins/console/server/live_hosts_manager/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import fetch from 'node-fetch';

const probe = async (host: string): Promise<{ ok: boolean }> => {
try {
await fetch(`${host}/`, { method: 'HEAD' });
return { ok: true };
} catch (e) {
return { ok: false };
}
};

const roundRobinNextIdx = (number: number, max: number) => {
if (number >= max) {
return 0;
}
return number + 1;
};

export const findLiveHostIdx = async (startIdx: number, hosts: string[]) => {
if ((await probe(hosts[startIdx])).ok) {
jloleysens marked this conversation as resolved.
Show resolved Hide resolved
return startIdx;
}

const recurse = async (idx: number): Promise<number> => {
if (idx === startIdx) {
throw new Error('No live host found!');
}
const host = hosts[idx];
const result = await probe(host);
if (result.ok) {
return idx;
}
const nextId = roundRobinNextIdx(idx, hosts.length);
return recurse(nextId);
};

return await recurse(roundRobinNextIdx(startIdx, hosts.length));
};
3 changes: 2 additions & 1 deletion src/legacy/core_plugins/console/server/proxy_route.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getProxyHeaders(req) {
}

export const createProxyRoute = ({
baseUrl = '/',
liveHostsManager,
pathFilters = [/.*/],
getConfigForReq = () => ({}),
}) => ({
Expand Down Expand Up @@ -100,6 +100,7 @@ export const createProxyRoute = ({
handler: async (req, h) => {
const { payload, query } = req;
const { path, method } = query;
const baseUrl = await liveHostsManager.getLiveHost();
const uri = toURL(baseUrl, path);

// Because this can technically be provided by a settings-defined proxy config, we need to
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3652,6 +3652,13 @@
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "2.5.3"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.3.tgz#b84127facd93642b1fb6439bc630ba0612e3ec50"
integrity sha512-X3TNlzZ7SuSwZsMkb5fV7GrPbVKvHc2iwHmslb8bIxRKWg2iqkfm3F/Wd79RhDpOXR7wCtKAwc5Y2JE6n/ibyw==
dependencies:
"@types/node" "*"

"@types/node-fetch@^2.5.0":
version "2.5.0"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.0.tgz#1c55616a4591bdd15a389fbd0da4a55b9502add5"
Expand Down