fluent-plugin-sanitizer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4955af12965cbeeb185d2d62e6049804e063d679a64f6aabf392ec2413b3545
4
- data.tar.gz: 961c45840adf34d58080f978e71b4dd9b562fa87e76766d439fe2f8088377c71
3
+ metadata.gz: be5c045333ced653e07d2dcaf634ac05d2896441778e29a5d7baf60e7eee6cc8
4
+ data.tar.gz: 23b29b983f14c2f43b2460eaaf0ab0284ade49e1289ae4db741f16d5b2c00a3c
5
5
  SHA512:
6
- metadata.gz: 9895d0a3cdaa2524affac2ccd33411964948dbc7faefa34d3295d2d8d86bfea36e744615a62de51f24da9519b20fefe3f95155320eb058ad4c4b372c20f09578
7
- data.tar.gz: 553c62ebcf205ff05d405ddf054fe36b04adac50236744d1a044b50d50a9cde1f791d5a4812d68fd205b4a4da7b82f3dc0f7f1f3a913d8ede2f7c22a2fb53594
6
+ metadata.gz: '09818b00aea38d0afc90a34660b6df04be062cef758fa09aa62b893c6fc17ccf44e466fc30dfe1559c9d32a341c8fa31496d1f4d214f4c82ac135130b17cd998'
7
+ data.tar.gz: d7a609f1afc62e3118d311bb4b5708880e4e40ff88e07a945cb485f23240f91a05b9781f22279229fbef32563084eb2ee9850fbdd1a054a587f8cff9abaa28ef
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "fluent-plugin-sanitizer"
6
- spec.version = "0.1.0"
6
+ spec.version = "0.1.1"
7
7
  spec.authors = ["TK Kubota"]
8
8
  spec.email = ["tkubota@ctc-america.com"]
9
9
 
@@ -24,7 +24,7 @@ module Fluent
24
24
  helpers :event_emitter, :record_accessor
25
25
 
26
26
  desc "Hash salt to be used to generate hash values with MD5(optional)"
27
- config_param :hash_salt, :string, default: nil
27
+ config_param :hash_salt, :string, default: ""
28
28
 
29
29
  config_section :rule, param_name: :rules, multi: true do
30
30
  desc "Name of keys whose valuse are to be sanitized"
@@ -35,210 +35,192 @@ module Fluent
35
35
  config_param :pattern_fqdn, :bool, default: false
36
36
  desc "Sanitize if values mactch custom regular expression (optional)"
37
37
  config_param :pattern_regex, :regexp, default: /^$/
38
+ desc "Prefix for pattern_regex (optional)"
39
+ config_param :pattern_regex_prefix, :string, default: "Regex"
38
40
  desc "Sanitize if values mactch custom keywords (optional)"
39
41
  config_param :pattern_keywords, :array, default: []
42
+ desc "Prefix for pattern_keywords (optional)"
43
+ config_param :pattern_keywords_prefix, :string, default: "Keywords"
40
44
  end
41
45
 
42
46
  def configure(conf)
43
47
  super
44
48
 
45
- @salt = ""
46
- @salt = conf['hash_salt'] if conf['hash_salt'] != nil
49
+ @salt = conf['hash_salt']
47
50
 
48
51
  @sanitizerules = []
49
52
  @rules.each do |rule|
50
53
  if rule.keys.empty?
51
54
  raise Fluent::ConfigError, "You need to specify at least one key in rule statement."
52
55
  else
56
+ #keys = record_accessor_create(rule.keys)
53
57
  keys = rule.keys
54
58
  end
55
59
 
56
- if rule.pattern_ipv4 == true || rule.pattern_ipv4 == false
60
+ #record_accessor_create(rule.keys)
61
+
62
+ if rule.pattern_ipv4 || !rule.pattern_ipv4
57
63
  pattern_ipv4 = rule.pattern_ipv4
58
64
  else
59
65
  raise Fluent::ConfigError, "true or false is available for pattern_ipv4 option."
60
66
  end
61
67
 
62
- if rule.pattern_fqdn == true || rule.pattern_fqdn == false
68
+ if rule.pattern_fqdn || !rule.pattern_fqdn
63
69
  pattern_fqdn = rule.pattern_fqdn
