Is it safe to alter application settings (config.*) on a Rails application that is already running?
How can I persist the changes, preferably to a database?
Update:
I want to allow the administrator to configure things like Action Mailer through an admin control panel, but I'm not sure if Rails will pick up on the changes.
Another approach instead of querying DB is to use Environment variables.
This is how you access environment variables:
ENV['name']
You may define those variables in .bash_rc for example.
Related
What is the best strategy in regards to checking in sensitive information into git? For example, database connection credentials, api keys, etc. For rails app, is it best to add environment files to .gitignore?
Thanks.
Your best bet is to use environment variables.
Check out these two links. The second link will show you how to keep your sensitive information secure by using environment variables to store sensitive data.
Rails Environment Variables
Environment Variables in Ruby on Rails
Nope, you don't need to ignore your env files - just remove all the secrets and use config variables ibstead... you can then refer to them using ENV['varname']
This gist shows one way of doing that using SECRET_KEY_BASE as an example (but you should do it with every sensitive key you have):
https://gist.github.com/cjolly/6265302
I have a rails app that will run on 3 web instances behind a load balancer.
I understand that the secret_key_base must be the same across the 3 instances.
Whats a good way to keep it synced without having to set the environment variable manualy on each instance?
Just initialize them during deploy of your Rails application (i.e. put it to .bash_profile or define env variable in command which run your Rails server, etc.) or during provisioning your servers use such tools like Puppet, Chef, Ansible, etc.
You have a lot of ways to make it automatically. Just choose one.
If it's only going to be 3 instances that's not a key than ever changes so you could set it once in /etc/environnement on 3 instances and forget it.
If you are planning on deploying more instances I would suggest you to try https://www.docker.com/ you will have an automatic deployment system that lets you set specifics
Not sure if you would actually do this. So please suggest other ways if you wish.
I am looking to create a config file for a rails engine that you can either create for your project or use the config file in the engine its self.
This config file would contain default names for things like roles, groups and permissions.
The idea is if you create this config file in your project that implement the engine, we will use those default names you have specified through out the seed file and through out things like the controller actions to assign to and check for these specific groups, roles and permissions.
An example is, in the check for is_admin? I look to see if a user belongs to an Admin role.
Well in your app you might have called that Super User. So thats where this config file would come in handy, it would allow you to create your own names for default groups and roles and so on with out worrying about breaking the engine or with out the engine having to hard code things.
You could still use is_admin? and instead of checking for Admin role it would now know to check for Super User.
So my question is:
How do I remove hard coded checks for column names and replace them with "global" variables you can configure in your app or that are configured in the rails engine?
How do I allow users to create their own config file that the engine knows about when you go to "install" the engine in your app.
Exposing engine's config is a pretty simple task. Using my engine as an example:
Actual configuration file. This is where you can set your defaults.
https://github.com/comfy/comfortable-mexican-sofa/blob/master/lib/comfortable_mexican_sofa/configuration.rb
This way whatever is defined in that class is accessible via ComfortableMexicanSofa.config.some_config_option
Now you need a nice way to expose configuration:
https://github.com/comfy/comfortable-mexican-sofa/blob/master/lib/comfortable_mexican_sofa.rb
This is so it's possible to have this kind of interface:
ComfortableMexicanSofa.configure do |config|
config.cms_title = 'My Custom title'
end
So now you can have initializer that you can move to host app with a generator:
https://github.com/comfy/comfortable-mexican-sofa/blob/master/config/initializers/comfortable_mexican_sofa.rb
So now you can use configuration to make your is_admin? method more flexible. Of course there's always an option to completely override engine's stuff from the host application.
I am trying a number of different applications into my rails project. For security reasons, I am storing any sensitive keys as environment variables.
This is easy to do with Heroku but on the local environment side I find my windows environment variables starting to pile up. If I happen to have two projects with facebook authentication now I have to name them uniquely on my computer not to get mixed up with each others, which then means I have to rename them in my rails projects, which then means I need to rename them in Heroku... AH
Is there an easier way of doing this such as a configuration file that is added to gitignore, or is that still not quite safe?
What's the best practice for this?
Rails 4.1 comes with secrets.yml, which is where you would put these. Please see this section of the Rails 4.1 release notes for more info.
I have a url that I am using in one of the controllers. Is there a better place to put this url? The url uses an API key and I was wondering if there is a better place to add this url and/or api key such that its not added in the controller class code and ergo more editable? If i add it as an environment variable or anything else how do i access it from my controller class? thank you. ITS A RUBY AND RAILS PROJECT
Using environment variables might be a good idea if you want to keep things like API keys and passwords out of your source code. Accessing them from within your code is done with the ENV object:
my_api_key = ENV['MY_API_KEY']
To use this technique, you need to set up the variables in your environment before launching your app, and how you do this depends on your local setup, and will likely also vary between development and production.
In development, you can simply set the environment vars in your shell, e.g. with bash:
$ export MY_API_KEY=foobar123abc
$ rails s
Now rails will start and have access to this environment variable. You can also set variables for just a single command:
$ MY_API_KEY=foobar123abc rails s
Depending on what the sevice/api is, you could set some of them to default development/test values in config/environments/development.rb (or test.rb):
ENV['MY_API_KEY'] = 'non_secret_api_key_that_can_be_shared_around'
Setting up environment variables in production will depend on how you're deploying your app. Phusion have an article on using environment variables in Passenger if your using that. There's also a useful article on using environment variables with Heroku which is worth a read even if you're not using them for deployment.
You can add it to application.rb file as
config.service {
:api_key => 'api_key'
}
Or better yet, add it to development.rb and production.rb files so that you can test it better.
You can access this api_key from controller like
Rails.application.config.service[:api_key]