data_anonymizer 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a269e2dc155681d60c66c2c563de4265e72d5cc3
4
+ data.tar.gz: 2c718ea1852ba175eb7b656d7e391673eda081fa
5
+ SHA512:
6
+ metadata.gz: 236ea25c6b82e0884b76039a7f5c8dba0d21badab55075756d813758ce107fa2409e26c9830c98f18465235ce3cdef3ae39927a41ab5ffe578387cf06e84e231
7
+ data.tar.gz: cfe50fb54ed353ff8e6d4ad3c591744b978a61d74eb72c5a804a962777a9d381a870cd97d1f745c4b3d197622c177979517baa80532b676152d4a25d54dc44c1
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in data_anonymizer.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data_anonymizer (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ data_anonymizer!
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Pranab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DataAnonymizer
2
+
3
+ Anonymize Data containing Personally Identifiable Information
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'data_anonymizer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install data_anonymizer
20
+
21
+ ## Usage
22
+
23
+ `DataAnonymizer.instance.anonymize_common_attributes(["Person", "CensusMember", "CuramUser"], ["first_name", "last_name", "ssn"])`
24
+
25
+ where ["Person", "CensusMember", "CuramUser"] are the model names and ["first_name", "last_name", "ssn"] are the common attributes in these models that we want to anonymize and sync up.
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/data_anonymizer.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "data_anonymizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "data_anonymizer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data_anonymizer"
8
+ spec.version = DataAnonymizer::VERSION
9
+ spec.authors = ["Pranab"]
10
+ spec.email = ["pranab.khanal@gmail.com"]
11
+
12
+ spec.summary = %q{Anonymize Data}
13
+ spec.description = %q{Anonymize Data containing Personally Identifiable Information}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,85 @@
1
+ require "data_anonymizer/version"
2
+ require 'singleton'
3
+
4
+ module DataAnonymizer
5
+ class Sanitize
6
+ include Singleton
7
+
8
+ @@models_to_anonymize = nil
9
+ @@attributes_to_sync = nil
10
+
11
+ NUMBERS = ('0'..'9').to_a
12
+ ALPHABETS = ("a".."z").to_a
13
+
14
+ #model_names = ["Person", "CensusMember", "CuramUser"]
15
+ #sync_attributes = ["first_name", "last_name", "ssn"]
16
+
17
+ def anonymize_common_attributes(model_names, sync_attributes)
18
+ return if model_names.blank? || sync_attributes.blank?
19
+
20
+ @@models_to_anonymize = process_model_names(model_names)
21
+ @@attributes_to_sync = process_sync_attributes(sync_attributes)
22
+
23
+ puts 'Data Anonymization Started...'
24
+ puts '*' * 85
25
+ # Update attributes that need to be in sync accross Models.
26
+ @@attributes_to_sync.each do |attribute|
27
+ puts "Anonymizing attribute: #{attribute} across #{@@models_to_anonymize}"
28
+ cipher_hash = build_substitution_cipher_hash_for(attribute)
29
+ @@models_to_anonymize.each do |model|
30
+ puts " - Processing #{model} Model"
31
+ model.constantize.each do |instance|
32
+ updated = false
33
+ while !updated do
34
+ begin
35
+ instance.update_attributes(attribute => encrypt_characters(cipher_hash, instance.send(attribute))) if instance.send(attribute).present?
36
+ updated = true
37
+ rescue Exception => e
38
+ puts "Error updating [#{attribute}] for [#{instance.class}, ID: #{instance.id}]. Retrying with a new shuffled value.. (Error Message: #{e.message})"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ puts '*' * 85
45
+ puts 'Data Anonymization Complete!!!'
46
+ end
47
+
48
+ private
49
+
50
+ def process_model_names(model_names)
51
+ model_names.map { |m| m.to_s }
52
+ end
53
+
54
+ def process_sync_attributes(sync_attributes)
55
+ sync_attributes
56
+ end
57
+
58
+ def build_substitution_cipher_hash_for(attribute)
59
+ if attribute.to_sym == :ssn
60
+ loop do
61
+ # Keep Shuffling until SSN is unique
62
+ shuffled_chars = NUMBERS.shuffle
63
+ return NUMBERS.zip(shuffled_chars).to_h if no_ssn_collision?(shuffled_chars)
64
+ end
65
+ else
66
+ shuffled_chars = ALPHABETS.shuffle
67
+ ALPHABETS.zip(shuffled_chars).to_h
68
+ end
69
+ end
70
+
71
+ def encrypt_characters(cipher_hash, attribute_value)
72
+ encrypted_value = ""
73
+ attribute_value.gsub!(/[^0-9A-Za-z]/, '') # remove non-alphanumeric characters from string.
74
+ attribute_value.downcase.split('').each {|char| encrypted_value << cipher_hash[char]}
75
+ encrypted_value
76
+ end
77
+
78
+ def no_ssn_collision?(shuffled_chars)
79
+ @@models_to_anonymize.each do |model|
80
+ return false if model.constantize.where(ssn: shuffled_chars).present?
81
+ end
82
+ return true
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module DataAnonymizer
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_anonymizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pranab
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Anonymize Data containing Personally Identifiable Information
42
+ email:
43
+ - pranab.khanal@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - data_anonymizer.gemspec
57
+ - lib/data_anonymizer.rb
58
+ - lib/data_anonymizer/version.rb
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.5.1
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Anonymize Data
83
+ test_files: []