william89731 / node-red-contrib-ssh-v3

node module for nodered
Apache License 2.0
9 stars 6 forks source link

Error: Invalid username when using flows.json #5

Closed IndifferentD closed 1 year ago

IndifferentD commented 1 year ago

OS: SUSE Linux Enterprise Server 12 SP4 Docker version 20.10.17-ce, build a89b84221c85 Docker Compose version 2.10.0 Node.js version: v16.16.0 Node-RED version: v3.0.2

I suppose something wrong with passing node options when registering node from flows.json (might be only when using shared docker volume, not sure)

Problem appeared, when i started node-red container with flows.json previously saved from another Node-RED instance:

nodered_skuprk  | 26 Oct 10:12:21 - [info] Starting flows
nodered_skuprk  | 26 Oct 10:12:21 - [info] Started flows
nodered_skuprk  | 26 Oct 10:12:22 - [red] Uncaught Exception:
nodered_skuprk  | 26 Oct 10:12:22 - [error] Error: Invalid username
nodered_skuprk  |     at Client.connect (/usr/src/node-red/node_modules/ssh2/lib/client.js:203:13)
nodered_skuprk  |     at _connectClient (/usr/src/node-red/node_modules/node-red-contrib-ssh-v3/ssh.js:56:15)
nodered_skuprk  |     at runNextTicks (node:internal/process/task_queues:61:5)
nodered_skuprk  |     at processImmediate (node:internal/timers:437:9)
nodered_skuprk  |     at SshV3._inputCallback (/usr/src/node-red/node_modules/node-red-contrib-ssh-v3/ssh.js:139:4)
nodered_skuprk exited with code 1

when trying to find the reason i added console.log(node) in ssh.js:62 and i saw empty credentials in console:

nodered_skuprk  | 26 Oct 11:04:02 - [info] Starting flows
nodered_skuprk  | SshConf {
nodered_skuprk  |   id: 'ac702ab14892a50f',
nodered_skuprk  |   type: 'ssh-conf',
nodered_skuprk  |   z: undefined,
nodered_skuprk  |   g: undefined,
nodered_skuprk  |   _closeCallbacks: [],
nodered_skuprk  |   _inputCallback: null,
nodered_skuprk  |   _inputCallbacks: null,
nodered_skuprk  |   name: 'admin@192.168.1.1',
nodered_skuprk  |   wires: [],
nodered_skuprk  |   _wireCount: 0,
nodered_skuprk  |   send: [Function: NOOP_SEND],
nodered_skuprk  |   credentials: {}
nodered_skuprk  | }
...
nodered_skuprk  | 26 Oct 11:04:02 - [info] Started flows
nodered_skuprk  | 26 Oct 11:04:02 - [red] Uncaught Exception:
nodered_skuprk  | 26 Oct 11:04:02 - [error] Error: Invalid username
nodered_skuprk  |     at Client.connect (/usr/src/node-red/node_modules/ssh2/lib/client.js:203:13)
nodered_skuprk  |     at _connectClient (/usr/src/node-red/node_modules/node-red-contrib-ssh-v3/ssh.js:56:15)
nodered_skuprk  |     at runNextTicks (node:internal/process/task_queues:61:5)
nodered_skuprk  |     at processImmediate (node:internal/timers:437:9)
nodered_skuprk  |     at SshV3._inputCallback (/usr/src/node-red/node_modules/node-red-contrib-ssh-v3/ssh.js:139:4)

but in flows.json there are credentials (tho "hostname","username" and "port" I filled manually since there was none) :

    {
        "id": "ac702ab14892a50f",
        "type": "ssh-conf",
    "hostname": "192.168.1.1",
        "name": "admin@192.168.1.1",
    "port": "22",
    "ssh": "/usr/src/node-red/.ssh/id_rsa",
    "username": "admin",
        "userlabel": "admin@srv1"
    },

I was able to fix this by replacing "node.credentials" const in ssh.js:62 :

        node.options = {
            host: node.credentials.hostname ? node.credentials.hostname : undefined,
            port: node.credentials.port ? node.credentials.port : undefined,
            username: node.credentials.username ? node.credentials.username : undefined,
            password: node.credentials.password ? node.credentials.password : undefined,
            privateKey: n.ssh ? require('fs').readFileSync(n.ssh) : undefined
            // TODO privatekey with passphrase will fail
            // passphrase: "passphraseofprivatekey"
        };

with argument "n" as it used in privateKey option:

        node.options = {
            host: n.hostname ? n.hostname : undefined,
            port: n.port ? n.port : undefined,
            username: n.username ? n.username : undefined,
            password: n.password ? n.password : undefined,
            privateKey: n.ssh ? require('fs').readFileSync(n.ssh) : undefined
            // TODO privatekey with passphrase will fail
            // passphrase: "passphraseofprivatekey"
        };

It is working fine for me now, but breaks credentials display in GUI: image Not sure if it is an issue or i'm doing something wrong, just wanted to notify

UPD: my final hacky fix is :

host: n.hostname ? n.hostname : node.credentials.hostname ? node.credentials.hostname : undefined ,
port: n.port ? n.port : node.credentials.port ? node.credentials.port : undefined ,
username: n.username ? n.username : node.credentials.username ? node.credentials.username : undefined ,
password: n.password ? n.password : node.credentials.password ? node.credentials.password : undefined ,

UPD2 found out that when I use node-red export: "ssh-conf"-nodes only get following options "id", "type" , "ssh", "name", and "userlabel"

        "id": "86708e445e822c5b",
        "type": "ssh-conf",
        "ssh": "/usr/src/node-red/.ssh/id_rsa",
        "name": "SRV1",
        "userlabel": "SRV1"
IndifferentD commented 1 year ago

Sorry, my bad, didn't know about flows_cred.json