anonymise 0.0.4 → 0.0.5

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: 96aaf3e416414c2fd10fc33c6266deae95c9b4a7eea50b7c32c351c33dda1774
4
- data.tar.gz: 4f6b0b2c01c54948d8b03565e54380e41312c49716a948ebf6f0b01754c1cd2a
3
+ metadata.gz: 36b2fe6689c2ef9209a40f6260c7b3f9d87d5f753285188b229bc0f81f9db829
4
+ data.tar.gz: 4b0007942885a364ba8a4c5a4d4f5ab48dae9e9f555dbd78370cca508ec0db22
5
5
  SHA512:
6
- metadata.gz: dcf2d1ecbf818c83d69cfdd2f9ac7367e219a31d81e777535dfee1417752536b270b3d17041e93d170da216c8acbd409c0adfab10fabbbcd8388d3c4ce497cbb
7
- data.tar.gz: 55464b134bd0e8e38abf895ec2c29551838e342d08acfbf1e867ca1f0293be00d62ebdd98572203fa8b11007528877e04cf86e4259b959969c5a2496c4ee1d71
6
+ metadata.gz: 78cf01d76b8c12c8502193f92ec4c667dabe5c2b989aed4cac3326b2a734488184d1253903b3cb1acb610ea19a48f5b00f821a8238adb26cfc33f7db39e21a8c
7
+ data.tar.gz: 97693e7e11a2d993f33b5d54a8d86bb6e104a811d4930b3fe4d838810360a1905e48534ed8bd4504919bbec6a7d512724adb99c4040d5edefcea5a9fff733512
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Anonymise
2
- Anonymise you postgres database
2
+ Anonymise your postgres database
3
3
  ## Installation
4
4
 
5
5
  Add this line to your application's Gemfile:
@@ -37,6 +37,8 @@ Datatypes that can be used:-
37
37
  3. number
38
38
  4. mobile
39
39
  5. url
40
+ 6. description
41
+ 7. company
40
42
  ```
41
43
  ## Usage
42
44
  ```$xslt
