Class: Contrast::Utils::NetHttpBase

Inherits:
Object
  • Object
show all
Includes:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/utils/net_http_base.rb

Overview

This module creates a Net::HTTP client base to be used by different services All HTTP clients reporting to Telemetry or TS should inherit from this

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Instance Attribute Details

#client_nameString (readonly)

Returns:



31
32
33
# File 'lib/contrast/utils/net_http_base.rb', line 31

def client_name
  @client_name
end

Class Method Details

.last_errorStandardError

Last recorded error

Returns:

  • (StandardError)


20
21
22
# File 'lib/contrast/utils/net_http_base.rb', line 20

def last_error
  @_last_error
end

.last_error=(error) ⇒ Object

Parameters:

  • (StandardError)


25
26
27
# File 'lib/contrast/utils/net_http_base.rb', line 25

def last_error= error
  @_last_error = error
end

Instance Method Details

#connection_verified?(client, url) ⇒ Boolean

Validates connection with assigned domain. If connection is running, SSL certificate of the endpoint is valid, Ip address is resolvable and response is received without peer’s reset or refuse of connection, then validation returns true.

Parameters:

  • client (Net::HTTP)
  • url (String)

Returns:

  • (Boolean)

    true | false



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/contrast/utils/net_http_base.rb', line 70

def connection_verified? client, url
  return @_connection_verified unless @_connection_verified.nil?
  return false if client.nil?

  ipaddr = get_ipaddr(client)
  response = client.request(Net::HTTP::Get.new(url))
  verify_cert = client.address.to_s.include?('localhost') ||
      OpenSSL::SSL.verify_certificate_identity(client.peer_cert, client.address)
  resolved = resolved?(client.address, ipaddr)
  @_connection_verified = if resolved && response && verify_cert
                            true
                          else
                            false
                          end
rescue OpenSSL::SSL::SSLError, Resolv::ResolvError, Errno::ECONNRESET, Errno::ECONNREFUSED,
       Errno::ETIMEDOUT, Errno::ESHUTDOWN, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Errno::EISCONN,
       Errno::ECONNABORTED, Errno::ENETRESET, Errno::ENETUNREACH => e

  Contrast::Utils::NetHttpBase.last_error = e
  unless client_name == Contrast::Agent::Telemetry::Client::SERVICE_NAME
    logger.error("#{ client_name } connection failed", e.message)
  end
  @_connection_verified = false
end

#initialize_connection(service_name, url, use_proxy: false, use_custom_cert: false) ⇒ Net::HTTP?

This method initializes the Net::HTTP client we’ll need. it will validate the connection and make the first request. If connection is valid and response is available then the open connection is returned.

self signed certificates provided by config [default = false]

Parameters:

  • service_name (String)

    Name of service used in logging messages

  • url (String)
  • use_proxy (Boolean) (defaults to: false)

    flag used to indicate proxy connections [default = false]

  • use_custom_cert (Boolean) (defaults to: false)

    flag used to indicate whether the client is to use

Returns:

  • (Net::HTTP, nil)

    Return open connection or nil



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/contrast/utils/net_http_base.rb', line 43

def initialize_connection service_name, url, use_proxy: false, use_custom_cert: false
  Contrast::Utils::NetHttpBase.last_error = nil
  @client_name = service_name
  return unless (addr = retrieve_address(url))
  return unless (net_http_client = configure_new_client(addr, use_proxy, use_custom_cert))
  return unless client_started?(net_http_client)

  logger.debug("Starting #{ client_name } connection test")
  return unless connection_verified?(net_http_client, url)

  logger.debug('Client verified', service: client_name, url: url)
  net_http_client
rescue StandardError => e
  Contrast::Utils::NetHttpBase.last_error = e
  return if client_name == Contrast::Agent::Telemetry::Client::SERVICE_NAME

  logger.error('Connection failed', e, service: client_name, url: url)
  nil
end