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 58a5112
Show file tree
Hide file tree
Showing 11 changed files with 59 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
3 changes: 2 additions & 1 deletion telegraf/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM arm64v8/telegraf:1.15.4
FROM telegraf:1.19

RUN apt-get update \
&& apt-get install -y vim netcat \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

COPY telegraf.conf /etc/telegraf/telegraf.conf
COPY entrypoint.sh /entrypoint.sh
23 changes: 23 additions & 0 deletions telegraf/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
set -e

# Configure auth
if [[ -z "${MQTT_USER}" || -z "${MQTT_PASSWORD}" ]]; then
echo "Using un-authenticated configuration"
else
echo "Configuring username/password authentication"
sed -i -e 's/^ # username =.*/ username = \"'"${MQTT_USER}"'\"/g' -e 's/^ # password =.*/ password = \"'"${MQTT_PASSWORD}"'\"/g' /etc/telegraf/telegraf.conf
fi

if [ "${1:0:1}" = '-' ]; then
set -- telegraf "$@"
fi

if [ $EUID -ne 0 ]; then
exec "$@"
else
# Allow telegraf to send ICMP packets and bind to privliged ports
setcap cap_net_raw,cap_net_bind_service+ep /usr/bin/telegraf || echo "Failed to set additional capabilities on /usr/bin/telegraf"

exec setpriv --reuid telegraf --init-groups "$@"
fi
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 58a5112

Please sign in to comment.