How to use PRIVATE RSA key file in Net::sftp in ruby on rails

1.1k Views Asked by At

I am trying to establish a sftp connection to my azure storage. I have a private key file which starts with

private_key = '-----BEGIN RSA PRIVATE KEY-----\nMnx......'

With this private_key, I try to establish the connection like

Net::SFTP.start('<remote-ip>', 'username', 
        :password => 'password', 
        :key_data => [ private_key ],
        keys_only: true,
        verify_host_key: :never) do |sftp|
          data = sftp.download!("/somefile.txt")
          p data

end

When I try to use this key file, I am getting an exception like

#<OpenSSL::PKey::PKeyError: Could not parse PKey: no start line>

However when I was using a key file which starts with below format

-----BEGIN OPENSSH PRIVATE KEY-----

everything was working fine.

But I want to use the new private key. What will be wrong in this code? Is there a way to specify the type of private key file to be used?

1

There are 1 best solutions below

0
ARK On

do this:

Before this action call, check if private_key variable contains -----BEGIN OPENSSH PRIVATE KEY----- line. If not, then prepend this to the private_key variable or in short bring private_key to the format which your start method is accepting.