lib/mongo/crypt/encryption_io.rb
# Provide a TLS socket to be used for KMS calls in a block API
#
# @param [ String ] endpoint The URI at which to connect the TLS socket.
# @yieldparam [ OpenSSL::SSL::SSLSocket ] ssl_socket Yields a TLS socket
#   connected to the specified endpoint.
#
# @raise [ Mongo::Error::KmsError ] If the socket times out or raises
#   an exception
#
# @note The socket is always closed when the provided block has finished
#   executing
Provide a TLS socket to be used for KMS calls in a block API
Provide a TLS socket to be used for KMS calls in a block API
def with_ssl_socket(endpoint)
        host, port = endpoint.split(':')
        port ||= 443 
        tcp_socket = TCPSocket.open(host, port)
        begin
          tcp_socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
          ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket)
          begin
            ssl_socket.sync_close = true
            ssl_socket.hostname = "#{host}:#{port}"
            Timeout.timeout(
              SOCKET_TIMEOUT,
              Error::SocketTimeoutError,
              "KMS socket connection timed out after #{SOCKET_TIMEOUT} seconds",
            ) do
              ssl_socket.connect
            end
            yield(ssl_socket)
          ensure
            begin
              Timeout.timeout(
                SOCKET_TIMEOUT,
                Error::SocketTimeoutError,
                'KMS TLS socket close timed out'
              ) do
                ssl_socket.sysclose
              end
            rescue
            end
          end
        ensure
          begin
            Timeout.timeout(
              SOCKET_TIMEOUT,
              Error::SocketTimeoutError,
              'KMS TCP socket close timed out'
            ) do
              tcp_socket.close
            end
          rescue
          end
        end
      rescue => e
        raise Error::KmsError, "Error decrypting data key: #{e.class}: #{e.message}"
      end
def with_ssl_socket ( endpoint ) host , port = endpoint . split ( ':' ) port ||= 443 tcp_socket = TCPSocket . open ( host , port ) begin tcp_socket . setsockopt ( :: Socket :: IPPROTO_TCP , :: Socket :: TCP_NODELAY , 1 ) ssl_socket = OpenSSL :: SSL :: SSLSocket . new ( tcp_socket ) begin ssl_socket . sync_close = true ssl_socket . hostname = "#{host}:#{port}" Timeout . timeout ( SOCKET_TIMEOUT , Error :: SocketTimeoutError , "KMS socket connection timed out after #{SOCKET_TIMEOUT} seconds" , ) do ssl_socket . connect end yield ( ssl_socket ) ensure begin Timeout . timeout ( SOCKET_TIMEOUT , Error :: SocketTimeoutError , 'KMS TLS socket close timed out' ) do ssl_socket . sysclose end rescue end end ensure begin Timeout . timeout ( SOCKET_TIMEOUT , Error :: SocketTimeoutError , 'KMS TCP socket close timed out' ) do tcp_socket . close end rescue end end rescue => e raise Error :: KmsError , "Error decrypting data key: #{e.class}: #{e.message}" end
Provide a TLS socket to be used for KMS calls in a block API
Provide a TLS socket to be used for KMS calls in a block API
# Default port for AWS KMS API # Create TCPSocket and set nodelay option # tcp_socket will be closed when ssl_socket is closed # perform SNI # Still close tcp socket manually in case TLS socket creation # fails.
[ Mongo::Error::KmsError ] If the socket times out or raises an exception
[ Mongo :: Error :: KmsError ] If the socket times out or raises an exception
endpoint
ssl_socket Yields a TLS socket connected to the specified endpoint.
ssl_socket Yields a TLS socket connected to the specified endpoint .
note
The socket is always closed when the provided block has finished executing
The socket is always closed when the provided block has finished executing