fluent-plugin-string-scrub 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +11 -0
- data/fluent-plugin-string-scrub.gemspec +23 -0
- data/lib/fluent/plugin/out_string_scrub.rb +85 -0
- data/test/helper.rb +28 -0
- data/test/plugin/test_out_string-scrub.rb +122 -0
- metadata +122 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Noriaki Katayama
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# fluent-plugin-string-scrub [![Build Status](https://travis-ci.org/kataring/fluent-plugin-string-scrub.svg)](https://travis-ci.org/kataring/fluent-plugin-string-scrub)
|
2
|
+
|
3
|
+
fluent plugin for string scrub.
|
4
|
+
|
5
|
+
## [String#scrub](https://github.com/hsbt/string-scrub)
|
6
|
+
|
7
|
+
>If the given string contains an invalid byte sequence then that invalid byte sequence is replaced with the [unicode replacement character](http://www.fileformat.info/info/unicode/char/0fffd/index.htm) (�) and a new string is returned.
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'fluent-plugin-string-scrub'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install fluent-plugin-string-scrub
|
25
|
+
|
26
|
+
## Configuration
|
27
|
+
|
28
|
+
```
|
29
|
+
<match **>
|
30
|
+
type string_scrub
|
31
|
+
tag scrubbed.string
|
32
|
+
replace_char ?
|
33
|
+
</match>
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
```
|
39
|
+
<source>
|
40
|
+
type forward
|
41
|
+
</source>
|
42
|
+
|
43
|
+
<match raw.**>
|
44
|
+
type string_scrub
|
45
|
+
remove_prefix raw
|
46
|
+
add_prefix scrubbed
|
47
|
+
</match>
|
48
|
+
|
49
|
+
<match scrubbed.**>
|
50
|
+
type stdout
|
51
|
+
</match>
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( https://github.com/[my-github-username]/fluent-plugin-string-scrub/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "fluent-plugin-string-scrub"
|
6
|
+
spec.version = "0.0.2"
|
7
|
+
spec.authors = ["Noriaki Katayama"]
|
8
|
+
spec.email = ["kataring@gmail.com"]
|
9
|
+
spec.summary = %q{Fluentd Output filter plugin.}
|
10
|
+
spec.description = %q{fluent plugin for string scrub.}
|
11
|
+
spec.homepage = "https://github.com/kataring/fluent-plugin-string-scrub"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "rake"
|
20
|
+
spec.add_development_dependency "test-unit"
|
21
|
+
spec.add_development_dependency "fluentd"
|
22
|
+
spec.add_runtime_dependency "string-scrub", "~> 0.0.5" if RUBY_VERSION.to_f < 2.1
|
23
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
class Fluent::StringScrubOutput < Fluent::Output
|
2
|
+
Fluent::Plugin.register_output('string_scrub', self)
|
3
|
+
|
4
|
+
config_param :tag, :string, :default => nil
|
5
|
+
config_param :remove_prefix, :string, :default => nil
|
6
|
+
config_param :add_prefix, :string, :default => nil
|
7
|
+
config_param :replace_char, :string, :default => ''
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
super
|
11
|
+
require 'string/scrub' if RUBY_VERSION.to_f < 2.1
|
12
|
+
end
|
13
|
+
|
14
|
+
def configure(conf)
|
15
|
+
super
|
16
|
+
|
17
|
+
if not @tag and not @remove_prefix and not @add_prefix
|
18
|
+
raise Fluent::ConfigError, "missing both of remove_prefix and add_prefix"
|
19
|
+
end
|
20
|
+
if @tag and (@remove_prefix or @add_prefix)
|
21
|
+
raise Fluent::ConfigError, "both of tag and remove_prefix/add_prefix must not be specified"
|
22
|
+
end
|
23
|
+
if @remove_prefix
|
24
|
+
@removed_prefix_string = @remove_prefix + '.'
|
25
|
+
@removed_length = @removed_prefix_string.length
|
26
|
+
end
|
27
|
+
if @add_prefix
|
28
|
+
@added_prefix_string = @add_prefix + '.'
|
29
|
+
end
|
30
|
+
|
31
|
+
if @replace_char and @replace_char.length >= 2
|
32
|
+
raise Fluent::ConfigError, "replace_char: mast be 1 character"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def emit(tag, es, chain)
|
37
|
+
tag = if @tag
|
38
|
+
@tag
|
39
|
+
else
|
40
|
+
if @remove_prefix and
|
41
|
+
( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix)
|
42
|
+
tag = tag[@removed_length..-1]
|
43
|
+
end
|
44
|
+
if @add_prefix
|
45
|
+
tag = if tag and tag.length > 0
|
46
|
+
@added_prefix_string + tag
|
47
|
+
else
|
48
|
+
@add_prefix
|
49
|
+
end
|
50
|
+
end
|
51
|
+
tag
|
52
|
+
end
|
53
|
+
|
54
|
+
es.each do |time,record|
|
55
|
+
scrubbed = recv_record(record)
|
56
|
+
next if scrubbed.nil?
|
57
|
+
Fluent::Engine.emit(tag, time, scrubbed)
|
58
|
+
end
|
59
|
+
|
60
|
+
chain.next
|
61
|
+
end
|
62
|
+
|
63
|
+
def recv_record(record)
|
64
|
+
scrubbed = {}
|
65
|
+
record.each do |k,v|
|
66
|
+
if v.instance_of? Hash
|
67
|
+
scrubbed[with_scrub(k)] = recv_record(v)
|
68
|
+
else
|
69
|
+
scrubbed[with_scrub(k)] = with_scrub(v)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
scrubbed
|
73
|
+
end
|
74
|
+
|
75
|
+
def with_scrub(string)
|
76
|
+
begin
|
77
|
+
string =~ //
|
78
|
+
return string
|
79
|
+
rescue ArgumentError => e
|
80
|
+
raise e unless e.message.index("invalid byte sequence in") == 0
|
81
|
+
string.scrub!(@replace_char)
|
82
|
+
retry
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
require 'fluent/test'
|
15
|
+
unless ENV.has_key?('VERBOSE')
|
16
|
+
nulllogger = Object.new
|
17
|
+
nulllogger.instance_eval {|obj|
|
18
|
+
def method_missing(method, *args)
|
19
|
+
# pass
|
20
|
+
end
|
21
|
+
}
|
22
|
+
$log = nulllogger
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'fluent/plugin/out_string_scrub'
|
26
|
+
|
27
|
+
class Test::Unit::TestCase
|
28
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class StringScrubOutputTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Fluent::Test.setup
|
6
|
+
end
|
7
|
+
|
8
|
+
CONFIG = %[
|
9
|
+
remove_prefix input
|
10
|
+
add_prefix scrubbed
|
11
|
+
]
|
12
|
+
|
13
|
+
CONFIG_REPLACE_CHAR = %[
|
14
|
+
remove_prefix input
|
15
|
+
add_prefix scrubbed
|
16
|
+
replace_char ?
|
17
|
+
]
|
18
|
+
|
19
|
+
def create_driver(conf=CONFIG,tag='test')
|
20
|
+
Fluent::Test::OutputTestDriver.new(Fluent::StringScrubOutput, tag).configure(conf)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_configure
|
24
|
+
assert_raise(Fluent::ConfigError) {
|
25
|
+
d = create_driver('')
|
26
|
+
}
|
27
|
+
assert_raise(Fluent::ConfigError) {
|
28
|
+
d = create_driver %[
|
29
|
+
tag foo.bar
|
30
|
+
remove_prefix removed
|
31
|
+
]
|
32
|
+
}
|
33
|
+
assert_raise(Fluent::ConfigError) {
|
34
|
+
d = create_driver %[
|
35
|
+
tag foo.bar
|
36
|
+
add_prefix added
|
37
|
+
]
|
38
|
+
}
|
39
|
+
assert_raise(Fluent::ConfigError) {
|
40
|
+
d = create_driver %[
|
41
|
+
tag foo.bar
|
42
|
+
remove_prefix removed
|
43
|
+
add_prefix added
|
44
|
+
]
|
45
|
+
}
|
46
|
+
assert_nothing_raised {
|
47
|
+
d = create_driver %[
|
48
|
+
tag foo.bar
|
49
|
+
]
|
50
|
+
}
|
51
|
+
assert_nothing_raised {
|
52
|
+
d = create_driver %[
|
53
|
+
add_prefix added
|
54
|
+
]
|
55
|
+
}
|
56
|
+
assert_nothing_raised {
|
57
|
+
d = create_driver %[
|
58
|
+
remove_prefix removed
|
59
|
+
]
|
60
|
+
}
|
61
|
+
assert_nothing_raised {
|
62
|
+
d = create_driver %[
|
63
|
+
remove_prefix removed
|
64
|
+
add_prefix added
|
65
|
+
]
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_emit1_invalid_string
|
70
|
+
orig_message = 'testtesttest'
|
71
|
+
invalid_utf8 = "\xff".force_encoding('UTF-8')
|
72
|
+
d1 = create_driver(CONFIG, 'input.log')
|
73
|
+
d1.run do
|
74
|
+
d1.emit({'message' => orig_message + invalid_utf8})
|
75
|
+
end
|
76
|
+
emits = d1.emits
|
77
|
+
assert_equal 1, emits.length
|
78
|
+
|
79
|
+
e1 = emits[0]
|
80
|
+
assert_equal "scrubbed.log", e1[0]
|
81
|
+
assert_equal orig_message, e1[2]['message']
|
82
|
+
|
83
|
+
invalid_ascii = "\xff".force_encoding('US-ASCII')
|
84
|
+
d2 = create_driver(CONFIG, 'input.log2')
|
85
|
+
d2.run do
|
86
|
+
d2.emit({'message' => orig_message + invalid_utf8})
|
87
|
+
end
|
88
|
+
emits = d2.emits
|
89
|
+
assert_equal 1, emits.length
|
90
|
+
|
91
|
+
e2 = emits[0]
|
92
|
+
assert_equal "scrubbed.log2", e2[0]
|
93
|
+
assert_equal orig_message, e2[2]['message']
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_emit2_replace_char
|
97
|
+
orig_message = 'testtesttest'
|
98
|
+
invalid_utf8 = "\xff".force_encoding('UTF-8')
|
99
|
+
d1 = create_driver(CONFIG_REPLACE_CHAR, 'input.log')
|
100
|
+
d1.run do
|
101
|
+
d1.emit({'message' => orig_message + invalid_utf8})
|
102
|
+
end
|
103
|
+
emits = d1.emits
|
104
|
+
assert_equal 1, emits.length
|
105
|
+
|
106
|
+
e1 = emits[0]
|
107
|
+
assert_equal "scrubbed.log", e1[0]
|
108
|
+
assert_equal orig_message + '?', e1[2]['message']
|
109
|
+
|
110
|
+
invalid_ascii = "\xff".force_encoding('US-ASCII')
|
111
|
+
d2 = create_driver(CONFIG_REPLACE_CHAR, 'input.log2')
|
112
|
+
d2.run do
|
113
|
+
d2.emit({'message' => orig_message + invalid_utf8})
|
114
|
+
end
|
115
|
+
emits = d2.emits
|
116
|
+
assert_equal 1, emits.length
|
117
|
+
|
118
|
+
e2 = emits[0]
|
119
|
+
assert_equal "scrubbed.log2", e2[0]
|
120
|
+
assert_equal orig_message + '?', e2[2]['message']
|
121
|
+
end
|
122
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-string-scrub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Noriaki Katayama
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-01-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: test-unit
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: fluentd
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: string-scrub
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.0.5
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.0.5
|
78
|
+
description: fluent plugin for string scrub.
|
79
|
+
email:
|
80
|
+
- kataring@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- fluent-plugin-string-scrub.gemspec
|
92
|
+
- lib/fluent/plugin/out_string_scrub.rb
|
93
|
+
- test/helper.rb
|
94
|
+
- test/plugin/test_out_string-scrub.rb
|
95
|
+
homepage: https://github.com/kataring/fluent-plugin-string-scrub
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.23.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Fluentd Output filter plugin.
|
120
|
+
test_files:
|
121
|
+
- test/helper.rb
|
122
|
+
- test/plugin/test_out_string-scrub.rb
|