diff --git a/lib/puppet/network/http_pool.rb b/lib/puppet/network/http_pool.rb index 8baf48c77..d7b0d4295 100644 --- a/lib/puppet/network/http_pool.rb +++ b/lib/puppet/network/http_pool.rb @@ -1,48 +1,48 @@ require 'puppet/ssl/host' require 'net/https' module Puppet::Network; end module Puppet::Network::HttpPool # Use the global localhost instance. def self.ssl_host Puppet::SSL::Host.localhost end # Use cert information from a Puppet client to set up the http object. def self.cert_setup(http) # Just no-op if we don't have certs. return false unless FileTest.exist?(Puppet[:hostcert]) and FileTest.exist?(Puppet[:localcacert]) http.cert_store = ssl_host.ssl_store http.ca_file = Puppet[:localcacert] http.cert = ssl_host.certificate.content http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.key = ssl_host.key.content end # Retrieve a cached http instance if caching is enabled, else return # a new one. - def self.http_instance(host, port, reset = false) + def self.http_instance(host, port, reset = false, use_ssl = true) args = [host, port] if Puppet[:http_proxy_host] == "none" args << nil << nil else args << Puppet[:http_proxy_host] << Puppet[:http_proxy_port] end http = Net::HTTP.new(*args) # Pop open the http client a little; older versions of Net::HTTP(s) didn't # give us a reader for ca_file... Grr... class << http; attr_accessor :ca_file; end - http.use_ssl = true + http.use_ssl = use_ssl # Use configured timeout (#1176) http.read_timeout = Puppet[:configtimeout] http.open_timeout = Puppet[:configtimeout] cert_setup(http) http end end diff --git a/lib/puppet/reports/http.rb b/lib/puppet/reports/http.rb index 7ac54dfbd..21855ca3a 100644 --- a/lib/puppet/reports/http.rb +++ b/lib/puppet/reports/http.rb @@ -1,22 +1,24 @@ require 'puppet' -require 'net/http' +require 'puppet/network/http_pool' require 'uri' Puppet::Reports.register_report(:http) do desc <<-DESC Send report information via HTTP to the `reporturl`. Each host sends its report as a YAML dump and this sends this YAML to a client via HTTP POST. The YAML is the `report` parameter of the request." DESC def process url = URI.parse(Puppet[:reporturl]) req = Net::HTTP::Post.new(url.path) req.body = self.to_yaml req.content_type = "application/x-yaml" - Net::HTTP.new(url.host, url.port).start {|http| + conn = Puppet::Network::HttpPool.http_instance(url.host, url.port, + ssl=(url.scheme == 'https')) + conn.start {|http| http.request(req) } end end diff --git a/spec/unit/reports/http_spec.rb b/spec/unit/reports/http_spec.rb index d7c37bfdd..f26532ae2 100755 --- a/spec/unit/reports/http_spec.rb +++ b/spec/unit/reports/http_spec.rb @@ -1,55 +1,56 @@ #!/usr/bin/env rspec require 'spec_helper' require 'puppet/reports' # FakeHTTP fakes the behavior of Net::HTTP#request and acts as a sensor for an # otherwise difficult to trace method call. # class FakeHTTP REQUESTS = {} def self.request(req) REQUESTS[req.path] = req end end processor = Puppet::Reports.report(:http) describe processor do before { Net::HTTP.any_instance.stubs(:start).yields(FakeHTTP) } subject { Puppet::Transaction::Report.new("apply").extend(processor) } it { should respond_to(:process) } - it "should use the reporturl setting's host and port" do + it "should use the reporturl setting's host, port and ssl option" do uri = URI.parse(Puppet[:reporturl]) - Net::HTTP.expects(:new).with(uri.host, uri.port).returns(stub_everything('http')) + ssl = (uri.scheme == 'https') + Puppet::Network::HttpPool.expects(:http_instance).with(uri.host, uri.port, ssl).returns(stub_everything('http')) subject.process end describe "request" do before { subject.process } describe "path" do it "should use the path specified by the 'reporturl' setting" do reports_request.path.should == URI.parse(Puppet[:reporturl]).path end end describe "body" do it "should be the report as YAML" do reports_request.body.should == subject.to_yaml end end describe "content type" do it "should be 'application/x-yaml'" do reports_request.content_type.should == "application/x-yaml" end end end private def reports_request; FakeHTTP::REQUESTS[URI.parse(Puppet[:reporturl]).path] end end