Skip to content

Commit

Permalink
hide passwords in debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jblance authored Oct 23, 2024
1 parent 1e5d3a6 commit df8ce93
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion powermon/configmodel/config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MQTTConfig(NoExtraBaseModel):
name: str
port: None | int = Field(default=None)
username: None | str = Field(default=None)
password: None | str = Field(default=None)
password: None | str = Field(default=None, repr=False)
adhoc_topic: None | str = Field(default=None)
adhoc_result_topic: None | str = Field(default=None)

Expand Down
2 changes: 1 addition & 1 deletion powermon/configmodel/port_config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BlePortConfig(NoExtraBaseModel):
type: Literal["ble"]
mac: str
protocol: None | str
victron_key: None | str = Field(default=None)
victron_key: None | str = Field(default=None, repr=False)


class SerialPortConfig(NoExtraBaseModel):
Expand Down
15 changes: 14 additions & 1 deletion powermon/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import time
from argparse import ArgumentParser
from copy import deepcopy
from platform import python_version

import yaml
Expand Down Expand Up @@ -63,6 +64,18 @@ def _process_command_line_overrides(args):
_config["debuglevel"] = logging.DEBUG
return _config

def _safe_config(config):
"""return a config dict that hides passwords etc"""
keys_to_hide = ['password', 'victron_key']
_config = deepcopy(config)
for key in _config.keys():
if isinstance(_config[key], dict):
_config[key] = _safe_config(_config[key])
if key in keys_to_hide:
_config[key] = "******"
return _config



def main():
"""main entry point for powermon command"""
Expand Down Expand Up @@ -144,7 +157,7 @@ def main():
log.setLevel(config.get("debuglevel", logging.WARNING))

# debug config
log.info("config: %s", config)
log.info("config: %s", _safe_config(config)) # TODO: fix dumping of password and victron_key

# build mqtt broker object (optional)
mqtt_broker = MqttBroker.from_config(config=config.get("mqttbroker"))
Expand Down
4 changes: 2 additions & 2 deletions tests/config/mqtt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ commands:
mqttbroker:
name: localhost
port: 1883
username: null
password: null
username: test
password: test_password
adhoc_topic: powermon/adhoc_commands
adhoc_result_topic: powermon/adhoc_results

Expand Down

0 comments on commit df8ce93

Please sign in to comment.