GitLab集成LDAP

目录
一、下载并安装GitLab
二、重置超级管理员的密码
三、开启LDAP
四、测试
五、LDAP详细配置说明
六、设置GitLab
七、关于老用户的账号权限问题
八、关于密码修改问题
九、关于拉取代码

一、下载并安装GitLab
1、下载地址
参考地址:https://www.gitlab.com.cn/downloads/
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/trusty/main/g/gitlab-ce/gitlab-ce_8.17.4-ce.0_amd64.deb

2、安装
sudo dpkg -i gitlab-ce_8.17.4-ce.0_amd64.deb

sudo dpkg -i gitlab-ce_8.17.4-ce.0_amd64.deb
(正在读取数据库 ... 系统当前共安装有 279385 个文件和目录。)
正准备解包 gitlab-ce_8.17.4-ce.0_amd64.deb  ...
正在将 gitlab-ce (8.17.4-ce.0) 解包到 (8.17.4-ce.0) 上 ...
正在设置 gitlab-ce (8.17.4-ce.0) ...


       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.

     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/


gitlab: Thank you for installing GitLab!
gitlab: To configure and start GitLab, RUN THE FOLLOWING COMMAND:

sudo gitlab-ctl reconfigure

gitlab: GitLab should be reachable at http://zhuwenping-PC
gitlab: Otherwise configure GitLab for your system by editing /etc/gitlab/gitlab.rb file
gitlab: And running reconfigure again.
gitlab: 
gitlab: For a comprehensive list of configuration options please see the Omnibus GitLab readme
gitlab: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
gitlab: 

gitlab: GitLab now ships with a newer version of PostgreSQL (9.6.1), and will be used
gitlab: as the default in the next major release. To upgrade, RUN THE FOLLOWING COMMANDS:

sudo gitlab-ctl pg-upgrade

gitlab: For more details, please see:
gitlab: https://docs.gitlab.com/omnibus/settings/database.html#upgrade-packaged-postgresql-server
gitlab: 
It looks like GitLab has not been configured yet; skipping the upgrade script.

3、配置生效
sudo gitlab-ctl reconfigure

4、重启
sudo gitlab-ctl restart

sudo gitlab-ctl restart
ok: run: gitlab-workhorse: (pid 6742) 1s
ok: run: logrotate: (pid 6751) 0s
ok: run: nginx: (pid 6758) 1s
ok: run: postgresql: (pid 6769) 0s
ok: run: redis: (pid 6777) 0s
ok: run: sidekiq: (pid 6792) 0s
ok: run: unicorn: (pid 6797) 0s

二、重置超级管理员的密码
在安装完成后,访问gitlab地址,会进入密码的初始化设置界面。


密码的初始化.png

如果你漏设置,或者错过了,则需要进行下面步骤。
1、cd /opt/gitlab/bin/

2、sudo gitlab-rails console production

Loading production environment (Rails 4.2.7.1)
irb(main):001:0> u=User.where(id:1).first
=> #<User id: 1, email: "admin@example.com", created_at: "2019-07-18 11:18:27", updated_at: "2019-07-18 11:33:15", name: "Administrator", admin: true, projects_limit: 10, skype: "", linkedin: "", twitter: "", authentication_token: "9sZv9cxDJJ5htEBogCL3", theme_id: 2, bio: nil, username: "root", can_create_group: true, can_create_team: false, state: "active", color_scheme_id: 1, password_expires_at: nil, created_by_id: nil, last_credential_check_at: nil, avatar: nil, hide_no_ssh_key: false, website_url: "", notification_email: "admin@example.com", hide_no_password: false, password_automatically_set: false, location: nil, encrypted_otp_secret: nil, encrypted_otp_secret_iv: nil, encrypted_otp_secret_salt: nil, otp_required_for_login: false, otp_backup_codes: nil, public_email: "", dashboard: 0, project_view: 0, consumed_timestep: nil, layout: 0, hide_project_limit: false, otp_grace_period_started_at: nil, ldap_email: false, external: false, organization: nil, incoming_email_token: "713ddwkga5c959pix8muak9ym", authorized_projects_populated: nil>

用户名为admin@example.com
密码设置为12345678

3、设置两遍密码

irb(main):005:0> u.password='12345678'
=> "12345678"
irb(main):006:0> u.password_confirmation='12345678'
=> "12345678"
irb(main):007:0> u.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 6de78974-94dd-47ed-9d2c-1c38cdb46fa5) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
=> true
irb(main):008:0> exit

注意:密码的长度至少为8位,如果你设置为123456或admin这样子的大众密码,会在save的提示报错ActiveRecord::RecordInvalid: Validation failed: Password is too short (minimum is 8 characters)

irb(main):002:0> u.password='admin'
=> "admin"
irb(main):003:0> u.password_confirmation='admin'
=> "admin"
irb(main):004:0> u.save!
ActiveRecord::RecordInvalid: Validation failed: Password is too short (minimum is 8 characters)

三、开启LDAP
sudo vi /etc/gitlab/gitlab.rb

LDAP设置.png
GitLab用户的name.png

四、测试
访问地址http://localhost:8888/,本机IP就是192.168.8.17
这个访问地址是在/etc/gitlab/gitlab.rb中设置

