简述

Gitlab支持集成LDAP用户认证系统。兼容包括Microsoft Active Directory, Apple Open Directory, Open LDAP, 与389 Server等。 演示使用的版本是 Gitlab CE 11.8.2社区版,LDAP用户认证服务使用的是Microsoft Active Directory。 EE企业版支持更高级的功能,包括用户组同步,同时支持多LDAP服务器等, 有需要可以安装EE版。

测试LDAP查询参数与查询条件

在修改Gitlab配置之前先测试一下是否可以正常从域控上获取用户信息。我这里设置了一个过滤条件,只获取gituser组内的用户。

# ldapsearch -h mydc1.example.com -D [email protected] -w StrongPassword -b "OU=Company,DC=EXAMPLE,DC=com" "(memberOf=CN=gituser,OU=Groups,OU=UsersAndGroups,DC=EXAMPLE,DC=com)" sAMAccountName
# extended LDIF
#
# LDAPv3
# base <OU=Company,DC=EXAMPLE,DC=com> with scope subtree
# filter: (memberOf=CN=unixtek,OU=GroupsMigrated,OU=UsersAndGroups,DC=EXAMPLE,DC=com)
# requesting: sAMAccountName
#

# USR1, UserAccounts, OFFICE1, Company, EXAMPLE.com
dn: CN=USR1,OU=UserAccounts,OU=OFFICE1,OU=Company,DC=EXAMPLE,DC=com
sAMAccountName: USR1

# USR2, UserAccounts, OFFICE2, Company, EXAMPLE.com
dn: CN=USR2,OU=UserAccounts,OU=OFFICE2,OU=Company,DC=EXAMPLE,DC=com
sAMAccountName: USR2

# search result
search: 2
result: 0 Success

# numResponses: 3
# numEntries: 2

创建配置文件

修改Gitlab的YAML格式配置模板 /etc/gitlab/gitlab.rb ,在其中添加以下内容。

gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main: # 'main' is the GitLab 'provider ID' of this LDAP server
  label: 'AD 认证'
  host: 'mydc1.example.com'
  port: 389
  uid: 'sAMAccountName'
  encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
  bind_dn: 'CN=binduser,OU=ServiceAccounts,OU=Company,DC=EXAMPLE,DC=com'
  password: 'StrongPassword'
  verify_certificates: true
  active_directory: true
  allow_username_or_email_login: true
  block_auto_created_users: false
  base: 'OU=Company,DC=EXAMPLE,DC=com'
  user_filter: 'memberOf=CN=gituser,OU=Groups,OU=UsersAndGroups,DC=EXAMPLE,DC=com'
EOS 

参数详解:

label: AD认证窗口上显示的标题,仅起提示作用,请根据需要填写,配置成功后会出现在登录页面。
host: 用于AD认证的域控服务器。
bind_dn: 与AD交互所使用的的用户账号,由于域控禁用了anonymousbind,所以需要建立一个用户来进行交互。
encryption: 密码加密方式,我这里使用的明文。
user_filter: 用户登录权限控制,格式为LDAP查询条件,我这里是按用户组进行控制。

重新配置Gitlab

# gitlab-ctl reconfigure
...
此处省略日志输出一万行
...
Running handlers:
Running handlers complete
Chef Client finished, 8/635 resources updated in 47 seconds
gitlab Reconfigured!

测试获取AD域用户列表

# gitlab-rake gitlab:ldap:check
Checking LDAP ...

LDAP: ... Server: ldapmain
LDAP authentication... Success
LDAP users with access to your GitLab server (only showing the first 100 results)
        DN: cn=usr1,ou=useraccounts,ou=office1,ou=company,dc=example,dc=com        sAMAccountName: USR1
        DN: cn=usr2,ou=useraccounts,ou=office2,ou=company,dc=example,dc=com        sAMAccountName: USR2
        
Checking LDAP ... Finished

观察输出是否包含域用户账号。

重新启动Gitlab

# gitlab-ctl restart
ok: run: alertmanager: (pid 28490) 1s
ok: run: gitaly: (pid 28502) 0s
ok: run: gitlab-monitor: (pid 28522) 1s
ok: run: gitlab-workhorse: (pid 28527) 0s
ok: run: logrotate: (pid 28538) 1s
ok: run: nginx: (pid 28548) 0s
ok: run: node-exporter: (pid 28556) 0s
ok: run: postgres-exporter: (pid 28560) 1s
ok: run: postgresql: (pid 28569) 0s
ok: run: prometheus: (pid 28577) 1s
ok: run: redis: (pid 28663) 0s
ok: run: redis-exporter: (pid 28689) 1s
ok: run: sidekiq: (pid 28703) 0s
ok: run: unicorn: (pid 28759) 0s

登录Gitlab查看效果

Gitlab 集成使用 AD 认证

参考

https://docs.gitlab.com/ee/administration/auth/how_to_configure_ldap_gitlab_ce/

相关文章: