Reputation: 232
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?
Upvotes: 0
Views: 1343
Reputation: 77
I ran into this before when loading keys via ENV and AWS SM
The newline characters are escaped when using single quotes, and so the error is correct there is no start line, just one enourmous line:
irb(main):058> private_key = '-----BEGIN RSA PRIVATE KEY-----\nMnx......'
=> "-----BEGIN RSA PRIVATE KEY-----\\nMnx......"
irb(main):059> puts private_key
-----BEGIN RSA PRIVATE KEY-----\nMnx......
but when you use double quotes:
irb(main):060> private_key = "-----BEGIN RSA PRIVATE KEY-----\nMnx......"
=> "-----BEGIN RSA PRIVATE KEY-----\nMnx......"
irb(main):061> puts private_key
-----BEGIN RSA PRIVATE KEY-----
Mnx......
The newline character stays as a newline. Alternatively, you can do a gsub:
irb(main):056> private_key = '-----BEGIN RSA PRIVATE KEY-----\nMnx......'.gsub('\\n', "\n")
=> "-----BEGIN RSA PRIVATE KEY-----\nMnx......"
irb(main):057> puts private_key
-----BEGIN RSA PRIVATE KEY-----
Mnx......
just make sure the second \n is in double quotes, and you escape the \ in the search string
Upvotes: 0
Reputation: 802
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.
Upvotes: 0