Abel
Abel

Reputation: 179

How I can read the encrypted file created with the gem called rails-env-credentials on Ruby?

I am currently working on a project where I have to make id and passwords encrypted, so I decided to use the gem called rails-env-credentials https://github.com/sinsoku/rails-env-credentials

It seemed that creating some files for development is fine, but I can't read the file on console or on web.

I guess it is either because I didn't set something to be set which I don't come up with or I am trying to get the value in a wrong way.

with the command like this rails env_credentials:edit -e development, I made the config/credentials/development.yml.enc which has encrypted string of this

  aws:
    access_key_id: 123
    secret_access_key: 345

And in the config/credentials/development.key file, something like this is automatically created.

<rails secret key>

I expected to get the value of the file by calling this.

Rails.application.credentials.aws

But I get nil.

and when I do Rails.application.credentials on console, I get something like this.

#<ActiveSupport::EncryptedConfiguration:0x000055882a4965f8 
@key_path=#<Pathname:config/master-development.key>, 
@content_path=#Pathname:config/credentials-development.yml.enc>, 
@env_key="RAILS_MASTER_KEY_DEVELOPMENT", 
@raise_if_missing_key=false, @config={}, @options={}>

Since there is no error, I got stack in the same place for days. If you have any ideas please let me know. I would really really appreciate it.

EDIT****

in the encrypted file, there is something like this. And I think I tried to get the value like the way to get value from hash but I can't get it correctly.

I tried something like

enter image description here

I am sorry but i have no ideas to fix this...

Do I have to do something with master.key?

Upvotes: 0

Views: 522

Answers (3)

susie
susie

Reputation: 193

If you are still stuck there then this is the way for reading the credentials

 Rails.application.credentials.config.dig(:aws, :access_key_id)
 Rails.application.credentials.config.dig(:aws, :secret_access_key)

Upvotes: 0

Sajid Ali
Sajid Ali

Reputation: 86

It's easy way to reading the credentials

Rails.application.credentials.aws[:access_key_id]
Rails.application.credentials.aws[:secret_access_key]

Upvotes: 1

Bouaik Lhoussaine
Bouaik Lhoussaine

Reputation: 584

This is the way for reading the credentials

Rails.application.credentials.dig(:aws, :access_key_id)
Rails.application.credentials.dig(:aws, :secret_access_key)

Upvotes: 5

Related Questions