active_record_encryption.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. %w(
  3. ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY
  4. ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT
  5. ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY
  6. ).each do |key|
  7. if ENV['SECRET_KEY_BASE_DUMMY']
  8. # Use placeholder value during production env asset compilation
  9. ENV[key] = SecureRandom.hex(64)
  10. end
  11. value = ENV.fetch(key, '')
  12. if value.blank?
  13. abort <<~MESSAGE
  14. Mastodon now requires that these variables are set:
  15. - ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY
  16. - ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT
  17. - ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY
  18. Run `bin/rails db:encryption:init` to generate new secrets and then assign the environment variables.
  19. Do not change the secrets once they are set, as doing so may cause data loss and other issues that will be difficult or impossible to recover from.
  20. MESSAGE
  21. end
  22. next unless Rails.env.production? && value.end_with?('DO_NOT_USE_IN_PRODUCTION')
  23. abort <<~MESSAGE
  24. It looks like you are trying to run Mastodon in production with a #{key} value from the test environment.
  25. Please generate fresh secrets using `bin/rails db:encryption:init` and use them instead.
  26. MESSAGE
  27. end
  28. Rails.application.configure do
  29. config.active_record.encryption.deterministic_key = ENV.fetch('ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY')
  30. config.active_record.encryption.key_derivation_salt = ENV.fetch('ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT')
  31. config.active_record.encryption.primary_key = ENV.fetch('ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY')
  32. config.active_record.encryption.support_sha1_for_non_deterministic_encryption = true
  33. end