2

I am trying to configure gitlab to run tests upon commiting, howerver in my test i use SFTP ( JSch ) like this:

sftp = new JSch();
sftp.addIdentity(Paths.get(ClassLoader.getSystemResource("private.ppk").toURI()).toString());
//sftp.setKnownHosts("~/.ssh/known_hosts");

Session session = sftp.getSession("test", "localhost", port);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey");
session.connect();

sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();

( port is random port that is free. )

this code, upon runnin in gitlab enviroment throws

com.jcraft.jsch.JSchException: invalid privatekey: [B@27e47833

Which i quite do not understand why, running this code locally works as it should. The private key which i load from resources also in my gitlab repo.

Why is this happening? Are there any extra steps i omited or why is Jsch on gitlab refusing private key that is valid when i run it locally?

Thanks for help!

Darlyn
  • 4,715
  • 12
  • 40
  • 90

1 Answers1

3

As mentioned here:

  • Check the PPK file and ensure that there are no obvious signs of a malformation. IE. Truncations, encoding issues, etc.
    
  • Verify the line endings in the PPK file.
    If you are developing a project and maintaining your code through a source code versioning system it is possible that your code gets versioned by different collaborators using different O.S. and architectures.
    As each operating system handles text end of lines differently (in Linux and OSX is handled as 'LF', in Windows as CRLF), if the PPK file gets modified, the character used to determine EOL could also be modified, thus impacting the matching of the private key. In these cases, it would be important to define the EOL character to use in your source code versioning tool settings or to directly avoid tracking the PPK file modifications.

See the comments of "invalid private key when opening SSH tunnel with jsch":

At least in 0.1.53 (and I doubt this would be removed) it does read PPK (in addition to OpenSSL PEMs = non-newfmt OpenSSH and several others) but only with Windows-type EOL (CR LF).
Did you copy your PPK to the affected system by a method that can change EOLs such as pasting to an editor or FTP TYPE A?

That or use a PEM format instead of PPK.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250