logstash-filter-useragent2 3.0.0-java

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
+ SHA1:
3
+ metadata.gz: 15bcd828b94689a402662e2dea628cb8cc95c94f
4
+ data.tar.gz: f9940c5b8ec2b47a44cbbfcee5d555823fd5609d
5
+ SHA512:
6
+ metadata.gz: 38cf7c7a81e129908467570c4239c6ee2c9703224ce2c602f8f91469dda02a81271612270e080f628064bf3936dc9fdd912e29286a8e7b59cb717cf23cdbb6e5
7
+ data.tar.gz: 877b57f4ff746cd07261b612f074e736103c288d9487d2b00b036713f82bf2759f28f6ee73b682a1cfd182e74c8601b7955ae9247e099c90bac3d2dbc481c46b
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # 2.0.6
2
+ - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash
3
+ # 2.0.5
4
+ - New dependency requirements for logstash-core for the 5.0 release
5
+ ## 2.0.4
6
+ - Fefactored field references, fixed specs and some cleanups
7
+
8
+ ## 2.0.0
9
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
10
+ instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
11
+ - Dependency on logstash-core update to 2.0
12
+
13
+ ## 2.0.1
14
+ - Add ability to replace source with target
15
+
16
+ ## 1.1.0
17
+ - Add LRU cache
data/CONTRIBUTORS ADDED
@@ -0,0 +1,18 @@
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
+ * Alex Tambellini (atambo)
6
+ * Colin Surprenant (colinsurprenant)
7
+ * Dan Everton (deverton)
8
+ * Jay Luker (lbjay)
9
+ * Jordan Sissel (jordansissel)
10
+ * Kubes (pkubat)
11
+ * Pier-Hugues Pellerin (ph)
12
+ * Richard Pijnenburg (electrical)
13
+ * Suyog Rao (suyograo)
14
+
15
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
16
+ Logstash, and you aren't on the list above and want to be, please let us know
17
+ and we'll make sure you're here. Contributions from folks like you are what make
18
+ open source awesome.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Logstash Plugin
2
+
3
+ [![Build Status](https://travis-ci.org/logstash-plugins/logstash-filter-useragent.svg?branch=master)](https://travis-ci.org/logstash-plugins/logstash-filter-useragent)
4
+
5
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
+
7
+ 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.
8
+
9
+ ## Documentation
10
+
11
+ 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/).
12
+
13
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - 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).
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ - Update your dependencies
37
+
38
+ ```sh
39
+ bundle install
40
+ ```
41
+
42
+ - Run tests
43
+
44
+ ```sh
45
+ bundle exec rspec
46
+ ```
47
+
48
+ ### 2. Running your unpublished Plugin in Logstash
49
+
50
+ #### 2.1 Run in a local Logstash clone
51
+
52
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
53
+ ```ruby
54
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
+ ```
56
+ - Install plugin
57
+ ```sh
58
+ bin/plugin install --no-verify
59
+ ```
60
+ - Run Logstash with your plugin
61
+ ```sh
62
+ bin/logstash -e 'filter {awesome {}}'
63
+ ```
64
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
65
+
66
+ #### 2.2 Run in an installed Logstash
67
+
68
+ 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:
69
+
70
+ - Build your plugin gem
71
+ ```sh
72
+ gem build logstash-filter-awesome.gemspec
73
+ ```
74
+ - Install the plugin from the Logstash home
75
+ ```sh
76
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
77
+ ```
78
+ - Start Logstash and proceed to test the plugin
79
+
80
+ ## Contributing
81
+
82
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
83
+
84
+ 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.
85
+
86
+ It is more important to the community that you are able to contribute.
87
+
88
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,4 @@
1
+ # this is a generated file, to avoid over-writing it just delete this comment
2
+ require 'jar_dependencies'
3
+
4
+ require_jar( 'eu.bitwalker', 'UserAgentUtils', '1.19' )
@@ -0,0 +1,132 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require "lru_redux"
5
+ require "tempfile"
6
+ require "logstash-filter-useragent_jars"
7
+
8
+ java_import "eu.bitwalker.useragentutils.UserAgent"
9
+
10
+ # Parse user agent strings into structured data based on the Java library:
11
+ # <https://github.com/HaraldWalker/user-agent-utils>
12
+ #
13
+ # UserAgent filter, adds information about user agent like family, operating
14
+ # system, version, and device
15
+ #
16
+ class LogStash::Filters::UserAgent < LogStash::Filters::Base
17
+ LOOKUP_CACHE = LruRedux::ThreadSafeCache.new(1000)
18
+
19
+ config_name "useragent2"
20
+
21
+ # The field containing the user agent string. If this field is an
22
+ # array, only the first value will be used.
23
+ config :source, :validate => :string, :required => true
24
+
25
+ # The name of the field to assign user agent data into.
26
+ #
27
+ # If not specified user agent data will be stored in the root of the event.
28
+ config :target, :validate => :string
29
+
30
+ # A string to prepend to all of the extracted keys
31
+ config :prefix, :validate => :string, :default => ''
32
+
33
+ # UA parsing is surprisingly expensive. This filter uses an LRU cache to take advantage of the fact that
34
+ # user agents are often found adjacent to one another in log files and rarely have a random distribution.
35
+ # The higher you set this the more likely an item is to be in the cache and the faster this filter will run.
36
+ # However, if you set this too high you can use more memory than desired.
37
+ #
38
+ # Experiment with different values for this option to find the best performance for your dataset.
39
+ #
40
+ # This MUST be set to a value > 0. There is really no reason to not want this behavior, the overhead is minimal
41
+ # and the speed gains are large.
42
+ #
43
+ # It is important to note that this config value is global. That is to say all instances of the user agent filter
44
+ # share the same cache. The last declared cache size will 'win'. The reason for this is that there would be no benefit
45
+ # to having multiple caches for different instances at different points in the pipeline, that would just increase the
46
+ # number of cache misses and waste memory.
47
+ config :lru_cache_size, :validate => :number, :default => 1000
48
+
49
+ def register
50
+
51
+ LOOKUP_CACHE.max_size = @lru_cache_size
52
+
53
+ # make @target in the format [field name] if defined, i.e. surrounded by brakets
54
+ @normalized_target = (@target && @target !~ /^\[[^\[\]]+\]$/) ? "[#{@target}]" : ""
55
+
56
+ end
57
+
58
+ def filter(event)
59
+ useragent = event[@source]
60
+ useragent = useragent.first if useragent.is_a?(Array)
61
+
62
+ return if useragent.nil? || useragent.empty?
63
+
64
+ begin
65
+ ua_data = lookup_useragent(useragent)
66
+ rescue StandardError => e
67
+ @logger.error("Uknown error while parsing user agent data", :exception => e, :field => @source, :event => event)
68
+ return
69
+ end
70
+
71
+ return unless ua_data
72
+
73
+ event.remove(@source) if @target == @source
74
+ set_fields(event, ua_data)
75
+
76
+ filter_matched(event)
77
+ end
78
+
79
+ # should be private but need to stay public for specs
80
+ # TODO: (colin) the related specs should be refactored to not rely on private methods.
81
+ def lookup_useragent(useragent)
82
+ return unless useragent
83
+
84
+ cached = LOOKUP_CACHE[useragent]
85
+ return cached if cached
86
+
87
+ ua_data = parse_useragent(useragent)
88
+
89
+ LOOKUP_CACHE[useragent] = ua_data
90
+ ua_data
91
+ end
92
+
93
+ private
94
+
95
+ # Transform UserAgent object to plain object
96
+ def parse_useragent(useragent)
97
+ ua_raw_data = UserAgent.parseUserAgentString(useragent)
98
+ ua_data = Hash.new
99
+
100
+ os = ua_raw_data.getOperatingSystem()
101
+ browser = ua_raw_data.getBrowser()
102
+ version = ua_raw_data.getBrowserVersion()
103
+
104
+ if browser
105
+ ua_data["name"] = browser.getGroup().getName()
106
+ ua_data["fullname"] = browser.getName()
107
+ ua_data["vendor"] = browser.getManufacturer().getName()
108
+ ua_data["type"] = browser.getBrowserType().getName()
109
+ end
110
+
111
+ if version
112
+ ua_data["major"] = version.getMajorVersion()
113
+ ua_data["minor"] = version.getMinorVersion()
114
+ ua_data["version"] = version.getVersion()
115
+ end
116
+
117
+ if os
118
+ ua_data["os_fullname"] = os.getName()
119
+ ua_data["os_vendor"] = os.getManufacturer().getName()
120
+ ua_data["os"] = os.getGroup().getName()
121
+ ua_data["device"] = os.getDeviceType().getName()
122
+ end
123
+
124
+ ua_data
125
+ end
126
+
127
+ def set_fields(event, ua_data)
128
+ ua_data.each do |field, value|
129
+ event["#{@normalized_target}[#{@prefix}#{field}]"] = value.dup if value
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,31 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.name = 'logstash-filter-useragent2'
4
+ s.version = '3.0.0'
5
+ s.licenses = ['Apache-2.0']
6
+ s.summary = "Parse user agent strings into structured data based on BrowserScope data"
7
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
8
+ s.authors = ["Elastic"]
9
+ s.email = 'info@elastic.co'
10
+ s.homepage = "https://github.com/ebuildy/logstash-filter-useragent"
11
+ s.require_paths = ["lib"]
12
+ s.platform = 'java'
13
+
14
+ # Files
15
+ s.files = Dir['lib/**/*','spec/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
16
+
17
+ # Tests
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+
20
+ # Special flag to let us know this is actually a logstash plugin
21
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
22
+
23
+ # Gem dependencies
24
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 1.0"
25
+ s.add_runtime_dependency 'lru_redux', '~> 1.1', '>= 1.1.0'
26
+
27
+ s.add_development_dependency 'jar-dependencies'
28
+ s.add_development_dependency 'logstash-devutils'
29
+
30
+ s.requirements << "jar eu.bitwalker:UserAgentUtils, 1.19"
31
+ end
@@ -0,0 +1,129 @@
1
+ # encoding: utf-8
2
+
3
+ require "logstash/devutils/rspec/spec_helper"
4
+ require "logstash/filters/useragent"
5
+
6
+ describe LogStash::Filters::UserAgent do
7
+
8
+ describe "defaults" do
9
+ config <<-CONFIG
10
+ filter {
11
+ useragent {
12
+ source => "message"
13
+ target => "ua"
14
+ }
15
+ }
16
+ CONFIG
17
+
18
+ sample "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31" do
19
+ insist { subject }.include?("ua")
20
+ insist { subject["ua"]["name"] } == "Chrome"
21
+ insist { subject["ua"]["os"] } == "Linux"
22
+ insist { subject["ua"]["major"] } == "26"
23
+ insist { subject["ua"]["minor"] } == "0"
24
+ end
25
+ end
26
+
27
+ describe "Without target field" do
28
+ config <<-CONFIG
29
+ filter {
30
+ useragent {
31
+ source => "message"
32
+ }
33
+ }
34
+ CONFIG
35
+
36
+ sample "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31" do
37
+ insist { subject["name"] } == "Chrome"
38
+ insist { subject["os"] } == "Linux"
39
+ insist { subject["major"] } == "26"
40
+ insist { subject["minor"] } == "0"
41
+ end
42
+ end
43
+
44
+ describe "Without user agent" do
45
+ config <<-CONFIG
46
+ filter {
47
+ useragent {
48
+ source => "message"
49
+ target => "ua"
50
+ }
51
+ }
52
+ CONFIG
53
+
54
+ sample "foo" => "bar" do
55
+ reject { subject }.include?("ua")
56
+ end
57
+
58
+ sample "" do
59
+ reject { subject }.include?("ua")
60
+ end
61
+ end
62
+
63
+ describe "LRU object identity" do
64
+ let(:ua_string) { "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36" }
65
+ let(:uafilter) { LogStash::Filters::UserAgent.new("source" => "foo") }
66
+ let(:ua_data) { uafilter.lookup_useragent(ua_string) }
67
+
68
+ subject(:target) { LogStash::Event.new("foo" => ua_string) }
69
+
70
+ before do
71
+ uafilter.register
72
+
73
+ # Stub this out because this UA doesn't have this field
74
+ allow(ua_data.version).to receive(:patch_minor).and_return("foo")
75
+
76
+ # expect(event).receive(:lookup_useragent)
77
+ uafilter.filter(target)
78
+ end
79
+
80
+ {
81
+ "name" => lambda {|uad| uad.name},
82
+ "os" => lambda {|uad| uad.os.to_s},
83
+ "os_name" => lambda {|uad| uad.os.name},
84
+ "os_major" => lambda {|uad| uad.os.version.major},
85
+ "os_minor" => lambda {|uad| uad.os.version.minor},
86
+ "device" => lambda {|uad| uad.device.to_s},
87
+ "major" => lambda {|uad| uad.version.major},
88
+ "minor" => lambda {|uad| uad.version.minor},
89
+ "patch" => lambda {|uad| uad.version.patch},
90
+ "build" => lambda {|uad| uad.version.patch_minor}
91
+ }.each do |field, uad_getter|
92
+ context "for the #{field} field" do
93
+ let(:value) {uad_getter.call(ua_data)}
94
+ let(:target_field) { target[field]}
95
+
96
+ it "should not have a nil value" do
97
+ expect(target_field).to be_truthy
98
+ end
99
+
100
+ it "should have equivalent values" do
101
+ expect(target_field).to eql(value)
102
+ end
103
+
104
+ it "should dup/clone the field to prevent cache corruption" do
105
+ expect(target_field.object_id).not_to eql(value.object_id)
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ describe "Replace source with target" do
112
+ config <<-CONFIG
113
+ filter {
114
+ useragent {
115
+ source => "message"
116
+ target => "message"
117
+ }
118
+ }
119
+ CONFIG
120
+
121
+ sample "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31" do
122
+ insist { subject.to_hash }.include?("message")
123
+ insist { subject["message"]["name"] } == "Chrome"
124
+ insist { subject["message"]["os"] } == "Linux"
125
+ insist { subject["message"]["major"] } == "26"
126
+ insist { subject["message"]["minor"] } == "0"
127
+ end
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-useragent2
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0
5
+ platform: java
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lru_redux
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.1.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.1'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.1.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: jar-dependencies
49
+ requirement: !ruby/object:Gem::Requirement
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
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: logstash-devutils
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: This gem is a logstash plugin required to be installed on top of the
76
+ Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not
77
+ a stand-alone program
78
+ email: info@elastic.co
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - CHANGELOG.md
84
+ - CONTRIBUTORS
85
+ - Gemfile
86
+ - LICENSE
87
+ - NOTICE.TXT
88
+ - README.md
89
+ - lib/eu/bitwalker/UserAgentUtils/1.19/UserAgentUtils-1.19.jar
90
+ - lib/logstash-filter-useragent_jars.rb
91
+ - lib/logstash/filters/useragent.rb
92
+ - logstash-filter-useragent.gemspec
93
+ - spec/filters/useragent_spec.rb
94
+ homepage: https://github.com/ebuildy/logstash-filter-useragent
95
+ licenses:
96
+ - Apache-2.0
97
+ metadata:
98
+ logstash_plugin: 'true'
99
+ logstash_group: filter
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements:
115
+ - jar eu.bitwalker:UserAgentUtils, 1.19
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Parse user agent strings into structured data based on BrowserScope data
121
+ test_files:
122
+ - spec/filters/useragent_spec.rb