Enabling SSL for a thin server and sinatra - ruby

I'm trying to enable SSL for my thin server web app so that it can work over HTTPS.
I have done the following:-
launching of thin web server
MyApp.run! :host => '127.0.0.1', :port => 9090, :sslenable => true, :sslverifyclient => OpenSSL::SSL::VERIFY_NONE,
:sslcertificate => '.ssl/server_key.pem', :sslprivatekey => '.ssl/key.pem'
I generated a self signed certificate and private key using the openssl module in Ruby, created a directory called .ssl and stored them there as pem files.
The web framework I'm using for my web app is Sinatra. I'm also using the rack-ssl gem in the following way..
myapp.rb
require 'rack/ssl'
class MyApp < Sinatra ::Base
use Rack::SSL
use Rack::Session::Cookie,
:key => '_rack_session',
:path => '/',
:expire_after => 2592000,
:secret => ''
...
end
When I go to http://localhost:9090, I would expect to see my app displayed as normal but with a padlock and a cross through it as any http request is being redirected to https and I see the error "webpage is not available". However when I remove ssl-rack ruby gem and restart my app and go to https://localhost:9090,i get an ssl connection error with the following details:
Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have.
Error code: ERR_SSL_PROTOCOL_ERROR
Can anyone please advise me on how best to configure a thin server to enable SSL?

I'm running Sinatra and Thin on Heroku with SSL using the Rack::SslEnforcer, doing this:
if production?
require 'rack/ssl-enforcer'
use Rack::SslEnforcer
end
This should be before you enable :sessions in your file. So, Rack::SslEnforcer needs to be placed above the session part when you configure your app.
Somewhat unrelated, but perhaps still relevant, you might consider adding:
require 'encrypted_cookie'
cookie_config = {
:key => 'usr',
:path => "/",
:expire_after => 86400, # one day in seconds
:secret => ENV["COOKIE_KEY"],
:httponly => true
}
cookie_config.merge!( :secure => true ) if production?
use Rack::Session::EncryptedCookie, cookie_config
You also need to set the COOKIE_KEY in your environment to something secret and long-ish.

Related

Tiny_tds: Connect: Server name not found in the configuration files

require "rubygems"
require "tiny_tds"
client = TinyTds::Client.new(:username => 'sa', :password => '', :host => 'RICHARD_PC\SQLEXPRESS')
result = client.execute("SELECT * FROM [Contacts]")
result.each do |row|
//Do something
end
I keep getting the same error: "Connect: Server name not found in the configuration files". All I need to do is to be at least be able to connect with Sql Server. So if the host is not the sqlexpress instance installed on my machine, what is it then? In the Github website it said this host => 'mydb.host.net' (:host - Used if :dataserver blank. Can be an host name or IP.)
Thanks for helping.
Either use
:dataserver => 'RICHARD_PC\SQLEXPRESS'
Or use
:host => 'RICHARD_PC', :port => 1433
For those running rails on vagrant, I got this error when my vagrant network connections stopped working (mostly just the DNS), so a reboot fixed the issue.

Getting Pony or Mail gems to work with Sinatra / Heroku

I'm a Ruby newbie so apologies if this question is simple. And many thanks in advance for your help.
We have a Sinatra application that's been deployed onto Heroku. We're trying to add a page that sends a simple email. I've added the SendGrid add-on to the Heroku app. Now, I'm just trying to add the Ruby code that creates and sends the message using the SendGrid SMTP server information.
The problem I'm having is that even though I installed the Mail gem on the Heroku app (by adding to the Gemfile), I get an error when the Heroku app is launched complaining that 'treetop/runtime' is not installed:
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `require': no such file to load -- treetop/runtime (LoadError)
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `rescue in block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:69:in `block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `each'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `<module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:2:in `<top (required)>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /usr/ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'
Similarly, when I try to install the Pony gem, it complains that it can't load Mail. Oddly, this is all works on my local system, so I think it's a problem with Heroku. I just can't get Heroku to fully load everything it needs from either Pony or Mail to launch successfully. (Note: I can't even get Heroku to launch, so I can't even test the actual sending of the email code.)
Any specific help/insight would be greatly appreciated. Has anyone encountered this with Heroku? Are there other gems that might work for this simple purpose?
Thanks!
P.S. The bundle successfully installed mail (2.4.4) and pony (1.4).
It would be helpful to see code snippets for your primary routing ruby script. But for my sinatra app, I configured Mail configs as.
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.sendgrid.net',
:port => 587,
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => 'plain',
:enable_starttls_auto => true
}
end
And in my sending the contact email
mail = Mail.deliver do
to <youremail>
from <someone>
subject 'Feedback for my Sintra app'
text_part do
body <your feedback>
end
end
Hope this helps.
In case you want some settings for Pony instead of Mail (they're very similar):
configure :production do # I have a different hash for development settings.
email_options, {
:from => "noreply#midas.heroku.com",
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
},
}
end
Pony.options = settings.email_options
Make sure you've set the sendgrid username and password via the config settings e.g.
heroku config:add SENDGRID_USERNAME=GIVEN_USERNAME SENDGRID_PASSWORD=GIVEN_PASSWORD
To send is very straightforward, just call Pony.mail and pass it a hash with 'to' and 'body' etc. (I'm sure you know this but I'm just trying to be thorough!)
I also use v1.4 but haven't had the same problem, but if it's saying it can't load Mail when you've switched to Pony I'd check for a rogue require statement.

