Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable use of mqtt username without password #25610

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ jobs:
VERSION=dev
DATE=${{ github.event.repository.updated_at }}

- name: release - Docker meta
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
uses: docker/metadata-action@v5
id: meta
with:
images: |
koenkk/zigbee2mqtt
ghcr.io/koenkk/zigbee2mqtt
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}

- name: release - Docker build and push
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
uses: docker/build-push-action@v6
Expand All @@ -93,7 +106,7 @@ jobs:
file: docker/Dockerfile
provenance: false
platforms: linux/arm64/v8,linux/amd64,linux/arm/v6,linux/arm/v7,linux/riscv64,linux/386
tags: koenkk/zigbee2mqtt:latest,ghcr.io/koenkk/zigbee2mqtt:latest,koenkk/zigbee2mqtt:${{ github.ref_name }},ghcr.io/koenkk/zigbee2mqtt:${{ github.ref_name }}
tags: ${{ steps.meta.outputs.tags }}
push: true
build-args: |
COMMIT=${{ github.sha }}
Expand Down Expand Up @@ -155,9 +168,7 @@ jobs:
git push origin master
git checkout dev
jq --indent 4 ".version = \"$TAG-dev\"" package.json > package.json.tmp
jq --indent 4 ".version = \"$TAG-dev\"" package-lock.json > package-lock.json.tmp
mv package.json.tmp package.json
mv package-lock.json.tmp package-lock.json
git add -A
git commit -m "chore: promote to dev"
git push origin dev
Expand Down
3 changes: 1 addition & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FROM linux-${TARGETARCH}-alpine AS base

ENV NODE_ENV=production
WORKDIR /app
RUN apk add --no-cache tzdata eudev tini nodejs libcap
RUN apk add --no-cache tzdata eudev tini nodejs

# Dependencies and build
FROM base AS deps
Expand Down Expand Up @@ -47,7 +47,6 @@ COPY package.json LICENSE index.js data/configuration.example.yaml ./

COPY docker/docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
RUN setcap 'cap_net_bind_service=+ep' /usr/bin/node

RUN mkdir /app/data

Expand Down
6 changes: 5 additions & 1 deletion lib/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ export default class MQTT {
logger.debug(`Using MQTT login with username: ${mqttSettings.user}`);
options.username = mqttSettings.user;
options.password = mqttSettings.password;
} else {
} else if(mqttSettings.user) {
logger.debug(`Using MQTT login with username only: ${mqttSettings.user}`);
options.username = mqttSettings.user;
}
else {
logger.debug(`Using MQTT anonymous login`);
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zigbee2mqtt",
"version": "2.0.0",
"version": "2.0.0-dev",
"description": "Zigbee to MQTT bridge using Zigbee-herdsman",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -62,7 +62,7 @@
"winston-transport": "^4.9.0",
"ws": "^8.18.0",
"zigbee-herdsman": "3.2.1",
"zigbee-herdsman-converters": "21.12.0",
"zigbee-herdsman-converters": "21.13.0",
"zigbee2mqtt-frontend": "0.9.4"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,46 @@ describe('Controller', () => {
expect(mockMQTTConnectAsync).toHaveBeenCalledWith('mqtt://localhost', expected);
});

it('Start controller with username MQTT settings', async () => {
const ca = tmp.fileSync().name;
fs.writeFileSync(ca, 'ca');
const key = tmp.fileSync().name;
fs.writeFileSync(key, 'key');
const cert = tmp.fileSync().name;
fs.writeFileSync(cert, 'cert');

const configuration = {
base_topic: 'zigbee2mqtt',
server: 'mqtt://localhost',
keepalive: 30,
ca,
cert,
key,
user: 'user1',
client_id: 'my_client_id',
reject_unauthorized: false,
version: 5,
maximum_packet_size: 20000,
};
settings.set(['mqtt'], configuration);
await controller.start();
await flushPromises();
expect(mockMQTTConnectAsync).toHaveBeenCalledTimes(1);
const expected = {
will: {payload: Buffer.from('{"state":"offline"}'), retain: true, topic: 'zigbee2mqtt/bridge/state', qos: 1},
keepalive: 30,
ca: Buffer.from([99, 97]),
key: Buffer.from([107, 101, 121]),
cert: Buffer.from([99, 101, 114, 116]),
username: 'user1',
clientId: 'my_client_id',
rejectUnauthorized: false,
protocolVersion: 5,
properties: {maximumPacketSize: 20000},
};
expect(mockMQTTConnectAsync).toHaveBeenCalledWith('mqtt://localhost', expected);
});

it('Should generate network_key, pan_id and ext_pan_id when set to GENERATE', async () => {
settings.set(['advanced', 'network_key'], 'GENERATE');
settings.set(['advanced', 'pan_id'], 'GENERATE');
Expand Down
19 changes: 19 additions & 0 deletions test/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,25 @@ describe('Settings', () => {
expect(settings.get().groups).toStrictEqual(expected);
});

it('Should allow username without password', () => {
const contentConfiguration = {
mqtt: {
server: 'my.mqtt.server',
user: 'myusername',
},
};
const expected = {
base_topic: 'zigbee2mqtt',
include_device_information: false,
maximum_packet_size: 1048576,
force_disable_retain: false,
server: 'my.mqtt.server',
user: 'myusername',
};
write(configurationFile, contentConfiguration);
expect(settings.get().mqtt).toStrictEqual(expected);
});

it('Should add groups with specific ID', () => {
write(configurationFile, {});

Expand Down
Loading