rails: Running "rails credentials:edit" returns "key must be 16 bytes"
Steps to reproduce
- Create (for my case) a Rails API, and set it up normally
- Add a
RAILS_MASTER_KEYto the figaro-generated application.yml file, with the result of the “rails secret” command - Create a Dockerfile where the context of RAILS_ENV is set to production.
- Run
bundle exec rails s
Here is the whole Dockerfile
FROM ruby:2.4.3-stretch
RUN apt-get update -qq && apt-get install -y build-essential curl redis-server
RUN curl https://getcaddy.com | bash -s personal
RUN chown root:root /usr/local/bin/caddy && chmod 755 /usr/local/bin/caddy
RUN mkdir /home/some-api
WORKDIR /home/some-api
COPY . ./
RUN bundle install
RUN EDITOR=nano rails credentials:edit
ENV RAILS_ENV=production
ENV RAKE_ENV=production
EXPOSE 3000
CMD ["bundle", "exec", "rails", "s"]
Expected behavior
Everything should work smoothly
Actual behavior
I’ve seen on some other issues that I somehow needed to delete “master.key” and “credential.yml.enc”, and that running rails credentials:edit should create them back. If I keep the original files without changing or deleting them, I get the following error:
/usr/local/bundle/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt’: ActiveSupport::MessageEncryptor::InvalidMessage (ActiveSupport::MessageEncryptor::InvalidMessage)
If I delete them, I’ll get an error saying that the “rails_secret_key” hasn’t been defined (I can’t retrieve the exact message)
If I run the command rails credentials:edit, I’ll get the error
No $EDITOR to open file in. Assign one like this: EDITOR=“mate --wait” bin/rails credentials:edit
(While I’ve seen a lot of people running this command without trouble)
And, finally, if I run the command EDITOR=nano rails credentials:edit, I get the following error:
/home/jaeger767/.rvm/gems/ruby-2.4.3/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:169:in `key=': key must be 16 bytes (ArgumentError)
So I’m stuck, I’ve tried a lot of solutions, and even running the commands outside the Docker context won’t work.
System configuration
Rails version:
Rails 5.2.0
Ruby version:
Ruby 2.4.3 (No problem if I have to update this one)
What should I do? Thank you in advance
P.S. I have absolutely no problem whatsoever when running in development mode
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (3 by maintainers)
the damage that master.key caused in every deployment is more than the whole prodactivity you gain building apps on Rails 😕 😕
Getting the same error as well. Rails 7.0.7 and Ruby 3.2.2
cat config/master.key | wc -c> 32echo $(cat config/master.key) | wc -c=> 33What workaround exists to ensure there are no extra chars included which such command ?
Guys, you won’t believe it. It worked for me when I switched off protection & masking settings in the CI/CD.
When they were masked & protected. it was coping $RAILS_MASTER_KEY as 1 char to congif/master.key which was making Rails complain of 16 bytes need for the key.
But when I relaxed it. It worked. After 16 Good hrs of debugging. -___-
I just set
RAILS_MASTER_KEYenvironment variable (.env) with a 32 char string and it worked. (Ruby 3.2.3 / Rails 6.1.7.7) cc/ @abel-tefera @johnnyiconI’m getting this error as well with Rails 7.0.7.2, Ruby 2.7.6.
I just tried the solution @m-faseeh-qbatch proposed. It gave me the same error.
To be clear, the error I’m getting is for 32 characters.
I got the same issue.
If you are working on a kubernetes cluster and if you are using a secret resource to pass a value, it may adds an extra eol at the end. It happens on GKE at least.
As a workaround, we can wrap the rails command as:
or we can manipulate the env variable at the beginning of the rails boot (maybe
config/application.rb?) with:(update)
I was wrong.
My local
config/master.keyitself has a line feed at the end. When encoding it into base64, the following command has done well:But it is still confusing because it works as expected on my local environment.