再谈git的http服务-权限控制hooks版
通过git-http-backend方法提供的http服务基本上可以做到认证用户才能使用,但只能控制到服务器路径访问,而且无法区分读写。经过不懈努力,找到了方法,相关脚本及配置见后。
基本思路就是利用apache的路径权限设置控制版本库的读权限;通过hooks/pre-receive钩子来控制版本库的写权限,具体钩子的原理和用户这里不写了。git提供了几个全局变量可以在pre-receive钩子里面使用,详情可以参看git-http-backend manual page。
通过REMOTE_USER获取执行push的用户名,通过PATH_INFO获取项目名称。
此方法的弊端就是配置比较复杂,而且需要重启apache
附上pre-receive:
#!/bin/sh
prj=${PATH_INFO:1}
prj=${prj%/*}"-write:"
priusr=$(cat ../git.group|grep $prj)
priusr=${priusr##*:}
for ele in $priusr
do
if [ $ele == $REMOTE_USER ]
then
#echo 'user found ' $ele
exit 0
fi
done
echo "Access denied"
exit 1
以及apache的git.conf相应内容:
#config for git-http-backend SetEnv GIT_PROJECT_ROOT /git/repositories SetEnv GIT_HTTP_EXPORT_ALL ScriptAlias /mygit/ /usr/libexec/git-core/git-http-backend/ #privilege config <Location /mygit/test> AuthType Basic AuthName "Git Access" AuthUserFile /git/repositories/git.passwd AuthGroupFile /git/repositories/git.group <Limit GET PUT POST DELETE PROPPATCH MKCOL COPY MOVE LOCK UNLOCK> Require group test-read</Limit> </Location>
/git/repositories/git.group如下,此处的read组一定要包含write组。
test-read:bill jobs
test-write:jobs
按照以上配置,bill,jobs都可以clone和pull项目,但只有jobs可以push