2013-03-10 335 views
8

我想用我們公司的LDAP作爲演示來設置Gitlab。但不幸的是,我必須在gitlab.yml中輸入管理員密碼才能使gitlab訪問LDAP服務。問題實際上是管理,因爲他們不想爲Gitlab設置另一個帳戶。有沒有辦法繞過這個,而不填寫我自己的密碼?有沒有辦法讓Gitlab只用提供的用戶證書建立LDAP連接?設置gitlab LDAP身份驗證沒有特殊的gitlab用戶

任何想法以匿名身份登錄?已經過帳here

回答

9

我還沒有嘗試過,但是從目前爲止我已經構建的對LDAP的驗證以及配置文件中的信息來看,只有當您的LDAP不支持匿名綁定時,才需要此用戶帳戶並搜索。

所以我會留下兩個條目bind_dnpassword註釋掉,試試它是否有效。

UPDATE

我實現了LDAP-Autehntication在Gitlab,它是相當容易的。

gitlab.yml-文件中有一個名爲ldap的部分。

您必須提供信息才能連接到您的LDAP。似乎所有的領域都必須得到,似乎沒有後備預設!如果要使用匿名綁定檢索用戶,則DN爲bind_dnpassword提供一個空字符串。評論他們似乎不工作!至少我得到了501錯誤信息。

更多信息,https://github.com/patthoyts/gitlabhq/wiki/Setting-up-ldap-auth被發現和(更陳舊,但仍然有用)https://github.com/intridea/omniauth-ldap

+0

這在我的公司不起作用,因爲既沒有爲匿名綁定設置權限,也沒有爲搜索設置權限。玩這個參數只會導致錯誤信息,因爲gitlab無法綁定,因此無法驗證提供的用戶的存在(據我所知gitlab試圖在此程序步驟獲得完整的bind_dn)。 – Bubu 2013-03-14 07:03:05

+0

然後,您將需要一個有權綁定到LDAP的用戶來搜索嘗試登錄的用戶的綁定-DN。當匿名搜索未啓用時,沒有辦法解決。抱歉。 – heiglandreas 2013-03-14 15:55:24

+0

但是爲什麼我要搜索綁定DN,如果我已經知道它?例如。用戶[email protected]想要登錄;我知道,Bind-DN類似於「uid = [email protected],ou = people,dc = example,dc = com」 - Gitlab可以在缺省情況下利用此選項,從而啓用LDAP登錄,而無需初始綁定。我看不到,爲什麼這不是默認的LDAP身份驗證策略。 – Bubu 2013-03-14 20:35:27

1

GitLab使用omniauth管理多個登錄源(包括LDAP)。

因此,如果您可以以某種方式擴展omniauth以便以不同方式管理LDAP連接,則可以從其他源獲取密碼。
這將允許您避免保留在ldap section of the gitlab.yml config file的密碼。

+0

