Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails credentials returning nil

I have a Rails 6.0.0rc2 app and I'm having trouble reading my credentials.

When I do rails credentials:edit I can add credentials and they're saving just fine.

aws:
  access_key_id: 123
  secret_access_key: 345

However, when I run a console and try reading the credentials I get the following:

Rails.application.credentials[:aws]
  => nil 

or

Rails.application.credentials.aws[:access_key_id]
Traceback (most recent call last):
    1: from (irb):2
NoMethodError (undefined method `[]' for nil:NilClass)

or

Rails.application.credentials[:aws][:access_key_id]
Traceback (most recent call last):
    1: from (irb):3
NoMethodError (undefined method `[]' for nil:NilClass)

No matter what I try my credentials always seem to be nil.

If I run Rails.application.credentials.secret_key_base I get the correct result, so the file can be read just fine.

like image 248
BillyBib Avatar asked Aug 03 '26 19:08

BillyBib


2 Answers

I ran into the same issue too. For me I restarting the Rails server fixed the problem.

Chances are the server either doesn't pick up the changes automatically, or that the server needs to be restarted whenever you create a new credentials.yml.enc file.

like image 143
Zain Rizvi Avatar answered Aug 05 '26 08:08

Zain Rizvi


Have you tried using dig?

https://ruby-doc.org/core-2.3.0_preview1/Hash.html#method-i-dig

Something like this might work:

Rails.application.credentials.dig(:aws, :access_key_id)
like image 29
vinyl Avatar answered Aug 05 '26 09:08

vinyl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!