USERAUTH使用JGit无法安全地访问PullCommand() [英] USERAUTH fail using JGit to access git repo securely for PullCommand()
问题描述
public class gitHubTest {
我试图用JGit的API做下面的代码: JSch jsch = new JSch();
//明确定义私钥文件位置
String userHome = System.getProperty(user.home);
String privateKey = userHome +/.ssh/id_rsa;
字符串knownHostsFile = userHome +/.ssh/known_hosts;
存储库localRepo =新的FileRepository(/ LocalPath / .git);
public gitHubTest()throws Exception {
jsch.setConfig(StrictHostKeyChecking,no);
jsch.setKnownHosts(knownHostsFile);
jsch.addIdentity(privateKey);
System.out.println(privateKey:+ privateKey);
Git git = new Git(localRepo);
PullCommand pullcmd = git.pull();
pullcmd.call();
错误堆栈跟踪:
org.eclipse.jgit.api.errors.TransportException:git@github.example.com:remote.git:USERAUTH在组织中失败
。 eclipse.jgit.api.FetchCommand.call(FetchCommand.java:245)
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:288)
at gitHubTest。< init> (gitHubTest.java:47)
在WebhooksServer.main(WebhooksServer.java:13)
引起:org.eclipse.jgit.errors.TransportException:git@github.example.com:remote.git :USERAUTH失败
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:160)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:137)
at org.eclipse.jgit.transport.TransportGitSsh $ SshFetchConnection。< init>(TransportGitSsh.java:274)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:169)
在org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProce在org.eclipse.jgit.transport.FetchProcess.execute上的
(FetchProcess.java:122)在org.eclipse.jgit.transport.Transport.fetch上的
(Transport.java: 1236)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:234)
... 3 more
引起:com.jcraft.jsch .JSchException:USERAUTH失败
at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
at com.jcraft.jsch.Session.connect(Session.java:470)
在org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:117)
... 10 more
我检查过的一些建议显示,我们需要实例化JschConfigSessionFactory,然后重写configure()方法来传递密码。我已经尝试过了。然后它显示一个错误。我提到了 http://www.codeaffine.com/2014/12 / 09 / jgit-authentication / ,它只读,但不是我的PullCommand。
任何人都可以请帮忙吗?
使用configure()方法实现代码:
public class gitHubTest {
JSch jsch = new JSch();
String userHome = System.getProperty(user.home);
String privateKey = userHome +/.ssh/id_rsa;
字符串knownHostsFile = userHome +/.ssh/known_hosts;
public gitHubTest()抛出IOException,JSchException,GitAPIException {
存储库localRepo = new FileRepository(/ LocalPath / branch.git);
final String remoteURL =git@github.example.com:remote.git;
JSch.setConfig(StrictHostKeyChecking,no);
jsch.setKnownHosts(knownHostsFile);
jsch.addIdentity(privateKey);
JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory(){
@Override
protected void configure(OpenSshConfig.Host host,Session session){
CredentialsProvider cp = new CredentialsProvider(){
@Override
public boolean isInteractive(){
return false;
}
@Override
public boolean supports(CredentialItem ... credentialItems){
return false;
}
@Override
public boolean get(URIish urIish,CredentialItem ... credentialItems)throws UnsupportedCredentialItem {
return false;
}
};
UserInfo userInfo = new CredentialsProviderUserInfo(session,cp);
session.setUserInfo(userInfo);
}
};
SshSessionFactory.setInstance(sessionFactory);
Git git = new Git(localRepo);
PullCommand pullcmd = git.pull();
pullcmd.call();
}}
这给出了同样的错误。
我可以弄清楚我正面临的一些问题。这是它的解决方案:
-
如果auth必须在没有密码的情况下完成,那么不需要密码就会生成id_rsa
-
我们需要在配置SshSessionfactory时使用addIdentity来覆盖
getJSch(final OpenSshConfig.Host hc,FS fs)
:
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory(){
@Override
protected void configure(OpenSshConfig.Host主持人,Session会话){
session.setConfig(StrictHostKeyChecking,no);
$ b @Override
protected JSch getJSch(final OpenSshConfig.Host hc,FS fs)throws JSchException {
JSch jsch = super.getJSch(hc,fs) ;
jsch.removeAllIdentity();
jsch.addIdentity(/ path / to / private / key);
返回jsch;
}
};
-
我们需要调用需要以不同的方式实例化的代码:
PullCommand pull = git.pull()。setTransportConfigCallback(new TransportConfigCallback(){
@Override
public void configure运输运输){
SshTransport sshTransport =(SshTransport)transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
});
然后调用拉实例:
PullResult pullResult = pull.call();
我希望这有助于您。
I am trying to do a git pull using JGit's API with the following code
public class gitHubTest {
JSch jsch = new JSch();
// Defining the private key file location explicitly
String userHome = System.getProperty("user.home");
String privateKey = userHome + "/.ssh/id_rsa";
String knownHostsFile = userHome + "/.ssh/known_hosts";
Repository localRepo = new FileRepository("/LocalPath/.git");
public gitHubTest() throws Exception {
jsch.setConfig("StrictHostKeyChecking", "no");
jsch.setKnownHosts(knownHostsFile);
jsch.addIdentity(privateKey);
System.out.println("privateKey :" + privateKey);
Git git = new Git(localRepo);
PullCommand pullcmd = git.pull();
pullcmd.call();
}
}
Error Stack Trace :
org.eclipse.jgit.api.errors.TransportException: git@github.example.com:remote.git: USERAUTH fail
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:245)
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:288)
at gitHubTest.<init>(gitHubTest.java:47)
at WebhooksServer.main(WebhooksServer.java:13)
Caused by: org.eclipse.jgit.errors.TransportException: git@github.example.com:remote.git: USERAUTH fail
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:160)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:137)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:274)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:169)
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:1236)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:234)
... 3 more
Caused by: com.jcraft.jsch.JSchException: USERAUTH fail
at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
at com.jcraft.jsch.Session.connect(Session.java:470)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:117)
... 10 more
Some suggestions I have checked show, we need to instantiate JschConfigSessionFactory and then overrride configure() method to pass passphrase . I have tried doing it already. Then it shows an error. I have referred to http://www.codeaffine.com/2014/12/09/jgit-authentication/ which reads just right but not for my PullCommand.
Can anyone please help? I have already read and tried a lot of posts here but nothing addresses my problem accurately.
Code implemenatation with configure() method :
public class gitHubTest {
JSch jsch = new JSch();
String userHome = System.getProperty("user.home");
String privateKey = userHome + "/.ssh/id_rsa";
String knownHostsFile = userHome + "/.ssh/known_hosts";
public gitHubTest() throws IOException, JSchException, GitAPIException {
Repository localRepo = new FileRepository("/LocalPath/branch.git");
final String remoteURL = "git@github.example.com:remote.git";
JSch.setConfig("StrictHostKeyChecking", "no");
jsch.setKnownHosts(knownHostsFile);
jsch.addIdentity(privateKey);
JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
CredentialsProvider cp = new CredentialsProvider() {
@Override
public boolean isInteractive() {
return false;
}
@Override
public boolean supports(CredentialItem... credentialItems) {
return false;
}
@Override
public boolean get(URIish urIish, CredentialItem... credentialItems) throws UnsupportedCredentialItem {
return false;
}
};
UserInfo userInfo = new CredentialsProviderUserInfo(session,cp);
session.setUserInfo(userInfo);
}
};
SshSessionFactory.setInstance(sessionFactory);
Git git = new Git(localRepo);
PullCommand pullcmd = git.pull();
pullcmd.call();
}}
this gives the same error.
I could figure out some of the issues I was facing. Here's solution to it:
if auth has to be done without the passphrase, generate the id_rsa without passphrase
We needed to override
getJSch(final OpenSshConfig.Host hc, FS fs)
making use of addIdentity in the configure of SshSessionfactory:SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session sess ion) { session.setConfig("StrictHostKeyChecking", "no"); } @Override protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException { JSch jsch = super.getJSch(hc, fs); jsch.removeAllIdentity(); jsch.addIdentity("/path/to/private/key"); return jsch; } };
We need to call needs to be instantiated differently:
PullCommand pull = git.pull().setTransportConfigCallback(new TransportConfigCallback() { @Override public void configure(Transport transport) { SshTransport sshTransport = (SshTransport) transport; sshTransport.setSshSessionFactory(sshSessionFactory); } });
And then call pull instance:
PullResult pullResult = pull.call();
I hope this helps.
这篇关于USERAUTH使用JGit无法安全地访问PullCommand()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!