【问题标题】:Elixir/Phoneix - SFTPClient Connection ErrorElixir/Phoenix - SFTP 客户端连接错误
【发布时间】:2020-10-29 20:34:12
【问题描述】:

我打算将 SFTPClient 库集成到 Dockerized Elixir/Phoenix 后端项目中,用于访问 SFTP 服务器。

使用 FileZilla,它与 OpenSSH 私钥完美配合。

但是使用该库,它不会连接到服务器并返回奇怪的错误消息。

options = %{
      host: "",
      port: ,
      user: "",
      password: "",
      private_key_path: "",
      connect_timeout: 60000,
    }

SFTPClient.connect(options)

错误信息:

protocol: String.Chars
{:badmatch,
 {:error,
  {:asn1,
   {{:invalid_value, 5},
    [
      {:asn1rt_nif, :decode_ber_tlv, 1, [file: 'asn1rt_nif.erl', line: 85]},
      {:"PKCS-FRAME", :decode, 2, [file: 'PKCS-FRAME.erl', line: 155]},
      {:public_key, :der_decode, 2, [file: 'public_key.erl', line: 239]},
      {SFTPClient.KeyProvider, :decode_private_key, 2,
       [file: 'lib/sftp_client/key_provider.ex', line: 44]},
      {:ssh_auth, :get_public_key, 2, [file: 'ssh_auth.erl', line: 145]},
      {:ssh_connection_handler, :is_usable_user_pubkey, 2,
       [file: 'ssh_connection_handler.erl', line: 1793]},
      {:ssh_connection_handler, :"-init_ssh_record/4-lc$^0/1-0-", 2,
       [file: 'ssh_connection_handler.erl', line: 477]},
      {:ssh_connection_handler, :init_ssh_record, 4,
       [file: 'ssh_connection_handler.erl', line: 476]},
      {:ssh_connection_handler, :init, 1, [file: 'ssh_connection_handler.erl', line: 412]},
      {:ssh_connection_handler, :init_connection_handler, 3,
       [file: 'ssh_connection_handler.erl', line: 374]},
      {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}
    ]}}}},
[
  {:public_key, :der_decode, 2, [file: 'public_key.erl', line: 243]},
  {SFTPClient.KeyProvider, :decode_private_key, 2,
   [file: 'lib/sftp_client/key_provider.ex', line: 44]},
  {:ssh_auth, :get_public_key, 2, [file: 'ssh_auth.erl', line: 145]},
  {:ssh_connection_handler, :is_usable_user_pubkey, 2,
   [file: 'ssh_connection_handler.erl', line: 1793]},
  {:ssh_connection_handler, :"-init_ssh_record/4-lc$^0/1-0-", 2,
   [file: 'ssh_connection_handler.erl', line: 477]},
  {:ssh_connection_handler, :init_ssh_record, 4, [file: 'ssh_connection_handler.erl', line: 476]},
  {:ssh_connection_handler, :init, 1, [file: 'ssh_connection_handler.erl', line: 412]},
  {:ssh_connection_handler, :init_connection_handler, 3,
   [file: 'ssh_connection_handler.erl', line: 374]},
  {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}
]

答案: 参考 i22-digitalagentur/sftp_client#11,OpenSSH 密钥无法与当前库一起使用,因此,我使用命令将当前 OpenSSH 密钥转换为 PEM 格式。

ssh-keygen -p -N "" -m pem -f ./key(=keypath)

【问题讨论】:

  • 您是否尝试在没有 docker 的情况下将其部署在 localhost 上?看来您可能缺少一些依赖项,例如 openssl
  • 我在查看库源代码时找到了原因。这是因为,尽管库使用的是 PEM 格式,但密钥文件是 openssh 格式。将密钥文件转换为PEM格式后,效果很好。
  • @EverestClimber - 您还可以包含用于转换密钥的代码 sn-p。
  • @AlekseiMatiushkin 刚刚发布了一个答案,希望对未来的访问者有所帮助。

标签: elixir phoenix


【解决方案1】:

终于找到解决当前 SFTPClient 库问题的方法了。

参考https://github.com/i22-digitalagentur/sftp_client/issues/11,OpenSSH 密钥无法与当前库一起使用,所以,我使用命令将当前 OpenSSH 密钥转换为 PEM 格式。

ssh-keygen -p -N "" -m pem -f ./key(=keypath)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多