Sunday, 10 November 2019

Credentials in Rails 5.2 and Rails 6 Multi Environment credentials support

 Rails 5.2 - Supports credentials , But Not support Multi Environment

credentials are stored in the file config/credentials.yml.enc

and this file is encrypted by the key located inside
config/master.key.

How to achieve multi-environment feature in rails 5.2 is



module YourAppModule
  class Application < Rails::Application
    def credentials
      if Rails.env.production?
        super
      else
        encrypted(
          "config/credentials.#{Rails.env.downcase}.yml.enc",
          key_path: "config/#{Rails.env.downcase}.key"
        )
      end
    end
  end
end



then run  the command
RAILS_ENV=staging bin/rails credentials:edit
create staging.key file if not created than re-run the command which will add some staging credentials



RAILS 6 Comes with Multi Environment credentials support which you can start with below commands
the command to create and edit credentials file
EDITOR=vim rails credentials:edit -e development

in this, you can define whatever editor you want to use with passing environment name with
option -e

Example of staging and production.

EDITOR=vim rails credentials:edit -e staging
EDITOR=vim rails credentials:edit -e production


Test
RAILS_ENV=staging rails c
pry(main)> Rails.application.credentials.aws[:key]

By mistake, I put the environment variable under this and got the below error if you got same just remove and use key-value pair under this

This will generate one yml file with a separate key, I have installed this then got an error or
undefined method `deep_symbolize_keys' for #<String..

No comments:

Post a Comment