@@ -0,0 +1 @@
1
+ {
2
  users: {email: 'email',firstname: 'firstname', lastname: 'lastname'},
1
3
  verified_emails: {email: 'email'},
2
4
  organisations: {name: 'company', description: 'description' },
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rake'
4
- require 'faker'
5
4
  require 'pg'
6
5
  require 'colorize'
7
6
  require 'thor'
8
7
  require 'thor/group'
9
- require 'anonymise/invoke'
8
+ require 'anonymise/db_faker'
10
9
  require 'anonymise/version'
10
+ require 'anonymise/resource'
11
11
  module Anonymise
12
12
  class Command < Thor
13
13
  desc '-v', 'Show Anonymise version'
@@ -17,10 +17,31 @@ module Anonymise
17
17
  say "Anonymise version #{Anonymise::VERSION}"
18
18
  end
19
19
 
20
- register Anonymise::Invoke, 'fake', 'fake db:db_name path:path_to_config_file', 'Anonymise Database'
20
+ desc 'fake',
21
+ 'Anonymise Database'
22
+ method_option :db, required: true, type: :string, desc: 'Database name to anonymise'
23
+ method_option :path, required: true, type: :string, desc: 'Anonymisation definitions to apply'
24
+ method_option :user, aliases: 'u', default: 'apps', type: :string, desc: 'User to connect as'
25
+ method_option :password, aliases: 'p', default: 'apps', type: :string, desc: 'Password for user'
26
+ method_option :host, aliases: 'h', default: '127.0.0.1', type: :string, desc: 'Database host address'
27
+ method_option :port, aliases: 'P', default: 5432, type: :numeric, desc: 'Port on host'
28
+ method_option :dry_run, default: false, type: :boolean, desc: 'No apply any changes to the database, just return counts'
29
+ def fake
30
+ db_args = { dbname: options.db, user: options.user,
31
+ password: options.password, port: options.port,
32
+ host: options.host }
33
+ puts '*** DRY RUN ***' if options.dry_run
34
+ DbFaker.new(db_args, config(options.path), options.dry_run).fake
35
+ end
21
36
 
22
37
  def self.exit_on_failure
23
38
  true
24
39
  end
40
+
41
+ private
42
+
43
+ def config(path)
44
+ Resource.new(path).content
45
+ end
25
46
  end
26
47
  end
@@ -7,9 +7,10 @@ module Anonymise
7
7
  class DbFaker
8
8
  include Anonymise::DbConnection
9
9
  attr_reader :config, :db_args
10
- def initialize(db_args, config)
10
+ def initialize(db_args, config, dry_run = false)
11
11
  @db_args = db_args
12
12
  @config = config
13
+ @dry_run = dry_run
13
14
  end
14
15
 
15
16
  def fake
@@ -18,24 +19,13 @@ module Anonymise
18
19
  puts "Anonymising table #{table}".colorize(:green)
19
20
  columns.each do |column, type|
20
21
  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']}'")
34
- end
22
+ print("...#{fake_column(column, table, type)} records affected for column #{column}")
35
23
  else
36
- puts "Column #{column} does not exist in #{table}_table".colorize(:red)
24
+ print "...column #{column} does not exist in #{table}_table".colorize(:red)
37
25
  end
26
+ print "\n"
38
27
  end
28
+ print "\n"
39
29
  else
40
30
  puts "Table #{table} does not exist on #{db_args[:db]}".colorize(:red)
41
31
  end
@@ -43,5 +33,40 @@ module Anonymise
43
33
 
44
34
  connection&.close
45
35
  end
36
+
37
+ def get_fake_for_type(type, origin_length)
38
+ val = Faker::Alphanumeric.unique.alpha(number: origin_length)
39
+ val = val.gsub(/[^a-z ]/, '')
40
+
41
+ val = Faker::Company.name if type == 'company'
42
+ val = Faker::Name.last_name if type == 'lastname'
43
+ val = Faker::Name.first_name if type == 'firstname'
44
+ if type == 'description'
45
+ val = Faker::Lorem.paragraph_by_chars(number: 256, supplemental: false)
46
+ end
47
+ val = Faker::Internet.unique.email if type == 'email'
48
+ val = Faker::Internet.unique.url if type == 'url'
49
+ val = Faker::PhoneNumber.unique.cell if type == 'mobile'
50
+ if type == 'number'
51
+ val = Faker::Faker::Number.unique.number(digits: origin_length)
52
+ end
53
+ val.gsub(/'/, '')
54
+ end
55
+
56
+ private
57
+
58
+ def fake_column(column, table, type)
59
+ available_data = connection.exec("select * from #{table}")
60
+ c_row_count = 0
61
+ available_data.each do |record|
62
+ c_row_count += 1
63
+ origin_length = record[column]&.length || 6
64
+ val = get_fake_for_type(type, origin_length)
65
+ unless @dry_run
66
+ connection.exec("UPDATE #{table} SET #{column}='#{val}' WHERE id='#{record['id']}'")
67
+ end
68
+ end
69
+ c_row_count
70
+ end
46
71
  end
47
72
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'colorize'
4
+
5
+ module Anonymise
6
+ class Resource
7
+ attr_reader :path
8
+ def initialize(path)
9
+ @path = path
10
+ end
11
+
12
+ def content
13
+ if @path.nil?
14
+ puts 'Path to config file is required'.colorize(:red)
15
+ exit
16
+ end
17
+ if @path.start_with?('http')
18
+ # get this from the URL via tempfile.
19
+ yml_file = ''
20
+ elsif File.exist?(@path)
21
+ yml_file = File.open(@path)
22
+ else
23
+ puts 'File does not exist'.colorize(:red)
24
+ exit
25
+ end
26
+ content = YAML.safe_load(yml_file)
27
+ yml_file.close
28
+ if content.empty?
29
+ puts 'Config file is empty kindly check https://github.com/thirunjuguna/anonymise/blob/master/anonymise.yml'
30
+ exit
31
+ end
32
+ content
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anonymise
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anonymise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thiru Njuguna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-26 00:00:00.000000000 Z
11
+ date: 2019-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -130,10 +130,11 @@ files:
130
130
  - bin/anonymise
131
131
  - bin/console
132
132
  - bin/setup
133
+ - decksender.yml
133
134
  - lib/anonymise.rb
134
135
  - lib/anonymise/db_connection.rb
135
136
  - lib/anonymise/db_faker.rb
136
- - lib/anonymise/invoke.rb
137
+ - lib/anonymise/resource.rb
137
138
  - lib/anonymise/version.rb
138
139
  homepage: http://github.com/thirunjuguna/anonymise
139
140
  licenses:
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'anonymise/db_faker'
4
- module Anonymise
5
- class Invoke < Thor::Group
6
- include Thor::Actions
7
-
8
- desc 'Anonymise DB'
9
-
10
- argument :opt, type: :hash, desc: 'Options'
11
-
12
- def db_name
13
- if opt['db'].nil?
14
- puts 'Database name to anonymise is required'.colorize(:red)
15
- exit
16
- end
17
- @db_name ||= opt['db']
18
- end
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
-
40
- def user
41
- @user ||= opt['user']
42
- end
43
-
44
- def password
45
- @password ||= opt['password']
46
- end
47
-
48
- def port
49
- @port ||= opt['port']
50
- end
51
-
52
- def host
53
- @host ||= opt['host']
54
- end
55
-
56
- def setup
57
- db_args = {
58
- dbname: @db_name,
59
- user: @user,
60
- password: @password,
61
- port: @port,
62
- host: @host
63
- }
64
- klass = Anonymise::DbFaker.new(db_args, config)
65
- klass.fake
66
- end
67
- end
68
- end