GitLab URL.png

在原本登录框,增加了一个LDAP,对应下图的label


image.png

访问结果:


image.png

五、LDAP详细配置说明
/etc/gitlab/gitlab.rb 中:

### 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'] = true

###! **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: '192.168.10.62'
     port: 389
     uid: 'cn'
     bind_dn: 'cn=admin,dc=domain,dc=com'
     password: 'admin'
     encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
     active_directory: false
     allow_username_or_email_login: false
     block_auto_created_users: false
     base: 'ou=people,dc=domain,dc=com'
     user_filter: ''
     attributes:
       username: ['cn']
       email:    ['mail']
       name:     'description'
       first_name: 'givenName'
       last_name:  'sn'
     ## EE only
#     group_base: ''
#     admin_group: ''
#     sync_ssh_keys: false
EOS

其中:
label 是显示在GitLab登录框的上方文字描述。
host 和 port 是 LDAP 服务的主机地址及端口
bind_dn 和 password 是一个管理 LDAP 的 dn 及密码
base 表示 LDAP 将以该 dn 为 节点,向下查找用户
user_filter 表示以某种过滤条件筛选用户,比如假设我们只希望所属组为 java-platform 的用户来访问 GitLab,则可以在这里设置 (memberOf=ou=java-platform,ou=people,dc=domain,dc=com)
attributes 表示 GitLab 中的字段与 LDAP 中哪些字段可以相互对应,比如可以用 LDAP 中的 cn 来作为 GitLab 用户名, description 作为GitLab 的姓名。

六、设置GitLab
1、去掉注册


设置.png

Admin Area.png
image.png

登录效果见下:

image.png

2、解除Blocked用户


解除Blocked用户.png

3、创建用户为管理员


image.png
新建用户.png

七、关于老用户的账号权限问题
在实际情况下,我们一般是先gitlab已有账号,且运行了一段时日。后面的需求是接入LDAP,但是用户和账号是想保留,对于用户来说是无感知的接入。

下面说说我们的做法。
1、LDAP创建的账号,cn=手机号,description=姓名,mail=邮箱


LDAP创建的账号.png

2、gitLab创建的账号,Username=手机号,Name=姓名, Email=邮箱


gitLab创建的账号.png

两个账户体系的账号,只要做到LDAP的邮箱mail和gitLab的邮箱Email一致,前者就不会重新创建账号,后者的姓名不会被前者所覆盖。

3、gitLab登录,账号的密码是12345678


gitLab登录.png

4、LDAP登录,账号是上文配置/etc/gitlab/gitlab.rb的
attributes:
username: ['cn']
密码为初始值123456

LDAP登录.png

5、为了区分验证,是否覆盖,是否会创建两个用户。
我们可以把Name姓名设置为不相等的值。

6、如果两边的账号邮箱不一致,会存在两个账号。见下图:


账号邮箱不一致.png

八、关于密码修改问题
在gitlab通过admin@example.com超级管理员修改某个用户的密码,并不会同步到LDAP。
背景描述:
一个用户在LDAP的账号,其邮箱是姓名格式,在gitLab的账号,其邮箱是手机号格式。
于是在gitLab后台看到两个用户。

如果我们修改他们的密码,gitLab账号保持8位密码12345678不变,LDAP账号修改为123456789,九位长度的密码,以作区别。

现在同样在LDAP和GitLab Standard两个tab页面登录。
1、LDAP页面登录


LDAP使用手机号登录成功.png

2、GitLab Standard页面登录
邮箱登录都能成功,但是登录密码需要对应好。
手机号、手机号格式的邮箱也都能登录成功,密码均为12345678
其中,姓名格式的邮箱,是LDAP的账号,超级管理员将密码修改为123456789

手机号格式的邮箱登录成功.png
手机号登录成功.png
姓名格式的邮箱登录成功.png

因为gitLab账号的Username为手机号,所以上面的手机号也能在GitLab Standard登录成功。


image.png

有个问题,手机号格式的邮箱账号,姓名格式的邮箱账号,手机号在GitLab Standard登录该使用哪个账号的密码呢??
原则是先入为主,先创建的账号是手机号格式的邮箱账号,后创建的账号是LDAP的姓名格式的邮箱账号。

3、GitLab Standard支持Username(在本文Username就是用户的手机号)和Email两种登录方式,密码是同一个。
这个不是关键,需要说明的是,LDAP接入后,gitLab管理后台,看到的用户列表,都是gitLab用户,只能在GitLab Standard登录框进行登录。
至于LDAP登录框, 使用的cn登录,后面把mail登录禁用就好。在本文,cn就是用户的手机号,密码默认为123456。

注意:同一个手机号在两个登录框尝试登录,所输入的密码不是同一个。
LDAP --------- 手机号/ 123456 (密码始终不会变,除非我们在lam修改)
GitLab Standard ---------- 手机号/12345678, xxx@xx.com/12345678 (因为我们支持Username和Email两种登录方式)

九、关于拉取代码
只能使用LDAP的账号密码,不能使用GitLab Standard的账号。

git clone http://192.168.8.17:8888/project1/app1.git
13xxxxxxxx 密码是123456就能拉下来; 密码是123456789就不行。

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268