Sending mails automatically through action mailer Rails 3.1

I have to send weekly emails to all the user about the latest things happening. I am using ActionMailer to accomplish other mailing task however I have no clue how to automate the weekly emails.
Update
I found whenever gem which could be used to schedule cron jobs. I guess this could be used to send weekly emails which I intend to. Still looking how to make it work with ActionMailer will update once I find the solution
Update 2
This is what I have done so far using whenever gem:-
in schedule.rb
every 1.minute do
runner "User.weekly_update", :environment => 'development'
end
in users_mailer.rb
def weekly_mail(email)
mail(:to => email, :subject => "Weekly email from footyaddicts")
end
in users.rb
def self.weekly_update
#user = User.all
#user.each do |u|
UsersMailer.weekly_mail(u.email).deliver
end
end
If i try to run User.weekly_update from the console I am able to get the mails. I am testing in development mode and using rvm. I checked my crontab file and it has got the right stuff.
However I am not getting any mails automatically from the app. Any clue what might be wrong?
Thanks,
OK so it turns out to be a path issue with whenever gem, and the problem was created when I installed another version of ruby.
In my machine the new ruby version is installed in /usr/local/bin/ruby. In my rails app I had to go to the file script/rails and replace #!/usr/bin/env ruby with #!/usr/local/bin/ruby.
I found this out by visiting cron.log file which showed this error message :- /usr/bin/env: ruby: No such file or directory
I made a cron.log file to log the cron error this is what I did in my schedule.rb code written in the question :-
every 2.minutes do
runner "User.weekly_update", :environment => 'development', :output => 'log/cron.log'
end
I am getting periodic mails now.
It seems like you haven't configured ActionMailer settings.
First check out the logs from console, whether the mailing process is working(paste your logs).
If yes then do following steps.
add this in your gemfile.
gem 'tlsmail'
run
bundle install
write these configuration setting in your config/environments/development.rb file
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:enable_starttls_auto => true,
:authentication => :login,
:user_name => "<address>#gmail.com",
:password => "<password>"
}
config.action_mailer.raise_delivery_errors = true
add your working password/email against user_name and password.
Don't forget to restart server.

Ruby NET::SCP and custom ports

I'm trying to define a custom port for the ssh connection using Net::SCP, but without luck so far.
Here's an example how I'm trying to download a remote file from a server with a custom ssh port:
require "rubygems"
require 'net/scp'
Net::SCP.download!("www.server.com", "user", "/opt/platform/upload/projects/file.txt", "/tmp/bb.pdf",{:password => "mypassword",:port => 22202})
The error message I'm getting is:
Errno::ECONNREFUSED: Connection refused - connect(2)
There are no entries in the server logs regarding the ssh connection, so I assume that Net::SCP isn't using my custom port.
Any tips for me ?
Regards, Alex
Well, I've found the solution myself.
require "rubygems"
require "net/scp"
Net::SSH.start("www.myserver.com", "theuser", {:password => "whateverpwd",:port => 22212}) do |ssh|
ssh.scp.download! "/opt/platform/upload/projects/my.pdf", "/tmp/bb.pdf"
end
I also keep SSH on a non-standard port and use SCP like so:
Net::SCP.upload!( "foo.net", "user", the_file, the_file, :ssh => { :keys => #keys, :port => the_port } )
Works like a champ. I also use key-based authentication, hence the keys parameter getting passed along with the port.

Cannot load 'paperclip/storage/ftp' when using paperclipftp in Rails 3

I've just installed paperclip 2.3.3 and paperclipftp 0.1.0.
Paperclip was working fine, the attachments were saving and everything was great.
Enter paperclipftp.
I've included both gems in my Gemfile, installed it with bundle and made sure all dependencies were satisfied. I've also double checked that all my ftp info is correct and the server is working fine.
When I try to attach a file using ftp:
has_attached_file :photo,
:styles => {
:small => "204x159#",
:original => "460X370#"
},
:storage => :ftp,
:path => "/:attachment/:attachment/:id/:style/:filename",
:url => "http://kickassserver.com/_myfolder/:attachment/:attachment/:id/:style/:filename"
I get the following error:
Paperclip::StorageMethodNotFound in SetupsController#create
Cannot load 'paperclip/storage/ftp'
I'm thinking that paperclipftp isn't actually being loaded by my app. Is there a way I can check to see that it's actually being loaded, or has anyone else experienced this?
Thanks,
Matt
I have ruby 1.9.2p180 and the problem is that the timeout class being loaded.
Only add,
require 'timeout'
to the application.rb and this will fix your error.

Resources