class Soar::Registry::Directory::Provider::Ldap

Attributes

client[RW]

Public Class Methods

new(config: , base: , attributes: , index: , credentials: ) click to toggle source

@param [Hash{String => String, Number}] config @option config [String] host @option config [Number] port @param [String] base ldap tree base eg 'dc=hetzner,dc=co,dc=za' @param [Array<String>] attributes array of attributes to return for queries @param [Array<String>] index @param [Hash{String => String}] credentials @option credentials [String] username @option credentials [String] password

# File lib/soar/registry/directory/provider/ldap.rb, line 24
def initialize(config: , base: , attributes: , index: , credentials: )
  adapt_exceptions do
    @index = index
    @attributes = attributes
    @config = {
      host: config['host'],
      port: config['port'],
      base: base,
      auth: {
        method: :simple,
        username: credentials['username'], 
        password: credentials['password']
      },
      encryption: {
        method: :simple_tls
      }
    }
    @client = Net::LDAP.new(@config)
  end
end

Public Instance Methods

delete(dn) click to toggle source

@param [String] dn @return [Boolean]

# File lib/soar/registry/directory/provider/ldap.rb, line 131
def delete(dn)
  adapt_exceptions do
    @client.delete(dn: dn)
    return true
  end
end
fetch(primary_key) click to toggle source

@param [String] primary key of the identity ie. the first index @return [Hash{String => String, Number, Array}] single matching entry @raise [Soar::Registry::Directory::Error::NoEntriesFoundError] if primary key not found @raise [Soar::Registry::Directory::Error::MultipleEntriesFound] if multiple matches found

# File lib/soar/registry/directory/provider/ldap.rb, line 74
def fetch(primary_key)
  adapt_exceptions do
    result = @client.search({
      attributes: @attributes,
      filter: Net::LDAP::Filter.eq(@index[0], primary_key),
      return_result: true
    })
    raise Soar::Registry::Directory::Error::NoEntriesFoundError, "No entries found for #{@index[0]} = #{primary_key}" if result.empty?
    raise Soar::Registry::Directory::Error::MultipleEntriesFound if result.length > 1
    response = {}
    @attributes.each { |attribute| 
      response[attribute] = result[0][attribute.to_sym][0]
    }
    return response
  end
end
index() click to toggle source

@return [Array<String, Number>] a list of indexes

# File lib/soar/registry/directory/provider/ldap.rb, line 121
def index
  adapt_exceptions do
    @index
  end
end
put(entry) click to toggle source

@param [Hash{String => String, Number, Array}] entry @return [Boolean] @raise [Soar::Registry::Directory::Error::DuplicateEntryError]

# File lib/soar/registry/directory/provider/ldap.rb, line 50
def put(entry)
  adapt_exceptions do

    @index.each { |index| 
      if (entry['attributes'].keys.include?(index))
        result = search(index, entry['attributes'][index])
        raise Soar::Registry::Directory::Error::DuplicateEntryError if result.length > 0
      end
    }

    @client.add({
      dn: entry['dn'],
      attributes: entry['attributes'].map { |k, v| [k.to_sym, v] }.to_h
    })
    return true
  end
end

Private Instance Methods

adapt_exceptions() { || ... } click to toggle source
# File lib/soar/registry/directory/provider/ldap.rb, line 140
def adapt_exceptions
  begin
    yield
  rescue Net::LDAP::BindingInformationInvalidError, Net::LDAP::NoBindResultError, Net::LDAP::ConnectionRefusedError, Errno::ECONNREFUSED => e
    raise Soar::Registry::Directory::Error::NetworkingError, e.message
  end
end