class LogStash::Inputs::LDAPSearch

Public Instance Methods

register() click to toggle source
# File lib/logstash/inputs/LDAPSearch.rb, line 22
def register
        require 'net/ldap'
end
run(queue) click to toggle source
# File lib/logstash/inputs/LDAPSearch.rb, line 27
def run(queue)
        begin
                if @usessl == true
                        conn = Net::LDAP.new :host => @host,
                                             :port => @port,
                                             :encryption => :simple_tls,
                                             :base => base,
                                             :auth => {
                                                :method => :simple,
                                                :username => @dn,
                                                :password => @password.value
                                             }
                else
                        conn = Net::LDAP.new :host => @host,
                                             :port => @port,
                                             :base => base,
                                             :auth => {
                                                :method => :simple,
                                                :username => @dn,
                                                :password => @password.value
                                             }
                end

                # Handling binding exception
                if ! conn.bind
                        puts "Connection failed - code:  #{conn.get_operation_result.code}: #{conn.get_operation_result.message}"
                end

                # Instantiating a LDAP filter
                search_filter = Net::LDAP::Filter.from_rfc2254(filter)

                # Lauching LDAP request
                conn.search( :filter => search_filter, :attributes => attrs ) { |entry|
                        event = LogStash::Event.new
                        decorate(event)
                        entry.attribute_names.each { |attr|
                                # Changing attribute variable type returned by attribute_name method from Symbol to String
                                attr = attr.to_s
                                # Suppressing default dn attribute if not wanted
                                next if (/^dn$/ =~ attr)
                                values = entry[attr]
                                # Formatting sAMAccountName to match classic case
                                attr = "sAMAccountName" if attr == "samaccountname"
                                values = values.map { |value|
                                        (/[^[:print:]]/ =~ value).nil? ? value : Base64.strict_encode64(value)
                                }
                                # Populating event
                                event.set(attr,values)
                        }
                        # Adding event and sending to logstash for processing
                        queue << event
                }
                #Managing LDAP exception
                rescue Net::LDAP::Error => le
                        puts "Got LDAP error: #{le}"
                        exit
        end
        # finished
end