在jgit中配置known_hosts [英] Configuring known_hosts in jgit

查看:1131
本文介绍了在jgit中配置known_hosts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



$ b

此方法在 pull.call() 方法调用,但有以下例外:

  com.jcraft.jsch.JSchException:UnknownHostKey:www .somehost.com。 RSA密钥指纹是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.Transport.fetch(Transport.java:1104)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
。 eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128)
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:245)
at net.intellidata.dpu。 controller.schema.EntityMapp ingController.commitToGitRepository(EntityMappingController.java:149)
...(截断符合我的代码的位置)

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



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

解决方案

这个答案提到:

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

但是您使用的是jgit,而不是 jsch(Java安全shell),所以我们来看看:

  C:\ Users \VonC\prog\git> git clone https://github.com/eclipse/jgit 
克隆到'jgit'中...
remote:计数对象:37854,完成。
remote:压缩对象:100%(7743/7743),完成。
remote:共计37854(delta 22009),重用34367(delta 18831)
接收对象:100%(37854/37854),6.73 MiB | 1.37 MiB / s,完成。
解决三角洲问题:完成100%(22009/22009)。

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

C:\ Users \VonC\prog\git\jgit> ; grep -nrHIsetKnownHosts*
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 基于系统。 getProperty(user.home)



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



user.home 应该是由java设置为Windows的%USERPROFILE%,也就是说,如果你在Windows上:在某些情况下,这不会总是起作用的)






现在,如果您的 具有%USERPROFILE%/。ssh / known_hosts ,那么,如提及在这里


只需SSH到客户端(使用命令行 ssh tool),这会将条目添加到〜/ .ssh / known_hosts文件中。






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


因为我在 Tomcat中将其作为Windows服务运行,Jsch(和通过扩展JGit)不在我的用户文件夹中,而是在 SYSTEM 帐户的主目录中为 .ssh code>文件夹。

在这种情况下,我继续将 .ssh 文件夹复制到 SYSTEM home文件夹,因为Tomcat只能在我的机器上运行,用于开发和测试目的(可能不是最好的安全策略,但在这种情况下风险极小)。

这个问题这一个,该目录为 LocalSystem Account 应该是:

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

该OP提到:

据此调用:

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

默认系统主目录对于Windows7(大概是任何其他基于NT的Windows系统)简单地是 C:\

(所以不理想,但为快速修复,它的工作原理)。


Using jgit with gitolite for source control, I have an application that generates certain code on command and which we want to be committed to source control. The goal is to pull with a fast forward, commit the new code, and then push it.

I have the following method:

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();
}

This method fails on the pull.call() method call, with the following exception:

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)

The way I read this, it seems that it's not finding my known_hosts file in user_home/.git. However, I've been searching for an hour and I'm not finding a way to configure JGit to tell JSch where to look for the known_hosts file.

Suggestions? I know the entry for the origin is present in my known_hosts file

解决方案

This answer mentions:

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

But you are using jgit, and not jsch (the Java secure shell) directly, so let's see:

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);

Found it!

This comes from JschConfigSessionFactory.java#knownHosts(), and looks like:

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

userHome is based on System.getProperty("user.home").

So make sure your java session has a user.home defined, and that you have a %USERPROFILE%/.ssh/known_hosts file in there.

(user.home should be set by java to %USERPROFILE% for Windows, that is, if you are on Windows: in some case, this won't always work).


Now if you do have a %USERPROFILE%/.ssh/known_hosts, then, as mentioned here

Just SSH to the client (using command-line ssh tool), this will add entry to your ~/.ssh/known_hosts file.


In this case, the StormeHawke mentions in the comments:

since I'm running this in Tomcat as a windows service, Jsch (and by extension JGit) was looking not in my user folder but in the SYSTEM account's home folder for the .ssh folder.
In this case I went ahead and just copied the .ssh folder into the SYSTEM home folder since Tomcat only runs on my machine for development and testing purposes (Probably not the best security policy but the risk is minimal in this case).

From this question, this one, that directory for the LocalSystem Account should be:

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

The OP mentions:

According to this call:

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

the default SYSTEM home directory for Windows7 (and presumably any other NT-based Windows system) is simply C:\.
(so not ideal, but for a quick fix, it works).

这篇关于在jgit中配置known_hosts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