64
70
  else
65
71
  raise Fluent::ConfigError, "true or false is available for pattern_fqdn option."
66
72
  end
67
-
68
- pattern_regex = rule.pattern_regex
73
+
74
+ if rule.pattern_regex.class == Regexp
75
+ pattern_regex = rule.pattern_regex
76
+ else
77
+ raise Fluent::ConfigError, "Your need to specify Regexp for pattern_fqdn option."
78
+ end
79
+
69
80
  pattern_keywords = rule.pattern_keywords
70
81
 
71
82
  case [pattern_ipv4, pattern_fqdn, pattern_regex, pattern_keywords.empty?]
72
83
  when [false, false, /^$/, true]
73
84
  raise Fluent::ConfigError, "You need to specify at least one pattern option in the rule statement."
74
85
  end
75
- @sanitizerules.push([keys, pattern_ipv4, pattern_fqdn, pattern_regex, pattern_keywords])
86
+
87
+ regex_prefix = rule.pattern_regex_prefix
88
+ keywords_prefix = rule.pattern_keywords_prefix
89
+
90
+ @sanitizerules.push([keys, pattern_ipv4, pattern_fqdn, pattern_regex, pattern_keywords, regex_prefix, keywords_prefix])
76
91
  end
77
92
  end
78
93
 
79
94
  def filter(tag, time, record)
80
- @sanitizerules.each do |keys, pattern_ipv4, pattern_fqdn, pattern_regex, pattern_keywords|
95
+ @sanitizerules.each do |keys, pattern_ipv4, pattern_fqdn, pattern_regex, pattern_keywords, regex_prefix, keywords_prefix|
81
96
  keys.each do |key|
82
- if key.include?(".")
83
- nkey = key.split(".")
84
- if nkey.length ==2
85
- if record[nkey[0]].key?(nkey[1])
86
- v = record[nkey[0]][nkey[1]]
87
- record[nkey[0]][nkey[1]] = sanitize_ipv4_val(@salt, record[nkey[0]][nkey[1]]) if pattern_ipv4 == true
88
- record[nkey[0]][nkey[1]] = sanitize_fqdn_val(@salt, record[nkey[0]][nkey[1]]) if pattern_fqdn == true
89
- record[nkey[0]][nkey[1]] = sanitize_regex(@salt, record[nkey[0]][nkey[1]]) if is_regex?(pattern_regex) && !!(pattern_regex =~ record[nkey[0]][nkey[1]])
90
- record[nkey[0]][nkey[1]] = sanitize_keyword(@salt, pattern_keywords, record[nkey[0]][nkey[1]]) if pattern_keywords.empty? == false
91
- else
92
- $log.error "no such nested key found : key name = #{key}"
93
- end
94
- elsif nkey.length ==3
95
- if record[nkey[0]][nkey[1]].key?(nkey[2])
96
- v = record[nkey[0]][nkey[1]][nkey[2]]
97
- record[nkey[0]][nkey[1]][nkey[2]] = sanitize_ipv4_val(@salt, record[nkey[0]][nkey[1]][nkey[2]]) if pattern_ipv4 == true
98
- record[nkey[0]][nkey[1]][nkey[2]] = sanitize_fqdn_val(@salt, record[nkey[0]][nkey[1]][nkey[2]]) if pattern_fqdn == true
99
- record[nkey[0]][nkey[1]][nkey[2]] = sanitize_regex(@salt, record[nkey[0]][nkey[1]][nkey[2]]) if is_regex?(pattern_regex) && !!(pattern_regex =~ record[nkey[0]][nkey[1]][nkey[2]])
100
- record[nkey[0]][nkey[1]][nkey[2]] = sanitize_keyword(@salt, pattern_keywords, record[nkey[0]][nkey[1]][nkey[2]]) if pattern_keywords.empty? == false
101
- else
102
- $log.error "no such nested key found : key name = #{key}"
103
- end
104
- end
105
- else
106
- if record.key?(key)
107
- v = record[key]
108
- record[key] = sanitize_ipv4_val(@salt, record[key]) if pattern_ipv4 == true
109
- record[key] = sanitize_fqdn_val(@salt, record[key]) if pattern_fqdn == true
110
- record[key] = sanitize_regex(@salt, v) if is_regex?(pattern_regex) && !!(pattern_regex =~ record[key])
111
- record[key] = sanitize_keyword_val(@salt, pattern_keywords, v) if pattern_keywords.empty? == false
112
- else
113
- $log.error "no such key found : key name = #{key}"
114
- end
115
- end
97
+ accessor = record_accessor_create("$."+key.to_s)
98
+ accessor.set(record, sanitize_ipv4_val(accessor.call(record).to_s)) if pattern_ipv4
99
+ accessor.set(record, sanitize_fqdn_val(accessor.call(record).to_s)) if pattern_fqdn
100
+ accessor.set(record, sanitize_regex_val(accessor.call(record).to_s, regex_prefix)) if accessor.call(record).to_s.match?(pattern_regex)
101
+ accessor.set(record, sanitize_keywords_val(accessor.call(record).to_s, pattern_keywords, keywords_prefix)) if !pattern_keywords.empty?
116
102
  end
