2

My user would need to validate his Private Key against his Git Repository . It is similar to "Test Connection" button in any DB Client tool.

I am using JSCH to do this validation in Java (i just need to connect using SSH and tell that connection is successful). Below is my Java code

public class SSHConnect {


    private String filePath = "c:\\me\\ssh-keys\\config_31_jan";


    public static void main(String... args) {
         new SSHConnect().invoke();
    }

    public void invoke () {
        JSch jSch = new JSch();
        try {

            jSch.addIdentity(filePath);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            Session session = jSch.getSession("MY_USER_NAME","github.my_company.com",22) ;
            session.setConfig(config);
            session.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I get the below exception.

c:\me\ssh-keys\config_31_jan
com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:519)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at com.pcfdev.main.SSHConnect.invoke(SSHConnect.java:32)
    at com.pcfdev.main.SSHConnect.main(SSHConnect.java:18)

I referred all other SO Forums and the solution given was to add the Public key in the corresponding server. I did that and am able to succesfully Authenticate using my SSH Command (as mentioned below). But i couldn't achieve the same using JSCH. Please help

WGC1008Q5B8H2 MINGW64 /c/me/ssh-keys
$ ssh -i config_31_jan -T git@github.my_company.com
Hi ARUNK2/spring-cloudconfig! You've successfully authenticated, but GitHub does not provide shell access.
Arun
  • 3,440
  • 11
  • 60
  • 108

1 Answers1

3
 jSch.getSession("MY_USER_NAME","github.my_company.com",22) ;

This should be instead:

 jSch.getSession("git","github.my_company.com",22) ;

You want to open an ssh connection as git, not as you.
Then your public key on the server side will authenticate you as you.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • One more question. Is there a way to add privateKey as a string directly ? rather mentioning it as `FilePath` – Arun Feb 02 '18 at 06:26
  • Adding the *private* directly hard-coded in the code would be a bad practice, so I hope not. – VonC Feb 02 '18 at 06:33
  • I am writing a App that will accept `Private-Key` from a `HTML texbox` and then validate against `git` and upon successful validation given him back some `auto-configruations` JSON File. So i will have to inject the value from the textbox to my `JSCH` code – Arun Feb 02 '18 at 06:38
  • 1
    Can you inject it in a temporary file, whose path you would then inject or use in your code? – VonC Feb 02 '18 at 06:41
  • 1
    @Arun Or at least, if you don't have write access to the user TEMP folder, write it in memory: https://stackoverflow.com/a/22307964/6309 or in a byte stream: https://stackoverflow.com/a/17595282/6309 – VonC Feb 02 '18 at 06:51
  • JSCH had another method of `addIdentity` which accepted privateKey as Bytes `jSch.addIdentity("privateKey",privateKeyBytes,null,null);` – Arun Feb 02 '18 at 12:22