Allow to store MQTT credentials separate from configuration.yaml. https://github.com/Koenkk/zigbee2mqtt/issues/2201

This commit is contained in:
Koenkk committed 2019-10-25 18:43:14 +02:00
1 parent 02bf8a88bc
commit 505be311a4
2 files changed
+50 -2

No files matched your search

+18
View File
@@ -277,6 +277,24 @@ function validate() {
function read() {
const s = yaml.read(file);
// Read !secret MQTT username and password if set
const interpetValue = (value) => {
const re = /!(.*) (.*)/g;
const match = re.exec(value);
if (match) {
const file = data.joinPath(`${match[1]}.yaml`);
const key = match[2];
return yaml.read(file)[key];
} else {
return value;
}
};
if (s.mqtt && s.mqtt.user && s.mqtt.password) {
s.mqtt.user = interpetValue(s.mqtt.user);
s.mqtt.password = interpetValue(s.mqtt.password);
}
// Read devices/groups configuration from separate file.
if (typeof s.devices === 'string') {
const file = data.joinPath(s.devices);
+32 -2
View File
@@ -6,12 +6,15 @@ const fs = require('fs');
const configurationFile = data.joinPath('configuration.yaml');
const devicesFile = data.joinPath('devices.yaml');
const groupsFile = data.joinPath('groups.yaml');
const secretFile = data.joinPath('secret.yaml');
const yaml = require('js-yaml');
describe('Settings', () => {
const write = (file, json) => {
const write = (file, json, reread=true) => {
fs.writeFileSync(file, yaml.safeDump(json))
settings._reRead();
if (reread) {
settings._reRead();
}
};
const read = (file) => yaml.safeLoad(fs.readFileSync(file, 'utf8'));
const remove = (file) => {
@@ -89,6 +92,33 @@ describe('Settings', () => {
settings.getDevice('0x12345678');
});
it('onlythis Should read MQTT username asn password form a separate file', () => {
const contentConfiguration = {
mqtt: {
server: 'my.mqtt.server',
user: '!secret username',
password: '!secret password',
},
};
const contentSecret = {
username: 'mysecretusername',
password: 'mysecretpassword',
};
write(secretFile, contentSecret, false);
write(configurationFile, contentConfiguration);
const expected = {
include_device_information: false,
password: "mysecretpassword",
server: "my.mqtt.server",
user: "mysecretusername",
};
expect(settings.get().mqtt).toStrictEqual(expected);
});
it('Should read devices form a separate file', () => {
const contentConfiguration = {
devices: 'devices.yaml',