From 5a616649b549eafc082ad876ac086da8945217f2 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 14 Mar 2014 08:52:57 +0100 Subject: Allow passing an adapter to Gitlab::LDAP::Person --- lib/gitlab/ldap/person.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/ldap/person.rb b/lib/gitlab/ldap/person.rb index 5ee383dfa03..06b17c58f8c 100644 --- a/lib/gitlab/ldap/person.rb +++ b/lib/gitlab/ldap/person.rb @@ -1,12 +1,14 @@ module Gitlab module LDAP class Person - def self.find_by_uid(uid) - Gitlab::LDAP::Adapter.new.user(config.uid, uid) + def self.find_by_uid(uid, adapter=nil) + adapter ||= Gitlab::LDAP::Adapter.new + adapter.user(config.uid, uid) end - def self.find_by_dn(dn) - Gitlab::LDAP::Adapter.new.user('dn', dn) + def self.find_by_dn(dn, adapter=nil) + adapter ||= Gitlab::LDAP::Adapter.new + adapter.user('dn', dn) end def initialize(entry) -- cgit v1.2.1 From af53aa9072ae355b6de167b0d20f9b87195131ec Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 14 Mar 2014 08:53:46 +0100 Subject: Add Gitlab::LDAP::Adapter.open This new method is based on Net::LDAP.open, which reuses a single LDAP connection. --- lib/gitlab/ldap/adapter.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/gitlab/ldap/adapter.rb b/lib/gitlab/ldap/adapter.rb index a7b5bcb207c..983a2956a35 100644 --- a/lib/gitlab/ldap/adapter.rb +++ b/lib/gitlab/ldap/adapter.rb @@ -3,7 +3,17 @@ module Gitlab class Adapter attr_reader :ldap - def initialize + def self.open(&block) + Net::LDAP.open(adapter_options) do |ldap| + block.call(self.new(ldap)) + end + end + + def self.config + Gitlab.config.ldap + end + + def self.adapter_options encryption = config['method'].to_s == 'ssl' ? :simple_tls : nil options = { @@ -23,8 +33,12 @@ module Gitlab if config['password'] || config['bind_dn'] options.merge!(auth_options) end + options + end + - @ldap = Net::LDAP.new(options) + def initialize(ldap=nil) + @ldap = ldap || Net::LDAP.new(self.class.adapter_options) end def users(field, value) @@ -65,7 +79,7 @@ module Gitlab private def config - @config ||= Gitlab.config.ldap + @config ||= self.class.config end end end -- cgit v1.2.1 From 56df3dbff2e07f6c4402ff7410412238d643ce0f Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 14 Mar 2014 08:55:50 +0100 Subject: Add Gitlab::LDAP::Access.open This new method wraps Gitlab::LDAP::Adapter.open to enable connection reuse. --- lib/gitlab/ldap/access.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/ldap/access.rb b/lib/gitlab/ldap/access.rb index 2a636244473..8f492e5c012 100644 --- a/lib/gitlab/ldap/access.rb +++ b/lib/gitlab/ldap/access.rb @@ -1,8 +1,20 @@ module Gitlab module LDAP class Access + attr_reader :adapter + + def self.open(&block) + Gitlab::LDAP::Adapter.open do |adapter| + block.call(self.new(adapter)) + end + end + + def initialize(adapter=nil) + @adapter = adapter + end + def allowed?(user) - !!Gitlab::LDAP::Person.find_by_dn(user.extern_uid) + !!Gitlab::LDAP::Person.find_by_dn(user.extern_uid, adapter) rescue false end -- cgit v1.2.1 From 48e9054056db7f14bdef0d2d5c859f357110ed95 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 14 Mar 2014 08:57:39 +0100 Subject: Open/close LDAP in ApplicationController By opening the LDAP connection at the controller level we can reuse it for all LDAP queries during the request. --- CHANGELOG | 1 + app/controllers/application_controller.rb | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 201990669a2..5a7263a9358 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -17,6 +17,7 @@ v 6.7.0 - Add GFM autocompletion for MergeRequests (Robert Speicher) - Add webhook when a new tag is pushed (Jeroen van Baarsen) - Add button for toggling inline comments in diff view + - Reuse the GitLab LDAP connection within each request v 6.6.2 - Fix 500 error on branch/tag create or remove via UI diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9a0c9f60b05..5f8b2da06f8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -182,13 +182,15 @@ class ApplicationController < ActionController::Base def ldap_security_check if current_user && current_user.requires_ldap_check? - if gitlab_ldap_access.allowed?(current_user) - current_user.last_credential_check_at = Time.now - current_user.save - else - sign_out current_user - flash[:alert] = "Access denied for your LDAP account." - redirect_to new_user_session_path + gitlab_ldap_access do |access| + if access.allowed?(current_user) + current_user.last_credential_check_at = Time.now + current_user.save + else + sign_out current_user + flash[:alert] = "Access denied for your LDAP account." + redirect_to new_user_session_path + end end end end @@ -198,8 +200,8 @@ class ApplicationController < ActionController::Base @event_filter ||= EventFilter.new(filters) end - def gitlab_ldap_access - Gitlab::LDAP::Access.new + def gitlab_ldap_access(&block) + Gitlab::LDAP::Access.open { |access| block.call(access) } end # JSON for infinite scroll via Pager object -- cgit v1.2.1