Skip to content

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

@noamshalev

Description

@noamshalev

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions