import { ConfigOptions } from 'elasticsearch' ; import { cloneDeep } from 'lodash' ; import { Duration } from 'moment' ; import { checkServerIdentity } from 'tls' ; import url from 'url' ; import { pick } from '@osd/std' ; import { Logger } from '../../logging' ; import { OpenSearchConfig } from '../opensearch_config' ; import { DEFAULT_HEADERS } from '../default_headers' ; privateRemarks deprecated public export type LegacyOpenSearchClientConfig = Pick < ConfigOptions , 'keepAlive' | 'log' | 'plugins' > & Pick < OpenSearchConfig , 'apiVersion' | 'customHeaders' | 'logQueries' | 'requestHeadersWhitelist' | 'sniffOnStart' | 'sniffOnConnectionFault' | 'hosts' | 'username' | 'password' > & { pingTimeout ? : OpenSearchConfig [ 'pingTimeout' ] | ConfigOptions [ 'pingTimeout' ] ; requestTimeout ? : OpenSearchConfig [ 'requestTimeout' ] | ConfigOptions [ 'requestTimeout' ] ; sniffInterval ? : OpenSearchConfig [ 'sniffInterval' ] | ConfigOptions [ 'sniffInterval' ] ; ssl ? : Partial < OpenSearchConfig [ 'ssl' ] > ; } ; internal interface LegacyOpenSearchClientConfigOverrides { /**
     * If set to `true`, username and password from the config won't be used
     * to access OpenSearch API even if these are specified.
     */ auth ? : boolean ; /**
     * If set to `true`, `ssl.key` and `ssl.certificate` provided through config won't
     * be used to connect to OpenSearch.
     */ ignoreCertAndKey ? : boolean ; } internal type ExtendedConfigOptions = ConfigOptions & Partial < { ssl : Partial < { rejectUnauthorized : boolean ; checkServerIdentity : typeof checkServerIdentity ; ca : string [ ] ; cert : string ; key : string ; passphrase : string ; } > ; } > ; internal export function parseOpenSearchClientConfig ( config , log , { ignoreCertAndKey = false , auth = true } = { } ) { const opensearchClientConfig = { keepAlive : true , ... pick ( config , [ 'apiVersion' , 'sniffOnStart' , 'sniffOnConnectionFault' , 'keepAlive' , 'log' , 'plugins' , ] ) , } ; if ( opensearchClientConfig . log == null ) { opensearchClientConfig . log = getLoggerClass ( log , config . logQueries ) ; } if ( config . pingTimeout != null ) { opensearchClientConfig . pingTimeout = getDurationAsMs ( config . pingTimeout ) ; } if ( config . requestTimeout != null ) { opensearchClientConfig . requestTimeout = getDurationAsMs ( config . requestTimeout ) ; } if ( config . sniffInterval ) { opensearchClientConfig . sniffInterval = getDurationAsMs ( config . sniffInterval ) ; } if ( Array . isArray ( config . hosts ) ) { const needsAuth = auth !== false && config . username && config . password ; opensearchClientConfig . hosts = config . hosts . map ( ( nodeUrl ) => { const uri = url . parse ( nodeUrl ) ; const httpsURI = uri . protocol === 'https:' ; const httpURI = uri . protocol === 'http:' ; const host = { host : uri . hostname , port : uri . port || ( httpsURI && '443' ) || ( httpURI && '80' ) , protocol : uri . protocol , path : uri . pathname , query : uri . query , headers : { ... DEFAULT_HEADERS , ... config . customHeaders , } , } ; if ( needsAuth ) { host . auth = `${ config . username }:${ config . password }` ; } return host ; } ) ; } if ( config . ssl === undefined ) { return cloneDeep ( opensearchClientConfig ) ; } opensearchClientConfig . ssl = { } ; const verificationMode = config . ssl . verificationMode ; switch ( verificationMode ) { case 'none' : opensearchClientConfig . ssl . rejectUnauthorized = false ; break ; case 'certificate' : opensearchClientConfig . ssl . rejectUnauthorized = true ; opensearchClientConfig . ssl . checkServerIdentity = ( ) => undefined ; break ; case 'full' : opensearchClientConfig . ssl . rejectUnauthorized = true ; break ; default : throw new Error ( `Unknown ssl verificationMode: ${ verificationMode }` ) ; } opensearchClientConfig . ssl . ca = config . ssl . certificateAuthorities ; if ( ! ignoreCertAndKey && config . ssl . certificate && config . ssl . key ) { opensearchClientConfig . ssl . cert = config . ssl . certificate ; opensearchClientConfig . ssl . key = config . ssl . key ; opensearchClientConfig . ssl . passphrase = config . ssl . keyPassphrase ; } return cloneDeep ( opensearchClientConfig ) ; } function getDurationAsMs ( duration ) { if ( typeof duration === 'number' ) { return duration ; } return duration . asMilliseconds ( ) ; } function getLoggerClass ( log , logQueries = false ) { return class OpenSearchClientLogging { public error ( err ) { log . error ( err ) ; } public warning ( message ) { log . warn ( message ) ; } public trace ( method , options , query , _ , statusCode ) { if ( logQueries ) { log . debug ( `${ statusCode }\n${ method } ${ options . path }\n${ query ? query . trim ( ) : '' }` , { tags : [ 'query' ] , } ) ; } } public info ( ) { } public debug ( ) { } public close ( ) { } } ; }