Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make redaction of private data dynamic #74

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions custom_components/e3dc_rscp/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections.abc import Callable
import logging
import re
from traceback import format_exception
from typing import Any

Expand All @@ -17,6 +18,8 @@

_LOGGER = logging.getLogger(__name__)

_redact_regex = re.compile("(system-mac|macAddress|serial)", re.IGNORECASE)


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
Expand Down Expand Up @@ -47,7 +50,7 @@ def __init__(self, _hass: HomeAssistant, _entry: ConfigEntry):
def create_dump(self):
"""Create the dump data and redact pricate data, central call-in point."""
self._collect_data()
self._redact_private_information_from_dump()
self._redact_private_information(self.result)

def get_dump(self) -> dict[str, Any]:
"""Get the collected data."""
Expand Down Expand Up @@ -109,19 +112,13 @@ def _query_data_for_dump(self, call: Callable[[], Any]) -> Any:
except Exception as ex: # pylint: disable=broad-exception-caught
return {"exception": format_exception(ex)}

def _redact_private_information_from_dump(self):
"""Redact sensitive data from the dump so that it can be shared."""
self.result["current_data"]["system-mac"] = "<redacted>"
self.result["get_system_info"]["macAddress"] = "<redacted>"
self.result["get_system_info"][
"serial"
] = f"{self.result['get_system_info']['serial'][:3]}<redacted>"

for pvi in self.result["get_pvis_data"]:
pvi["serialNumber"] = f"{pvi['serialNumber'][:3]}<redacted>"

for bat in self.result["get_batteries_data"]:
for dcb in bat["dcbs"]:
bat["dcbs"][dcb][
"serialCode"
] = f"{bat['dcbs'][dcb]['serialCode'][:3]}<redacted>"
def _redact_private_information(self, data: Any):
"""Redact data recursively so that it can be shared."""

if isinstance(data, dict | list):
for key, value in (
data.items() if isinstance(data, dict) else enumerate(data)
):
if isinstance(value, str) and _redact_regex.search(key) is not None:
data[key] = f"{value[:3]}<redacted>"
self._redact_private_information(value)
Loading