1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
require 'spec_helper'
require 'puppet_spec/https'
require 'puppet_spec/files'
require 'puppet/network/http_pool'
describe Puppet::Network::HttpPool, unless: Puppet::Util::Platform.jruby? do
include PuppetSpec::Files
before :all do
WebMock.disable!
end
after :all do
WebMock.enable!
end
before :each do
# make sure we don't take too long
Puppet[:http_connect_timeout] = '5s'
end
let(:hostname) { '127.0.0.1' }
let(:wrong_hostname) { 'localhost' }
let(:server) { PuppetSpec::HTTPSServer.new }
context "when calling deprecated HttpPool methods" do
before(:each) do
ssldir = tmpdir('http_pool')
Puppet[:ssldir] = ssldir
Puppet.settings.use(:main, :ssl)
File.write(Puppet[:localcacert], server.ca_cert.to_pem)
File.write(Puppet[:hostcrl], server.ca_crl.to_pem)
File.write(Puppet[:hostcert], server.server_cert.to_pem)
File.write(Puppet[:hostprivkey], server.server_key.to_pem)
end
def connection(host, port)
Puppet::Network::HttpPool.http_instance(host, port, use_ssl: true)
end
shared_examples_for 'HTTPS client' do
it "connects over SSL" do
server.start_server do |port|
http = connection(hostname, port)
res = http.get('/')
expect(res.code).to eq('200')
end
end
it "raises if the server's cert doesn't match the hostname we connected to" do
server.start_server do |port|
http = connection(wrong_hostname, port)
expect {
http.get('/')
}.to raise_error { |err|
expect(err).to be_instance_of(Puppet::SSL::CertMismatchError)
expect(err.message).to match(/\AServer hostname '#{wrong_hostname}' did not match server certificate; expected one of (.+)/)
md = err.message.match(/expected one of (.+)/)
expect(md[1].split(', ')).to contain_exactly('127.0.0.1', 'DNS:127.0.0.1', 'DNS:127.0.0.2')
}
end
end
it "raises if the server's CA is unknown" do
# File must exist and by not empty so DefaultValidator doesn't
# downgrade to VERIFY_NONE, so use a different CA that didn't
# issue the server's cert
capath = tmpfile('empty')
File.write(capath, cert_fixture('netlock-arany-utf8.pem'))
Puppet[:localcacert] = capath
Puppet[:certificate_revocation] = false
server.start_server do |port|
http = connection(hostname, port)
expect {
http.get('/')
}.to raise_error(Puppet::Error,
%r{certificate verify failed.* .self.signed certificate in certificate chain for CN=Test CA.})
end
end
it "detects when the server has closed the connection and reconnects" do
server.start_server do |port|
http = connection(hostname, port)
expect(http.request_get('/')).to be_a(Net::HTTPSuccess)
expect(http.request_get('/')).to be_a(Net::HTTPSuccess)
end
end
end
context "when using persistent HTTPS connections" do
around :each do |example|
begin
example.run
ensure
Puppet.runtime[:http].close
end
end
include_examples 'HTTPS client'
end
shared_examples_for "an HttpPool connection" do |klass, legacy_api|
before :each do
Puppet::Network::HttpPool.http_client_class = klass
end
it "connects using the scheme, host and port from the http instance preserving the URL path and query" do
request_line = nil
response_proc = -> (req, res) {
request_line = req.request_line
}
server.start_server(response_proc: response_proc) do |port|
http = Puppet::Network::HttpPool.http_instance(hostname, port, true)
path = "http://bogus.example.com:443/foo?q=a"
http.get(path)
if legacy_api
# The old API uses 'absolute-form' and passes the bogus hostname
# which isn't the host we connected to.
expect(request_line).to eq("GET http://bogus.example.com:443/foo?q=a HTTP/1.1\r\n")
else
expect(request_line).to eq("GET /foo?q=a HTTP/1.1\r\n")
end
end
end
it "requires the caller to URL encode the path and query when using absolute form" do
request_line = nil
response_proc = -> (req, res) {
request_line = req.request_line
}
server.start_server(response_proc: response_proc) do |port|
http = Puppet::Network::HttpPool.http_instance(hostname, port, true)
params = { 'key' => 'a value' }
encoded_url = "https://#{hostname}:#{port}/foo%20bar?q=#{Puppet::Util.uri_query_encode(params.to_json)}"
http.get(encoded_url)
if legacy_api
expect(request_line).to eq("GET #{encoded_url} HTTP/1.1\r\n")
else
expect(request_line).to eq("GET /foo%20bar?q=%7B%22key%22%3A%22a%20value%22%7D HTTP/1.1\r\n")
end
end
end
it "requires the caller to URL encode the path and query when using a path" do
request_line = nil
response_proc = -> (req, res) {
request_line = req.request_line
}
server.start_server(response_proc: response_proc) do |port|
http = Puppet::Network::HttpPool.http_instance(hostname, port, true)
params = { 'key' => 'a value' }
http.get("/foo%20bar?q=#{Puppet::Util.uri_query_encode(params.to_json)}")
expect(request_line).to eq("GET /foo%20bar?q=%7B%22key%22%3A%22a%20value%22%7D HTTP/1.1\r\n")
end
end
end
describe Puppet::Network::HTTP::Connection do
it_behaves_like "an HttpPool connection", described_class, false
end
end
context "when calling HttpPool.connection method" do
let(:ssl) { Puppet::SSL::SSLProvider.new }
let(:ssl_context) { ssl.create_root_context(cacerts: [server.ca_cert], crls: [server.ca_crl]) }
def connection(host, port, ssl_context:)
Puppet::Network::HttpPool.connection(host, port, ssl_context: ssl_context)
end
# Configure the server's SSLContext to require a client certificate. The `client_ca`
# setting allows the server to advertise which client CAs it will accept.
def require_client_certs(ctx)
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
ctx.client_ca = [cert_fixture('ca.pem')]
end
it "connects over SSL" do
server.start_server do |port|
http = connection(hostname, port, ssl_context: ssl_context)
res = http.get('/')
expect(res.code).to eq('200')
end
end
it "raises if the server's cert doesn't match the hostname we connected to" do
server.start_server do |port|
http = connection(wrong_hostname, port, ssl_context: ssl_context)
expect {
http.get('/')
}.to raise_error { |err|
expect(err).to be_instance_of(Puppet::SSL::CertMismatchError)
expect(err.message).to match(/\AServer hostname '#{wrong_hostname}' did not match server certificate; expected one of (.+)/)
md = err.message.match(/expected one of (.+)/)
expect(md[1].split(', ')).to contain_exactly('127.0.0.1', 'DNS:127.0.0.1', 'DNS:127.0.0.2')
}
end
end
it "raises if the server's CA is unknown" do
server.start_server do |port|
ssl_context = ssl.create_root_context(cacerts: [cert_fixture('netlock-arany-utf8.pem')],
crls: [server.ca_crl])
http = Puppet::Network::HttpPool.connection(hostname, port, ssl_context: ssl_context)
expect {
http.get('/')
}.to raise_error(Puppet::Error,
%r{certificate verify failed.* .self.signed certificate in certificate chain for CN=Test CA.})
end
end
it "warns when client has an incomplete client cert chain" do
expect(Puppet).to receive(:warning).with("The issuer 'CN=Test CA Agent Subauthority' of certificate 'CN=pluto' cannot be found locally")
pluto = cert_fixture('pluto.pem')
ssl_context = ssl.create_context(
cacerts: [server.ca_cert], crls: [server.ca_crl],
client_cert: pluto, private_key: key_fixture('pluto-key.pem')
)
# verify client has incomplete chain
expect(ssl_context.client_chain.map(&:to_der)).to eq([pluto.to_der])
# force server to require (not request) client certs
ctx_proc = -> (ctx) {
require_client_certs(ctx)
# server needs to trust the client's intermediate CA to complete the client's chain
ctx.cert_store.add_cert(cert_fixture('intermediate-agent.pem'))
}
server.start_server(ctx_proc: ctx_proc) do |port|
http = Puppet::Network::HttpPool.connection(hostname, port, ssl_context: ssl_context)
res = http.get('/')
expect(res.code).to eq('200')
end
end
it "sends a complete client cert chain" do
pluto = cert_fixture('pluto.pem')
client_ca = cert_fixture('intermediate-agent.pem')
ssl_context = ssl.create_context(
cacerts: [server.ca_cert, client_ca],
crls: [server.ca_crl, crl_fixture('intermediate-agent-crl.pem')],
client_cert: pluto,
private_key: key_fixture('pluto-key.pem')
)
# verify client has complete chain from leaf to root
expect(ssl_context.client_chain.map(&:to_der)).to eq([pluto, client_ca, server.ca_cert].map(&:to_der))
server.start_server(ctx_proc: method(:require_client_certs)) do |port|
http = Puppet::Network::HttpPool.connection(hostname, port, ssl_context: ssl_context)
res = http.get('/')
expect(res.code).to eq('200')
end
end
end
end
|