I am using Paperclip in my Rails 5.1 app and have the standard config in my development.rb and production.rb:
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
I'm storing the ENV variables in application.yml via Figaro gem.
This is fine, but I'm now trying to upload my app to a new AWS Elastic Beanstalk app and I'm having the build fail. Having spent the last 3 or so hours looking through all the logs and trying various changes, I've narrowed the failure down to the fact I think Beanstalk is reading the Paperclip ENVs and is getting confused.
From the logs:
rake aborted!
<<<
/var/app/ondeck/config/environments/production.rb:97:in `fetch'
/var/app/ondeck/config/environments/production.rb:97:in `block in <top (required)>'
/var/app/ondeck/config/environments/production.rb:1:in `<top (required)>'
/var/app/ondeck/config/environment.rb:5:in `<top (required)>'
/opt/rubies/ruby-2.4.3/bin/bundle:23:in `load'
/opt/rubies/ruby-2.4.3/bin/bundle:23:in `<main>'
Tasks: TOP => environment
and config/environments/production.rb:97 equates the below value from my Paperclip config:
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID')
I can't seem to find any items online that would help resolve this.
Any ideas?
Could I use the same IAM user for both so the ENV values match?
Add the paperclip S3 environment variables to your Elastic Beanstalk Environment
How?
Head over to your specific Environment
Select Configuration
In Configuration overview select Software tile
At the very bottom you'll see a form section Environment properties
Add key and value data for your variables
Save and wait for your environment to update.
Read more: Elastic Beanstalk Docs
Related
I'm trying to use the Whenever gem in my Rails app to schedule some rake tasks.
I'm hosting the app on Fly.io, and the rakes are aborting because they can't connect to the Postgres database in the production environment.
I wondered if the credentials stored in the secrets were somehow wrong; to troubleshoot this, I deleted the database and set up a new one, and set the secrets (username, URL, password) which are meant to be accessible in the ENV['DATABASE_URL'] environment variable.
I understand that the ENV['DATABASE_URL'] takes precedence over the database.yml file when it's present. I've tried all sorts of configurations, currently I have this in my schedule.rb file:
`env :DATABASE_URL, ENV['DATABASE_URL']
set :environment, :production`
And this in my database.yml file:
production: <<: *default
But I still get the following errors in my cron.log file:
rake aborted! ActiveRecord::ConnectionNotEstablished: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "thomaskitson" does not exist /Users/thomaskitson/code/tkitson/newsguessr/rails-authentication/lib/tasks/newspaper.rake:17:in block (2 levels) in '
/Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in load' /Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in '
Caused by:
PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "thomaskitson" does not exist
/Users/thomaskitson/code/tkitson/newsguessr/rails-authentication/lib/tasks/newspaper.rake:17:in block (2 levels) in <main>' /Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in load'
/Users/thomaskitson/.rbenv/versions/3.1.2/bin/bundle:25:in <main>' Tasks: TOP => newspaper:new_images (See full trace by running task with --trace)
"thomaskitson" is not the name of the database, so I don't know why it's searching for one called that. There is an env variable called DATABASE_URL present in the fly secrets, so I believe that this is set up properly. Any help appreciated!
Thanks
I am under a lot of stress due to this feature that is simply not needed in my scenario. It causes a lot of hassle and errors. For example:
[mememe#app site]# rails db:migrate RAILS_ENV=production
rails aborted!
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit`
Then when trying to edit:
[mememe#app site]# EDITOR="mate --wait" bin/rails credentials:edit
Adding config/master.key to store the master encryption key: 7b3516f223e08c7eb04813154582be2b
Save this in a password manager your team can access.
If you lose the key, no one, including you, can access anything encrypted with it.
create config/master.key
Traceback (most recent call last):
38: from bin/rails:3:in `<main>'
37: from bin/rails:3:in `load'
36: from /app/site/bin/spring:15:in `<top (required)>'
...
1: from /usr/local/rvm/gems/ruby-2.5.5/gems/activesupport-5.2.3/lib/active_support/message_encryptor.rb:183:in `_decrypt'
/usr/local/rvm/gems/ruby-2.5.5/gems/activesupport-5.2.3/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt': ActiveSupport::MessageEncryptor::InvalidMessage (ActiveSupport::MessageEncryptor::InvalidMessage)
Then I tried to set it up in .env, and now I get:
[mememe#app site]# rails db:migrate RAILS_ENV=production
rails aborted!
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
/usr/local/rvm/gems/ruby-2.5.5/gems/activesupport-5.2.3/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt'
I also tried setting in production.rb:
config.require_master_key=false
I just want to migrate the db to production, I do not want all this :( can someone more experienced please tell me how to get rid of this "feature"?
secret_key_base is used for things like cookies signing and session encryption, if you're going to use these then you need it and need to keep it secret.
In rails 5 it can also be set via old-fashined config/secrets.yml (use rails secret to generate):
development: &defaults
secret_key_base: some_other_very_long_key_here
test:
<<: *defaults
production:
<<: *defaults
secret_key_base: <%= ENV["SECRET_KEY_BASE"] || 'some_very_long_key_here' %>
And previously you had to keep this file with production keys secret. With credentials - you only keep master key secret.
If you're sure that you do not need "secrets"/"credentials" - you can commit above file in your code and rails will not complain
I have migrated my Ruby on Rails application from Rails 5.1.2 to Rails 5.2.0 to use the encrypted secrets. Application is successfully deployed to Digital Ocean Ubuntu Server. But when I go in browser to access, it shows the following log.
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:184:in `_decrypt'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:157:in `decrypt_and_verify'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/messages/rotator.rb:21:in `decrypt_and_verify'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:79:in `decrypt'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:42:in `read'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:21:in `read'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:33:in `config'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:38:in `options'
/home/deploy/apps/GeekyCricket/shared/bundle/ruby/2.4.0/gems/activesupport-5.2.0/lib/active_support/core_ext/module/delegation.rb:271:in `method_missing'
(erb):12:in `<main>'
I have added encrypted secrets using rails credentials:edit, which creates config/credentials.yml.enc and master.key.
I also have added the master.key file on /app_name/shared/config/ on my ubuntu server, also placed an env variable RAILS_MASTER_KEY. But still getting this error, I don't know what I am missing here.
Solution A and B are different solutions. Just choose which is good for you.
a. Deploy from Continous Integration
1. Edit deploy.rb
set :default_env, {
"RAILS_ENV" => "production",
"RAILS_MASTER_KEY" => ENV["RAILS_MASTER_KEY"]
}
2. Add RAILS_MASTER_KEY as a variable
Travis CI
GitLab CI
b. Usage of master.key
1. Edit deploy.rb
append :linked_files, "config/master.key"
2. Upload master.key with :linked_files
Let's assume our application's root path is /home/deploy/awesome-project. All we need to do is upload the key file to /home/deploy/awesome-project/shared/config/master.key.
I faced the same problem, when deploying for the first time on my DigitalOcean Droplet, every time I ran RAILS_ENV=production cap production deploy:initial it failed complaining this error - ActiveSupport::MessageEncryptor::InvalidMessage
I tried below options which all failed -
removing master.key and credential.yml.enc file and then deploying again.
adding both files again and deploying.
Finally one solution worked, i just added master.key, removing credentials.yml.enc file, committed it and redeployed, and it worked without changing my deploy.rb file
I have finished Michael Hartl tutorial and I'm trying to deploy it with https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04. Unfortunately I'm stuck with RAILS_ENV=production rake db:migrate because it's generating following error:
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key
/home/me/simpleapp/config/initializers/carrier_wave.rb:3:in `block in <top (required)>'
/home/me/simpleapp/config/initializers/carrier_wave.rb:2:in `<top (required)>'
/home/me/simpleapp/config/environment.rb:5:in `<top (required)>
'
I have even copied those files 1:1 and still this error persists. Any idea how could I solve it? I really am tired with trying to fix it with different solutions from web.
Thanks
edit: I am adding my carrier_wave file:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY']
}
config.fog_directory = ENV['S3_BUCKET']
end
end
The root issue is that Carrier Wave is expecting your AWS environment variables to be populated but they are not set in your production environment.
I would recommend you look at something like the dotenv gem which can find here. Environment variable housekeeping in multiple environments can be a real pain so it helps to have a tool to facilitate. Dotenv (and others like it) provides more of a turn key approach to this.
Note that the recently introduced secrets.yml for Rails secret management is a nice step in the right direction, but still requires a bit of code/know how to utilize.
In any event, be very careful about how you manage any files with secrets. At a minimum, the file should be in your .gitignore so that you aren't potentially broadcasting your secrets.
I clone rails app (our company project) and then install everything that related to mongo db and mongoid gem. Then I run the rake db:setup and then rails s. Its working fine, I can access all the sites and do everything. But I got problem in the rails console.
Whenever I do this:
rails c
then
User.first
I will get this error
Loading development environment (Rails 4.1.1) irb(main):001:0>
User.first Mongoid::Errors::NoSessionConfig: Problem: No
configuration could be found for a session named 'default'. Summary:
When attempting to create the new session, Mongoid could not find a
session configuration for the name: 'default'. This is necessary in
order to know the host, port, and options needed to connect.
Resolution: Double check your mongoid.yml to make sure under the
sessions key that a configuration exists for 'default'. If you have
set the configuration programatically, ensure that 'default' exists in
the configuration hash. from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/factory.rb:27:in
create' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:65:in
with_name' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:105:in
mongo_session' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:121:in
collection' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/options.rb:161:in
method_missing' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual/mongo.rb:263:in
initialize' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
new' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
create_context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:35:in
context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:20:in
first' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/findable.rb:122:in
first' from (irb):1 from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in
console' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in
run_command!' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in
' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inblock in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in load_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inrequire' from
/Users/rizalmuthi/Documents/Sites/WORK/tapway/bin/rails:8:in <top
(required)>' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:inload' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in block in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:inload_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from -e:1:in'irb(main):002:0>
And FYI, this is the mongoid.yml that I generated by run
rails g mongoid:config
mongoid.yml
development:
sessions:
default:
database: mongo_demo_development
hosts:
- localhost:27017
options:
options:
test:
sessions:
default:
database: mongo_demo_test
hosts:
- localhost:27017
options:
consistency: :strong
max_retries: 1
retry_interval: 0
I have been looking all over google and some blogs, could not figure it out how to fix this.
Besides that, we dont use the Rspec tho.
change your config/application.rb file:
replace it Bundler.require(*Rails.groups)
to it Bundler.require(*Rails.groups(assets: %w(development test)))
In the latest mongoid -5.0.0 version you can simply solve by placing the below code in application.rb
config.generators do |g|
g.orm :mongoid
end
In config/application.rb change
Bundler.require(*Rails.groups)
to:
Bundler.require(:default, Rails.env)
Rails is trying to eager load the group of gems by environment and the stock config/mongoid.yml file you get when you run rails g mongoid:config only gives you a test and development namespace. Even though you're only running development, it's looking at all the groups. If you add the production namespace to the yml file it will load but my answer above works without you needing to do that.
Just make sure that on application initialization you do the following (in Rails console)
Mongoid.load!("path/to/your/mongoid.yml")
For more details visit http://mongoid.org/en/mongoid/docs/installation.html
With MongoId version 5.x, You will change config in Rails application follow:
File: application.rb
# Add line.
config.generators do |g|
g.orm :mongoid
end
I was set it and it working in rails console.
See more at:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-installation/#rails-applications
may be The problem is in the gem file, you did't mention the version of mongoid gem.
in gem file change
gem "mongoid"
to
gem "mongoid" , '~> 4.0.2'
and run bundle install
restart server and console.