Cannot edit rails credentials Rails 5.2
Asked Answered
W

3

10

I am working on a Rails 5.2 app and I have installed and setup active_storage, however, I can't seem to edit or show the rails credentials.

Here is an output of the error

ben@ben-VirtualBox:~/Desktop/benbagley$ EDITOR="atom --wait" rails credentials:edit
Traceback (most recent call last):
    38: from bin/rails:3:in `<main>'
    37: from bin/rails:3:in `load'
    36: from /home/ben/Desktop/benbagley/bin/spring:15:in `<top (required)>'
    35: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
    34: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
    33: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
    32: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
    31: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
    30: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
    29: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
    28: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
    27: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
    26: from /home/ben/Desktop/benbagley/bin/rails:9:in `<top (required)>'
    25: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
    24: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
    23: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
    22: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
    21: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
    20: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
    19: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
    18: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
    17: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands.rb:18:in `<main>'
    16: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command.rb:46:in `invoke'
    15: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command/base.rb:65:in `perform'
    14: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    13: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    12: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    11: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/credentials/credentials_command.rb:26:in `edit'
    10: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command/helpers/editor.rb:26:in `catch_editing_exceptions'
     9: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/credentials/credentials_command.rb:27:in `block in edit'
     8: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/credentials/credentials_command.rb:50:in `change_credentials_in_system_editor'
     7: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:54:in `change'
     6: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:21:in `read'
     5: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:42:in `read'
     4: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:79:in `decrypt'
     3: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/messages/rotator.rb:21:in `decrypt_and_verify'
     2: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:157:in `decrypt_and_verify'
     1: from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:183:in `_decrypt'
/home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt': ActiveSupport::MessageEncryptor::InvalidMessage (ActiveSupport::MessageEncryptor::InvalidMessage)

However, when I generate a new app I can edit and show the credentials. So not sure on the issue with this app as I haven't changed the master key.

EDIT::

I'm also getting

ben@ben-VirtualBox:~/Desktop/benbagley$ rails s
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.4 (ruby 2.5.0-p0), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/" for 127.0.0.1 at 2018-05-20 14:39:37 +0100
   (5.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98
Processing by PagesController#welcome as HTML
  Rendering pages/welcome.html.erb within layouts/application
  Rendered pages/welcome.html.erb within layouts/application (1.0ms)
  Rendered layouts/_navigation.html.erb (0.6ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 1149ms (Views: 1136.3ms | ActiveRecord: 0.0ms)


Started GET "/works" for 127.0.0.1 at 2018-05-20 14:39:41 +0100
Processing by WorksController#index as HTML
  Rendering works/index.html.erb within layouts/application
  Work Load (1.7ms)  SELECT "works".* FROM "works"
  ↳ app/views/works/index.html.erb:5
  ActiveStorage::Attachment Load (1.7ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 2], ["record_type", "Work"], ["name", "header_image"], ["LIMIT", 1]]
  ↳ app/views/works/index.html.erb:7
  Rendered works/index.html.erb within layouts/application (63.0ms)
Completed 500 Internal Server Error in 71ms (ActiveRecord: 15.9ms)



ActionView::Template::Error (ActiveSupport::MessageEncryptor::InvalidMessage):
    4: 
    5:     <% @works.each do |work| %>
    6:     <section class="max-w-md w-full lg:flex mb-2 shadow-md">
    7:       <section class="h-48 lg:h-auto lg:w-64 flex-none bg-cover rounded-t lg:rounded-t-none lg:rounded-l text-center overflow-hidden" style="background-image: url('<%= polymorphic_url work.header_image %>'); background-position: 50%; background-repeat: no-repeat;" title="<%= work.title %>">
    8:       </section>
    9:       <section class="border-r border-b border-l border-champagne-dark lg:border-l-0 lg:border-t bg-champagne-dark rounded-b lg:rounded-b-none lg:rounded-r p-4 flex flex-col justify-between leading-normal">
   10:         <section class="mb-6">

(erb):12:in `<main>'
app/views/works/index.html.erb:7:in `block in _app_views_works_index_html_erb__3122246537996604426_69953460278620'
app/views/works/index.html.erb:5:in `_app_views_works_index_html_erb__3122246537996604426_69953460278620'
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2018-05-20 14:39:55 +0100 ===
- Goodbye!
Exiting
Whitmire answered 20/5, 2018 at 13:12 Comment(0)
M
23

Comment the lines where you use your credentials and run EDITOR="atom --wait" rails credentials:edit (and uncomment them after you edit it). If this doesn't work, delete credentials.yml.enc and try again.

Monreal answered 7/7, 2018 at 20:3 Comment(1)
remove credentials.yml.enc and master.key then rerun rails credentials:edit worked. ThanksCesya
D
13

Move or remove credentials.yml.enc and master.key and rerun rails credentials:edit. This worked for me.

Diversion answered 26/5, 2018 at 0:59 Comment(0)
C
0

I had the same issue as well. I found out that running EDITOR="atom --wait" rails credentials:edit was not sufficient.

If you set Rails.application.credentials.some_api_key in your source code without first defining it in your encrypted credentials, you will be unable to edit the credentials file or even view it. This is presumably because the entire application must run in order for the credentials to be decrypted.

Cresa answered 12/5, 2019 at 13:50 Comment(1)
For me, I just had to remove instances of Rails.application.credentials from my application.rb file and then I was able to run the rails credentials:edit command. Thankfully it seems like you don't need to remove these from every file in the code.Octant

© 2022 - 2024 — McMap. All rights reserved.