Class: ForgetThat::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/forget_that/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Service

Returns a new instance of Service.



5
6
7
# File 'lib/forget_that/service.rb', line 5

def initialize(options = {})
  @custom_anonymizers = options[:anonymizers] || {}
end

Instance Method Details

#anonymizersObject



46
47
48
49
50
51
52
53
54
# File 'lib/forget_that/service.rb', line 46

def anonymizers
  {
    random_date: -> { Time.now - [*1..10**4].sample.days },
    hex_string: -> { SecureRandom.hex(5) },
    random_phone: -> { "+49#{[*0..10].map { SecureRandom.random_number(10) } .join}" },
    fake_personal_id_number: -> { "DE#{[*0..10].map { SecureRandom.random_number(10) }.join}" },
    random_amount: -> { [*1..80].sample * [*900..1100].sample }
  }.merge(@custom_anonymizers)
end

#callObject

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/forget_that/service.rb', line 9

def call
  raise InvalidConfigError unless valid_config?

  config.each do |table, columns|
    klass = Class.new(ForgetThat::Record) { self.table_name = table }
    records_hash = klass
                   .for_anonymization
                   .pluck(:id)
                   .map { |id| [id, populate_records_hash(columns)] }
                   .to_h
    klass.update(records_hash.keys, records_hash.values)
  end
end

#sanitize_collection(collection) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/forget_that/service.rb', line 23

def sanitize_collection(collection)
  raise InvalidConfigError unless valid_anonymizer_set?
  raise InvalidCollectionError unless valid_records? collection

  table = collection.klass.table_name
  unsafe_columns = config[table].keys
  safe_columns = (collection.klass.columns.map(&:name) - unsafe_columns).reject { |e| e == 'id' }
  safe_data = collection.pluck(*safe_columns).map { |r| r.is_a?(Array) ? r : [r] }
  safe_data.map do |record|
    makeshift = {}
    unsafe_columns.each do |column|
      makeshift[column] = config[table][column]
    end
    makeshift = makeshift
                .map { |key, value| [key, value.to_s % generate_anonymized_values] }
                .to_h
    safe_columns
      .map.with_index { |value, index| [value, record[index]] }
      .to_h
      .merge(makeshift)
  end
end

#valid_anonymizer_set?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/forget_that/service.rb', line 63

def valid_anonymizer_set?
  available = anonymizers.stringify_keys.keys
  required = config
             .map { |_key, value| value.map { |_key, val| val } }
             .flatten
             .map { |value| value.to_s.scan(/%{(\w*)\.?.*}/) }
             .flatten
             .uniq
  return true if (required - available).empty?

  ForgetThat.logger.error("Anonymizers #{(required - available).join(', ')} are not defined")
  false
end

#valid_config?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/forget_that/service.rb', line 56

def valid_config?
  return false unless valid_anonymizer_set?
  return false unless valid_database_schema?

  true
end

#valid_database_schema?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/forget_that/service.rb', line 77

def valid_database_schema?
  if config
     .keys
     .map { |table| ActiveRecord::Base.connection.columns(table).map(&:name).include? 'anonymized' }
     .reduce { |a, b| a && b }
    true
  else
    ForgetThat.logger.error('Some of the tables in your database do not contain `anonymized` flag')
    false
  end
end

#valid_records?(collection) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/forget_that/service.rb', line 89

def valid_records?(collection)
  return true if collection.is_a?(ActiveRecord::Relation)

  false
end