简体   繁体   English

GitPython - 使用 ssh 密钥克隆 - 主机密钥验证失败

[英]GitPython - cloning with ssh key - Host key verification failed


i have a problem with cloning git repository in my application.我在我的应用程序中克隆 git 存储库时遇到问题。

KEY_FILE = "/opt/app/.ssh/id_rsa"

def read_git_branch(config_id, branch):
    config = RepoConfig.objects.get(id=config_id)
    save_rsa_key(Credentials.objects.get(id=1).key)
    git_ssh_identity_file = os.path.expanduser(KEY_FILE)
    git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
    with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
        with tempfile.TemporaryDirectory() as tmpdir:
            repo = Repo.clone_from(config.url, tmpdir, branch=branch)
            branch_obj, _ = Branch.objects.get_or_create(name=branch)
            ....

def save_rsa_key(key):
    if not os.path.exists(os.path.dirname(KEY_FILE)):
        try:
            os.makedirs(os.path.dirname(KEY_FILE))
        except OSError as exc:
            if exc.errno != errno.EEXIST:
                raise
    with open(KEY_FILE, 'w') as id_rsa:
        id_rsa.write(key)
        os.chmod(KEY_FILE, 0o600)

Expected result is to clone repository to temporary directory, do something with it and delete all files.预期结果是将存储库克隆到临时目录,对其进行处理并删除所有文件。
Instead I'm getting:相反,我得到:

DEBUG/ForkPoolWorker-2] AutoInterrupt wait stderr: b'Host key verification failed.\\nfatal: Could not read from remote repository.\\n\\nPlease make sure you have the correct access rights\\nand the repository exists.\\n' DEBUG/ForkPoolWorker-2] AutoInterrupt wait stderr: b'主机密钥验证失败。\\n致命:无法从远程存储库读取。\\n\\n请确保您具有正确的访问权限\\n并且存储库存在。\\n'

git.exc.GitCommandError: Cmd('git') failed due to: exit code(128) cmdline: git clone --branch=master -v git@gitlab.foo:bar/project.git /tmp/tmpi_w2xhgt stderr: 'Host key verification failed. git.exc.GitCommandError: Cmd('git') 失败原因:退出代码(128) cmdline: git clone --branch=master -v git@gitlab.foo:bar/project.git /tmp/tmpi_w2xhgt stderr: '主机密钥验证失败。

When i try to connect to the same repo directly from machine with key file created by code above with:当我尝试使用由上面的代码创建的密钥文件直接从机器连接到同一个 repo 时:

ssh-agent bash -c 'ssh-add /opt/app/.ssh/id_rsa; git clone git@gitlab.foo:bar/project.git'

Repo is cloned without problems + host is added to known_hosts .回购克隆没有问题+主机被添加到known_hosts After doing that my code works as expected...这样做之后,我的代码按预期工作......

It has to be something with known_hosts .它必须是known_hosts东西。 Anyone had similar problem?有人遇到过类似的问题吗?

Thanks for your help.谢谢你的帮助。

You should use env of clone_from.您应该使用 clone_from 的 env。

with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
    repo = Repo.clone_from(config.url, tmpdir, branch=branch)

git.Repo.clone_from(url, repo_dir, env={"GIT_SSH_COMMAND": 'ssh -i /PATH/TO/KEY'})

This variant:这个变种:

git.Repo.clone_from("git@bitbucket.org:user/coolrepo.git", r"..\coolrepo", env=dict(GIT_SSH_COMMAND="ssh -i id_rsa"))

works fine for me!对我来说很好用!

While the existing answers cover cases where missing the SSH env was the issue, I had a scenario where the remote host key would only be accepted via GitPython, and the environment wasn't able to be modified to include that host key in known hosts.虽然现有答案涵盖了缺少 SSH 环境的情况,但我有一个场景,即只能通过 GitPython 接受远程主机密钥,并且无法修改环境以在已知主机中包含该主机密钥。

To ensure host key mismatches never break your code, disable strict host key checks through manipulation of the ssh command:为确保主机密钥不匹配永远不会破坏您的代码,请通过操作ssh命令禁用严格的主机密钥检查:

git.Repo.clone_from(
    url, 
    repo_dir, 
    env={
        "GIT_SSH_COMMAND": "ssh -o StrictHostKeyChecking=no -i /path/to/key"
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果使用子进程运行,ssh 主机密钥验证失败 - ssh host key verification failed if run with subprocess CreateProcessW 失败错误:2 ssh_askpass:posix_spawn:没有这样的文件或目录主机密钥验证失败,远程服务器上的 jupyter notebook - CreateProcessW failed error:2 ssh_askpass: posix_spawn: No such file or directory Host key verification failed, jupyter notebook on remote server 如何使用带有 ssh 密钥的 GitPython? - How do I use GitPython with a ssh key? 使用mpi4py的主机密钥验证失败 - Host key verification failed using mpi4py 在主管下使用gitpython时,SSH身份密钥被忽略 - SSH identity key ignored when using gitpython under supervisor Python 操作系统如何接受 SSH 密钥的验证 - Python os how to accept verification for SSH key Windows 上的 Jenkins:iOS 的 UE4 打包失败,并显示“主机密钥验证失败”。 - Jenkins on Windows: UE4 packaging for iOS fails with "Host key verification failed." 在base 64中获取ssh服务器的主机密钥 - Get host key of an ssh server in base 64 salt-ssh:使用python api禁用SSH主机密钥检查 - salt-ssh : Disable SSH Host Key Checking with python api Python Paramiko:手动验证SSH主机密钥指纹 - Python Paramiko: verifying SSH host key fingerprints manually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM