【问题标题】:Spring Cloud Config cannot clone private bitbucket repository using ssh keySpring Cloud Config 无法使用 ssh 密钥克隆私有 bitbucket 存储库
【发布时间】:2017-02-19 02:14:10
【问题描述】:

我在 Linux (arch) 上,尝试使用 ssh 密钥在 tutorial 之后配置 Spring Cloud Config,并使用私有 bitbucket git 存储库,但我不断收到错误消息:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is java.lang.IllegalStateException: Cannot
clone or checkout repository] with root cause com.jcraft.jsch.JSchException: Auth fail

现在,根据教程,它应该可以工作了:

如果您不使用 HTTPS 和用户凭据,那么当您将密钥存储在默认目录 (~/.ssh) 并且 uri 指向 SSH 位置(例如“git@github.com:configuration/cloud-configuration”。 ~/.ssh/known_hosts 中的所有密钥都采用“ssh-rsa”格式,这一点很重要。不支持新的“ecdsa-sha2-nistp256”格式。使用 JGit 访问存储库,因此您找到的任何文档都应该适用。 HTTPS 代理设置可以在 ~/.git/config 中设置,也可以通过系统属性(-Dhttps.proxyHost 和 -Dhttps.proxyPort)以与任何其他 JVM 进程相同的方式设置。

我在 ~/.ssh 文件夹中确实有一个名为 bitbucket-rsa 的私有 ssh 密钥,它是使用命令 ssh-keygen -t rsa -b 4096 -C "my-email@provider.com" 创建的。公钥已正确添加到 Bitbucket,因为我可以毫不费力地从命令行从存储库中克隆、拉取和推送。私钥已添加到 ssh-agent 中,bitbucket.org 存在于 known_hosts 文件中。

这是配置服务项目中的 bootstrap.yml:

spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: "git@bitbucket.org:TarekSaid/my-private-repo.git"
server:
  port: 8888

使用带有用户名和密码的 https 有效,但我仍然更喜欢使用 ssh 密钥,我怎样才能使它工作?

【问题讨论】:

    标签: java spring bitbucket ssh-keys spring-cloud-config


    【解决方案1】:

    终于成功了!

    这个问题:How to use a custom ssh key location with Spring Cloud Config 为我指明了正确的方向。我调试了JschConfigSessionFactory 类,发现当没有提供用户名和密码时,它会从~/.ssh/config 中的默认配置文件中获取配置。

    因此,我所要做的就是将以下内容添加到我的 ~/.ssh/config 文件中:

    ~/.ssh/config

    Host bitbucket.org
      User TarekSaid
      Hostname bitbucket.org
      PreferredAuthentications publickey
      IdentitiesOnly yes
      IdentityFile ~/.ssh/bitbucket_rsa
    

    现在它正在工作。

    【讨论】:

    • 很好的解决方案,也对我有用!!!但小注意:rsa 密钥应该在没有密码的情况下生成,否则密码应该在spring: cloud: config: server: git: passphrase: myprivatekeypassword 中指定,而且看起来Host bitbucket.org IdentityFile ~/.ssh/private_rsa 就足够了
    猜你喜欢
    • 2018-10-11
    • 2017-03-25
    • 1970-01-01
    • 2015-11-15
    • 2018-06-14
    • 2019-01-26
    • 2021-02-22
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多