From: Sangwan Kwon Date: Wed, 23 Oct 2019 04:51:10 +0000 (+0900) Subject: Move APIs to vist-service X-Git-Tag: submit/tizen/20200810.073515~177 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=26544b18ab02cc97dea04b7e2ff23b6351c1332c;p=platform%2Fcore%2Fsecurity%2Fvist.git Move APIs to vist-service Osquery-manager is deprecated Signed-off-by: Sangwan Kwon --- diff --git a/api/osquery_manager.h b/api/osquery_manager.h deleted file mode 100644 index 5e27a87..0000000 --- a/api/osquery_manager.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2019 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 - */ - - -/** - * @file osquery_manager.h - * @brief Osquery manager C++ API header - */ - - -#pragma once - -#include -#include -#include -#include - -namespace osquery { - -using Row = std::map; -using Rows = std::vector; - -using Callback = std::function; - -/// TBD: Consider error handling. -class OsqueryManager final { -public: - /// Query Execution method - static Rows execute(const std::string& query); - - /// Event Subscription method - static void subscribe(const std::string& table, const Callback& callback); - - /// Table information - static std::vector columns(const std::string& table) noexcept; -}; - -} // namespace osquery diff --git a/api/property.h b/api/property.h index 07d2a7d..373102e 100644 --- a/api/property.h +++ b/api/property.h @@ -23,11 +23,12 @@ #pragma once -#include #include #include +#include +#include -namespace osquery { +namespace vist { /// TBD: Consider error handling. template @@ -71,4 +72,4 @@ private: std::vector> datas; }; -} // namespace osquery +} // namespace vist diff --git a/src/osquery/include/osquery/notification.h b/src/osquery/include/osquery/notification.h deleted file mode 100644 index 4f33588..0000000 --- a/src/osquery/include/osquery/notification.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019 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 - */ - - -/** - * @file notification.h - * @brief Notify to registered stuffs when event-callback called - */ - - -#pragma once - -#include -#include - -#include - -#include -#include -#include - -namespace osquery { - -using NotifyCallback = Callback; - -class Notification final { -public: - static Notification& instance(); - - Status add(const std::string& table, const NotifyCallback& callback); - Status emit(const std::string& table, const Row& result) const; - -public: - Notification(const Notification&) = delete; - Notification& operator=(const Notification&) = delete; - -private: - Notification() = default; - ~Notification() = default; - - std::multimap callbacks; -}; - -} // namespace osquery diff --git a/src/vist/CMakeLists.txt b/src/vist/CMakeLists.txt index db63cb9..adb7459 100644 --- a/src/vist/CMakeLists.txt +++ b/src/vist/CMakeLists.txt @@ -23,7 +23,6 @@ INCLUDE_DIRECTORIES(. common) ADD_SUBDIRECTORY(client) ADD_SUBDIRECTORY(common) -ADD_SUBDIRECTORY(manager) ADD_SUBDIRECTORY(notification) ADD_SUBDIRECTORY(property) ADD_SUBDIRECTORY(service) diff --git a/src/vist/client/query.cpp b/src/vist/client/query.cpp index 29783ad..2a012ce 100644 --- a/src/vist/client/query.cpp +++ b/src/vist/client/query.cpp @@ -19,7 +19,7 @@ #include "ipc/client.h" namespace { - const std::string SOCK_ADDR = "/tmp/.vistd"; + const std::string SOCK_ADDR = "/tmp/.vist"; } // anonymous namespace namespace vist { @@ -28,7 +28,7 @@ Rows Query::Execute(const std::string& statement) { auto& client = ipc::Client::Instance(SOCK_ADDR); - return client->methodCall("Vistd::query", statement); + return client->methodCall("Vist::query", statement); } } // namespace vist diff --git a/src/vist/main/main.cpp b/src/vist/main/main.cpp index edee3f4..64e1c0e 100644 --- a/src/vist/main/main.cpp +++ b/src/vist/main/main.cpp @@ -14,16 +14,16 @@ * limitations under the License */ -#include "service/vistd.h" +#include "service/vist.h" #include #include +using namespace vist; + int main() try { - vist::Vistd vistd; - vistd.start(); - + Vist::Instance().start(); return EXIT_SUCCESS; } catch(const std::exception&) { diff --git a/src/vist/manager/CMakeLists.txt b/src/vist/manager/CMakeLists.txt deleted file mode 100644 index 41b6d57..0000000 --- a/src/vist/manager/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright (c) 2019 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 - -ADD_VIST_LIBRARY(manager manager.cpp - manager_impl.cpp) - -FILE(GLOB MANAGER_TESTS "tests/m*.cpp") -ADD_VIST_TEST(${MANAGER_TESTS}) - -IF(DEFINED GBS_BUILD) - FILE(GLOB POLICYD_TESTS "tests/p*.cpp") - ADD_VIST_TEST(${POLICYD_TESTS}) -ENDIF(DEFINED GBS_BUILD) diff --git a/src/vist/manager/manager.cpp b/src/vist/manager/manager.cpp deleted file mode 100644 index 020e5be..0000000 --- a/src/vist/manager/manager.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2019 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 - */ -/* - * @file manager.cpp - * @author Sangwan Kwon (sangwan.kwon@samsung.com) - * @brief Implementation of osquery manager - */ - -#include - -#include "manager_impl.h" - -namespace osquery { - -Rows OsqueryManager::execute(const std::string& query) -{ - return ManagerImpl::instance().execute(query); -} - -void OsqueryManager::subscribe(const std::string& table, const Callback& callback) -{ - return ManagerImpl::instance().subscribe(table, callback); -} - -std::vector OsqueryManager::columns(const std::string& table) noexcept -{ - return ManagerImpl::instance().columns(table); -} - -} // namespace osquery diff --git a/src/vist/manager/manager_impl.cpp b/src/vist/manager/manager_impl.cpp deleted file mode 100644 index f0d1ee6..0000000 --- a/src/vist/manager/manager_impl.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (c) 2019 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 ManagerImplied. - * See the License for the specific language governing permissions and - * limitations under the License - */ -/* - * @file manager_impl.cpp - * @author Sangwan Kwon (sangwan.kwon@samsung.com) - * @brief Implementation of osquery manager's impl - */ - -#include "manager_impl.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include -#include -#include - -namespace osquery { - -ManagerImpl::ManagerImpl() -{ - auto logDir = Flag::getValue("osquery_log_dir"); - if (!logDir.empty() && !(pathExists(logDir).ok())) - boost::filesystem::create_directories(logDir); - - LOG(INFO) << "Initalize osquery manager. "; - - registryAndPluginInit(); -} - -ManagerImpl::~ManagerImpl() noexcept -{ - LOG(INFO) << "Shutdown osquery manager."; -} - -ManagerImpl& ManagerImpl::instance() -{ - static ManagerImpl instance; - return instance; -} - -Rows ManagerImpl::execute(const std::string& query) -{ - LOG(INFO) << "Execute query: " << query; - osquery::SQL sql(query, true); - if (!sql.ok()) { - LOG(ERROR) << "Executing query failed: " << sql.getMessageString(); - return Rows(); - } - - return std::move(sql.rows()); -} - -void ManagerImpl::subscribe(const std::string& table, const Callback& callback) -{ - LOG(INFO) << "Subscribe event: " << table; - - auto status = Notification::instance().add(table, callback); - if (!status.ok()) - LOG(ERROR) << "Subscribing event failed: " << status.getCode(); -} - -std::vector ManagerImpl::columns(const std::string& table) noexcept -{ - std::stringstream query; - query << "SELECT * FROM " << table; - - TableColumns columns; - getQueryColumns(query.str(), columns); - - // Extract column names - std::vector names; - for (auto& c : columns) - names.emplace_back(std::move(std::get<0>(c))); - - return names; -} - -} // namespace osquery diff --git a/src/vist/manager/manager_impl.h b/src/vist/manager/manager_impl.h deleted file mode 100644 index 6867d9a..0000000 --- a/src/vist/manager/manager_impl.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2019 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 ManagerImplied. - * See the License for the specific language governing permissions and - * limitations under the License - */ -/* - * @file manager_impl.h - * @author Sangwan Kwon (sangwan.kwon@samsung.com) - * @brief Implementation interface of osquery manager - */ - -#pragma once - -#include - -#include -#include - -namespace osquery { - -/// Singleton class -class ManagerImpl final { -public: - ManagerImpl(const ManagerImpl&) = delete; - ManagerImpl& operator=(const ManagerImpl&) = delete; - - ManagerImpl(ManagerImpl&&) noexcept = default; - ManagerImpl& operator=(ManagerImpl&&) noexcept = default; - - static ManagerImpl& instance(); - - Rows execute(const std::string& query); - void subscribe(const std::string& table, const Callback& callback); - - std::vector columns(const std::string& table) noexcept; - -private: - ManagerImpl(); - ~ManagerImpl() noexcept; -}; - -} // namespace osquery diff --git a/src/vist/manager/tests/manager_tests.cpp b/src/vist/manager/tests/manager_tests.cpp deleted file mode 100644 index 30cac9a..0000000 --- a/src/vist/manager/tests/manager_tests.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2019 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 - -#include - -#include -#include - -using namespace osquery; - -class ManagerTests : public testing::Test {}; - -TEST_F(ManagerTests, test_manager_execute) { - std::string query = "SELECT * FROM time"; - auto rows = OsqueryManager::execute(query); - EXPECT_EQ(rows.size(), 1); - - LOG(INFO) << "[Test] time table rows:"; - LOG(INFO) << "\t hour: " << rows[0]["hour"]; - LOG(INFO) << "\t minutes: " << rows[0]["minutes"]; - LOG(INFO) << "\t seconds: " << rows[0]["seconds"]; -} - -TEST_F(ManagerTests, test_manager_subscribe) { - int called = 0; - auto callback = [&](const Row& row) { - LOG(INFO) << "NotifyCallback called:"; - for (const auto& r : row) - LOG(INFO) << "\t" << r.first << " : " << r.second; - called++; - }; - - OsqueryManager::subscribe("manager_test", callback); - - Row row; - row["foo"] = "bar"; - row["foo2"] = "bar2"; - row["foo3"] = "bar3"; - - /// Notification trigger - auto s = Notification::instance().emit("manager_test", row); - - EXPECT_TRUE(s.ok()); - EXPECT_EQ(called, 1); -} - -TEST_F(ManagerTests, test_manager_columns) { - auto columns = OsqueryManager::columns("time"); - EXPECT_TRUE(columns.size() > 0); - - LOG(INFO) << "[Test] Enabled columns of time table:"; - for (const auto& c : columns) - LOG(INFO) << "\t" << c; -} diff --git a/src/vist/manager/tests/policy_tests.cpp b/src/vist/manager/tests/policy_tests.cpp deleted file mode 100644 index 18ab5f6..0000000 --- a/src/vist/manager/tests/policy_tests.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2019 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 - -#include - -#include - -#include - -using namespace osquery; - -class PolicydTests : public testing::Test {}; - -TEST_F(PolicydTests, select) { - std::string query = "SELECT * FROM policy"; - auto rows = OsqueryManager::execute(query); - EXPECT_TRUE(rows.size() > 0); - - LOG(INFO) << "[Test] policy table rows:"; - for (auto& r : rows) { - LOG(INFO) << "\t name: " << r["name"]; - LOG(INFO) << "\t value: " << r["value"]; - } - - query = "SELECT * FROM policy WHERE name = 'bluetooth'"; - rows = OsqueryManager::execute(query); - - LOG(INFO) << "[Test] policy table rows with where clause:"; - for (auto& r : rows) { - LOG(INFO) << "\t name: " << r["name"]; - LOG(INFO) << "\t value: " << r["value"]; - } - - EXPECT_EQ(rows.size(), 1); -} - -TEST_F(PolicydTests, update) { - auto& manager = policyd::PolicyManager::Instance(); - manager.enroll("admin", 0); - - std::string query = "SELECT * FROM policy WHERE name = 'bluetooth'"; - auto rows = OsqueryManager::execute(query); - /// Initial policy value - EXPECT_EQ(rows[0]["value"], std::to_string(1)); - - query = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'"; - rows = OsqueryManager::execute(query); - EXPECT_EQ(rows.size(), 0); - - query = "SELECT * FROM policy WHERE name = 'bluetooth'"; - rows = OsqueryManager::execute(query); - EXPECT_EQ(rows[0]["value"], std::to_string(3)); - - manager.disenroll("admin", 0); -} diff --git a/src/vist/notification/notification.cpp b/src/vist/notification/notification.cpp index 9db0272..f30285d 100644 --- a/src/vist/notification/notification.cpp +++ b/src/vist/notification/notification.cpp @@ -19,16 +19,19 @@ * @brief Implementation of notification */ +#include "notification.h" + #include -#include #include namespace { std::mutex mutex; } // anonymous namespace -namespace osquery { +namespace vist { + +using namespace osquery; Notification& Notification::instance() { @@ -72,4 +75,4 @@ Status Notification::emit(const std::string& table, const Row& result) const return Status(0, "OK"); } -} // namespace osquery +} // namespace vist diff --git a/src/vist/notification/notification.h b/src/vist/notification/notification.h new file mode 100644 index 0000000..56a5962 --- /dev/null +++ b/src/vist/notification/notification.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 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 + */ + + +/** + * @file notification.h + * @brief Notify to registered stuffs when event-callback called + */ + + +#pragma once + +#include +#include + +#include + +namespace vist { + +using Row = std::map; +using Rows = std::vector; + +using Callback = std::function; + +using NotifyCallback = Callback; + +class Notification final { +public: + static Notification& instance(); + + Notification(const Notification&) = delete; + Notification& operator=(const Notification&) = delete; + + osquery::Status add(const std::string& table, const NotifyCallback& callback); + osquery::Status emit(const std::string& table, const Row& result) const; + +private: + Notification() = default; + ~Notification() = default; + + std::multimap callbacks; +}; + +} // namespace vist diff --git a/src/vist/notification/tests/notification_tests.cpp b/src/vist/notification/tests/notification_tests.cpp index 721bbf1..d01af91 100644 --- a/src/vist/notification/tests/notification_tests.cpp +++ b/src/vist/notification/tests/notification_tests.cpp @@ -16,10 +16,10 @@ #include -#include +#include "../notification.h" #include -using namespace osquery; +using namespace vist; class NotificationTests : public testing::Test {}; diff --git a/src/vist/property/property.cpp b/src/vist/property/property.cpp index cbff8eb..094a952 100644 --- a/src/vist/property/property.cpp +++ b/src/vist/property/property.cpp @@ -19,12 +19,13 @@ * @brief Implementation of Property */ -#include #include #include #include +#include "../service/vist.h" + #include #include @@ -55,12 +56,12 @@ auto db = make_database("db", time, processes); } // anonymous namespace -namespace osquery { +namespace vist { template Property::Property() { - auto results = OsqueryManager::execute(db.selectAll()); + auto results = Vist::Query(db.selectAll()); if (results.size() > 0) this->data = std::move(results[0]); } @@ -110,7 +111,7 @@ Member Property::operator[](Member Struct::*field) const template Properties::Properties() { - auto results = OsqueryManager::execute(db.selectAll()); + auto results = Vist::Query(db.selectAll()); for (auto& r : results) this->datas.emplace_back(Property(std::move(r))); } @@ -130,4 +131,4 @@ template long long int Property::operator[](long long int Processes:: template std::string Property::at(std::string Processes::*) const; template std::string Property::operator[](std::string Processes::*) const; -} // namespace osquery +} // namespace vist diff --git a/src/vist/property/tests/property_tests.cpp b/src/vist/property/tests/property_tests.cpp index 00a7e43..e505712 100644 --- a/src/vist/property/tests/property_tests.cpp +++ b/src/vist/property/tests/property_tests.cpp @@ -24,6 +24,7 @@ #include using namespace osquery; +using namespace vist; class PropertyTests : public testing::Test {}; diff --git a/src/vist/service/CMakeLists.txt b/src/vist/service/CMakeLists.txt index d02ecd1..ee1c427 100644 --- a/src/vist/service/CMakeLists.txt +++ b/src/vist/service/CMakeLists.txt @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License -ADD_VIST_LIBRARY(vist_daemon vistd.cpp) +ADD_VIST_LIBRARY(vist_core vist.cpp) -FILE(GLOB DAEMON_TESTS "tests/*.cpp") -ADD_VIST_TEST(${DAEMON_TESTS}) +FILE(GLOB CORE_TESTS "tests/*.cpp") +ADD_VIST_TEST(${CORE_TESTS}) diff --git a/src/vist/service/tests/core_tests.cpp b/src/vist/service/tests/core_tests.cpp new file mode 100644 index 0000000..1d3e381 --- /dev/null +++ b/src/vist/service/tests/core_tests.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019 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 + +#include "../vist.h" + +#include +#include +#include + +#include + +using namespace vist; + +class CoreTests : public testing::Test {}; + +TEST_F(CoreTests, query_select) { + auto rows = Vist::Query("SELECT * FROM policy"); + + EXPECT_TRUE(rows.size() > 0); + + std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'"; + rows = Vist::Query(statement); + + EXPECT_EQ(rows.size(), 1); + EXPECT_EQ(rows[0]["name"], "bluetooth"); +} + +TEST_F(CoreTests, query_update) { + auto& manager = policyd::PolicyManager::Instance(); + manager.enroll("admin", 0); + + std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'"; + auto rows = Vist::Query(statement); + /// Initial policy value + EXPECT_EQ(rows[0]["value"], std::to_string(1)); + + statement = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'"; + rows = Vist::Query(statement); + EXPECT_EQ(rows.size(), 0); + + statement = "SELECT * FROM policy WHERE name = 'bluetooth'"; + rows = Vist::Query(statement); + EXPECT_EQ(rows[0]["value"], std::to_string(3)); + + manager.disenroll("admin", 0); +} diff --git a/src/vist/service/tests/daemon_tests.cpp b/src/vist/service/tests/daemon_tests.cpp deleted file mode 100644 index 0de3101..0000000 --- a/src/vist/service/tests/daemon_tests.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 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 - -#include "../vistd.h" -#include "ipc/client.h" - -#include -#include - -using namespace vist; - -class DaemonTests : public testing::Test {}; - -TEST_F(DaemonTests, query) { - Vistd vistd; - auto rows = vistd.query("SELECT * FROM policy"); - - EXPECT_TRUE(rows.size() > 0); -} diff --git a/src/vist/service/vist.cpp b/src/vist/service/vist.cpp new file mode 100644 index 0000000..9e78b0e --- /dev/null +++ b/src/vist/service/vist.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 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 "vist.h" +#include "ipc/server.h" + +#include + +#include +#include +#include + +#define QUERY_RET_TYPE std::vector> + +namespace { + const std::string SOCK_ADDR = "/tmp/.vist"; +} // anonymous namespace + +namespace vist { + +Vist::Vist() +{ + osquery::registryAndPluginInit(); +} + +void Vist::start() +{ + auto& server = ipc::Server::Instance(SOCK_ADDR); + + server->expose(this, "", (QUERY_RET_TYPE)(Vist::query)(std::string)); + server->start(); +} + +Rows Vist::query(const std::string& statement) +{ + osquery::SQL sql(statement, true); + if (!sql.ok()) + throw std::runtime_error("Faild to execute query: " + sql.getMessageString()); + + return std::move(sql.rows()); +} + +} // namespace vist diff --git a/src/vist/service/vist.h b/src/vist/service/vist.h new file mode 100644 index 0000000..03cf9e9 --- /dev/null +++ b/src/vist/service/vist.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 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 { + +using Row = std::map; +using Rows = std::vector; + +class Vist final { +public: + Vist(const Vist&) = delete; + Vist& operator=(const Vist&) = delete; + + Vist(Vist&&) = default; + Vist& operator=(Vist&&) = default; + + /// Exposed method (API) + Rows query(const std::string& statement); + + static Vist& Instance() + { + static Vist instance; + return instance; + } + + static Rows Query(const std::string& statement) + { + return Vist::Instance().query(statement); + } + + void start(); + +private: + explicit Vist(); + ~Vist() = default; +}; + +} // namespace vist diff --git a/src/vist/service/vistd.cpp b/src/vist/service/vistd.cpp deleted file mode 100644 index 3b4ec1e..0000000 --- a/src/vist/service/vistd.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2019 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 "vistd.h" -#include "ipc/server.h" - -#include - -#include -#include -#include - -#define QUERY_RET_TYPE std::vector> - -namespace { - const std::string SOCK_ADDR = "/tmp/.vistd"; -} // anonymous namespace - -namespace vist { - -Vistd::Vistd() -{ - osquery::registryAndPluginInit(); -} - -void Vistd::start() -{ - auto& server = ipc::Server::Instance(SOCK_ADDR); - - server->expose(this, "", (QUERY_RET_TYPE)(Vistd::query)(std::string)); - server->start(); -} - -Rows Vistd::query(const std::string& statement) -{ - osquery::SQL sql(statement, true); - if (!sql.ok()) - throw std::runtime_error("Faild to execute query: " + sql.getMessageString()); - - return std::move(sql.rows()); -} - -} // namespace vist diff --git a/src/vist/service/vistd.h b/src/vist/service/vistd.h deleted file mode 100644 index 897e9f3..0000000 --- a/src/vist/service/vistd.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2019 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 { - -using Row = std::map; -using Rows = std::vector; - -class Vistd final { -public: - explicit Vistd(); - - Rows query(const std::string& statement); - void start(); -}; - -} // namespace vist