Chef 中的 Git 身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20470076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:26:23  来源:igfitidea点击:

Git authentication in Chef

gitsshchef

提问by L. Adamek

When deploying an application with Chef, I've got the code base set to be cloned from a private github repository with the following resource:

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

git '/mnt/application' do
    repository '[email protected]:organization/repository'

    reference 'master'
    action :sync

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

However, after scanning the documentation for the gitresource, I can't see how you supply the key file for authentication. I'm also confused as to how to store this key in a data bag, as the file contains a bunch of new lines. Any ideas?

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

回答by psamaan

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

In case someone comes across this, the above didn't work for me, I kept getting the error:

如果有人遇到这个问题,上面的方法对我不起作用,我不断收到错误消息:

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

What specifying ssh_wrapper does is it sets the GIT_SSH environment variable, and it turns out you can't provide parameters in the GIT_SSH environment variable (see Git clone with custom SSH using GIT_SSH error).

指定 ssh_wrapper 的作用是设置 GIT_SSH 环境变量,结果证明您无法在 GIT_SSH 环境变量中提供参数(请参阅使用 GIT_SSH 错误使用自定义 SSH 的 Git 克隆)。

Instead, you would need to write your script to a file first, then set GIT_SSH to it.

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

So:

所以:

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

And change the git resource part to:

并将 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

回答by Draco Ater

We use the similar setup for Mercurial, but it should be the same with Git, I hope.

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

We use ssh keys to authenticate. The key is stored in encrypted databag (with newlines replaced by "\n"). First of all this private key is created on the node from databag.

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

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

And then use it when connecting to git repository using ssh_wrapper:

然后在使用 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

回答by sadaf2605

I went through same problem, Only thing I was missing was this command then everything went well:

我遇到了同样的问题,我唯一缺少的是这个命令,然后一切顺利:

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

Reference and for my whole steps can be found at my blog: http://www.sadafnoor.com/blog/simplest-way-to-write-your-chef-cookbook-that-git-clone-private-repo-using-bitbucket-deploy-key/

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

回答by Vineeth Guna

if you are in a linux distribution store your ssh key in <your home directory>/.sshand add github.com to <your home directory>/.ssh/known_hosts

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

You can add github.com to known_hosts using the following command

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

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

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

After doing this you can clone your repo using gitresource of chef

完成此操作后,您可以使用git厨师资源克隆您的存储库

回答by Molotoff

Based on the hintby sadaf2605, this was the easiest way for me – I just had to make sure to set the correct user/group as well as turn off StrictHostKeyChecking:

根据该提示sadaf2605,这对我来说是最简单的方法-我必须确保设置了正确的用户/组,以及关闭StrictHostKeyChecking:

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

回答by pedrosfdcarneiro

You should try this cookbook https://github.com/poise/application_git. It solves the problem that you mentioned.

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

With this cookbook, you can use application_gitresource, specifiyng the private key:

有了这本食谱,您可以使用application_git资源,指定私钥:

application_git '/srv/myapp' do
  repository '[email protected]:organization/repository'
  deploy_key '/some/path/id_rsa'
end