anonymise 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4908d0afa0613463253178c7dd9cb55e3b5697a47fe1ed98d822576b9b4c419
4
- data.tar.gz: fe8abd61f8308405c82899d22e73d21cd8fc2ef2fdf11e4bb14e2ec55ddc8062
3
+ metadata.gz: 96aaf3e416414c2fd10fc33c6266deae95c9b4a7eea50b7c32c351c33dda1774
4
+ data.tar.gz: 4f6b0b2c01c54948d8b03565e54380e41312c49716a948ebf6f0b01754c1cd2a
5
5
  SHA512:
6
- metadata.gz: 2b7c727b9efc943c26fafaf5a7a6ed932942847eae6188e49aab769716da9fb43a39d912d854ab2263a428bd2e620e2942455f999b46f3c354f16fc11d8843f1
7
- data.tar.gz: a243deaf319517dc8705aa34e01b48df312dd3ac8e497c1555b41bb8e0bd982f523f4163187ac841343a1ba921da5cb456c641dfc09680778af2777ce52db17e
6
+ metadata.gz: dcf2d1ecbf818c83d69cfdd2f9ac7367e219a31d81e777535dfee1417752536b270b3d17041e93d170da216c8acbd409c0adfab10fabbbcd8388d3c4ce497cbb
7
+ data.tar.gz: 55464b134bd0e8e38abf895ec2c29551838e342d08acfbf1e867ca1f0293be00d62ebdd98572203fa8b11007528877e04cf86e4259b959969c5a2496c4ee1d71
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # Anonymise
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/anonymise`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
-
2
+ Anonymise you postgres database
6
3
  ## Installation
7
4
 
8
5
  Add this line to your application's Gemfile:
@@ -19,20 +16,31 @@ Or install it yourself as:
19
16
 
20
17
  $ gem install anonymise
21
18
  Create `anonymise.yml` file in your local machine
22
- Add below code to `/anonymise.yml`
19
+
23
20
  ```$xslt
21
+
24
22
  {
25
- name: ["first_name","last_name","name"],
26
- internet: ["email","url"],
27
- address: ["street"]
23
+ users: {email: 'email',last_name: 'string'},
28
24
  }
25
+
29
26
  ```
30
- `anonymise.yml` file will holder customer column names that need to be anonymised.
31
- The column names are group in categories according to [Faker class names](https://github.com/faker-ruby/faker#default)
27
+ `users` is a table in the database you want to anonymise
28
+
29
+ `email: 'email'` defines a column in users that has data type email
32
30
 
31
+ `last_name: 'string''` defines a column is users table that has data type string
32
+
33
+ Datatypes that can be used:-
34
+ ```aidl
35
+ 1. email
36
+ 2. string
37
+ 3. number
38
+ 4. mobile
39
+ 5. url
40
+ ```
33
41
  ## Usage
34
42
  ```$xslt
35
- $ bundle exec rake anonymise:start db:dbname path:path_to_anonymise.yml
43
+ $ anonymise fake db:dbname path:path_to_anonymise.yml
36
44
  ```
37
45
 
38
46
  ## Development
data/anonymise.gemspec CHANGED
@@ -25,11 +25,11 @@ Gem::Specification.new do |spec|
25
25
  spec.metadata['homepage_uri'] = spec.homepage
26
26
  spec.metadata['source_code_uri'] = spec.homepage
27
27
 
28
+ spec.add_runtime_dependency 'bundler', '~> 2.1.2'
28
29
  spec.add_runtime_dependency('colorize', '0.8.1')
29
- spec.add_runtime_dependency 'thor', '1.0.1'
30
30
  spec.add_runtime_dependency 'faker', '2.9.0'
31
31
  spec.add_runtime_dependency 'pg', '1.1.4'
32
- spec.add_runtime_dependency 'bundler', '~> 2.1.2'
32
+ spec.add_runtime_dependency 'thor', '1.0.1'
33
33
 
34
34
  spec.add_development_dependency 'rake', '~> 10.0'
35
35
  spec.add_development_dependency 'rspec', '~> 3.0'
