Rails and the hidden credentials
Moving away from ENV
After doing my homework there was a lot of suggestions to use the new Rails Credentials setup. This was a bit more difficult as adding credentials to the credentials.yml.enc file isn't as simple as just pasting them in.
First you have to get into your credentials.yml.enc file. If you just open it you'll notice it has a single line of hexadecimal code. Do not edit this directly.
To edit this file you'l need to enter the follow command in your terminal:
EDITOR="atom --wait" rails credentials:edit
Replace "atom" with your own editor. This will bring up the file you need to edit. The syntax to setting up your credentials is:
name_of_group:
id:
secret:
For example:aws:
access_key_id: YOURACCESSKEY
secret_access_key: YOURSECRETACCESSKEY
You can put as many different credentials as you want in that file, just separate them into groups.Once that is done, save the file and close it. If you look in your terminal you should see "Encrypted and saved" but you can also confirm this by running:
rails credentials:show
Now you'll need to insert this into your code. Where ever it is you need it you'll insert Rails.application.credentials.dig()
Using the example above it would look like this:
amazon: service: S3 access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> region: us-east-2 bucket: <%= Rails.application.credentials.dig(:aws, :bucket) %>
Comments
Post a Comment