data_veil 0.0.1
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/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +14 -0
- data/exe/data_veil +7 -0
- data/lib/data_veil/cli.rb +44 -0
- data/lib/data_veil/mask/base.rb +71 -0
- data/lib/data_veil/mask_data_generator.rb +53 -0
- data/lib/data_veil/masking/initializer.rb +41 -0
- data/lib/data_veil/version.rb +5 -0
- data/lib/data_veil.rb +22 -0
- data/sig/data_veil.rbs +4 -0
- data/tasks/db/migrate/create_test_tables.rb +33 -0
- data/tasks/db.rake +86 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3ca5158d386c5fc4da6ec7af25b70946f2d6921c013379b7353e8878c6ff396f
|
4
|
+
data.tar.gz: c7d3e590d8181fed9c872befbb33adfd510163568939d087cb76150bc19dc809
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b40639580d357c91e3db62c52228d59778c0ceebea460ea7f0a4cebe2bf96c6f511921c9a072e20049f91674b6fe3950b18050b5a00f260df84345c4d0c97ae
|
7
|
+
data.tar.gz: 4bb25e018c7f5b3cf57d6bd8259e7e67311ac031f618371fb38abc67a6bef18c257d14ed9e6b5a40571695d39a2e5c787d3a7f969d6b3e689672acb3659b7ff1
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 toshichanapp
|
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,104 @@
|
|
1
|
+
# DataVeil
|
2
|
+
|
3
|
+
DataVeil is a Ruby gem designed to mask sensitive data in your database for development and testing purposes. It provides a flexible way to define masking rules and apply them to your database tables.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'data_veil'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```
|
22
|
+
$ gem install data_veil
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
|
29
|
+
1. Create a database configuration file (e.g., `config/mask_database.yml`) with your database settings:
|
30
|
+
|
31
|
+
```yaml
|
32
|
+
development:
|
33
|
+
adapter: mysql2
|
34
|
+
database: your_database_name
|
35
|
+
username: your_username
|
36
|
+
password: your_password
|
37
|
+
host: localhost
|
38
|
+
```
|
39
|
+
|
40
|
+
2. Create a masking configuration file (e.g., `config/masking.yml`) to define your masking rules:
|
41
|
+
|
42
|
+
```yaml
|
43
|
+
your_database_name:
|
44
|
+
users:
|
45
|
+
email:
|
46
|
+
type: email
|
47
|
+
first_name:
|
48
|
+
type: first_name
|
49
|
+
last_name:
|
50
|
+
type: last_name
|
51
|
+
birth_date:
|
52
|
+
type: date
|
53
|
+
start_date: "1970-01-01"
|
54
|
+
end_date: "2000-12-31"
|
55
|
+
phone:
|
56
|
+
type: tel
|
57
|
+
```
|
58
|
+
|
59
|
+
### Running the Masking Process
|
60
|
+
|
61
|
+
To run the masking process, use the following command:
|
62
|
+
|
63
|
+
```
|
64
|
+
$ data_veil mask -d path/to/mask_database.yml -m path/to/masking.yml -e development
|
65
|
+
```
|
66
|
+
|
67
|
+
Options:
|
68
|
+
- `-d, --database-config PATH`: Path to the database configuration file (default: "./config/mask_database.yml")
|
69
|
+
- `-m, --masking-config PATH`: Path to the masking configuration file (default: "./config/masking.yml")
|
70
|
+
- `-e, --environment ENV`: Environment (default: "development")
|
71
|
+
|
72
|
+
### Supported Masking Types
|
73
|
+
|
74
|
+
- `email`: Generates a random email address
|
75
|
+
- `string`: Generates a random string
|
76
|
+
- `password`: Generates a random password
|
77
|
+
- `tel`: Generates a random phone number
|
78
|
+
- `first_name`: Generates a random first name
|
79
|
+
- `last_name`: Generates a random last name
|
80
|
+
- `integer`: Generates a random integer within a specified range
|
81
|
+
- `date`: Generates a random date within a specified range
|
82
|
+
- `enum`: Selects a random value from a specified list
|
83
|
+
|
84
|
+
### Type-specific Options
|
85
|
+
|
86
|
+
Different masking types support different options:
|
87
|
+
|
88
|
+
- `string`:
|
89
|
+
- `length`: The length of the generated string (default: 20)
|
90
|
+
- `password`:
|
91
|
+
- `min_length`: Minimum length of the password (default: 10)
|
92
|
+
- `max_length`: Maximum length of the password (default: 20)
|
93
|
+
- `integer`:
|
94
|
+
- `min`: Minimum value (default: 0)
|
95
|
+
- `max`: Maximum value (default: 100)
|
96
|
+
- `date`:
|
97
|
+
- `start_date`: The earliest date to generate (format: "YYYY-MM-DD")
|
98
|
+
- `end_date`: The latest date to generate (format: "YYYY-MM-DD")
|
99
|
+
- `enum`:
|
100
|
+
- `values`: An array of possible values to choose from
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require "rubocop/rake_task"
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
task default: %i[spec rubocop]
|
13
|
+
|
14
|
+
Dir.glob("tasks/**/*.rake").each { |r| load r }
|
data/exe/data_veil
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataVeil
|
4
|
+
class CLI < Thor
|
5
|
+
SUCCESS_EXIT_CODE = 0
|
6
|
+
ERROR_EXIT_CODE = 1
|
7
|
+
|
8
|
+
desc "mask", "Run masking process"
|
9
|
+
option :database_config_path,
|
10
|
+
aliases: "-d",
|
11
|
+
default: "./config/mask_database.yml",
|
12
|
+
desc: "Path to the database configuration file"
|
13
|
+
option :masking_config_path,
|
14
|
+
aliases: "-m",
|
15
|
+
default: "./config/masking.yml",
|
16
|
+
desc: "Path to the masking configuration file"
|
17
|
+
option :environment,
|
18
|
+
aliases: "-e",
|
19
|
+
default: "development",
|
20
|
+
desc: "Environment"
|
21
|
+
|
22
|
+
def mask
|
23
|
+
masked_classes = DataVeil::Masking::Initializer.setup(
|
24
|
+
database_config_path: options[:database_config_path],
|
25
|
+
masking_config_path: options[:masking_config_path],
|
26
|
+
environment: options[:environment]
|
27
|
+
)
|
28
|
+
run_mask_all_on_generated_classes(masked_classes)
|
29
|
+
SUCCESS_EXIT_CODE
|
30
|
+
rescue StandardError => e
|
31
|
+
puts e.inspect
|
32
|
+
ERROR_EXIT_CODE
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def run_mask_all_on_generated_classes(masked_classes)
|
38
|
+
masked_classes.each do |klass|
|
39
|
+
puts "Running mask_all! on #{klass.table_name}"
|
40
|
+
klass.mask_all!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataVeil
|
4
|
+
module Mask
|
5
|
+
module Base
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
@skip_verify_host = false
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
attr_accessor :skip_verify_host
|
12
|
+
|
13
|
+
def verify_non_production_host!
|
14
|
+
current_host = connection_db_config.host
|
15
|
+
|
16
|
+
if defined?(ApplicationRecord)
|
17
|
+
app_host = ApplicationRecord.connection_db_config.host
|
18
|
+
compare_hosts(current_host, app_host, "ApplicationRecord")
|
19
|
+
elsif ENV["DATABASE_HOST"].present? || ENV["DATABASE_URL"].present?
|
20
|
+
env_host = ENV["DATABASE_HOST"]
|
21
|
+
db_url = ENV["DATABASE_URL"]
|
22
|
+
|
23
|
+
compare_hosts(current_host, env_host, "ENV['DATABASE_HOST']") if env_host
|
24
|
+
compare_url(current_host, db_url, "ENV['DATABASE_URL']") if db_url
|
25
|
+
else
|
26
|
+
raise "DATABASE_URL or DATABASE_HOST not set. "
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def compare_hosts(current_host, other_host, source)
|
31
|
+
if current_host == other_host
|
32
|
+
puts "Connected to the same host as #{source}: #{current_host}"
|
33
|
+
raise "Connected to a production database host: #{current_host}" unless skip_verify_host
|
34
|
+
|
35
|
+
warn "Connected to a production database host: #{current_host}"
|
36
|
+
|
37
|
+
else
|
38
|
+
puts "Different hosts detected: Current=#{current_host}, #{source}=#{other_host}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def compare_url(current_host, url, source)
|
43
|
+
db_uri = URI.parse(url)
|
44
|
+
host = db_uri.host
|
45
|
+
|
46
|
+
compare_hosts(current_host, host, source)
|
47
|
+
end
|
48
|
+
|
49
|
+
def columns_to_mask
|
50
|
+
raise NotImplementedError, "You must implement the columns_to_mask method"
|
51
|
+
end
|
52
|
+
|
53
|
+
def mask_all!
|
54
|
+
verify_non_production_host!
|
55
|
+
in_batches do |relation|
|
56
|
+
relation.each(&:mask)
|
57
|
+
relation.upsert_all(relation.map(&:attributes))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def mask
|
63
|
+
self.columns_to_mask.each do |column, mask_config|
|
64
|
+
mask_type = mask_config["type"]
|
65
|
+
masked = MaskedDataGenerator.generate_masked_value(mask_type, mask_config)
|
66
|
+
public_send("#{column}=", masked)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class MaskedDataGenerator
|
4
|
+
def self.mask_strategies
|
5
|
+
@mask_strategies ||= {
|
6
|
+
"uuid" => ->(_) { Faker::Internet.uuid },
|
7
|
+
"email" => ->(_) { Faker::Internet.unique.email },
|
8
|
+
"string" => ->(config) { Faker::Lorem.characters(number: config[:length] || 20) },
|
9
|
+
"postcode" => ->(_) { Faker::Base.numerify("###-####", leading_zero: true) },
|
10
|
+
"tel" => ->(_) { Faker::PhoneNumber.phone_number },
|
11
|
+
"first_name" => ->(_) { Faker::Name.first_name },
|
12
|
+
"last_name" => ->(_) { Faker::Name.last_name },
|
13
|
+
"integer" => method(:generate_integer_masked_value),
|
14
|
+
"password" => method(:generate_password_masked_value),
|
15
|
+
"date" => method(:generate_date_masked_value),
|
16
|
+
"enum" => method(:generate_enum_masked_value)
|
17
|
+
}.freeze
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.generate_integer_masked_value(config)
|
21
|
+
min = config[:min] || 0
|
22
|
+
max = config[:max] || 100
|
23
|
+
Faker::Number.between(from: min, to: max)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.generate_password_masked_value(config)
|
27
|
+
min_length = config[:min_length] || 10
|
28
|
+
max_length = config[:max_length] || 20
|
29
|
+
Faker::Internet.password(min_length: min_length, max_length: max_length)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.generate_date_masked_value(config)
|
33
|
+
start_date = Date.parse(config[:start_date] || "1970-01-01")
|
34
|
+
end_date = Date.parse(config[:end_date] || Date.today.to_s)
|
35
|
+
Faker::Date.between(from: start_date, to: end_date)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.generate_enum_masked_value(config)
|
39
|
+
values = config[:values]
|
40
|
+
raise ArgumentError, "Enum values must be specified" if values.nil? || values.empty?
|
41
|
+
|
42
|
+
values.sample
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.generate_masked_value(mask_type, config = {})
|
46
|
+
strategy = mask_strategies[mask_type]
|
47
|
+
raise ArgumentError, "Unknown mask type: #{mask_type}" unless strategy
|
48
|
+
|
49
|
+
strategy.call(config.transform_keys(&:to_sym))
|
50
|
+
rescue StandardError => e
|
51
|
+
raise ArgumentError, "Error generating masked value for #{mask_type}: #{e.message}"
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataVeil
|
4
|
+
module Masking
|
5
|
+
class Initializer
|
6
|
+
def self.setup(database_config_path:, masking_config_path:, environment:)
|
7
|
+
puts "environment: #{environment}"
|
8
|
+
puts "database_config_path: #{database_config_path}"
|
9
|
+
load_database_config(database_config_path, environment)
|
10
|
+
puts "masking_config_path: #{masking_config_path}"
|
11
|
+
create_mask_classes(masking_config_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.load_database_config(database_config_path, environment)
|
15
|
+
db_config = YAML.safe_load(ERB.new(File.read(database_config_path)).result, aliases: true)
|
16
|
+
ActiveRecord::Base.configurations = db_config[environment]
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.create_mask_classes(masking_config_path)
|
20
|
+
config = YAML.load_file(masking_config_path)
|
21
|
+
config.flat_map do |database_name, tables|
|
22
|
+
base_class_name = "#{database_name.classify}Record"
|
23
|
+
base_klass = Class.new(ActiveRecord::Base) do
|
24
|
+
include DataVeil::Mask::Base
|
25
|
+
self.abstract_class = true
|
26
|
+
end
|
27
|
+
# connects_toは Anonymous class is not allowed.なので命名する。
|
28
|
+
base_class = DataVeil::Mask.const_set(base_class_name, base_klass)
|
29
|
+
base_class.connects_to database: { writing: database_name.to_sym, reading: database_name.to_sym }
|
30
|
+
|
31
|
+
tables.map do |table_name, columns|
|
32
|
+
Class.new(base_class) do
|
33
|
+
self.table_name = table_name
|
34
|
+
class_attribute :columns_to_mask, default: columns
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/data_veil.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "data_veil/version"
|
4
|
+
require "erb"
|
5
|
+
require "faker"
|
6
|
+
require "thor"
|
7
|
+
require "uri"
|
8
|
+
require "yaml"
|
9
|
+
begin
|
10
|
+
require "dotenv/load"
|
11
|
+
rescue LoadError
|
12
|
+
end
|
13
|
+
require "active_record"
|
14
|
+
require "data_veil/mask_data_generator"
|
15
|
+
require "data_veil/masking/initializer"
|
16
|
+
require "data_veil/mask/base"
|
17
|
+
require "data_veil/cli"
|
18
|
+
|
19
|
+
module DataVeil
|
20
|
+
class Error < StandardError; end
|
21
|
+
# Your code goes here...
|
22
|
+
end
|
data/sig/data_veil.rbs
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# spec/support/create_test_tables.rb
|
4
|
+
|
5
|
+
require "active_record"
|
6
|
+
|
7
|
+
class CreateTestTables < ActiveRecord::Migration[7.2]
|
8
|
+
def up
|
9
|
+
# Create users table
|
10
|
+
create_table :users do |t|
|
11
|
+
t.string :email, null: false
|
12
|
+
t.string :name
|
13
|
+
t.string :password
|
14
|
+
t.string :phone
|
15
|
+
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
|
19
|
+
# Create customer_analytics_emails table
|
20
|
+
create_table :customer_analytics_emails do |t|
|
21
|
+
t.string :email, null: false
|
22
|
+
t.string :customer_id
|
23
|
+
t.datetime :last_sent_at
|
24
|
+
|
25
|
+
t.timestamps
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def down
|
30
|
+
drop_table :users
|
31
|
+
drop_table :customer_analytics_emails
|
32
|
+
end
|
33
|
+
end
|
data/tasks/db.rake
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/tasks/db.rake
|
4
|
+
|
5
|
+
require "rake"
|
6
|
+
require "active_record"
|
7
|
+
require "yaml"
|
8
|
+
require "mysql2"
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
def load_config
|
12
|
+
YAML.load_file("config/database.yml")["test"]
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_database(config, database)
|
16
|
+
charset = config["encoding"] || "utf8mb4"
|
17
|
+
collation = config["collation"] || "utf8mb4_unicode_ci"
|
18
|
+
client = Mysql2::Client.new(
|
19
|
+
host: config["host"],
|
20
|
+
username: config["username"],
|
21
|
+
password: config["password"]
|
22
|
+
)
|
23
|
+
client.query("CREATE DATABASE IF NOT EXISTS `#{database}` CHARACTER SET #{charset} COLLATE #{collation}")
|
24
|
+
client.close
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Create test databases"
|
28
|
+
task :create do
|
29
|
+
config = load_config
|
30
|
+
%w[default customer_analytics].each do |db|
|
31
|
+
create_database(config[db], config[db]["database"])
|
32
|
+
puts "Created database '#{config[db]["database"]}'"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Drop test databases"
|
37
|
+
task :drop do
|
38
|
+
config = load_config
|
39
|
+
%w[default customer_analytics].each do |db|
|
40
|
+
client = Mysql2::Client.new(
|
41
|
+
host: config[db]["host"],
|
42
|
+
username: config[db]["username"],
|
43
|
+
password: config[db]["password"]
|
44
|
+
)
|
45
|
+
client.query("DROP DATABASE IF EXISTS `#{config[db]["database"]}`")
|
46
|
+
client.close
|
47
|
+
puts "Dropped database '#{config[db]["database"]}'"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "Run migrations for test databases"
|
52
|
+
task migrate: :create do
|
53
|
+
config = load_config
|
54
|
+
|
55
|
+
# Migrate default database
|
56
|
+
ActiveRecord::Base.establish_connection(config["default"])
|
57
|
+
require_relative "../../db/migrate/create_test_tables"
|
58
|
+
CreateTestTables.new.up
|
59
|
+
puts "Migrated default database"
|
60
|
+
|
61
|
+
# Migrate customer_analytics database
|
62
|
+
ActiveRecord::Base.establish_connection(config["customer_analytics"])
|
63
|
+
CreateTestTables.new.up
|
64
|
+
puts "Migrated customer_analytics database"
|
65
|
+
|
66
|
+
puts "Migration completed successfully."
|
67
|
+
end
|
68
|
+
|
69
|
+
desc "Rollback migrations for test databases"
|
70
|
+
task :rollback do
|
71
|
+
config = load_config
|
72
|
+
|
73
|
+
# Rollback default database
|
74
|
+
ActiveRecord::Base.establish_connection(config["default"])
|
75
|
+
require_relative "../../db/migrate/create_test_tables"
|
76
|
+
CreateTestTables.new.down
|
77
|
+
puts "Rolled back default database"
|
78
|
+
|
79
|
+
# Rollback customer_analytics database
|
80
|
+
ActiveRecord::Base.establish_connection(config["customer_analytics"])
|
81
|
+
CreateTestTables.new.down
|
82
|
+
puts "Rolled back customer_analytics database"
|
83
|
+
|
84
|
+
puts "Rollback completed successfully."
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_veil
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- toshichanapp
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faker
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dotenv
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mysql2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '13.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '13.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.21'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.21'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0.21'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0.21'
|
139
|
+
description: Write a longer description or delete this line.
|
140
|
+
email:
|
141
|
+
- toshichanapp@gmail.com
|
142
|
+
executables:
|
143
|
+
- data_veil
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- ".rspec"
|
148
|
+
- ".rubocop.yml"
|
149
|
+
- CODE_OF_CONDUCT.md
|
150
|
+
- LICENSE.txt
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- exe/data_veil
|
154
|
+
- lib/data_veil.rb
|
155
|
+
- lib/data_veil/cli.rb
|
156
|
+
- lib/data_veil/mask/base.rb
|
157
|
+
- lib/data_veil/mask_data_generator.rb
|
158
|
+
- lib/data_veil/masking/initializer.rb
|
159
|
+
- lib/data_veil/version.rb
|
160
|
+
- sig/data_veil.rbs
|
161
|
+
- tasks/db.rake
|
162
|
+
- tasks/db/migrate/create_test_tables.rb
|
163
|
+
homepage: https://github.com/toshichanapp
|
164
|
+
licenses:
|
165
|
+
- MIT
|
166
|
+
metadata:
|
167
|
+
homepage_uri: https://github.com/toshichanapp
|
168
|
+
source_code_uri: https://github.com/toshichanapp/data_veil
|
169
|
+
post_install_message:
|
170
|
+
rdoc_options: []
|
171
|
+
require_paths:
|
172
|
+
- lib
|
173
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: 3.0.0
|
178
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
|
+
requirements:
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: '0'
|
183
|
+
requirements: []
|
184
|
+
rubygems_version: 3.5.22
|
185
|
+
signing_key:
|
186
|
+
specification_version: 4
|
187
|
+
summary: Write a short summary, because RubyGems requires one.
|
188
|
+
test_files: []
|