Skip to content

Commit 23e71d9

Browse files
authored
Merge pull request #2425 from reubenmiller/test-hide-token-message
tests: hide cumulocity iot token message from the test log output
2 parents abfd325 + 95d5dc3 commit 23e71d9

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
__version__ = "0.0.1"
3535
__author__ = "Reuben Miller"
36+
C8Y_TOKEN_TOPIC = "c8y/s/dat"
3637

3738

3839
class MQTTMessage:
@@ -170,7 +171,22 @@ def get_logs(
170171
except Exception as ex:
171172
log.warning("Failed to retrieve logs. %s", ex, exc_info=True)
172173

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
174190

175191
def log_operations(self, mo_id: str, status: str = None):
176192
"""Log operations to help with debugging
@@ -523,10 +539,16 @@ def log_mqtt_messages(self, topic: str = "#", date_from: Union[datetime, float]
523539
date_from=date_from,
524540
**kwargs,
525541
)
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}')
530552
log.info("---- mqtt messages ----\n%s", "\n".join(entries))
531553

532554
@keyword("Should Have MQTT Messages")

0 commit comments

Comments
 (0)