import { ConnectionOptions as TlsConnectionOptions } from 'tls' ; import { URL } from 'url' ; import { Duration } from 'moment' ; import { ClientOptions , NodeOptions } from '@elastic/elasticsearch' ; import { OpenSearchConfig } from '../opensearch_config' ; import { DEFAULT_HEADERS } from '../default_headers' ; /**
 * Configuration options to be used to create a  IClusterClient using the
 *  OpenSearchServiceStart . createClient public export type OpenSearchClientConfig = Pick < OpenSearchConfig , 'customHeaders' | 'logQueries' | 'sniffOnStart' | 'sniffOnConnectionFault' | 'requestHeadersWhitelist' | 'sniffInterval' | 'hosts' | 'username' | 'password' > & { pingTimeout ? : OpenSearchConfig [ 'pingTimeout' ] | ClientOptions [ 'pingTimeout' ] ; requestTimeout ? : OpenSearchConfig [ 'requestTimeout' ] | ClientOptions [ 'requestTimeout' ] ; ssl ? : Partial < OpenSearchConfig [ 'ssl' ] > ; keepAlive ? : boolean ; } ; param config param scoped export function parseClientOptions ( config , scoped ) { const clientOptions = { sniffOnStart : config . sniffOnStart , sniffOnConnectionFault : config . sniffOnConnectionFault , headers : { ... DEFAULT_HEADERS , ... config . customHeaders , } , } ; if ( config . pingTimeout != null ) { clientOptions . pingTimeout = getDurationAsMs ( config . pingTimeout ) ; } if ( config . requestTimeout != null ) { clientOptions . requestTimeout = getDurationAsMs ( config . requestTimeout ) ; } if ( config . sniffInterval != null ) { clientOptions . sniffInterval = typeof config . sniffInterval === 'boolean' ? config . sniffInterval : getDurationAsMs ( config . sniffInterval ) ; } if ( config . keepAlive ) { clientOptions . agent = { keepAlive : config . keepAlive , } ; } if ( config . username && config . password && ! scoped ) { clientOptions . auth = { username : config . username , password : config . password , } ; } clientOptions . nodes = config . hosts . map ( ( host ) => convertHost ( host ) ) ; if ( config . ssl ) { clientOptions . ssl = generateSslConfig ( config . ssl , scoped && ! config . ssl . alwaysPresentCertificate ) ; } return clientOptions ; } const generateSslConfig = ( sslConfig , ignoreCertAndKey ) : TlsConnectionOptions => { const ssl = { ca : sslConfig . certificateAuthorities , } ; const verificationMode = sslConfig . verificationMode ; switch ( verificationMode ) { case 'none' : ssl . rejectUnauthorized = false ; break ; case 'certificate' : ssl . rejectUnauthorized = true ; ssl . checkServerIdentity = ( ) => undefined ; break ; case 'full' : ssl . rejectUnauthorized = true ; break ; default : throw new Error ( `Unknown ssl verificationMode: ${ verificationMode }` ) ; } if ( ! ignoreCertAndKey && sslConfig . certificate && sslConfig . key ) { ssl . cert = sslConfig . certificate ; ssl . key = sslConfig . key ; ssl . passphrase = sslConfig . keyPassphrase ; } return ssl ; } ; const convertHost = ( host ) : NodeOptions => { const url = new URL ( host ) ; const isHTTPS = url . protocol === 'https:' ; url . port = url . port || ( isHTTPS ? '443' : '80' ) ; return { url , } ; } ; const getDurationAsMs = ( duration ) => typeof duration === 'number' ? duration : duration . asMilliseconds ( ) ;