Skip to content

Commit

Permalink
fix logging handler to parse out passwords
Browse files Browse the repository at this point in the history
Signed-off-by: jacobdonenfeld <jedonenfeld@gmail.com>
  • Loading branch information
jacobdonenfeld committed Feb 24, 2023
1 parent 378d447 commit cceeb8a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
21 changes: 2 additions & 19 deletions custom_components/aerogarden/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@
)


class RedactingFormatter(object):
def __init__(self, orig_formatter, patterns):
self.orig_formatter = orig_formatter
self._patterns = patterns

def format(self, record):
msg = self.orig_formatter.format(record)
for pattern in self._patterns:
msg = msg.replace(pattern, "<PASSWD>")
return msg

def __getattr__(self, attr):
return getattr(self.orig_formatter, attr)


def postAndHandle(url, post_data, headers):
try:
r = requests.post(url, json=post_data, headers=headers)
Expand All @@ -60,6 +45,7 @@ def postAndHandle(url, post_data, headers):
response = r.json()
except ValueError as ex:
if post_data["&userPwd"]:
# Remove password before printing
del post_data["&userPwd"]
_LOGGER.exception(
"error: Could not marshall post request to json.\n post:\n%s\n\nexception:\n%s",
Expand Down Expand Up @@ -226,10 +212,7 @@ def setup(hass, config: dict):

username = domain_config.get(CONF_USERNAME)
password = domain_config.get(CONF_PASSWORD)
# Filter out password from logging
logging.Handler.setFormatter(
RedactingFormatter(logging.Formatter, patterns=[password])
)

host = domain_config.get(CONF_HOST, DEFAULT_HOST)

ag = AerogardenAPI(username, password, host)
Expand Down
16 changes: 8 additions & 8 deletions custom_components/aerogarden/manifest.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"domain": "aerogarden",
"name": "Aerogarden",
"documentation": "https://github.com/jacobdonenfeld/homeassistant-aerogarden",
"issue_tracker": "https://github.com/jacobdonenfeld/homeassistant-aerogarden/issues",
"after_dependencies": [
"integration"
],
"codeowners": ["@jacobdonenfeld"],
"dependencies": [
"device_tracker",
"binary_sensor",
"light"
],
"after_dependencies": [
"integration"
],
"codeowners": ["@jacobdonenfeld"],
"documentation": "https://github.com/jacobdonenfeld/homeassistant-aerogarden",
"issue_tracker": "https://github.com/jacobdonenfeld/homeassistant-aerogarden/issues",
"iot_class": "cloud_polling",
"requirements": [],
"version": "0.2.0",
"iot_class": "cloud_polling"
"version": "0.2.0"
}

0 comments on commit cceeb8a

Please sign in to comment.