node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

privateKey: Buffer.from('...')

Open YanDevDe opened this issue 3 years ago • 4 comments

README.md shows privateKey: Buffer.from('...'), but I assume the privateKey expects only string, or not? Would fix this typo to avoid confusion as setting Buffer in privateKey actually won't work.

YanDevDe avatar Sep 24 '22 01:09 YanDevDe

@YanDevDe facing the same issue .. what did work for you ?

eyal-solomon1 avatar Sep 28 '22 11:09 eyal-solomon1

Convert the Buffer to string:

privateKey: buffer.toString(“utf8”)

Make sure that the key is in PEM format (if not, you may need to convert it)

YanDevDe avatar Sep 28 '22 14:09 YanDevDe

Would think that reading the file as UTF-8 would work, but I get:

const privateKey = await fs.readFile(env.SSH_privateKey, 'utf-8');
// Error: ENOENT: no such file or directory, stat '-----BEGIN OPENSSH PRIVATE KEY----- …

I also get errors when I try to use the privateKeyPath approach:

const privateKeyPath = '/myPathTo/id_ed25519';
// TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

So not figured out how to get this to work, and had to downgrade to 12.05 for now.

EDIT: Turns out my first approach worked, also what steelbrain suggested bellow, but I had some error checks in my own client that was the issue.

ecker00 avatar Dec 20 '22 14:12 ecker00

@ecker00 I think you're trying to read the private key (which is already the key itself, not a path) as a path from the filesystem.

In your case, you may be able to get away with just replacing with the following code:

const privateKey = Buffer.from(env.SSH_privateKey, 'utf-8')

steelbrain avatar Dec 20 '22 14:12 steelbrain