[英]RSpec model test testing encrypted attribute randomly failing with "key_derivation_salt is not configured"
I have a Rails 7.0.3 app with a model that has an encrypted attribute .我有一个带有加密属性的模型的 Rails 7.0.3 应用程序。 I have an RSpec test which tests the model's behaviour.
我有一个 RSpec 测试来测试模型的行为。 I have a GitHub Actions workflow setup running RSpec.
我有一个运行 RSpec 的 GitHub Actions 工作流设置。 However : every first run for a specific commit fails, every next run succeeds .
但是:特定提交的每次第一次运行都失败,每次下一次运行都成功。 As
作为
The error:错误:
ActiveRecord::Encryption::Errors::Configuration:
key_derivation_salt is not configured. Please configure it via credential active_record_encryption.key_derivation_salt or by setting config.active_record.encryption.key_derivation_salt
The GitHub actions config (non-essential details left out for brevity): GitHub 操作配置(为简洁起见,省略了非必要细节):
name: CI
on: [push]
jobs:
rspec:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ".ruby-version"
- name: Bundle Install
run: |
gem install bundler -v $(grep 'BUNDLED WITH' -A1 Gemfile.lock | tail -n 1 )
bundle config set --local path 'vendor/bundle'
bundle install --jobs 4 --retry 3
- env:
RAILS_MASTER_KEY: "${{ secrets.RAILS_MASTER_KEY }}"
run: RAILS_ENV=test bundle exec rspec
I have the secret setup in the repo configuration:我在 repo 配置中有秘密设置:
The necessary encryption configuration is stored in test.enc.yml
:必要的加密配置存储在
test.enc.yml
中:
active_record_encryption:
primary_key: u▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉
deterministic_key: 4▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉
key_derivation_salt: R▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉
I really dislike the idea of using some flavour of RSpec retry/rerun gem to fix it.我真的不喜欢使用某种 RSpec retry/rerun gem 来修复它的想法。 I'd really like to solve the underlying issue.
我真的很想解决根本问题。 Anybody any idea?
有人知道吗?
it looks more like a issue with loading your credentials
in test environment.它看起来更像是在测试环境中加载
credentials
的问题。 I've stumbled upon similar error today, but I don't want to pass RAILS_MASTER_KEY
for test environment.我今天偶然发现了类似的错误,但我不想将
RAILS_MASTER_KEY
用于测试环境。 What I did were adding this code to config/environments/test.rb
file:我所做的是将此代码添加到
config/environments/test.rb
文件中:
config.active_record.encryption.primary_key = 'test'
config.active_record.encryption.deterministic_key = 'test'
config.active_record.encryption.key_derivation_salt = 'test'
Unless you don't need an exteremly secure DB in a test environment this should do the job for you.除非您在测试环境中不需要绝对安全的数据库,否则这应该可以为您完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.