117
103
  end
118
104
  record
119
105
  end
120
106
 
121
107
  def include_ipv4?(str)
122
- !!(str =~ /^.*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}.*$/)
108
+ str.match?(/^.*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}.*$/)
123
109
  end
124
110
 
125
111
  def is_ipv4?(str)
126
- !!(str =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
112
+ str.match?(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
127
113
  end
128
114
 
129
115
  def is_ipv4_port?(str)
130
- !!(str =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:[0-9]{1,5}$/)
116
+ str.match?(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:[0-9]{1,5}$/)
131
117
  end
132
118
 
133
119
  def include_fqdn?(str)
134
- !!(str =~ /^.*\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z]){2,}.*$/)
120
+ str.match?(/^.*\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z]){2,}.*$/)
135
121
  end
136
122
 
137
123
  def is_fqdn?(str)
138
- !!(str =~ /^\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z]){2,}$/)
124
+ str.match?(/^\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z]){2,}$/)
139
125
  end
140
126
 
141
127
  def is_fqdn_port?(str)
142
- !!(str =~ /^\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z]){2,}:[0-9]{1,5}$/)
128
+ str.match?(/^\b(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.){2,}([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z]){2,}:[0-9]{1,5}$/)
143
129
  end
144
130
 
145
131
  def is_url?(str)
146
- !!(str =~ /^[a-zA-Z0-9]{2,}:\/\/.*$/)
147
- end
148
-
149
- def is_regex?(regex)
150
- return regex.class == Regexp
132
+ str.match?(/^[a-zA-Z0-9]{2,}:\/\/.*$/)
151
133
  end
152
134
 
153
135
  def subtract_quotations(str)
154
- return str.gsub(/\\\"|\'|\"|\\\'/,'')
136
+ str.gsub(/\\\"|\'|\"|\\\'/,'')
155
137
  end
156
138
 
157
- def sanitize_ipv4(salt, str)
158
- return str, "IPv4_"+Digest::MD5.hexdigest(salt + str)
139
+ def sanitize_ipv4(str)
140
+ return "IPv4_"+Digest::MD5.hexdigest(@salt + str)
159
141
  end
160
142
 
161
- def sanitize_fqdn(salt, str)
162
- return str, "FQDN_"+Digest::MD5.hexdigest(salt + str)
143
+ def sanitize_fqdn(str)
144
+ return "FQDN_"+Digest::MD5.hexdigest(@salt + str)
163
145
  end
164
146
 
165
- def sanitize_regex(salt, str)
166
- return "Regex_"+Digest::MD5.hexdigest(salt + str)
147
+ def sanitize_regex(str, prefix)
148
+ return prefix + "_" + Digest::MD5.hexdigest(@salt + str)
167
149
  end
168
150
 
169
- def sanitize_keyword(salt, str)
170
- return "Keyword_"+Digest::MD5.hexdigest(salt + str)
151
+ def sanitize_keyword(str, prefix)
152
+ return prefix + "_" + Digest::MD5.hexdigest(@salt + str)
171
153
  end
172
154
 
173
- def sanitize_ipv4_port(salt, str)
155
+ def sanitize_ipv4_port(str)
174
156
  ip_port = []
175
157
  str.split(":").each do |s|
176
- b, s = sanitize_ipv4(salt, s) if is_ipv4?(s)
158
+ s = sanitize_ipv4(s) if is_ipv4?(s)
177
159
  ip_port.push(s)
178
160
  end
179
- return str, ip_port.join(":")
161
+ return ip_port.join(":")
180
162
  end
181
163
 
182
- def sanitize_fqdn_port(salt, str)
164
+ def sanitize_fqdn_port(str)
183
165
  fqdn_port = []
184
166
  str.split(":").each do |s|
185
- b, s = sanitize_fqdn(salt, s) if is_fqdn?(s)
167
+ s = sanitize_fqdn(s) if is_fqdn?(s)
186
168
  fqdn_port.push(s)
187
169
  end
188
- return str, fqdn_port.join(":")
170
+ return fqdn_port.join(":")
189
171
  end
190
172
 
191
- def sanitize_ipv4_url(salt, str)
173
+ def sanitize_ipv4_url(str)
192
174
  ip_url = []
193
175
  str.split("://").each do |s|
194
176
  if s.include?("/")
195
177
  url_slash = []
196
178
  s.split("/").each do |ss|
197
- b, ss = sanitize_ipv4(salt, ss) if is_ipv4?(ss)
198
- b, ss = sanitize_ipv4_port(salt, ss) if is_ipv4_port?(ss)
179
+ ss = sanitize_ipv4(ss) if is_ipv4?(ss)
180
+ ss = sanitize_ipv4_port(ss) if is_ipv4_port?(ss)
199
181
  url_slash.push(ss)
200
182
  end
201
183
  s = url_slash.join("/")
202
184
  else
203
- b, s = sanitize_ipv4(salt, s) if is_ipv4?(s)
204
- b, s = sanitize_ipv4_port(salt, s) if is_ipv4_port?(s)
185
+ s = sanitize_ipv4_port(s) if is_ipv4_port?(s)
186
+ s = sanitize_ipv4_port(s) if is_ipv4_port?(s)
205
187
  end
206
188
  ip_url.push(s)
207
189
  end
208
- return str, ip_url.join("://")
190
+ return ip_url.join("://")
209
191
  end
210
192
 
211
- def sanitize_fqdn_url(salt, str)
193
+ def sanitize_fqdn_url(str)
212
194
  fqdn_url = []
213
195
  str.split("://").each do |s|
214
196
  if s.include?("/")
215
197
  url_slash = []
216
198
  s.split("/").each do |ss|
217
- b, ss = sanitize_fqdn(salt, ss) if is_fqdn?(ss)
218
- b, ss = sanitize_fqdn_port(salt, ss) if is_fqdn_port?(ss)
199
+ ss = sanitize_fqdn(ss) if is_fqdn?(ss)
200
+ ss = sanitize_fqdn_port(ss) if is_fqdn_port?(ss)
219
201
  url_slash.push(ss)
220
202
  end
221
203
  s = url_slash.join("/")
222
204
  else
223
- b, s = sanitize_fqdn(salt, s) if is_fqdn?(s)
224
- b, s = sanitize_fqdn_port(salt, s) if is_fqdn_port?(s)
205
+ s = sanitize_fqdn(s) if is_fqdn?(s)
206
+ s = sanitize_fqdn_port(s) if is_fqdn_port?(s)
225
207
  end
226
208
  fqdn_url.push(s)
227
209
  end
228
- return str, fqdn_url.join("://")
210
+ return fqdn_url.join("://")
229
211
  end
230
212
 
231
- def sanitize_ipv4_val(salt, v)
213
+ def sanitize_ipv4_val(v)
232
214
  line = []
233
215
  if v.include?(",")
234
216
  v.split(",").each do |s|
235
217
  s = subtract_quotations(s)
236
218
  if include_ipv4?(s)
237
219
  if is_url?(s)
238
- b, s = sanitize_ipv4_url(salt, s)
220
+ s = sanitize_ipv4_url(s)
239
221
  else
240
- b, s = sanitize_ipv4(salt, s) if is_ipv4?(s)
241
- b, s = sanitize_ipv4_port(salt, s) if is_ipv4_port?(s)
222
+ s = sanitize_ipv4(s) if is_ipv4?(s)
223
+ s = sanitize_ipv4_port(s) if is_ipv4_port?(s)
242
224
  end
243
225
  end
244
226
  line.push(s)
@@ -249,29 +231,30 @@ module Fluent
249
231
  s = subtract_quotations(s)
250
232
  if include_ipv4?(s)
251
233
  if is_url?(s)
252
- b, s = sanitize_ipv4_url(salt, s)
234
+ s = sanitize_ipv4_url(s)
253
235
  else
254
- b, s = sanitize_ipv4(salt, s) if is_ipv4?(s)
255
- b, s = sanitize_ipv4_port(salt, s) if is_ipv4_port?(s)
236
+ s = sanitize_ipv4(s) if is_ipv4?(s)
237
+ s = sanitize_ipv4_port(s) if is_ipv4_port?(s)
256
238
  end
257
239
  end
258
240
  line.push(s)
259
241
  end
242
+ $log.debug "[pattern_ipv4] sanitize '#{v}' to '#{line.join(" ")}'" if v != line.join(" ")
260
243
  return line.join(" ")
261
244
  end
262
245
  end
263
246
 
264
- def sanitize_fqdn_val(salt, v)
247
+ def sanitize_fqdn_val(v)
265
248
  line = []
266
249
  if v.include?(",")
267
250
  v.split(",").each do |s|
268
251
  s = subtract_quotations(s)
269
252
  if include_fqdn?(s)
270
253
  if is_url?(s)
271
- b, s = sanitize_fqdn_url(salt, s)
254
+ s = sanitize_fqdn_url(s)
272
255
  else
273
- b, s = sanitize_fqdn(salt, s) if is_fqdn?(s)
274
- b, s = sanitize_fqdn_port(salt, s) if is_fqdn_port?(s)
256
+ s = sanitize_fqdn(s) if is_fqdn?(s)
257
+ s = sanitize_fqdn_port(s) if is_fqdn_port?(s)
275
258
  end
276
259
  end
277
260
  line.push(s)
@@ -282,27 +265,35 @@ module Fluent
282
265
  s = subtract_quotations(s)
283
266
  if include_fqdn?(s)
284
267
  if is_url?(s)
285
- b, s = sanitize_fqdn_url(salt, s)
268
+ s = sanitize_fqdn_url(s)
286
269
  else
287
- b, s = sanitize_fqdn(salt, s) if is_fqdn?(s)
288
- b, s = sanitize_fqdn_port(salt, s) if is_fqdn_port?(s)
270
+ s = sanitize_fqdn(s) if is_fqdn?(s)
271
+ s = sanitize_fqdn_port(s) if is_fqdn_port?(s)
289
272
  end
290
273
  end
291
274
  line.push(s)
292
275
  end
276
+ $log.debug "[pattern_fqdn] sanitize '#{v}' to '#{line.join(" ")}'" if v != line.join(" ")
293
277
  return line.join(" ")
294
278
  end
295
279
  end
296
280
 
297
- def sanitize_keyword_val(salt, keywords, v)
281
+ def sanitize_regex_val(v, prefix)
282
+ s = sanitize_regex(v, prefix)
283
+ $log.debug "[pattern_keywords] sanitize '#{v}' to '#{s}'" if v != s
284
+ return s
285
+ end
286
+
287
+ def sanitize_keywords_val(v, keywords, prefix)
298
288
  line = []
299
289
  v.split().each do |vv|
300
290
  if keywords.include?(vv)
301
- line.push(sanitize_keyword(salt, vv))
291
+ line.push(sanitize_keyword(vv, prefix))
302
292
  else
303
293
  line.push(vv)
304
294
  end
305
295
  end
296
+ $log.debug "[pattern_keywords] sanitize '#{v}' to '#{line.join(" ")}'" if v != line.join(" ")
306
297
  return line.join(" ")
307
298
  end
308
299
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-sanitizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TK Kubota
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-27 00:00:00.000000000 Z
11
+ date: 2021-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler