21

I recently deleted my local project but did a git clone and picked up from where I last committed. As I tried to upload images to AWS, which was already configured, I got this error:

Aws::Sigv4::Errors::MissingCredentialsError in RentalsController#create


Cannot load `Rails.config.active_storage.service`: missing credentials, provide credentials with one of the following options: - :access_key_id and :secret_access_key - :credentials - :credentials_provider

I tried to look at my credentials.yml file for any error but when I input this command:

EDITOR="code --wait" rails credentials:edit

...I get this error:

Couldn't decrypt config/credentials.yml.enc. Perhaps you passed the wrong key?

What could be the problem?

David Gay
  • 1,094
  • 2
  • 16
  • 32
Brian Ngeywo
  • 398
  • 1
  • 5
  • 10

1 Answers1

43

You are missing the master.key

When you create a new project, rails will setup the credentials.yml.enc and a matching master.key file to access the credentials. The master.key file will also automatically be added to the gitignore, so if you delete your project local and clone it again your master.key will be lost.

The only way to fix this is by setting up new credentials.yml.enc and add your AWS credentials to it again.

First you need to remove the existing credentials.yml.enc and then run: rails credentials:edit to create a new one.

Hackman
  • 1,614
  • 1
  • 12
  • 15
  • Thank you for posting this answer, it helped me solve the same problem as the OP. However I have a further question, this now means that I have a changed `credentials.yml.enc` file that could be staged for commit. My gut feeling is that I should not commit this as it is created with a different master key to my production app. What would be a good solution for this changed file? Should it be gitignored? – L.Youl Feb 17 '21 at 10:49
  • 1
    You should commit this and set the new `master.key` for your production app. You cannot use the old file because you lost the `master.key`. If you didn't lose the `master.key` than you shouldn't create a new `credentials.yaml.enc` at all and just put back your `master.key` – Hackman Feb 17 '21 at 11:05
  • 4
    For those of us who don't do a first deploy very often, this is easy to forget. And since the source of this error is well understood, surely Rails itself could provide a more useful error message that suggests the exact root cause given the context. – michael_teter Jun 07 '21 at 06:15
  • In my case I store master key in env. variable. I can see all credentials when I go to rails console with `Rails.application.credentials`. But when I try to use `bin/rails credentials:edit` I get error that it couldn't decrypt config/credentials.ymc.enc - what am I missing? – Jared Mar 07 '23 at 20:13