2018-09-14 13:21:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module BackgroundMigration
|
|
|
|
# EncryptColumn migrates data from an unencrypted column - `foo`, say - to
|
|
|
|
# an encrypted column - `encrypted_foo`, say.
|
|
|
|
#
|
2018-11-21 05:46:36 -05:00
|
|
|
# To avoid depending on a particular version of the model in app/, add a
|
|
|
|
# model to `lib/gitlab/background_migration/models/encrypt_columns` and use
|
|
|
|
# it in the migration that enqueues the jobs, so code can be shared.
|
|
|
|
#
|
2018-09-14 13:21:28 -04:00
|
|
|
# For this background migration to work, the table that is migrated _has_ to
|
|
|
|
# have an `id` column as the primary key. Additionally, the encrypted column
|
|
|
|
# should be managed by attr_encrypted, and map to an attribute with the same
|
|
|
|
# name as the unencrypted column (i.e., the unencrypted column should be
|
2018-11-21 05:46:36 -05:00
|
|
|
# shadowed), unless you want to define specific methods / accessors in the
|
|
|
|
# temporary model in `/models/encrypt_columns/your_model.rb`.
|
2018-09-14 13:21:28 -04:00
|
|
|
#
|
|
|
|
class EncryptColumns
|
|
|
|
def perform(model, attributes, from, to)
|
|
|
|
model = model.constantize if model.is_a?(String)
|
2018-11-22 11:17:08 -05:00
|
|
|
|
|
|
|
# If sidekiq hasn't undergone a restart, its idea of what columns are
|
|
|
|
# present may be inaccurate, so ensure this is as fresh as possible
|
|
|
|
model.reset_column_information
|
|
|
|
model.define_attribute_methods
|
|
|
|
|
2018-09-14 13:21:28 -04:00
|
|
|
attributes = expand_attributes(model, Array(attributes).map(&:to_sym))
|
|
|
|
|
|
|
|
model.transaction do
|
|
|
|
# Use SELECT ... FOR UPDATE to prevent the value being changed while
|
|
|
|
# we are encrypting it
|
|
|
|
relation = model.where(id: from..to).lock
|
|
|
|
|
|
|
|
relation.each do |instance|
|
|
|
|
encrypt!(instance, attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-03 08:29:51 -05:00
|
|
|
def clear_migrated_values?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-09-14 13:21:28 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Build a hash of { attribute => encrypted column name }
|
|
|
|
def expand_attributes(klass, attributes)
|
|
|
|
expanded = attributes.flat_map do |attribute|
|
|
|
|
attr_config = klass.encrypted_attributes[attribute]
|
|
|
|
crypt_column_name = attr_config&.fetch(:attribute)
|
|
|
|
|
|
|
|
raise "Couldn't determine encrypted column for #{klass}##{attribute}" if
|
|
|
|
crypt_column_name.nil?
|
|
|
|
|
2018-11-22 11:17:08 -05:00
|
|
|
raise "#{klass} source column: #{attribute} is missing" unless
|
|
|
|
klass.column_names.include?(attribute.to_s)
|
|
|
|
|
|
|
|
# Running the migration without the destination column being present
|
|
|
|
# leads to data loss
|
|
|
|
raise "#{klass} destination column: #{crypt_column_name} is missing" unless
|
|
|
|
klass.column_names.include?(crypt_column_name.to_s)
|
|
|
|
|
2018-09-14 13:21:28 -04:00
|
|
|
[attribute, crypt_column_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
Hash[*expanded]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate ciphertext for each column and update the database
|
|
|
|
def encrypt!(instance, attributes)
|
|
|
|
to_clear = attributes
|
|
|
|
.map { |plain, crypt| apply_attribute!(instance, plain, crypt) }
|
|
|
|
.compact
|
|
|
|
.flat_map { |plain| [plain, nil] }
|
|
|
|
|
|
|
|
to_clear = Hash[*to_clear]
|
|
|
|
|
|
|
|
if instance.changed?
|
|
|
|
instance.save!
|
2018-12-04 04:10:04 -05:00
|
|
|
|
2018-12-03 08:29:51 -05:00
|
|
|
if clear_migrated_values?
|
|
|
|
instance.update_columns(to_clear)
|
|
|
|
end
|
2018-09-14 13:21:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_attribute!(instance, plain_column, crypt_column)
|
|
|
|
plaintext = instance[plain_column]
|
|
|
|
ciphertext = instance[crypt_column]
|
|
|
|
|
|
|
|
# No need to do anything if the plaintext is nil, or an encrypted
|
|
|
|
# value already exists
|
2019-03-06 07:18:53 -05:00
|
|
|
return unless plaintext.present?
|
|
|
|
return if ciphertext.present?
|
2018-09-14 13:21:28 -04:00
|
|
|
|
|
|
|
# attr_encrypted will calculate and set the expected value for us
|
|
|
|
instance.public_send("#{plain_column}=", plaintext) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
|
|
|
|
plain_column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|