Let's assume I have a config/secrets.yml
file that contains:
development:
app_name: MyApp
secret: <%= ENV['SECRET_VALUE'] %>
If I set SECRET_VALUE
with a newline, it will break. E.g:
export SECRET_VALUE=$(printf "hello\nworld")
Then when I start my rail application I get this error:
/usr/local/lib/ruby/3.0.0/psych.rb:457:in 'parse': (<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1 (Psych::SyntaxError)
While debugging the issue, I realized that the literal value of ENV['SECRET_VALUE']
is added to the yaml file before parsing it. That means if I wanted to achieve what I'm trying to do, I would have to do something like:
export SECRET_VALUE=$(printf "|\n hello\n world")
This works but this is very ugly and I can't be the only one who think this behaviour is absurd?? Is there a better way to do this?
EDIT:
I tried adding quotes around the value:
development:
app_name: MyApp
secret: "<%= ENV['SECRET_VALUE'] %>"
It "works" but the newline gets removed from the string...
root@4e4431bae32e:/app# rails console
Loading development environment (Rails 7.0.3)
irb(main):001:0> puts ENV['SECRET_VALUE']
hello
world
=> nil
irb(main):002:0> puts Rails.application.secrets[:secret]
hello world
=> nil
It's important that the newline remains for my use case.