How do I see the ENV vars in a Rails app?
Asked Answered
D

3

32

I am taking over an old Rails app. No one has touched it in a year. The last developer left in April of 2015 and I have no way to contact him. I do have ssh access to the server, and I have access to the Github repo.

I don't know any of the usernames/passwords.

If I ssh to the server and I cat the database.yml file, I see stuff like:

  staging:
        adapter: mysql2
        encoding: utf8
        pool: 5
        socket: /var/lib/mysql/mysql.sock
        database: o_wawa_stage
        username: wawa_stage
        password: <%= ENV['STAGE_DATABASE_PASSWORD'] %>
        host: access.dmedia.com

If I run the "printenv" command then I don't see any of these vars. I assume they are only loaded by the Rails environment.

I guess I can edit the templates to spit out the values with a bunch of "put" statements, but I'm thinking there must be a more obvious way to do this, other than printing the data where the public could see it?

If I try to run "rails console" I get:

  Rails Error: Unable to access log file. Please ensure that /var/www/haha/production/releases/20150118213616/log/development.log exists and is writable (ie, make it writable for user and group: chmod 0664 /var/www/haha/production/releases/20150118213616/log/development.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.

I don't have sudo on this box, so I can not address the error.

Dachy answered 29/2, 2016 at 16:48 Comment(0)
P
41

Assuming the staging environment, as your example points to. You'll want to load the console by prepending the RAILS_ENV environment variable to the rails console command.

RAILS_ENV=staging rails console

That should get you in. Once you're in, you can just access the ENV variable directly.

2.2.2 (main):0 > ENV

And that will dump out the environment variables for you. Note, your prompt may look different. If you want to access a specific value, such as the database password, you can:

2.2.2 (main):0 > ENV['STAGE_DATABASE_PASSWORD']
Petro answered 29/2, 2016 at 17:31 Comment(2)
Is there a way I can tell "rails console" where to find the log? The log is at "/var/www/wawa/production/shared/log" but rails console is looking for it in "/var/www/wawa/production/log" and it throws an error because it can't find it.Dachy
Have a look inside of config/application.rb and config/enivornments/production.rb for any lines that look like config.logger. It's usually in one of those two files where logging configuration is overwritten. guides.rubyonrails.org/configuring.html might be helpful too.Petro
B
6

Within your app directory, simply launch the Rails Console:

rails c

Then at the prompt:

ENV

This will list all loaded environmental variables for whichever environment you last exported.

Sorry, after posting this, I realized that the author had already tried to use rails console with errors...but I am fairly sure this should always work. You can't ask for printenv or env within the console, you must use all caps "ENV"

Bohi answered 10/10, 2018 at 13:11 Comment(0)
A
0

yourapp/config/env.yml or application.yml etc...

Look for code that looks like

AWS_KEY_ID: blahblah23rkjewfojerflbah
AWS_SECRET_KEY_ID: blahblah2394082fkwejfoblah
Aldaaldan answered 29/2, 2016 at 16:54 Comment(7)
That would only be true if we were using the Figaro gem, yes?Dachy
This returns nothing: find . -name application.ymlDachy
This returns nothing: find . -name env.ymlDachy
"find . -name *.yml" returns over a 100 files, but nothing with "application" or "env" in the name.Dachy
Yes figaro or envyous, although if the prev dev was using ENV variables they must be stored somewhere, unless the code was pushed to github in which you pulled from when that .yml file was inside of the .gitignoreAldaaldan
If it weren't over 100 files I would say look through them, but try checking specifically inside /config for *.ymlAldaaldan
If I run "grep -iR DATABASE_PASSWORD *" I find nothing that sets the ENV. Whatever sets the ENV variables, it is outside of the project. I would guess it is perhaps set by the Capistrano scripts that initiate the project.Dachy

© 2022 - 2024 — McMap. All rights reserved.