logstash-filter-urlnormalize 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db30cbadff620704f8de9d79db0a042c6bd61e57d8095c30fbcd03b99d9b6434
4
+ data.tar.gz: 981ef908186d7a932159e5bc29b1a6d2ff9cac0a491776453a977f5f69ce14c0
5
+ SHA512:
6
+ metadata.gz: ae079b61bb3350c4df7b45e2945e081a3bd5906e0645218581a889febecc090916587a004c8c733f809e8eb16bcc72cd5d0711d6ad4444687f333fe34c06853b
7
+ data.tar.gz: e713fb9205f36c1900e947cdd97ade8a49704f7fc20af74fd6d4664a78c8568a38044333f7d681c13c9996eb7848edaea9855e890589a32ef2f6ce09a94be982
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Duknam Yoo - yoo2001818@gmail.com
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-urlnormalize
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require "uri"
5
+
6
+ # The urlnormalize filter will replace the contents of the default
7
+ # message field with whatever you specify in the configuration.
8
+ class LogStash::Filters::Urlnormalize < LogStash::Filters::Base
9
+
10
+ # Setting the config_name here is required. This is how you
11
+ # configure this filter from your Logstash config.
12
+ #
13
+ # filter {
14
+ # urlnormalize {
15
+ # field => "message"
16
+ # path_field => "path_normalized"
17
+ # hosts => [
18
+ # "localhost"
19
+ # ]
20
+ # patterns => {
21
+ # "/posts/:content_id" => { "content_type" => "post" }
22
+ # }
23
+ # }
24
+ # }
25
+ #
26
+ config_name "urlnormalize"
27
+
28
+ # The field where the URL is normalized
29
+ config :field, :validate => :string, :default => "message"
30
+
31
+ # The field where the normalized path value of URL is written
32
+ config :path_field, :validate => :string, :default => "path_normalized"
33
+
34
+ # The list of hosts (localhost) which the path will be parsed. Defaults to everything.
35
+ config :hosts, :validate => :array, :default => []
36
+
37
+ # The list of path patterns.
38
+ config :patterns, :validate => :hash, :default => {}
39
+
40
+ # The field where the path variables should be written.
41
+ config :variable_field, :validate => :string, :default => ""
42
+
43
+ public
44
+ def register
45
+ # Parse paths and create regular expressions.
46
+ @path_patterns = @patterns.keys.map do |key|
47
+ PathPattern.new(key, @patterns[key])
48
+ end
49
+ if /^\[.+\]$/.match(@variable_field)
50
+ @variable_field = "[" + @variable_field + "]"
51
+ end
52
+ end # def register
53
+
54
+ public
55
+ def filter(event)
56
+ if @field and event.get(@field) != nil
57
+ # Try to parse the URL.
58
+ uri = URI(URI.encode(event.get(@field)))
59
+ if @hosts.empty? or @hosts.include? uri.host
60
+ result = nil
61
+ matched = @path_patterns.find do |pattern|
62
+ r = pattern.test(uri.path)
63
+ result = r
64
+ r != nil
65
+ end
66
+ if matched != nil
67
+ event.set(@path_field, matched.path)
68
+ result.each_pair do |key, value|
69
+ event.set(@variable_field + "[" + key + "]", value)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ # filter_matched should go in the last line of our successful code
76
+ filter_matched(event)
77
+ end # def filter
78
+
79
+ class PathPattern
80
+ attr_accessor :path
81
+
82
+ def initialize(path, constants)
83
+ @path = path
84
+ @constants = constants
85
+ # Parse path using regular expressions.
86
+ result = "^"
87
+ @variables = []
88
+ pos = 0
89
+ depth = 0
90
+ while pos < path.length do
91
+ case path[pos]
92
+ when "\\" then
93
+ pos += 1
94
+ result << Regexp.escape(path[pos])
95
+ when "(" then
96
+ result << "(?:"
97
+ when ")" then
98
+ result << ")?"
99
+ when ":" then
100
+ # Get keyword
101
+ keyword = (/^[a-zA-Z0-9_]+/.match path[pos + 1..-1])[0]
102
+ pos += keyword.length
103
+ result << "([^/]+)"
104
+ @variables << keyword
105
+ when "*" then
106
+ result << ".*?"
107
+ else
108
+ result << Regexp.escape(path[pos])
109
+ end
110
+ pos += 1
111
+ end
112
+ result << "/?$"
113
+ @regex = Regexp.new(result)
114
+ end
115
+
116
+ def test(url)
117
+ result = @regex.match(url)
118
+ return nil if result == nil
119
+ variables = {}
120
+ @variables.each_index do |i|
121
+ variables[@variables[i]] = result[i + 1]
122
+ end
123
+ return @constants.merge(variables)
124
+ end
125
+ end
126
+ end # class LogStash::Filters::Urlnormalize
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-urlnormalize'
3
+ s.version = '1.0.0'
4
+ s.licenses = []
5
+ s.summary = 'Logstash URL normalization filter'
6
+ s.homepage = 'https://github.com/yoo2001818/logstash-filter-urlnormalize'
7
+ s.authors = ['Duknam Yoo']
8
+ s.email = 'yoo2001818@gmail.com'
9
+ s.require_paths = ['lib']
10
+
11
+ # Files
12
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
13
+ # Tests
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+
16
+ # Special flag to let us know this is actually a logstash plugin
17
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
18
+
19
+ # Gem dependencies
20
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
21
+ s.add_development_dependency 'logstash-devutils'
22
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ require_relative "../spec_helper"
3
+ require "logstash/filters/urlnormalize"
4
+
5
+ describe LogStash::Filters::Urlnormalize do
6
+ describe "Should parse pattern" do
7
+ let(:config) do <<-CONFIG
8
+ filter {
9
+ urlnormalize {
10
+ field => "url"
11
+ path_field => "path"
12
+ patterns => {
13
+ "/posts/:content_id" => { "content_type" => "post" }
14
+ }
15
+ }
16
+ }
17
+ CONFIG
18
+ end
19
+
20
+ sample("url" => "http://example.com/posts/55") do
21
+ expect(subject).to include("path")
22
+ expect(subject.get("path")).to eq("/posts/:content_id")
23
+ expect(subject).to include("content_id")
24
+ expect(subject.get("content_id")).to eq("55")
25
+ expect(subject).to include("content_type")
26
+ expect(subject.get("content_type")).to eq("post")
27
+ end
28
+ sample("url" => "http://hello.com/") do
29
+ expect(subject).not_to include("path")
30
+ expect(subject).not_to include("content_id")
31
+ expect(subject).not_to include("content_type")
32
+ end
33
+ sample("url" => nil) do
34
+ end
35
+ sample("url" => "") do
36
+ end
37
+ end
38
+ describe "Should honor hosts" do
39
+ let(:config) do <<-CONFIG
40
+ filter {
41
+ urlnormalize {
42
+ field => "url"
43
+ path_field => "path"
44
+ hosts => [
45
+ "example.com",
46
+ "localhost"
47
+ ]
48
+ patterns => {
49
+ "/posts/:content_id" => { "content_type" => "post" }
50
+ }
51
+ }
52
+ }
53
+ CONFIG
54
+ end
55
+
56
+ sample("url" => "http://example.com/posts/55") do
57
+ expect(subject).to include("path")
58
+ expect(subject.get("path")).to eq("/posts/:content_id")
59
+ expect(subject).to include("content_id")
60
+ expect(subject.get("content_id")).to eq("55")
61
+ expect(subject).to include("content_type")
62
+ expect(subject.get("content_type")).to eq("post")
63
+ end
64
+ sample("url" => "http://127.0.0.1/posts/55") do
65
+ expect(subject).not_to include("path")
66
+ expect(subject).not_to include("content_id")
67
+ expect(subject).not_to include("content_type")
68
+ end
69
+ end
70
+ describe "Should honor variable_field" do
71
+ let(:config) do <<-CONFIG
72
+ filter {
73
+ urlnormalize {
74
+ field => "url"
75
+ path_field => "path"
76
+ variable_field => "data"
77
+ patterns => {
78
+ "/posts/:content_id" => { "content_type" => "post" }
79
+ }
80
+ }
81
+ }
82
+ CONFIG
83
+ end
84
+
85
+ sample("url" => "http://example.com/posts/55") do
86
+ expect(subject).to include("path")
87
+ expect(subject.get("path")).to eq("/posts/:content_id")
88
+ expect(subject).to include("data")
89
+ data = subject.get("data")
90
+ expect(data).to include("content_id")
91
+ expect(data["content_id"]).to eq("55")
92
+ expect(data).to include("content_type")
93
+ expect(data["content_type"]).to eq("post")
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-urlnormalize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Duknam Yoo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: logstash-devutils
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: yoo2001818@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - CHANGELOG.md
48
+ - CONTRIBUTORS
49
+ - DEVELOPER.md
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - lib/logstash/filters/urlnormalize.rb
54
+ - logstash-filter-urlnormalize.gemspec
55
+ - spec/filters/urlnormalize_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: https://github.com/yoo2001818/logstash-filter-urlnormalize
58
+ licenses: []
59
+ metadata:
60
+ logstash_plugin: 'true'
61
+ logstash_group: filter
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.13
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Logstash URL normalization filter
82
+ test_files:
83
+ - spec/filters/urlnormalize_spec.rb
84
+ - spec/spec_helper.rb