mqtt: add support for "secret" credentials

Deprecate the "password_file" option in favor of "password".

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-12-24 05:58:35 -05:00
parent c5fd863bb7
commit 1e440741da
1 changed files with 8 additions and 2 deletions

View File

@ -143,8 +143,12 @@ class MQTTClient(APITransport, Subscribable):
self.event_loop = self.server.get_event_loop() self.event_loop = self.server.get_event_loop()
self.address: str = config.get('address') self.address: str = config.get('address')
self.port: int = config.getint('port', 1883) self.port: int = config.getint('port', 1883)
self.user_name = config.get('username', None) user = config.gettemplate('username', None)
pw_file_path = config.get('password_file', 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 self.password: Optional[str] = None
if pw_file_path is not None: if pw_file_path is not None:
pw_file = pathlib.Path(pw_file_path).expanduser().absolute() pw_file = pathlib.Path(pw_file_path).expanduser().absolute()
@ -152,6 +156,8 @@ class MQTTClient(APITransport, Subscribable):
raise config.error( raise config.error(
f"Password file '{pw_file}' does not exist") f"Password file '{pw_file}' does not exist")
self.password = pw_file.read_text().strip() 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") protocol = config.get('mqtt_protocol', "v3.1.1")
self.protocol = MQTT_PROTOCOLS.get(protocol, None) self.protocol = MQTT_PROTOCOLS.get(protocol, None)
if self.protocol is None: if self.protocol is None: