ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

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

Open noamshalev opened this issue 5 years ago • 1 comments

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> [email protected]

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?

noamshalev avatar Dec 05 '19 18:12 noamshalev

See my pull request #808, adding support for SSH certificates and #551 for a duplicate of this issue.

TimWolla avatar Jan 07 '20 17:01 TimWolla