這是我的第一次想法,但從哪裏去?我沒有真正進入ruby的東西。我發現[this](https://gist.github.com/mgrobelin/3953472) - 因爲它似乎不使用初始綁定,它看起來對我非常有前途。你有其他提示,在哪裏挖? – Bubu 2013-03-14 07:19:22

+0

@Bubu現在沒有其他提示。你發現的東西似乎是擴展omniauth的一個很好的例子,這正是我的建議。 – VonC 2013-03-14 07:25:06

+0

你是否曾經得到這個工作?我正在嘗試做同樣的事情。 – orodbhen 2015-01-15 17:35:37

3

我已經修補gitlab這樣的工作方式和http://foivos.zakkak.net/tutorials/gitlab_ldap_auth_without_querying_account.html記錄的過程

我無恥地複製在這裏說明自我-completeness。

說明:本教程最後使用從源代碼安裝的gitlab 8.2進行了測試。

本教程旨在描述如何修改Gitlab安裝到 使用用戶憑證對LDAP服務器進行身份驗證。通過 默認Gitlab依靠匿名綁定或特殊查詢用戶 詢問用戶是否存在LDAP服務器 之前,用她自己的憑證驗證了她。但出於安全原因,許多管理員禁用匿名綁定並禁止創建特殊的查詢 LDAP用戶。

在本教程中,我們假設我們在 gitlab.example.com有gitlab設置和LDAP服務器上運行ldap.example.com和 用戶有以下形式的DN: CN=username,OU=Users,OU=division,OU=department,DC=example,DC=com

修補

要在我們需要部分修改其關於LDAP 認證機制,這種情況下Gitlab工作。

首先,我們用this派生替換omniauth-ldap模塊。爲了 爲此,我們使用下面的補丁來gitlab/Gemfile

diff --git a/Gemfile b/Gemfile 
index 1171eeb..f25bc60 100644 
--- a/Gemfile 
+++ b/Gemfile 
@@ -44,4 +44,5 @@ gem 'gitlab-grack', '~> 2.0.2', require: 'grack' 
# LDAP Auth 
# GitLab fork with several improvements to original library. For full list of changes 
# see https://github.com/intridea/omniauth-ldap/compare/master...gitlabhq:master 
-gem 'gitlab_omniauth-ldap', '1.2.1', require: "omniauth-ldap" 
+#gem 'gitlab_omniauth-ldap', '1.2.1', require: "omniauth-ldap" 
+gem 'gitlab_omniauth-ldap', :git => 'https://github.com/zakkak/omniauth-ldap.git', require: 'net-ldap', require: "omniauth-ldap" 

現在,我們需要執行以下操作:

  1. sudo -u git -H bundle install --without development test mysql --path vendor/bundle --no-deployment
  2. sudo -u git -H bundle install --deployment --without development test mysql aws

這些命令將取 gitlab/vendor/bundle/ruby/2.x.x/bundler/gems中的修改的omniauth-ldap模塊。現在該模塊被抓取到了 ,我們需要修改它以使用我們的LDAP服務器所期望的DN。我們 通過修補lib/omniauth/strategies/ldap.rbgitlab/vendor/bundle/ruby/2.x.x/bundler/gems/omniauth-ldap與實現這一目標:

diff --git a/lib/omniauth/strategies/ldap.rb b/lib/omniauth/strategies/ldap.rb 
index 9ea62b4..da5e648 100644 
--- a/lib/omniauth/strategies/ldap.rb 
+++ b/lib/omniauth/strategies/ldap.rb 
@@ -39,7 +39,7 @@ module OmniAuth 
     return fail!(:missing_credentials) if missing_credentials? 

     # The HACK! FIXME: do it in a more generic/configurable way 
-  @options[:bind_dn] = "CN=#{request['username']},OU=Test,DC=my,DC=example,DC=com" 
+  @options[:bind_dn] = "CN=#{request['username']},OU=Users,OU=division,OU=department,DC=example,DC=com" 
     @options[:password] = request['password'] 
     @adaptor = OmniAuth::LDAP::Adaptor.new @options 

有了這個模塊,gitlab使用用戶的憑據綁定到LDAP 服務器和查詢它,以及,對自己進行身份驗證的用戶。

然而,這隻會在用戶不使用ssh-key到 與Gitlab進行身份驗證時才起作用。當通過ssh密鑰進行身份驗證時,默認 默認Gitlab查詢LDAP服務器以確定 對應用戶是否(仍然)是有效用戶。此時,我們 不能使用用戶憑證來查詢LDAP服務器,因爲用戶 沒有提供給我們。因此,我們禁用了這種機制, 實質上允許用戶使用已註冊的ssh密鑰但已從 LDAP服務器中刪除,仍然使用我們的Gitlab安裝程序。爲了防止 這類用戶仍然可以使用您的Gitlab設置,您必須手動刪除其設置中的任何帳戶的ssh密鑰 。

要禁用此機制,我們修補gitlab/lib/gitlab/ldap/access.rb 有:

diff --git a/lib/gitlab/ldap/access.rb b/lib/gitlab/ldap/access.rb 
index 16ff03c..9ebaeb6 100644 
--- a/lib/gitlab/ldap/access.rb 
+++ b/lib/gitlab/ldap/access.rb 
@@ -14,15 +14,16 @@ module Gitlab 
     end 

     def self.allowed?(user) 
-  self.open(user) do |access| 
-   if access.allowed? 
-   user.last_credential_check_at = Time.now 
-   user.save 
-   true 
-   else 
-   false 
-   end 
-  end 
+  true 
+  # self.open(user) do |access| 
+  # if access.allowed? 
+  #  user.last_credential_check_at = Time.now 
+  #  user.save 
+  #  true 
+  # else 
+  #  false 
+  # end 
+  # end 
     end 

     def initialize(user, adapter=nil) 
@@ -32,20 +33,21 @@ module Gitlab 
     end 

def allowed? 
-  if Gitlab::LDAP::Person.find_by_dn(user.ldap_identity.extern_uid, adapter) 
-   return true unless ldap_config.active_directory 
+  true 
+  # if Gitlab::LDAP::Person.find_by_dn(user.ldap_identity.extern_uid, adapter) 
+  # return true unless ldap_config.active_directory 

-   # Block user in GitLab if he/she was blocked in AD 
-   if Gitlab::LDAP::Person.disabled_via_active_directory?(user.ldap_identity.extern_uid, adapter) 
-   user.block unless user.blocked? 
-   false 
-   else 
-   user.activate if user.blocked? && !ldap_config.block_auto_created_users 
-   true 
-   end 
-  else 
-   false 
-  end 
+  # # Block user in GitLab if he/she was blocked in AD 
+  # if Gitlab::LDAP::Person.disabled_via_active_directory?(user.ldap_identity.extern_uid, adapter) 
+  #  user.block unless user.blocked? 
+  #  false 
+  # else 
+  #  user.activate if user.blocked? && !ldap_config.block_auto_created_users 
+  #  true 
+  # end 
+  # else 
+  # false 
+  # end 
rescue 
false 
end 

配置

gitlab.yml使用類似以下內容(修改您的需要):

# 
# 2. Auth settings 
# ========================== 

## LDAP settings 
# You can inspect a sample of the LDAP users with login access by running: 
# bundle exec rake gitlab:ldap:check RAILS_ENV=production 
ldap: 
    enabled: true 
    servers: 
    ########################################################################## 
    # 
    # Since GitLab 7.4, LDAP servers get ID's (below the ID is 'main'). GitLab 
    # Enterprise Edition now supports connecting to multiple LDAP servers. 
    # 
    # If you are updating from the old (pre-7.4) syntax, you MUST give your 
    # old server the ID 'main'. 
    # 
    ########################################################################## 
    main: # 'main' is the GitLab 'provider ID' of this LDAP server 
     ## label 
     # 
     # A human-friendly name for your LDAP server. It is OK to change the label later, 
     # for instance if you find out it is too large to fit on the web page. 
     # 
     # Example: 'Paris' or 'Acme, Ltd.' 
     label: 'LDAP_EXAMPLE_COM' 

     host: ldap.example.com 
     port: 636 
     uid: 'sAMAccountName' 
     method: 'ssl' # "tls" or "ssl" or "plain" 
     bind_dn: '' 
     password: '' 

     # This setting specifies if LDAP server is Active Directory LDAP server. 
     # For non AD servers it skips the AD specific queries. 
     # If your LDAP server is not AD, set this to false. 
     active_directory: true 

     # If allow_username_or_email_login is enabled, GitLab will ignore everything 
     # after the first '@' in the LDAP username submitted by the user on login. 
     # 
     # Example: 
     # - the user enters '[email protected]' and '[email protected]' as LDAP credentials; 
     # - GitLab queries the LDAP server with 'jane.doe' and '[email protected]'. 
     # 
     # If you are using "uid: 'userPrincipalName'" on ActiveDirectory you need to 
     # disable this setting, because the userPrincipalName contains an '@'. 
     allow_username_or_email_login: false 

     # To maintain tight control over the number of active users on your GitLab installation, 
     # enable this setting to keep new users blocked until they have been cleared by the admin 
     # (default: false). 
     block_auto_created_users: false 

     # Base where we can search for users 
     # 
     # Ex. ou=People,dc=gitlab,dc=example 
     # 
     base: 'OU=Users,OU=division,OU=department,DC=example,DC=com' 

     # Filter LDAP users 
     # 
     # Format: RFC 4515 http://tools.ietf.org/search/rfc4515 
     # Ex. (employeeType=developer) 
     # 
     # Note: GitLab does not support omniauth-ldap's custom filter syntax. 
     # 
     user_filter: '(&(objectclass=user)(objectclass=person))'