Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 526175

Summary: EGit searches for the SSH keys in default dir rather than the one specified in preferences
Product: [Technology] JGit Reporter: Alexander Abdugafarov <fswork90>
Component: JGitAssignee: Thomas Wolf <twolf>
Status: VERIFIED FIXED QA Contact:
Severity: critical    
Priority: P3 CC: fswork90, matthias.sohn, twolf
Version: 4.9   
Target Milestone: 4.9.1   
Hardware: PC   
OS: Windows 8   
See Also: https://git.eclipse.org/r/110345
https://git.eclipse.org/c/jgit/jgit.git/commit/?id=adbf0935e105819f6b8f65325013b6def6205f18
Whiteboard:
Attachments:
Description Flags
Configuration page in Eclipse
none
Error pop-up none

Description Alexander Abdugafarov CLA 2017-10-17 15:37:47 EDT
Created attachment 271048 [details]
Configuration page in Eclipse

Issue appeared after updating from EGit from 4.8.0 to 4.9.0
EGit now searches for SSH keys in default folder (C:\Users\FS\.ssh) rather than one specified by preferences (D:\_Home\.ssh) in branch "General > Network Connections > SSH2"

Full error log:

eclipse.buildId=4.7.0.I20170612-0950
java.version=1.8.0_121
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=ru_RU
Framework arguments:  -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -product org.eclipse.epp.package.java.product -data file:/D:/_Home/eclipse-workspaces/workspace-FS/ -product org.eclipse.epp.package.java.product

org.eclipse.egit.ui
Error
Wed Oct 18 00:28:58 UZT 2017
Transport Error: Cannot get remote repository refs.

org.eclipse.jgit.api.errors.TransportException: git@github.com:frozenspider/fs-common-utils.git: java.io.FileNotFoundException: C:\Users\FS\.ssh\id_rsa_unprotected (Системе не удается найти указанный путь)
	at org.eclipse.jgit.api.LsRemoteCommand.execute(LsRemoteCommand.java:222)
	at org.eclipse.jgit.api.LsRemoteCommand.call(LsRemoteCommand.java:160)
	at org.eclipse.egit.core.op.ListRemoteOperation.run(ListRemoteOperation.java:114)
	at org.eclipse.egit.ui.internal.components.RefSpecPage$3.run(RefSpecPage.java:258)
	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)
Caused by: org.eclipse.jgit.errors.TransportException: git@github.com:frozenspider/fs-common-utils.git: java.io.FileNotFoundException: C:\Users\FS\.ssh\id_rsa_unprotected (Системе не удается найти указанный путь)
	at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:163)
	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.api.LsRemoteCommand.execute(LsRemoteCommand.java:199)
	... 4 more
Caused by: com.jcraft.jsch.JSchException: java.io.FileNotFoundException: C:\Users\FS\.ssh\id_rsa_unprotected (Системе не удается найти указанный путь)
	at com.jcraft.jsch.KeyPair.load(KeyPair.java:543)
	at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
	at com.jcraft.jsch.Session.applyConfig(Session.java:2745)
	at com.jcraft.jsch.Session.<init>(Session.java:166)
	at com.jcraft.jsch.JSch.getSession(JSch.java:274)
	at org.eclipse.jsch.internal.core.Utils.createSession(Utils.java:126)
	at org.eclipse.jsch.internal.core.JSchProvider.createSession(JSchProvider.java:48)
	at org.eclipse.egit.core.EclipseSshSessionFactory.createSession(EclipseSshSessionFactory.java:51)
	at org.eclipse.jgit.transport.JschConfigSessionFactory.createSession(JschConfigSessionFactory.java:179)
	at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:110)
	... 8 more
Caused by: java.io.FileNotFoundException: C:\Users\FS\.ssh\id_rsa_unprotected (Системе не удается найти указанный путь)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileInputStream.<init>(FileInputStream.java:93)
	at com.jcraft.jsch.Util.fromFile(Util.java:508)
	at com.jcraft.jsch.KeyPair.load(KeyPair.java:540)
	... 17 more
Comment 1 Alexander Abdugafarov CLA 2017-10-17 15:38:43 EDT
Created attachment 271049 [details]
Error pop-up
Comment 2 Alexander Abdugafarov CLA 2017-10-17 15:47:27 EDT
In the meantime, workarounds I found:
1) Create a symbolic link from configured folder to default path
2) Use external Git client for pushing/fetching (which kinda voids the benefits of EGit)
Comment 3 Thomas Wolf CLA 2017-10-17 17:37:50 EDT
Duplicate of big 398085?

The only thing that makes me wonder is that you say this issue was new in 4.9.0.

