0

I have been running Sinatra with Webrick and SSL using Rack::SSLenforcer in a development environment for a long while without any issues (based on https://github.com/tobmatth/rack-ssl-enforcer#readme ), i am trying to migrate to Thin in order to add websockets support but have issues getting my current app (without websockets) to run with Thin and SSL.

The basic code that i currently have on websockets is the following:

begin
  pkey = OpenSSL::PKey::RSA.new(File.open("private_key.pem").read)
  cert = OpenSSL::X509::Certificate.new(File.open("certificate.pem").read)
end

webrick_options = {
        :Port               => 8447,
        :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
        :DocumentRoot       => "/ruby/htdocs",
        :SSLEnable          => true,
        :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate     => cert,
        :SSLPrivateKey      => pkey,
        :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ],
        :app                  => MyWebRTCServer
}

Rack::Server.start webrick_options

Then in my app i have the following:

configure do
    # require SSL - https://github.com/tobmatth/rack-ssl-enforcer#readme
    use Rack::SslEnforcer
    set :session_secret, 'asdfa2342923422f1adc05c837fa234230e3594b93824b00e930ab0fb94b'

    use Rack::Session::Cookie, :key => '_rack_session',
                           :path => '/',
                           :expire_after => 2592000, # In seconds
                           :secret => session_secret

    # load password file - 
    begin
      @@config = YAML.load_file(File.join(Dir.pwd, 'config', 'users.yml'))
    rescue ArgumentError => e
      puts "Could not parse YAML: #{e.message}"
    end

    # puts "config: " + @@config.to_s
    use Rack::Auth::Basic, "Restricted Area" do |u, p|
      $LOG.info "Use Rack::Auth::Basic"

      if (!@@config[:users][u])
        puts "Bad username"
        false
      else
        # initialize the BCrypt with the password
        tPassword = BCrypt::Password.new(@@config[:users][u][:password].to_s)
        # puts "From BCrypt: " + tPassword
        if (tPassword == p)
          # puts "Validated password"
          # check whether the user is already logged in or not
          if (!@@user_table_cache[u.to_sym])
            # puts "User already logged in or session has not expired"
            userHash = Hash.new
            userHash[:name] = u
            userHash[:privilege] = @@config[:users][u][:privilege]

            # add the user hash to the cache
            @@user_table_cache[u.to_sym] = userHash
          end

        end
          true
        end
      end
    end

All of this works on webrick with Sinatra. I have tried the following on Thin (based on Can I enable SSL in Sinatra with Thin?)

class MyApp < Sinatra::Base
  # ...
  get '/' do
    puts "got request"
  end

end

MyApp.run! do |server|

  ssl_options = {
    :cert_chain_file => './certificate.pem',
    :private_key_file => './private_key.pem',
    :verify_peer => false
  }
  server.ssl = true
  server.ssl_options = ssl_options
end

However, I get the following error, when i try to access it from the browser.

C:\Software\Ruby Projects\Utils\sandbox\thintest>thistest
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Th
in
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
terminate called after throwing an instance of 'std::runtime_error'
  what():  Encryption not available on this event-machine

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

any thoughts would be greatly appreciated.

Community
  • 1
  • 1
SBG
  • 357
  • 4
  • 17
  • Did you build the event-machine without ssl header files? – Sir l33tname Jan 14 '15 at 07:50
  • Why get an application server to handle SSL? Put a reverse proxy in front of it and it will handle everything quicker and easier. Just my 2 pence. – ian Jan 15 '15 at 06:49
  • Thanks, after many hours of following various leads on forums, I will think about shifting away from windows environment to a virtualbox Ubuntu - windows has been no end of problems getting certain gems running (e.g., GSL and this one). In which case, i will also look at using a reverse proxy. – SBG Jan 18 '15 at 13:38

0 Answers0