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 +4 -4
- data/README.md +19 -11
- data/anonymise.gemspec +2 -2
- data/anonymise.yml +1 -1
- data/lib/anonymise/db_connection.rb +29 -0
- data/lib/anonymise/db_faker.rb +28 -70
- data/lib/anonymise/invoke.rb +21 -2
- data/lib/anonymise/version.rb +1 -1
- data/lib/anonymise.rb +1 -1
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96aaf3e416414c2fd10fc33c6266deae95c9b4a7eea50b7c32c351c33dda1774
|
4
|
+
data.tar.gz: 4f6b0b2c01c54948d8b03565e54380e41312c49716a948ebf6f0b01754c1cd2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
19
|
+
|
23
20
|
```$xslt
|
21
|
+
|
24
22
|
{
|
25
|
-
|
26
|
-
internet: ["email","url"],
|
27
|
-
address: ["street"]
|
23
|
+
users: {email: 'email',last_name: 'string'},
|
28
24
|
}
|
25
|
+
|
29
26
|
```
|
30
|
-
`
|
31
|
-
|
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
|
-
$
|
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 '
|
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
@@ -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
|
data/lib/anonymise/db_faker.rb
CHANGED
@@ -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
|
-
|
8
|
+
include Anonymise::DbConnection
|
9
|
+
attr_reader :config, :db_args
|
10
|
+
def initialize(db_args, config)
|
9
11
|
@db_args = db_args
|
10
|
-
@
|
12
|
+
@config = config
|
11
13
|
end
|
12
14
|
|
13
15
|
def fake
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/anonymise/invoke.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/anonymise/version.rb
CHANGED
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, '
|
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.
|
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:
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
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:
|
26
|
+
version: 2.1.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: colorize
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
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:
|
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:
|
70
|
+
name: thor
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '='
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
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:
|
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
|