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

Force user_type to UPN when username is a UPN #17690

Merged
merged 3 commits into from
Jul 17, 2018
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
22 changes: 16 additions & 6 deletions lib/miq_ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,24 @@ def normalize(dn)
dn.split(",").collect { |i| i.downcase.strip }.join(",")
end

def is_dn?(str)
def dn?(str)
!!(str =~ /^([a-z|0-9|A-Z]+ *=[^,]+[,| ]*)+$/)
end

def upn?(str)
!!(str =~ /^.+@.+$/)
end

def domain_username?(str)
!!(str =~ /^([a-zA-Z][a-zA-Z0-9.-]+)\\.+$/)
end

def fqusername(username)
return username if self.is_dn?(username) || self.domain_username?(username)
return username if dn?(username) || domain_username?(username)

user_type = @user_type.split("-").first
return username if user_type != "mail" && upn?(username)

user_prefix = @user_type.split("-").last
user_prefix = "cn" if user_prefix == "dn"
case user_type
Expand All @@ -286,13 +292,12 @@ def fqusername(username)
return username
when "upn", "userprincipalname"
return username if @user_suffix.blank?
return username if username =~ /^.+@.+$/ # already qualified with user@domain

return "#{username}@#{@user_suffix}"
when "mail"
username = "#{username}@#{@user_suffix}" unless @user_suffix.blank? || username =~ /^.+@.+$/
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is not correct. I'm fixing it now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fix has been completed.

username = "#{username}@#{@user_suffix}" unless @user_suffix.blank? || upn?(username)
dbuser = User.find_by_email(username.downcase)
dbuser = User.find_by_userid(username.downcase) unless dbuser
dbuser ||= User.find_by_userid(username.downcase)
return dbuser.userid if dbuser && dbuser.userid

return username
Expand All @@ -303,7 +308,12 @@ def fqusername(username)

def get_user_object(username, user_type = nil)
user_type ||= @user_type.split("-").first
user_type = "dn" if self.is_dn?(username)
if dn?(username)
user_type = "dn"
elsif upn?(username)
user_type = "upn"
end

begin
search_opts = {:base => @basedn, :scope => :sub, :attributes => ["*", @group_attribute]}

Expand Down
69 changes: 69 additions & 0 deletions spec/lib/miq_ldap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,74 @@

ldap.get_user_object("myuserid@mycompany.com", "upn")
end

it "searches for group membership when username is upn regardless of user_type" do
ldap = MiqLdap.new(:host => ["192.0.2.2"])
@opts[:attributes] = ["*", "memberof"]
expect(ldap).to receive(:search).with(@opts)

ldap.get_user_object("myuserid@mycompany.com", "bad_user_type")
end
end

context "#fqusername" do
before do
allow(TCPSocket).to receive(:new)
@opts = {:host => ["192.0.2.2"], :user_suffix => 'mycompany.com', :domain_prefix => 'my\domain'}
end

it "returns username when username is already a dn" do
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername("cn=myuser,ou=people,ou=prod,dc=example,dc=com")).to eq("cn=myuser,ou=people,ou=prod,dc=example,dc=com")
end

it "returns username when username is a dn with an @ in the dn" do
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername("cn=my@user,ou=people,ou=prod,dc=example,dc=com")).to eq("cn=my@user,ou=people,ou=prod,dc=example,dc=com")
end

it "returns a constructed dn when user type is a dn" do
@opts[:user_type] = 'dn'
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername("myuser")).to eq("cn=myuser,mycompany.com")
end

it "returns username when username is already a upn" do
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername("myuserid@mycompany.com")).to eq("myuserid@mycompany.com")
end

it "returns username when username is already a domain username" do
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername('my\domain\myuserid')).to eq('my\domain\myuserid')
end

it "returns username when username is already a upn even if user_type is samaccountname" do
@opts[:user_type] = 'samaccountname'
@opts[:user_suffix] = 'not_mycompany.com'
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername("myuserid@mycompany.com")).to eq("myuserid@mycompany.com")
end

it "returns upn when user_type is upn" do
@opts[:user_type] = 'userprincipalname'
@opts[:user_suffix] = 'mycompany.com'
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername("myuserid")).to eq("myuserid@mycompany.com")
end

it "returns samaccountname when user_type is samaccountname" do
@opts[:user_type] = 'samaccountname'
ldap = MiqLdap.new(@opts)
expect(ldap.fqusername('myuserid')).to eq('my\domain\myuserid')
end

it "searches for username when user_type is mail even when username is UPN" do
@opts[:user_type] = 'mail'
ldap = MiqLdap.new(@opts)
expect(User).to receive(:find_by_email)
expect(User).to receive(:find_by_userid)
expect(ldap.fqusername('myuserid@mycompany.com')).to eq('myuserid@mycompany.com')
end
end
end