Class: Puppetserver::Ca::Utils::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetserver/ca/utils/http_client.rb

Overview

Utilities for doing HTTPS against the CA that wraps Net::HTTP constructs

Defined Under Namespace

Classes: Connection, Result, URL

Constant Summary collapse

DEFAULT_HEADERS =
{
  'User-Agent'   => 'PuppetserverCaCli',
  'Content-Type' => 'application/json',
  'Accept'       => 'application/json'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ HttpClient

Returns a new instance of HttpClient.



18
19
20
21
22
23
24
# File 'lib/puppetserver/ca/utils/http_client.rb', line 18

def initialize(settings)
  @store = make_store(settings[:localcacert],
                      settings[:certificate_revocation],
                      settings[:hostcrl])
  @cert = load_cert(settings[:hostcert])
  @key = load_key(settings[:hostprivkey])
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



16
17
18
# File 'lib/puppetserver/ca/utils/http_client.rb', line 16

def store
  @store
end

Instance Method Details

#load_cert(cert_path) ⇒ Object



26
27
28
# File 'lib/puppetserver/ca/utils/http_client.rb', line 26

def load_cert(cert_path)
  OpenSSL::X509::Certificate.new(File.read(cert_path))
end

#load_key(key_path) ⇒ Object



30
31
32
# File 'lib/puppetserver/ca/utils/http_client.rb', line 30

def load_key(key_path)
  OpenSSL::PKey.read(File.read(key_path))
end

#with_connection(url, &block) ⇒ Object

Takes an instance URL (defined lower in the file), and creates a connection. The given block is passed our own Connection object. The Connection object should have HTTP verbs defined on it that take a body (and optional overrides). Returns whatever the block given returned.



38
39
40
41
42
43
44
45
# File 'lib/puppetserver/ca/utils/http_client.rb', line 38

def with_connection(url, &block)
  request = ->(conn) { block.call(Connection.new(conn, url)) }

  Net::HTTP.start(url.host, url.port,
                  use_ssl: true, cert_store: @store,
                  cert: @cert, key: @key,
                  &request)
end