Skip to content

Commit

Permalink
Add ability to use bind_as with a service account
Browse files Browse the repository at this point in the history
  • Loading branch information
yachub committed Aug 21, 2023
1 parent 7813470 commit d138634
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 66 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ GEM
rspec (~> 3)

PLATFORMS
arm64-darwin-22
universal-java-11
x86_64-linux

Expand Down
12 changes: 12 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ This can be a string providing a single DN. For multiple DNs please specify the
The LDAP object-type used to designate a user object.
(optional)

### LDAP\_SERVICE_ACCOUNT\_HASH

A hash containing the following parameters for a service account to perform the
initial bind. After the initial bind, then a search query is performed using the
'base' and 'user_object', then re-binds as the returned user.

- :user_dn: The full distinguished name (DN) of the service account used to bind.

- :password: The password for the service account used to bind.

(optional)

### SITE\_NAME

The name of your deployment.
Expand Down
28 changes: 23 additions & 5 deletions lib/vmpooler/api/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def authorized?
end
end

def authenticate_ldap(port, host, encryption_hash, user_object, base, username_str, password_str)
def authenticate_ldap(port, host, encryption_hash, user_object, base, username_str, password_str, service_account_hash = nil)
tracer.in_span(
"Vmpooler::API::Helpers.#{__method__}",
attributes: {
Expand All @@ -79,19 +79,35 @@ def authenticate_ldap(port, host, encryption_hash, user_object, base, username_s
},
kind: :client
) do
if service_account_hash
username = service_account_hash[:user_dn]
password = service_account_hash[:password]
else
username = "#{user_object}=#{username_str},#{base}"
password = password_str
end

ldap = Net::LDAP.new(
:host => host,
:port => port,
:encryption => encryption_hash,
:base => base,
:auth => {
:method => :simple,
:username => "#{user_object}=#{username_str},#{base}",
:password => password_str
:username => username,
:password => password
}
)

return true if ldap.bind
if service_account_hash
return true if ldap.bind_as(
:base => base,
:filter => "(#{user_object}=#{username_str})",
:password => password_str
)
else
return true if ldap.bind
end

return false
end
Expand All @@ -116,6 +132,7 @@ def authenticate(auth, username_str, password_str)
:method => :start_tls,
:tls_options => { :ssl_version => 'TLSv1' }
}
service_account_hash = auth[:ldap]['service_account_hash']

unless ldap_base.is_a? Array
ldap_base = ldap_base.split
Expand All @@ -134,7 +151,8 @@ def authenticate(auth, username_str, password_str)
search_user_obj,
search_base,
username_str,
password_str
password_str,
service_account_hash
)
return true if result
end
Expand Down
Loading

0 comments on commit d138634

Please sign in to comment.