Move APIs to vist-service
authorSangwan Kwon <sangwan.kwon@samsung.com>
Wed, 23 Oct 2019 04:51:10 +0000 (13:51 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Wed, 23 Oct 2019 04:51:10 +0000 (13:51 +0900)
Osquery-manager is deprecated

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
24 files changed:
api/osquery_manager.h [deleted file]
api/property.h
src/osquery/include/osquery/notification.h [deleted file]
src/vist/CMakeLists.txt
src/vist/client/query.cpp
src/vist/main/main.cpp
src/vist/manager/CMakeLists.txt [deleted file]
src/vist/manager/manager.cpp [deleted file]
src/vist/manager/manager_impl.cpp [deleted file]
src/vist/manager/manager_impl.h [deleted file]
src/vist/manager/tests/manager_tests.cpp [deleted file]
src/vist/manager/tests/policy_tests.cpp [deleted file]
src/vist/notification/notification.cpp
src/vist/notification/notification.h [new file with mode: 0644]
src/vist/notification/tests/notification_tests.cpp
src/vist/property/property.cpp
src/vist/property/tests/property_tests.cpp
src/vist/service/CMakeLists.txt
src/vist/service/tests/core_tests.cpp [new file with mode: 0644]
src/vist/service/tests/daemon_tests.cpp [deleted file]
src/vist/service/vist.cpp [new file with mode: 0644]
src/vist/service/vist.h [new file with mode: 0644]
src/vist/service/vistd.cpp [deleted file]
src/vist/service/vistd.h [deleted file]

diff --git a/api/osquery_manager.h b/api/osquery_manager.h
deleted file mode 100644 (file)
index 5e27a87..0000000
+++ /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 <string>
-#include <map>
-#include <vector>
-#include <functional>
-
-namespace osquery {
-
-using Row = std::map<std::string, std::string>;
-using Rows = std::vector<Row>;
-
-using Callback = std::function<void(const Row&)>;
-
-/// 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<std::string> columns(const std::string& table) noexcept;
-};
-
-} // namespace osquery
index 07d2a7d2746c32e55391d2e35f65affa73f01704..373102e4ed753db55a65d01cef33744424337044 100644 (file)
 
 #pragma once
 
-#include <string>
 #include <map>
 #include <stdexcept>
+#include <string>
+#include <vector>
 
-namespace osquery {
+namespace vist {
 
 /// TBD: Consider error handling.
 template <typename T>
@@ -71,4 +72,4 @@ private:
        std::vector<Property<T>> 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 (file)
index 4f33588..0000000
+++ /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 <map>
-#include <vector>
-
-#include <osquery_manager.h>
-
-#include <osquery/database.h>
-#include <osquery/status.h>
-#include <osquery/registry.h>
-
-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<std::string, NotifyCallback> callbacks;
-};
-
-} // namespace osquery
index db63cb9d05cdb206963bca213d2e07a7ffa388ac..adb7459037c2323fd9da174d6391bda12c13f257 100644 (file)
@@ -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)
index 29783addb975478a7436512bf6546837563670d1..2a012ce53b9bcb7d2b37bd0cc4376c8496661c7c 100644 (file)
@@ -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<Rows>("Vistd::query", statement);
+       return client->methodCall<Rows>("Vist::query", statement);
 }
 
 } // namespace vist
index edee3f43461762a7e59d846f2c6f141988531e61..64e1c0eecaad69191526a898565b46e4f823ea29 100644 (file)
  *  limitations under the License
  */
 
-#include "service/vistd.h"
+#include "service/vist.h"
 
 #include <cstdlib>
 #include <stdexcept>
 
+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 (file)
index 41b6d57..0000000
+++ /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 (file)
index 020e5be..0000000
+++ /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 <osquery_manager.h>
-
-#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<std::string> 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 (file)
index f0d1ee6..0000000
+++ /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 <osquery/registry.h>
-#include <osquery/core.h>
-#include <osquery/filesystem/filesystem.h>
-#include <osquery/flags.h>
-#include <osquery/logger.h>
-#include <osquery/notification.h>
-#include <osquery/sql.h>
-#include <osquery/status.h>
-
-#include <gflags/gflags.h>
-
-#include <boost/filesystem/operations.hpp>
-
-#include <functional>
-#include <sstream>
-#include <tuple>
-
-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<std::string> 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<std::string> 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 (file)
index 6867d9a..0000000
+++ /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 <osquery_manager.h>
-
-#include <string>
-#include <vector>
-
-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<std::string> 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 (file)
index 30cac9a..0000000
+++ /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 <gtest/gtest.h>
-
-#include <osquery_manager.h>
-
-#include <osquery/notification.h>
-#include <osquery/logger.h>
-
-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 (file)
index 18ab5f6..0000000
+++ /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 <gtest/gtest.h>
-
-#include <osquery_manager.h>
-
-#include <osquery/logger.h>
-
-#include <policyd/core/policy-manager.h>
-
-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);
-}
index 9db0272d643fac3e44db17157e8f888feeff3742..f30285df89b470567e01c11bba76028b64c83bd0 100644 (file)
  * @brief Implementation of notification
  */
 
