Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Rails.application.secret_key_base directly #3043

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions lib/avo/services/encryption_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,7 @@ def decrypt
private

def encryption_key
secret_key_base[0..31]
end

def secret_key_base
# Try to fetch the secret key base from ENV or the credentials file
key = ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base

# If key is blank and Rails version is less than 7.2.0
# Try to fetch the secret key base from the secrets file
# Rails 7.2.0 made secret_key_base from secrets obsolete
if key.blank? && (Gem::Version.new(Rails.gem_version) < Gem::Version.new("7.2.0"))
key = Rails.application.secrets.secret_key_base
end

return key if key.present?

# Avoid breaking in production
# All features relying on encryption will not work properly without a configured secret key base
return SecureRandom.random_bytes(32) if Rails.env.production?

raise "Unable to fetch secret key base. Please set it in your credentials or environment variables\n" \
"For more information check https://docs.avohq.io/3.0/encryption-service.html#secret-key-base"
Rails.application.secret_key_base[0..31]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Rails.application.secret_key_base[0..31]
Rails.application.secret_key_base[0..31]
rescue
# Avoid breaking in production
# All features relying on encryption will not work properly without a configured secret key base
return SecureRandom.random_bytes(32) if Rails.env.production?
raise "Unable to fetch secret key base. Please set it in your credentials or environment variables\n" \
"For more information check https://docs.avohq.io/3.0/encryption-service.html#secret-key-base"

end
end
end
Expand Down
Loading