dissect.target 3.20.dev32__py3-none-any.whl → 3.20.dev33__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,13 @@
1
+ from __future__ import annotations
2
+
1
3
  import re
4
+ from datetime import datetime, timezone, tzinfo
2
5
  from pathlib import Path
3
6
  from typing import Iterator
4
7
 
5
8
  from dissect.target import Target
6
9
  from dissect.target.exceptions import UnsupportedPluginError
10
+ from dissect.target.helpers.fsutil import open_decompress
7
11
  from dissect.target.helpers.record import TargetRecordDescriptor
8
12
  from dissect.target.helpers.utils import year_rollover_helper
9
13
  from dissect.target.plugin import Plugin, alias, export
@@ -66,7 +70,7 @@ class MessagesPlugin(Plugin):
66
70
 
67
71
  for log_file in self.log_files:
68
72
  if "cloud-init" in log_file.name:
69
- yield from self._parse_cloud_init_log(log_file)
73
+ yield from self._parse_cloud_init_log(log_file, tzinfo)
70
74
  continue
71
75
 
72
76
  for ts, line in year_rollover_helper(log_file, RE_TS, DEFAULT_TS_LOG_FORMAT, tzinfo):
@@ -83,7 +87,7 @@ class MessagesPlugin(Plugin):
83
87
  _target=self.target,
84
88
  )
85
89
 
86
- def _parse_cloud_init_log(self, log_file: Path) -> Iterator[MessagesRecord]:
90
+ def _parse_cloud_init_log(self, log_file: Path, tzinfo: tzinfo | None = timezone.utc) -> Iterator[MessagesRecord]:
87
91
  """Parse a cloud-init.log file.
88
92
 
89
93
  Lines are structured in the following format:
@@ -96,18 +100,41 @@ class MessagesPlugin(Plugin):
96
100
 
97
101
  Returns: ``MessagesRecord``
98
102
  """
99
- for line in log_file.open("rt").readlines():
100
- if line := line.strip():
101
- if match := RE_CLOUD_INIT_LINE.match(line):
102
- match = match.groupdict()
103
- yield MessagesRecord(
104
- ts=match["ts"].split(",")[0],
105
- daemon=match["daemon"],
106
- pid=None,
107
- message=match["message"],
108
- source=log_file,
109
- _target=self.target,
110
- )
111
- else:
112
- self.target.log.warning("Could not match cloud-init log line")
103
+
104
+ ts_fmt = "%Y-%m-%d %H:%M:%S,%f"
105
+
106
+ with open_decompress(log_file, "rt") as fh:
107
+ for line in fh:
108
+ if not (line := line.strip()):
109
+ continue
110
+
111
+ if not (match := RE_CLOUD_INIT_LINE.match(line)):
112
+ self.target.log.warning("Could not match cloud-init log line in file: %s", log_file)
113
113
  self.target.log.debug("No match for line '%s'", line)
