Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure user name is set even when common LDAP attributes are missing. #14142

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/models/authenticator/httpd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ def update_user_attributes(user, _username, identity)
user_attrs, _membership_list = identity

user.userid = user_attrs[:username]
user.name = user_attrs[:fullname]
user.first_name = user_attrs[:firstname]
user.last_name = user_attrs[:lastname]
user.email = user_attrs[:email] unless user_attrs[:email].blank?
user.name = user_attrs[:fullname]
user.name = "#{user.first_name} #{user.last_name}" if user.name.blank?
user.name = user.userid if user.name.blank?
end

private
Expand Down
4 changes: 3 additions & 1 deletion app/models/authenticator/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ def groups_for(obj)

def update_user_attributes(user, _username, lobj)
user.userid = ldap.normalize(ldap.get_attr(lobj, :userprincipalname) || ldap.get_attr(lobj, :dn))
user.name = ldap.get_attr(lobj, :displayname)
user.first_name = ldap.get_attr(lobj, :givenname)
user.last_name = ldap.get_attr(lobj, :sn)
email = ldap.get_attr(lobj, :mail)
user.email = email unless email.blank?
user.name = ldap.get_attr(lobj, :displayname)
user.name = "#{user.first_name} #{user.last_name}" if user.name.blank?
user.name = user.userid if user.name.blank?
end

REQUIRED_LDAP_USER_PROXY_KEYS = [:basedn, :bind_dn, :bind_pwd, :ldaphost, :ldapport, :mode]
Expand Down
28 changes: 28 additions & 0 deletions spec/models/authenticator/httpd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,34 @@ def authenticate
expect(MiqTask.status_error?(task.status)).to be_truthy
end
end

context "when fullname is blank" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how test tests turned out joe

let(:username) { 'betty' }
let(:headers) do
super().merge('X-Remote-User-FullName' => '',
'X-Remote-User-FirstName' => 'Betty',
'X-Remote-User-LastName' => 'Boop',
'X-Remote-User-Email' => '[email protected]')
end

it "creates a new User with name set to FirstName + LastName" do
expect(-> { authenticate }).to change { User.where(:name => 'Betty Boop').count }.from(0).to(1)
end
end

context "when fullname, firstname and lastname are blank" do
let(:username) { 'sam' }
let(:headers) do
super().merge('X-Remote-User-FullName' => '',
'X-Remote-User-FirstName' => '',
'X-Remote-User-LastName' => '',
'X-Remote-User-Email' => '[email protected]')
end

it "creates a new User with name set to the userid" do
expect(-> { authenticate }).to change { User.where(:name => 'sam').count }.from(0).to(1)
end
end
end

describe ".user_attrs_from_external_directory" do
Expand Down
40 changes: 40 additions & 0 deletions spec/models/authenticator/ldap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def normalize(dn)
'rootdn' => {:password => 'verysecret'},
'alice' => alice_data,
'bob' => bob_data,
'betty' => betty_data,
'sam' => sam_data,
}
end
let(:alice_data) do
Expand All @@ -86,6 +88,28 @@ def normalize(dn)
:groups => %w(wibble bubble),
}
end
let(:betty_data) do
{
:userprincipalname => 'betty',
:password => 'secret',
:displayname => nil,
:givenname => 'Betty',
:sn => 'Builderson',
:mail => '[email protected]',
:groups => %w(wibble bubble),
}
end
let(:sam_data) do
{
:userprincipalname => 'sam',
:password => 'secret',
:displayname => nil,
:givenname => nil,
:sn => nil,
:mail => '[email protected]',
:groups => %w(wibble bubble),
}
end

before(:each) do
allow(MiqLdap).to receive(:new) { FakeLdap.new(user_data) }
Expand Down Expand Up @@ -462,6 +486,22 @@ def authenticate
expect(MiqTask.status_error?(task.status)).to be_truthy
end
end

context "when display name is blank" do
let(:username) { 'betty' }

it "creates a new User with name set to givenname + sn" do
expect(-> { authenticate }).to change { User.where(:name => 'Betty Builderson').count }.from(0).to(1)
end
end

context "when display name, givenname and sn are blank" do
let(:username) { 'sam' }

it "creates a new User with name set to the userid" do
expect(-> { authenticate }).to change { User.where(:name => 'sam').count }.from(0).to(1)
end
end
end
end
end
Expand Down