From 1e440741da866155e8672f5056e22889ee29c505 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Fri, 24 Dec 2021 05:58:35 -0500 Subject: [PATCH] mqtt: add support for "secret" credentials Deprecate the "password_file" option in favor of "password". Signed-off-by: Eric Callahan --- moonraker/components/mqtt.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/moonraker/components/mqtt.py b/moonraker/components/mqtt.py index a3c75d6..daf4c94 100644 --- a/moonraker/components/mqtt.py +++ b/moonraker/components/mqtt.py @@ -143,8 +143,12 @@ class MQTTClient(APITransport, Subscribable): self.event_loop = self.server.get_event_loop() self.address: str = config.get('address') self.port: int = config.getint('port', 1883) - self.user_name = config.get('username', None) - pw_file_path = config.get('password_file', None) + user = config.gettemplate('username', None) + self.user_name: Optional[str] = None + if user: + self.user_name = user.render() + pw_file_path = config.get('password_file', None, deprecate=True) + pw_template = config.gettemplate('password', None) self.password: Optional[str] = None if pw_file_path is not None: pw_file = pathlib.Path(pw_file_path).expanduser().absolute() @@ -152,6 +156,8 @@ class MQTTClient(APITransport, Subscribable): raise config.error( f"Password file '{pw_file}' does not exist") self.password = pw_file.read_text().strip() + if pw_template is not None: + self.password = pw_template.render() protocol = config.get('mqtt_protocol', "v3.1.1") self.protocol = MQTT_PROTOCOLS.get(protocol, None) if self.protocol is None: