Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read rails encrypted credentials in webpacker react app

With .env files, it was easy to inject variables in webpacker js packs. Since 5.2, you can use the encrypted secrets, but is there a way to read (decrypt) them in inject some of them on webpacker build time?

like image 558
borisrorsvort Avatar asked Oct 30 '20 10:10

borisrorsvort


People also ask

How do I decrypt credentials in rails?

The master key When you create a new rails app a file called credentials. yml. enc is added to the config directory. This file will be decrypted in a production environment using a key stored either on a RAILS_MASTER_KEY environment variable or a master.

Where is rails application secrets?

Rails stores secrets in config/credentials. yml. enc, which is encrypted and cannot be edited directly.

How do I change my rails credentials?

Your text editor will open an unencrypted version of your credentials. If you don't have EDITOR set, you can run EDITOR=vi bin/rails credentials:edit or use your favorite text editor. After saving the file, the encrypted version will be saved to config/credentials. yml.


Video Answer


1 Answers

You can read from credentials and pass that value to Webpacker.

  1. Create a config/initializers/webpacker.rb file.

  2. Pass it to the Webpacker::Compiler through file above.

    Webpacker::Compiler.env['VALUE'] = Rails.application.credentials.dig(:value)
    
  3. Read it as console.log(process.env.VALUE).

  4. If you want to try it on development, you need to add these to the bin/webpack-dev-server:

    require_relative '../config/application'
    Rails.application.initialize!
    

Sources:

  • http://translate.google.com/translate?hl=&sl=ja&tl=en&u=https%3A%2F%2Fqiita.com%2Ftakeyuweb%2Fitems%2F61e6ba07fe0df3079041
  • https://github.com/rails/webpacker/issues/2794
  • https://github.com/rails/webpacker/blob/830f47695ea3f40dcbab5ee117769ab67b96af36/docs/env.md
like image 84
ogirginc Avatar answered Oct 08 '22 14:10

ogirginc