gpt4 book ai didi

git - 使用 git-shell 并限制开发人员提交自己的项目

转载 作者:太空狗 更新时间:2023-10-29 12:50:02 25 4
gpt4 key购买 nike

我们正在使用 git-shell 来确保 git 帐户仅用于 git 操作。这很好用。

然而,在我们开始全职使用 git 之前,我们如何将其设置为类似于 github,从而根据您的公钥您只能提交到您自己的存储库?

据我所知,github 上的人可能正在推出他们自己的 git-shellsource code appears to be very simple to hack

最佳答案

我使用了一些更简单的东西,你只需要设置三个文件,authorized_keys 文件,gitsecurity.rb 文件和一个权限文件 gitpermissions 。为简单起见,它们都可以放在 git accounts .ssh 文件夹中。 (理解此处所需的基本 unix 管理技能)

gitpermissions 文件看起来像这样,并且应该是相当 self 解释的:

repo1.git|jane|rw
repo1.git|james|r
repo2.git|bob|rw

autorized_keys 文件看起来像这样:

command="/Users/git/.ssh/gitsecurity.rb jacob",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa rFaivBw.....5Rws jacob
command="/Users/git/.ssh/gitsecurity.rb bob",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa rFaivBw.....5Rws bob

最后是 gitsecurity.rb 脚本,只需复制并粘贴即可:

#!/usr/bin/ruby

class GitPermission
attr_accessor :username;
attr_accessor :repository;
attr_accessor :read;
attr_accessor :write;

def initialize(line)
bits = line.split('|');
if(bits.length!=3)
$stderr.puts "Invalid configuration file"
Process.exit(4)
end
@repository = bits[0]
@username = bits[1]
@read = bits[2].include?("r")
@write = bits[2].include?("w")
end

end

if(!ENV.has_key?("SSH_ORIGINAL_COMMAND"))
$stderr.puts "SSH not allowed to the git account."
Process.exit(1);
end
command = ENV["SSH_ORIGINAL_COMMAND"];

if(!ARGV.length == 1)
$stderr.puts "Authorised keys file misconfigured, username not specified correctly."
Process.exit(1);
end

if(!ARGV[0].match(/^[A-Za-z0-9]+$/))
$stderr.puts "Authorised keys file misconfigured, username contains invalid characters: "+ARGV[0];
Process.exit(1);
end
username = ARGV[0]

if(!command.match(/^git[ -]upload-pack /) && !command.match(/^git[ -]receive-pack /))
$stderr.puts "Only git commands are allowed."
Process.exit(2);
end

repository = command[(command.index(' ')+1)..-1]

if(!repository.match(/'.*'/))
$stderr.puts "Repository parameter incorrect."
Process.exit(2);
end
repository = repository[1,repository.length-2]

begin
file = File.new("/Users/git/.ssh/gitpermissions", "r")
while (line = file.gets)
p = GitPermission.new(line);
if(p.repository == repository && p.username == username)
if((p.write == true || (p.read == true && command.match(/^git[ -]upload-pack/))) )
exec "/usr/local/git/bin/" + command
Process.exit(0);
end
end
end
file.close
rescue => err
$stderr.puts "Problem with server configuration: #{err}"
Process.exit(4)
end

$stderr.puts "You do not have permission to complete this operation"
Process.exit(5)

关于git - 使用 git-shell 并限制开发人员提交自己的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2083807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com