|
18 | 18 | # |
19 | 19 |
|
20 | 20 | require 'spec_helper' |
| 21 | +require 'timeout' |
21 | 22 |
|
22 | 23 | describe 'NonblockingServer' do |
23 | 24 |
|
@@ -260,4 +261,102 @@ def setup_client_thread(result) |
260 | 261 | expect(@server_thread.join(2)).not_to be_nil |
261 | 262 | end |
262 | 263 | end |
| 264 | + |
| 265 | + describe "#{Thrift::NonblockingServer} with TLS transport" do |
| 266 | + before(:each) do |
| 267 | + @port = available_port |
| 268 | + handler = Handler.new |
| 269 | + processor = SpecNamespace::NonblockingService::Processor.new(handler) |
| 270 | + @transport = Thrift::SSLServerSocket.new('localhost', @port, create_server_ssl_context) |
| 271 | + transport_factory = Thrift::FramedTransportFactory.new |
| 272 | + logger = Logger.new(STDERR) |
| 273 | + logger.level = Logger::WARN |
| 274 | + @server = Thrift::NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger) |
| 275 | + handler.server = @server |
| 276 | + |
| 277 | + @server_thread = Thread.new(Thread.current) do |master_thread| |
| 278 | + begin |
| 279 | + @server.serve |
| 280 | + rescue => e |
| 281 | + master_thread.raise e |
| 282 | + end |
| 283 | + end |
| 284 | + |
| 285 | + @clients = [] |
| 286 | + wait_until_listening |
| 287 | + end |
| 288 | + |
| 289 | + after(:each) do |
| 290 | + @clients.each(&:close) |
| 291 | + @server.shutdown if @server |
| 292 | + @server_thread.join(2) if @server_thread |
| 293 | + @transport.close if @transport |
| 294 | + end |
| 295 | + |
| 296 | + it "should handle requests over TLS" do |
| 297 | + expect(@server_thread).to be_alive |
| 298 | + |
| 299 | + client = setup_tls_client |
| 300 | + expect(client.greeting(true)).to eq(SpecNamespace::Hello.new) |
| 301 | + |
| 302 | + @server.shutdown |
| 303 | + expect(@server_thread.join(2)).to be_an_instance_of(Thread) |
| 304 | + end |
| 305 | + |
| 306 | + def setup_tls_client |
| 307 | + transport = Thrift::FramedTransport.new( |
| 308 | + Thrift::SSLSocket.new('localhost', @port, nil, create_client_ssl_context) |
| 309 | + ) |
| 310 | + protocol = Thrift::BinaryProtocol.new(transport) |
| 311 | + client = SpecNamespace::NonblockingService::Client.new(protocol) |
| 312 | + transport.open |
| 313 | + @clients << transport |
| 314 | + client |
| 315 | + end |
| 316 | + |
| 317 | + def wait_until_listening |
| 318 | + Timeout.timeout(2) do |
| 319 | + until @transport.handle |
| 320 | + raise "Server thread exited unexpectedly" unless @server_thread.alive? |
| 321 | + sleep 0.01 |
| 322 | + end |
| 323 | + end |
| 324 | + end |
| 325 | + |
| 326 | + def available_port |
| 327 | + TCPServer.open('localhost', 0) { |server| server.addr[1] } |
| 328 | + end |
| 329 | + |
| 330 | + def ssl_keys_dir |
| 331 | + File.expand_path('../../../test/keys', __dir__) |
| 332 | + end |
| 333 | + |
| 334 | + def create_server_ssl_context |
| 335 | + OpenSSL::SSL::SSLContext.new.tap do |ctx| |
| 336 | + ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER |
| 337 | + if ctx.respond_to?(:min_version=) && OpenSSL::SSL.const_defined?(:TLS1_2_VERSION) |
| 338 | + ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION |
| 339 | + end |
| 340 | + ctx.ca_file = File.join(ssl_keys_dir, 'CA.pem') |
| 341 | + ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, 'server.crt'))) |
| 342 | + ctx.cert_store = OpenSSL::X509::Store.new |
| 343 | + ctx.cert_store.add_file(File.join(ssl_keys_dir, 'client.pem')) |
| 344 | + ctx.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, 'server.key'))) |
| 345 | + end |
| 346 | + end |
| 347 | + |
| 348 | + def create_client_ssl_context |
| 349 | + OpenSSL::SSL::SSLContext.new.tap do |ctx| |
| 350 | + ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER |
| 351 | + if ctx.respond_to?(:min_version=) && OpenSSL::SSL.const_defined?(:TLS1_2_VERSION) |
| 352 | + ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION |
| 353 | + end |
| 354 | + ctx.ca_file = File.join(ssl_keys_dir, 'CA.pem') |
| 355 | + ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, 'client.crt'))) |
| 356 | + ctx.cert_store = OpenSSL::X509::Store.new |
| 357 | + ctx.cert_store.add_file(File.join(ssl_keys_dir, 'server.pem')) |
| 358 | + ctx.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, 'client.key'))) |
| 359 | + end |
| 360 | + end |
| 361 | + end |
263 | 362 | end |
0 commit comments