jenkins 如何在运行于其上的Docker中使用主机系统的SSH密钥?

ogq8wdun  于 2023-06-21  发布在  Jenkins
关注(0)|答案(1)|浏览(168)

增编1:感谢您迄今为止的答复,这些答复都很有帮助。不幸的是,我在思想上犯了一个错误。实际问题发生在我在管道中执行RAILS_ENV=test rails webpacker:install或者在此过程中执行yarn install时。这将访问package.jsonpackage.json包含以下行:
"foo-app": "ssh://git@foo.bar.com:7999/pac/foo-app.git"。我想如果我可以克隆仓库,这种访问也会起作用。但事实并非如此,我得到以下错误消息:

Installing all JavaScript dependencies [5.4.4]
         run yarn add @rails/webpacker@5.4.4 from "."
yarn add v1.22.19
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://git@foo.bar.com:7999/pac/foo-bar.git
Directory: /workspace
Output:
Repository not found
The requested repository does not exist or you are not authorised to access it.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
Installing webpack and webpack-cli as direct dependencies
         run yarn add webpack@^4.46.0 webpack-cli@^3.3.12 from "."
yarn add v1.22.19
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
error Command failed.
Exit code: 128

所以我的问题实际上是,我想在安装yarn的过程中访问bb repo,但它不能以这种方式工作。
原题:
我有一个运行Jenkins的服务器。Jenkins服务器的SSH密钥存储在我的Bitbucket存储库中。我可以从Jenkins服务器运行以下命令,并将repo的内容克隆到我的Jenkins服务器:
git clone ssh://git@bb.repo-to-connect-to.com:7999/pac/sample-repo.git
我可以使用以下命令从Jenkins服务器SSH到另一个服务器:ssh -i ~/.ssh/id_rsa test_user@testServer.testhost.host
在Jenkins服务器上,一个Docker容器正在运行,我也想从其中访问BB repo。如果我将id_rsa和id_rsa.pub复制到Docker容器的~/.ssh中,然后使用docker exec -it test_rails_container bin/bash连接到容器并执行-i ~/.ssh/id_rsa test_user@testServer.testhost.host,则Docker容器通过SSH连接到testServer。但是,我不能运行git clone ssh://git@bb.repo-to-connect-to.com:7999/pac/sample-repo.git。当我这样做时,我得到以下错误:

git clone ssh://git@bb.repo-to-connect-to.com:7999/pac/sample-repo.git
Cloning into 'sample-repo'...
git@bb.repo-to-connect-to.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

现在我已经尝试了各种其他的可能性。除此之外,我创建了一个新的SSH密钥,在BB中注册,并尝试克隆存储库(没有成功)。
有谁能告诉我,我在哪里犯了错误,或者我是否在思想上犯了某种错误?

4c8rllxm

4c8rllxm1#

恕我直言,你直截了当的想法是好的,你也许已经发现了配置上的一点差距。让我解释一下我的意思。
当使用git+ssh时,它可能会从~/.ssh/config文件(SSH客户端的用户配置文件)中获取额外的配置细节,而在Docker容器中则不会,因为它与Docker容器不同。
查看工作命令时,可以看到一个配置选项正在运行:

$ ssh -i ~/.ssh/id_rsa test_user@testServer.testhost.host
      ^^^^^^^^^^^^^^^^

它告诉SSH客户端使用哪个密钥。现在,当git执行时,它不会知道该选项,并且(取决于配置,因此只有一个示例,尽管很常见)ssh会咨询其本地配置(例如,~/.ssh/config)查看主机名的某些特定配置是否可用。比如钥匙。
现在,如果我们将其与 Git Internals - Environment Variables¹中的 Miscellaneous 部分进行比较,我们可以找到以下对**GIT_SSH_COMMAND**环境参数的描述,其中包含了我们现在熟悉的内容:
GIT_SSH_COMMAND设置Git尝试连接SSH主机时使用的SSH命令。命令由shell解释,ssh可以使用额外的命令行参数,例如GIT_SSH_COMMAND="ssh -i ~/.ssh/my_key" git clone git@example.com:my/repo
突出显示特定设置

GIT_SSH_COMMAND="ssh -i ~/.ssh/my_key"
                     ^^^^^^^^^^^^^^^^

我们可以看到,这正是告诉SSH客户端密钥文件所在位置的配置。
所以说到Docker,你可以给予一下,在容器环境中设置这个环境参数,然后至少Git应该使用这个文件进行SSH。
TLDR:告诉Git使用哪个SSH密钥,否则它会尝试不使用,然后你(正确地,对吗?)获取访问被拒绝。

***额外注解:***使用GIT_SSH_COMMAND参数只是一种方法。了解问题的原因以及缺少配置的确切部分,然后提供。然后事情只是工作。

一个警告:不要将整个~/.ssh/config文件复制到容器中。这是自找麻烦。这两种情况都是在不理解的情况下实施的,除非你确切地知道你在做什么,否则会带来非常严重的潜在安全影响。这不是容器的SSH配置,而是主机的SSH配置。以正确的方式懒惰。保重

相关问题