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
|
# -*- coding: utf-8 -*-
require "spec_helper"
require "socket"
CERTIFICATE_DIR = ENV.fetch("BUNNY_CERTIFICATE_DIR", "./spec/tls")
puts "Will use certificates from #{CERTIFICATE_DIR}"
shared_examples_for "successful TLS connection" do
it "succeeds", skip: ENV["CI"] do
expect(subject).to be_tls
ch = subject.create_channel
ch.confirm_select
q = ch.queue("", exclusive: true)
x = ch.default_exchange
x.publish("xyzzy", routing_key: q.name).
publish("xyzzy", routing_key: q.name).
publish("xyzzy", routing_key: q.name).
publish("xyzzy", routing_key: q.name)
x.wait_for_confirms
expect(q.message_count).to eq 4
i = 0
q.subscribe do |delivery_info, _, payload|
i += 1
end
sleep 1.0
expect(i).to eq 4
expect(q.message_count).to eq 0
ch.close
end
end
def local_hostname
ENV.fetch("BUNNY_RABBITMQ_HOSTNAME", "localhost")
end
context "initialized with tls: true", skip: ENV["CI"] do
let(:subject) do
Bunny.new(
hostname: local_hostname(),
user: "bunny_gem",
password: "bunny_password",
vhost: "bunny_testbed",
tls: true,
verify_peer: verify_peer,
tls_cert: "#{CERTIFICATE_DIR}/client_certificate.pem",
tls_key: "#{CERTIFICATE_DIR}/client_key.pem",
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"])
end
context "peer verification is off" do
let(:verify_peer) { false }
it "uses TLS port" do
expect(subject.port).to eq AMQ::Protocol::TLS_PORT
end
it "sends the SNI details" do
# https://github.com/ruby-amqp/bunny/issues/440
subject.start
expect(subject.transport.socket.hostname).to_not be_empty
end
after :each do
subject.close
end
end
context "peer verification is on" do
let(:verify_peer) { true }
it "uses TLS port" do
expect(subject.port).to eq AMQ::Protocol::TLS_PORT
end
end
end
describe "TLS connection to RabbitMQ with client certificates", skip: ENV["CI"] do
let(:subject) do
c = Bunny.new(
hostname: local_hostname(),
username: "bunny_gem",
password: "bunny_password",
vhost: "bunny_testbed",
tls: true,
tls_protocol: :TLSv1_2,
tls_cert: "#{CERTIFICATE_DIR}/client_certificate.pem",
tls_key: "#{CERTIFICATE_DIR}/client_key.pem",
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"],
verify_peer: false)
c.start
c
end
after :each do
subject.close
end
include_examples "successful TLS connection"
end
describe "TLS connection to RabbitMQ without client certificates", skip: ENV["CI"] do
let(:subject) do
c = Bunny.new(
hostname: local_hostname(),
username: "bunny_gem",
password: "bunny_password",
vhost: "bunny_testbed",
tls: true,
tls_protocol: :TLSv1_2,
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"],
verify_peer: false)
c.start
c
end
after :each do
subject.close
end
include_examples "successful TLS connection"
end
describe "TLS connection to RabbitMQ with a connection string", skip: ENV["CI"] do
let(:subject) do
c = Bunny.new("amqps://bunny_gem:bunny_password@#{local_hostname()}/bunny_testbed",
tls_protocol: :TLSv1_2,
tls_cert: "#{CERTIFICATE_DIR}/client_certificate.pem",
tls_key: "#{CERTIFICATE_DIR}/client_key.pem",
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"],
verify_peer: false)
c.start
c
end
after :each do
subject.close
end
include_examples "successful TLS connection"
context "when URI contains query parameters" do
subject(:session) do
Bunny.new("amqps://bunny_gem:bunny_password@#{local_hostname()}/bunny_testbed?heartbeat=10&connection_timeout=100&channel_max=1000&verify=false&cacertfile=#{CERTIFICATE_DIR}/ca_certificate.pem&certfile=#{CERTIFICATE_DIR}/client_certificate.pem&keyfile=#{CERTIFICATE_DIR}/client_key.pem")
end
it "parses extra connection parameters" do
session.start
expect(session.uses_tls?).to eq(true)
expect(session.transport.verify_peer).to eq(false)
expect(session.transport.tls_ca_certificates).to eq(["#{CERTIFICATE_DIR}/ca_certificate.pem"])
expect(session.transport.tls_certificate_path).to eq("#{CERTIFICATE_DIR}/client_certificate.pem")
expect(session.transport.tls_key_path).to eq("#{CERTIFICATE_DIR}/client_key.pem")
end
end
end
describe "TLS connection to RabbitMQ with a connection string and w/o client certificate and key", skip: ENV["CI"] do
let(:subject) do
c = Bunny.new("amqps://bunny_gem:bunny_password@#{local_hostname()}/bunny_testbed",
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"],
tls_protocol: :TLSv1_2,
verify_peer: verify_peer)
c.start
c
end
after :each do
subject.close
end
context "peer verification is off" do
let(:verify_peer) { false }
include_examples "successful TLS connection"
it "sends the SNI details" do
# https://github.com/ruby-amqp/bunny/issues/440
expect(subject.transport.socket.hostname).to_not be_empty
end
end
context "peer verification is on" do
let(:verify_peer) { true }
include_examples "successful TLS connection"
it "sends the SNI details" do
# https://github.com/ruby-amqp/bunny/issues/440
expect(subject.transport.socket.hostname).to_not be_empty
end
end
end
describe "TLS connection to RabbitMQ w/o client certificate", skip: ENV["CI"] do
let(:subject) do
c = Bunny.new("amqps://bunny_gem:bunny_password@#{local_hostname()}/bunny_testbed",
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"],
tls_protocol: :TLSv1_2,
verify_peer: false,
tls_silence_warnings: should_silence_warnings)
c.start
c
end
after :each do
subject.close
end
context "TLS-related warnings are enabled" do
let(:should_silence_warnings) { false }
include_examples "successful TLS connection"
end
context "TLS-related warnings are silenced" do
let(:should_silence_warnings) { true }
include_examples "successful TLS connection"
end
end
describe "TLS connection to RabbitMQ with client certificates provided inline", skip: ENV["CI"] do
let(:subject) do
c = Bunny.new(
hostname: local_hostname(),
username: "bunny_gem",
password: "bunny_password",
vhost: "bunny_testbed",
tls: true,
tls_cert: File.read("#{CERTIFICATE_DIR}/client_certificate.pem"),
tls_key: File.read("#{CERTIFICATE_DIR}/client_key.pem"),
tls_ca_certificates: ["#{CERTIFICATE_DIR}/ca_certificate.pem"],
tls_protocol: :TLSv1_2,
verify_peer: false)
c.start
c
end
after :each do
subject.close
end
include_examples "successful TLS connection"
end
|