Rails: Unable to use new `config/secrets.yml` - ruby-on-rails

Anybody know how to set ENV variables in production.rb using the new secrets.yml?
I'm getting key: wrong number of arguments (0 for 1) for the below:
production.rb:
# DOES NOT WORK
ENV["SOME_STUFF_KEY"] = Rails.application.secrets.some_stuff.key
ENV["SOME_STUFF_SECRET_KEY"] = Rails.application.secrets.some_stuff.secret_key
# NOTE THAT THIS WORKS:
# ENV["SOME_STUFF_KEY"] = "abcdefg";
# ENV["SOME_STUFF_SECRET_KEY"] = "123456789123456789";
secrets.yml:
production:
some_stuff:
key: abcdefg
secret_key: 123456789123456789

I haven't found any information about nesting like you did with some_stuff. The release notes and all other blog posts just use flat keys as an example:
development:
secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
some_api_key: SOMEKEY
Rails.application.secrets.some_api_key returns SOMEKEY in the development environment.
That's why someone created the dot_secrets gem ("Enable dot syntax on nested Rails.application.secrets")

To get nested values like yours you need to use this syntax.
Rails.application.secrets.some_stuff[:secret_key]

Related

Rails secrets.yml with values containing newlines

Let's assume I have a config/secrets.yml file that contains:
development:
app_name: MyApp
secret: <%= ENV['SECRET_VALUE'] %>
If I set SECRET_VALUE with a newline, it will break. E.g:
export SECRET_VALUE=$(printf "hello\nworld")
Then when I start my rail application I get this error:
/usr/local/lib/ruby/3.0.0/psych.rb:457:in 'parse': (<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1 (Psych::SyntaxError)
While debugging the issue, I realized that the literal value of ENV['SECRET_VALUE'] is added to the yaml file before parsing it. That means if I wanted to achieve what I'm trying to do, I would have to do something like:
export SECRET_VALUE=$(printf "|\n hello\n world")
This works but this is very ugly and I can't be the only one who think this behaviour is absurd?? Is there a better way to do this?
EDIT:
I tried adding quotes around the value:
development:
app_name: MyApp
secret: "<%= ENV['SECRET_VALUE'] %>"
It "works" but the newline gets removed from the string...
root#4e4431bae32e:/app# rails console
Loading development environment (Rails 7.0.3)
irb(main):001:0> puts ENV['SECRET_VALUE']
hello
world
=> nil
irb(main):002:0> puts Rails.application.secrets[:secret]
hello world
=> nil
It's important that the newline remains for my use case.
You can use an escaped quoted string. YAML's string format is similar enough to a superset of Ruby's for String#dump to work:
development:
app_name: MyApp
secret: <%= ENV['SECRET_VALUE'].dump %>

Recaptcha::Recaptcha Error - No site key specified

I am working on a MVC project in Ruby on Rails. I have the actual code and i have to understand it in next few days and then try to upgrade it.
I have a home-page with a textbox which ask you the e-mail for resistration. The problem is that when i push the submit button it gives me
Recaptcha::Recaptcha Error - No site key specified
I know that there are many posts about this problem but i don't manage to resolve it.
Here is my .env file
RECAPTCHA_SITE_KEY=6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy
RECAPTCHA_SECRET_KEY=6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx
OMNIAUTH_PROVIDER_KEY=false
OMNIAUTH_PROVIDER_SECRET=false
ADMIN_NAME=false
SECRET_KEY_BASE=false
GA_PROVIDER_KEY=false
TWILIO_SID=false
TWILIO_TOKEN=false
TWILIO_NUMBER=false
Please help me with this.
You need to configure recaptcha to pull the keys from your secrets.yml by adding an initializer file, config/initializers/recaptcha.rb:
# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.site_key = Rails.application.secrets[:RECAPTCHA_SITE_KEY]
config.secret_key = Rails.application.secrets[:RECAPTCHA_PRIVATE_KEY]
# Uncomment the following line if you are using a proxy server:
# config.proxy = 'http://myproxy.com.au:8080'
end
End in secrets.yml RECAPTCHA_SITE_KEY and RECAPTCHA_PRIVATE_KEY make sure that it is written in capital letters

Ruby on rails - pass params from db to environment config file

im using a gem for a sms service, and i have to put the params in the specific environment config file.
All is ok with static params, but if i call this params from a db, starting the webrick i have an error:
(ActiveRecord::ConnectionNotEstablished)
this is my original code in development.rb
Skuby.setup do |config|
config.method = 'send_sms_classic' #default
config.username = 'myusername'
config.password = 'mypassword'
config.password = Setting.where(campo: 'skebby_password').valore
config.sender_string = 'company' #optional
config.sender_number = '39123456790' #optional
config.charset = 'UTF-8' #skebby default is ISO-8859-1
end
changing something, like:
config.password = Setting.where(campo: 'skebby_password').valore
it goes down!
How can i fix?
Do you think it's ok to do this in your config/environment. I would think that such would be better suited for your config/initializers, within your initializer, your db connection is already setup.
BTW, if you're using different parameters for your environments, I think you should rather use a configuration file, which would set your params per environment and you could then use that.

Rails reading from settings.yml

Hi is it possible to read from an controller the settings yml? Im using rails 4.2.0
I want to store some global information in the settings.yml to display those in the view
at the moment I try this way:
settingsfile = File.read(Rails.root + "./config/settings.yml")
settingsfile = YAML.load(settingsfile)
settingsfile["default"]
works but when I want to go deeper
settingsfile["default"]["info"]
its empty
Yml File
default:
supported_languages:
de: Deutsch
fr: Francais
en: English
it: Italiano
sv: Svenska
pt: Português
nl: Nederlands
es: Español
production:
info:
version: 2.0.0
datum: 11.05.2015
without using gem..you can use something like ..
your dev/config/email.yml
development:
:address: smtp.service.com
:port: 25
:user_name: mike#gmail.com
:password: mikepassword
production:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: mike#gmail.com
:password: mikepassword
:enable_starttls_auto: true
you can load this yml file simply like....
email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
also you may use - Rails::Application.config_for(:email) as shown in the documentation for Rails 4+ onwards.
Take a look at the gem rails_config.
You could try a solution such as BetterSettings or settingslogic which provide a simple way to load YML files into a settings object.
Both libraries allow to easily access settings using method calls:
class Settings < BetterSettings
source Rails.root.join('config/settings.yml'), namespace: :default
end
which would allow you to access settings like:
Settings.info.version # '2.0.0'
Settings.supported_languages.es # 'Español'
In this example, we are using :default as the namespace, but you could also use Rails.env to scope the settings to a specific environment.
You might try:
#settingsvariable = Settings.default.supported_languages.de
This approach worked in my app.

Rails asset pipeline Rails.application.assets.instance_variable_get('#environment') returns nil

I am trying to compile javascript dynamically and then adding it to the sprockets store so it is available. Everywhere I researched suggested the below code to register the javascript:
env = Rails.application.assets.is_a?(Sprockets::Index) ? Rails.application.assets.instance_variable_get('#environment') : Rails.application
Rails.application.config.assets.digests[file_name] = env[file_name].digest_path
in production, Rails.application.assets.instance_variable_get('#environment') always returns nil, is there something I am doing wrong? or should I be adding something else?
Rails.application.assets itself is an instance of Sprockets::Environment
#environment' is a variable of assets_manifest, that belongs to Rails.application, like this:
Rails.application.instance_variable_get('#assets_manifest').instance_variable_get('#environment')
I got similar problems with RAILS 3.2.15,
but it was Rails.application.assets returns nil
quiet_assets.rb:4:in': undefined method logger=' for nil:NilClass (NoMethodError)
the issued line was
Rails.application.assets.logger = Logger.new('logger.log')
I back to Rails console, and find Rails.application.assets just returned nil.
I fix this issue by this step:
include two gem in your Gemfile in case you don't have it.
gem 'sprockets'
gem 'sprockets-rails'
find the file cause the issue , and initial your assets object.
you can also put that in application.rb , in my case I put it in config/initializers/quiet_assets.rb , before I refer to logger.
add this line:
Rails.application.assets = Sprockets::Environment.new
before this issued line:
Rails.application.assets.logger = Logger.new('logger.log')
in application.rb , remember have activate assets pipeline .
config.assets.enabled = true
for production you may need to set config.assets.compile = true
hope this helps somehow
Build it yourself (which is needed for new versions)
env = Sprockets::Railtie.build_environment(Rails.application)

Resources