2014-09-02 18:07:02 +05:30
|
|
|
module Gitlab
|
|
|
|
module LDAP
|
|
|
|
class Adapter
|
2015-04-26 12:48:37 +05:30
|
|
|
attr_reader :provider, :ldap
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def self.open(provider, &block)
|
|
|
|
Net::LDAP.open(config(provider).adapter_options) do |ldap|
|
|
|
|
block.call(self.new(provider, ldap))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def self.config(provider)
|
|
|
|
Gitlab::LDAP::Config.new(provider)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def initialize(provider, ldap=nil)
|
|
|
|
@provider = provider
|
|
|
|
@ldap = ldap || Net::LDAP.new(config.adapter_options)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def config
|
|
|
|
Gitlab::LDAP::Config.new(provider)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def users(field, value, limit = nil)
|
2014-09-02 18:07:02 +05:30
|
|
|
if field.to_sym == :dn
|
|
|
|
options = {
|
|
|
|
base: value,
|
|
|
|
scope: Net::LDAP::SearchScope_BaseObject
|
|
|
|
}
|
|
|
|
else
|
|
|
|
options = {
|
2015-04-26 12:48:37 +05:30
|
|
|
base: config.base,
|
2014-09-02 18:07:02 +05:30
|
|
|
filter: Net::LDAP::Filter.eq(field, value)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
if config.user_filter.present?
|
|
|
|
user_filter = Net::LDAP::Filter.construct(config.user_filter)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
options[:filter] = if options[:filter]
|
|
|
|
Net::LDAP::Filter.join(options[:filter], user_filter)
|
|
|
|
else
|
|
|
|
user_filter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
if limit.present?
|
|
|
|
options.merge!(size: limit)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
entries = ldap_search(options).select do |entry|
|
|
|
|
entry.respond_to? config.uid
|
|
|
|
end
|
|
|
|
|
|
|
|
entries.map do |entry|
|
2015-04-26 12:48:37 +05:30
|
|
|
Gitlab::LDAP::Person.new(entry, provider)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user(*args)
|
|
|
|
users(*args).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def dn_matches_filter?(dn, filter)
|
2015-04-26 12:48:37 +05:30
|
|
|
ldap_search(base: dn,
|
|
|
|
filter: filter,
|
|
|
|
scope: Net::LDAP::SearchScope_BaseObject,
|
|
|
|
attributes: %w{dn}).any?
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def ldap_search(*args)
|
2016-01-14 18:37:52 +05:30
|
|
|
# Net::LDAP's `time` argument doesn't work. Use Ruby `Timeout` instead.
|
|
|
|
Timeout.timeout(config.timeout) do
|
|
|
|
results = ldap.search(*args)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
if results.nil?
|
|
|
|
response = ldap.get_operation_result
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
unless response.code.zero?
|
|
|
|
Rails.logger.warn("LDAP search error: #{response.message}")
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
[]
|
|
|
|
else
|
|
|
|
results
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
rescue Timeout::Error
|
|
|
|
Rails.logger.warn("LDAP search timed out after #{config.timeout} seconds")
|
|
|
|
[]
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|