logstash-input-ldap 0.3

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: 274ac5affd4347dbb2952533f6fba92d10487cd4
4
+ data.tar.gz: 968101426a058e95525ae2ece63fb7748e3d9e18
5
+ SHA512:
6
+ metadata.gz: 514371e36afaec7df5435ee2470d166f9906565a9ec75345d36d811bc9185bab766bfc49010464745140e9d6643fd7373e0f4bb897863ce71c15bcbd514210d8
7
+ data.tar.gz: 5fcea41d8a229f6e1f4c1c3b9132d8654f3e38b8e29ff6bcbd54fccd15e57aafaa42a406115b9355444140c6cbfd9810675fed1e3db1f53387085be18f859ba9
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
+ * dariko - git@dariozanzico.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-input-ldap
2
+ Example input plugin. This should help bootstrap your effort to write your own input 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,214 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "stud/interval"
5
+ require "socket" # for Socket.gethostname
6
+
7
+ class LdapConnectTimeout < Timeout::Error;end
8
+
9
+ class LogStash::Inputs::Ldap < LogStash::Inputs::Base
10
+ config_name "ldap"
11
+
12
+ default :codec, "plain"
13
+
14
+ config :bind_dn, :validate => :string, :required => true
15
+ config :bind_password, :validate => :string, :required => true
16
+ config :ldap_uri, :validate => :string, :required => true
17
+ config :cacert_path, :validate => :string, :required => false
18
+ config :filter, :validate => :string, :default => "(objectclass=*)"
19
+ config :interval, :validate => :number
20
+ config :timeout, :validate => :number, :default => 10
21
+ config :timestamp_filter_on, :validate => :boolean, :default => false
22
+ config :timestamp_filter_field, :validate => :string, :default => "reqstart"
23
+ config :timestamp_filter_field_end, :validate => :string, :default => nil
24
+ config :timestamp_filter_save_metadata, :validate => :boolean, :default => true
25
+ config :timestamp_filter_metadata_path, :validate => :string,
26
+ :default => "#{ENV['HOME']}/.logstash_ldap_last_run"
27
+
28
+ public
29
+ def format_ldap_time(time)
30
+ time.utc.strftime("%Y%m%d%H%M%SZ")
31
+ end
32
+
33
+ def register
34
+ require 'net-ldap'
35
+ require 'uri'
36
+ require "stud/interval"
37
+ require "yaml"
38
+ # timeout has problems in jruby 1.7.19, using jruby_timeout instead
39
+ # require 'timeout'
40
+
41
+ begin
42
+ parsed_uri = URI.parse @ldap_uri
43
+ raise ArgumentError.new("uri is not an ldap uri") unless
44
+ [ URI::LDAP, URI::LDAPS ].index parsed_uri.class
45
+ @host = parsed_uri.host
46
+ @port = parsed_uri.port
47
+ @ssl = parsed_uri.scheme == "ldaps" ? true : false
48
+ @base = parsed_uri.dn
49
+ @scope = case parsed_uri.scope
50
+ when "sub"
51
+ Net::LDAP::SearchScope_WholeSubtree
52
+ when "base"
53
+ Net::LDAP::SearchScope_BaseObject
54
+ when "single"
55
+ Net::LDAP::SearchScope_SingleLevel
56
+ end
57
+ rescue ArgumentError, URI::InvalidURIError => e
58
+ @logger.error("Not an LDAP/LDAPS uri", :error_message => e.message)
59
+ raise e
60
+ end
61
+
62
+ begin
63
+ @parsed_filter=Net::LDAP::Filter.from_rfc2254 @filter
64
+ rescue ArgumentError, URI::InvalidURIError => e
65
+ @logger.error("Invalid filter #{@filter}", :error_message => e.message)
66
+ raise e
67
+ end
68
+
69
+ if @cacert_path and not File.readable? @cacert_path
70
+ @logger.error "Unreadable #{@cacert_path}"
71
+ raise IOError.new("Unreadable #{@cacert_path}")
72
+ end
73
+
74
+ # load timestamp_filter_last_value from file if exists
75
+ if @timestamp_filter_on && File.exist?(@timestamp_filter_metadata_path)
76
+ @timestamp_filter_last_value = YAML.load( File.read( @timestamp_filter_metadata_path ) )
77
+ @logger.debug("read timestamp_filter_last_value: #{@timestamp_filter_last_value}")
78
+ elsif @timestamp_filter_on
79
+ @timestamp_filter_last_value = "00000101000000.000000Z"
80
+ @logger.debug("set timestamp_filter_last_value as default: #{@timestamp_filter_last_value}")
81
+ end
82
+ end
83
+
84
+ def entry_to_event( entry )
85
+ data = {}
86
+ entry.attribute_names.each do |an|
87
+ data[an.to_s] = entry[an]
88
+ end
89
+ #data["ldap_server"] = @host
90
+ event = LogStash::Event.new( data )
91
+ decorate( event )
92
+ event
93
+ end
94
+
95
+ def connect()
96
+ ldap=nil
97
+ begin
98
+ tls_options = (not @cacert_path) ? {} : {
99
+ :ca_file => @cacert_path
100
+ }
101
+ enc = ( not @ssl ) ? {} : {
102
+ :method => :simple_tls,
103
+ :tls_options => tls_options
104
+ }
105
+ jruby_timeout(@timeout, LdapConnectTimeout) do
106
+ ldap=Net::LDAP.new(
107
+ :host => @host, :base => @base, :port => @port, :encryption => enc,
108
+ :auth => {
109
+ :username => @bind_dn, :password => @bind_password, :method => :simple
110
+ }
111
+ #,:connect_timeout => @timeout
112
+ )
113
+ end
114
+ rescue LdapConnectTimeout => ex
115
+ @logger.error("Timeout connecting to LDAP")
116
+ raise ex
117
+ rescue Net::LDAP::Error => ex
118
+ @logger.error("Failed to connect to LDAP", :error_message => ex.message)
119
+ raise ex
120
+ else
121
+ return ldap
122
+ end
123
+ end
124
+
125
+ # alternate timeout for jruby
126
+ # https://gist.github.com/jorgenpt/1356797
127
+ def jruby_timeout(sec,klass)
128
+ return yield(sec) if sec == nil or sec.zero?
129
+ thread = Thread.new { yield(sec) }
130
+
131
+ if thread.join(sec).nil?
132
+ java_thread = JRuby.reference(thread)
133
+ thread.kill
134
+ java_thread.native_thread.interrupt
135
+ thread.join(0.15)
136
+ raise klass, 'execution expired'
137
+ else
138
+ thread.value
139
+ end
140
+ end
141
+
142
+ def run_once(queue)
143
+ begin
144
+ filter= if not @timestamp_filter_on
145
+ @parsed_filter
146
+ else
147
+ @parsed_filter.&(
148
+ Net::LDAP::Filter.ge( timestamp_filter_field, @timestamp_filter_last_value )
149
+ )
150
+ end
151
+ @logger.debug("Using filter: #{filter.to_s}")
152
+
153
+ timestamp_filter_last_value = @timestamp_filter_last_value
154
+
155
+ # jruby_timeout overcoming old jruby problems
156
+ jruby_timeout(@timeout, LdapConnectTimeout ) do
157
+ ris = @ldap.search( :base => @base,
158
+ :filter => filter,
159
+ :return_result => false,
160
+ # maybe, someday
161
+ #:time_limit => @timeout,
162
+ :scope => Net::LDAP::SearchScope_SingleLevel) do |entry|
163
+ yield(entry)
164
+ if @timestamp_filter_on
165
+ entry_timestamp = if @timestamp_filter_field_end
166
+ entry[@timestamp_filter_field_end][0].to_s
167
+ else
168
+ entry[@timestamp_filter_field][0].to_s
169
+ end
170
+
171
+ timestamp_filter_last_value = entry_timestamp if
172
+ entry_timestamp > timestamp_filter_last_value
173
+ end
174
+ end
175
+ end
176
+ rescue LdapConnectTimeout => e
177
+ @logger.error("Timeout running query")
178
+ rescue Net::Ldap::Error => e
179
+ @logger.error("LDAP error running query: #{e.message}")
180
+ rescue Exception => e
181
+ @logger.error("Error running query [#{e.class}]: #{e}", :error_message => e.message)
182
+ raise e
183
+ else
184
+ @timestamp_filter_last_value = timestamp_filter_last_value
185
+ update_state_file if @timestamp_filter_save_metadata
186
+ end
187
+ end
188
+ def run(queue)
189
+ @ldap=connect()
190
+ loop do
191
+ run_once(queue) do |entry|
192
+ event = entry_to_event entry
193
+ queue << event
194
+ end
195
+ # run only once if @interval not set
196
+ break if (not @interval) or stop?
197
+ Stud.stoppable_sleep(@interval) { stop? }
198
+ end
199
+ end
200
+
201
+ def stop
202
+ # nothing to do in this case so it is not necessary to define stop
203
+ # examples of common "stop" tasks:
204
+ # * close sockets (unblocking blocking reads/accepts)
205
+ # * cleanup temporary files
206
+ # * terminate spawned threads
207
+ end
208
+ def update_state_file
209
+ if @timestamp_filter_save_metadata
210
+ @logger.debug("saving timestamp_filter_last_value: #{@timestamp_filter_last_value}")
211
+ File.write(@timestamp_filter_metadata_path, YAML.dump(@timestamp_filter_last_value))
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-ldap'
3
+ s.version = '0.3'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This input reads data from a slapo-accesslog overlay via LDAP"
6
+ 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"
7
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
8
+ s.authors = ["Dario Zanzico"]
9
+ s.email = 'git@dariozanzico.com'
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "input" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_runtime_dependency 'logstash-codec-plain'
23
+ s.add_runtime_dependency 'stud', '>= 0.0.22'
24
+ s.add_development_dependency 'logstash-devutils', '>= 0.0.16'
25
+ s.add_runtime_dependency 'net-ldap', '>= 0.12.1'
26
+ s.add_runtime_dependency 'rufus-scheduler'
27
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/inputs/ldap"
4
+
5
+ #describe LogStash::Inputs::Ldap do
6
+
7
+ #it_behaves_like "an interruptible input plugin" do
8
+ #let(:config) { { "interval" => 100 } }
9
+ #end
10
+
11
+ #end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-ldap
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ platform: ruby
6
+ authors:
7
+ - Dario Zanzico
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-21 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-codec-plain
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.0.22
47
+ name: stud
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.22
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 0.0.16
61
+ name: logstash-devutils
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.16
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.12.1
75
+ name: net-ldap
76
+ prerelease: false
77
+ type: :runtime
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.12.1
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: rufus-scheduler
90
+ prerelease: false
91
+ type: :runtime
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ 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
98
+ email: git@dariozanzico.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - CHANGELOG.md
104
+ - CONTRIBUTORS
105
+ - DEVELOPER.md
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - lib/logstash/inputs/ldap.rb
110
+ - logstash-input-ldap.gemspec
111
+ - spec/inputs/ldap_spec.rb
112
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
113
+ licenses:
114
+ - Apache License (2.0)
115
+ metadata:
116
+ logstash_plugin: 'true'
117
+ logstash_group: input
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.8
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: This input reads data from a slapo-accesslog overlay via LDAP
138
+ test_files:
139
+ - spec/inputs/ldap_spec.rb