my_obfuscate 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/lib/my_obfuscate.rb +9 -5
- data/lib/my_obfuscate/config_applicator.rb +15 -15
- data/lib/my_obfuscate/insert_statement_parser.rb +4 -1
- data/lib/my_obfuscate/mysql.rb +6 -5
- data/lib/my_obfuscate/postgres.rb +2 -2
- data/lib/my_obfuscate/sql_server.rb +1 -1
- data/lib/my_obfuscate/version.rb +1 -1
- data/spec/my_obfuscate/config_applicator_spec.rb +5 -5
- data/spec/my_obfuscate/mysql_spec.rb +8 -1
- data/spec/spec_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c81cc9453a733909c1a906c2369cce0999e8c627
|
4
|
+
data.tar.gz: f218ca6b9c443c1046dafa4877b279346258101c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd82ab134257c31299a91e79d2363176e47ae545e0d7f88e3672a01a4f8fce2c92b4e52ee003d20c9bb82cc145840d9a15ee0fbc25d757c40127999945670cd7
|
7
|
+
data.tar.gz: e0698977563fc19685b32e78943cf9633aa476f92dbc25f9bbaf33987d302452bdc264774c5fa6f9900cd2a1998ebb798d229b95e56b6666d3674aa67e542d5b
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.2.0
|
data/lib/my_obfuscate.rb
CHANGED
@@ -42,14 +42,14 @@ class MyObfuscate
|
|
42
42
|
database_helper.parse(self, config, input_io, output_io)
|
43
43
|
end
|
44
44
|
|
45
|
-
def reassembling_each_insert(line, table_name, columns)
|
45
|
+
def reassembling_each_insert(line, table_name, columns, ignore = nil)
|
46
46
|
output = database_helper.rows_to_be_inserted(line).map do |sub_insert|
|
47
47
|
result = yield(sub_insert)
|
48
48
|
result = result.map do |i|
|
49
49
|
database_helper.make_valid_value_string(i)
|
50
50
|
end
|
51
51
|
end
|
52
|
-
database_helper.make_insert_statement(table_name, columns, output)
|
52
|
+
database_helper.make_insert_statement(table_name, columns, output, ignore)
|
53
53
|
end
|
54
54
|
|
55
55
|
def check_for_defined_columns_not_in_table(table_name, columns)
|
@@ -63,8 +63,12 @@ class MyObfuscate
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
def missing_column_list(table_name, columns)
|
67
|
+
columns - (config[table_name].keys + (globally_kept_columns || []).map {|i| i.to_sym}).uniq
|
68
|
+
end
|
69
|
+
|
66
70
|
def check_for_table_columns_not_in_definition(table_name, columns)
|
67
|
-
missing_columns =
|
71
|
+
missing_columns = missing_column_list(table_name, columns)
|
68
72
|
unless missing_columns.length == 0
|
69
73
|
error_message = missing_columns.map do |missing_column|
|
70
74
|
"Column '#{missing_column}' defined in table '#{table_name}', but not found in table definition, please fix your obfuscator config."
|
@@ -73,7 +77,7 @@ class MyObfuscate
|
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
76
|
-
def obfuscate_bulk_insert_line(line, table_name, columns)
|
80
|
+
def obfuscate_bulk_insert_line(line, table_name, columns, ignore = nil)
|
77
81
|
table_config = config[table_name]
|
78
82
|
if table_config == :truncate
|
79
83
|
""
|
@@ -83,7 +87,7 @@ class MyObfuscate
|
|
83
87
|
check_for_defined_columns_not_in_table(table_name, columns)
|
84
88
|
check_for_table_columns_not_in_definition(table_name, columns) if fail_on_unspecified_columns?
|
85
89
|
# Note: Remember to SQL escape strings in what you pass back.
|
86
|
-
reassembling_each_insert(line, table_name, columns) do |row|
|
90
|
+
reassembling_each_insert(line, table_name, columns, ignore) do |row|
|
87
91
|
ConfigApplicator.apply_table_config(row, table_config, columns)
|
88
92
|
end
|
89
93
|
end
|
@@ -30,42 +30,42 @@ class MyObfuscate
|
|
30
30
|
row[index.to_i] = case definition[:type]
|
31
31
|
when :email
|
32
32
|
md5 = Digest::MD5.hexdigest(rand.to_s)[0...5]
|
33
|
-
clean_quotes("#{
|
33
|
+
clean_quotes("#{FFaker::Internet.email}.#{md5}.example.com")
|
34
34
|
when :string
|
35
35
|
random_string(definition[:length] || 30, definition[:chars] || SENSIBLE_CHARS)
|
36
36
|
when :lorem
|
37
|
-
clean_bad_whitespace(clean_quotes(
|
37
|
+
clean_bad_whitespace(clean_quotes(FFaker::Lorem.sentences(definition[:number] || 1).join(". ")))
|
38
38
|
when :like_english
|
39
39
|
clean_quotes random_english_sentences(definition[:number] || 1)
|
40
40
|
when :name
|
41
|
-
clean_quotes(
|
41
|
+
clean_quotes(FFaker::Name.name)
|
42
42
|
when :first_name
|
43
|
-
clean_quotes(
|
43
|
+
clean_quotes(FFaker::Name.first_name)
|
44
44
|
when :last_name
|
45
|
-
clean_quotes(
|
45
|
+
clean_quotes(FFaker::Name.last_name)
|
46
46
|
when :address
|
47
|
-
clean_quotes("#{
|
47
|
+
clean_quotes("#{FFaker::AddressUS.street_address}\\n#{FFaker::AddressUS.city}, #{FFaker::AddressUS.state_abbr} #{FFaker::AddressUS.zip_code}")
|
48
48
|
when :street_address
|
49
|
-
clean_bad_whitespace(clean_quotes(
|
49
|
+
clean_bad_whitespace(clean_quotes(FFaker::AddressUS.street_address))
|
50
50
|
when :city
|
51
|
-
clean_quotes(
|
51
|
+
clean_quotes(FFaker::AddressUS.city)
|
52
52
|
when :state
|
53
|
-
clean_quotes
|
53
|
+
clean_quotes FFaker::AddressUS.state_abbr
|
54
54
|
when :zip_code
|
55
|
-
|
55
|
+
FFaker::AddressUS.zip_code
|
56
56
|
when :phone
|
57
|
-
clean_quotes
|
57
|
+
clean_quotes FFaker::PhoneNumber.phone_number
|
58
58
|
when :company
|
59
|
-
clean_bad_whitespace(clean_quotes(
|
59
|
+
clean_bad_whitespace(clean_quotes(FFaker::Company.name))
|
60
60
|
when :ipv4
|
61
|
-
|
61
|
+
FFaker::Internet.ip_v4_address
|
62
62
|
when :ipv6
|
63
|
-
# Inlined from
|
63
|
+
# Inlined from FFaker because ffaker doesn't have ipv6.
|
64
64
|
@@ip_v6_space ||= (0..65535).to_a
|
65
65
|
container = (1..8).map{ |_| @@ip_v6_space.sample }
|
66
66
|
container.map{ |n| n.to_s(16) }.join(':')
|
67
67
|
when :url
|
68
|
-
clean_bad_whitespace(
|
68
|
+
clean_bad_whitespace(FFaker::Internet.http_url)
|
69
69
|
when :integer
|
70
70
|
random_integer(definition[:between] || (0..1000)).to_s
|
71
71
|
when :fixed
|
@@ -6,8 +6,9 @@ class MyObfuscate
|
|
6
6
|
if table_data = parse_insert_statement(line)
|
7
7
|
table_name = table_data[:table_name]
|
8
8
|
columns = table_data[:column_names]
|
9
|
+
ignore = table_data[:ignore]
|
9
10
|
if config[table_name]
|
10
|
-
output_io.puts obfuscator.obfuscate_bulk_insert_line(line, table_name, columns)
|
11
|
+
output_io.puts obfuscator.obfuscate_bulk_insert_line(line, table_name, columns, ignore)
|
11
12
|
else
|
12
13
|
$stderr.puts "Deprecated: #{table_name} was not specified in the config. A future release will cause this to be an error. Please specify the table definition or set it to :keep."
|
13
14
|
output_io.write line
|
@@ -20,3 +21,5 @@ class MyObfuscate
|
|
20
21
|
|
21
22
|
end
|
22
23
|
end
|
24
|
+
|
25
|
+
|
data/lib/my_obfuscate/mysql.rb
CHANGED
@@ -5,22 +5,23 @@ class MyObfuscate
|
|
5
5
|
def parse_insert_statement(line)
|
6
6
|
if regex_match = insert_regex.match(line)
|
7
7
|
{
|
8
|
-
:
|
9
|
-
:
|
8
|
+
:ignore => !regex_match[1].nil?,
|
9
|
+
:table_name => regex_match[2].to_sym,
|
10
|
+
:column_names => regex_match[3].split(/`\s*,\s*`/).map { |col| col.gsub('`', "").to_sym }
|
10
11
|
}
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
|
-
def make_insert_statement(table_name, column_names, values)
|
15
|
+
def make_insert_statement(table_name, column_names, values, ignore = nil)
|
15
16
|
values_strings = values.collect do |values|
|
16
17
|
"(" + values.join(",") + ")"
|
17
18
|
end.join(",")
|
18
19
|
|
19
|
-
"INSERT INTO `#{table_name}` (`#{column_names.join('`, `')}`) VALUES #{values_strings};"
|
20
|
+
"INSERT #{ignore ? 'IGNORE ' : '' }INTO `#{table_name}` (`#{column_names.join('`, `')}`) VALUES #{values_strings};"
|
20
21
|
end
|
21
22
|
|
22
23
|
def insert_regex
|
23
|
-
/^\s*INSERT INTO `(.*?)` \((.*?)\) VALUES\s*/i
|
24
|
+
/^\s*INSERT\s*(IGNORE )?\s*INTO `(.*?)` \((.*?)\) VALUES\s*/i
|
24
25
|
end
|
25
26
|
|
26
27
|
def rows_to_be_inserted(line)
|
@@ -2,7 +2,7 @@ class MyObfuscate
|
|
2
2
|
class Postgres
|
3
3
|
include MyObfuscate::CopyStatementParser
|
4
4
|
|
5
|
-
# Copy statements contain the column values tab
|
5
|
+
# Copy statements contain the column values tab separated like so:
|
6
6
|
# blah blah blah blah
|
7
7
|
# which we want to turn into:
|
8
8
|
# [['blah','blah','blah','blah']]
|
@@ -33,7 +33,7 @@ class MyObfuscate
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def make_insert_statement(table_name, column_names, values)
|
36
|
+
def make_insert_statement(table_name, column_names, values, ignore = nil)
|
37
37
|
values.join("\t")
|
38
38
|
end
|
39
39
|
|
@@ -26,7 +26,7 @@ class MyObfuscate
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def make_insert_statement(table_name, column_names, values)
|
29
|
+
def make_insert_statement(table_name, column_names, values, ignore = nil)
|
30
30
|
values_strings = values.collect do |values|
|
31
31
|
"(" + values.join(",") + ")"
|
32
32
|
end.join(",")
|
data/lib/my_obfuscate/version.rb
CHANGED
@@ -232,11 +232,11 @@ describe MyObfuscate::ConfigApplicator do
|
|
232
232
|
|
233
233
|
describe "when faker generates values with quotes in them" do
|
234
234
|
before do
|
235
|
-
allow(
|
236
|
-
allow(
|
237
|
-
allow(
|
238
|
-
allow(
|
239
|
-
allow(
|
235
|
+
allow(FFaker::Address).to receive(:city).and_return("O'ReillyTown")
|
236
|
+
allow(FFaker::Name).to receive(:name).and_return("Foo O'Reilly")
|
237
|
+
allow(FFaker::Name).to receive(:first_name).and_return("O'Foo")
|
238
|
+
allow(FFaker::Name).to receive(:last_name).and_return("O'Reilly")
|
239
|
+
allow(FFaker::Lorem).to receive(:sentences).with(any_args).and_return(["Foo bar O'Thingy"])
|
240
240
|
end
|
241
241
|
|
242
242
|
it "should remove single quotes from the value" do
|
@@ -73,7 +73,14 @@ describe MyObfuscate::Mysql do
|
|
73
73
|
|
74
74
|
it "should return a hash of table name, column names for MySQL insert statements" do
|
75
75
|
hash = subject.parse_insert_statement("INSERT INTO `some_table` (`email`, `name`, `something`, `age`) VALUES ('bob@honk.com','bob', 'some\\'thin,ge())lse1', 25),('joe@joe.com','joe', 'somethingelse2', 54);")
|
76
|
-
expect(hash).to eq({:table_name => :some_table, :column_names => [:email, :name, :something, :age]})
|
76
|
+
expect(hash).to eq({:ignore => false, :table_name => :some_table, :column_names => [:email, :name, :something, :age]})
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#parse_insert_ignore_statement" do
|
81
|
+
it "should return a hash of IGNORE, table name, column names for MySQL insert statements" do
|
82
|
+
hash = subject.parse_insert_statement("INSERT IGNORE INTO `some_table` (`email`, `name`, `something`, `age`) VALUES ('bob@honk.com','bob', 'some\\'thin,ge())lse1', 25),('joe@joe.com','joe', 'somethingelse2', 54);")
|
83
|
+
expect(hash).to eq({:ignore => true, :table_name => :some_table, :column_names => [:email, :name, :something, :age]})
|
77
84
|
end
|
78
85
|
end
|
79
86
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_obfuscate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Cantino
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date:
|
16
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: ffaker
|