114
+ continue
115
+
116
+ values = match.groupdict()
117
+
118
+ # Actual format is ``YYYY-MM-DD HH:MM:SS,000`` (asctime with milliseconds) but python has no strptime
119
+ # operator for 3 digit milliseconds, so we convert and pad to six digit microseconds.
120
+ # https://github.com/canonical/cloud-init/blob/main/cloudinit/log/loggers.py#DEFAULT_LOG_FORMAT
121
+ # https://docs.python.org/3/library/logging.html#asctime
122
+ raw_ts, _, milliseconds = values["ts"].rpartition(",")
123
+ raw_ts += "," + str((int(milliseconds) * 1000)).zfill(6)
124
+
125
+ try:
126
+ ts = datetime.strptime(raw_ts, ts_fmt).replace(tzinfo=tzinfo)
127
+
128
+ except ValueError as e:
129
+ self.target.log.warning("Timestamp '%s' does not match format '%s'", raw_ts, ts_fmt)
130
+ self.target.log.debug("", exc_info=e)
131
+ ts = datetime(1970, 1, 1, 0, 0, 0, 0)
132
+
133
+ yield MessagesRecord(
134
+ ts=ts,
135
+ daemon=values["daemon"],
136
+ pid=None,
137
+ message=values["message"],
138
+ source=log_file,
139
+ _target=self.target,
140
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dissect.target
3
- Version: 3.20.dev32
3
+ Version: 3.20.dev33
4
4
  Summary: This module ties all other Dissect modules together, it provides a programming API and command line tools which allow easy access to various data sources inside disk images or file collections (a.k.a. targets)
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Affero General Public License v3
@@ -264,7 +264,7 @@ dissect/target/plugins/os/unix/log/audit.py,sha256=OjorWTmCFvCI5RJq6m6WNW0Lhb-po
264
264
  dissect/target/plugins/os/unix/log/auth.py,sha256=l7gCuRdvv9gL0U1N0yrR9hVsMnr4t_k4t-n-f6PrOxg,2388
265
265
  dissect/target/plugins/os/unix/log/journal.py,sha256=xe8p8MM_95uYjFNzNSP5IsoIthJtxwFEDicYR42RYAI,17681
266
266
  dissect/target/plugins/os/unix/log/lastlog.py,sha256=Wq89wRSFZSBsoKVCxjDofnC4yw9XJ4iOF0XJe9EucCo,2448
267
- dissect/target/plugins/os/unix/log/messages.py,sha256=O10Uw3PGTanfGpphUWYqOwOIR7XiiM-clfboVCoiP0U,4501
267
+ dissect/target/plugins/os/unix/log/messages.py,sha256=1-GsvubgGffwOYz0GPG00PkEFALu3VwkyoFLEhhJxuQ,5695
268
268
  dissect/target/plugins/os/unix/log/utmp.py,sha256=1nPHIaBUHt_9z6PDrvyqg4huKLihUaWLrMmgMsbaeIo,7755
269
269
  dissect/target/plugins/os/windows/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
270
270
  dissect/target/plugins/os/windows/_os.py,sha256=-Bsp9696JqU7luh_AbqojzG9BxVdYIFl5Ma-LiFBQBo,12505
@@ -370,10 +370,10 @@ dissect/target/volumes/luks.py,sha256=OmCMsw6rCUXG1_plnLVLTpsvE1n_6WtoRUGQbpmu1z
370
370
  dissect/target/volumes/lvm.py,sha256=wwQVR9I3G9YzmY6UxFsH2Y4MXGBcKL9aayWGCDTiWMU,2269
371
371
  dissect/target/volumes/md.py,sha256=7ShPtusuLGaIv27SvEETtgsuoQyAa4iAAeOR1NEaajI,1689
372
372
  dissect/target/volumes/vmfs.py,sha256=-LoUbn9WNwTtLi_4K34uV_-wDw2W5hgaqxZNj4UmqAQ,1730
373
- dissect.target-3.20.dev32.dist-info/COPYRIGHT,sha256=m-9ih2RVhMiXHI2bf_oNSSgHgkeIvaYRVfKTwFbnJPA,301
374
- dissect.target-3.20.dev32.dist-info/LICENSE,sha256=DZak_2itbUtvHzD3E7GNUYSRK6jdOJ-GqncQ2weavLA,34523
375
- dissect.target-3.20.dev32.dist-info/METADATA,sha256=SuQ-1t2xvit888n0TOA-4zCj7Bc1BdYAi-hC1BqvJhs,12897
376
- dissect.target-3.20.dev32.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
377
- dissect.target-3.20.dev32.dist-info/entry_points.txt,sha256=BWuxAb_6AvUAQpIQOQU0IMTlaF6TDht2AIZK8bHd-zE,492
378
- dissect.target-3.20.dev32.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
379
- dissect.target-3.20.dev32.dist-info/RECORD,,
373
+ dissect.target-3.20.dev33.dist-info/COPYRIGHT,sha256=m-9ih2RVhMiXHI2bf_oNSSgHgkeIvaYRVfKTwFbnJPA,301
374
+ dissect.target-3.20.dev33.dist-info/LICENSE,sha256=DZak_2itbUtvHzD3E7GNUYSRK6jdOJ-GqncQ2weavLA,34523
375
+ dissect.target-3.20.dev33.dist-info/METADATA,sha256=uD4GaxEUoWZL5H_-CPfby5_dO5AtF9FcMXqIAESZIUI,12897
376
+ dissect.target-3.20.dev33.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
377
+ dissect.target-3.20.dev33.dist-info/entry_points.txt,sha256=BWuxAb_6AvUAQpIQOQU0IMTlaF6TDht2AIZK8bHd-zE,492
378
+ dissect.target-3.20.dev33.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
379
+ dissect.target-3.20.dev33.dist-info/RECORD,,