Skip to content

Commit

Permalink
feat: Enabled authentication for MQTT
Browse files Browse the repository at this point in the history
fixes #13
  • Loading branch information
hferentschik committed Feb 21, 2022
1 parent 6a2c570 commit 84fb402
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 12 deletions.
3 changes: 2 additions & 1 deletion anemometer/anemometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def measure(self):

broker_address = os.environ.get('MQTT_BROKER') or "mqtt"
client = mqtt.Client("1")

if "MQTT_USER" in os.environ and "MQTT_PASSWORD" in os.environ:
client.username_pw_set(username=os.environ.get('MQTT_USER'),password=os.environ.get('MQTT_PASSWORD'))

def record():
client.connect(broker_address)
Expand Down
3 changes: 2 additions & 1 deletion humidity/sht30.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def data(self):

broker_address = os.environ.get('MQTT_BROKER') or "mqtt"
client = mqtt.Client("1")

if "MQTT_USER" in os.environ and "MQTT_PASSWORD" in os.environ:
client.username_pw_set(username=os.environ.get('MQTT_USER'),password=os.environ.get('MQTT_PASSWORD'))

def record():
client.connect(broker_address)
Expand Down
5 changes: 3 additions & 2 deletions mqtt/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM arm64v8/eclipse-mosquitto:2.0.10
FROM arm64v8/eclipse-mosquitto:2.0.14

COPY mosquitto.conf /mosquitto/config/mosquitto.conf
COPY mosquitto.conf /mosquitto/config/mosquitto.conf
COPY docker-entrypoint.sh .
1 change: 0 additions & 1 deletion mqtt/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Mosquitto

This container configures [Eclipse Mosquitto](https://github.com/eclipse/mosquitto) as a MQTT message broker sensors can send their data to.

19 changes: 19 additions & 0 deletions mqtt/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/ash
set -e

# Configure auth
if [[ -z "${MQTT_USER}" || -z "${MQTT_PASSWORD}" ]]; then
echo "Using un-authenticated configuration"
else
echo "Configruing username/password authentication"
sed -i -e 's/^#password_file$/password_file \/mosquitto\/config\/passwd/g' -e 's/^allow_anonymous true$/allow_anonymous false/' /mosquitto/config/mosquitto.conf
mosquitto_passwd -c -b /mosquitto/config/passwd "${MQTT_USER}" "${MQTT_PASSWORD}"
fi

# Set permissions
user="$(id -u)"
if [ "$user" = '0' ]; then
[ -d "/mosquitto" ] && chown -R mosquitto:mosquitto /mosquitto || true
fi

exec "$@"
2 changes: 1 addition & 1 deletion nginx/http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ server {
proxy_pass http://dashboard:80/weather/;
}

location /{
location / {
proxy_pass http://mqtt:9001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
Expand Down
3 changes: 2 additions & 1 deletion raingauge/raingauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def reset(self):

broker_address = os.environ.get('MQTT_BROKER') or "mqtt"
client = mqtt.Client("1")

if "MQTT_USER" in os.environ and "MQTT_PASSWORD" in os.environ:
client.username_pw_set(username=os.environ.get('MQTT_USER'),password=os.environ.get('MQTT_PASSWORD'))

def record():
client.connect(broker_address)
Expand Down
2 changes: 1 addition & 1 deletion telegraf/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM arm64v8/telegraf:1.15.4
FROM telegraf:1.19

RUN apt-get update \
&& apt-get install -y vim netcat \
Expand Down
6 changes: 3 additions & 3 deletions temperature/sensor_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def read_temperature
:value => measurement,
:sensor => 'DS18B20'
}
}
}

json_payload = JSON[payload]
logger.info "new measurement: #{json_payload}"
begin
MQTT::Client.connect(broker_address) do |c|
MQTT::Client.connect(:host => broker_address, :username => ENV['MQTT_USER'], :password => ENV['MQTT_PASSWORD'],) do |c|
c.publish('sensors', json_payload)
end
rescue Exception => e
rescue Exception => e
logger.info "unable to connect or publish to MQTT client: #{e.message}"
end
end
Expand Down
3 changes: 2 additions & 1 deletion windvane/windvane.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def record(self):
windvane = Windvane()
broker_address = os.environ.get('MQTT_BROKER') or "mqtt"
client = mqtt.Client("1")

if "MQTT_USER" in os.environ and "MQTT_PASSWORD" in os.environ:
client.username_pw_set(username=os.environ.get('MQTT_USER'),password=os.environ.get('MQTT_PASSWORD'))

def record():
client.connect(broker_address)
Expand Down

0 comments on commit 84fb402

Please sign in to comment.