Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Cannot parse privateKey: Unsupported key format #131

Open
dogui1718 opened this issue Apr 25, 2019 · 5 comments
Open

Cannot parse privateKey: Unsupported key format #131

dogui1718 opened this issue Apr 25, 2019 · 5 comments

Comments

@dogui1718
Copy link

I am getting this error when I try to connect with server using a ssh key.

(node:13393) UnhandledPromiseRejectionWarning: Error: Cannot parse privateKey: Unsupported key format
   at Client.connect (/node_modules/ssh2/lib/client.js:146:13)
   at Client.sftp (/node_modules/scp2/lib/client.js:99:7)
   at Client.download (/node_modules/scp2/lib/client.js:289:8)
   at cp2local (/node_modules/scp2/lib/scp.js:84:10)
   at Client.exports.scp (/node_modules/scp2/lib/scp.js:102:5)
   at createUserCertificate (/app/controllers/user-migration.js:203:10)
   at <anonymous>
   at process._tickDomainCallback (internal/process/next_tick.js:228:7)

I am using this key format

-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----

This is my code

client.scp({
    host: '*******************',
    username: '******************',
    privateKey:  '~/.ssh/user',
    path: `***************************`
  }, `*******************************`,
  function(err){
    if (err){
      console.log(err);
    }
  });

Looking for more info about this error, I guess you must to add a new property for ask which ssh key formar are using. Take a look at this documentation:

https://github.com/mscdex/ssh2-streams#utility-methods

@geovanneb
Copy link

This is not a bug. You should use:

client.scp({
host: '*',
username: '
',
privateKey: fs.readFileSync('~/.ssh/user'),
path: ***************************
}, *******************************,
function(err){
if (err){
console.log(err);
}
});

@SergeyMell
Copy link

SergeyMell commented Aug 6, 2019

Thank you @geovanneb, your solution worked for mу. However, instead of ~/.ssh/id_rsa I had to write path like /Users/sergey/.ssh/id_rsa. For some reason fs did not recognize ~ correctly

@AkshayK47
Copy link

I tried using exact path @geovanneb, here is my code

test.scp("***************************", { host: "localhost", username: "********", port: "2223", privateKey: fs.readFileSync('/home/***********/xyz.key'), path: ".", }, (err) => { console.log(err); });

@midnightcodr
Copy link

midnightcodr commented Mar 4, 2020

@SergeyMell, the problem you encountered is not specific to this library, but rather node.js. You can test with this simple code:

const fs = require('fs')
console.log(fs.readFileSync('~/test-file.txt').toString()) // this would throw error

You can use the solution in this SO entry to work around that problem.

@Robin-Sch
Copy link

	scpclient.scp({
		host,
		port,
		username,
		privateKey: require('fs').readFileSync(SSH_KEY_PATH),
		passphrase,
		path,
	}, dest);

same error, console.log(require('fs').readFileSync(SSH_KEY_PATH)) gives a Buffer

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants