Skip to content

reimplement do_https() using HttpPool #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions lib/puppet/util/nc_https.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'puppet/network/http_pool'
require 'json'

class Puppet::Util::Nc_https
Expand Down Expand Up @@ -181,37 +182,28 @@ def get_nodes(name)
def do_https(endpoint, method = 'post', data = {})
url = "#{@classifier_url}/#{endpoint}"
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)

unless @token and ! @token.empty?
Puppet.debug('Using SSL authentication')
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(File.read @certificate_path)
http.key = OpenSSL::PKey::RSA.new(File.read @private_key_path)
http.ca_file = @ca_certificate_path
http.verify_mode = OpenSSL::SSL::VERIFY_CLIENT_ONCE
else
Puppet.debug('Using token authentication')
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

req = Net::HTTP.const_get(method.capitalize).new(uri.request_uri)
req.body = data.to_json
req.content_type = 'application/json'

# If using token
req['X-Authentication'] = @token if @token
ssl_context = Puppet.lookup(:ssl_context)
conn = Puppet::Network::HttpPool.connection(uri.host, uri.port, ssl_context: ssl_context)

begin
res = http.request(req)
case method.capitalize
when 'Get'
res = conn.get(uri.path, {'Content-Type' => 'application/json'})
when 'Post'
res = conn.post(uri.path, data.to_json, {'Content-Type' => 'application/json'})
when 'Delete'
res = conn.delete(uri.path, {'Content-Type' => 'application/json'})
else
fail("Operation #{method.capitalize} is not supported!")
end
rescue Exception => e
fail(e.message)
debug(e.backtrace.inspect)
else
res
end
end

def error_msg(res)
json = JSON.parse(res.body)
kind = json['kind']
Expand Down