Skip to content

Commit

Permalink
enable example extension + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scoders-tob authored and Smjert committed Nov 14, 2019
1 parent 84022a1 commit d17641a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions osquery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function(osqueryMain)
add_subdirectory("ev2")
add_subdirectory("experimental")
add_subdirectory("system")
add_subdirectory("examples")

generateOsqueryHeaders()
generateOsqueryd()
Expand Down
11 changes: 11 additions & 0 deletions osquery/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(${CMAKE_SOURCE_DIR}/external/cmake/cmakelibs.cmake)

function(osqueryExtensionsExampleMain)
addOsqueryExtension(example_extension example_extension.cpp)
target_link_libraries(example_extension PRIVATE
osquery_sdk_pluginsdk
osquery_extensions_implthrift
)
endfunction()

osqueryExtensionsExampleMain()
11 changes: 6 additions & 5 deletions osquery/examples/example_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* the LICENSE file found in the root directory of this source tree.
*/

#include <osquery/sdk.h>
#include <osquery/sdk/sdk.h>
#include <osquery/sql/dynamic_table_row.h>
#include <osquery/system.h>

using namespace osquery;
Expand Down Expand Up @@ -72,10 +73,10 @@ class ComplexExampleTable : public TablePlugin {
auto r = make_table_row();

// Use the basic 'force' flag to check implicit SQL usage.
auto flags =
SQL("select default_value from osquery_flags where name = 'force'");
if (flags.rows().size() > 0) {
r["flag_test"] = flags.rows().back().at("default_value");
auto flags = SQL::selectFrom(
{"default_value"}, "osquery_flags", "name", EQUALS, "force");
if (flags.size() > 0) {
r["flag_test"] = flags[0]["default_value"];
}

std::string content;
Expand Down
2 changes: 1 addition & 1 deletion tools/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function(generatePythonTests)

# The following tests need to be restored when extensions work again
addPythonTest(NAME tools_tests_testextensions SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/test_extensions.py")
set_tests_properties(tools_tests_testextensions PROPERTIES DISABLED TRUE)
set_tests_properties(tools_tests_testextensions PROPERTIES TIMEOUT 120)
endif()
endfunction()

Expand Down
42 changes: 21 additions & 21 deletions tools/tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_2_daemon_api(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# List the number of extensions
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_3_example_extension(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# Make sure there are no extensions registered
Expand All @@ -95,7 +95,7 @@ def test_3_example_extension(self):
# Now that an extension has started, check extension list
result = test_base.expect(em.extensions, 1)
self.assertEqual(len(result), 1)
ex_uuid = result.keys()[0]
ex_uuid = list(result.keys())[0]
ex_data = result[ex_uuid]
self.assertEqual(ex_data.name, "example")
self.assertEqual(ex_data.version, "0.0.1")
Expand All @@ -104,7 +104,7 @@ def test_3_example_extension(self):
# Get a python-based thrift client to the extension's service
client2 = test_base.EXClient(daemon.options["extensions_socket"],
uuid=ex_uuid)
self.assertTrue(client2.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client2.open())
ex = client2.getEX()
self.assertEqual(ex.ping().code, 0)

Expand Down Expand Up @@ -147,7 +147,7 @@ def test_4_extension_dies(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# Make sure there are no extensions registered
Expand Down Expand Up @@ -207,21 +207,21 @@ def test_5_extension_timeout(self):
# Get a python-based thrift client
client = test_base.EXClient(extension.options["extensions_socket"])
test_base.expectTrue(client.try_open)
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# The waiting extension should have connected to the daemon.
result = test_base.expect(em.extensions, 1)
self.assertEqual(len(result), 1)

client.close()
daemon.kill(True)
daemon.kill()
extension.kill()

@test_base.flaky
def test_6_extensions_autoload(self):
loader = test_base.Autoloader(
[test_base.ARGS.build + "/osquery/example_extension.ext"])
[test_base.BUILD_DIR + "/osquery/examples/example_extension.ext"])
daemon = self._run_daemon({
"disable_watchdog": True,
"extensions_timeout": EXTENSION_TIMEOUT,
Expand All @@ -231,7 +231,7 @@ def test_6_extensions_autoload(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# The waiting extension should have connected to the daemon.
Expand All @@ -243,7 +243,7 @@ def test_6_extensions_autoload(self):

@test_base.flaky
def test_6_extensions_directory_autoload(self):
utils.copy_file(test_base.ARGS.build + "/osquery/example_extension.ext",
utils.copy_file(test_base.BUILD_DIR + "/osquery/examples/example_extension.ext",
test_base.CONFIG_DIR)
loader = test_base.Autoloader([test_base.CONFIG_DIR])
daemon = self._run_daemon({
Expand All @@ -255,7 +255,7 @@ def test_6_extensions_directory_autoload(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# The waiting extension should have connected to the daemon.
Expand All @@ -268,7 +268,7 @@ def test_6_extensions_directory_autoload(self):
@test_base.flaky
def test_7_extensions_autoload_watchdog(self):
loader = test_base.Autoloader(
[test_base.ARGS.build + "/osquery/example_extension.ext"])
[test_base.BUILD_DIR + "/osquery/examples/example_extension.ext"])
daemon = self._run_daemon({
"extensions_timeout": EXTENSION_TIMEOUT,
"extensions_autoload": loader.path,
Expand All @@ -277,7 +277,7 @@ def test_7_extensions_autoload_watchdog(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# The waiting extension should have connected to the daemon.
Expand All @@ -290,7 +290,7 @@ def test_7_extensions_autoload_watchdog(self):
@test_base.flaky
def test_8_external_config(self):
loader = test_base.Autoloader(
[test_base.ARGS.build + "/osquery/example_extension.ext"])
[test_base.BUILD_DIR + "/osquery/examples/example_extension.ext"])
daemon = self._run_daemon({
"extensions_autoload": loader.path,
"extensions_timeout": EXTENSION_TIMEOUT,
Expand All @@ -300,7 +300,7 @@ def test_8_external_config(self):

# Get a python-based thrift client
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# The waiting extension should have connected to the daemon.
Expand Down Expand Up @@ -328,17 +328,17 @@ def test_9_external_config_update(self):
# Get a python-based thrift client to the manager and extension.
client = test_base.EXClient(extension.options["extensions_socket"])
test_base.expectTrue(client.try_open)
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# Need the manager to request the extension's UUID.
result = test_base.expect(em.extensions, 1)
self.assertTrue(result is not None)
ex_uuid = result.keys()[0]
self.assertTrue(len(result), 1)
ex_uuid = list(result.keys())[0]
client2 = test_base.EXClient(extension.options["extensions_socket"],
uuid=ex_uuid)
test_base.expectTrue(client2.try_open)
self.assertTrue(client2.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client2.open())
ex = client2.getEX()

# Trigger an async update from the extension.
Expand All @@ -362,7 +362,7 @@ def test_9_external_config_update(self):
@test_base.flaky
def test_91_extensions_settings(self):
loader = test_base.Autoloader(
[test_base.ARGS.build + "/osquery/example_extension.ext"])
[test_base.BUILD_DIR + "/osquery/examples/example_extension.ext"])
daemon = self._run_daemon({
"disable_watchdog": True,
"extensions_timeout": EXTENSION_TIMEOUT,
Expand All @@ -372,7 +372,7 @@ def test_91_extensions_settings(self):

# Get a python-based thrift client for the manager (core).
client = test_base.EXClient(daemon.options["extensions_socket"])
self.assertTrue(client.open(timeout=EXTENSION_TIMEOUT))
self.assertTrue(client.open())
em = client.getEM()

# The waiting extension should have connected to the daemon.
Expand Down

0 comments on commit d17641a

Please sign in to comment.