Skip to content

Commit 190445d

Browse files
committed
Use raw events for regular Osquery machine snapshot updates
1 parent d9630b7 commit 190445d

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

tests/osquery/test_osquery_api.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from zentral.conf import settings
1111
from zentral.contrib.inventory.events import MachineTagEvent
1212
from zentral.contrib.inventory.models import EnrollmentSecret, MachineSnapshot, MachineTag, MetaBusinessUnit, Tag
13+
from zentral.contrib.inventory.utils import commit_machine_snapshot_and_trigger_events
1314
from zentral.contrib.osquery.compliance_checks import sync_query_compliance_check
1415
from zentral.contrib.osquery.conf import INVENTORY_QUERY_NAME
1516
from zentral.contrib.osquery.events import (OsqueryEnrollmentEvent, OsqueryRequestEvent, OsqueryResultEvent,
@@ -273,24 +274,29 @@ def post_default_inventory_query_snapshot(
273274
missing_windows_build_data=False,
274275
unknown_windows_build=False,
275276
):
276-
return self.post_as_json(
277-
"log",
278-
{"node_key": node_key,
279-
"log_type": "result",
280-
"data": [{
281-
'action': 'snapshot',
282-
"name": INVENTORY_QUERY_NAME,
283-
"snapshot": self.get_default_inventory_query_snapshot(
284-
platform,
285-
with_app,
286-
with_ec2,
287-
no_windows_build_data,
288-
missing_windows_build_data,
289-
unknown_windows_build,
290-
),
291-
'unixTime': '1480605737',
292-
}]}
293-
)
277+
with patch("zentral.contrib.osquery.public_views.post_machine_snapshot_raw_event") as pmsre:
278+
def store_mstree(ms_tree):
279+
# simulate what is done by the preprocessor
280+
commit_machine_snapshot_and_trigger_events(ms_tree)
281+
pmsre.side_effect = store_mstree
282+
return self.post_as_json(
283+
"log",
284+
{"node_key": node_key,
285+
"log_type": "result",
286+
"data": [{
287+
'action': 'snapshot',
288+
"name": INVENTORY_QUERY_NAME,
289+
"snapshot": self.get_default_inventory_query_snapshot(
290+
platform,
291+
with_app,
292+
with_ec2,
293+
no_windows_build_data,
294+
missing_windows_build_data,
295+
unknown_windows_build,
296+
),
297+
'unixTime': '1480605737',
298+
}]}
299+
)
294300

295301
# enrollment
296302

zentral/contrib/osquery/public_views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.http import Http404, JsonResponse
1111
from django.utils.crypto import get_random_string
1212
from django.views.generic import View
13+
from zentral.contrib.inventory.events import post_machine_snapshot_raw_event
1314
from zentral.contrib.inventory.exceptions import EnrollmentSecretVerificationFailed
1415
from zentral.contrib.inventory.models import MachineSnapshot, MetaMachine
1516
from zentral.contrib.inventory.utils import (add_machine_tags,
@@ -144,6 +145,7 @@ def do_post(self):
144145
if business_unit:
145146
tree["business_unit"] = business_unit.serialize()
146147
update_tree_with_enrollment_host_details(tree, self.data.get("host_details"))
148+
# commit to push the extra inventory queries quickly
147149
commit_machine_snapshot_and_trigger_events(tree)
148150

149151
post_enrollment_event(self.serial_number,
@@ -484,7 +486,8 @@ def do_node_post(self):
484486
if business_unit:
485487
tree["business_unit"] = business_unit.serialize()
486488
update_tree_with_inventory_query_snapshot(tree, last_inventory_snapshot)
487-
commit_machine_snapshot_and_trigger_events(tree)
489+
# use the raw events queue to process this in the background
490+
post_machine_snapshot_raw_event(tree)
488491
post_results(self.machine.serial_number, results, self.request)
489492
elif log_type == "status":
490493
# TODO: configuration option to filter some of those (severity) or maybe simply ignore them

0 commit comments

Comments
 (0)