From 34e2dd11b27f131f6d43eeb1011a76a11bd4eb49 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Mon, 16 Sep 2024 17:22:50 -0400 Subject: [PATCH] Added local-dev broker and golang lib --- .mosquitto/.gitignore | 1 + .mosquitto/mosquitto.conf | 2 ++ Makefile | 11 +++++++++++ docker-compose.yaml | 14 ++++++++++++++ go.mod | 7 +++++++ go.sum | 8 ++++++++ scripts/add-mqtt-user.sh | 29 +++++++++++++++++++++++++++++ scripts/common.sh | 10 ++++++++++ scripts/encrypt-passwd.sh | 16 ++++++++++++++++ 9 files changed, 98 insertions(+) create mode 100644 .mosquitto/.gitignore create mode 100644 .mosquitto/mosquitto.conf create mode 100644 Makefile create mode 100644 docker-compose.yaml create mode 100644 go.sum create mode 100755 scripts/add-mqtt-user.sh create mode 100644 scripts/common.sh create mode 100755 scripts/encrypt-passwd.sh diff --git a/.mosquitto/.gitignore b/.mosquitto/.gitignore new file mode 100644 index 0000000..748a50e --- /dev/null +++ b/.mosquitto/.gitignore @@ -0,0 +1 @@ +passwd \ No newline at end of file diff --git a/.mosquitto/mosquitto.conf b/.mosquitto/mosquitto.conf new file mode 100644 index 0000000..760313a --- /dev/null +++ b/.mosquitto/mosquitto.conf @@ -0,0 +1,2 @@ +allow_anonymous false +password_file /etc/mosquitto/passwd \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eac9505 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +start-dev: + docker compose up -d +.PHONEY: start-dev + +stop-dev: + docker compose down +.PHONEY: stop-dev + +install-dev-deps: + python3 -m pip install pre-commit +.PHONEY: install-dev-dep \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..b28adfa --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,14 @@ +version: "3.7" + +services: + mosquitto: + image: eclipse-mosquitto:latest + hostname: mosquitto + container_name: mosquitto + restart: unless-stopped + ports: + - "1883:1883" + - "9001:9001" + volumes: + - ./.mosquitto:/etc/mosquitto + - ./.mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf diff --git a/go.mod b/go.mod index a6fa431..419dff4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module git.runcible.io/learning/learn_mqtt_go go 1.23.1 + +require ( + github.com/eclipse/paho.mqtt.golang v1.5.0 // indirect + github.com/gorilla/websocket v1.5.3 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/sync v0.7.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b555dd0 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o= +github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= diff --git a/scripts/add-mqtt-user.sh b/scripts/add-mqtt-user.sh new file mode 100755 index 0000000..02564e3 --- /dev/null +++ b/scripts/add-mqtt-user.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/common.sh" + +USER="$1" +PASSWD="$2" + +if [ -z "$USER" ]; then + echo "Error must provide a user name as first argument" + exit 1 +fi + +if [ -z "$PASSWD" ]; then + echo "Error must provide a user name as first argument" + exit 1 +fi + +echo "Checking Mosquitto is running..." +if ! check_port 1883 || ! check_port 9001; then + echo "Mosquitto port 1883 or 9001 not found running" +fi + +echo "Adding Mosquitto user ${USER}" +docker exec mosquitto mosquitto_passwd -b /etc/mosquitto/passwd "${USER}" "${PASSWD}" + +echo "User added!" \ No newline at end of file diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 0000000..54af3fa --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +check_port(){ + port=$1 + if ss -tuln | grep -q ":$port ";then + return 0 + else + return 1 + fi +} diff --git a/scripts/encrypt-passwd.sh b/scripts/encrypt-passwd.sh new file mode 100755 index 0000000..19a85c0 --- /dev/null +++ b/scripts/encrypt-passwd.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/common.sh" + +echo "Checking Mosquitto is running..." +if ! check_port 1883 || ! check_port 9001; then + echo "Mosquitto port 1883 or 9001 not found running" +fi + +echo "Updating ./.mosquitto/passwd plaintext to use hashed passwords" +docker exec mosquitto mosquitto_passwd -U /etc/mosquitto/passwd + +echo "Update complete please restart Mosquitto service" \ No newline at end of file