data/anonymise.yml CHANGED
@@ -1 +1 @@
1
- {
2
1
  name: ["first_name","last_name","name"],
3
2
  internet: ["email","url"],
4
3
  address: ["street"]
4
+ {
5
5
  users: {email: 'email',last_name: 'string'},
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg'
4
+ require 'colorize'
5
+ module Anonymise
6
+ module DbConnection
7
+ def t_ables
8
+ t_ables = connection.exec "SELECT table_name FROM information_schema.tables
9
+ WHERE table_schema = 'public'"
10
+ @t_ables ||= t_ables.map { |m| m['table_name'] }
11
+ end
12
+
13
+ def column_exists?(table, column)
14
+ res = connection.exec('SELECT TRUE FROM pg_attribute WHERE attrelid = (SELECT c.oid
15
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
16
+ WHERE n.nspname = CURRENT_SCHEMA() AND c.relname =\''"#{table}"'\')
17
+ AND attname = \''"#{column}"'\' AND NOT attisdropped AND attnum > 0')
18
+ records_count = res.count
19
+ records_count.positive?
20
+ end
21
+
22
+ def connection
23
+ @connection ||= PG.connect db_args
24
+ rescue PG::Error => e
25
+ puts e.message.colorize(:red)
26
+ exit
27
+ end
28
+ end
29
+ end
@@ -1,89 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'faker'
4
- require 'pg'
5
4
  require 'colorize'
5
+ require 'anonymise/db_connection'
6
6
  module Anonymise
7
7
  class DbFaker
8
- def initialize(db_args, path)
8
+ include Anonymise::DbConnection
9
+ attr_reader :config, :db_args
10
+ def initialize(db_args, config)
9
11
  @db_args = db_args
10
- @path = path
12
+ @config = config
11
13
  end
12
14
 
13
15
  def fake
14
- tables.each do |table|
15
- next if excluded_tables.include?(table['table_name'])
16
-
17
- puts "Anonymising #{table['table_name']}_table".colorize(:green)
18
- columns.each do |key, values|
19
- values.each do |column|
20
- next unless column_exists?(table['table_name'], column)
21
-
22
- available_data = connection.exec("select * from #{table['table_name']}")
23
- available_data.each do |record|
24
- klass_data = Object.const_get("Faker::#{key.capitalize}").send(column.to_sym)
25
-
26
- unless column == 'email'
27
- klass_data = klass_data.gsub(/[^a-z ]/, '')
16
+ config.each do |table, columns|
17
+ if t_ables.include?(table)
18
+ puts "Anonymising table #{table}".colorize(:green)
19
+ columns.each do |column, type|
20
+ if column_exists?(table, column)
21
+ available_data = connection.exec("select * from #{table}")
22
+ available_data.each do |record|
23
+ val = Faker::Alphanumeric.unique.alpha(number: 6)
24
+
25
+ val = val.gsub(/[^a-z ]/, '')
26
+
27
+ val = Faker::Internet.unique.email if type == 'email'
28
+ val = Faker::Internet.unique.url if type == 'url'
29
+ val = Faker::PhoneNumber.unique.cell if type == 'mobile'
30
+ if type == 'number'
31
+ val = Faker::Faker::Number.unique.number(digits: 6)
32
+ end
33
+ connection.exec("UPDATE #{table} SET #{column}='#{val}' WHERE id='#{record['id']}'")
28
34
  end
29
- connection.exec("UPDATE #{table['table_name']} SET #{column}='#{klass_data}' WHERE id='#{record['id']}'")
35
+ else
36
+ puts "Column #{column} does not exist in #{table}_table".colorize(:red)
30
37
  end
31
38
  end
39
+ else
40
+ puts "Table #{table} does not exist on #{db_args[:db]}".colorize(:red)
32
41
  end
33
42
  end
34
- connection&.close
35
- end
36
43
 
37
- private
38
-
39
- def connection
40
- @connection ||= PG.connect @db_args
41
- rescue PG::Error => e
42
- puts e.message.colorize(:red)
43
- end
44
-
45
- def columns
46
- @columns ||= if configured_columns.nil?
47
- {
48
- name: %w[
49
- first_name
50
- last_name
51
- name
52
- ],
53
- internet: %w[
54
- email
55
- url
56
- ]
57
- }
58
- else
59
- configured_columns
60
- end
61
- end
62
-
63
- def configured_columns
64
- @configured_columns ||= if !@path.nil? && File.exist?(@path)
65
- YAML.safe_load(File.open(@path))
66
- else
67
- []
68
- end
69
- end
70
-
71
- def tables
72
- @tables ||= connection.exec "SELECT table_name FROM information_schema.tables
73
- WHERE table_schema = 'public'"
74
- end
75
-
76
- def excluded_tables
77
- @excluded_tables ||= %w[schema_migrations ar_internal_metadata]
78
- end
79
-
80
- def column_exists?(table, column)
81
- res = connection.exec('SELECT TRUE FROM pg_attribute WHERE attrelid = (SELECT c.oid
82
- FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
83
- WHERE n.nspname = CURRENT_SCHEMA() AND c.relname =\''"#{table}"'\')
84
- AND attname = \''"#{column}"'\' AND NOT attisdropped AND attnum > 0')
85
- records_count = res.count
86
- records_count.positive?
44
+ connection&.close
87
45
  end
88
46
  end
89
47
  end
@@ -17,6 +17,26 @@ module Anonymise
17
17
  @db_name ||= opt['db']
18
18
  end
19
19
 
20
+ def config
21
+ if opt['path'].nil?
22
+ puts 'Path to config file is required'.colorize(:red)
23
+ exit
24
+ end
25
+
26
+ unless File.exist?(opt['path'])
27
+ puts 'File does not exist'.colorize(:red)
28
+ exit
29
+ end
30
+ @content = YAML.safe_load(File.open(opt['path']))
31
+
32
+ if @content.empty?
33
+ puts 'Config file is empty kindly check https://github.com/thirunjuguna/anonymise/blob/master/anonymise.yml'
34
+ exit
35
+ end
36
+
37
+ @config ||= @content
38
+ end
39
+
20
40
  def user
21
41
  @user ||= opt['user']
22
42
  end
@@ -41,8 +61,7 @@ module Anonymise
41
61
  port: @port,
42
62
  host: @host
43
63
  }
44
- path = opt['path']
45
- klass = Anonymise::DbFaker.new(db_args, path)
64
+ klass = Anonymise::DbFaker.new(db_args, config)
46
65
  klass.fake
47
66
  end
48
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anonymise
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
data/lib/anonymise.rb CHANGED
@@ -17,7 +17,7 @@ module Anonymise
17
17
  say "Anonymise version #{Anonymise::VERSION}"
18
18
  end
19
19
 
20
- register Anonymise::Invoke, 'start', 'db:db_name path:path_to_config_file', 'Anonymise Database'
20
+ register Anonymise::Invoke, 'fake', 'fake db:db_name path:path_to_config_file', 'Anonymise Database'
21
21
 
22
22
  def self.exit_on_failure
23
23
  true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anonymise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thiru Njuguna
@@ -11,33 +11,33 @@ cert_chain: []
11
11
  date: 2019-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: colorize
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.8.1
19
+ version: 2.1.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.8.1
26
+ version: 2.1.2
27
27
  - !ruby/object:Gem::Dependency
28
- name: thor
28
+ name: colorize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.1
33
+ version: 0.8.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 1.0.1
40
+ version: 0.8.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: faker
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -67,19 +67,19 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.1.4
69
69
  - !ruby/object:Gem::Dependency
70
- name: bundler
70
+ name: thor
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: 2.1.2
75
+ version: 1.0.1
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: 2.1.2
82
+ version: 1.0.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -131,6 +131,7 @@ files:
131
131
  - bin/console
132
132
  - bin/setup
133
133
  - lib/anonymise.rb
134
+ - lib/anonymise/db_connection.rb
134
135
  - lib/anonymise/db_faker.rb
135
136
  - lib/anonymise/invoke.rb
136
137
  - lib/anonymise/version.rb