pg_anonymize 0.2.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +20 -0
- data/.rubocop_todo.yml +49 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Guardfile +96 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/pg_anonymize/anonymizer.rb +95 -0
- data/lib/pg_anonymize/version.rb +5 -0
- data/lib/pg_anonymize.rb +68 -0
- data/pg_anonymize-0.1.0.gem +0 -0
- data/pg_anonymize.gemspec +34 -0
- metadata +195 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4088a76938fa78627377bc7f537235f7a8351b347ff49f864865483f7885f5fe
|
4
|
+
data.tar.gz: 87ff397405e34766842d3fcf8863a69918e15bebf0bcd0cec920e33d9c9184bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d98e6b6493eba573c4f8bb2e97458f961403e0de196f5bd5431165be535a8107b9f75234bae928f063a9c997b464b40a72c13ec08f21ebc743a124886283ea1
|
7
|
+
data.tar.gz: 556f83985d17cf60791ade0cc549ed0c3a307e28426b2462a98da6f47d7af28ac8acab05c992d36c75ef55a0c66b5d20a63d18e3dc82bdf7400ac7686224a268
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.4
|
5
|
+
|
6
|
+
Include:
|
7
|
+
- '**/*.rake'
|
8
|
+
- '**/config.ru'
|
9
|
+
- '**/Gemfile'
|
10
|
+
- '**/Rakefile'
|
11
|
+
- '**/Guardfile'
|
12
|
+
|
13
|
+
|
14
|
+
Metrics/BlockLength:
|
15
|
+
Exclude:
|
16
|
+
- 'pg_anonymize.gemspec'
|
17
|
+
|
18
|
+
Style/Documentation:
|
19
|
+
Enabled:
|
20
|
+
false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-11-05 13:39:03 -0500 using RuboCop version 0.51.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
# Configuration parameters: AllowSafeAssignment.
|
11
|
+
Lint/AssignmentInCondition:
|
12
|
+
Exclude:
|
13
|
+
- 'lib/pg_anonymize/anonymizer.rb'
|
14
|
+
|
15
|
+
# Offense count: 1
|
16
|
+
Lint/UselessAssignment:
|
17
|
+
Exclude:
|
18
|
+
- 'lib/pg_anonymize/anonymizer.rb'
|
19
|
+
|
20
|
+
# Offense count: 1
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Max: 46
|
23
|
+
|
24
|
+
# Offense count: 2
|
25
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
26
|
+
Metrics/BlockLength:
|
27
|
+
Max: 43
|
28
|
+
|
29
|
+
# Offense count: 1
|
30
|
+
Metrics/CyclomaticComplexity:
|
31
|
+
Max: 7
|
32
|
+
|
33
|
+
# Offense count: 1
|
34
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
35
|
+
# URISchemes: http, https
|
36
|
+
Metrics/LineLength:
|
37
|
+
Max: 218
|
38
|
+
|
39
|
+
# Offense count: 1
|
40
|
+
# Configuration parameters: CountComments.
|
41
|
+
Metrics/MethodLength:
|
42
|
+
Max: 21
|
43
|
+
|
44
|
+
# Offense count: 1
|
45
|
+
# Cop supports --auto-correct.
|
46
|
+
# Configuration parameters: AllowAsExpressionSeparator.
|
47
|
+
Style/Semicolon:
|
48
|
+
Exclude:
|
49
|
+
- 'lib/pg_anonymize/anonymizer.rb'
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.2
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at akashani62@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A sample Guardfile
|
4
|
+
# More info at https://github.com/guard/guard#readme
|
5
|
+
|
6
|
+
## Uncomment and set this to only include directories you want to watch
|
7
|
+
# directories %w(app lib config test spec features) \
|
8
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
9
|
+
|
10
|
+
## Note: if you are using the `directories` clause above and you are not
|
11
|
+
## watching the project directory ('.'), then you will want to move
|
12
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
13
|
+
#
|
14
|
+
# $ mkdir config
|
15
|
+
# $ mv Guardfile config/
|
16
|
+
# $ ln -s config/Guardfile .
|
17
|
+
#
|
18
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
19
|
+
|
20
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
21
|
+
# rspec may be run, below are examples of the most common uses.
|
22
|
+
# * bundler: 'bundle exec rspec'
|
23
|
+
# * bundler binstubs: 'bin/rspec'
|
24
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
25
|
+
# installed the spring binstubs per the docs)
|
26
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
27
|
+
# * 'just' rspec: 'rspec'
|
28
|
+
|
29
|
+
clearing :on
|
30
|
+
notification :terminal_notifier if `uname`.match?(/Darwin/)
|
31
|
+
group :red_green_refactor, halt_on_fail: true do
|
32
|
+
guard :rspec, cmd: 'rspec' do
|
33
|
+
callback(:start_begin) { `mate .` }
|
34
|
+
|
35
|
+
require 'guard/rspec/dsl'
|
36
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
37
|
+
|
38
|
+
# Feel free to open issues for suggestions and improvements
|
39
|
+
|
40
|
+
# RSpec files
|
41
|
+
rspec = dsl.rspec
|
42
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
43
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
44
|
+
watch(rspec.spec_files)
|
45
|
+
|
46
|
+
# Ruby files
|
47
|
+
ruby = dsl.ruby
|
48
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
49
|
+
|
50
|
+
# Rails files
|
51
|
+
rails = dsl.rails(view_extensions: %w[erb haml slim])
|
52
|
+
dsl.watch_spec_files_for(rails.app_files)
|
53
|
+
dsl.watch_spec_files_for(rails.views)
|
54
|
+
|
55
|
+
watch(rails.controllers) do |m|
|
56
|
+
[
|
57
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
58
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
59
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
60
|
+
]
|
61
|
+
end
|
62
|
+
|
63
|
+
# Rails config changes
|
64
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
65
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
66
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
67
|
+
|
68
|
+
# Capybara features specs
|
69
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
70
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
71
|
+
|
72
|
+
# Turnip features and steps
|
73
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
74
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
75
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
guard :bundler do
|
80
|
+
require 'guard/bundler'
|
81
|
+
require 'guard/bundler/verify'
|
82
|
+
helper = Guard::Bundler::Verify.new
|
83
|
+
|
84
|
+
files = ['Gemfile']
|
85
|
+
files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }
|
86
|
+
|
87
|
+
# Assume files are symlinked from somewhere
|
88
|
+
files.each { |file| watch(helper.real_path(file)) }
|
89
|
+
end
|
90
|
+
|
91
|
+
guard :rubocop, cli: ['--format', 'clang', '-D', '-E', '-a'] do
|
92
|
+
watch(/.+\.rb$/)
|
93
|
+
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
|
94
|
+
watch(/pg_anonymize.gemspec/)
|
95
|
+
end
|
96
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Arash Kashani
|
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,43 @@
|
|
1
|
+
# PgAnonymize
|
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/pg_anonymize`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'pg_anonymize'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install pg_anonymize
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. 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]/pg_anonymize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the PgAnonymize project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pg_anonymize/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'pg_anonymize'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PgAnonymize
|
4
|
+
class Anonymizer
|
5
|
+
class << ::ActiveRecord::Base
|
6
|
+
def do
|
7
|
+
puts "\n\n\n\n\nBitch Please!\n\n\n\n"
|
8
|
+
end
|
9
|
+
|
10
|
+
def anonymize_with(options)
|
11
|
+
options[:skip_columns]&.each { |cn| skip_columns << cn }
|
12
|
+
options[:replace_columns]&.each { |cn| replace_columns << cn }
|
13
|
+
@after_anonymize = options[:after] if options[:after]
|
14
|
+
end
|
15
|
+
|
16
|
+
def skip_columns
|
17
|
+
@skip_columns ||= []
|
18
|
+
end
|
19
|
+
|
20
|
+
def replace_columns
|
21
|
+
@replace_columns ||= []
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :after_anonymize
|
25
|
+
|
26
|
+
def skip_on_anonymize
|
27
|
+
@skip_on_anonymize = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def skip_on_anonymize?
|
31
|
+
@skip_on_anonymize
|
32
|
+
end
|
33
|
+
|
34
|
+
def dump_anonymized
|
35
|
+
Rails.application.eager_load!
|
36
|
+
klasses = ActiveRecord::Base.descendants - [Delayed::Backend::ActiveRecord::Job, Doorkeeper::AccessGrant, Doorkeeper::AccessToken, Doorkeeper::Application, WaitFor, AppLog]
|
37
|
+
klasses.each do |klass|
|
38
|
+
(puts "-- skipping #{name}" if klass.skip_on_anonymize?; next) if klass.abstract_class? || klass.skip_on_anonymize?
|
39
|
+
puts "-- #{klass.name} replace_columns: #{klass.replace_columns}, skip_columns: #{klass.skip_columns}"
|
40
|
+
conn = klass.connection.raw_connection
|
41
|
+
puts "DELETE FROM #{klass.table_name};"
|
42
|
+
puts "COPY #{klass.table_name} (#{(klass.column_names - klass.skip_columns).map { |c| "\"#{c}\"" }.join(', ')}) FROM STDIN;"
|
43
|
+
klass.replace_columns.tap do |rc|
|
44
|
+
conn.exec("COPY (SELECT #{(klass.column_names - klass.skip_columns).map { |cn| "#{rc.include?(cn.to_sym) ? 'md5(random()::text)' : "\"#{cn}\""} AS #{cn}" }.join(', ')} FROM #{klass.table_name}) TO STDOUT;")
|
45
|
+
end
|
46
|
+
buf = ''
|
47
|
+
$stdout.puts(buf) while buf = conn.get_copy_data
|
48
|
+
puts '\\.'
|
49
|
+
end
|
50
|
+
|
51
|
+
klasses.each do |klass|
|
52
|
+
if block = klass.after_anonymize
|
53
|
+
puts "-- #{klass.name}.after_anonymize"
|
54
|
+
puts block.call
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
def sql_text
|
60
|
+
puts "DELETE FROM #{table};"
|
61
|
+
puts "COPY #{table} (#{copy_map}) FROM STDIN;"
|
62
|
+
conn.exec copy
|
63
|
+
$stdout.puts(buf) while conn.get_copy_data
|
64
|
+
puts '\\.'
|
65
|
+
end
|
66
|
+
|
67
|
+
def buf
|
68
|
+
''
|
69
|
+
end
|
70
|
+
|
71
|
+
def copy_map
|
72
|
+
columns.map { |c| "\"#{c}\"" }.join(', ')
|
73
|
+
end
|
74
|
+
|
75
|
+
def copy
|
76
|
+
"COPY (SELECT #{columns} FROM #{table}) TO STDOUT;"
|
77
|
+
end
|
78
|
+
|
79
|
+
def columns
|
80
|
+
columns.map { |_cn| dont_know_what_to_call_this }
|
81
|
+
end
|
82
|
+
|
83
|
+
def dont_know_what_to_call_this
|
84
|
+
rc.include?(cn.to_sym) ? encrypt : dont_encrypt
|
85
|
+
end
|
86
|
+
|
87
|
+
def dont_encrypt
|
88
|
+
"\"#{cn}\""
|
89
|
+
end
|
90
|
+
|
91
|
+
def encrypt
|
92
|
+
'md5(random()::text)'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/pg_anonymize.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pg_anonymize/version'
|
4
|
+
|
5
|
+
module PgAnonymize
|
6
|
+
module ActiveRecordAnonymizer
|
7
|
+
module ClassMethods
|
8
|
+
def anonymize_with(options)
|
9
|
+
options[:skip_columns]&.each { |cn| skip_columns << cn }
|
10
|
+
options[:replace_columns]&.each { |cn| replace_columns << cn }
|
11
|
+
@after_anonymize = options[:after] if options[:after]
|
12
|
+
end
|
13
|
+
|
14
|
+
def skip_columns
|
15
|
+
@skip_columns ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def replace_columns
|
19
|
+
@replace_columns ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
def after_anonymize
|
23
|
+
@after_anonymize
|
24
|
+
end
|
25
|
+
|
26
|
+
def skip_on_anonymize
|
27
|
+
@skip_on_anonymize = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def skip_on_anonymize?
|
31
|
+
@skip_on_anonymize
|
32
|
+
end
|
33
|
+
|
34
|
+
def dump_anonymized
|
35
|
+
Rails.application.eager_load!
|
36
|
+
klasses = ApplicationRecord.descendants - [Delayed::Backend::ActiveRecord::Job, Doorkeeper::AccessGrant, Doorkeeper::AccessToken, Doorkeeper::Application, WaitFor, AppLog]
|
37
|
+
klasses.each do |klass|
|
38
|
+
(puts "-- skipping #{name}" if klass.skip_on_anonymize?; next) if klass.abstract_class? || klass.skip_on_anonymize?
|
39
|
+
puts "-- #{klass.name} replace_columns: #{klass.replace_columns}, skip_columns: #{klass.skip_columns}"
|
40
|
+
conn = klass.connection.raw_connection
|
41
|
+
puts "DELETE FROM #{klass.table_name};"
|
42
|
+
puts "COPY #{klass.table_name} (#{(klass.column_names - klass.skip_columns).map { |c| "\"#{c}\"" }.join(', ')}) FROM STDIN;"
|
43
|
+
klass.replace_columns.tap do |rc|
|
44
|
+
conn.exec("COPY (SELECT #{(klass.column_names - klass.skip_columns).map { |cn| "#{rc.include?(cn.to_sym) ? 'md5(random()::text)' : "\"#{cn}\""} AS #{cn}" }.join(', ')} FROM #{klass.table_name}) TO STDOUT;")
|
45
|
+
end
|
46
|
+
buf = ''
|
47
|
+
$stdout.puts(buf) while buf = conn.get_copy_data
|
48
|
+
puts '\\.'
|
49
|
+
end
|
50
|
+
|
51
|
+
klasses.each do |klass|
|
52
|
+
if block = klass.after_anonymize
|
53
|
+
puts "-- #{klass.name}.after_anonymize"
|
54
|
+
puts block.call
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.included(klass)
|
61
|
+
klass.send :extend, ActiveRecordAnonymizer::ClassMethods
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class ApplicationRecord
|
66
|
+
include ActiveRecordAnonymizer
|
67
|
+
end
|
68
|
+
end
|
Binary file
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'pg_anonymize/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'pg_anonymize'
|
10
|
+
spec.version = PgAnonymize::VERSION
|
11
|
+
spec.authors = ['Arash Kashani']
|
12
|
+
spec.email = ['akashani62@gmail.com']
|
13
|
+
spec.summary = 'Anonymize postgres'
|
14
|
+
spec.description = 'Anonymize postgres to use development data'
|
15
|
+
spec.homepage = 'http://arashkashani.com'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
spec.add_dependency 'rails', '~> 5.1', '>= 5.1.4'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rb-readline'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'guard', '> 0'
|
31
|
+
spec.add_development_dependency 'guard-bundler', '> 0'
|
32
|
+
spec.add_development_dependency 'guard-rspec', '> 0'
|
33
|
+
spec.add_development_dependency 'guard-rubocop', '> 0'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_anonymize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arash Kashani
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 5.1.4
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 5.1.4
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.15'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.15'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rb-readline
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: guard
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: guard-bundler
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: guard-rspec
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: guard-rubocop
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">"
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
description: Anonymize postgres to use development data
|
146
|
+
email:
|
147
|
+
- akashani62@gmail.com
|
148
|
+
executables: []
|
149
|
+
extensions: []
|
150
|
+
extra_rdoc_files: []
|
151
|
+
files:
|
152
|
+
- ".gitignore"
|
153
|
+
- ".rspec"
|
154
|
+
- ".rubocop.yml"
|
155
|
+
- ".rubocop_todo.yml"
|
156
|
+
- ".ruby-version"
|
157
|
+
- ".travis.yml"
|
158
|
+
- CODE_OF_CONDUCT.md
|
159
|
+
- Gemfile
|
160
|
+
- Guardfile
|
161
|
+
- LICENSE.txt
|
162
|
+
- README.md
|
163
|
+
- Rakefile
|
164
|
+
- bin/console
|
165
|
+
- bin/setup
|
166
|
+
- lib/pg_anonymize.rb
|
167
|
+
- lib/pg_anonymize/anonymizer.rb
|
168
|
+
- lib/pg_anonymize/version.rb
|
169
|
+
- pg_anonymize-0.1.0.gem
|
170
|
+
- pg_anonymize.gemspec
|
171
|
+
homepage: http://arashkashani.com
|
172
|
+
licenses:
|
173
|
+
- MIT
|
174
|
+
metadata: {}
|
175
|
+
post_install_message:
|
176
|
+
rdoc_options: []
|
177
|
+
require_paths:
|
178
|
+
- lib
|
179
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
requirements: []
|
190
|
+
rubyforge_project:
|
191
|
+
rubygems_version: 2.7.2
|
192
|
+
signing_key:
|
193
|
+
specification_version: 4
|
194
|
+
summary: Anonymize postgres
|
195
|
+
test_files: []
|