+#include "notification.h"
+
 #include <mutex>
 
-#include <osquery/notification.h>
 #include <osquery/logger.h>
 
 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 (file)
index 0000000..56a5962
--- /dev/null
@@ -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 <map>
+#include <vector>
+
+#include <osquery/status.h>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+using Callback = std::function<void(const Row&)>;
+
+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<std::string, NotifyCallback> callbacks;
+};
+
+} // namespace vist
index 721bbf18595b1963e190acba8fce204f9a28ae9a..d01af913e07aaffc2d5772c8e79117fc9e73e5b1 100644 (file)
 
 #include <gtest/gtest.h>
 
-#include <osquery/notification.h>
+#include "../notification.h"
 #include <osquery/logger.h>
 
-using namespace osquery;
+using namespace vist;
 
 class NotificationTests : public testing::Test {};
 
index cbff8eb2b59ad1adfde810f1676875516dc3c6f9..094a9528aa487af2b86cb4932628dfcb31c4ff8a 100644 (file)
  * @brief Implementation of Property
  */
 
-#include <osquery_manager.h>
 #include <property.h>
 
 #include <schema/time.h>
 #include <schema/processes.h>
 
+#include "../service/vist.h"
+
 #include <osquery/logger.h>
 
 #include <tsqb.hxx>
@@ -55,12 +56,12 @@ auto db = make_database("db", time, processes);
 
 } // anonymous namespace
 
-namespace osquery {
+namespace vist {
 
 template <typename T>
 Property<T>::Property()
 {
-       auto results = OsqueryManager::execute(db.selectAll<T>());
+       auto results = Vist::Query(db.selectAll<T>());
        if (results.size() > 0)
                this->data = std::move(results[0]);
 }
@@ -110,7 +111,7 @@ Member Property<T>::operator[](Member Struct::*field) const
 template <typename T>
 Properties<T>::Properties()
 {
-       auto results = OsqueryManager::execute(db.selectAll<T>());
+       auto results = Vist::Query(db.selectAll<T>());
        for (auto& r : results)
                this->datas.emplace_back(Property<T>(std::move(r)));
 }
@@ -130,4 +131,4 @@ template long long int Property<Processes>::operator[](long long int Processes::
 template std::string Property<Processes>::at(std::string Processes::*) const;
 template std::string Property<Processes>::operator[](std::string Processes::*) const;
 
-} // namespace osquery
+} // namespace vist
index 00a7e43c9584cc54837040a116d514c7073de4e0..e5057121ab0d18c9fc9fd878b9d86deaede8b363 100644 (file)
@@ -24,6 +24,7 @@
 #include <schema/processes.h>
 
 using namespace osquery;
+using namespace vist;
 
 class PropertyTests : public testing::Test {};
 
index d02ecd1b2b010a91cb58d9ff8c31e463a0d48f6d..ee1c427b7971516e7aad5259177df8b7a0b5b524 100644 (file)
@@ -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 (file)
index 0000000..1d3e381
--- /dev/null
@@ -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 <gtest/gtest.h>
+
+#include "../vist.h" 
+
+#include <iostream>
+#include <chrono>
+#include <thread>
+
+#include <policyd/core/policy-manager.h>
+
+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 (file)
index 0de3101..0000000
+++ /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 <gtest/gtest.h>
-
-#include "../vistd.h" 
-#include "ipc/client.h"
-
-#include <chrono>
-#include <thread>
-
-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 (file)
index 0000000..9e78b0e
--- /dev/null
@@ -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 <stdexcept>
+
+#include <osquery/registry.h>
+#include <osquery/sql.h>
+#include <osquery/status.h>
+
+#define QUERY_RET_TYPE std::vector<std::map<std::string, std::string>>
+
+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 (file)
index 0000000..03cf9e9
--- /dev/null
@@ -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 <map>
+#include <string>
+#include <vector>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+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 (file)
index 3b4ec1e..0000000
+++ /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 <stdexcept>
-
-#include <osquery/registry.h>
-#include <osquery/sql.h>
-#include <osquery/status.h>
-
-#define QUERY_RET_TYPE std::vector<std::map<std::string, std::string>>
-
-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 (file)
index 897e9f3..0000000
+++ /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 <map>
-#include <string>
-#include <vector>
-
-namespace vist {
-
-using Row = std::map<std::string, std::string>;
-using Rows = std::vector<Row>;
-
-class Vistd final {
-public:
-       explicit Vistd();
-
-       Rows query(const std::string& statement);
-       void start();
-};
-
-} // namespace vist