Commit 3802306f authored by Douwe Maan's avatar Douwe Maan

Support simple string LDAP attribute specifications, and search for name...

Support simple string LDAP attribute specifications, and search for name rather than username attributes

# Conflicts:
#	lib/gitlab/ldap/adapter.rb
#	spec/lib/gitlab/ldap/adapter_spec.rb
parent 739b3212
---
title: Fix signing in using LDAP when attribute mapping uses simple strings instead
of arrays
merge_request:
author:
type: fixed
......@@ -52,12 +52,6 @@ module EE
LDAP::Group.new(entry, self)
end
end
def user_attributes
attributes = super
attributes << config.sync_ssh_keys if config.sync_ssh_keys
attributes
end
end
end
end
......
......@@ -42,6 +42,13 @@ module EE
.reverse
.join('.')
end
def ldap_attributes(config)
super + [
'memberof', # Used in `memberof`
config.sync_ssh_keys # Used in `ssh_keys`
]
end
end
def ssh_keys
......
......@@ -79,7 +79,7 @@ module Gitlab
private
def user_options(field, value, limit)
options = { attributes: user_attributes }
options = { attributes: Gitlab::LDAP::Person.ldap_attributes(config).compact.uniq }
options[:size] = limit if limit
if field.to_sym == :dn
......@@ -105,10 +105,6 @@ module Gitlab
filter
end
end
def user_attributes
%W(#{config.uid} cn dn memberof) + config.attributes['username'] + config.attributes['email']
end
end
end
end
......@@ -3,7 +3,7 @@
module Gitlab
module LDAP
class Person
include ::EE::Gitlab::LDAP::Person
prepend ::EE::Gitlab::LDAP::Person
# Active Directory-specific LDAP filter that checks if bit 2 of the
# userAccountControl attribute is set.
......@@ -25,6 +25,15 @@ module Gitlab
adapter.dn_matches_filter?(dn, AD_USER_DISABLED)
end
def self.ldap_attributes(config)
[
'dn', # Used in `dn`
config.uid, # Used in `uid`
*config.attributes['name'], # Used in `name`
*config.attributes['email'] # Used in `email`
]
end
def initialize(entry, provider)
Rails.logger.debug { "Instantiating #{self.class.name} with LDIF:\n#{entry.to_ldif}" }
@entry = entry
......
......@@ -39,11 +39,4 @@ describe Gitlab::LDAP::Adapter do
expect(results.first.member_dns).to match_array(%w(john mary))
end
end
describe '#user_attributes' do
it 'appends EE-specific attributes' do
stub_ldap_config(uid: 'uid', sync_ssh_keys: 'sshPublicKey')
expect(adapter.user_attributes).to match_array(%w(uid dn cn email mail memberof sAMAccountName sshPublicKey uid userPrincipalName userid))
end
end
end
......@@ -9,6 +9,13 @@ describe Gitlab::LDAP::Person do
expect(described_class).to include(EE::Gitlab::LDAP::Person)
end
describe '.ldap_attributes' do
it 'appends EE-specific attributes' do
stub_ldap_config(sync_ssh_keys: 'sshPublicKey')
expect(described_class.ldap_attributes(ldap_adapter.config)).to include('sshPublicKey')
end
end
describe '.find_by_email' do
let(:adapter) { ldap_adapter }
......
......@@ -16,7 +16,7 @@ describe Gitlab::LDAP::Adapter do
expect(adapter).to receive(:ldap_search) do |arg|
expect(arg[:filter].to_s).to eq('(uid=johndoe)')
expect(arg[:base]).to eq('dc=example,dc=com')
expect(arg[:attributes]).to match(%w{uid cn dn memberof uid userid sAMAccountName mail email userPrincipalName})
expect(arg[:attributes]).to match(%w{dn uid cn mail email userPrincipalName memberof})
end.and_return({})
adapter.users('uid', 'johndoe')
......@@ -26,7 +26,7 @@ describe Gitlab::LDAP::Adapter do
expect(adapter).to receive(:ldap_search).with(
base: 'uid=johndoe,ou=users,dc=example,dc=com',
scope: Net::LDAP::SearchScope_BaseObject,
attributes: %w{uid cn dn memberof uid userid sAMAccountName mail email userPrincipalName},
attributes: %w{dn uid cn mail email userPrincipalName memberof},
filter: nil
).and_return({})
......@@ -63,7 +63,7 @@ describe Gitlab::LDAP::Adapter do
it 'uses the right uid attribute when non-default' do
stub_ldap_config(uid: 'sAMAccountName')
expect(adapter).to receive(:ldap_search).with(
hash_including(attributes: %w{sAMAccountName cn dn memberof uid userid sAMAccountName mail email userPrincipalName})
hash_including(attributes: %w{dn sAMAccountName cn mail email userPrincipalName memberof})
).and_return({})
adapter.users('sAMAccountName', 'johndoe')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment