设置gitlab LDAP身份验证没有特殊的gitlab用户

问题描述:

我想用我们公司的LDAP作为演示来设置Gitlab。但不幸的是,我必须在gitlab.yml中输入管理员密码才能使gitlab访问LDAP服务。问题实际上是管理,因为他们不想为Gitlab设置另一个帐户。有没有办法绕过这个,而不填写我自己的密码?有没有办法让Gitlab只用提供的用户证书建立LDAP连接?设置gitlab LDAP身份验证没有特殊的gitlab用户

任何想法以匿名身份登录?已经过帐here

我还没有尝试过,但是从目前为止我已经构建的对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

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

我已经修补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))'