在Chef中进行Git身份验证

时间:2013-12-09 11:58:22

标签: git ssh chef

在使用Chef部署应用程序时,我已经使用以下资源从私有github存储库中克隆了代码库:

git '/mnt/application' do
    repository 'git@github.com:organization/repository'

    reference 'master'
    action :sync

    user node.application.user
    group node.application.user
end

但是,在扫描git资源的文档后,我无法看到您如何提供用于身份验证的密钥文件。我也很困惑如何将这个密钥存储在数据包中,因为该文件包含一堆新行。有什么想法吗?

6 个答案:

答案 0 :(得分:27)

ssh_wrapper "ssh -i /some/path/id_rsa"

如果有人碰到这个,上面的内容对我没用,我一直收到错误:

error: cannot run ssh -i /some/path/id_rsa: No such file or directory

指定ssh_wrapper的作用是设置GIT_SSH环境变量,结果表明你无法在GIT_SSH环境变量中提供参数(参见Git clone with custom SSH using GIT_SSH error)。

相反,您需要先将脚本写入文件,然后将GIT_SSH设置为该文件。

所以:

file "/some/path/git_wrapper.sh" do
  owner "your_user"
  mode "0755"
  content "#!/bin/sh\nexec /usr/bin/ssh -i /some/path/id_rsa \"$@\""
end

将git资源部分更改为:

git "/opt/mysources/couch" do
  repository "git://git.apache.org/couchdb.git"
  reference "master"
  action :sync
  ssh_wrapper "/some/path/git_wrapper.sh"
end

答案 1 :(得分:12)

我们使用Mercurial的类似设置,但我希望它与Git相同。

我们使用ssh密钥进行身份验证。密钥存储在加密的数据库中(换行符替换为“\ n”)。首先,在databag节点上创建此私钥。

git_key = Chef::EncryptedDataBagItem.load( "private_keys", "git_key" )
file "/some/path/id_rsa" do
  content git_key['private']
end

然后在使用ssh_wrapper连接到git存储库时使用它:

git "/opt/mysources/couch" do
  repository "git://git.apache.org/couchdb.git"
  reference "master"
  action :sync
  ssh_wrapper "ssh -i /some/path/id_rsa" #the path to our private key file
end

答案 2 :(得分:1)

我经历了同样的问题,只有我失踪的是这个命令然后一切顺利:

GIT_SSH_COMMAND="ssh -i ~/.ssh/bitbucket_rsa"

参考和我的整个步骤可以在我的博客上找到:http://www.sadafnoor.com/blog/simplest-way-to-write-your-chef-cookbook-that-git-clone-private-repo-using-bitbucket-deploy-key/

答案 3 :(得分:0)

如果您在Linux发行版中存储了<your home directory>/.ssh中的ssh密钥并将github.com添加到<your home directory>/.ssh/known_hosts

您可以使用以下命令将github.com添加到known_hosts

ssh-keyscan -H github.com >> <your home directory>/.ssh/known_hosts

执行此操作后,您可以使用主厨的git资源克隆您的仓库

答案 4 :(得分:0)

基于hintsadaf2605的使用,这对我来说是最简单的方法–我只需要确保设置正确的用户/组并关闭StrictHostKeyChecking:

git '/path/to/destination' do
  environment 'GIT_SSH_COMMAND' => 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /path/to/private_key'
  repository 'git@github.com:your/repo.git'
  reference 'master'
  action :sync
  user 'vagrant'
  group 'vagrant'
end

答案 5 :(得分:-1)

你应该尝试这本食谱https://github.com/poise/application_git。它解决了你提到的问题。

使用此食谱,您可以使用application_git资源,指定私钥:

application_git '/srv/myapp' do
  repository 'git@github.com:organization/repository'
  deploy_key '/some/path/id_rsa'
end