What _is_ new in 4.9.0 is that we taught Jsch (the Java ssh library used by EGit and JGit) to honor ~/.ssh/config more completely. (And bug 398085 means that when the user specifies a different directory in the UI, we'll still read ~/.ssh/config instead of <user-specified directory>/config. But that was like this already before 4.9.0.) I wonder if this problem could be a side-effect of that change... 

Do you have a file C:\Users\FS\.ssh\config, and if so, can you show it in its entirety? Because of https://git.eclipse.org/r/#/c/99067/ it _is_ possible that some IdentityFile settings that were previously ignored now are considered, and if they're incorrect might lead to such exceptions.

I don't see how https://git.eclipse.org/r/#/c/99067/ could otherwise cause this. It doesn't touch the JSch "IdentityRepository" nor the loading of the keys defined in the UI.
Comment 4 Alexander Abdugafarov CLA 2017-10-17 17:41:48 EDT
No, in fact, the folder C:\Users\FS\.ssh itself was non-existent until I manually made a symbolic link pointing to D:\_Home\.ssh as a workaround for this issue.
Comment 5 Thomas Wolf CLA 2017-10-17 17:44:33 EDT
(In reply to Alexander Abdugafarov from comment #4)
> No, in fact, the folder C:\Users\FS\.ssh itself was non-existent until I
> manually made a symbolic link pointing to D:\_Home\.ssh as a workaround for
> this issue.

Aha. And do you have a file D:\Home\.ssh\config, and if so, can you show that?
Comment 6 Alexander Abdugafarov CLA 2017-10-17 17:45:34 EDT
Sure:

Host github.com
  IdentityFile ~/.ssh/id_rsa_unprotected

Host openshift-simplelogics
  Hostname simplelogics-frozenspider.rhcloud.com
  User 54572a90e0b8cd2518000660
  IdentityFile ~/.ssh/id_rsa_unprotected

Host sl
  Hostname simplelogics-frozenspider.rhcloud.com
  User 54572a90e0b8cd2518000660
  IdentityFile ~/.ssh/id_rsa_unprotected
Comment 7 Alexander Abdugafarov CLA 2017-10-17 17:46:38 EDT
I should also add that I have a %HOME% env var pointing to D:\_Home\.ssh
Comment 8 Alexander Abdugafarov CLA 2017-10-17 17:47:45 EDT
Damn, how do I edit the comment?.. I've meant "pointing to D:\_Home\"
Comment 9 Alexander Abdugafarov CLA 2017-10-17 17:58:17 EDT
Just confirmed: reverting to 4.8.0 resolves the issue
Comment 10 Thomas Wolf CLA 2017-10-18 15:21:53 EDT
(In reply to Alexander Abdugafarov from comment #9)
> Just confirmed: reverting to 4.8.0 resolves the issue

This appears to be a Windows-specific problem relating to the way the Java system property "user.home" is set.

In JGit 4.8.0, the Jsch library ignored the .ssh/config file completely; it was parsed and ~ was replaced in JGit only.

In JGit 4.9.0, we still parse .ssh/config correctly and replace ~ in JGit correctly, but we also give that parsed config file to Jsch. Jsch then picks up the IdentityFile again. It finds the original value as parsed with the ~ and then replaces ~ by the Java system property "user.home".

According to the JDK 8 release notes[1] and to JDK bug 6519127[2] "user.home" is on Windows not set to the value of %HOME% but of %USERPROFILE%. So Jsch replaces ~ by your real home directory, and then cannot find the key file and throws this nasty exception.

When JGit (not Jsch) does the ~ replacement on Windows, it considers the following values in this order:

1. %HOME%
2. %HOMEDRIVE%/%HOMEPATH%
3. %HOMESHARE%
4. user.home

So https://git.eclipse.org/r/#/c/99067/ does the replacement correctly as it was in 4.8.0, but since newly Jsch also sees the ~ and does the replacement differently, we get into troubles.

On Linux and Mac, JGit also simply uses "user.home" to replace the tilde, so there's no difference in behavior.

Patch a-coming.

[1] http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html#A999387
[2] http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6519127
Comment 11 Eclipse Genie CLA 2017-10-18 16:46:16 EDT
New Gerrit change created: https://git.eclipse.org/r/110345
Comment 12 Thomas Wolf CLA 2017-10-18 16:48:47 EDT
@Alexander: could you please double-check what the value of the system property user.home in your Eclipse is? It's shown when you go to "About Eclipse", "Installation Details", "Configuration" tab.
Comment 13 Alexander Abdugafarov CLA 2017-10-18 16:56:45 EDT
You are correct:

user.home=C:\Users\FS
Comment 14 Eclipse Genie CLA 2017-10-22 17:50:09 EDT
Gerrit change https://git.eclipse.org/r/110345 was merged to [stable-4.9].
Commit: http://git.eclipse.org/c/jgit/jgit.git/commit/?id=adbf0935e105819f6b8f65325013b6def6205f18
Comment 15 Thomas Wolf CLA 2017-10-26 06:49:05 EDT
@Alexander: the fix should now be available in the EGit/JGit nightly versions (update site: http://download.eclipse.org/egit/updates-nightly/ ).

Would be great if you could verify that this now works as expected.
Comment 16 Alexander Abdugafarov CLA 2017-10-26 07:42:56 EDT
@Thomas: Confirmed, in my case EGit uses the intended folder and not %USERPROFILE%/.ssh
Thanks!
Comment 17 Thomas Wolf CLA 2017-10-26 08:08:44 EDT
Thank you very much for double-checking!