Skip to content

Commit

Permalink
Allow to store MQTT credentials separate from configuration.yaml. #2201
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 25, 2019
1 parent 02bf8a8 commit 505be31
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 32 additions & 2 deletions test/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 505be31

Please sign in to comment.