公司代码库用Git,全部用SSH认证,多个代码库多个用户,权限管理是个头疼的问题,今天终于有空测试下Gitolite,
Gitolite是在Git之上的一个授权层,依托sshd
或者httpd
来进行认证。(概括:认证是确定用户是谁,授权是决定该用户是否被允许做他想做的事情)。
Gitolite允许你定义访问许可而不只作用于仓库,而同样于仓库中的每个branch和tag name。你可以定义确切的人(或一组人)只能push特定的"refs"(或者branches或者tags)而不是其他人。
测试环境:
【Gitolite服务器】git代码库和Gitolite是同一台服务器。
【管理端】管理员管理Gitolite
【客户端】Git代码库使用者
安装Gitolite
首先各服务器都要安装git环境,
ubuntu: apt-get install git-core 或 apt-get install git centos: yum install git
Gitolite服务器
建gituser帐号,源代码仓库放在服务器的普通帐号下 [email protected]# adduser --system --shell /bin/bash --group gituser [email protected]# passwd gituser 用户gituser设定密码,在SSH公钥建立后可以把这个密码禁掉
管理端生成密钥
[email protected]# adduser gitadmin [email protected]# passwd gitadmin [email protected]#su - gitadmin [email protected]~$ssh-keygen -t rsa 这样就生成gitadmin的密钥 我们将gitadmin下的密钥拷贝到Gitolite的gituser用户下 [email protected]~$ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] 第一次传输要输入gituser的密码即可。 当然这里只是两台机子做SSH无密码登录验证,也没啥难的。 确认是否成功, [email protected]~$ssh [email protected] 这样就无密码登录到了Gitolite服务器上 [email protected]~$ mv .ssh/authorized_keys gitadmin.pub 把之前传过来密钥做个标记
安装Gitolite
在Gitolite服务器上操作
[email protected]~$ git clone https://github.com/sitaramc/gitolite.git #下载源码 [email protected]~$ mkdir bin [email protected]~$ ls bin gitolite gitadmin.pub [email protected]~$ ./gitolite/install -to /home/gituser/bin/ [email protected]~$ cd bin/ [email protected]~$ ls commands gitolite gitolite-shell lib syntactic-sugar triggers VERSION VREF 这样我们就在gitolite安装好了,下面生成gitadmin.pub的管理git库 [email protected]:~/bin$ gl-setup -q ~/gitadmin.pub creating gitolite-admin... Initialized empty Git repository in /home/gituser/repositories/gitolite-admin.git/ creating testing... Initialized empty Git repository in /home/gituser/repositories/testing.git/ [master (root-commit) 74ddb16] start 2 files changed, 6 insertions(+) create mode 100644 conf/gitolite.conf create mode 100644 keydir/sunny.pub 可以看出会有两个默认git项目库 gitolite-admin、testing 执行gl-setup时几点注意: gl-setup要放到$PATH,请确定~/bin已放在PATH变量,如没有,请编辑~/.bashrc,最后面添加 PATH=~/bin:$PATH 并执行以下命令生效: sh ~/.bashrc
Gitolite的管理及权限设定
gitolite的管理,包括用户管理和代码库管理,都是通过操作一个指定名称的库来实现:gitolite-admin仓库。因此gitolite的管理员sunny需要先把这个库抓到本地,进行必要的配置后,再push到remote 服务器,让设定生效
管理端服务器
抓取gitolite-admin仓库到本地
[[email protected] tmp]$ git clone [email protected]:gitolite-admin Initialized empty Git repository in /tmp/gitolite-admin/.git/ remote: Counting objects: 6, done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 0), reused 0 (delta 0) Receiving objects: 100% (6/6), done. You have mail in /var/spool/mail/root [[email protected]]$ cd gitolite-admin/ [[email protected] gitolite-admin]$ ls conf keydir conf 配置项目与权限的 keydir ssh 密钥所在目录
Gitolite配置管理
- 新增加用户 增加testuserGIT库有访问权限
GIT客户端 testuser产生密钥传给管理服务器
[[email protected] ~]# su testuser [[email protected] ~]$ ssh-keygen -t rsa id_rsa id_rsa.pub 将id_rsa.pub传给管理服务器的 gitolite-admin/keydir目录下命名为testuser.pub
进入到管理端 [[email protected] gitolite-admin]$ ls conf keydir [[email protected] gitolite-admin]$ cd keydir/ [[email protected] keydir]$ ls gitadmin.pub testuser.pub [[email protected] keydir]$ cd .. [[email protected] gitolite-admin]$ git add . [[email protected] gitolite-admin]$ git commit -m "add testuser pubkey" [master 7fd7da1] add testuser pubkey Committer: sunny <[email protected](none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email [email protected] If the identity used for this commit is wrong, you can fix it with: git commit --amend --author=‘Your Name <[email protected]>‘ 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 keydir/testuser.pub [[email protected] gitolite-admin]$ git push Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 681 bytes, done. Total 4 (delta 0), reused 0 (delta 0) remote: remote: ***** WARNING ***** remote: the following users (pubkey files in parens) do not appear in the config file: remote: testuser(testuser.pub) To [email protected]:gitolite-admin 74ddb16..7fd7da1 master -> master 这样就给testuser添加了SSH认证,不过testuser只能访问系统自带的testing库外,操作不了其他的库
2.配置代码库权限
Gitolite服务器
现在要在gitserver新建一个代码仓proj_a,依然是对gitolite-admin进行配置
编辑gitolite-admin/conf/gitolite.conf,仿照以后的库的格式,添加:
[[email protected] gitolite-admin]$ cd conf/ [[email protected] gitolite-admin]$ cat gitolite.conf repo gitolite-admin RW+ = gitadmin repo testing RW+ = @all repo proj_a # 这里设定是新增库的名称 RW+ = gitadmin # R是读权限,W是写权限,+是包括“强制更新一个分支,删除分支和更新一个Tag”的权限 RW = testuser # 设定RW权限的人
执行add, commmit, push进行推送,即可
[[email protected] gitolite-admin]$ git add . [[email protected] gitolite-admin]$ git commit -m "add a new repo" [master a0f9f26] add a new repo Committer: sunny <[email protected](none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email [email protected] If the identity used for this commit is wrong, you can fix it with: git commit --amend --author=‘Your Name <[email protected]>‘ 1 files changed, 7 insertions(+), 0 deletions(-) [[email protected] gitolite-admin]$ git push Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 395 bytes, done. Total 4 (delta 0), reused 0 (delta 0) remote: creating proj_a... remote: Initialized empty Git repository in /home/gituser/repositories/proj_a.git/ To [email protected]:gitolite-admin 7fd7da1..a0f9f26 master -> master
gitolite.conf的格式其实有相当多的复杂配置(详情请看官方手册),一般来说,象以下这样的,已够几个人的开放型小团队使用,例如:
@proj_a = sunny jacob # @proj_a是分组命名,分组用@表示,可以在后面引用分组 @proj_b = sunny taylor jean # 如果有多个用户,用空格隔开@admins = sunny @qa = elapse flora @engineers = sunny jacob taylor jean @staff = @admins @qa @engineers # 分组可以被引用 repo gitolite-admin # 这个是gitolite的管理仓库,sunny是指定可以对其进行操作的管理员 RW+ = sunny repo proj_a RW+ = @admins RW = @proj_a elapse repo proj_b RW+ = sunny RW = @engineers flora R refs/tags/ = @qa # 这里设定的,是QA这个组,对refs/tags/开头的路径的文件只有读权限 repo testing RW+ = @stall
3.删除用户
管理员在本地,删除了gitolite-admin/keydir目录下对应的用户pubkey,然后执行git 的rm操作,再 commit, push推送,即可
[[email protected] gitolite-admin]$ rm -f keydir/jacob.pub [[email protected] gitolite-admin]$ git rm keydir/jacob.pub [[email protected] gitolite-admin]$ git commit -m "delete a user" [[email protected] gitolite-admin]$ git push
4.删除代码库
如要删除proj_a代码库,管理员要分两步走:
本地编辑gitolite-admin/conf/gitolite.conf,删除相关的repo proj_a代码,然后add, commit, push推送
[[email protected] gitolite-admin]$ git add . [[email protected] gitolite-admin]$ git commit -m "remove a repo" [[email protected] gitolite-admin]$ git push
用gituser帐号或root帐号进入gitserver服务器,把gituser/repositories下相关的repo目录删除
[email protected]:/home/gituser/repositories$ rm -Rf proj_a.git # 库对应proj_a的目录,带.git结尾
5.修改代码库的名字
改名,也是分两步,和上面执行删除的顺序反过来,
先用gituser帐号或root帐号进入gitserver服务器,cd repositories,执行移动
[email protected]:/home/gituser/repositories$ mv proj_a.git proj_b.git
回到管理端,修改conf/gitolite.conf,把old-name的地方修改为new-name,然后add, commit并push
[[email protected] gitolite-admin]$ git add . [[email protected] gitolite-admin]$ git commit -m "rename a repo" [[email protected] gitolite-admin]$ git push
6.普通用户签出操作
例如jacob要把代码库checkout出来,本地修改,然后再和remote库的代码进行版本合并。这些操作是纯粹的git操作了,团队的日常开发流程,正是这样子开展。把remote库抓取下来,然后才开始修改
[email protected]:~$ git clone [email protected]:proj_a # 项目默认放到proj_a目录 或 [email protected]:~$ git clone [email protected]:proj_a my_proj_a # 项目被下载到my_proj_a目录下
jacob本地已有一个现成的项目proj_a正在开发,并用git在管理着,现在想share到这个项目到proj_a
[email protected]:~$ cd proj_a [email protected]:~/proj_a$ git push --all [email protected]:proj_a # 推送到remote server
7. 列出本人拥有权限的库
使用普通用户gituser的帐号尝试登录remote服务器即可,
[[email protected] proj_a]$ ssh [email protected] PTY allocation request failed on channel 0 hello testuser, this is gitolite 2.2-1 (Debian) running on git 1.7.9.5 the gitolite config gives you the following access: R W proj_a @R_ @W_ testing Connection to 192.168.1.50 closed.
Gitolite管理git server代码库权限基本就介绍这么多,后期测试不同服务器之间git代码库的导入和备份。请继续关注。