Allow to specify MQTT server in secrets (#12904)

* add server, pan_id, ext_pan_id to secrets

* fix tests

* clean secrets before every test run

* add interpolation of mqtt.server, advanced.pan_id, advanced.ext_pan_id and split mqtt.user, mqtt.pass requirement

* remove pan_id and ext_pan_id from secrets processing
This commit is contained in:
Vladimir Vitkov
2022-06-27 18:42:05 +02:00
committed by GitHub
parent e291687870
commit 326c2aaa90
2 changed files with 53 additions and 1 deletions
+9 -1
View File
@@ -196,6 +196,7 @@ function write(): void {
// In case the setting is defined in a separte file (e.g. !secret network_key) update it there.
for (const path of [
['mqtt', 'server'],
['mqtt', 'user'],
['mqtt', 'password'],
['advanced', 'network_key'],
@@ -323,11 +324,18 @@ function read(): Settings {
}
};
if (s.mqtt?.user && s.mqtt?.password) {
if (s.mqtt?.user) {
s.mqtt.user = interpetValue(s.mqtt.user);
}
if (s.mqtt?.password) {
s.mqtt.password = interpetValue(s.mqtt.password);
}
if (s.mqtt?.server) {
s.mqtt.server = interpetValue(s.mqtt.server);
}
if (s.advanced?.network_key) {
s.advanced.network_key = interpetValue(s.advanced.network_key);
}
+44
View File
@@ -39,6 +39,7 @@ describe('Settings', () => {
beforeEach(() => {
remove(configurationFile);
remove(secretFile);
remove(devicesFile);
remove(groupsFile);
clearEnvironmentVariables();
@@ -171,6 +172,49 @@ describe('Settings', () => {
expect(read(secretFile)).toStrictEqual({...contentSecret, username: 'test123', network_key: [1,2,3,4]});
});
it('Should read ALL secrets form a separate file', () => {
const contentConfiguration = {
mqtt: {
server: '!secret server',
user: '!secret username',
password: '!secret password',
},
advanced: {
network_key: '!secret network_key',
}
};
const contentSecret = {
server: 'my.mqtt.server',
username: 'mysecretusername',
password: 'mysecretpassword',
network_key: [1,2,3],
};
write(secretFile, contentSecret, false);
write(configurationFile, contentConfiguration);
const expected = {
base_topic: 'zigbee2mqtt',
include_device_information: false,
force_disable_retain: false,
password: "mysecretpassword",
server: "my.mqtt.server",
user: "mysecretusername",
};
expect(settings.get().mqtt).toStrictEqual(expected);
expect(settings.get().advanced.network_key).toStrictEqual([1,2,3]);
settings.testing.write();
expect(read(configurationFile)).toStrictEqual(contentConfiguration);
expect(read(secretFile)).toStrictEqual(contentSecret);
settings.set(['mqtt', 'server'], 'not.secret.server');
expect(read(configurationFile)).toStrictEqual(contentConfiguration);
expect(read(secretFile)).toStrictEqual({...contentSecret, server: 'not.secret.server'});
});
it('Should read devices form a separate file', () => {
const contentConfiguration = {
devices: 'devices.yaml',