scrubber 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+
23
+ spec/test.db
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dan Pickett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ = scrubber
2
+
3
+ Scrubber.perform do
4
+ scrub_table :users, :unless => proc {|user| user.email =~ /mydomain.com/i } do |user|
5
+ user.scrub :email, :set_to => "user[:id]@example.com"
6
+ user.scrub :credit_card_number, :set_to => ""
7
+ end
8
+ end
9
+
10
+ == Note on Patches/Pull Requests
11
+
12
+ * Fork the project.
13
+ * Make your feature addition or bug fix.
14
+ * Add tests for it. This is important so I don't break it in a
15
+ future version unintentionally.
16
+ * Commit, do not mess with rakefile, version, or history.
17
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2010 Dan Pickett. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "scrubber"
8
+ gem.summary = %Q{A DSL for scrubbing private data}
9
+ gem.description = %Q{Obfuscate your private data using a simple dsl}
10
+ gem.email = "dpickett@enlightsolutions.com"
11
+ gem.homepage = "http://github.com/dpickett/scrubber"
12
+ gem.authors = ["Dan Pickett"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "scrubber #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_record'
4
+
5
+ [
6
+ 'field_transform',
7
+ 'job',
8
+ 'worker'
9
+ ].each do |file|
10
+ require "scrubber/#{file}"
11
+ end
12
+
13
+ module Scrubber
14
+ def self.perform(&block)
15
+ worker = Scrubber::Worker.new
16
+ yield(worker)
17
+ worker.perform
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ module Scrubber
2
+ class FieldTransform
3
+ attr_reader :field, :set_to
4
+ def initialize(field, options = {})
5
+ @field = field.to_s
6
+ unless options.has_key?(:set_to)
7
+ raise "Please specify what you'd like to change the field #{field} to with a :set_to"
8
+ end
9
+ @set_to = options[:set_to]
10
+ end
11
+
12
+ def perform_on(record)
13
+ val = nil
14
+ if @set_to.is_a?(String)
15
+ val = interpolate_string_for(record)
16
+ else
17
+ val = @set_to
18
+ end
19
+
20
+ record.send("#{@field}=", val)
21
+ end
22
+
23
+ private
24
+ def interpolate_string_for(record)
25
+ @set_to.gsub(/\[\:([^\]]*)\]/){|s| record.send(s.gsub(/\[|\]|\:/, '')) }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,55 @@
1
+ module Scrubber
2
+ class Job
3
+ attr_reader :table_name, :conditional_proc, :conditional_proc_type, :field_transforms
4
+ def initialize(table_name, options = {})
5
+ @table_name = table_name.to_s
6
+ unless options.empty?
7
+ @conditional_proc_type = options.has_key?(:unless) ? :unless : :if
8
+ @conditional_proc = options[:unless]
9
+ @field_transforms = []
10
+ end
11
+ end
12
+
13
+ def scrub(field, options)
14
+ @field_transforms << Scrubber::FieldTransform.new(field, options)
15
+ end
16
+
17
+ def perform
18
+ #turn off timestamps (we want to preserve original timestamps)
19
+ prior_setting = record_activerecord_timestamps(false)
20
+ table_class.find_in_batches do |batch|
21
+ batch.each do |record|
22
+ if self.conditional_proc_type == :unless
23
+ unless @conditional_proc.call(record)
24
+ perform_scrubs_for(record)
25
+ end
26
+ elsif self.conditional_proc_type == :if
27
+ if @conditional_proc.call(record)
28
+ perform_scrubs_for(record)
29
+ end
30
+ else
31
+ perform_scrubs_for(record)
32
+ end
33
+ end
34
+ end
35
+ #restore timestamping to prior state
36
+ record_activerecord_timestamps(prior_setting)
37
+ end
38
+
39
+ private
40
+ def perform_scrubs_for(record)
41
+ @field_transforms.each {|t| t.perform_on(record) }
42
+ record.save!
43
+ end
44
+
45
+ def table_class
46
+ @table_class ||= Kernel.const_get(@table_name.classify)
47
+ end
48
+
49
+ def record_activerecord_timestamps(shall_record)
50
+ prior_setting = table_class.record_timestamps
51
+ table_class.record_timestamps = shall_record
52
+ prior_setting
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,18 @@
1
+ module Scrubber
2
+ class Worker
3
+ attr_reader :jobs
4
+
5
+ def initialize
6
+ @jobs = []
7
+ end
8
+
9
+ def scrub_table(table_name, options = {})
10
+ @jobs << Scrubber::Job.new(table_name, options)
11
+ yield(@jobs.last)
12
+ end
13
+
14
+ def perform
15
+ @jobs.each{|j| j.perform}
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe "scrubbing" do
4
+ it 'should set the first users email to have an example.com domain' do
5
+ perform_scrub
6
+ User.first.email.should =~ /example.com/
7
+ end
8
+
9
+ it 'should not perform scrubbing on a mydomain.com email due to the unless proc' do
10
+ u = User.create! do |u|
11
+ u.first_name = "Davie"
12
+ u.last_name = "Crocket"
13
+ u.login = "racoon"
14
+ u.email = "davie@mydomain.com"
15
+ end
16
+
17
+ perform_scrub
18
+
19
+ u.reload
20
+ u.email.should =~ /mydomain.com/
21
+ end
22
+
23
+ def perform_scrub
24
+ Scrubber.perform do |s|
25
+ s.scrub_table :users, :unless => proc {|user| user.email =~ /mydomain.com/i } do |user|
26
+ user.scrub :email, :set_to => "user[:id]@example.com"
27
+ user.scrub :last_name, :set_to => "blank"
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => 'sqlite3',
3
+ :database => File.join(File.dirname(__FILE__), 'test.db')
4
+ )
5
+
6
+ class CreateSchema < ActiveRecord::Migration
7
+ def self.up
8
+ create_table :users, :force => true do |t|
9
+ t.string :first_name
10
+ t.string :last_name
11
+ t.string :email
12
+ t.string :login
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
17
+
18
+ CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
19
+
20
+ class User < ActiveRecord::Base
21
+
22
+ end
23
+
24
+ User.destroy_all
25
+
26
+ User.create! do |u|
27
+ u.first_name = "John"
28
+ u.last_name = "Smith"
29
+ u.email = "john@acme.com"
30
+ u.login = "john"
31
+ end
32
+
33
+ User.create! do |u|
34
+ u.first_name = "Jane"
35
+ u.last_name = "Doe"
36
+ u.email = "jane@acme.com"
37
+ u.login = "jane"
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scrubber::FieldTransform do
4
+ before(:each) do
5
+ @field_name = :email
6
+ @transform = Scrubber::FieldTransform.new(@field_name, :set_to => nil)
7
+ end
8
+
9
+ it 'should have a field' do
10
+ @transform.field.should eql(@field_name.to_s)
11
+ end
12
+
13
+ it 'should raise an error if I do not specify a set_to param' do
14
+ lambda { Scrubber::FieldTransform.new(@field_name) }.should raise_error
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scrubber::Job do
4
+ before(:each) do
5
+ @table_name = :users
6
+ @job = Scrubber::Job.new(@table_name, :unless => proc {|user| user.email != "farfig" })
7
+ end
8
+
9
+ it 'should have a table name' do
10
+ @job.table_name.should eql(@table_name.to_s)
11
+ end
12
+
13
+ it 'should have a conditional proc' do
14
+ @job.conditional_proc.should_not be_nil
15
+ end
16
+
17
+ it 'should have a conditional proc type of unless' do
18
+ @job.conditional_proc_type.should eql(:unless)
19
+ end
20
+
21
+ it 'should append to the list of field transforms when I specify a scrub' do
22
+ @job.scrub(:email, :set_to => nil)
23
+ @job.field_transforms.size.should eql(1)
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scrubber::Worker do
4
+ before(:each) do
5
+ @worker = Scrubber::Worker.new
6
+ @worker.scrub_table :some_table do |t|
7
+
8
+ end
9
+ end
10
+
11
+ it 'should have a job' do
12
+ @worker.jobs.size.should eql(1)
13
+ end
14
+ end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'scrubber'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ require 'models'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scrubber
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Dan Pickett
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-09 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Obfuscate your private data using a simple dsl
35
+ email: dpickett@enlightsolutions.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/scrubber.rb
51
+ - lib/scrubber/field_transform.rb
52
+ - lib/scrubber/job.rb
53
+ - lib/scrubber/worker.rb
54
+ - spec/integration_spec.rb
55
+ - spec/models.rb
56
+ - spec/scrubber/field_transform_spec.rb
57
+ - spec/scrubber/job_spec.rb
58
+ - spec/scrubber/worker_spec.rb
59
+ - spec/spec.opts
60
+ - spec/spec_helper.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/dpickett/scrubber
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A DSL for scrubbing private data
91
+ test_files:
92
+ - spec/integration_spec.rb
93
+ - spec/models.rb
94
+ - spec/scrubber/field_transform_spec.rb
95
+ - spec/scrubber/job_spec.rb
96
+ - spec/scrubber/worker_spec.rb
97
+ - spec/spec_helper.rb