|
| 1 | +require 'spec_helper' |
| 2 | +require 'puppet_spec/https' |
| 3 | +require 'puppet_spec/files' |
| 4 | + |
| 5 | +describe Puppet::HTTP::Client, unless: Puppet::Util::Platform.jruby? do |
| 6 | + include PuppetSpec::Files |
| 7 | + |
| 8 | + before :all do |
| 9 | + WebMock.disable! |
| 10 | + end |
| 11 | + |
| 12 | + after :all do |
| 13 | + WebMock.enable! |
| 14 | + end |
| 15 | + |
| 16 | + before :each do |
| 17 | + # make sure we don't take too long |
| 18 | + Puppet[:http_connect_timeout] = '5s' |
| 19 | + end |
| 20 | + |
| 21 | + let(:hostname) { '127.0.0.1' } |
| 22 | + let(:wrong_hostname) { 'localhost' } |
| 23 | + let(:server) { PuppetSpec::HTTPSServer.new } |
| 24 | + let(:client) { Puppet::HTTP::Client.new } |
| 25 | + let(:ssl_provider) { Puppet::SSL::SSLProvider.new } |
| 26 | + let(:root_context) { ssl_provider.create_root_context(cacerts: [server.ca_cert], crls: [server.ca_crl]) } |
| 27 | + |
| 28 | + context "when verifying an HTTPS server" do |
| 29 | + it "connects over SSL" do |
| 30 | + server.start_server do |port| |
| 31 | + res = client.get(URI("https://127.0.0.1:#{port}"), ssl_context: root_context) |
| 32 | + expect(res).to be_success |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + it "raises if the server's cert doesn't match the hostname we connected to" do |
| 37 | + server.start_server do |port| |
| 38 | + expect { |
| 39 | + client.get(URI("https://#{wrong_hostname}:#{port}"), ssl_context: root_context) |
| 40 | + }.to raise_error { |err| |
| 41 | + expect(err).to be_instance_of(Puppet::HTTP::ConnectionError) |
| 42 | + expect(err.message).to match(/Server hostname '#{wrong_hostname}' did not match server certificate; expected one of (.+)/) |
| 43 | + |
| 44 | + md = err.message.match(/expected one of (.+)/) |
| 45 | + expect(md[1].split(', ')).to contain_exactly('127.0.0.1', 'DNS:127.0.0.1', 'DNS:127.0.0.2') |
| 46 | + } |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + it "raises if the server's CA is unknown" do |
| 51 | + wrong_ca = cert_fixture('netlock-arany-utf8.pem') |
| 52 | + alt_context = ssl_provider.create_root_context(cacerts: [wrong_ca], revocation: false) |
| 53 | + |
| 54 | + server.start_server do |port| |
| 55 | + expect { |
| 56 | + client.get(URI("https://127.0.0.1:#{port}"), ssl_context: alt_context) |
| 57 | + }.to raise_error(Puppet::HTTP::ConnectionError, |
| 58 | + %r{certificate verify failed.* .self signed certificate in certificate chain for CN=Test CA.}) |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context "with client certs" do |
| 64 | + let(:ctx_proc) { |
| 65 | + -> ctx { |
| 66 | + # configures the server to require the client to present a client cert |
| 67 | + ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + it "mutually authenticates the connection" do |
| 72 | + client_context = ssl_provider.create_context( |
| 73 | + cacerts: [server.ca_cert], crls: [server.ca_crl], |
| 74 | + client_cert: server.server_cert, private_key: server.server_key |
| 75 | + ) |
| 76 | + |
| 77 | + server.start_server(ctx_proc: ctx_proc) do |port| |
| 78 | + res = client.get(URI("https://127.0.0.1:#{port}"), ssl_context: client_context) |
| 79 | + expect(res).to be_success |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "with a system trust store" do |
| 85 | + it "connects when the client trusts the server's CA" do |
| 86 | + system_context = ssl_provider.create_system_context(cacerts: [server.ca_cert]) |
| 87 | + |
| 88 | + server.start_server do |port| |
| 89 | + res = client.get(URI("https://127.0.0.1:#{port}"), ssl_context: system_context) |
| 90 | + expect(res).to be_success |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + it "connects when the server's CA is in the system store" do |
| 95 | + # create a temp cacert bundle |
| 96 | + ssl_file = tmpfile('systemstore') |
| 97 | + File.write(ssl_file, server.ca_cert) |
| 98 | + |
| 99 | + # override path to system cacert bundle, this must be done before |
| 100 | + # the SSLContext is created and the call to X509::Store.set_default_paths |
| 101 | + Puppet::Util.withenv("SSL_CERT_FILE" => ssl_file) do |
| 102 | + system_context = ssl_provider.create_system_context(cacerts: []) |
| 103 | + server.start_server do |port| |
| 104 | + res = client.get(URI("https://127.0.0.1:#{port}"), ssl_context: system_context) |
| 105 | + expect(res).to be_success |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + it "raises if the server's CA is not in the context or system store" do |
| 111 | + system_context = ssl_provider.create_system_context(cacerts: [cert_fixture('netlock-arany-utf8.pem')]) |
| 112 | + |
| 113 | + server.start_server do |port| |
| 114 | + expect { |
| 115 | + client.get(URI("https://127.0.0.1:#{port}"), ssl_context: system_context) |
| 116 | + }.to raise_error(Puppet::HTTP::ConnectionError, |
| 117 | + %r{certificate verify failed.* .self signed certificate in certificate chain for CN=Test CA.}) |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments