背景:

紧跟 gitlab 15.8 on rocky 8,准备将gitlab与ldap打通,后续jenkins也是。方便用户的统一管理,现在的用户管理都是单独的,用户的离职和管理很是麻烦,正好借这次条例流程尝试全部打通,统一管理一下!关于ldap的搭建可以参考: Kuberneters 搭建openLDAP

gitlab ldap集成

gitlab开启并配置ldap

编辑gitlab.rb配置文件

打开编辑gitlab.rb配置文件:

vi /etc/gitlab/gitlab.rb 
  • 1.

gitlab与ldap集成_git
按住ctrl+v进入VISUAL BLOCK模式 选中要注释的行的# ,按d键删除注释符(secondaryx相关的其实不需要去掉…具体参照下面代码块配置!)
gitlab与ldap集成_mail_02
**gitlab_rails[‘ldap_enabled’] = true **开启ldap

 gitlab_rails['ldap_enabled'] = true
# gitlab_rails['prevent_ldap_sign_in'] = false
  • 1.
  • 2.

gitlab_rails[‘ldap_servers’] = YAML.load <<-‘EOS’
**EOS **为ldap相关配置,请参照个人ldap服务器进行相关配置。具体参数可以参照官方文档 https://docs.gitlab.com/ee/administration/auth/ldap/

gitlab与ldap集成_git_03
注:图只截取了一部分,详细的去看文档!



###! **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'  ###可以自定义gitlab登陆况显示的名词
     host: '10.0.4.25' ###ldap服务器地址
     port: xxxx  ###ldap端口(我是k8s中搭建的故不是默认的389)
     uid: 'cn'  ###ldap登陆的用户名
     bind_dn: 'cn=admin,dc=xxxx,dc=com'  #绑定的用户的完整 DN
     password: 'xxxxxxxx'                ##绑定用户的密码
     encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
     verify_certificates: false
     smartcard_auth: false
     active_directory: flase
     allow_username_or_email_login: false  ###邮箱用户是否可以登陆
     lowercase_usernames: false
     block_auto_created_users: false   ####不允许用户注册
     base: 'ou=devops,dc=xxxx,dc=com'    ###用户的搜索域
     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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

gitlab与ldap集成_gitlab_04
特别强调EOF要去掉注释
gitlab与ldap集成_gitlab_05

加载配置文件

重置加载配置:

sudo gitlab-ctl reconfigure
  • 1.

gitlab与ldap集成_ldap_06
重启gitlb服务:

sudo gitlab-ctl restart
  • 1.

gitlab与ldap集成_ldap_07
恩小彩蛋命令百度来的:

sudo gitlab-rake gitlab:ldap:check
  • 1.

gitlab与ldap集成_mail_08

ldap用户登陆gitlab

浏览器登陆gitlab地址,出现如下ldap登陆框并登陆用户
gitlab与ldap集成_git_09
登陆进入用户配置页面:
gitlab与ldap集成_git_10
貌似强制要求输入邮箱…
gitlab与ldap集成_git_11
傻傻的我以为登陆管理员root可以关闭邮件通知就可以?结果发现不是这样的…
gitlab与ldap集成_gitlab_12
gitlab与ldap集成_mail_13
老老实实配置smtp吧…

gitlab 集成mail配置

编辑gitlab.rb 开启smtp配置

编辑/etc/gitlab/gitlab.rb文件

vi /etc/gitlab/gitlab.rb 
  • 1.

gitlab与ldap集成_git_14
当然了由于使用qq邮箱先登陆邮箱 设置-账户这里
gitlab与ldap集成_ldap_15
**POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 **生成授权码
gitlab与ldap集成_git_16
将密码 参数替换到配置文件,如下:

 gitlab_rails['smtp_enable'] = true
 gitlab_rails['smtp_address'] = "smtp.qq.com"
 gitlab_rails['smtp_port'] = 465
 gitlab_rails['smtp_user_name'] = "75430361@qq.com"
 gitlab_rails['smtp_password'] = "xxxxxx"
 gitlab_rails['smtp_domain'] = "smtp.qq.com"
 gitlab_rails['smtp_authentication'] = "login"
 gitlab_rails['smtp_enable_starttls_auto'] = true
 gitlab_rails['smtp_tls'] = true
 gitlab_rails['smtp_pool'] = false

###! **Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert'**
###! Docs: http://api.rubyonrails.org/classes/ActionMailer/Base.html
# gitlab_rails['smtp_openssl_verify_mode'] = 'none'

# gitlab_rails['smtp_ca_path'] = "/etc/ssl/certs"
# gitlab_rails['smtp_ca_file'] = "/etc/ssl/certs/ca-certificates.crt"

### Email Settings

# gitlab_rails['gitlab_email_enabled'] = true

##! If your SMTP server does not like the default 'From: gitlab@gitlab.example.com'
##! can change the 'From' with this setting.
gitlab_rails['gitlab_email_from'] = '75430361@qq.com'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

gitlab与ldap集成_git_17

加载配置并重启服务
sudo gitlab-ctl reconfigure
  • 1.

gitlab与ldap集成_ldap_18

sudo gitlab-ctl restart
  • 1.

gitlab与ldap集成_gitlab_19

测试邮件发送功能

进入console控制台

[root@gitlab gitlab]# gitlab-rails console
  • 1.

发送测试邮件:

Notify.test_email('820042728@qq.com', 'zhangpengnihao', '张鹏').deliver_now

  • 1.
  • 2.

gitlab与ldap集成_git_20
邮箱接收到测试邮件,测试通过!
gitlab与ldap集成_mail_21
恩成功了继续尝试一下用户的登陆

ldap用户登陆绑定邮箱

由于zhangpeng用户之前输入了820042728@qq.com邮箱了,这里只能刷新一下 点击mail下面的重新发送了!弹出如下界面,重新发送激活邮件
gitlab与ldap集成_gitlab_22
邮箱受到激活邮件点击激活:
gitlab与ldap集成_ldap_23
重新登陆zhangpeng用户 or 刷新setting配置页面的continue就进入了gitlab控制台:
gitlab与ldap集成_ldap_24
尝试一下huozhonghao用户:
gitlab与ldap集成_gitlab_25
gitlab登陆
gitlab与ldap集成_gitlab_26
输入mail邮箱475602333@qq.com(邮箱不能与之前用户的重复,貌似一个邮箱只能绑定一个用户)
gitlab与ldap集成_ldap_27
gitlab与ldap集成_ldap_28
登陆邮箱激活用户:
gitlab与ldap集成_git_29
登陆huozhonghao用户如下:

gitlab与ldap集成_mail_30
恩 我还修改了此用户的个人头像!

总结

  1. 这里完成了 gitlab 与ldap mail的整合,ldap并没有进行更严格的匹配方式,mail也没有自定义配置
  2. 接下来要考虑gitlab项目组权限?各种配置
  3. 还有怎么能导入其他gitlab的仓库?
  4. 常用的命令**: sudo gitlab-ctl reconfigure sudo gitlab-ctl restart gitlab-rails console sudo gitlab-rake gitlab:ldap:check 等等**