将 Gitlab 与 OpenLDAP 集成以进行身份​​验证

本教程将带您了解如何将 Gitlab 与 OpenLDAP 集成以进行身份​​验证。 Gitlab 支持 LDAP 身份验证。

在我们之前的教程中,我们学习了如何在 Debian/Ubuntu 上安装 Gitlab;

在 Debian 11 上安装 Gitlab CE

在 Ubuntu 20.04 上安装带有 SSL/TLS 证书的 Gitlab

我们还有关于设置 OpenLDAP 的教程;

在 Debian 11 上安装和设置 OpenLDAP 服务器

在 Rocky Linux 8 上安装和设置 OpenLDAP

将 Gitlab 与 OpenLDAP 集成以进行身份​​验证

假设您已经设置并运行了 Gitlab 和 OpenLDAP 服务器,让我们继续看看如何将 Gitlab 与 OpenLDAP 集成以进行身份​​验证。

创建 Gitlab 用户 OpenLDAP 成员组

为了确保只有特定用户可以通过 OpenLDAP 登录 Gitlab 服务器,我们将在我们的 OpenLDAP 服务器上创建一个成员组。

如何创建 OpenLDAP 成员组已在以下指南中进行了广泛的描述;

如何创建 OpenLDAP 成员组

假设我们有以下用户,我们希望允许他们通过 OpenLDAP 登录 Gitlab 服务器;

ldapsearch -H ldapi:/// -Y EXTERNAL -LLL -b "dc=ldapmaster,dc=kifarunix-demo,dc=com" uid=* dn -Q
dn: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com  dn: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com

因此,使用您喜欢的任何名称创建一个组。 例如,让我们创建一个名为的组 gitlab.

在我们当前的 LDAP 服务器中,我们有一个组 OU, ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com

创建一个 GitLab 分组并添加用户 janedoe 和 johndoe,创建一个 LDIF 配置文件,如下所示;

cat > gitlab-group.ldif << EOL dn: cn=gitlab,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com objectClass: groupOfNames cn: gitlab member: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com member: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com EOL

根据您组织的 OpenLDAP 服务器设置更新上述内容。

接下来,在 OpenLDAP 服务器终端上运行以下命令以创建与该组成员一起的组。

ldapadd -Y EXTERNAL -H ldapi:/// -Q -f gitlab-group.ldif

命令运行后,您可以验证组是否可用和成员;

ldapsearch -H ldapi:/// -Y EXTERNAL -LLL -b "dc=ldapmaster,dc=kifarunix-demo,dc=com" cn=gitlab -Q
dn: cn=gitlab,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com objectClass: groupOfNames cn: gitlab member: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com member: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com

您可以按照上述帖子中的说明向群组添加更多成员。

将 Gitlab 与 OpenLDAP 集成以进行身份​​验证

设置 OpenLDAP 组并添加成员后,您现在可以配置 Gitlab 以使用 OpenLDAP 进行身份验证。

打开Gitlab配置文件进行编辑;

vim /etc/gitlab/gitlab.rb

向下滚动到 LDAP 设置配置部分;

默认情况下,LDAP 配置设置被注释掉;

### LDAP Settings ###! Docs: https://docs.gitlab.com/omnibus/settings/ldap.html ###! **Be careful not to break the indentation in the ldap_servers block. It is ###!   in yaml format and the spaces must be retained. Using tabs will not work.** # gitlab_rails['ldap_enabled'] = false # gitlab_rails['prevent_ldap_sign_in'] = false  ###! **remember to close this block with 'EOS' below** # gitlab_rails['ldap_servers'] = YAML.load <<-'EOS' #   main: # 'main' is the GitLab 'provider ID' of this LDAP server #     label: 'LDAP' #     host: '_your_ldap_server' #     port: 389 #     uid: 'sAMAccountName' #     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with' #     password: '_the_password_of_the_bind_user' #     encryption: 'plain' # "start_tls" or "simple_tls" or "plain" #     verify_certificates: true #     smartcard_auth: false #     active_directory: true #     allow_username_or_email_login: false #     lowercase_usernames: false #     block_auto_created_users: false #     base: '' #     user_filter: '' #     ## EE only #     group_base: '' #     admin_group: '' #     sync_ssh_keys: false # #   secondary: # 'secondary' is the GitLab 'provider ID' of second LDAP server #     label: 'LDAP' #     host: '_your_ldap_server' #     port: 389 #     uid: 'sAMAccountName' #     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with' #     password: '_the_password_of_the_bind_user' #     encryption: 'plain' # "start_tls" or "simple_tls" or "plain" #     verify_certificates: true #     smartcard_auth: false #     active_directory: true #     allow_username_or_email_login: false #     lowercase_usernames: false #     block_auto_created_users: false #     base: '' #     user_filter: '' #     ## EE only #     group_base: '' #     admin_group: '' #     sync_ssh_keys: false # EOS 

我们将更新此配置部分,使其看起来像;

### LDAP Settings  gitlab_rails['ldap_enabled'] = true gitlab_rails['prevent_ldap_sign_in'] = false  gitlab_rails['ldap_servers'] = { 'main' => {   'label' => 'LDAP',   'host' =>  'ldap.kifarunix-demo.com',   'port' => 389,   'uid' => 'uid',   'encryption' => 'start_tls',   'verify_certificates' => false,   'tls_options' => {     'ca_file' => '/etc/ssl/certs/ldapca.pem'    # 'ssl_version' => '',    # 'ciphers' => '',    # 'cert' => '',    # 'key' => ''   },   'bind_dn' => 'cn=readonly,ou=system,dc=ldapmaster,dc=kifarunix-demo,dc=com',   'password' => 'hacker',   'timeout' => 10,   'active_directory' => false,   'allow_username_or_email_login' => true,   'block_auto_created_users' => true,   'base' => 'dc=ldapmaster,dc=kifarunix-demo,dc=com',   'user_filter' => '(memberOf=cn=gitlab,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com)',   'attributes' => {     'username' => ['uid'],     'email' => ['Email'],     'name' => 'ui',     'first_name' => 'cn',     'last_name' => 'sn'   },   'lowercase_usernames' => false,    # EE Only   'group_base' => '',   'admin_group' => '',   'external_groups' => [],   'sync_ssh_keys' => false   } } 

务必更换:

  • host: LDAP 服务器的地址、IP 或可解析的主机名
  • bind_dn:要绑定的 LDAP 用户的 DN。
  • bind_dn password:绑定用户的密码。
  • base: 搜索用户的基地。
  • user_filter: 过滤以指定允许通过 OpenLDAP 登录 Gitlab 的用户。 在我们的例子中,它是我们上面创建的组。
  • 与您的 LDAP 设置相匹配的用户属性。

注意对于 SSL 配置,我们使用自签名 ssl 进行演示。 因此,我们将验证设置为 false。

完成对配置的更改后,重新配置 Gitlab 应用程序。

gitlab-ctl reconfigure

检查 bind_dnpassword 证书

接下来,运行以下命令以“测试 bind_dnpassword 凭据(如果已配置)并列出 LDAP 用户示例”;

gitlab-rake gitlab:ldap:check

样本输出;

Checking LDAP ...  LDAP: ... Server: ldapmain not verifying SSL hostname of LDAPS server 'ldap.kifarunix-demo.com:389' LDAP authentication... Success LDAP users with access to your GitLab server (only showing the first 100 results) 	DN: uid=johndoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com	 uid: johndoe 	DN: uid=janedoe,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com	 uid: janedoe  Checking LDAP ... Finished

似乎一切都很好,-:)。

使用 OpenLDAP 凭证登录 Gitlab

接下来,导航到 Gitlab Web 界面。 您会注意到在登录页面上,您有两个登录选项:LDAP 和标准。

使用 LDAP 用户登录。 如果用户帐户必须由 admin 在登录之前,您会看到这样的消息;

Your account is pending approval from your GitLab administrator and hence blocked. Please contact your GitLab administrator if you think this is an error.

使用标准帐户以默认 Gitlab 管理用户身份登录,并验证上面的 LDAP 用户帐户。

导航 管理 > 用户 > 待批准. 您应该会看到 LDAP 用户帐户。 单击用户旁边的齿轮按钮,然后单击 批准.

用户现在列在活动用户中。

现在让LDAP用户登录;

你去吧。

这标志着我们关于如何将 Gitlab 与 OpenLDAP 集成以进行身份​​验证的教程的结束。

阅读更多

Gitlab LDAP 文档页面

如何配置 DokuWiki OpenLDAP 身份验证