yaml_db_anonymizer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/Guardfile +13 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/Rakefile +7 -0
- data/lib/tasks/yaml_db_anonymizer.rake +8 -0
- data/lib/yaml_db/anonymizer/dump.rb +33 -0
- data/lib/yaml_db/anonymizer/helper.rb +19 -0
- data/lib/yaml_db/anonymizer/railtie.rb +9 -0
- data/lib/yaml_db/anonymizer/version.rb +5 -0
- data/lib/yaml_db/anonymizer.rb +43 -0
- data/lib/yaml_db_anonymizer.rb +1 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/yaml_db/anonymizer_spec.rb +91 -0
- data/yaml_db_anonymizer.gemspec +24 -0
- metadata +145 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color -f documentation --drb
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
guard 'spork' do
|
2
|
+
watch('Gemfile')
|
3
|
+
watch('Gemfile.lock')
|
4
|
+
watch('spec/spec_helper.rb') { :rspec }
|
5
|
+
end
|
6
|
+
|
7
|
+
guard 'rspec', :version => 2 do
|
8
|
+
watch(%r{^spec/.+_spec\.rb$})
|
9
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
10
|
+
watch('spec/spec_helper.rb') { "spec" }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Thomas Hollstegge
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# YamlDb::Anonymizer
|
2
|
+
|
3
|
+
Dumps anonymized database contents to a YAML file. This is useful if you want to develop Rails applications with near-live data from your server. Based on [yaml_db](https://github.com/ludicast/yaml_db).
|
4
|
+
|
5
|
+
(c) 2012 by Thomas Hollstegge
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'yaml_db_anonymizer'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install yaml_db_anonymizer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
* Specify the database fields to anonymize, e.g. in
|
24
|
+
`config/initializers/yaml_db_anonymizer.rb`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
YamlDb::Anonymizer.define do
|
28
|
+
table :users
|
29
|
+
remove :encrypted_password
|
30
|
+
replace :name, with: 'John Doe'
|
31
|
+
replace :phone, with: ->(phone) { phone.to_s[0..-3] + '***' }
|
32
|
+
end
|
33
|
+
|
34
|
+
table :logs do
|
35
|
+
truncate
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
* Run `rake db:data:dump_anonymized` on your server. This creates an anonymized dump in `db/data.yml`
|
41
|
+
|
42
|
+
* Copy `db/data.yml` to your local machine
|
43
|
+
|
44
|
+
* Run `rake db:data:load` on your local machine.
|
45
|
+
|
46
|
+
## Todo
|
47
|
+
|
48
|
+
* Provide capistrano integration
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
MIT License. Copyright 2012 by Thomas Hollstegge
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module YamlDb
|
2
|
+
module Anonymizer
|
3
|
+
class Dump < ::YamlDb::Dump
|
4
|
+
|
5
|
+
def self.dump_table_records(io, table)
|
6
|
+
return if Anonymizer.definition[table.to_s] == :truncate
|
7
|
+
table_record_header(io)
|
8
|
+
|
9
|
+
column_names = table_column_names(table)
|
10
|
+
|
11
|
+
each_table_page(table) do |records|
|
12
|
+
rows = SerializationHelper::Utils.unhash_records(records, column_names)
|
13
|
+
records_anonymized = records.map do |record|
|
14
|
+
# FIXME Why isn't there a map_with_index?
|
15
|
+
record_anonymized = []
|
16
|
+
record.each_with_index do |value, i|
|
17
|
+
record_anonymized << Anonymizer.anonymize(table, column_names[i], value)
|
18
|
+
end
|
19
|
+
record_anonymized
|
20
|
+
end
|
21
|
+
io.write(YamlDb::Utils.chunk_records(records_anonymized))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.dump_table_columns(io, table)
|
26
|
+
super(io, table) unless Anonymizer.definition[table.to_s] == :truncate
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yaml_db'
|
2
|
+
require 'yaml_db/anonymizer/railtie'
|
3
|
+
|
4
|
+
module YamlDb
|
5
|
+
module Anonymizer
|
6
|
+
autoload :Dump, 'yaml_db/anonymizer/dump'
|
7
|
+
autoload :Helper, 'yaml_db/anonymizer/helper'
|
8
|
+
autoload :VERSION, 'yaml_db/anonymizer/version'
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :definition
|
12
|
+
|
13
|
+
def define(*args, &block)
|
14
|
+
self.definition = {}
|
15
|
+
class_eval &block
|
16
|
+
end
|
17
|
+
|
18
|
+
def table(table_name, &block)
|
19
|
+
@table_name = table_name.to_s
|
20
|
+
self.definition[@table_name] = {}
|
21
|
+
class_eval &block
|
22
|
+
end
|
23
|
+
|
24
|
+
def truncate
|
25
|
+
self.definition[@table_name] = :truncate
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove(column_name)
|
29
|
+
self.definition[@table_name][column_name.to_s] = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def replace(column_name, opts={})
|
33
|
+
self.definition[@table_name][column_name.to_s] = opts[:with]
|
34
|
+
end
|
35
|
+
|
36
|
+
def anonymize(table, column, value)
|
37
|
+
return value unless self.definition.key?(table.to_s) and self.definition[table.to_s].key?(column.to_s)
|
38
|
+
block_or_value = self.definition[table.to_s][column.to_s]
|
39
|
+
block_or_value.is_a?(Proc) ? block_or_value.call(value) : block_or_value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'yaml_db/anonymizer'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe YamlDb::Anonymizer do
|
4
|
+
context 'definition' do
|
5
|
+
before do
|
6
|
+
YamlDb::Anonymizer.define do
|
7
|
+
table :mytable do
|
8
|
+
remove :a
|
9
|
+
replace :b, with: 0
|
10
|
+
replace :c, with: lambda { |val| "Anonymized #{val}" }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should anonymize" do
|
16
|
+
YamlDb::Anonymizer.anonymize(:mytable, :a, "foo").should == nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should anonymize with simple values" do
|
20
|
+
YamlDb::Anonymizer.anonymize(:mytable, :b, "foo").should == 0
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should anonymize with blocks" do
|
24
|
+
YamlDb::Anonymizer.anonymize(:mytable, :c, "bar").should == "Anonymized bar"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'export' do
|
29
|
+
before do
|
30
|
+
# Taken from yaml_db specs
|
31
|
+
silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base').as_null_object }
|
32
|
+
ActiveRecord::Base.stub(:connection).and_return(stub('connection').as_null_object)
|
33
|
+
ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ])
|
34
|
+
ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([ mock('a',:name => 'a', :type => :string), mock('b', :name => 'b', :type => :string) ])
|
35
|
+
ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"})
|
36
|
+
ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ])
|
37
|
+
YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
|
38
|
+
end
|
39
|
+
|
40
|
+
before(:each) do
|
41
|
+
# Taken from yaml_db specs
|
42
|
+
File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new)
|
43
|
+
@io = StringIO.new
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'truncated' do
|
47
|
+
before do
|
48
|
+
YamlDb::Anonymizer.define do
|
49
|
+
table :mytable do
|
50
|
+
truncate
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should not export headers for truncated tables' do
|
56
|
+
YamlDb::Anonymizer::Dump.dump_table_columns(@io, 'mytable')
|
57
|
+
@io.rewind
|
58
|
+
@io.read.should == ""
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should not export records for truncated tables' do
|
62
|
+
YamlDb::Anonymizer::Dump.dump_table_records(@io, 'mytable')
|
63
|
+
@io.rewind
|
64
|
+
@io.read.should == ""
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'anonymized' do
|
69
|
+
before do
|
70
|
+
YamlDb::Anonymizer.define do
|
71
|
+
table :mytable do
|
72
|
+
replace 'a', with: 0
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should anonymize the dump with the given schema' do
|
78
|
+
YamlDb::Anonymizer::Dump.dump_table_records(@io, 'mytable')
|
79
|
+
@io.rewind
|
80
|
+
@io.read.should == <<EOYAML
|
81
|
+
records:
|
82
|
+
- - 0
|
83
|
+
- 2
|
84
|
+
- - 0
|
85
|
+
- 4
|
86
|
+
EOYAML
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/yaml_db/anonymizer/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Thomas Hollstegge"]
|
6
|
+
gem.email = ["thomas@hollstegge.net"]
|
7
|
+
gem.description = %q{Dumps anonymized database contents to .yml files}
|
8
|
+
gem.summary = %q{A database dumper with anonymization}
|
9
|
+
gem.homepage = "https://github.com/Tho85/yaml_db_anonymizer"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "yaml_db_anonymizer"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = YamlDb::Anonymizer::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'yaml_db', '= 0.2.3'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec', '~> 2.11.0'
|
21
|
+
gem.add_development_dependency 'rails', '~> 3.2'
|
22
|
+
gem.add_development_dependency 'guard-rspec', '~> 1.1.0'
|
23
|
+
gem.add_development_dependency 'guard-spork', '~> 1.1.0'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml_db_anonymizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Hollstegge
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: yaml_db
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.2.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.2.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.11.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.11.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.2'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard-rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.1.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.1.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-spork
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.1.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.1.0
|
94
|
+
description: Dumps anonymized database contents to .yml files
|
95
|
+
email:
|
96
|
+
- thomas@hollstegge.net
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- .travis.yml
|
104
|
+
- Gemfile
|
105
|
+
- Guardfile
|
106
|
+
- LICENSE
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/tasks/yaml_db_anonymizer.rake
|
110
|
+
- lib/yaml_db/anonymizer.rb
|
111
|
+
- lib/yaml_db/anonymizer/dump.rb
|
112
|
+
- lib/yaml_db/anonymizer/helper.rb
|
113
|
+
- lib/yaml_db/anonymizer/railtie.rb
|
114
|
+
- lib/yaml_db/anonymizer/version.rb
|
115
|
+
- lib/yaml_db_anonymizer.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/yaml_db/anonymizer_spec.rb
|
118
|
+
- yaml_db_anonymizer.gemspec
|
119
|
+
homepage: https://github.com/Tho85/yaml_db_anonymizer
|
120
|
+
licenses: []
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.8.21
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: A database dumper with anonymization
|
143
|
+
test_files:
|
144
|
+
- spec/spec_helper.rb
|
145
|
+
- spec/yaml_db/anonymizer_spec.rb
|