Skip to content

Commit

Permalink
Fix the santa log parsing tests
Browse files Browse the repository at this point in the history
Remove the filebeat contrib app dependency
  • Loading branch information
np5 committed Sep 18, 2019
1 parent 3e6857c commit 2b8b08b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion tests/santa/test_santa_log_parsing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from dateutil.tz.tz import tzlocal
from django.test import TestCase
from zentral.contrib.santa.preprocessors.log import parse_santa_log_message
from zentral.contrib.santa.utils import parse_santa_log_message


LOG1 = ("[2019-09-16T13:53:17.187Z] I santad: action=EXEC|decision=ALLOW|reason=CERT|"
Expand Down
47 changes: 1 addition & 46 deletions zentral/contrib/santa/preprocessors/log.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,15 @@
import json
import logging
from dateutil import parser
from zentral.contrib.filebeat.utils import (get_serial_number_from_raw_event,
get_user_agent_and_ip_address_from_raw_event)
from zentral.contrib.santa.events import SantaLogEvent
from zentral.contrib.santa.utils import parse_santa_log_message
from zentral.utils.json import save_dead_letter


logger = logging.getLogger("zentral.contrib.santa.preprocessors.log")


def parse_santa_log_message(message):
d = {}
current_attr = ""
current_val = ""
state = None
for c in message:
if state is None:
if c == "[":
current_attr = "timestamp"
state = "VAL"
elif c == ":":
state = "ATTR"
current_attr = ""
elif state == "ATTR":
if c == "=":
state = "VAL"
elif current_attr or c != " ":
current_attr += c
elif state == "VAL":
if c == "|" or (current_attr == "timestamp" and c == "]"):
if c == "|":
state = "ATTR"
elif c == "]":
state = None
if current_attr == "timestamp":
current_val = parser.parse(current_val)
d[current_attr] = current_val
current_attr = ""
current_val = ""
else:
current_val += c
if current_attr and current_val:
d[current_attr] = current_val
for attr, val in d.items():
if attr.endswith("id"):
try:
d[attr] = int(val)
except ValueError:
pass
args = d.get("args")
if args:
d["args"] = args.split()
return d


class SantaLogPreprocessor(object):
routing_key = "santa_logs"

Expand Down
46 changes: 46 additions & 0 deletions zentral/contrib/santa/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import plistlib
from dateutil import parser
from zentral.conf import settings
from zentral.utils.osx_package import distribute_tls_server_certs, TLS_SERVER_CERTS_CLIENT_PATH
from zentral.utils.payloads import generate_payload_uuid, get_payload_identifier, sign_payload_openssl
Expand Down Expand Up @@ -50,3 +51,48 @@ def build_configuration_profile(enrolled_machine):

content = sign_payload_openssl(plistlib.dumps(configuration_profile_data))
return "com.google.santa.zentral.mobileconfig", content


def parse_santa_log_message(message):
d = {}
current_attr = ""
current_val = ""
state = None
for c in message:
if state is None:
if c == "[":
current_attr = "timestamp"
state = "VAL"
elif c == ":":
state = "ATTR"
current_attr = ""
elif state == "ATTR":
if c == "=":
state = "VAL"
elif current_attr or c != " ":
current_attr += c
elif state == "VAL":
if c == "|" or (current_attr == "timestamp" and c == "]"):
if c == "|":
state = "ATTR"
elif c == "]":
state = None
if current_attr == "timestamp":
current_val = parser.parse(current_val)
d[current_attr] = current_val
current_attr = ""
current_val = ""
else:
current_val += c
if current_attr and current_val:
d[current_attr] = current_val
for attr, val in d.items():
if attr.endswith("id"):
try:
d[attr] = int(val)
except ValueError:
pass
args = d.get("args")
if args:
d["args"] = args.split()
return d

0 comments on commit 2b8b08b

Please sign in to comment.