import { Socket } from 'net' ; import { DetailedPeerCertificate , PeerCertificate , TLSSocket } from 'tls' ; import { promisify } from 'util' ; public export interface IKibanaSocket { getPeerCertificate ( detailed ) : DetailedPeerCertificate | null ; getPeerCertificate ( detailed ) : PeerCertificate | null ; param detailed returns getPeerCertificate ( detailed ? ) : PeerCertificate | DetailedPeerCertificate | null ; /**
     * Returns a string containing the negotiated SSL/TLS protocol version of the current connection. The value 'unknown' will be returned for
     * connected sockets that have not completed the handshaking process. The value null will be returned for server sockets or disconnected
     * client sockets. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_get_version.html for more information.
     */ getProtocol ( ) : string | null ; param options returns renegotiate ( options ) : Promise < void > ; /**
     * Indicates whether or not the peer certificate was signed by one of the specified CAs. When TLS
     * isn't used the value is `undefined`.
     */ readonly authorized ? : boolean ; /**
     * The reason why the peer's certificate has not been verified. This property becomes available
     * only when `authorized` is `false`.
     */ readonly authorizationError ? : Error ; } export class KibanaSocket implements IKibanaSocket { public get authorized ( ) { return this . socket instanceof TLSSocket ? this . socket . authorized : undefined ; } public get authorizationError ( ) { return this . socket instanceof TLSSocket ? this . socket . authorizationError : undefined ; } constructor ( private readonly socket ) { } getPeerCertificate ( detailed ) ; getPeerCertificate ( detailed ) ; getPeerCertificate ( detailed ? ) ; public getPeerCertificate ( detailed ? ) { if ( this . socket instanceof TLSSocket ) { const peerCertificate = this . socket . getPeerCertificate ( detailed ) ; if ( peerCertificate && Object . keys ( peerCertificate ) . length > 0 ) return peerCertificate ; } return null ; } public getProtocol ( ) { if ( this . socket instanceof TLSSocket ) { return this . socket . getProtocol ( ) ; } return null ; } public async renegotiate ( options ) { if ( this . socket instanceof TLSSocket ) { return promisify ( this . socket . renegotiate . bind ( this . socket ) ) ( options ) ; } return Promise . reject ( new Error ( 'Cannot renegotiate a connection when TLS is not enabled.' ) ) ; } }