Rails: During asset precompile throws error key must be 16 bytes
Asked Answered
A

4

7

I am storing my secret key in environment and /config/environments/production.rb has config.require_master_key = true uncommented

config.require_master_key = true

When running

RAILS_ENV=production bundle exec rake assets:precompile

I get the error

/Users/something/Development/wwwroot/trivial/config/environment.rb:5:in `<main>'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'

Caused by:
ArgumentError: key must be 16 bytes
/Users/something/Development/wwwroot/trivial/config/environment.rb:5:in `<main>'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
Tasks: TOP => environment

any ideas on how to fix this error? What else can I do?

Aeroembolism answered 6/1, 2019 at 17:55 Comment(0)
A
4

Your problem is that the key you generated is longer of what rails expects https://github.com/rails/rails/issues/33528#issuecomment-412677795

Solution

You can recreate a new one by deleting your master.key and credentials.yml.enc and run

rails credentials:edit
Akee answered 21/1, 2019 at 19:14 Comment(1)
It is worth noting that you might want to save your credentials somewhere before you do this.Spermicide
P
3

I faced the same issue while setting up a Rails 6.0 application on Ubuntu in production.

I was using the figaro gem for my environment variables.

The issue was that I was copying the content of the secret_key_base instead of the master_key

Here's how I solved it

Delete the previous master.key and the credentials.yml.enc file

Recreate a new master.key and credentials.yml.enc:

rails credentials:edit

OR

EDITOR="code --wait" bin/rails credentials:edit  # If you want to use VS Code as your editor

Copy the contents of the master.key, which is of this format:

34d3cc7c5305dde06865acfa473716cd

Replace my RAILS_MASTER_KEY value with the master_key in production:

RAILS_MASTER_KEY: "34d3cc7c5305dde06865acfa473716cd"

And then save it.

Note: You could also experience this issue if you set/specify a wrong RAILS_MASTER_KEY environment variable in your .env files (.env, .env.development, .env.test, .env.production). Say you just want to use it as a placeholder temporarily. This may also throw an error key=': key must be 16 bytes (ArgumentError) if you try to generate new master.key and the credentials.yml.enc files using rails credentials:edit or EDITOR="code --wait" bin/rails credentials:edit

What you have to do is to either provide the right RAILS_MASTER_KEY environment variable in the .env file(s) or comment out the RAILS_MASTER_KEY environment variable if you are not using it.

That's it.

I hope this helps

Patman answered 22/3, 2020 at 15:32 Comment(2)
Isn't 34d3cc7c5305dde06865acfa473716cd 32 bytes?Elemi
each character is hexadecimal so 16 bits so 2 bytes, so 32 characters is 16 * 32 bits (512 bits) which is 512 / 8 bytes so 64 bytes, weirdStorm
M
2

For me I had to ensure I remove the quotes around the key in my .env file.

It seems my server(AWS ECS Fargate) was counting the "" as part of the key. Locally it was all fine.

Before

RAILS_MASTER_KEY="12345"

After

RAILS_MASTER_KEY=12345
Meuse answered 5/1, 2021 at 19:44 Comment(0)
A
-3

You can run this in your terminal

heroku config:set RAILS_MASTER_KEY=`cat config/master.key`

You can follow the tutorial here

Audiometer answered 5/5, 2022 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.