MQTT node: Add SSL/TLS support (#1828)

* MQTT node: Add SSL/TLS support

* Add import IDisplayOptions

* Remove  as NodePropertyTypes
This commit is contained in:
Frederic Alix
2021-09-03 14:37:19 +02:00
committed by GitHub
parent e928fb182b
commit 7b752ce492
3 changed files with 166 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
import { import {
ICredentialType, ICredentialType,
IDisplayOptions,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@@ -69,5 +70,99 @@ export class Mqtt implements ICredentialType {
default: '', default: '',
description: 'Client ID. If left empty, one is autogenrated for you', description: 'Client ID. If left empty, one is autogenrated for you',
}, },
{
displayName: 'SSL',
name: 'ssl',
type: 'boolean',
default: false,
},
{
displayName: 'Passwordless',
name: 'passwordless',
type: 'boolean',
displayOptions: {
show: {
ssl: [
true,
],
},
},
default: true,
description: 'Passwordless connection with certificates (SASL mechanism EXTERNAL)',
},
{
displayName: 'CA Certificates',
name: 'ca',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
ssl: [
true,
],
},
},
default: '',
description: 'SSL CA Certificates to use.',
},
{
displayName: 'Reject Unauthorized Certificate',
name: 'rejectUnauthorized',
type: 'boolean',
displayOptions: {
show: {
ssl: [
true,
],
passwordless: [
true,
],
},
} as IDisplayOptions,
default: '',
description: 'Validate Certificate.',
},
{
displayName: 'Client Certificate',
name: 'cert',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
ssl: [
true,
],
passwordless: [
true,
],
},
} as IDisplayOptions,
default: '',
description: 'SSL Client Certificate to use.',
},
{
displayName: 'Client Key',
name: 'key',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
ssl: [
true,
],
passwordless: [
true,
],
},
},
default: '',
description: 'SSL Client Key to use.',
},
]; ];
} }

View File

@@ -119,7 +119,15 @@ export class Mqtt implements INodeType {
const port = credentials.port as number || 1883; const port = credentials.port as number || 1883;
const clientId = credentials.clientId as string || `mqttjs_${Math.random().toString(16).substr(2, 8)}`; const clientId = credentials.clientId as string || `mqttjs_${Math.random().toString(16).substr(2, 8)}`;
const clean = credentials.clean as boolean; const clean = credentials.clean as boolean;
const ssl = credentials.ssl as boolean;
const ca = credentials.ca as string;
const cert = credentials.cert as string;
const key = credentials.key as string;
const rejectUnauthorized = credentials.rejectUnauthorized as boolean;
let client: mqtt.MqttClient;
if (ssl === false) {
const clientOptions: IClientOptions = { const clientOptions: IClientOptions = {
port, port,
clean, clean,
@@ -131,7 +139,26 @@ export class Mqtt implements INodeType {
clientOptions.password = credentials.password as string; clientOptions.password = credentials.password as string;
} }
const client = mqtt.connect(brokerUrl, clientOptions); client = mqtt.connect(brokerUrl, clientOptions);
}
else {
const clientOptions: IClientOptions = {
port,
clean,
clientId,
ca,
cert,
key,
rejectUnauthorized,
};
if (credentials.username && credentials.password) {
clientOptions.username = credentials.username as string;
clientOptions.password = credentials.password as string;
}
client = mqtt.connect(brokerUrl, clientOptions);
}
const sendInputData = this.getNodeParameter('sendInputData', 0) as boolean; const sendInputData = this.getNodeParameter('sendInputData', 0) as boolean;
// tslint:disable-next-line: no-any // tslint:disable-next-line: no-any

View File

@@ -102,7 +102,15 @@ export class MqttTrigger implements INodeType {
const port = credentials.port as number || 1883; const port = credentials.port as number || 1883;
const clientId = credentials.clientId as string || `mqttjs_${Math.random().toString(16).substr(2, 8)}`; const clientId = credentials.clientId as string || `mqttjs_${Math.random().toString(16).substr(2, 8)}`;
const clean = credentials.clean as boolean; const clean = credentials.clean as boolean;
const ssl = credentials.ssl as boolean;
const ca = credentials.ca as string;
const cert = credentials.cert as string;
const key = credentials.key as string;
const rejectUnauthorized = credentials.rejectUnauthorized as boolean;
let client: mqtt.MqttClient;
if (ssl === false) {
const clientOptions: IClientOptions = { const clientOptions: IClientOptions = {
port, port,
clean, clean,
@@ -114,7 +122,25 @@ export class MqttTrigger implements INodeType {
clientOptions.password = credentials.password as string; clientOptions.password = credentials.password as string;
} }
const client = mqtt.connect(brokerUrl, clientOptions); client = mqtt.connect(brokerUrl, clientOptions);
}
else {
const clientOptions: IClientOptions = {
port,
clean,
clientId,
ca,
cert,
key,
rejectUnauthorized,
};
if (credentials.username && credentials.password) {
clientOptions.username = credentials.username as string;
clientOptions.password = credentials.password as string;
}
client = mqtt.connect(brokerUrl, clientOptions);
}
const self = this; const self = this;