骆驼SFTP组件-SSH私钥URI与privateKeyFile一起使用,不适用于privateKey [英] Camel SFTP component - SSH private key URI works with privateKeyFile, doesn't work with privateKey

查看:174
本文介绍了骆驼SFTP组件-SSH私钥URI与privateKeyFile一起使用,不适用于privateKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条类似下一条的骆驼路线:

I have a camel route that looks like the next one:

from("direct:download")
        .pollEnrich()
        .simple("sftp://my.host:22/folder/?username=foo&fileName=${header.CamelFileName}
        &privateKeyFile=src/main/resources/privateSSHKey")
        .to("file://state/downloaded");

文件src/main/resources/privateSSHKey是RSA私钥.这样做没有问题:Jamel(Camel用于SFTP端点的库)设法连接并下载所需的文件.

The file src/main/resources/privateSSHKey is an RSA private key. That works without a problem : JSCH (library used by Camel for the SFTP endpoint) manages to connect and download the desired file.

在开发时可以进行以前的设置,因为我可以在本地使用密钥创建文件. 但是,对于prod,我们还有其他系统可以在其中获得包含键内容的字节数组.为此,我将路线更改为:

The previous setup is ok while developing, because I can have the file with the key locally. However, for prod, we have other system in which I will be able to get a byte array with the content of the key. For that, I am changing the route to look like this:

from("direct:download")
        .pollEnrich()
        .simple("sftp://my.host:22/folder/?username=foo&fileName=${header.CamelFileName}
     &privateKey=" + URLEncoder.encode(new String(sshPrivateKey), "UTF-8"))
        .to("file://state/downloaded");

...成为sshPrivateKey的字节数组.不幸的是,我总是从JSCH获得"auth_cancel",并且进行调试后,我发现在尝试与SFTP服务器握手时会发生这种情况.

...being sshPrivateKey the byte array. Unfortunately, I always get "auth_cancel" from JSCH, and debugging I can see that this happens when trying to handshake with the SFTP server.

我错过了什么吗?我很确定编码sshPrivateKey byte []是正确的方法(如果我不这样做,JSCH会抱怨错误的密钥),但是我不确定我还缺少什么?

Am I missing something? I am pretty sure that encoding the sshPrivateKey byte[] is the way to go (JSCH was complaining about wrong key if I didn't do it), but I am not sure about what else I am missing?

推荐答案

问题的根源是编码,URLEncoding,byteString可能会丢失某些字符,例如+\\. 我设法使其与privateKey方法Java DSL的Byte[]参数一起使用.

The origin of the problem is the encoding, the URLEncoding, byte and String can loose some character like + or \\. I managed to make it work with Byte[] parameter for privateKey method Java DSL.

示例:

String privateKeyString = Files.readString(Path.of("/.ssh/private_key_rsa"), StandardCharsets.UTF_8);
Byte[] privateKeyAsByteArray = ArrayUtils.toObject(privateKeyString.getBytes());

from("file://tmp")
.to(sftp("localhost")
        .username("test")
        .privateKey(privateKeyAsByteArray));

这篇关于骆驼SFTP组件-SSH私钥URI与privateKeyFile一起使用,不适用于privateKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