Skip to content

Commit 7960d16

Browse files
committed
Add SSL support for database connection
This update adds SSL support for the database connection in main.js and introduces new form fields in main.html to handle SSL-related configurations like CA-Certificate, Cert, and Key. It also includes error handling for failed certificate reads. It enhances security by enabling encrypted connections to databases using certificates.
1 parent daca6fa commit 7960d16

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## Unreleased
4+
5+
- 🔐 Add new ssl options for securing connections with certificates
6+
7+
38
## 1.0.5 (2021-01-25)
49

510
- 🐞 Fix an issue where msg.payload wasn't checked well (see #3)
@@ -24,4 +29,4 @@
2429

2530
## 1.0.0 (2020-05-11)
2631

27-
- 🎉 First version
32+
- 🎉 First version

src/main.html

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@
1212
<input type="checkbox" id="node-config-input-tls" style="display:inline-block; width:auto; vertical-align:top;">
1313
<label for="node-config-input-tls" style="width:auto !important;"> Encrypt connection to database</label>
1414
</div>
15+
<div class="form-row">
16+
<label for="node-config-input-rejectUnauthorized"></label>
17+
<input type="checkbox" id="node-config-input-rejectUnauthorized"
18+
style="display:inline-block; width:auto; vertical-align:top;">
19+
<label for="node-config-input-rejectUnauthorized" style="width:auto !important;"> Verify certificate
20+
(recommended)</label>
21+
</div>
22+
<div class="form-row">
23+
<label for="node-config-input-caCertificate"><i class="fa fa-certificate"></i> CA-Cert.</label>
24+
<input type="text" id="node-config-input-caCertificate">
25+
</div>
26+
<div class="form-row">
27+
<label for="node-config-input-cert"><i class="fa fa-certificate"></i> Cert</label>
28+
<input type="text" id="node-config-input-cert">
29+
</div>
30+
<div class="form-row">
31+
<label for="node-config-input-key"><i class="fa fa-key"></i> Key</label>
32+
<input type="text" id="node-config-input-key">
33+
</div>
1534
<div class="form-row">
1635
<label for="node-config-input-user"><i class="fa fa-user"></i> User</label>
1736
<input type="text" id="node-config-input-user">
@@ -49,6 +68,22 @@
4968
value: true,
5069
required: true
5170
},
71+
rejectUnauthorized: {
72+
value: true,
73+
required: true
74+
},
75+
caCertificate: {
76+
value: "",
77+
required: false
78+
},
79+
cert: {
80+
value: "",
81+
required: false
82+
},
83+
key: {
84+
value: "",
85+
required: false
86+
},
5287
database: {
5388
value: "",
5489
required: true
@@ -65,6 +100,21 @@
65100
// Note: label (and probably labelStyle) have to be a classical function (not an arrow function)
66101
label: function () {
67102
return this.name || this.database
103+
},
104+
oneditprepare: function () {
105+
function toggleTlsFields(show) {
106+
$("#node-config-input-caCertificate").closest('div').toggle(show);
107+
$("#node-config-input-cert").closest('div').toggle(show);
108+
$("#node-config-input-key").closest('div').toggle(show);
109+
$("#node-config-input-rejectUnauthorized").closest('div').toggle(show);
110+
}
111+
112+
const tlsCheckbox = $("#node-config-input-tls");
113+
tlsCheckbox.on('change', function () {
114+
toggleTlsFields(this.checked);
115+
});
116+
117+
toggleTlsFields(tlsCheckbox.prop('checked'));
68118
}
69119
});
70120
</script>
@@ -162,4 +212,4 @@ <h3>References</h3>
162212
return this.name ? 'node_label_italic' : ''
163213
}
164214
});
165-
</script>
215+
</script>

src/main.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs');
2+
13
module.exports = (RED) => {
24
'use strict';
35

@@ -39,6 +41,38 @@ module.exports = (RED) => {
3941
return;
4042
}
4143

44+
let sslOptions;
45+
if (config.tls) {
46+
sslOptions = {};
47+
if (config.caCertificate) {
48+
try {
49+
sslOptions.ca = fs.readFileSync(config.caCertificate);
50+
} catch (err) {
51+
this.error(`Unable to read CA-Certificate: ${err}`);
52+
return;
53+
}
54+
}
55+
if (config.cert) {
56+
try {
57+
sslOptions.ca = fs.readFileSync(config.cert)
58+
} catch (err) {
59+
this.error(`Unable to read cert: ${err}`);
60+
return;
61+
}
62+
}
63+
if (config.key) {
64+
try {
65+
sslOptions.ca = fs.readFileSync(config.key)
66+
} catch (err) {
67+
this.error(`Unable to read key: ${err}`);
68+
return;
69+
}
70+
}
71+
sslOptions.rejectUnauthorized = config.rejectUnauthorized;
72+
} else {
73+
sslOptions = false;
74+
}
75+
4276
// Note: the connection is not done here
4377
this.pool = mysql.createPool({
4478
host: config.host,
@@ -50,7 +84,7 @@ module.exports = (RED) => {
5084
connectionLimit: 5,
5185
queueLimit: 0,
5286
connectTimeout: 1000,
53-
ssl: config.tls ? {} : false,
87+
ssl: sslOptions,
5488

5589
// See https://www.npmjs.com/package/mysql#custom-format
5690
queryFormat: (query, values) => {

0 commit comments

Comments
 (0)