Skip to content

Commit

Permalink
Create password file
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Jan 3, 2023
1 parent 7fe4551 commit 282e555
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions server/services/mqtt/lib/configureContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const { constants } = require('fs');
const os = require('os');

const logger = require('../../../utils/logger');
const { DEFAULT } = require('./constants');

const MOSQUITTO_DIRECTORY = '/var/lib/gladysassistant/mosquitto';
const MOSQUITTO_CONFIG_FILE_PATH = `${MOSQUITTO_DIRECTORY}/mosquitto.conf`;
const MOSQUITTO_INTERNAL_PASSWORD_FILE_PATH = '/mosquitto/config/mosquitto.passwd';

const MOSQUITTO_CONFIG_PORT = 'listener 1883';
const MOSQUITTO_CONFIG_CONTENT = [
'allow_anonymous false',
'connection_messages false',
`password_file ${MOSQUITTO_INTERNAL_PASSWORD_FILE_PATH}`,
`password_file ${DEFAULT.PASSWORD_FILE_PATH}`,
MOSQUITTO_CONFIG_PORT,
];

Expand Down Expand Up @@ -42,6 +42,10 @@ async function configureContainer() {
logger.info('Writting default eclipse-mosquitto configuration...');
await fs.writeFile(MOSQUITTO_CONFIG_FILE_PATH, MOSQUITTO_CONFIG_CONTENT.join(os.EOL));
}

// Create empty password file if not already exists
const pwdFile = await fs.open(DEFAULT.PASSWORD_FILE_PATH, 'w');
await pwdFile.close();
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions server/services/mqtt/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const DEFAULT = {
ERROR: 'ERROR',
},
MOSQUITTO_VERSION: '2',
PASSWORD_FILE_PATH: '/mosquitto/config/mosquitto.passwd',
};

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions server/services/mqtt/lib/saveConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ async function saveConfiguration({ mqttUrl, mqttUsername, mqttPassword, useEmbed
if (oldUser) {
// Delete old user
await this.gladys.system.exec(container.id, {
Cmd: ['mosquitto_passwd', '-D', '/mosquitto/config/mosquitto.passwd', oldUser],
Cmd: ['mosquitto_passwd', '-D', DEFAULT.PASSWORD_FILE_PATH, oldUser],
});
}

if (mqttUsername) {
// Generate password
await this.gladys.system.exec(container.id, {
Cmd: ['mosquitto_passwd', '-b', '/mosquitto/config/mosquitto.passwd', mqttUsername, mqttPassword],
Cmd: ['mosquitto_passwd', '-b', DEFAULT.PASSWORD_FILE_PATH, mqttUsername, mqttPassword],
});
}

Expand Down
4 changes: 4 additions & 0 deletions server/test/services/mqtt/lib/configureContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('mqttHandler.configureContainer', () => {
fsMock.readFile = fake.resolves('read');
fsMock.appendFile = fake.resolves(true);
fsMock.writeFile = fake.resolves('write');
fsMock.open = fake.resolves({ close: () => {} });
});

afterEach(() => {
Expand All @@ -44,6 +45,7 @@ describe('mqttHandler.configureContainer', () => {
constants.R_OK | constants.W_OK,
);
assert.calledOnceWithExactly(fsMock.readFile, '/var/lib/gladysassistant/mosquitto/mosquitto.conf');
assert.calledOnceWithExactly(fsMock.open, '/mosquitto/config/mosquitto.passwd', 'w');
assert.notCalled(fsMock.appendFile);
assert.notCalled(fsMock.writeFile);
});
Expand All @@ -65,6 +67,7 @@ describe('mqttHandler.configureContainer', () => {
'/var/lib/gladysassistant/mosquitto/mosquitto.conf',
`${os.EOL}listener 1883`,
);
assert.calledOnceWithExactly(fsMock.open, '/mosquitto/config/mosquitto.passwd', 'w');
assert.notCalled(fsMock.writeFile);
});

Expand All @@ -88,5 +91,6 @@ describe('mqttHandler.configureContainer', () => {
'/var/lib/gladysassistant/mosquitto/mosquitto.conf',
`allow_anonymous false${os.EOL}connection_messages false${os.EOL}password_file /mosquitto/config/mosquitto.passwd${os.EOL}listener 1883`,
);
assert.calledOnceWithExactly(fsMock.open, '/mosquitto/config/mosquitto.passwd', 'w');
});
});

0 comments on commit 282e555

Please sign in to comment.