RSpec 模型测试测试加密属性随机失败,并显示“key_derivation_salt 未配置”

use*_*745 3 encryption rspec ruby-on-rails rails-activerecord github-actions

我有一个 Rails 7.0.3 应用程序,其模型具有加密属性。我有一个 RSpec 测试来测试模型的行为。我有一个运行 RSpec 的 GitHub Actions 工作流程设置。但是特定提交的每次第一次运行都会失败,而接下来的每次运行都会成功。作为

\n

错误:

\n
ActiveRecord::Encryption::Errors::Configuration:\n       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\n
Run Code Online (Sandbox Code Playgroud)\n

GitHub 操作配置(为简洁起见,省略了非必要的细节):

\n
ActiveRecord::Encryption::Errors::Configuration:\n       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\n
Run Code Online (Sandbox Code Playgroud)\n

我在回购配置中有秘密设置:

\n

在此输入图像描述

\n

必要的加密配置存储在test.enc.yml

\n
name: CI\non: [push]\njobs:\n  rspec:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v1\n    - name: Set up Ruby\n      uses: ruby/setup-ruby@v1\n      with:\n        ruby-version: ".ruby-version"\n    - name: Bundle Install\n      run: |\n        gem install bundler -v $(grep \'BUNDLED WITH\' -A1 Gemfile.lock | tail -n 1 )\n        bundle config set --local path \'vendor/bundle\'\n        bundle install --jobs 4 --retry 3\n    - env:\n        RAILS_MASTER_KEY: "${{ secrets.RAILS_MASTER_KEY }}"\n      run: RAILS_ENV=test bundle exec rspec\n
Run Code Online (Sandbox Code Playgroud)\n

我真的不喜欢使用某种 RSpec 重试/重新运行 gem 来修复它的想法。我真的很想解决根本问题。有人有什么想法吗?

\n

小智 5

credentials它看起来更像是加载测试环境的问题。我今天偶然发现了类似的错误,但我不想通过RAILS_MASTER_KEY测试环境。我所做的是将这段代码添加到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'
Run Code Online (Sandbox Code Playgroud)

除非您在测试环境中不需要极其安全的数据库,否则这应该可以满足您的要求。