mscdex / mscdex/ssh2

How to connect with a key signed by a certificate authority?

Open
#858 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
5.8k
Forks
734
PR merge metrics
No merged PRs in 30d

Description

I need my node.js app to connect to a target machine using a key signed by a certificate authority.

When connecting from a terminal, the following 3 commands work:
eval "$(ssh-agent -s)"
ssh-add
ssh -i <path to signed key> ubuntu@10.0.0.21

In order to do it from my node.js app, I first start the ssh-agent and add the identity:
eval "$(ssh-agent -s)"
ssh-add
and launch the following node.js app with the environment variables of the ssh agent:
SSH_AUTH_SOCK=<socket> SSH_AGENT_PID=<pid> node app.js

This is the code I'm using to connect:

    conn = new SSHClient();
    conn.on('ready', function() {
        socket.emit('data', 'Connection to ' + asset.ip + ' established\n');
        conn.shell(function(err, stream) {
            if (err)
                return socket.emit('data', 'Connection to ' + asset.ip + ' shell error: ' + err.message + ' \n');  
            socket.on('data', function(data) {  
                stream.write(data);  
            });  
            stream.on('data', function(d) {  
                socket.emit('data', d.toString('binary'));  
            }).on('close', function() {  
                conn.end();  
            });  
        });
    }).on('close', function() {
        socket.emit('data', 'Connection to ' + asset.ip + ' closed.\n');
    }).on('error', function(err) {
        socket.emit('data', 'Connection to ' + asset.ip + ' ERROR: ' + err.message + '\n');
    }).connect({
        host: asset.ip,
        port: 22,
        username: asset.login,
        privateKey: require('fs').readFileSync('<path to signed key>'),
        agent: process.env.SSH_AUTH_SOCK
    });

When trying to connect I get the error:

privateKey value does not contain a (valid) private key

I struggled with the code, tried it with few configurations and couldn't find the right way to make it work.
Any idea how I should start this connection?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the connection setup in app.js, especially the privateKey and agent options passed to SSHClient.connect, and compare them with the working ssh-agent commands and the reported invalid-private-key error. Check the ssh2 connection documentation and determine whether the supplied signed key and agent configuration are supported; done means documenting or reproducing a valid connection path.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.