Skip to content

Commit 0d9625c

Browse files
committed
decode fix
1 parent 1b1fc56 commit 0d9625c

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

nodescraper/connection/inband/inbandlocal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def run_command(
5454

5555
res = subprocess.run(
5656
command,
57-
encoding=None,
57+
encoding="utf-8",
5858
shell=True,
5959
timeout=timeout,
6060
capture_output=True,

nodescraper/plugins/inband/journal/journal_collector.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26-
import io
27-
import json
26+
import base64
2827

2928
from nodescraper.base import InBandDataCollector
3029
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily
@@ -45,10 +44,9 @@ def _read_with_journalctl(self):
4544
Returns:
4645
str|None: system journal read
4746
"""
48-
cmd = "journalctl --no-pager --system --all --output=short-iso"
47+
cmd = "journalctl --no-pager --system --all --output=short-iso 2>&1 | base64 -w0"
4948
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False, strip=False)
5049

51-
5250
if res.exit_code != 0:
5351
self._log_event(
5452
category=EventCategory.OS,
@@ -61,20 +59,17 @@ def _read_with_journalctl(self):
6159
self.result.status = ExecutionStatus.ERROR
6260
return None
6361

64-
out = res.stdout
65-
66-
if isinstance(out, (bytes, bytearray)):
67-
try:
68-
text = out.decode("utf-8")
69-
except UnicodeDecodeError:
70-
text = out.decode("utf-8", errors="replace")
62+
if isinstance(res.stdout, (bytes, bytearray)):
63+
b64 = (
64+
res.stdout if isinstance(res.stdout, str) else res.stdout.decode("ascii", "ignore")
65+
)
66+
raw = base64.b64decode("".join(b64.split()))
67+
text = raw.decode("utf-8", errors="replace")
7168
else:
72-
text = out
69+
text = res.stdout
7370

74-
text = text.replace("\r\n", "\n").replace("\r", "\n").replace("\x00", "")
7571
return text
7672

77-
7873
def collect_data(self, args=None) -> tuple[TaskResult, JournalData | None]:
7974
"""Collect journal logs
8075

nodescraper/plugins/inband/journal/journaldata.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26-
import json
2726
import os
2827

2928
from nodescraper.models import DataModel
@@ -32,7 +31,7 @@
3231
class JournalData(DataModel):
3332
"""Data model for journal logs"""
3433

35-
journal_log: str
34+
journal_log: str
3635

3736
def log_model(self, log_path: str):
3837
"""Log data model to a file
@@ -42,6 +41,4 @@ def log_model(self, log_path: str):
4241
"""
4342
log_name = os.path.join(log_path, "journal.log")
4443
with open(log_name, "w", encoding="utf-8") as log_filename:
45-
for e in self.journal_log:
46-
log_filename.write(json.dumps(e, ensure_ascii=False, separators=(",", ":")))
47-
log_filename.write("\n")
44+
log_filename.write(self.journal_log)

test/unit/plugin/test_journal_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ def run_map(cmd, **kwargs):
6868
result, data = c.collect_data()
6969
assert isinstance(data, JournalData)
7070

71-
assert data.journal_log == [{"MESSAGE": "hello"}]
71+
assert data.journal_log == '{"MESSAGE":"hello"}\n'

0 commit comments

Comments
 (0)