kibana/src/plugins/console/server/lib/elasticsearch_proxy_config.ts
Jean-Louis Leysens edfdb78957
[Console] Clean up use of any (#95065)
* cleaned up autocomplete.ts and get_endpoint_from_postition.ts of anys

* general clean up of lowering hanging fruit

* cleaned up remaining anys, still need to test

* fix remaining TS compilation issues

* also tidy up use of "as any" and some more ": any"

* addressed type issues and introduced the default editor settings object

* clean up @ts-ignore

* added comments to interface

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-03-24 14:05:08 +01:00

57 lines
1.9 KiB
TypeScript

/*
* 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 _ from 'lodash';
import http from 'http';
import https from 'https';
import url from 'url';
import { ESConfigForProxy } from '../types';
const createAgent = (legacyConfig: ESConfigForProxy) => {
const target = url.parse(_.head(legacyConfig.hosts)!);
if (!/^https/.test(target.protocol || '')) return new http.Agent();
const agentOptions: https.AgentOptions = {};
const verificationMode = legacyConfig.ssl && legacyConfig.ssl.verificationMode;
switch (verificationMode) {
case 'none':
agentOptions.rejectUnauthorized = false;
break;
case 'certificate':
agentOptions.rejectUnauthorized = true;
// by default, NodeJS is checking the server identify
agentOptions.checkServerIdentity = (_.noop as unknown) as https.AgentOptions['checkServerIdentity'];
break;
case 'full':
agentOptions.rejectUnauthorized = true;
break;
default:
throw new Error(`Unknown ssl verificationMode: ${verificationMode}`);
}
agentOptions.ca = legacyConfig.ssl?.certificateAuthorities;
const ignoreCertAndKey = !legacyConfig.ssl?.alwaysPresentCertificate;
if (!ignoreCertAndKey && legacyConfig.ssl?.certificate && legacyConfig.ssl?.key) {
agentOptions.cert = legacyConfig.ssl.certificate;
agentOptions.key = legacyConfig.ssl.key;
agentOptions.passphrase = legacyConfig.ssl.keyPassphrase;
}
return new https.Agent(agentOptions);
};
export const getElasticsearchProxyConfig = (legacyConfig: ESConfigForProxy) => {
return {
timeout: legacyConfig.requestTimeout.asMilliseconds(),
agent: createAgent(legacyConfig),
};
};