Module: PiiSafeSchema

Extended by:
Notify
Defined in:
lib/pii_safe_schema.rb,
lib/pii_safe_schema/notify.rb,
lib/pii_safe_schema/railtie.rb,
lib/pii_safe_schema/version.rb,
lib/pii_safe_schema/pii_column.rb,
lib/pii_safe_schema/annotations.rb,
lib/pii_safe_schema/configuration.rb,
lib/pii_safe_schema/notifiers/std_out.rb,
lib/pii_safe_schema/notifiers/data_dog.rb,
lib/pii_safe_schema/migration_generator.rb,
lib/pii_safe_schema/invalid_column_error.rb

Defined Under Namespace

Modules: Annotations, MigrationGenerator, Notify Classes: Configuration, ConfigurationError, InvalidColumnError, PiiColumn, Railtie

Constant Summary collapse

VERSION =
'1.4.4'.freeze

Constants included from Notify

Notify::METHODS

Class Method Summary collapse

Methods included from Notify

notify

Class Method Details

.activate!Object



30
31
32
33
34
35
36
37
38
# File 'lib/pii_safe_schema.rb', line 30

def self.activate!
  return if Rails.env.test?

  ActiveSupport.on_load :active_record do
    Notify.notify(PiiSafeSchema::PiiColumn.all)
  end
rescue ActiveRecord::NoDatabaseError
  Rails.logger.info('PiiSafeSchema: No DB'.red)
end

.configurationObject



18
19
20
# File 'lib/pii_safe_schema.rb', line 18

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/pii_safe_schema.rb', line 26

def self.configure
  yield(configuration)
end

.generate_migrations(additional_pii_columns = []) ⇒ Object



40
41
42
43
44
# File 'lib/pii_safe_schema.rb', line 40

def self.generate_migrations(additional_pii_columns = [])
  PiiSafeSchema::MigrationGenerator.generate_migrations(
    PiiSafeSchema::PiiColumn.all + additional_pii_columns,
  )
end

.parse_additional_columns(arguments) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pii_safe_schema.rb', line 46

def self.parse_additional_columns(arguments)
  arguments.map do |str|
    matches = /([a-z_]+):([a-z_]+):([a-z_]+)/i.match(str)
    return print_help! if matches.blank?

    suggestion = Annotations.comment(matches[3])
    return print_help! if suggestion.blank?

    PiiColumn.from_column_name(table: matches[1], column: matches[2], suggestion: suggestion)
  end
end


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pii_safe_schema.rb', line 58

def self.print_help!(do_exit: true)
  puts <<~HELPMSG # rubocop:disable Rails/Output
    Usage:
      rake pii_safe_schema:generate_migrations [table:column:annotation_type] ...

    Arguments:
      [table:column:annotation_type]   # A column to manually annotate. Can be repeated.
                                       # annotation_type can be "email", "phone", "ip_address",
                                       # "geolocation", "address", "postal_code", "name",
                                       # "sensitive_data", or "encrypted_data"

    Description:
      Generates a migration to add PII annotation comments to appropriate columns on a table.
      Uses a series of regular expressions to find sensitive fields.

      Optionally supply arguments to annotate columns explicitly

    Example:
      rake pii_safe_schema:generate_migrations signatures:signatory_name:name signatures:landline:phone

      Will generate a migration with the following, assuming automatic regex had no matches:

      class ChangeCommentsInSignatures < ActiveRecord::Migration[5.2]
        def change
          safety_assured do
            change_column :signatures, :signatory_name, :string, comment: '{"pii":{"obfuscate":"name_obfuscator"}}'
            change_column :signatures, :landline, :string, comment: '{"pii":{"obfuscate":"phone_obfuscator"}}'
          end
        end
      end
  HELPMSG

  exit(1) if do_exit # rubocop:disable Rails/Exit
end

.reset_configuration!Object



22
23
24
# File 'lib/pii_safe_schema.rb', line 22

def self.reset_configuration!
  @configuration = Configuration.new
end