-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DB encryption for email + OpenAI and Anthropic API keys (#274)
- Loading branch information
1 parent
cb823eb
commit 6fadd43
Showing
18 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Commonly configured through config/credentials.yml.enc. | ||
# For deployment to Render, we support configuration through ENV variables | ||
if ENV['CONFIGURE_ACTIVE_RECORD_ENCRYPTION_FROM_ENV'] == 'true' | ||
Rails.application.configure do | ||
Rails.logger.info "Configuring active record encryption from environment" | ||
config.active_record.encryption.primary_key = ENV['ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY'] | ||
config.active_record.encryption.deterministic_key = ENV['ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY'] | ||
config.active_record.encryption.key_derivation_salt = ENV['ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class EncryptKeys < ActiveRecord::Migration[7.1] | ||
def up | ||
puts "" | ||
puts "### ERROR? #################################################" | ||
puts "### YOU SHOULD NOT RUN db:migrate INSTEAD RUN db:prepare ###" | ||
puts "############################################################" | ||
puts "" | ||
|
||
User.find_each do |user| | ||
Rails.logger.info "Encrypt keys for #{user.id}. Has openai_key: #{user.openai_key.present?}; has anthropic_key: #{user.anthropic_key.present?}" | ||
user.encrypt | ||
if !user.save | ||
Rails.logger.warn "Could not update user #{user.id}: #{user.errors.full_messages.join(',')}" | ||
else | ||
Rails.logger.info "Successfully updated user #{user.id}" | ||
end | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration.new "Won't decrypt data" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class EncryptPersonEmails < ActiveRecord::Migration[7.1] | ||
def up | ||
Person.find_each do |person| | ||
Rails.logger.info "Encrypt email for #{person.id}" | ||
person.encrypt | ||
if !person.save(validate: false) | ||
Rails.logger.warn "Could not update person #{person.id}: #{person.errors.full_messages.join(',')}" | ||
else | ||
Rails.logger.info "Successfully updated user #{person.id}" | ||
end | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration.new "Won't decrypt data" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace :db do | ||
desc "Setup database encryption and update credentials" | ||
task setup_encryption: :environment do | ||
ensure_master_key | ||
|
||
old_config = Rails.application.credentials.config | ||
config = old_config.deep_dup | ||
|
||
if config[:secret_key_base].nil? && ENV['SECRET_KEY_BASE'].nil? | ||
config = add_secret_key_base(config) | ||
end | ||
|
||
if config[:active_record_encryption].nil? && ENV['CONFIGURE_ACTIVE_RECORD_ENCRYPTION_FROM_ENV'] != 'true' | ||
config = add_active_record_encryption(config) | ||
end | ||
|
||
if config != old_config | ||
Rails.application.credentials.write(config.to_yaml) | ||
ActiveRecord::Encryption.config.primary_key = config[:active_record_encryption][:primary_key] | ||
ActiveRecord::Encryption.config.deterministic_key = config[:active_record_encryption][:deterministic_key] | ||
ActiveRecord::Encryption.config.key_derivation_salt = config[:active_record_encryption][:key_derivation_salt] | ||
end | ||
end | ||
end | ||
|
||
Rake::Task["db:prepare"].enhance [:setup_encryption] | ||
|
||
def add_secret_key_base(config) | ||
config[:secret_key_base] = SecureRandom.hex(64) | ||
config | ||
end | ||
|
||
def add_active_record_encryption(config) | ||
config[:active_record_encryption] = { | ||
primary_key: SecureRandom.alphanumeric(32), | ||
deterministic_key: SecureRandom.alphanumeric(32), | ||
key_derivation_salt: SecureRandom.alphanumeric(32), | ||
} | ||
config | ||
end | ||
|
||
def encryption_init | ||
original_stdout = $stdout | ||
$stdout = StringIO.new | ||
Rake::Task["db:encryption:init"].invoke | ||
output = $stdout.string | ||
$stdout = original_stdout | ||
|
||
{ | ||
primary_key: output.match(/primary_key: (\S+)/)[1], | ||
deterministic_key: output.match(/deterministic_key: (\S+)/)[1], | ||
key_derivation_salt: output.match(/key_derivation_salt: (\S+)/)[1], | ||
} | ||
end | ||
|
||
def ensure_master_key | ||
master_key_path = Rails.root.join('config', 'master.key') | ||
unless File.exist?(master_key_path) | ||
key = SecureRandom.hex(16) | ||
File.write(master_key_path, key) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters