Save credentials from runtime

Hello,

I'm completely new to NodeRED development since a few days and I'm facing some difficulties with config and credentials. Probably I still have some problems with understanding the interaction between editor and runtime.

I want to write nodes that interact with a webservice of mine. For this purpose I want to create a kind of generic username in form of a random value (64 digits aplhanumeric) after the installation of the nodes. I want to report this to my webservice and get a pfx as Base64 back that will be used as a client certificate. I would like to save these two values.

I have now reached the point where I think I have understood that the calculation of the username and the registration to the webservice can only be done in the Runtime at the first start of a "normal" node (as a config node has no runtime). I would like to save this in a config node, because these values (mainly the username) are used by several of my nodes.

But I have not succeeded in changing or saving the credentials of a node from the runtime yet. Is this even possible with config/credentials, or do I have to use a different technique? Do I perhaps have to use the httpAdmin function or a own file?

Hope you can help, thanks,
Thorsten

1 Like

Hi Thorsten, welcome to the forum. I think a few people are on holiday which may be why you didn't get a response sooner (I've just returned).

You can indeed store information reasonably securely in Node-RED using credentials from a custom node. In order to do so, you need to have credentials fields defined in your Editor config for the config node (the .html file) and you need to pass them back to the runtime from your node (the .js file). The instructions for doing this are in the docs.

There are several examples of this type of processing in different nodes and it is sometimes easiest to go look at the source code.

You need to:

  1. Set up a configuration node
  2. Configure your custom node to reference the configuration node
  3. In your config node, add the credential fields (I don't think they have to be visible - useful if you are creating the values from your runtime code.
  4. In the definition of your config node, make the API calls and create the credentials - you will need to pass the credentials back to the Editor in the optional parameter of the RED.nodes.registerType function.

This is not correct really. It does have a runtime component. What it can't do is accept msg's.

Here is an example of passing a credential from the .js file back to the runtime:

    /** Register the node by name. This must be called before overriding any of the
     *  Node functions. */
    RED.nodes.registerType(uib.moduleName, nodeGo, {
        credentials: {
            jwtSecret: {type:'password'},
        },
        settings: {
            uibuilderNodeEnv: { value: process.env.NODE_ENV, exportable: true },
        },
    })

The jwtSecret has to be defined in the .html file:

    RED.nodes.registerType(moduleName, {
        category: 'config', // Palette category - specific to config nodes
        defaults: {
            // ...
        },
        credentials: {
            jwtSecret: { type:'password' },  // text or password
        },
        // ...

RED.nodes.registerType takes 2 parameters, the first is the name of the function that is executed when Node-RED starts up. So if you put your credential creation code in there, it will be run every time Node-RED starts

Thanks for this informations! The delay was good, so I searched by myself and learned a lot.

I now using the credentials together with the httpAdmin functions. The UI asks through the httpAdmin, the runtime requests the credentials from my server and passes them back to the UI in the response. The UI inserts this values in the hidden fields.

The only think the user have to do is pressing the save button.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.