Skip to content

Commit

Permalink
extensions: Always compile examples to prevent them from breaking (#6747
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alessandrogario committed Nov 11, 2020
1 parent da62106 commit ffa3da4
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 194 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ cscope.out

# Ignored components
/external/*
!/external/examples
!/external/cmake/cmakelibs.cmake
!/external/CMakeLists.txt
18 changes: 18 additions & 0 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function(subdirlist output path)
file(GLOB children RELATIVE ${path} "${path}/*")
set(DIRS "")
foreach(child ${children})
if("${child}" STREQUAL "examples")
continue()
endif()

get_filename_component(full_child "${path}/${child}" REALPATH)
if(IS_DIRECTORY "${full_child}")
list(APPEND DIRS "${path}/${child}")
Expand All @@ -38,7 +42,21 @@ function(subdirlist output path)
set(${output} ${DIRS} PARENT_SCOPE)
endfunction()

function(externalsExamples)
set(example_name_list
"config_plugin"
"read_only_table"
"writable_table"
)

foreach(example_name ${example_name_list})
add_subdirectory("examples/${example_name}")
endforeach()
endfunction()

function(externalsMain)
externalsExamples()

# Discover the implementation for extensions or modules in external directory
subdirlist(EXTERNAL_PROJECTS ${EXTERNALS_DIR})

Expand Down
13 changes: 13 additions & 0 deletions external/examples/config_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2014-present, The osquery authors
#
# This source code is licensed as defined by the LICENSE file found in the
# root directory of this source tree.
#
# SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)

project("config_plugin_extension")

addOsqueryExtension(
"${PROJECT_NAME}"
main.cpp
)
43 changes: 43 additions & 0 deletions external/examples/config_plugin/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/

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

using namespace osquery;

class ExampleConfigPlugin : public ConfigPlugin {
public:
Status setUp() {
LOG(WARNING) << "ExampleConfigPlugin setting up";
return Status::success();
}

Status genConfig(std::map<std::string, std::string>& config) {
config["data"] = "{\"queries\":{}}";
return Status::success();
}
};

REGISTER_EXTERNAL(ExampleConfigPlugin, "config", "example");

int main(int argc, char* argv[]) {
osquery::Initializer runner(argc, argv, ToolType::EXTENSION);

auto status = startExtension("example", "0.0.1");
if (!status.ok()) {
LOG(ERROR) << status.getMessage();
runner.requestShutdown(status.getCode());
}

// Finally wait for a signal / interrupt to shutdown.
runner.waitForShutdown();
return 0;
}
13 changes: 13 additions & 0 deletions external/examples/read_only_table/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2014-present, The osquery authors
#
# This source code is licensed as defined by the LICENSE file found in the
# root directory of this source tree.
#
# SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)

project("read_only_table_extension")

addOsqueryExtension(
"${PROJECT_NAME}"
main.cpp
)
52 changes: 52 additions & 0 deletions external/examples/read_only_table/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/

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

using namespace osquery;

class ExampleTable : public TablePlugin {
private:
TableColumns columns() const {
return {
std::make_tuple("example_text", TEXT_TYPE, ColumnOptions::DEFAULT),
std::make_tuple(
"example_integer", INTEGER_TYPE, ColumnOptions::DEFAULT),
};
}

TableRows generate(QueryContext& request) {
TableRows results;

auto r = make_table_row();
r["example_text"] = "example";
r["example_integer"] = INTEGER(1);

results.push_back(std::move(r));
return results;
}
};

REGISTER_EXTERNAL(ExampleTable, "table", "example");

int main(int argc, char* argv[]) {
osquery::Initializer runner(argc, argv, ToolType::EXTENSION);

auto status = startExtension("example", "0.0.1");
if (!status.ok()) {
LOG(ERROR) << status.getMessage();
runner.requestShutdown(status.getCode());
}

// Finally wait for a signal / interrupt to shutdown.
runner.waitForShutdown();
return 0;
}
13 changes: 13 additions & 0 deletions external/examples/writable_table/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2014-present, The osquery authors
#
# This source code is licensed as defined by the LICENSE file found in the
# root directory of this source tree.
#
# SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)

project("writable_table_extension")

addOsqueryExtension(
"${PROJECT_NAME}"
main.cpp
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

#include <osquery/core/system.h>
#include <osquery/sdk.h>
#include <osquery/sdk/sdk.h>
#include <osquery/sql/dynamic_table_row.h>
#include <osquery/utils/conversions/tryto.h>
#include <osquery/utils/json/json.h>

Expand Down Expand Up @@ -108,25 +109,33 @@ class WritableTable : public TablePlugin {

public:
/// Describes the columns available in the table
TableColumns columns() const {
virtual TableColumns columns() const override {
return {std::make_tuple("text", TEXT_TYPE, ColumnOptions::DEFAULT),
std::make_tuple("integer", INTEGER_TYPE, ColumnOptions::DEFAULT)};
}

/// Generates the rows for osquery
QueryData generate(QueryContext& context) {
virtual TableRows generate(QueryContext& request) override {
std::lock_guard<std::mutex> lock(mutex);

QueryData results;
TableRows result;

for (const auto& pkey_row_pair : data) {
results.push_back(pkey_row_pair.second);
auto r = make_table_row();

for (const auto& column : pkey_row_pair.second) {
r[column.first] = column.second;
}

result.push_back(std::move(r));
}

return results;
return result;
}

/// Callback for INSERT queries
QueryData insert(QueryContext& context, const PluginRequest& request) {
virtual QueryData insert(QueryContext& context,
const PluginRequest& request) override {
std::lock_guard<std::mutex> lock(mutex);

// Generate the Row from the json_value_array json
Expand Down Expand Up @@ -186,7 +195,8 @@ class WritableTable : public TablePlugin {
}

/// Callback for DELETE queries
QueryData delete_(QueryContext& context, const PluginRequest& request) {
virtual QueryData delete_(QueryContext& context,
const PluginRequest& request) override {
std::lock_guard<std::mutex> lock(mutex);

const auto& rowid = request.at("id");
Expand All @@ -213,7 +223,8 @@ class WritableTable : public TablePlugin {
}

// Callback for UPDATE queries
QueryData update(QueryContext& context, const PluginRequest& request) {
virtual QueryData update(QueryContext& context,
const PluginRequest& request) override {
std::lock_guard<std::mutex> lock(mutex);

// Validate the rowid
Expand Down Expand Up @@ -294,6 +305,6 @@ int main(int argc, char* argv[]) {
}

// Finally wait for a signal / interrupt to shutdown.
runner.waitThenShutdown();
runner.waitForShutdown();
return 0;
}
110 changes: 0 additions & 110 deletions osquery/examples/example_extension.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions osquery/examples/example_test.cpp

This file was deleted.

Loading

0 comments on commit ffa3da4

Please sign in to comment.