Skip to content

Commit

Permalink
table plugin: Convert all values to strings (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy Reed authored May 29, 2017
1 parent bbb3263 commit 769e8a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion osquery/table_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from abc import ABCMeta, abstractmethod
from collections import namedtuple
import json
import logging

from osquery.extensions.ttypes import ExtensionResponse, ExtensionStatus
from osquery.plugin import BasePlugin
Expand All @@ -38,13 +39,24 @@ def call(self, context):
ctx = {}
if "context" in context:
ctx = json.dumps(context["context"])
rows = self.generate(ctx)
for i, row in enumerate(rows):
for key, value in row.items():
if not isinstance(value, basestring):
try:
rows[i][key] = str(value)
except ValueError as e:
rows[i][key] = ''
logging.error("Cannot convert key %s: %s" % (
i, key, str(e)))
return ExtensionResponse(
status=ExtensionStatus(code=0, message="OK",),
response=self.generate(ctx),)
response=rows)
elif context["action"] == "columns":
return ExtensionResponse(
status=ExtensionStatus(code=0, message="OK",),
response=self.routes(),)
return ExtensionResponse(code=1, message="Unknown action",)

def registry_name(self):
"""The name of the registry type for table plugins.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_table_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def columns(self):
return [
osquery.TableColumn(name="foo", type=osquery.STRING),
osquery.TableColumn(name="baz", type=osquery.STRING),
osquery.TableColumn(name="int", type=osquery.INTEGER),
]

def generate(self, context):
Expand All @@ -35,6 +36,7 @@ def generate(self, context):
row = {}
row["foo"] = "bar"
row["baz"] = "baz"
row["int"] = 42
query_data.append(row)

return query_data
Expand Down Expand Up @@ -64,6 +66,12 @@ def test_routes_are_correct(self):
"type": "TEXT",
"name": "baz",
},
{
"id": "column",
"op": "0",
"type": "INTEGER",
"name": "int",
},
]
osquery.ExtensionManager().add_plugin(MockTablePlugin)
mtp = MockTablePlugin()
Expand All @@ -77,10 +85,12 @@ def test_simple_call(self):
{
"foo": "bar",
"baz": "baz",
"int": "42",
},
{
"foo": "bar",
"baz": "baz",
"int": "42",
},
]
self.assertEqual(results.response, expected)
Expand Down

0 comments on commit 769e8a5

Please sign in to comment.