在jgit中配置known_hosts

时间:2013-04-05 20:55:50

标签: java git ssh jsch jgit

使用jgit和gitolite进行源代码控制,我有一个应用程序可以在命令中生成某些代码,我们希望将其提交给源代码控制。目标是快速前进,提交新代码,然后推送它。

我有以下方法:

private void commitToGitRepository(String updateComment, Config config)
      throws IOException, NoFilepatternException, GitAPIException
{
   if(git == null)
   {
      git = Git.open(new File(config.getDpuCheckoutDir()));
   }
   PullCommand pull = git.pull();
   pull.call();
}

此方法在pull.call()方法调用失败,但有以下异常:

com.jcraft.jsch.JSchException: UnknownHostKey: www.somehost.com. RSA key fingerprint is 9d:92:a9:c5:5d:cb:5f:dc:57:ff:38:7e:34:31:fe:75
at com.jcraft.jsch.Session.checkHost(Session.java:748)
at com.jcraft.jsch.Session.connect(Session.java:319)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:121)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:248)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:147)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1104)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128)
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:245)
at net.intellidata.dpu.controller.schema.EntityMappingController.commitToGitRepository(EntityMappingController.java:149)
... (truncated where it meets my code)

我读这篇文章的方式似乎并没有在user_home/.git中找到我的known_hosts文件。但是,我一直在寻找一个小时,我没有找到一种方法来配置JGit告诉JSch在哪里查找known_hosts文件。

连连呢?我知道origin的条目存在于我的known_hosts文件中

1 个答案:

答案 0 :(得分:8)

This answer提及:

jsch.setKnownHosts("C:\\Users\\aUsername\\known_hosts");

但你直接使用jgit和 jsch (the Java secure shell),所以让我们看看:

C:\Users\VonC\prog\git>git clone https://github.com/eclipse/jgit
Cloning into 'jgit'...
remote: Counting objects: 37854, done.
remote: Compressing objects: 100% (7743/7743), done.
remote: Total 37854 (delta 22009), reused 34367 (delta 18831)
Receiving objects: 100% (37854/37854), 6.73 MiB | 1.37 MiB/s, done.
Resolving deltas: 100% (22009/22009), done.

C:\Users\VonC\prog\git>cd jgit

C:\Users\VonC\prog\git\jgit>grep -nrHI "setKnownHosts" *
org.eclipse.jgit/src/org/eclipse/jgit/transport/JschConfigSessionFactory.java:262:                              sch.setKnownHosts(in);

发现它!

这来自JschConfigSessionFactory.java#knownHosts(),看起来像是:

new File(new File(home, ".ssh"), "known_hosts");
# with:
home = fs.userHome();

userHome基于System.getProperty("user.home")

因此,请确保您的java会话有一个user.home defined,并且您有一个%USERPROFILE%/.ssh/known_hosts文件。

(对于Windows,user.home应该由java设置为%USERPROFILE%,也就是说,如果你在Windows上:this won't always work}。


现在,如果你%USERPROFILE%/.ssh/known_hosts,那么,mentioned here

  

只需通过SSH连接到客户端(使用命令行ssh工具),这会向~/.ssh/known_hosts file添加条目。


在这种情况下,StormeHawke中提及the comments

  

因为我在 Tomcat中作为Windows服务运行,所以Jsch(以及扩展名为JGit)不在我的用户文件夹中,而是在 SYSTEM帐户的主页中.ssh文件夹的文件夹   在这种情况下,我继续将.ssh文件夹复制到SYSTEM主文件夹中,因为Tomcat仅在我的机器上运行以进行开发和测试(可能不是最好的安全策略,但风险很小)这种情况)。

this questionthis one开始,LocalSystem Account的目录应为:

C:\Documents and Settings\Default User
# or Wind7 / 2008
C:\Windows\System32\Config\systemprofile

OP提到:

根据这个电话:

 System.out.println(System.getProperty("user.home")); 

Windows7的默认SYSTEM主目录(可能是任何其他基于NT的Windows系统)只是C:\
(所以不太理想,但为了快速修复,它可以工作)。