tidus 1.2.3 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tasks/views.rake +8 -8
- data/lib/tidus/anonymization.rb +11 -9
- data/lib/tidus/query.rb +4 -2
- data/lib/tidus/strategies/base_selector.rb +8 -2
- data/lib/tidus/strategies/postgresql/email_anonymizer.rb +15 -3
- data/lib/tidus/strategies/postgresql/sha256_anonymizer.rb +15 -0
- data/lib/tidus/strategies/sha256_anonymizer.rb +6 -0
- data/lib/tidus/version.rb +1 -1
- data/lib/tidus.rb +8 -8
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e856d718f2b4408d674dbd77073588f3da9f9c26dd8f9f61f9d4c1bbb674d525
|
4
|
+
data.tar.gz: ed491dc4694a926936fe3d8c879a48894ae6ef77685730004c63e1cd6e1e1493
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7ceb7506c37c5c9fd66fc5f417e8318b566ad8fcf900341c61a0cc3423bf69fe8066194c8cb1610296ecfe36fc48fd3ded8361904ab29d449e99828c17a54c8
|
7
|
+
data.tar.gz: 885933234f25620860632d23ea8e1567034ee9455840b1ebde57e5a88a25c4cb4803ed66a45b2ae9ffdcac93bb95eeb03ddc052557d116097207a638451282d8
|
data/lib/tasks/views.rake
CHANGED
@@ -4,7 +4,7 @@ skip_tables = [
|
|
4
4
|
]
|
5
5
|
|
6
6
|
namespace :db do
|
7
|
-
desc
|
7
|
+
desc 'Clears all the views which are currently existing'
|
8
8
|
task :clear_views do
|
9
9
|
Rails.application.eager_load! if defined?(Rails)
|
10
10
|
|
@@ -17,7 +17,7 @@ namespace :db do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
desc
|
20
|
+
desc 'Generates all the views for the models'
|
21
21
|
task :generate_views do
|
22
22
|
Rails.application.eager_load! if defined?(Rails)
|
23
23
|
|
@@ -33,13 +33,13 @@ namespace :db do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
Rake::Task[
|
37
|
-
Rake::Task[
|
36
|
+
Rake::Task['db:migrate'].enhance ['db:clear_views']
|
37
|
+
Rake::Task['db:rollback'].enhance ['db:clear_views']
|
38
38
|
|
39
|
-
Rake::Task[
|
40
|
-
Rake::Task[
|
39
|
+
Rake::Task['db:migrate'].enhance do
|
40
|
+
Rake::Task['db:generate_views'].invoke
|
41
41
|
end
|
42
42
|
|
43
|
-
Rake::Task[
|
44
|
-
Rake::Task[
|
43
|
+
Rake::Task['db:rollback'].enhance do
|
44
|
+
Rake::Task['db:generate_views'].invoke
|
45
45
|
end
|
data/lib/tidus/anonymization.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
module Tidus
|
4
4
|
module Anonymization
|
5
|
+
|
5
6
|
include Tidus::Query
|
6
7
|
|
7
8
|
def skip_anonymization
|
@@ -13,7 +14,7 @@ module Tidus
|
|
13
14
|
end
|
14
15
|
|
15
16
|
def view_postfix
|
16
|
-
@view_postfix ||
|
17
|
+
@view_postfix || 'anonymized'
|
17
18
|
end
|
18
19
|
|
19
20
|
def view_postfix=(val)
|
@@ -30,9 +31,10 @@ module Tidus
|
|
30
31
|
|
31
32
|
def view_columns
|
32
33
|
@view_columns ||= {}
|
33
|
-
default_view_columns
|
34
|
-
|
35
|
-
|
34
|
+
default_view_columns
|
35
|
+
.merge(@view_columns)
|
36
|
+
.map { |k, v| ["#{v} AS #{k}"] }
|
37
|
+
.flatten
|
36
38
|
end
|
37
39
|
|
38
40
|
def default_view_columns
|
@@ -49,8 +51,8 @@ module Tidus
|
|
49
51
|
options = attributes.extract_options!.dup
|
50
52
|
columns = attributes - [options]
|
51
53
|
|
52
|
-
raise ArgumentError
|
53
|
-
raise ArgumentError
|
54
|
+
raise ArgumentError.new('Must have at least one attribute') if attributes.empty?
|
55
|
+
raise ArgumentError.new('Must have a strategy') if options[:strategy].blank?
|
54
56
|
|
55
57
|
columns.each do |column|
|
56
58
|
key = options[:strategy].to_s.camelize
|
@@ -62,14 +64,14 @@ module Tidus
|
|
62
64
|
klass = const_get("Tidus::#{key}Anonymizer")
|
63
65
|
end
|
64
66
|
rescue NameError
|
65
|
-
raise ArgumentError
|
67
|
+
raise ArgumentError.new("Unknown anonymizer: '#{key}'")
|
66
68
|
end
|
67
69
|
|
68
70
|
@view_columns[column.to_sym] = klass.anonymize(table_name, column, options)
|
69
71
|
end
|
70
72
|
end
|
71
73
|
|
72
|
-
|
74
|
+
end
|
73
75
|
end
|
74
76
|
|
75
|
-
ActiveRecord::Base.extend Tidus::Anonymization
|
77
|
+
ActiveRecord::Base.extend Tidus::Anonymization
|
data/lib/tidus/query.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
module Tidus
|
4
4
|
module Query
|
5
|
+
|
5
6
|
def create_view_query_part
|
6
7
|
case connection.instance_values['config'][:adapter].to_s.downcase
|
7
8
|
when 'postgresql'
|
@@ -15,8 +16,8 @@ module Tidus
|
|
15
16
|
|
16
17
|
def create_query
|
17
18
|
"#{create_view_query_part} #{view_name} AS " +
|
18
|
-
|
19
|
-
|
19
|
+
"SELECT #{view_columns.join(', ')} " +
|
20
|
+
"FROM #{table_name}"
|
20
21
|
end
|
21
22
|
|
22
23
|
def create_view
|
@@ -34,5 +35,6 @@ module Tidus
|
|
34
35
|
def clear_view
|
35
36
|
connection.execute(clear_query)
|
36
37
|
end
|
38
|
+
|
37
39
|
end
|
38
40
|
end
|
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
module Tidus
|
4
4
|
class BaseSelector
|
5
|
+
|
5
6
|
def self.anonymize(table_name, column_name, options = {})
|
6
|
-
adapter = ActiveRecord::Base.connection.instance_values[
|
7
|
+
adapter = ActiveRecord::Base.connection.instance_values['config'][:adapter]
|
7
8
|
|
8
9
|
begin
|
9
10
|
klass = Kernel.const_get("Tidus::#{adapter.camelize}::#{self.name.demodulize}")
|
@@ -11,7 +12,12 @@ module Tidus
|
|
11
12
|
rescue NameError
|
12
13
|
raise "#{self.name} not implemented for #{adapter}"
|
13
14
|
end
|
14
|
-
|
15
|
+
rescue ActiveRecord::ConnectionNotEstablished
|
16
|
+
# Do nothing here.
|
17
|
+
# If the connection cannot be established this cannot be run in a migration
|
18
|
+
# and will fail there.
|
19
|
+
# For all other cases it does not matter, since the return value of anonymize is not used.
|
15
20
|
end
|
21
|
+
|
16
22
|
end
|
17
23
|
end
|
@@ -1,15 +1,27 @@
|
|
1
1
|
module Tidus
|
2
2
|
module Postgresql
|
3
3
|
class EmailAnonymizer
|
4
|
+
|
4
5
|
def self.anonymize(table_name, column_name, options = {})
|
5
6
|
name = "#{table_name}.#{column_name}"
|
6
|
-
options[:length]
|
7
|
+
length = options[:length] || 15
|
8
|
+
anonymize_domain = options[:anonymize_domain] || false
|
7
9
|
|
8
10
|
return "CASE WHEN ((#{name})::text ~~ '%@%'::text) " +
|
9
|
-
"THEN (((\"left\"(md5((#{name})::text), #{
|
10
|
-
"||
|
11
|
+
"THEN (((\"left\"(md5((#{name})::text), #{length}) || '@'::text) " +
|
12
|
+
"|| #{domain_part(name, anonymize_domain, length)}))::character varying " +
|
11
13
|
"ELSE #{name} END"
|
12
14
|
end
|
15
|
+
|
16
|
+
def self.domain_part(name, anonymize_domain, length)
|
17
|
+
if anonymize_domain
|
18
|
+
return "(\"left\"(md5(split_part((#{name})::text, '@'::text, 2)::text), #{length}) " +
|
19
|
+
"|| '.com')"
|
20
|
+
end
|
21
|
+
|
22
|
+
return "split_part((#{name})::text, '@'::text, 2)"
|
23
|
+
end
|
24
|
+
|
13
25
|
end
|
14
26
|
end
|
15
27
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Tidus
|
2
|
+
module Postgresql
|
3
|
+
class Sha256Anonymizer
|
4
|
+
|
5
|
+
def self.anonymize(table_name, column_name, options = {})
|
6
|
+
name = "#{table_name}.#{column_name}"
|
7
|
+
|
8
|
+
length = options[:length] || 64
|
9
|
+
|
10
|
+
return "SUBSTR(ENCODE(DIGEST(#{name}, 'sha256')::TEXT, 'HEX'), 0, #{length.to_i + 1})"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/tidus/version.rb
CHANGED
data/lib/tidus.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'rake'
|
4
|
+
require 'active_record'
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
6
|
+
require 'tidus/version'
|
7
|
+
require 'tidus/query'
|
8
|
+
require 'tidus/anonymization'
|
9
|
+
require 'tidus/strategies/base_selector'
|
10
10
|
Dir["#{File.dirname(__FILE__)}/tidus/strategies/**/*.rb"].each { |f| require f }
|
11
11
|
|
12
|
-
load
|
13
|
-
load
|
12
|
+
load 'active_record/railties/databases.rake' if defined?(Rails)
|
13
|
+
load 'tasks/views.rake'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tidus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Schoknecht
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -90,9 +90,11 @@ files:
|
|
90
90
|
- lib/tidus/strategies/postgresql/overlay_anonymizer.rb
|
91
91
|
- lib/tidus/strategies/postgresql/regex_replace_anonymizer.rb
|
92
92
|
- lib/tidus/strategies/postgresql/remove_json_keys_anonymizer.rb
|
93
|
+
- lib/tidus/strategies/postgresql/sha256_anonymizer.rb
|
93
94
|
- lib/tidus/strategies/postgresql/text_anonymizer.rb
|
94
95
|
- lib/tidus/strategies/regex_replace_anonymizer.rb
|
95
96
|
- lib/tidus/strategies/remove_json_keys_anonymizer.rb
|
97
|
+
- lib/tidus/strategies/sha256_anonymizer.rb
|
96
98
|
- lib/tidus/strategies/sqlite3/null_anonymizer.rb
|
97
99
|
- lib/tidus/strategies/static_anonymizer.rb
|
98
100
|
- lib/tidus/strategies/text_anonymizer.rb
|
@@ -116,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
118
|
- !ruby/object:Gem::Version
|
117
119
|
version: '0'
|
118
120
|
requirements: []
|
119
|
-
rubygems_version: 3.
|
121
|
+
rubygems_version: 3.4.13
|
120
122
|
signing_key:
|
121
123
|
specification_version: 4
|
122
124
|
summary: Gem for creating anonymization views.
|