在jgit配置的known_hosts(Configuring known_hosts in jgi

2019-08-19 23:11发布

使用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)

我读了这个问题的方法,似乎它没有找到我的known_hosts文件user_home/.git 。 不过,我一直在寻找一个小时,我没有找到一个方法来配置JGit告诉JSch到哪里寻找known_hosts文件。

建议? 我知道原点的条目存在于我的known_hosts文件

Answer 1:

这个答案提到:

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

但是,你正在使用jgit,而不是 jsch(在Java安全壳)直接,所以让我们来看看:

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定义 ,和你有一个%USERPROFILE%/.ssh/known_hosts在里面的文件。

user.home应该由Java来进行设置%USERPROFILE%为Windows,也就是说,如果你是在Windows上:在某些情况下, 这将不总是有效 )。


现在,如果有一个%USERPROFILE%/.ssh/known_hosts ,那么,作为这里所说的

只是SSH客户端(使用命令行ssh工具),这将增加进入您~/.ssh/known_hosts file


在这种情况下, StormeHawke提到了评论 :

因为我在Tomcat中运行此作为Windows服务 ,Jsch(通过扩展JGit)一直在寻找不在我的用户文件夹,但在SYSTEM帐户的主文件夹 .ssh文件夹。
在这种情况下,我继续刚才复制.ssh文件夹进入SYSTEM主文件夹,因为Tomcat的只有我的机器上运行,用于开发和测试目的(可能不是最好的安全策略,但风险是最小的在这种情况下)。

从这个问题 , 这一次 ,该目录为LocalSystem帐户应该是:

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

该OP提到:

根据这一呼吁:

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

默认的SYSTEM为Windows7的(大概任何其他基于Windows NT的系统)的主目录仅仅是C:\
(所以不是很理想,但对于一个快速解决,它的工作原理)。



文章来源: Configuring known_hosts in jgit