Using n8n nodes to generate self signed certificates private and public keys

Hello !

I interested is it possible n8n flow node (for example in function node ) to generate self signed certificates private and public keys using input json values and using management REST API add these new certificates. (solution has management REST API that supports certificate adding ).

Example in openssl utlity to describe my aim generate self signed certificates private and public keys

openssl req -new -newkey rsa:2048 -x509 -sha256 -days 3000 -nodes -out .cert -keyout .key -batch -subj

Lauri-Alo Adamson

Hey @alolauri!

Are you self-hosting n8n? If yes, you can use the Execute Command node. Make sure that you have openssl installed on your server. You may have to rebuild the Docker image to include the openssl package.

Hello !

Thanks for answer.

Is there other possiblities for example using JavaScript Code Snipets with some thing like this Generate a self signed x509 certificate from node.js.

Lauri-Alo Adamosn

1 Like

As long as you are self hosting you can do something similar to the post below. Just use the name of the library you are interested in.

Many Thanks for your answer . i tried to add the library and use ths library inside function node
I instlaled the library:

npm -g install selfsigned

and envnment varibale: variable

export NODE_FUNCTION_ALLOW_EXTERNAL=selfsigned

but then tired use the libraaary i got the error messages:

ReferenceError: selfsigned is not defined
at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1:109
at Object. (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:4:2)
at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:66:31)
at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:424:62
at processTicksAndRejections (internal/process/task_queues.js:95:5)

How can i use this libarary correctly?

Where did you do npm -g install selfsigned? in the dockerfile?

I’m running n8n in virtualmachine under the ubuntu 20.04.
I have installed n8n and selfsigned under ubuntu.

So you are not using docker?

I’m not using docker

Hey @alolauri!

Can you please share how are you using n8n? Are you using PM2?

I’ m not using PM2 . I start n8n from linux desktop in terminal window like this:

administrator@test:~$ n8n
Initializing n8n process

n8n now checks for new versions and security updates. You can turn this off using the environment variable N8N_VERSION_NOTIFICATIONS_ENABLED to “false”
For more information, please refer to Configuration | Docs

n8n ready on 0.0.0.0, port 5678
Version: 0.134.0

Editor is now accessible via:
http://localhost:5678/

Press “o” to open in Browser.

Is it showing in the package.json?

Do yean this package.json file ?
/usr/local/lib/node_modules/n8n/package.json

There are lot of package.json files

Do you mean thatt this export NODE_FUNCTION_ALLOW_EXTERNAL=selfsigned must be in package.json file ? Then it is this case then it is not showing in pacakage.json file.

I think you need to add it to the package file in the path you mentioned and add it to the env variable so you can actually use it.

@RicardoE105 @harshil1712 is this right?

Thanks for your answer . I have question about /usr/local/lib/node_modules/n8n/package.json file what excatly must i add to this file and where ? For example there is section:

“dependencies”: {

},

I added the to this section
selfsigned": “^1.10.11”

and ectivated the the environmet variable NODE_FUNCTION_ALLOW_EXTERNAL=selfsigned
and started n8n . But i got the same error message as past ERROR: selfsigned is not defined

Hey @alolauri,

I suggest you use Docker and build a custom image. This makes it easy to configure n8n with custom settings.

Since you’re using n8n that is globally installed on your machine, you would have to build it again so that it takes your changes into effect. I am not sure how one would go about this with a globally installed npm package.

Hum, I think (but I am not sure) it should work with globally installed packages.

Just to make sure, did you require the package? Like const selfsigned = require('selfsigned');?

Just done a test here and it is working as expected with global packages. My steps were…

sudo npm install n8n -g
sudo npm install selfsigned -g
export NODE_FUNCTION_ALLOW_EXTERNAL=selfsigned
n8n

Then I was presented with the normal console window…

[jonathan@desktop ~]$ n8n
Initializing n8n process

n8n now checks for new versions and security updates. You can turn this off using the environment variable N8N_VERSION_NOTIFICATIONS_ENABLED to "false"
For more information, please refer to https://docs.n8n.io/getting-started/installation/advanced/configuration.html

n8n ready on 0.0.0.0, port 5678
Version: 0.135.1

Editor is now accessible via:
http://localhost:5678/

Press "o" to open in Browser.

I opened the browser and added a function and put in…

var selfsigned = require('selfsigned');
var attrs = [{ name: 'commonName', value: 'contoso.com' }];
var pems = selfsigned.generate(attrs, { days: 365 });
console.log(pems)

return items;

Gave it a run and checked the browser console to see…

It is not a cert I am keeping so more than happy for the private key to be there for the example.

1 Like

Many thanks i tried and it worked , and experimented way to save certs, kys as files also.
Also am looking ways how to use json values to form file name.

Hey @alolauri,

Can you please mark the solution that resolved your issue? This will help other members find the solution easily :slight_smile:

1 Like