Topic: Environmental vars passed to Rails initializers aren't working

I have a couple Environmental variables that I need to pass to my Rails app.
RESQUE_USER, RESQUE_PASS, EMAIL_USER, EMAIL_PASS, PAPERCLIP_HASH_SECRET, APP_SECRET_TOKEN

The RESQUE_* variables are for accessing the Resque front-end.
The EMAIL_* variables are for sending the emails
The others are secret hashes that are for Paperclip and the apps secret_token

This is my Resque configuration with the env vars

Resque::Server.use(Rack::Auth::Basic) do |user, pass|
  user == ENV['RESQUE_USER']
  pass == ENV['RESQUE_PASS']
end

This works, if I type in the user and pass it lets me access Resque.

Now this is my email configuration

ActionMailer::Base.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: 'gmail.com',
  authentication: :login,
  enable_starttls_auto: true,
  user_name: ENV['EMAIL_USER'],
  password: ENV['EMAIL_PASS']
}

Now this part isn't working, the user/pass aren't being sent in or something because the emails aren't sending..(But when I hardcode the user/pass in the emails send)

And I have no clue if the hash variables are sending in. So how would I fix this?

Re: Environmental vars passed to Rails initializers aren't working

Is this in production it's failing, or development?

also look what you posted???

Resque::Server.use(Rack::Auth::Basic) do |user, pass|
  user == ENV['RESQUE_USER']
  pass == ENV['RESQUE_PASS']
end

Is that a typo?

== instead of =

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Environmental vars passed to Rails initializers aren't working

It's in development currently, but it will need to work in production as well.

And no the double equal isn't a typo the Rack::Auth::Basic is an alert that checks the inputted user/pass so the block takes user/pass and check's if its equal to an string

It looks weird doesn't it?

Last edited by larzconwell (2012-03-19 18:30:38)

Re: Environmental vars passed to Rails initializers aren't working

Oh and here is the script I have that sets the environmental variables

#! /bin/bash

export RESQUE_USER="username"
export RESQUE_PASS="password"
export EMAIL_USER="blah@blah.com"
export EMAIL_PASS="password"
export PAPERCLIP_HASH_SECRET=$(ruby ../key_generator.rb)
export APP_SECRET_TOKEN=$(ruby ../key_generator.rb)$(ruby ../key_generator.rb)
thin start

Re: Environmental vars passed to Rails initializers aren't working

Oh nevermind! It turns out the environmental variables are working. It was something dealing with Resque!

Re: Environmental vars passed to Rails initializers aren't working

In order to make environment variables available in Apache,  you have to add directives to the Apache configuration file. 

Webrick starts from the command line,  so I would of thought that it would have access to any environment variables to exported prior to running rails server.

Have you tried something like

config/initializers/setenvs.rb

EVN['EMAIL_USER'] = 'me@myco.com'

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Environmental vars passed to Rails initializers aren't working

I figured out what was going on. The environmental variables were working, just the Resque gem isn't using the env variables to authenticate it for some reason so i opened an issue on their Github.

Re: Environmental vars passed to Rails initializers aren't working

Thinking further, I have to ask,  who determines the values of those environment variables?  Some higher level authority?  I can't think of a reason to use environment variables like that unless someone higher up the food chain than you has dictated to you that must obtain your data from environment variables,  and they control their values,  and the way they've decided to communicate those values to you and your Rails app is via environment variables. 

If that is the case,  then you have to figure out how to 'fake' those environment variable settings in your development environment, and just TRUST that when you go into production, the higher level authority will have those environment variables set, (i.e via Apache Configuration that you don't control).

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Environmental vars passed to Rails initializers aren't working

Opps,  you answered while I was composing my followup post.   I'm still curious as to why environment variables are being used.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Environmental vars passed to Rails initializers aren't working

No I decided to use the env variables just because I thought that'd be the safest thing to do. Also because I'm hosting my app on Github I don't want the app's secret token compromised.

I'm glad you told me about the Apache config part though! That's helpful so when I get ready for production mode.

Re: Environmental vars passed to Rails initializers aren't working

Also I was debating on using Heroku for production and it's config vars seem to be similar to the normal Bash environmental variables so I thought i'd do it the same was throughout the app.

Re: Environmental vars passed to Rails initializers aren't working

Got it.

Yea,  there is also a little thing you can do in Apache (I assume that's how you'll deploy) if your UBER concerned about protecting those passwords.  I ran across it when I configured SSL on Apache.  My app was failing to boot because my server key file was password protected.  My choices were either remove the password protection, OR employe this neat little hack that would run an external script to obtain the password needed so I could load my server key file.   

Seeing as your only concern is not having your password on Github,  you probably don't need to worry,  but if you're really paranoid that someone could get ahold of your Apache config file,  this hack would add another layer of security,  keeping the password out of the Apache config file and placing it deeper inside your system,  making it harder to hack out!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Environmental vars passed to Rails initializers aren't working

Nah I'm not really to concerned with it, well at least at the moment haha I'm not exactly sure how I'm going to deploy yet.

Re: Environmental vars passed to Rails initializers aren't working

I'd highly recommend Phusion Passenger under Apache.  Using this combo,  you don't have to mess with SPECIAL Apache configs,  you just configure Apache to run the app like it were any other WEB app,  and Passenger interrogates your rails configuration and makes the necessary adjustments to Apache.  Really simple!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Environmental vars passed to Rails initializers aren't working

That does sound pretty easy, thanks!