From 2bde07561fc9ded6b7e0f196d4e0b72d05b106d9 Mon Sep 17 00:00:00 2001 From: mblackstock Date: Mon, 27 Feb 2017 13:04:19 -0800 Subject: [PATCH 1/2] UI to upload certificates and keys for TLS, and send them to node red in configuration properties to store them in credentials file by default upload buttons will be shown unless a cert or key path is already set added new settings flag called 'tlsDisableLocalFiles' to disable UI for local paths for cloud hosted NR --- nodes/core/io/05-tls.html | 105 ++++++++++++++++++++++++- nodes/core/io/05-tls.js | 69 +++++++++++----- nodes/core/locales/en-US/messages.json | 2 + red/api/info.js | 4 + settings.js | 4 + 5 files changed, 161 insertions(+), 23 deletions(-) diff --git a/nodes/core/io/05-tls.html b/nodes/core/io/05-tls.html index 5de7e99c4..7befd1f1f 100644 --- a/nodes/core/io/05-tls.html +++ b/nodes/core/io/05-tls.html @@ -15,17 +15,42 @@ --> diff --git a/nodes/core/io/05-tls.js b/nodes/core/io/05-tls.js index a0929d988..a93ed1b39 100644 --- a/nodes/core/io/05-tls.js +++ b/nodes/core/io/05-tls.js @@ -21,34 +21,65 @@ module.exports = function(RED) { function TLSConfig(n) { RED.nodes.createNode(this,n); this.valid = true; + this.verifyservercert = n.verifyservercert; var certPath = n.cert.trim(); var keyPath = n.key.trim(); var caPath = n.ca.trim(); - if ( (certPath.length > 0) !== (keyPath.length > 0)) { - this.valid = false; - this.error(RED._("tls.error.missing-file")); - return; - } - this.verifyservercert = n.verifyservercert; + if ((certPath.length > 0) || (keyPath.length > 0)) { - try { - if (certPath) { - this.cert = fs.readFileSync(certPath); + if ( (certPath.length > 0) !== (keyPath.length > 0)) { + this.valid = false; + this.error(RED._("tls.error.missing-file")); + return; } - if (keyPath) { - this.key = fs.readFileSync(keyPath); + + try { + if (certPath) { + this.cert = fs.readFileSync(certPath); + } + if (keyPath) { + this.key = fs.readFileSync(keyPath); + } + if (caPath) { + this.ca = fs.readFileSync(caPath); + } + } catch(err) { + this.valid = false; + this.error(err.toString()); + return; + } + } else { + if (this.credentials) { + var certData = this.credentials.certdata || ""; + var keyData = this.credentials.keydata || ""; + var caData = this.credentials.cadata || ""; + + if ((certData.length > 0) !== (keyData.length > 0)) { + this.valid = false; + this.error(RED._("tls.error.missing-file")); + return; + } + + if (certData) { + this.cert = certData; + } + if (keyData) { + this.key = keyData; + } + if (caData) { + this.ca = caData; + } } - if (caPath) { - this.ca = fs.readFileSync(caPath); - } - } catch(err) { - this.valid = false; - this.error(err.toString()); - return; } } - RED.nodes.registerType("tls-config",TLSConfig); + RED.nodes.registerType("tls-config", TLSConfig, { + credentials: { + certdata: {type:"text"}, + keydata: {type:"text"}, + cadata: {type:"text"} + } + }); TLSConfig.prototype.addTLSOptions = function(opts) { if (this.valid) { diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index 0ad39f7a8..448382e4a 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -126,6 +126,8 @@ "tls": { "tls": "TLS configuration", "label": { + "use-local-files": "Use key and certificates from local files", + "upload": "Upload", "cert": "Certificate", "key": "Private Key", "ca": "CA Certificate", diff --git a/red/api/info.js b/red/api/info.js index e2661bdc3..72ec52b93 100644 --- a/red/api/info.js +++ b/red/api/info.js @@ -43,6 +43,10 @@ module.exports = { safeSettings.flowFilePretty = settings.flowFilePretty; } + if (settings.tlsDisableLocalFiles) { + safeSettings.tlsDisableLocalFiles = settings.tlsDisableLocalFiles; + } + if (!runtime.nodes.paletteEditorEnabled()) { safeSettings.editorTheme = safeSettings.editorTheme || {}; safeSettings.editorTheme.palette = safeSettings.editorTheme.palette || {}; diff --git a/settings.js b/settings.js index 79e8839f7..6b9228343 100644 --- a/settings.js +++ b/settings.js @@ -47,6 +47,10 @@ module.exports = { // The maximum length, in characters, of any message sent to the debug sidebar tab debugMaxLength: 1000, + // To disable the option for using local files for storing keys and certificates in the TLS configuration + // node, set this to true + //tlsDisableLocalFiles:true, + // Colourise the console output of the debug node //debugUseColors: true, From 0979d565bbbada4a7e92f3a71c88f9a15c8554c5 Mon Sep 17 00:00:00 2001 From: mblackstock Date: Tue, 28 Feb 2017 14:03:35 -0800 Subject: [PATCH 2/2] changes as suggested by @knolleary --- nodes/core/io/05-tls.html | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/nodes/core/io/05-tls.html b/nodes/core/io/05-tls.html index 7befd1f1f..b28ab7314 100644 --- a/nodes/core/io/05-tls.html +++ b/nodes/core/io/05-tls.html @@ -22,9 +22,10 @@
- + + @@ -33,9 +34,10 @@
- + + @@ -44,9 +46,10 @@
- + + @@ -89,7 +92,6 @@ certname: {value:""}, keyname: {value:""}, caname: {value:""}, - uselocalfiles: {value:true}, verifyservercert: {value: true} }, credentials: { @@ -116,6 +118,7 @@ $("#node-config-input-uselocalfiles").on("click",function() { updateFileUpload(); }); + function saveFile(property, file) { var dataInputId = "#node-config-input-"+property+"data"; var filenameInputId = "#node-config-input-"+property+"name"; @@ -137,6 +140,22 @@ $("#node-config-input-cafile" ).change(function() { saveFile("ca", this.files[0]); }); + + function clearNameData(prop) { + $("#tls-config-"+prop+"name").text(""); + $("#node-config-input-"+prop+"data").val(""); + $("#node-config-input-"+prop+"name").val(""); + } + $("#tls-config-button-cert-clear").click(function() { + clearNameData("cert"); + }); + $("#tls-config-button-key-clear").click(function() { + clearNameData("key"); + }); + $("#tls-config-button-ca-clear").click(function() { + clearNameData("ca"); + }); + if (RED.settings.tlsDisableLocalFiles) { $("#node-config-row-uselocalfiles").hide(); } else { @@ -144,7 +163,6 @@ } // in case paths were set from old TLS config if(this.cert || this.key || this.ca) { - this.uselocalfiles = true; $("#node-config-input-uselocalfiles").prop('checked',true); } $("#tls-config-certname").text(this.certname); @@ -154,12 +172,9 @@ }, oneditsave: function() { if ($("#node-config-input-uselocalfiles").is(':checked')) { - $("#node-config-input-cadata").val(""); - $("#node-config-input-caname").val(""); - $("#node-config-input-certdata").val(""); - $("#node-config-input-certname").val(""); - $("#node-config-input-keydata").val(""); - $("#node-config-input-keyname").val(""); + clearNameData("ca"); + clearNameData("cert"); + clearNameData("key"); } else { $("#node-config-input-ca").val(""); $("#node-config-input-cert").val("");