|
33 | 33 |
|
34 | 34 | __version__ = "0.0.1"
|
35 | 35 | __author__ = "Reuben Miller"
|
| 36 | +C8Y_TOKEN_TOPIC = "c8y/s/dat" |
36 | 37 |
|
37 | 38 |
|
38 | 39 | class MQTTMessage:
|
@@ -170,7 +171,22 @@ def get_logs(
|
170 | 171 | except Exception as ex:
|
171 | 172 | log.warning("Failed to retrieve logs. %s", ex, exc_info=True)
|
172 | 173 |
|
173 |
| - super().get_logs(device.get_id(), date_from=date_from, show=show) |
| 174 | + log_output = super().get_logs(device.get_id(), date_from=date_from, show=False) |
| 175 | + if show: |
| 176 | + hide_sensitive = self._hide_sensitive_factory() |
| 177 | + for line in log_output: |
| 178 | + print(hide_sensitive(line)) |
| 179 | + |
| 180 | + def _hide_sensitive_factory(self): |
| 181 | + # This is fragile and should be improved upon once a more suitable/robust method of logging and querying |
| 182 | + # the mqtt messages is found. |
| 183 | + token_replace_pattern = re.compile(r"\{.+$") |
| 184 | + def _hide(line: str) -> str: |
| 185 | + if C8Y_TOKEN_TOPIC in line and "71," in line: |
| 186 | + line_sensitive = token_replace_pattern.sub(f"(redacted log entry): Received token: topic={C8Y_TOKEN_TOPIC}, message=71,<redacted>", line) |
| 187 | + return line_sensitive |
| 188 | + return line |
| 189 | + return _hide |
174 | 190 |
|
175 | 191 | def log_operations(self, mo_id: str, status: str = None):
|
176 | 192 | """Log operations to help with debugging
|
@@ -523,10 +539,16 @@ def log_mqtt_messages(self, topic: str = "#", date_from: Union[datetime, float]
|
523 | 539 | date_from=date_from,
|
524 | 540 | **kwargs,
|
525 | 541 | )
|
526 |
| - entries = [ |
527 |
| - f'{item["message"]["tst"].replace("+0000", ""):32} {item["message"]["topic"]:70} {bytes.fromhex(item["payload_hex"]).decode("utf8", errors="replace")}' |
528 |
| - for item in items |
529 |
| - ] |
| 542 | + |
| 543 | + # hide sensitive information |
| 544 | + # This is fragile and should be improved upon once a more suitable/robust method of logging and querying |
| 545 | + # the mqtt messages is found. |
| 546 | + entries = [] |
| 547 | + for item in items: |
| 548 | + payload = bytes.fromhex(item["payload_hex"]).decode("utf8", errors="replace") |
| 549 | + if item["message"]["topic"] == C8Y_TOKEN_TOPIC and payload.startswith("71,"): |
| 550 | + payload = "71,<redacted>" |
| 551 | + entries.append(f'{item["message"]["tst"].replace("+0000", ""):32} {item["message"]["topic"]:70} {payload}') |
530 | 552 | log.info("---- mqtt messages ----\n%s", "\n".join(entries))
|
531 | 553 |
|
532 | 554 | @keyword("Should Have MQTT Messages")
|
|
0 commit comments