在gitlab上测试jsch导致invalid privatekey错误
Testing jsch on gitlab results in invalid privatekey error
我正在尝试将 gitlab 配置为在提交时进行 运行 测试,但是在我的测试中我使用 SFTP ( JSch ) 是这样的:
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
是空闲的随机端口。)
这段代码,在 gitlab 环境中 运行nin 抛出
com.jcraft.jsch.JSchException: invalid privatekey: [B@27e47833
我完全不明白为什么,运行将这段代码在本地正常工作。我从资源中加载的私钥也在我的 gitlab 存储库中。
为什么会这样?是否有任何我省略的额外步骤,或者为什么 gitlab 上的 Jsch 拒绝在我 运行 在本地有效时有效的私钥?
感谢帮助!
-
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.
参见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?
或者使用 PEM 格式而不是 PPK。
我正在尝试将 gitlab 配置为在提交时进行 运行 测试,但是在我的测试中我使用 SFTP ( JSch ) 是这样的:
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
是空闲的随机端口。)
这段代码,在 gitlab 环境中 运行nin 抛出
com.jcraft.jsch.JSchException: invalid privatekey: [B@27e47833
我完全不明白为什么,运行将这段代码在本地正常工作。我从资源中加载的私钥也在我的 gitlab 存储库中。
为什么会这样?是否有任何我省略的额外步骤,或者为什么 gitlab 上的 Jsch 拒绝在我 运行 在本地有效时有效的私钥?
感谢帮助!
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 asCRLF
), 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.
参见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?
或者使用 PEM 格式而不是 PPK。