gpt4 book ai didi

java - 在 jgit 中配置 known_hosts

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:23 26 4
gpt4 key购买 nike

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

我有以下方法:

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 文件。

建议?我知道来源条目存在于我的 known_hosts 文件中

最佳答案

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 session 有一个 user.home defined ,并且您在其中有一个 %USERPROFILE%/.ssh/known_hosts 文件。

(对于 Windows,user.home 应该由 java 设置为 %USERPROFILE%,也就是说,如果您使用的是 Windows:在某些情况下,this won't always work ) .


现在,如果您确实有一个%USERPROFILE%/.ssh/known_hosts,那么,作为mentioned here

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


在这种情况下,StormeHawkethe 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).

来自 this question , this 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(可能还有任何其他基于 NT 的 Windows 系统)的默认 SYSTEM 主目录只是 C:\
(所以不太理想,但为了快速修复,它是有效的)。

关于java - 在 jgit 中配置 known_hosts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com