Skip to content

Commit 2ad42e3

Browse files
authored
Added Container Runtime Interface (CRI) text parser plugin (#4742)
1 parent 38fa61b commit 2ad42e3

File tree

7 files changed

+220
-1
lines changed

7 files changed

+220
-1
lines changed

plaso/data/formatters/generic.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ short_source: 'LOG'
141141
source: 'Confluence access log'
142142
---
143143
type: 'conditional'
144+
data_type: 'cri:container:log:entry'
145+
message:
146+
- 'Stream: {stream}'
147+
- 'Tag: {tag}'
148+
- 'Body: {body}'
149+
short_message:
150+
- 'Stream: {stream}'
151+
- 'Body: {body}'
152+
short_source: 'CRI'
153+
source: 'Container Runtime Interface Container Log'
154+
---
155+
type: 'conditional'
144156
data_type: 'cups:ipp:event'
145157
message:
146158
- 'Status: {status}'

plaso/data/timeliner.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ attribute_mappings:
281281
description: 'Last Visited Time'
282282
place_holder_event: true
283283
---
284+
data_type: 'cri:container:log:entry'
285+
attribute_mappings:
286+
- name: 'event_datetime'
287+
description: 'Content Modification Time'
288+
place_holder_event: true
289+
---
284290
data_type: 'cups:ipp:event'
285291
attribute_mappings:
286292
- name: 'creation_time'

plaso/parsers/text_plugins/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from plaso.parsers.text_plugins import aws_elb_access
88
from plaso.parsers.text_plugins import bash_history
99
from plaso.parsers.text_plugins import confluence_access
10+
from plaso.parsers.text_plugins import cri
1011
from plaso.parsers.text_plugins import dpkg
1112
from plaso.parsers.text_plugins import gdrive_synclog
1213
from plaso.parsers.text_plugins import google_logging

plaso/parsers/text_plugins/cri.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -*- coding: utf-8 -*-
2+
"""Text file parser plugin for Container Runtime Interface (CRI) log format.
3+
4+
This is a text-based log format used in kubernetes/GKE.
5+
6+
Also see:
7+
https://github.com/kubernetes/design-proposals-archive/blob/main/node/kubelet-cri-logging.md
8+
"""
9+
10+
import pyparsing
11+
12+
from dfdatetime import time_elements
13+
14+
from plaso.containers import events
15+
from plaso.lib import errors
16+
from plaso.parsers import text_parser
17+
from plaso.parsers.text_plugins import interface
18+
19+
20+
class CRIEventData(events.EventData):
21+
"""CRI log event data.
22+
23+
Attributes:
24+
body (str): the log message body.
25+
event_datetime (time_elements.TimeElementsInNanoseconds): the datetime of
26+
the log message.
27+
stream (str): the log stream. Currently only 'stdout' and 'stderr' are
28+
supported.
29+
tag (str): the log tag. Currently only 'P' (partial) and 'F' (full) are
30+
supported.
31+
"""
32+
DATA_TYPE = 'cri:container:log:entry'
33+
34+
def __init__(self):
35+
"""Initializes event data."""
36+
super(CRIEventData, self).__init__(data_type=self.DATA_TYPE)
37+
self.body = None
38+
self.event_datetime = None
39+
self.stream = None
40+
self.tag = None
41+
42+
43+
class CRITextPlugin(interface.TextPlugin):
44+
"""Text file parser plugin for CRI log files."""
45+
46+
NAME = 'cri_log'
47+
DATA_FORMAT = 'Container Runtime Interface log file'
48+
49+
ENCODING = 'utf-8'
50+
51+
# Date and time values are formatted as: 2016-10-06T00:17:09.669794202Z
52+
_DATE_AND_TIME = (
53+
pyparsing.Regex(r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,9}Z')
54+
).setResultsName('date_time')
55+
56+
_STREAM = (
57+
pyparsing.Literal('stderr') ^ pyparsing.Literal('stdout')
58+
).setResultsName('stream')
59+
60+
# P indicates a partial log,
61+
# F indicates a complete or the end of a multiline log.
62+
_TAG = pyparsing.oneOf(['P', 'F']).setResultsName('tag')
63+
64+
_LOG = (
65+
pyparsing.restOfLine() + pyparsing.Suppress(pyparsing.LineEnd())
66+
).setResultsName('body')
67+
68+
_LOG_LINE = _DATE_AND_TIME + _STREAM + _TAG + _LOG
69+
_LINE_STRUCTURES = [('log_line', _LOG_LINE)]
70+
71+
VERIFICATION_GRAMMAR = _LOG_LINE
72+
73+
def _ParseRecord(self, parser_mediator, key, structure):
74+
"""Parses a pyparsing structure.
75+
76+
Args:
77+
parser_mediator (ParserMediator): mediates interactions between parsers
78+
and other components, such as storage and dfVFS.
79+
key (str): name of the parsed structure.
80+
structure (pyparsing.ParseResults): tokens from a parsed log line.
81+
82+
Raises:
83+
ParseError: if the structure cannot be parsed.
84+
"""
85+
if key == 'log_line':
86+
date_time = time_elements.TimeElementsInNanoseconds()
87+
date_time.CopyFromStringISO8601(self._GetValueFromStructure(
88+
structure, 'date_time'))
89+
event_data = CRIEventData()
90+
event_data.event_datetime = date_time
91+
event_data.body = self._GetValueFromStructure(
92+
structure, 'body')[0]
93+
event_data.stream = self._GetValueFromStructure(structure, 'stream')
94+
event_data.tag = self._GetValueFromStructure(structure, 'tag')
95+
parser_mediator.ProduceEventData(event_data)
96+
97+
def CheckRequiredFormat(self, parser_mediator, text_reader):
98+
"""Check if the log record has the minimal structure required by the parser.
99+
100+
Args:
101+
parser_mediator (ParserMediator): mediates interactions between parsers
102+
and other components, such as storage and dfVFS.
103+
text_reader (EncodedTextReader): text reader.
104+
105+
Returns:
106+
bool: True if this is the correct parser, False otherwise.
107+
"""
108+
try:
109+
self._VerifyString(text_reader.lines)
110+
except errors.ParseError:
111+
return False
112+
113+
return True
114+
115+
116+
text_parser.TextLogParser.RegisterPlugin(CRITextPlugin)

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ bencode.py
66
certifi >= 2016.9.26
77
cffi >= 1.9.1
88
defusedxml >= 0.5.0
9-
dfdatetime >= 20240330
9+
dfdatetime >= 20240504
1010
dfvfs >= 20240115
1111
dfwinreg >= 20240229
1212
dtfabric >= 20230518

test_data/cri.log

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2016-10-06T00:17:09.669794202Z stdout P log content 1
2+
2016-10-06T00:17:09.669794203Z stderr F log content 2
3+
2024-04-16T06:21:30.494902976Z stderr F 2024/04/16 06:21:30 Starting server...
4+
2024-04-16T06:21:30.49495227Z stderr F 2024/04/16 06:21:30 Health service listening on 0.0.0.0:81
5+
2024-04-16T06:21:30.494958332Z stderr F 2024/04/16 06:21:30 HTTP service listening on 0.0.0.0:80
6+
2024-04-16T06:21:39.0Z stdout F 10.0.2.1:59838 - - [Tue, 16 Apr 2024 06:21:39 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
7+
2024-04-16T06:21:44.09Z stdout F 10.0.2.1:57922 - - [Tue, 16 Apr 2024 06:21:44 UTC] "GET /healthz HTTP/1.1" kube-probe/1.27
8+
2024-04-16T06:23:05.887Z stdout F 10.0.2.15:37674 - - [Tue, 16 Apr 2024 06:23:05 UTC] "GET / HTTP/1.0" Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
9+
2024-04-16T06:23:44.0952Z stdout F 10.0.2.1:47888 - - [Tue, 16 Apr 2024 06:23:44 UTC] "GET /healthz HTTP/1.1" kube-probe/1.27
10+
2024-04-16T06:24:29.09466Z stdout F 10.0.2.1:36006 - - [Tue, 16 Apr 2024 06:24:29 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
11+
2024-04-16T06:24:39.095116Z stdout F 10.0.2.1:50862 - - [Tue, 16 Apr 2024 06:24:39 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
12+
2024-04-16T06:24:44.09475473Z stdout F 10.0.2.1:34564 - - [Tue, 16 Apr 2024 06:24:44 UTC] "GET /healthz HTTP/1.1" kube-probe/1.27
13+
2024-04-16T06:24:49.09457166Z stdout F 10.0.2.1:34574 - - [Tue, 16 Apr 2024 06:24:49 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
14+
2024-04-16T06:24:59.094443628Z stdout F 10.0.2.1:48854 - - [Tue, 16 Apr 2024 06:24:59 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
15+
2024-04-16T06:25:29.09520786Z stdout F 10.0.2.1:39914 - - [Tue, 16 Apr 2024 06:25:29 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
16+
2024-04-16T06:25:39.094238892Z stdout F 10.0.2.1:45586 - - [Tue, 16 Apr 2024 06:25:39 UTC] "GET /readiness HTTP/1.1" kube-probe/1.27
17+
2024-04-16T06:25:44.094383157Z stdout F 10.0.2.1:38936 - - [Tue, 16 Apr 2024 06:25:44 UTC] "GET /healthz HTTP/1.1" kube-probe/1.27

tests/parsers/text_plugins/cri.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""Tests for the Container Runtime Interface (CRI) log text plugin."""
4+
5+
import unittest
6+
7+
from plaso.parsers.text_plugins import cri
8+
9+
from tests.parsers.text_plugins import test_lib
10+
11+
12+
class CRILogTextPluginTest(test_lib.TextPluginTestCase):
13+
"""Tests for the CRI log text parser plugin."""
14+
15+
def testProcess(self):
16+
"""Tests for the CheckRequiredFormat method."""
17+
plugin = cri.CRITextPlugin()
18+
storage_writer = self._ParseTextFileWithPlugin(
19+
['cri.log'], plugin)
20+
21+
number_of_event_data = storage_writer.GetNumberOfAttributeContainers(
22+
'event_data')
23+
self.assertEqual(number_of_event_data, 17)
24+
25+
number_of_warnings = storage_writer.GetNumberOfAttributeContainers(
26+
'extraction_warning')
27+
self.assertEqual(number_of_warnings, 0)
28+
29+
number_of_warnings = storage_writer.GetNumberOfAttributeContainers(
30+
'recovery_warning')
31+
self.assertEqual(number_of_warnings, 0)
32+
33+
expected_event_values = {
34+
'data_type': 'cri:container:log:entry',
35+
'event_datetime': '2016-10-06T00:17:09.669794202+00:00',
36+
'body': ' log content 1',
37+
'stream': 'stdout',
38+
'tag': 'P'}
39+
40+
event_data = storage_writer.GetAttributeContainerByIndex('event_data', 0)
41+
self.CheckEventData(event_data, expected_event_values)
42+
43+
expected_event_values = {
44+
'data_type': 'cri:container:log:entry',
45+
'event_datetime': '2016-10-06T00:17:09.669794203+00:00',
46+
'body': ' log content 2',
47+
'stream': 'stderr',
48+
'tag': 'F'}
49+
50+
event_data = storage_writer.GetAttributeContainerByIndex('event_data', 1)
51+
self.CheckEventData(event_data, expected_event_values)
52+
53+
expected_event_values = {
54+
'data_type': 'cri:container:log:entry',
55+
'event_datetime': '2024-04-16T06:25:29.095207860+00:00',
56+
'body': (
57+
' 10.0.2.1:39914 - - [Tue, 16 Apr 2024 06:25:29 UTC] '
58+
'"GET /readiness HTTP/1.1" kube-probe/1.27'),
59+
'stream': 'stdout',
60+
'tag': 'F'}
61+
62+
event_data = storage_writer.GetAttributeContainerByIndex('event_data', 14)
63+
self.CheckEventData(event_data, expected_event_values)
64+
65+
66+
if __name__ == '__main__':
67+
unittest.main()

0 commit comments

Comments
 (0)