Rails 4.1 pushing secrets to heroku - ruby-on-rails

Rails 4.1.0.beta1 and Devise.
I'm trying to remove all of my keys from version control and I've upgraded to Rails 4.1 to give this new secrets.yml a shot
Trying to push Devise's config.secret_key to heroku but it's failing after assets:precompile
Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = 'EXAMPLE_KEY_HERE'
Please ensure you restarted your application after installing Devise or setting the key.
Here are my changes, the old code I'll leave in comments. (it works)
devise.rb
# config.secret_key = 'THIS_IS_A_FAKE_KEY' #<---this_is_commented_out
config.secret_key = Rails.application.secrets.devise_secret_key
secrets.yml
production:
devise_secret_key: 'THIS_IS_A_FAKE_KEY'
then ran heroku labs:enable user-env-compile -a myapp (not sure if that's necessary)
and then when I push to my forked heroku envionment git push forked master I get the above error.
I also noticed some discussion on this in this Devise repo so I figured I'd update my gem alongside the source repo, no difference. Here's part of that discussion (here).

You've likely got secrets.yml added to your .gitignore. Which makes sense, since you put secret keys in it -- but since Heroku deployment uses git, it never sees your secrets.yml.
One solution is to use the heroku_secrets gem - See https://stackoverflow.com/a/22458102/2831572 .
Another solution is to add secrets.yml to git (i.e. remove it from .gitignore) after replacing all sensitive keys with references to environment variables.
So:
production:
devise_secret_key: <%= ENV['DEVISE_KEY'] %>
then run heroku config:set DEVISE_KEY='7658699e0f765e8whatever'

Related

Running a rails server in production locally (InvalidMessage error)

I'm running Ruby 2.5.1 and Rails 5.2.0. I ran rails s -e production, and it gives this error:
/home/roy/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:206:in `rescue in_decrypt': ActiveSupport::MessageEncryptor::InvalidMessage
(ActiveSupport::MessageEncryptor::InvalidMessage)
How do I do this properly?
EDIT:
The same error appears whenever I try to edit the credentials file using
EDITOR="nano --wait" bin/rails credentials:edit
Also I realized that I didn't create a production database yet so I tried that using
RAILS_ENV=production bundle exec rails db:reset
(I know db:reset is a bit redundant but it should work trying to create, migrate and seed a server)
Sadly I get the same kind of error (InvalidMessage error)
Unsupported rails environment for compass
rake aborted!
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
/home/roy/apps/myappname/config/environment.rb:5:in `<main>'
/home/roy/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/home/roy/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
Caused by:
OpenSSL::Cipher::CipherError:
/home/roy/apps/myappname/config/environment.rb:5:in `<main>'
/home/roy/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/home/roy/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:create => db:load_config => environment
Okay I got it working finally.
I simply deleted my master.key and credentials.yml.enc files and then ran
bin/rails credentials:edit
Which created new files. After that everything worked fine.
I don't really understand why it works though. Can anyone give a good explanation for this?
It appears your solution of removing the master.key and credentials.yml.enc indicates you are running Rails 5.2. This setup changed from a similar encrypted secrets.yml.enc file used in Rails 5.1.
The goal is to allow committing secret keys (AWS, Rails' secrect_key_base) to a project's code repository. These would typically be set with ENV variables. Now collaborators need only share the master.key that was generated to decrypt and modify or read the contents of credentials.yml.enc.
When you removed both the master.key and credentials.yml.enc files, rails generated a new pair, now you were able to decrypt credentials.yml.enc and this file was initialized with a new Rails secret_key_base value needed to avoid the ActiveSupport::MessageEncryptor::InvalidMessage. If you track down the source of that message, it's likely referencing the Rails credentials secret key base: Rails.application.credentials.secret_key_base.
These are nice write ups on the topic:
https://medium.com/cedarcode/rails-5-2-credentials-9b3324851336
https://www.engineyard.com/blog/rails-encrypted-credentials-on-rails-5.2
For Rails 6, I had a multi-environment credentials setup.
One for development, staging, and production.
The master.key works for the main credentials.yml file
The other environments have there own key, so for staging we used the production.key in place of the RAILS_MASTER_KEY config envs on heroku and that fixed it for me.
I had this similar issue when working with a Rails 5 application in production, royketelaar's answer and gib's answer
Just to add a few things:
After deleting the credentials.yml.enc and master.key files,
And running the command below to generate a new secret_key_base, credentials.yml.enc and master.key files (my editor is VS Code and not Nano):
EDITOR="code --wait" bin/rails credentials:edit
Ensure that uncomment the following configuration in your config/environments/production.rb file:
config.require_master_key = true
For your production environment, since the master.key file containing the master key which is used for decrypting the credentials.yml.enc is not recommended to be committed to version system control, save the master key in a RAILS_MASTER_KEY environment variable using the figaro gem.
That's all.
I hope this helps
You need to ask for the master key to you project leader / team leader / coworkers.
With that long key like 63y4gh47373h3733jj474 you copy it and paste it the master.key file under config folder.
That solve the issue.

Omniauth Facebook Devise fails production Heroku but works in development

I developed this app on Nitrous.io (using a heroku postgresql DB). Omniauth works correctly when tested on that platform. When I try on production Heroku, I get a "The parameter app_id is required" error from facebook.
inside devise.rb
config.omniauth :facebook, ENV["FB_APP_ID"], ENV["FB_APP_SECRET"]
My facebook developer site url's and callbacks are up to date. ENV variables are by Figaro, and again, it works fine when run through Nitrous.io.
https://github.com/laserlemon/figaro
Did you try the following???
Heroku
Heroku already makes setting application configuration easy:
$ heroku config:set google_analytics_key=UA-35722661-5
Using the figaro command, you can set values from your configuration file all at once:
$ figaro heroku:set -e production
For more information:
$ figaro help heroku:set
You can also add/delete config variables by going to heroku settings then click on the reveal config variables button.

How to set up RefineryCMS with Heroku and Amazon AWS assets

I've spent most of the last three days struggling with installing RefineryCMS on Heroku.
There are a lot of questions on SO and on various blogs, as well as documentation from Refinery and Heroku (and Rails) but none of the walkthroughs have helped 100%... Every page seems to be missing some vital piece of information.
I've tried to document all the necessary steps having gone through them three or four times, refining the procedure each time (working out what is and isn't necessary).
References included where they were obvious.
Run the refinery initialisation script, with Heroku option
refinerycms myapp --heroku
From http://refinerycms.com/guides/heroku
The output should give you a new heroku app and its name listed in the output:
"Creating Heroku app.. run heroku create --stack cedar from "."
Creating ... done, stack is cedar
http://[your heroku app].herokuapp.com/ | git#heroku.com:[your heroku app].git
Git remote heroku added"
Create bucket on Amazon AWS…
Should be self-explanatory.
Set connection info for Amazon in the Heroku environment
We need both sets of credentials.
AWS_* and FOG_* is for Heroku (and the rails precompile, I believe).
S3_* stuff is for Refinery to be able to upload images etc.
heroku config:add AWS_ACCESS_KEY_ID="<your key>" AWS_SECRET_ACCESS_KEY="<your secret>" FOG_DIRECTORY="<your bucket name>" FOG_PROVIDER="AWS" FOG_REGION="<your aws region>"
heroku config:add S3_BUCKET="<your bucket name>" S3_KEY="<your key>" S3_REGION="<your aws region>" S3_SECRET="<your secret>"
Add required gems to your Gemfile
gem 'globalize3', '0.3.0'
From refinerycms not working when adding page
gem 'unf'
(fixes some warnings)
gem 'rails_12factor'
From Why is the rails_12factor gem necessary on Heroku?
gem 'asset_sync'
From https://github.com/rumblelabs/asset_sync.
This gem seems the only way to get the assets pushed up to the cloud... Although perhaps you can make do without it; perhaps someone else can confirm.
ruby '2.0.0'
[ place this at the end of the Gemfile. (Needed to clear Heroku warnings) ]
Add asset_sync asset host path in config/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
From https://github.com/rumblelabs/asset_sync
Set the site name in config/initializers/refinery/core.rb
config.site_name = <your site name>
Set the s3_backend in the config/environments/production.rb
Refinery::Core.config.s3_backend = true
From https://github.com/refinery/refinerycms/issues/1879
Configure database details
Remove sqlite3 in config/database.yml and setting postgresql instead: this is optional but recommended by Heroku and others
For adapter:
sqlite3 => postgresql
For database name:
db/foo.sqlite3 => <sitename>_foo
Set user-env-precompile settings
heroku labs:enable user-env-compile -a myapp
From https://devcenter.heroku.com/articles/labs-user-env-compile
Run the Bundler
bundle install
Note: First of all, I had to run, as prompted:
1. rvm use 2.0.0 in order to match the version we're using in Gemfile
2. bundle update globalize3
From refinerycms not working when adding page
Create (local) production database
RAILS_ENV=production rake db:create
Set environment variables needed before asset precompile can work
(this is for *nix, do whatever you need to on your platform)
export FOG_DIRECTORY="<your bucket name>"
export FOG_PROVIDER="AWS"
export AWS_SECRET_ACCESS_KEY="<your secret>"
export AWS_ACCESS_KEY_ID="<your key>"
Precompile the assets (???)
NOTE: This MAY NOT be required... (I did do this step each time but cannot be sure whether it's required. The next steps suggest to me it's not necessary to manually precompile: we need to change the "initialize_on_precompile" to false, run a git push to heroku (i.e. without assets), then set the "initialize_on_precompile" back to true for future pushes. Not sure why we need to do this, and it may be an issue only with Rails 3.* (see: https://devcenter.heroku.com/articles/rails-asset-pipeline)
RAILS_ENV=production bundle exec rake assets:precompile
Set precompile false in config/application.rb
config.assets.initialize_on_precompile = false
From http://refinerycms.com/guides/heroku…
This setting is required the first time you git push to heroku, because otherwise the precompile step of git push heroku master always fails with:
Connecting to database specified by DATABASE_URL
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
NOTE: The reference is not clear on this (although setting intially to false then true is mentioned elsewhere).
Check in files to git and commit changes
Note: add the Gemfile.lock along with all the other changes.
Push to heroku
git push heroku master
Set precompile option back to true in config/application.rb
config.assets.initialize_on_precompile = true
From http://refinerycms.com/guides/heroku…
Add config/application.rb to git and commit (!!)
... if you don't, the next push will fail
Push to heroku (Demonstrates that this time it succeeds)
git push heroku master
Migrate and seed on the Heroku database
heroku run rake db:migrate
heroku run rake db:seed
From http://refinerycms.com/guides/heroku
Ready to go!
Hopefully from here you have access to your RefineryCMS page, with all the Refinery CSS and images displaying correctly (both on the admin screens and when 'viewing website' but still logged in.
If you add an image using the Refinery menu you should subsequently be able to see that image added to your AWS bucket. I don't have thumbnails working yet.

Heroku upload-Precompiling assets failed

I need help. When trying to upload my app to heroku, I get this error, anyone know why? A few was wrong. thanks
Using rake (10.1.0)
...
Using tlsmail (0.0.1)
Using uglifier (2.1.2)
Your bundle is complete! It was installed into ./vendor/bundle
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/tlsmail-0.0.1/lib/net/smtp.rb:806: warning: already initialized constant SMTPSession
...
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:702: warning: already initialized constant APOPSession
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/Rakefile:7)
...
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize'
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
...
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)
!
! Precompiling assets failed.
From the Heroku docs:
This means that your app is attempting to connect to the database as part of rake assets:precompile. Because the config vars are not present in the environment, we use a placeholder DATABASE_URL to satisfy Rails.
To resolve this issue, ensure that the following line appears in your config/application.rb:
# config/application.rb
config.assets.initialize_on_precompile = false
Once added, commit your change and redeploy to Heroku – your assets should compile without your app attempting to connect to the database, which should resolve the error you're witnessing.
UPDATE:
Line 46 of your stacktrace includes the following message: Devise.secret_key was not set.
According to the author of Devise, José Valim, this issue can be resolved in the following manner:
Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Alternatively, the following solution seems to have worked for a number of users:
I went to my routes.rb file and commented out the line devise_for :installs
Then I went back and reran rails generate devise:install. If that doesn't work, use the previous version of devise by editing your Gemfile's reference to Devise like this: gem 'devise', '3.0.3' and then follow the steps i mentioned above.
There a few things that solved this issue for me:
# config/application.rb
config.assets.initialize_on_precompile = false
Then, before I deployed, I compiled my assets locally and committed them:
RAILS_ENV=production bundle exec rake assets:precompile
Also, I installed this heroku add on, as was specified by the app I was launching (in my case, Spree commerce)
heroku labs:enable user-env-compile -a myapp
And of course, make sure your database.yml file is set to use adapter: postgresql.
Commit all of this, push to heroku, and hopefully it will launch. If you still cannot open your app, try looking at the Heroku logs: heroku logs -n 500
I still needed to migrate my database with heroku run rake db:migrate
when you are using github and you are pushing to heroku while you are in develop branch, dont do it, go to master branch and get the updates in the develop by git merge develop
after that,
rails precompile:assets
git add -A
git commit -m "Precompile assets"
git push heroku master
if you want to open the website that you deployed
heroku open
if nothing shows, migrate your database first by:
heroku run rails db:migrate
heroku open
I have failed Heroku proceompiling with same error message.
Carrierwave causes that because I have missed set up SECRET_KEY_BASE to Heroku setting.

gitignored file to Heroku

I am a rails developer and trying to get familiar with Heroku.
I don't add config/environment.rb to git as environment.rb might be different for each environment.
For example, ENV['RAILS_ENV'] = 'some_env' should be in environment.rb.
However, Heroku requires environment.rb to be in git so that I can push it to Heroku.
Is there a way to just copy environment.rb to Heroku without version-controlling it?
Thanks.
Sam
You always want to keep the environment.rb file checked in. You can set the environment using config variables
heroku config:add RAILS_ENV=some_env
So you store your config with each individual app. You can read any config variables from your application by using ENV[]

Resources