Skip to content

Commit 769e8a5

Browse files
author
Teddy Reed
authored
table plugin: Convert all values to strings (#36)
1 parent bbb3263 commit 769e8a5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

osquery/table_plugin.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from abc import ABCMeta, abstractmethod
1414
from collections import namedtuple
1515
import json
16+
import logging
1617

1718
from osquery.extensions.ttypes import ExtensionResponse, ExtensionStatus
1819
from osquery.plugin import BasePlugin
@@ -38,13 +39,24 @@ def call(self, context):
3839
ctx = {}
3940
if "context" in context:
4041
ctx = json.dumps(context["context"])
42+
rows = self.generate(ctx)
43+
for i, row in enumerate(rows):
44+
for key, value in row.items():
45+
if not isinstance(value, basestring):
46+
try:
47+
rows[i][key] = str(value)
48+
except ValueError as e:
49+
rows[i][key] = ''
50+
logging.error("Cannot convert key %s: %s" % (
51+
i, key, str(e)))
4152
return ExtensionResponse(
4253
status=ExtensionStatus(code=0, message="OK",),
43-
response=self.generate(ctx),)
54+
response=rows)
4455
elif context["action"] == "columns":
4556
return ExtensionResponse(
4657
status=ExtensionStatus(code=0, message="OK",),
4758
response=self.routes(),)
59+
return ExtensionResponse(code=1, message="Unknown action",)
4860

4961
def registry_name(self):
5062
"""The name of the registry type for table plugins.

tests/test_table_plugin.py

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def columns(self):
2626
return [
2727
osquery.TableColumn(name="foo", type=osquery.STRING),
2828
osquery.TableColumn(name="baz", type=osquery.STRING),
29+
osquery.TableColumn(name="int", type=osquery.INTEGER),
2930
]
3031

3132
def generate(self, context):
@@ -35,6 +36,7 @@ def generate(self, context):
3536
row = {}
3637
row["foo"] = "bar"
3738
row["baz"] = "baz"
39+
row["int"] = 42
3840
query_data.append(row)
3941

4042
return query_data
@@ -64,6 +66,12 @@ def test_routes_are_correct(self):
6466
"type": "TEXT",
6567
"name": "baz",
6668
},
69+
{
70+
"id": "column",
71+
"op": "0",
72+
"type": "INTEGER",
73+
"name": "int",
74+
},
6775
]
6876
osquery.ExtensionManager().add_plugin(MockTablePlugin)
6977
mtp = MockTablePlugin()
@@ -77,10 +85,12 @@ def test_simple_call(self):
7785
{
7886
"foo": "bar",
7987
"baz": "baz",
88+
"int": "42",
8089
},
8190
{
8291
"foo": "bar",
8392
"baz": "baz",
93+
"int": "42",
8494
},
8595
]
8696
self.assertEqual(results.response, expected)

0 commit comments

Comments
 (0)