Class: LogStash::Inputs::Ldap

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/ldap.rb

Instance Method Summary collapse

Instance Method Details

#connectObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/logstash/inputs/ldap.rb', line 95

def connect()
  ldap=nil
  begin
    tls_options = (not @cacert_path) ? {} : {
      :ca_file => @cacert_path
    }
    enc = ( not @ssl ) ? {} : {
        :method => :simple_tls,
        :tls_options => tls_options
    }
    jruby_timeout(@timeout, LdapConnectTimeout) do
      ldap=Net::LDAP.new(
        :host => @host, :base => @base, :port => @port, :encryption => enc,
        :auth => {
          :username => @bind_dn, :password => @bind_password, :method => :simple
        }
        #,:connect_timeout => @timeout
      )
    end
  rescue LdapConnectTimeout => ex
    @logger.error("Timeout connecting to LDAP")
    raise ex
  rescue Net::LDAP::Error => ex
    @logger.error("Failed to connect to LDAP", :error_message => ex.message)
    raise ex
  else
    return ldap
  end
end

#entry_to_event(entry) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/logstash/inputs/ldap.rb', line 84

def entry_to_event( entry )
  data = {}
  entry.attribute_names.each do |an|
    data[an.to_s] = entry[an]
  end
  #data["ldap_server"] = @host
  event = LogStash::Event.new( data )
  decorate( event )
  event
end

#format_ldap_time(time) ⇒ Object



29
30
31
# File 'lib/logstash/inputs/ldap.rb', line 29

def format_ldap_time(time)
  time.utc.strftime("%Y%m%d%H%M%SZ")
end

#jruby_timeout(sec, klass) ⇒ Object

alternate timeout for jruby gist.github.com/jorgenpt/1356797



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/logstash/inputs/ldap.rb', line 127

def jruby_timeout(sec,klass)
  return yield(sec) if sec == nil or sec.zero?
  thread = Thread.new { yield(sec) }

  if thread.join(sec).nil?
    java_thread = JRuby.reference(thread)
    thread.kill
    java_thread.native_thread.interrupt
    thread.join(0.15)
    raise klass, 'execution expired'
  else
    thread.value
  end
end

#registerObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/logstash/inputs/ldap.rb', line 33

def register
  require 'net-ldap'
  require 'uri'
  require "stud/interval"
  require "yaml"
  # timeout has problems in jruby 1.7.19, using jruby_timeout instead
  # require 'timeout'

  begin
    parsed_uri = URI.parse @ldap_uri
    raise ArgumentError.new("uri is not an ldap uri") unless
            [ URI::LDAP, URI::LDAPS ].index parsed_uri.class
    @host   = parsed_uri.host
    @port   = parsed_uri.port
    @ssl    = parsed_uri.scheme == "ldaps" ? true : false
    @base   = parsed_uri.dn
    @scope  = case parsed_uri.scope
      when "sub"
        Net::LDAP::SearchScope_WholeSubtree
      when "base"
        Net::LDAP::SearchScope_BaseObject
      when "single"
        Net::LDAP::SearchScope_SingleLevel
    end
  rescue ArgumentError, URI::InvalidURIError => e
    @logger.error("Not an LDAP/LDAPS uri", :error_message => e.message)
    raise e
  end

  begin
    @parsed_filter=Net::LDAP::Filter.from_rfc2254 @filter
  rescue ArgumentError, URI::InvalidURIError => e
    @logger.error("Invalid filter #{@filter}", :error_message => e.message)
    raise e
  end

  if @cacert_path and not File.readable? @cacert_path
    @logger.error "Unreadable #{@cacert_path}"
    raise IOError.new("Unreadable #{@cacert_path}")
  end

  # load timestamp_filter_last_value from file if exists
  if @timestamp_filter_on && File.exist?()
    @timestamp_filter_last_value = YAML.load( File.read(  ) )
    @logger.debug("read timestamp_filter_last_value: #{@timestamp_filter_last_value}")
  elsif @timestamp_filter_on
    @timestamp_filter_last_value = "00000101000000.000000Z"
    @logger.debug("set timestamp_filter_last_value as default: #{@timestamp_filter_last_value}")
  end
end

#run(queue) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/logstash/inputs/ldap.rb', line 188

def run(queue)
  @ldap=connect()
  loop do
    run_once(queue) do |entry|
      event = entry_to_event entry
      queue << event
    end
    # run only once if @interval not set
    break if (not @interval) or stop?
    Stud.stoppable_sleep(@interval) { stop? }
  end
end

#run_once(queue) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/logstash/inputs/ldap.rb', line 142

def run_once(queue)
  begin
    filter= if not @timestamp_filter_on
      @parsed_filter
    else
      @parsed_filter.&(
        Net::LDAP::Filter.ge( timestamp_filter_field, @timestamp_filter_last_value )
      )
    end
    @logger.debug("Using filter: #{filter.to_s}")

    timestamp_filter_last_value = @timestamp_filter_last_value

    # jruby_timeout overcoming old jruby problems
    jruby_timeout(@timeout, LdapConnectTimeout ) do
      ris = @ldap.search( :base => @base,
                          :filter => filter,
                          :return_result => false,
                          # maybe, someday 
                          #:time_limit => @timeout,
                          :scope => Net::LDAP::SearchScope_SingleLevel) do |entry|
        yield(entry)
        if @timestamp_filter_on
           = if @timestamp_filter_field_end
            entry[@timestamp_filter_field_end][0].to_s
          else
            entry[@timestamp_filter_field][0].to_s
          end

          timestamp_filter_last_value =  if
                 > timestamp_filter_last_value
        end
      end
    end
  rescue LdapConnectTimeout => e
    @logger.error("Timeout running query")
  rescue Net::Ldap::Error => e
    @logger.error("LDAP error running query: #{e.message}")
  rescue Exception => e
    @logger.error("Error running query [#{e.class}]: #{e}", :error_message => e.message)
    raise e
  else
    @timestamp_filter_last_value = timestamp_filter_last_value
    update_state_file if 
  end
end

#stopObject



201
202
203
204
205
206
207
# File 'lib/logstash/inputs/ldap.rb', line 201

def stop
  # nothing to do in this case so it is not necessary to define stop
  # examples of common "stop" tasks:
  #  * close sockets (unblocking blocking reads/accepts)
  #  * cleanup temporary files
  #  * terminate spawned threads
end

#update_state_fileObject

nothing to do in this case so it is not necessary to define stop examples of common “stop” tasks:

* close sockets (unblocking blocking reads/accepts)
* cleanup temporary files
* terminate spawned threads


208
209
210
211
212
213
# File 'lib/logstash/inputs/ldap.rb', line 208

def update_state_file
  if 
    @logger.debug("saving timestamp_filter_last_value: #{@timestamp_filter_last_value}")
    File.write(, YAML.dump(@timestamp_filter_last_value))
  end
end