【问题标题】:Can't git clone by adding ssh-key in Node.js无法通过在 Node.js 中添加 ssh-key 来进行 git clone
【发布时间】:2013-03-10 09:21:40
【问题描述】:
  # Write the SSH-KEY to the disk
  fs.writeFile "/cgrepos/.ssh/#{repo.id}.pub", repo.public_key, (err) ->
    throw err if err

    fs.writeFile "/cgrepos/.ssh/#{repo.id}", repo.private_key, (err) ->
      throw err if err

      exec "chmod 400 /cgrepos/.ssh/#{repo.id} && eval `ssh-agent -s` && ssh-add /cgrepos/.ssh/#{repo.id}", (error) ->
        throw error if error
        # First, delete the git repo on the hard drive, if it exists
        exec "rm -rf #{git_location}", options, (error) ->
          throw error if error
          # Second, clone the repo into the location
          console.log "Cloning repo #{repo.id}: #{repo.repo_name} into #{git_location}. This could take a minute"
          exec "git clone #{repo.url} #{git_location}", options, (error) ->
            throw error if error

我正在 node 中尝试这个(使用 coffee 用于那些很棒的)。但是由于某种原因,当它运行时,它给了我一个错误:Error: Command failed: conq: repository access denied. deployment key is not associated with the requested repository.

不知道我做错了什么。如果我直接从命令行运行这些命令,一切似乎都正常。有什么想法吗?

【问题讨论】:

  • 你有没有试过直接用ssh复制这个,把git排除在外?尽管您不太可能从 Bitbucket 的服务器中获得任何有用的信息(从错误消息中我认为这是您正在使用的),但您应该看到“您可以使用 git 或 hg 连接到 Bitbucket。Shell 访问被禁用。”如果有效;不行的话可以试试ssh -v调试。
  • 您可以尝试的另一件事是制作一个运行exec ssh -v "$@" 的包装shell 脚本并设置GIT_SSH 环境变量,以便git 以详细模式运行SSH。然后你可以看到git是如何运行SSH的,这可能会给你一些线索。

标签: node.js command-line ssh-keys


【解决方案1】:

当你尝试从 node.js 执行git clone 进程时,它运行在不同的环境中。

当您在受保护的(基于 ssh 协议的)存储库上使用 git clone 时,ssh-agent 首先尝试使用提供的公钥对您进行身份验证。由于exec 每次调用都使用不同的运行时环境,即使您明确添加私钥,由于运行时环境不同,它也不会起作用。

在 ssh 中进行身份验证时,git clone 会查找 SSH_AUTH_SOCK。通常,此环境变量具有您的密码密钥环服务的路径,例如(gnome-keyring 或 kde-wallet)。

先试试这个检查一下。

env | grep -i ssh

它应该列出 SSH_AGENT_PID 和 SSH_AUTH_SOCK。问题是运行git clone 时未设置这些环境变量。因此,您可以将它们设置为 exec 函数调用中的选项(只需 SSH_AUTH_SOCK 就足够了)。看看 here 如何将 env 密钥对传递给 exec。

var exec = require('child_process').exec,
    child;

child = exec('git clone cloneurl', {
  cwd: cwdhere,      // working dir path for git clone
  env: {
           envVar1: envVarValue1,
           SSH_AUTH_SOCK: socketPathHere
       } 
}, callback);

如果这不起作用,请尝试在 exec 函数中执行 ssh -vvv user@git-repo-host。查看这个过程的输出,你会发现错误。

如果错误显示为debug1: No more authentication methods to try. Permission denied (publickey).,则向 $HOME/.ssh/config 文件添加主机别名,如下所示。

Host hostalias
 Hostname git-repo-host
 IdentityFile ~/.ssh/your_private_key_path

这将对指定主机的所有身份验证请求使用提供的私钥。在此选项中,您还可以更改您的 origin's url 以使用上面文件中配置的 hostalias。 reporoot/.git/config 文件将如下所示。

[remote "origin"]
    url = user@hostalias:repo.git

【讨论】:

  • 我正在尝试以编程方式为我不拥有或控制的数千个存储库执行此操作。我生成了ssh 密钥,我们的用户会将它们添加到他们的部署密钥中。所以我不确定这是否可行
  • 在这种情况下,hostalias 方式不适用。但是您应该尝试为该过程设置适当的环境。
猜你喜欢
  • 2023-03-29
  • 2015-11-07
  • 2015-11-23
  • 2021-12-07
  • 2020-07-20
  • 2018-10-29
  • 2012-05-03
  • 1970-01-01
  • 2015-12-15
相关资源
最近更新 更多