Git -只读本地副本

  • 本文关键字:副本 只读 Git git
  • 更新时间 :
  • 英文 :


我有一个在生产服务器上运行的repo的本地副本。

有些人一直在修改这个本地副本上的小文件,但我认为这是不可接受的,因为这是一个生产副本,考虑到我们禁止直接在主分支中进行更改(pr是强制性的)。

我想让本地副本只读,但是当我们使用git pull时,这些文件仍然需要写入。如果我执行chmod -R -w .,我明白git pull是不可能的。

保护这个副本不被本地编辑的最好方法是什么?

我假设这个问题是在问:"如何使非sudoers帐户的目录不可写,但必须使用git pull命令可写">

请注意,在这个答案中,我不打算输入sudo,只是使用常识,其中命令需要sudo'ed

没有关于如何在git上完成它的文档(据我所知)。但你总可以做点hack。基本思路如下:

  • 创建新用户(我们称之为userX)或使用现有帐户
  • 将目录所有者设置为X(例如:chown -R userX:userX .),
  • 然后使文件权限只允许用户X写(例如:chmod -R 775 .),
  • 然后实现一些方法来执行git pull与用户X,不需要登录到用户X的帐户,不使用sudo(下面描述)。

选择1:

设置git二进制文件的setuid(4000)特殊权限,并将其所有者更改为X

  • chmod 4775 /usr/bin/git
  • chown userX:root /usr/bin/git这将使git的二进制文件归用户X所有,并归组root所有(不变),并且当git二进制文件被执行时,它将以用户X而不是当前用户
  • 执行。

选择2:

设置git二进制的setgid(2000)特殊权限,并将其组所有者更改为X

  • chmod 2755 /usr/bin/git
  • chown root:userX /usr/bin/git这将使git的二进制文件归root所有(不变),归组userX所有,并且当git二进制文件被执行时,它将以(组)用户X而不是当前用户
  • 执行。

阅读更多关于setgid和setgid的信息:

  • https://docs.oracle.com/cd/E19683-01/816-4883/secfile-69/index.html
  • https://linuxconfig.org/how-to-use-special-permissions-the-setuid-setgid-and-sticky-bits
  • https://www.liquidweb.com/kb/how-do-i-set-up-setuid-setgid-and-sticky-bits-on-linux/

选择3:

  • 假设您的存储库存在于/var/www/html/myrepo(默认)
  • 运行apache(或任何你喜欢的web服务器),让它监听一些未使用的端口,例如端口8081(确保端口不暴露给互联网出于安全原因,例如:被防火墙过滤,或者它只是绑定到localhost而不是0.0.0.0或公共ip地址)
  • 假设您的apache用户是www-data(默认)
  • chown -R www-data:www-data /var/www/html/myrepo
  • chmod -R 755 /var/www/html/myrepo
  • 在apache中创建一个vhost,文件根指向某个目录(例如:/home/userX/www
  • )在/home/userX/www/index.php中创建包含的php文件
<?php
chdir("/var/www/html/myrepo");
exec("git pull origin branchnamehere");
  • 当您需要执行git pull时,只需运行命令:wget http://localhost:8081

解释

选项1和选项2:

  • 将包含存储库的目录设置为只能由用户X可写
  • setuid或setgid特殊权限:
When set-user identification (setuid) permission is set on an executable file, 
a process that runs this file is granted access based on the owner of the file
(usually root), rather than the user who is running the executable file. 
This special permission allows a user to access files and directories that 
are normally only available to the owner. 
For example, the setuid permission on the passwd command makes it possible 
for a user to change passwords...
  • 所以,你将git的二进制文件设置为以X用户执行,因此git pull命令可以在目录中写入,但普通用户不能在目录
  • 中写入

选择3:

  • 将包含您的存储库的目录设置为只能由apache用户(例如:www-data)可写
  • 这里不需要设置id或setgid特殊权限
  • 当一个HTTP请求被webserver(例如:apache)处理时,它将作为webserver的用户(例如:www-data)运行
  • 在索引页
  • ,有一个php命令执行命令在shell包含git pull命令
  • 还确保将/home/userX/www/index.php设置为555或不可写(因此非sudoers不能写入它)

比较
  • 选项1和选项2可能是危险的,因为我们正在更改git的二进制权限,所以更新git可能会破坏
  • 备选方案3可能会引入计算资源开销,因为我们需要运行额外的web服务器

其他想法h1> li>将repo的所有者更改为用户Xchown -R userX:userX /var/www/html/myrepo
  • 将权限修改为仅由所有者chmod -R 775 /var/www/html/myrepo可写
  • 创建bash脚本/home/me/pullme.sh,包含:
  • #! /bin/bash
    # change directory to your repository
    cd /var/www/html/myrepo
    # execute git pull
    git pull origin branchnamehere
    
    • 让你的bash脚本属于用户Xchown userX:userX /home/me/pullme.sh
    • 设置脚本的setuidchmod 4755 /home/me/pullme.sh
    • 当你想拉,只需运行sh /home/me/pullme.sh

    最新更新