From 80001c215aca2bd01ead5b2bf8bb3027031c2f90 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Wed, 22 Jul 2020 17:11:40 +0900 Subject: [PATCH] Migrate bluetooth-policy to DynamicTable Change-Id: I7646bd813c310523fed86d17eea286594b159dbb Signed-off-by: Sangwan Kwon --- packaging/vist.spec | 19 +-- src/vist/table/CMakeLists.txt | 4 + src/vist/table/dynamic-table.hpp | 16 ++- .../table/policy/bluetooth/CMakeLists.txt | 51 +++++++ src/vist/table/policy/bluetooth/policy.cpp | 96 +++++++++++++ src/vist/table/policy/bluetooth/policy.hpp | 57 ++++++++ src/vist/table/policy/bluetooth/table.cpp | 134 ++++++++++++++++++ src/vist/table/policy/bluetooth/table.hpp | 39 +++++ .../policy/bluetooth/tests/bluetooth.cpp | 83 +++++++++++ src/vist/table/policy/sample/CMakeLists.txt | 4 +- .../policy/sample/{sample.hpp => policy.hpp} | 35 +---- .../policy/sample/{sample.cpp => table.cpp} | 27 ++-- src/vist/table/policy/sample/table.hpp | 40 ++++++ 13 files changed, 547 insertions(+), 58 deletions(-) create mode 100644 src/vist/table/policy/bluetooth/CMakeLists.txt create mode 100644 src/vist/table/policy/bluetooth/policy.cpp create mode 100644 src/vist/table/policy/bluetooth/policy.hpp create mode 100644 src/vist/table/policy/bluetooth/table.cpp create mode 100644 src/vist/table/policy/bluetooth/table.hpp create mode 100644 src/vist/table/policy/bluetooth/tests/bluetooth.cpp rename src/vist/table/policy/sample/{sample.hpp => policy.hpp} (73%) rename src/vist/table/policy/sample/{sample.cpp => table.cpp} (82%) create mode 100644 src/vist/table/policy/sample/table.hpp diff --git a/packaging/vist.spec b/packaging/vist.spec index e28e684..b6329c5 100644 --- a/packaging/vist.spec +++ b/packaging/vist.spec @@ -77,6 +77,7 @@ make %{?jobs:-j%jobs} %install %make_install mkdir -p %{buildroot}/%{vist_db_dir} +mkdir -p %{buildroot}/%{vist_table_dir} mkdir -p %{buildroot}/%{vist_plugin_dir} mkdir -p %{buildroot}/%{vist_script_dir} @@ -110,7 +111,7 @@ fi %{_unitdir}/sockets.target.wants/vist.socket %{vist_script_dir}/*.sql %dir %attr(-, %{user_name}, %{group_name}) %{vist_db_dir} -%dir %attr(-, %{user_name}, %{group_name}) %{vist_plugin_dir} +%dir %attr(-, %{user_name}, %{group_name}) %{vist_table_dir} %dir %attr(-, %{user_name}, %{group_name}) %{vist_script_dir} ## Test Package ############################################################## @@ -120,14 +121,16 @@ Group: Security/Testing BuildRequires: gtest-devel Requires: gtest -%description test +%description test Provides internal testcases for ViST implementation. %files test %manifest packaging/%{name}-test.manifest %{_bindir}/osquery-test %attr(4755 %{user_name}, %{group_name}) %{_bindir}/vist-test -%dir %attr(-, %{user_name}, %{group_name}) %{vist_plugin_dir}/sample +%dir %attr(-, %{user_name}, %{group_name}) %{vist_table_dir} +%attr(-, %{user_name}, %{group_name}) %{vist_table_dir}/libvist-table-sample.so +%attr(-, %{user_name}, %{group_name}) %{vist_plugin_dir}/libtest-plugin.so ## ViST Plugins ############################################################# %package plugins @@ -147,12 +150,10 @@ Requires: klay Provides plugins for controlling policies. %pre plugins -rm -f %{vist_plugin_dir}/bluetooth -rm -f %{vist_plugin_dir}/wifi +#rm -f %{vist_plugin_dir}/bluetooth +#rm -f %{vist_plugin_dir}/wifi %files plugins %manifest packaging/%{name}-plugins.manifest -%{vist_plugin_dir}/bluetooth -%{vist_plugin_dir}/wifi -%{_bindir}/vist-plugin-bluetooth-test -%{_bindir}/vist-plugin-wifi-test +%attr(-, %{user_name}, %{group_name}) %{vist_table_dir}/libvist-bluetooth-policy.so +%attr(4755 %{user_name}, %{group_name}) %{_bindir}/vist-bluetooth-policy-test diff --git a/src/vist/table/CMakeLists.txt b/src/vist/table/CMakeLists.txt index f05ce01..aefd1ef 100644 --- a/src/vist/table/CMakeLists.txt +++ b/src/vist/table/CMakeLists.txt @@ -18,3 +18,7 @@ ADD_VIST_LIBRARY(vist_table policy/policy-admin.cpp ## dynamic virtual table ADD_SUBDIRECTORY(policy/sample) + +IF(DEFINED GBS_BUILD) + ADD_SUBDIRECTORY(policy/bluetooth) +ENDIF(DEFINED GBS_BUILD) diff --git a/src/vist/table/dynamic-table.hpp b/src/vist/table/dynamic-table.hpp index b04de07..7b82781 100644 --- a/src/vist/table/dynamic-table.hpp +++ b/src/vist/table/dynamic-table.hpp @@ -14,16 +14,30 @@ * limitations under the License */ +#include + +#include +#include #include +using namespace osquery; + namespace vist { namespace table { -class DynamicTable : public osquery::TablePlugin { +class DynamicTable : public TablePlugin { public: using FactoryType = DynamicTable* (*)(); virtual void init() = 0; + + template + static void Register(const std::string& name, std::shared_ptr&& table) + { + // Register virtual table to sqlite3 + auto tables = RegistryFactory::get().registry("table"); + tables->add(name, std::move(table)); + } }; } // namespace table diff --git a/src/vist/table/policy/bluetooth/CMakeLists.txt b/src/vist/table/policy/bluetooth/CMakeLists.txt new file mode 100644 index 0000000..6f63912 --- /dev/null +++ b/src/vist/table/policy/bluetooth/CMakeLists.txt @@ -0,0 +1,51 @@ +# Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +SET(TARGET "vist-bluetooth-policy") +SET(TARGET_TEST ${TARGET}-test) + +SET(DEPENDENCY bluetooth-api + capi-network-bluetooth) + +PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) + +INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS}) + +## LIB ########################################### +ADD_LIBRARY(${TARGET} SHARED policy.cpp table.cpp) +TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES} + ${TARGET_VIST_COMMON_LIB} + ${TARGET_VIST_POLICY_LIB}) + +INSTALL(TARGETS ${TARGET} DESTINATION ${TABLE_INSTALL_DIR}) + +## TEST ############################################# +ADD_EXECUTABLE(${TARGET_TEST} ../../../main/tests.cpp + policy.cpp + table.cpp + tests/bluetooth.cpp) +TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES} + ${TARGET_VIST_CLIENT_LIB} + ${TARGET_VIST_LIB} + gtest + pthread) +INSTALL(TARGETS ${TARGET_TEST} + DESTINATION ${CMAKE_INSTALL_BINDIR} + PERMISSIONS OWNER_READ + OWNER_WRITE + OWNER_EXECUTE + GROUP_READ + GROUP_EXECUTE + WORLD_READ + WORLD_EXECUTE) diff --git a/src/vist/table/policy/bluetooth/policy.cpp b/src/vist/table/policy/bluetooth/policy.cpp new file mode 100644 index 0000000..49ba0b4 --- /dev/null +++ b/src/vist/table/policy/bluetooth/policy.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include "policy.hpp" + +#include +#include + +#include +#include +#include + +namespace vist { +namespace policy { + +namespace { + +bool failed(int value) +{ + return value == BLUETOOTH_DPM_RESULT_ACCESS_DENIED || value == BLUETOOTH_DPM_RESULT_FAIL; +} + +auto allowed(int value) +{ + return value ? BLUETOOTH_DPM_ALLOWED : BLUETOOTH_DPM_RESTRICTED; +} + +auto bt_allowed(int value) +{ + return value ? BLUETOOTH_DPM_BT_ALLOWED : BLUETOOTH_DPM_BT_RESTRICTED; +} + +} // anonymous namespace + +void BluetoothState::onChanged(const PolicyValue& value) +{ + auto enable = bt_allowed(value); + auto ret = ::bluetooth_dpm_set_allow_mode(enable); + if (failed(ret)) + THROW(ErrCode::RuntimeError) << "Failed to change bluetooth state: " << ret; + + INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable; +} + +void DesktopConnectivity::onChanged(const PolicyValue& value) +{ + auto enable = allowed(value); + auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable); + if (failed(ret)) + THROW(ErrCode::RuntimeError) << "Failed to change bt_desktop_connectivity: " << ret; + + INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable; +} + +void Pairing::onChanged(const PolicyValue& value) +{ + auto enable = allowed(value); + auto ret = ::bluetooth_dpm_set_pairing_state(enable); + if (failed(ret)) + THROW(ErrCode::RuntimeError) << "Failed to change bluetooth pairing: " << ret; + + INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable; +} + +void Tethering::onChanged(const PolicyValue& value) +{ + auto enable = value; + INFO(VIST_PLUGIN) << "Bluetooth tethering state is changed to " << enable; +} + +Bluetooth::Bluetooth(const std::string& name) : PolicyProvider(name) +{ + if (::bt_initialize() != BT_ERROR_NONE) + THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider."; +} + +Bluetooth::~Bluetooth() +{ + ::bt_deinitialize(); +} + +} // namespace policy +} // namespace vist diff --git a/src/vist/table/policy/bluetooth/policy.hpp b/src/vist/table/policy/bluetooth/policy.hpp new file mode 100644 index 0000000..3c927df --- /dev/null +++ b/src/vist/table/policy/bluetooth/policy.hpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#pragma once + +#include +#include +#include + +namespace vist { +namespace policy { + +class BluetoothState : public PolicyModel { +public: + BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {} + void onChanged(const PolicyValue& value) override; +}; + +class DesktopConnectivity : public PolicyModel { +public: + DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {} + void onChanged(const PolicyValue& value) override; +}; + +class Pairing: public PolicyModel { +public: + Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {} + void onChanged(const PolicyValue& value) override; +}; + +class Tethering: public PolicyModel { +public: + Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {} + void onChanged(const PolicyValue&); +}; + +class Bluetooth final : public PolicyProvider { +public: + Bluetooth(const std::string& name); + ~Bluetooth(); +}; + +} // namespace policy +} // namespace vist diff --git a/src/vist/table/policy/bluetooth/table.cpp b/src/vist/table/policy/bluetooth/table.cpp new file mode 100644 index 0000000..4af0a16 --- /dev/null +++ b/src/vist/table/policy/bluetooth/table.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include "policy.hpp" +#include "table.hpp" + +#include +#include +#include +#include + +extern "C" vist::table::DynamicTable* DynamicTableFactory() +{ + return new vist::table::BluetoothTable; +} + +namespace vist { +namespace table { + +namespace { + +std::map ALIAS = { + { "state", "bluetooth" }, + { "desktopConnectivity", "bluetooth-desktop-connectivity" }, + { "pairing", "bluetooth-pairing" }, + { "tethering", "bluetooth-tethering"} +}; + +void setPolicy(const std::string& name, int value) +{ + vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value)); +} + +} // anonymous namespace + +void BluetoothTable::init() +{ + DynamicTable::Register("bluetooth", std::make_shared()); + + auto provider = std::make_shared("bluetooth"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + + policy::API::Admin::AddProvider(provider); + + INFO(VIST_PLUGIN) << "Bluetooth plugin loaded."; +} + +TableColumns BluetoothTable::columns() const +{ + return { + std::make_tuple("state", INTEGER_TYPE, ColumnOptions::DEFAULT), + std::make_tuple("desktopConnectivity", INTEGER_TYPE, ColumnOptions::DEFAULT), + std::make_tuple("pairing", INTEGER_TYPE, ColumnOptions::DEFAULT), + std::make_tuple("tethering", INTEGER_TYPE, ColumnOptions::DEFAULT), + }; +} + +TableRows BluetoothTable::generate(QueryContext&) try +{ + INFO(VIST) << "Select query about bluetooth table."; + + Row row; + for (const auto& [schemaName, policyName] : ALIAS) { + int value = vist::policy::API::Get(policyName); + row[schemaName] = std::to_string(value); + } + + QueryData results; + results.emplace_back(std::move(row)); + + return osquery::tableRowsFromQueryData(std::move(results)); +} catch (const vist::Exception& e) +{ + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return osquery::tableRowsFromQueryData({ r }); +} catch (...) +{ + ERROR(VIST) << "Failed to query with unknown exception."; + Row r; + return osquery::tableRowsFromQueryData({ r }); +} + +QueryData BluetoothTable::update(QueryContext&, const PluginRequest& request) try +{ + INFO(VIST) << "Update query about bluetooth table."; + if (request.count("json_values") == 0) + throw std::runtime_error("Wrong request format. Not found json value."); + + DEBUG(VIST) << "Request values: " << request.at("json_values"); + json::Json document = json::Json::Parse(request.at("json_values")); + json::Array values = document.get("values"); + if (values.size() != 4) + throw std::runtime_error("Wrong request format."); + + /// TODO(Sangwan): Sync vtab schema with policy definition + setPolicy("bluetooth", static_cast(values.at(0))); + setPolicy("bluetooth-desktop-connectivity", static_cast(values.at(1))); + setPolicy("bluetooth-pairing", static_cast(values.at(2))); + setPolicy("bluetooth-tethering", static_cast(values.at(3))); + + Row r; + r["status"] = "success"; + return { r }; +} catch (const vist::Exception& e) +{ + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; +} catch (...) +{ + ERROR(VIST) << "Failed to query with unknown exception."; + Row r; + return { r }; +} + +} // namespace table +} // namespace vist diff --git a/src/vist/table/policy/bluetooth/table.hpp b/src/vist/table/policy/bluetooth/table.hpp new file mode 100644 index 0000000..67996b0 --- /dev/null +++ b/src/vist/table/policy/bluetooth/table.hpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/* + * Query example + * - SELECT * FROM bluetooth + * - UPDATE bluetooth SET state = 1 // enable + * - UPDATE bluetooth SET state = 0 // disable + */ + +#include + +namespace vist { +namespace table { + +class BluetoothTable final : public DynamicTable { +public: + void init(); + +private: + TableColumns columns() const override; + TableRows generate(QueryContext&) override; + QueryData update(QueryContext&, const PluginRequest& request) override; +}; + +} // namespace table +} // namespace vist diff --git a/src/vist/table/policy/bluetooth/tests/bluetooth.cpp b/src/vist/table/policy/bluetooth/tests/bluetooth.cpp new file mode 100644 index 0000000..d682805 --- /dev/null +++ b/src/vist/table/policy/bluetooth/tests/bluetooth.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include "../policy.hpp" + +#include +#include +#include +#include + +#include + +#include + +using namespace vist; +using namespace vist::policy; + +namespace { + +template +void change_policy_state() +{ + std::shared_ptr policy = std::make_shared(); + /// enable policy + policy->onChanged(PolicyValue(1)); + /// disable policy + policy->onChanged(PolicyValue(0)); +} + +} // anonymous namespace + +TEST(BluetoothTests, change_policy_state) +{ + try { + change_policy_state(); + change_policy_state(); + change_policy_state(); + change_policy_state(); + } catch(const vist::Exception& e) { + EXPECT_TRUE(false) << e.what(); + } +} + +TEST(BluetoothTests, get_policies) +{ + auto rows = Query::Execute("SELECT * FROM bluetooth"); + + EXPECT_TRUE(rows.size() == 1); +} + +TEST(BluetoothTests, set_policies) +{ + // Binany should be enrolled as admin to control policy. + Query::Execute("INSERT INTO policy_admin (name) VALUES ('vist-bluetooth-policy-test')"); + + Query::Execute("UPDATE bluetooth SET desktopConnectivity = 3, state = 7"); + Query::Execute("UPDATE bluetooth SET pairing = 2, tethering = 9"); + + auto rows = Query::Execute("SELECT * FROM bluetooth"); + if (rows.size() == 1) { + EXPECT_EQ(rows[0]["state"], "7"); + EXPECT_EQ(rows[0]["desktopConnectivity"], "3"); + EXPECT_EQ(rows[0]["pairing"], "2"); + EXPECT_EQ(rows[0]["tethering"], "9"); + } else { + EXPECT_TRUE(false); + } + + Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-bluetooth-policy-test'"); +} diff --git a/src/vist/table/policy/sample/CMakeLists.txt b/src/vist/table/policy/sample/CMakeLists.txt index d97eea6..9a1765a 100644 --- a/src/vist/table/policy/sample/CMakeLists.txt +++ b/src/vist/table/policy/sample/CMakeLists.txt @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # -SET(TARGET "vist-plugin-sample") +SET(TARGET "vist-table-sample") INCLUDE_DIRECTORIES(SYSTEM) -ADD_LIBRARY(${TARGET} SHARED sample.cpp) +ADD_LIBRARY(${TARGET} SHARED table.cpp) TARGET_LINK_LIBRARIES(${TARGET} vist-common ${TARGET_VIST_POLICY_LIB}) INSTALL(TARGETS ${TARGET} DESTINATION ${TABLE_INSTALL_DIR}) diff --git a/src/vist/table/policy/sample/sample.hpp b/src/vist/table/policy/sample/policy.hpp similarity index 73% rename from src/vist/table/policy/sample/sample.hpp rename to src/vist/table/policy/sample/policy.hpp index 3b241f5..b9c309f 100644 --- a/src/vist/table/policy/sample/sample.hpp +++ b/src/vist/table/policy/sample/policy.hpp @@ -13,22 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License */ -/* - * Query example - * - SELECT * FROM sample_policy - * - UPDATE sample_policy SET sample_int_policy = 99 - * - UPDATE sample_policy SET sample_str_policy = 'TEST_VALUE' - */ -#include -#include +#pragma once +#include #include #include #include -#include - #include #include @@ -72,31 +64,16 @@ public: } }; -class Sample : public PolicyProvider { +class SampleProvider : public PolicyProvider { public: - Sample(const std::string& name) : PolicyProvider(name) + SampleProvider(const std::string& name) : PolicyProvider(name) { } - ~Sample() + ~SampleProvider() { } }; -} // namespace policy - -namespace table { -using namespace osquery; - -class SamplePolicyTable final : public DynamicTable { -public: - void init(); - -private: - TableColumns columns() const override; - TableRows generate(QueryContext&) override; - QueryData update(QueryContext&, const PluginRequest& request) override; -}; - -} // namespace table +} // namespace policy } // namespace vist diff --git a/src/vist/table/policy/sample/sample.cpp b/src/vist/table/policy/sample/table.cpp similarity index 82% rename from src/vist/table/policy/sample/sample.cpp rename to src/vist/table/policy/sample/table.cpp index f5d02a2..3fc8019 100644 --- a/src/vist/table/policy/sample/sample.cpp +++ b/src/vist/table/policy/sample/table.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,18 +14,14 @@ * limitations under the License */ -#include "sample.hpp" - -#include -#include -#include +#include "policy.hpp" +#include "table.hpp" +#include +#include #include #include -#include -#include - extern "C" vist::table::DynamicTable* DynamicTableFactory() { return new vist::table::SamplePolicyTable; @@ -34,18 +30,15 @@ extern "C" vist::table::DynamicTable* DynamicTableFactory() namespace vist { namespace table { -using namespace osquery; - void SamplePolicyTable::init() { - // Register virtual table to sqlite3 - auto tables = RegistryFactory::get().registry("table"); - tables->add("sample_policy", std::make_shared()); + DynamicTable::Register("sample_policy", std::make_shared()); // Register policy to policy-manager - auto provider = std::make_shared("sample-provider"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); + using namespace policy; + auto provider = std::make_shared("sample-provider"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); policy::API::Admin::AddProvider(std::move(provider)); diff --git a/src/vist/table/policy/sample/table.hpp b/src/vist/table/policy/sample/table.hpp new file mode 100644 index 0000000..ff80992 --- /dev/null +++ b/src/vist/table/policy/sample/table.hpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/* + * Query example + * - SELECT * FROM sample_policy + * - UPDATE sample_policy SET sample_int_policy = 99 + * - UPDATE sample_policy SET sample_str_policy = 'TEST_VALUE' + */ + +#include +#include + +namespace vist { +namespace table { + +class SamplePolicyTable final : public DynamicTable { +public: + void init(); + +private: + TableColumns columns() const override; + TableRows generate(QueryContext&) override; + QueryData update(QueryContext&, const PluginRequest& request) override; +}; + +} // namespace table +} // namespace vist -- 2.34.1