lib/shared/pe_status_check.rb
def http_get(path, port, host = Puppet[:certname])
    # Use an instance variable to only create an SSLContext once
    @ssl_context ||= Puppet::SSL::SSLContext.new(
      cacerts: Puppet[:localcacert],
      private_key: OpenSSL::PKey::RSA.new(File.read(Puppet[:hostprivkey])),
      client_cert: OpenSSL::X509::Certificate.new(File.open(Puppet[:hostcert])),
    )

    client = Net::HTTP.new(host, port)
    # The main reason to use this approach is to set open and read timeouts to a small value
    # Puppet's HTTP client does not allow access to these
    client.open_timeout = 2
    client.read_timeout = 2
    client.use_ssl = true
    client.verify_mode = OpenSSL::SSL::VERIFY_PEER
    client.cert = @ssl_context.client_cert
    client.key = @ssl_context.private_key
    client.ca_file = @ssl_context.cacerts

    response = client.request_get(Puppet::Util.uri_encode(path))
    if response.is_a? Net::HTTPSuccess
      JSON.parse(response.body)
    else
      false
    end
  rescue StandardError => e
    Facter.warn("Error in fact 'pe_status_check' when querying #{path}: #{e.message}")
    Facter.debug(e.backtrace)
    false
  end
# Module method to make a GET request to an api specified by path and port params
# @return [Hash] Response body of the API call
# @param path [String] The API path to query.  Should include a '/' prefix and query parameters
# @param port [Integer] The port to use
# @param host [String] The FQDN to use in making the connection.  Defaults to the Puppet certname
Module method to make a GET request to an api specified by path and port params
Module method to make a GET request to an api specified by path and port params
def http_get(path, port, host = Puppet[:certname])
    @ssl_context ||= Puppet::SSL::SSLContext.new(
      cacerts: Puppet[:localcacert],
      private_key: OpenSSL::PKey::RSA.new(File.read(Puppet[:hostprivkey])),
      client_cert: OpenSSL::X509::Certificate.new(File.open(Puppet[:hostcert])),
    )
    client = Net::HTTP.new(host, port)
    client.open_timeout = 2
    client.read_timeout = 2
    client.use_ssl = true
    client.verify_mode = OpenSSL::SSL::VERIFY_PEER
    client.cert = @ssl_context.client_cert
    client.key = @ssl_context.private_key
    client.ca_file = @ssl_context.cacerts
    response = client.request_get(Puppet::Util.uri_encode(path))
    if response.is_a? Net::HTTPSuccess
      JSON.parse(response.body)
    else
      false
    end
  rescue StandardError => e
    Facter.warn("Error in fact 'pe_status_check' when querying #{path}: #{e.message}")
    Facter.debug(e.backtrace)
    false
  end
def http_get ( path , port , host = Puppet [ :certname ] ) @ssl_context ||= Puppet :: SSL :: SSLContext . new ( cacerts : Puppet [ :localcacert ] , private_key : OpenSSL :: PKey :: RSA . new ( File . read ( Puppet [ :hostprivkey ] ) ) , client_cert : OpenSSL :: X509 :: Certificate . new ( File . open ( Puppet [ :hostcert ] ) ) , ) client = Net :: HTTP . new ( host , port ) client . open_timeout = 2 client . read_timeout = 2 client . use_ssl = true client . verify_mode = OpenSSL :: SSL :: VERIFY_PEER client . cert = @ssl_context . client_cert client . key = @ssl_context . private_key client . ca_file = @ssl_context . cacerts response = client . request_get ( Puppet :: Util . uri_encode ( path ) ) if response . is_a? Net :: HTTPSuccess JSON . parse ( response . body ) else false end rescue StandardError => e Facter . warn ( "Error in fact 'pe_status_check' when querying #{path}: #{e.message}" ) Facter . debug ( e . backtrace ) false end
Module method to make a GET request to an api specified by path and port params
Module method to make a GET request to an api specified by path and port params
# Use an instance variable to only create an SSLContext once # The main reason to use this approach is to set open and read timeouts to a small value # Puppet's HTTP client does not allow access to these
Response body of the API call
Response body of the API call
Hash
path
The API path to query. Should include a '/' prefix and query parameters
The API path to query . Should include a ' / ' prefix and query parameters
port
The port to use
The port to use
host
The FQDN to use in making the connection. Defaults to the Puppet certname
The FQDN to use in making the connection . Defaults to the Puppet certname