mask_sql 0.1.0
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/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +4 -0
- data/.rubocop_todo.yml +124 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +120 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/mask_sql +5 -0
- data/lib/mask_sql/cli.rb +38 -0
- data/lib/mask_sql/converter.rb +122 -0
- data/lib/mask_sql/version.rb +3 -0
- data/lib/mask_sql.rb +2 -0
- data/mask_sql.gemspec +31 -0
- metadata +160 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: b5ca609251d6a3962619f3f8bbadc2aa40e1be58
|
|
4
|
+
data.tar.gz: 143619986d8f24f62e137a7555ad1b06b81dee39
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 66910e63c190b90c2d7068fb459fb272c0287f7dfe1d906835890d421efc0628c6f8d37bedf55523c07a0021612b7e34fb05b5e0f984aa2901f0b1b9646ca7d6
|
|
7
|
+
data.tar.gz: 1764029ae3ddc98cb0213f2ee3dde590ea8b9e961db3d50969b0a9faa9f81ad40e70c3936dc05c7bb7025b2e48405120c4015463b6c429d8d8fe1b34d2279935
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# This configuration was generated by
|
|
2
|
+
# `rubocop --auto-gen-config`
|
|
3
|
+
# on 2017-04-17 20:01:29 +0900 using RuboCop version 0.48.1.
|
|
4
|
+
# The point is for the user to remove these configuration records
|
|
5
|
+
# one by one as the offenses are removed from the code base.
|
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
|
8
|
+
|
|
9
|
+
# Offense count: 2
|
|
10
|
+
Lint/NonLocalExitFromIterator:
|
|
11
|
+
Exclude:
|
|
12
|
+
- 'lib/mask_sql/converter.rb'
|
|
13
|
+
|
|
14
|
+
# Offense count: 1
|
|
15
|
+
Metrics/AbcSize:
|
|
16
|
+
Max: 44
|
|
17
|
+
|
|
18
|
+
# Offense count: 6
|
|
19
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
|
20
|
+
Metrics/BlockLength:
|
|
21
|
+
Max: 270
|
|
22
|
+
|
|
23
|
+
# Offense count: 2
|
|
24
|
+
Metrics/CyclomaticComplexity:
|
|
25
|
+
Max: 7
|
|
26
|
+
|
|
27
|
+
# Offense count: 49
|
|
28
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
|
29
|
+
# URISchemes: http, https
|
|
30
|
+
Metrics/LineLength:
|
|
31
|
+
Max: 153
|
|
32
|
+
|
|
33
|
+
# Offense count: 3
|
|
34
|
+
# Configuration parameters: CountComments.
|
|
35
|
+
Metrics/MethodLength:
|
|
36
|
+
Max: 29
|
|
37
|
+
|
|
38
|
+
# Offense count: 2
|
|
39
|
+
Style/Documentation:
|
|
40
|
+
Exclude:
|
|
41
|
+
- 'spec/**/*'
|
|
42
|
+
- 'test/**/*'
|
|
43
|
+
- 'lib/mask_sql/cli.rb'
|
|
44
|
+
- 'lib/mask_sql/converter.rb'
|
|
45
|
+
|
|
46
|
+
# Offense count: 1
|
|
47
|
+
# Cop supports --auto-correct.
|
|
48
|
+
Style/EmptyLineAfterMagicComment:
|
|
49
|
+
Exclude:
|
|
50
|
+
- 'mask_sql.gemspec'
|
|
51
|
+
|
|
52
|
+
# Offense count: 1
|
|
53
|
+
# Cop supports --auto-correct.
|
|
54
|
+
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
|
|
55
|
+
Style/ExtraSpacing:
|
|
56
|
+
Exclude:
|
|
57
|
+
- 'mask_sql.gemspec'
|
|
58
|
+
|
|
59
|
+
# Offense count: 1
|
|
60
|
+
# Cop supports --auto-correct.
|
|
61
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
|
|
62
|
+
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
|
|
63
|
+
Style/HashSyntax:
|
|
64
|
+
Exclude:
|
|
65
|
+
- 'Rakefile'
|
|
66
|
+
|
|
67
|
+
# Offense count: 4
|
|
68
|
+
# Cop supports --auto-correct.
|
|
69
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
70
|
+
# SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
|
|
71
|
+
Style/IndentHeredoc:
|
|
72
|
+
Exclude:
|
|
73
|
+
- 'spec/mask_sql/cli_spec.rb'
|
|
74
|
+
|
|
75
|
+
# Offense count: 2
|
|
76
|
+
# Cop supports --auto-correct.
|
|
77
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
|
|
78
|
+
# SupportedStyles: aligned, indented, indented_relative_to_receiver
|
|
79
|
+
Style/MultilineMethodCallIndentation:
|
|
80
|
+
Exclude:
|
|
81
|
+
- 'lib/mask_sql/converter.rb'
|
|
82
|
+
|
|
83
|
+
# Offense count: 1
|
|
84
|
+
# Cop supports --auto-correct.
|
|
85
|
+
Style/MutableConstant:
|
|
86
|
+
Exclude:
|
|
87
|
+
- 'lib/mask_sql/version.rb'
|
|
88
|
+
|
|
89
|
+
# Offense count: 1
|
|
90
|
+
# Cop supports --auto-correct.
|
|
91
|
+
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
|
|
92
|
+
# SupportedStyles: predicate, comparison
|
|
93
|
+
Style/NumericPredicate:
|
|
94
|
+
Exclude:
|
|
95
|
+
- 'spec/**/*'
|
|
96
|
+
- 'lib/mask_sql/converter.rb'
|
|
97
|
+
|
|
98
|
+
# Offense count: 39
|
|
99
|
+
# Cop supports --auto-correct.
|
|
100
|
+
# Configuration parameters: PreferredDelimiters.
|
|
101
|
+
Style/PercentLiteralDelimiters:
|
|
102
|
+
Exclude:
|
|
103
|
+
- 'lib/mask_sql/cli.rb'
|
|
104
|
+
- 'mask_sql.gemspec'
|
|
105
|
+
- 'spec/mask_sql/cli_spec.rb'
|
|
106
|
+
|
|
107
|
+
# Offense count: 1
|
|
108
|
+
# Cop supports --auto-correct.
|
|
109
|
+
# Configuration parameters: AllowForAlignment.
|
|
110
|
+
Style/SpaceAroundOperators:
|
|
111
|
+
Exclude:
|
|
112
|
+
- 'mask_sql.gemspec'
|
|
113
|
+
|
|
114
|
+
# Offense count: 6
|
|
115
|
+
# Cop supports --auto-correct.
|
|
116
|
+
Style/TrailingWhitespace:
|
|
117
|
+
Exclude:
|
|
118
|
+
- 'spec/mask_sql/cli_spec.rb'
|
|
119
|
+
|
|
120
|
+
# Offense count: 2
|
|
121
|
+
# Cop supports --auto-correct.
|
|
122
|
+
Style/UnneededPercentQ:
|
|
123
|
+
Exclude:
|
|
124
|
+
- 'mask_sql.gemspec'
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 emsk
|
|
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,120 @@
|
|
|
1
|
+
# MaskSQL
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/emsk/mask_sql)
|
|
4
|
+
[](https://coveralls.io/github/emsk/mask_sql)
|
|
5
|
+
[](https://codeclimate.com/github/emsk/mask_sql)
|
|
6
|
+
[](https://gemnasium.com/github.com/emsk/mask_sql)
|
|
7
|
+
[](http://inch-ci.org/github/emsk/mask_sql)
|
|
8
|
+
[](LICENSE.txt)
|
|
9
|
+
|
|
10
|
+
MaskSQL is a command-line tool to mask sensitive values in a SQL file.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
WIP
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
$ mask_sql --in dump.sql --out masked_dump.sql --config mask_config.yml
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Command Options
|
|
23
|
+
|
|
24
|
+
| Option | Alias | Description | Default |
|
|
25
|
+
| :----- | :---- | :---------- | :------ |
|
|
26
|
+
| `--in` | `-i` | Input file path (Required) | |
|
|
27
|
+
| `--out` | `-o` | Output file path (Required) | |
|
|
28
|
+
| `--config` | `-c` | Config YAML file path | `.mask.yml` in the working directory |
|
|
29
|
+
| `--insert` | | `true` if mask `INSERT` SQL | `false`, but `true` if `--insert`, `--replace`, and `--copy` options are not given |
|
|
30
|
+
| `--replace` | | `true` if mask `REPLACE` SQL | `false`, but `true` if `--insert`, `--replace`, and `--copy` options are not given |
|
|
31
|
+
| `--copy` | | `true` if mask `COPY` SQL | `false`, but `true` if `--insert`, `--replace`, and `--copy` options are not given |
|
|
32
|
+
|
|
33
|
+
## Config
|
|
34
|
+
|
|
35
|
+
The following keys are needed in the config YAML file.
|
|
36
|
+
|
|
37
|
+
| Key | Description | Type |
|
|
38
|
+
| :-- | :---------- | :--- |
|
|
39
|
+
| `mark` | Replacement text | String |
|
|
40
|
+
| `targets` | Array of targets | Array |
|
|
41
|
+
| `table` | Target table name | String |
|
|
42
|
+
| `columns` | Columns count of the table | Integer |
|
|
43
|
+
| `indexes` | Target column index (zero-based) and masking text | Hash |
|
|
44
|
+
|
|
45
|
+
## Examples
|
|
46
|
+
|
|
47
|
+
Input file (includes sensitive values):
|
|
48
|
+
|
|
49
|
+
```sql
|
|
50
|
+
INSERT INTO `people` (`id`, `code`, `name`, `email`) VALUES (1,'01','坂本龍馬','ryoma-sakamoto@example.com'),(2,'02','高杉晋作','shinsaku-takasugi@example.com'),(3,'03','沖田総司','soji-okita@example.com');
|
|
51
|
+
INSERT INTO `cats` (`code`, `name`) VALUES ('01','Sora'),('02','Hana'),('03','Leo');
|
|
52
|
+
|
|
53
|
+
REPLACE INTO `people` (`id`, `code`, `name`, `email`) VALUES (1,'01','坂本龍馬','ryoma-sakamoto@example.com'),(2,'02','高杉晋作','shinsaku-takasugi@example.com'),(3,'03','沖田総司','soji-okita@example.com');
|
|
54
|
+
REPLACE INTO `cats` (`code`, `name`) VALUES ('01','Sora'),('02','Hana'),('03','Leo');
|
|
55
|
+
|
|
56
|
+
COPY people (id, code, name, email) FROM stdin;
|
|
57
|
+
1 01 坂本龍馬 ryoma-sakamoto@example.com
|
|
58
|
+
2 02 高杉晋作 shinsaku-takasugi@example.com
|
|
59
|
+
3 03 沖田総司 soji-okita@example.com
|
|
60
|
+
\.
|
|
61
|
+
COPY cats (code, name) FROM stdin;
|
|
62
|
+
01 Sora
|
|
63
|
+
02 Hana
|
|
64
|
+
03 Leo
|
|
65
|
+
\.
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Output file (the sensitive values are masked):
|
|
69
|
+
|
|
70
|
+
```sql
|
|
71
|
+
INSERT INTO `people` (`id`, `code`, `name`, `email`) VALUES (1,'01','氏名1','email-1@example.com'),(2,'02','氏名2','email-2@example.com'),(3,'03','氏名3','email-3@example.com');
|
|
72
|
+
INSERT INTO `cats` (`code`, `name`) VALUES ('code-1','Cat name 1'),('code-2','Cat name 2'),('code-3','Cat name 3');
|
|
73
|
+
|
|
74
|
+
REPLACE INTO `people` (`id`, `code`, `name`, `email`) VALUES (1,'01','氏名1','email-1@example.com'),(2,'02','氏名2','email-2@example.com'),(3,'03','氏名3','email-3@example.com');
|
|
75
|
+
REPLACE INTO `cats` (`code`, `name`) VALUES ('code-1','Cat name 1'),('code-2','Cat name 2'),('code-3','Cat name 3');
|
|
76
|
+
|
|
77
|
+
COPY people (id, code, name, email) FROM stdin;
|
|
78
|
+
1 01 氏名1 email-1@example.com
|
|
79
|
+
2 02 氏名2 email-2@example.com
|
|
80
|
+
3 03 氏名3 email-3@example.com
|
|
81
|
+
\.
|
|
82
|
+
COPY cats (code, name) FROM stdin;
|
|
83
|
+
code-1 Cat name 1
|
|
84
|
+
code-2 Cat name 2
|
|
85
|
+
code-3 Cat name 3
|
|
86
|
+
\.
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Config file:
|
|
90
|
+
|
|
91
|
+
```yaml
|
|
92
|
+
mark: '[mask]'
|
|
93
|
+
targets:
|
|
94
|
+
- table: people
|
|
95
|
+
columns: 4
|
|
96
|
+
indexes:
|
|
97
|
+
2: 氏名[mask]
|
|
98
|
+
3: email-[mask]@example.com
|
|
99
|
+
- table: cats
|
|
100
|
+
columns: 2
|
|
101
|
+
indexes:
|
|
102
|
+
0: code-[mask]
|
|
103
|
+
1: Cat name [mask]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Supported Ruby Versions
|
|
107
|
+
|
|
108
|
+
* Ruby 2.0.0
|
|
109
|
+
* Ruby 2.1
|
|
110
|
+
* Ruby 2.2
|
|
111
|
+
* Ruby 2.3
|
|
112
|
+
* Ruby 2.4
|
|
113
|
+
|
|
114
|
+
## Contributing
|
|
115
|
+
|
|
116
|
+
Bug reports and pull requests are welcome.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
[MIT](LICENSE.txt)
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'mask_sql'
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require 'irb'
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/mask_sql
ADDED
data/lib/mask_sql/cli.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'thor'
|
|
2
|
+
require 'mask_sql/converter'
|
|
3
|
+
|
|
4
|
+
module MaskSQL
|
|
5
|
+
class CLI < Thor
|
|
6
|
+
default_command :mask
|
|
7
|
+
|
|
8
|
+
desc 'mask', 'Mask sensitive values in a SQL file'
|
|
9
|
+
option :in, type: :string, aliases: '-i', required: true, banner: 'INPUT FILE PATH'
|
|
10
|
+
option :out, type: :string, aliases: '-o', required: true, banner: 'OUTPUT FILE PATH'
|
|
11
|
+
option :config, type: :string, aliases: '-c', banner: 'CONFIG FILE PATH'
|
|
12
|
+
option :insert, type: :boolean, banner: 'MASK `INSERT` SQL'
|
|
13
|
+
option :replace, type: :boolean, banner: 'MASK `REPLACE` SQL'
|
|
14
|
+
option :copy, type: :boolean, banner: 'MASK `COPY` SQL'
|
|
15
|
+
|
|
16
|
+
def mask
|
|
17
|
+
converter_options = options.dup
|
|
18
|
+
|
|
19
|
+
if options[:config]
|
|
20
|
+
converter_options[:config] = File.expand_path(options[:config])
|
|
21
|
+
else
|
|
22
|
+
default_config = File.expand_path('.mask.yml')
|
|
23
|
+
converter_options[:config] = default_config if File.exist?(default_config)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
converter = Converter.new(converter_options)
|
|
27
|
+
converter.mask
|
|
28
|
+
puts "\e[32mDone.\e[0m"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
desc 'version, -v, --version', 'Print the version'
|
|
32
|
+
map %w(-v --version) => :version
|
|
33
|
+
|
|
34
|
+
def version
|
|
35
|
+
puts "mask_sql #{MaskSQL::VERSION}"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
require 'csv'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
require 'nkf'
|
|
4
|
+
|
|
5
|
+
module MaskSQL
|
|
6
|
+
class Converter
|
|
7
|
+
def initialize(options)
|
|
8
|
+
@options = options
|
|
9
|
+
|
|
10
|
+
config = YAML.load_file(options[:config])
|
|
11
|
+
@mark = config['mark']
|
|
12
|
+
@targets = config['targets']
|
|
13
|
+
|
|
14
|
+
if options[:insert].nil? && options[:replace].nil? && options[:copy].nil?
|
|
15
|
+
@options[:insert] = true
|
|
16
|
+
@options[:replace] = true
|
|
17
|
+
@options[:copy] = true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
@matched_copy = {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def mask
|
|
24
|
+
encoding = NKF.guess(File.read(@options[:in])).name
|
|
25
|
+
|
|
26
|
+
File.open(@options[:out], "w:#{encoding}") do |out_file|
|
|
27
|
+
File.open(@options[:in], "r:#{encoding}") do |in_file|
|
|
28
|
+
in_file.each_line do |line|
|
|
29
|
+
@matched_copy.empty? ? write_line(line, out_file) : write_copy_line(line, out_file)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def write_line(line, output_file)
|
|
38
|
+
@targets.each do |target|
|
|
39
|
+
table = target['table']
|
|
40
|
+
matched_line = match_line(line, table)
|
|
41
|
+
next unless matched_line
|
|
42
|
+
|
|
43
|
+
if matched_line.names.include?('copy_sql')
|
|
44
|
+
output_file.puts line
|
|
45
|
+
@matched_copy[:sql] = matched_line[:copy_sql]
|
|
46
|
+
@matched_copy[:indexes] = target['indexes']
|
|
47
|
+
@matched_copy[:record_index] = 1
|
|
48
|
+
return
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
columns = target['columns']
|
|
52
|
+
indexes = target['indexes'].keys
|
|
53
|
+
|
|
54
|
+
record_values = CSV.parse(matched_line[:all_values])[0].each_slice(columns).to_a
|
|
55
|
+
record_values.map!.with_index(1) do |values, record_index|
|
|
56
|
+
indexes.each do |mask_index|
|
|
57
|
+
before_value = values[mask_index]
|
|
58
|
+
values[mask_index] = target['indexes'][mask_index].gsub(@mark, record_index.to_s)
|
|
59
|
+
values[mask_index].insert(0, "'") if before_value.start_with?("'", "('")
|
|
60
|
+
values[mask_index].insert(-1, "'") if before_value.end_with?("'", "')")
|
|
61
|
+
values[mask_index].insert(0, '(') if mask_index == 0
|
|
62
|
+
values[mask_index].insert(-1, ')') if mask_index == columns - 1
|
|
63
|
+
end
|
|
64
|
+
values
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
output_file.puts "#{matched_line[:prefix]}#{record_values.join(',')}#{matched_line[:suffix]}"
|
|
68
|
+
return
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
output_file.puts line
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def write_copy_line(line, output_file)
|
|
75
|
+
if /\A\\.\Z/ =~ line
|
|
76
|
+
output_file.puts line
|
|
77
|
+
@matched_copy.clear
|
|
78
|
+
return
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
record_values = CSV.parse(line, col_sep: "\t")[0]
|
|
82
|
+
@matched_copy[:indexes].each do |mask_index, mask_value|
|
|
83
|
+
record_values[mask_index] = mask_value.sub(/\A'/, '')
|
|
84
|
+
.sub(/'\z/, '')
|
|
85
|
+
.gsub(@mark, @matched_copy[:record_index].to_s)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
output_file.puts record_values.join("\t")
|
|
89
|
+
@matched_copy[:record_index] += 1
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def match_line(line, table)
|
|
93
|
+
if @options[:insert]
|
|
94
|
+
matched_line = sql_regexp(table, :insert).match(line)
|
|
95
|
+
return matched_line if matched_line
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
if @options[:replace]
|
|
99
|
+
matched_line = sql_regexp(table, :replace).match(line)
|
|
100
|
+
return matched_line if matched_line
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
if @options[:copy]
|
|
104
|
+
matched_line = sql_regexp(table, :copy).match(line)
|
|
105
|
+
return matched_line if matched_line
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
nil
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def sql_regexp(table, sql_kind)
|
|
112
|
+
case sql_kind
|
|
113
|
+
when :insert
|
|
114
|
+
/\A(?<prefix>INSERT (INTO)?\s*`?#{table}`?.*VALUES\s*)(?<all_values>[^;]+)(?<suffix>;?)\Z/i
|
|
115
|
+
when :replace
|
|
116
|
+
/\A(?<prefix>REPLACE (INTO)?\s*`?#{table}`?.*VALUES\s*)(?<all_values>[^;]+)(?<suffix>;?)\Z/i
|
|
117
|
+
when :copy
|
|
118
|
+
/(?<copy_sql>COPY\s*`?#{table}`?.*FROM stdin;)/i
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
data/lib/mask_sql.rb
ADDED
data/mask_sql.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'mask_sql/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'mask_sql'
|
|
8
|
+
spec.version = MaskSQL::VERSION
|
|
9
|
+
spec.authors = ['emsk']
|
|
10
|
+
spec.email = ['emsk1987@gmail.com']
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Mask sensitive values in a SQL file}
|
|
13
|
+
spec.description = %q{MaskSQL is a command-line tool to mask sensitive values in a SQL file}
|
|
14
|
+
spec.homepage = 'https://github.com/emsk/mask_sql'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
|
19
|
+
end
|
|
20
|
+
spec.bindir = 'exe'
|
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
22
|
+
spec.require_paths = ['lib']
|
|
23
|
+
|
|
24
|
+
spec.add_runtime_dependency 'thor', '~> 0.19'
|
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
|
26
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
29
|
+
spec.add_development_dependency 'rubocop', '~> 0.48'
|
|
30
|
+
spec.add_development_dependency 'simplecov', '~> 0.14'
|
|
31
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mask_sql
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- emsk
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-04-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: thor
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.19'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.19'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.14'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.14'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: coveralls
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0.8'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0.8'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rubocop
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0.48'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0.48'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: simplecov
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0.14'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0.14'
|
|
111
|
+
description: MaskSQL is a command-line tool to mask sensitive values in a SQL file
|
|
112
|
+
email:
|
|
113
|
+
- emsk1987@gmail.com
|
|
114
|
+
executables:
|
|
115
|
+
- mask_sql
|
|
116
|
+
extensions: []
|
|
117
|
+
extra_rdoc_files: []
|
|
118
|
+
files:
|
|
119
|
+
- ".gitignore"
|
|
120
|
+
- ".rspec"
|
|
121
|
+
- ".rubocop.yml"
|
|
122
|
+
- ".rubocop_todo.yml"
|
|
123
|
+
- ".travis.yml"
|
|
124
|
+
- Gemfile
|
|
125
|
+
- LICENSE.txt
|
|
126
|
+
- README.md
|
|
127
|
+
- Rakefile
|
|
128
|
+
- bin/console
|
|
129
|
+
- bin/setup
|
|
130
|
+
- exe/mask_sql
|
|
131
|
+
- lib/mask_sql.rb
|
|
132
|
+
- lib/mask_sql/cli.rb
|
|
133
|
+
- lib/mask_sql/converter.rb
|
|
134
|
+
- lib/mask_sql/version.rb
|
|
135
|
+
- mask_sql.gemspec
|
|
136
|
+
homepage: https://github.com/emsk/mask_sql
|
|
137
|
+
licenses:
|
|
138
|
+
- MIT
|
|
139
|
+
metadata: {}
|
|
140
|
+
post_install_message:
|
|
141
|
+
rdoc_options: []
|
|
142
|
+
require_paths:
|
|
143
|
+
- lib
|
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
|
+
requirements:
|
|
146
|
+
- - ">="
|
|
147
|
+
- !ruby/object:Gem::Version
|
|
148
|
+
version: '0'
|
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
|
+
requirements:
|
|
151
|
+
- - ">="
|
|
152
|
+
- !ruby/object:Gem::Version
|
|
153
|
+
version: '0'
|
|
154
|
+
requirements: []
|
|
155
|
+
rubyforge_project:
|
|
156
|
+
rubygems_version: 2.6.10
|
|
157
|
+
signing_key:
|
|
158
|
+
specification_version: 4
|
|
159
|
+
summary: Mask sensitive values in a SQL file
|
|
160
|
+
test_files: []
|