summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatheus <matheus.guedes.mg.m@gmail.com>2025-10-10 23:22:52 -0300
committerMatheus <matheus.guedes.mg.m@gmail.com>2025-10-10 23:22:52 -0300
commita4fec975a665b6ebcb07fd7451e381f49f3c497a (patch)
treeadce02e32102a13c00ea25dea33ce06059654f74
parent22935fae60b6ab2040596d4a78d1812e2a16589e (diff)
fix:Não colocar credenciais no código
(-_-)
-rw-r--r--compose.yaml3
-rw-r--r--mqtt_client/main.py11
2 files changed, 13 insertions, 1 deletions
diff --git a/compose.yaml b/compose.yaml
index f56c1d2..4e4def4 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -18,6 +18,9 @@ services:
source: ./mosquitto-config/
target: /mosquitto/config/
mqtt-client:
+ environment:
+ MQTT_CLIENT_USER: ${MQTT_CLIENT_USER}
+ MQTT_CLIENT_PASSWORD: ${MQTT_CLIENT_PASSWORD}
build:
context: ./mqtt_client/
network_mode: "service:mosquitto"
diff --git a/mqtt_client/main.py b/mqtt_client/main.py
index 78cdca6..80b4da9 100644
--- a/mqtt_client/main.py
+++ b/mqtt_client/main.py
@@ -2,6 +2,7 @@ import paho.mqtt.client as mqtt
from paho.mqtt.enums import MQTTProtocolVersion
import json
from time import sleep
+import os
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Conectado: {reason_code}")
@@ -16,10 +17,18 @@ def on_message(client, userdata, msg):
print(f"Unit: {payload["unit"]}")
print(f"Timestamp: {payload["timestamp"]}")
+try:
+ user_name = os.environ["MQTT_CLIENT_USER"]
+ user_pass = os.environ["MQTT_CLIENT_PASSWORD"]
+except KeyError:
+ print("credentials not supplied in environment variables. Going unauthenticated...")
+ user_name = None
+ user_pass = None
+
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=MQTTProtocolVersion.MQTTv5)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
-mqttc.username_pw_set('tester', 'rosebud')
+mqttc.username_pw_set(user_name, user_pass)
connected = False