Detach osquery dependency from client
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 24 Oct 2019 05:55:21 +0000 (14:55 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Wed, 30 Oct 2019 04:46:01 +0000 (13:46 +0900)
Make logger as common

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/policyd/core/CMakeLists.txt
src/policyd/core/logger.cpp [deleted file]
src/policyd/core/logger.h [deleted file]
src/policyd/core/policy-manager.cpp
src/policyd/core/policy-storage.cpp
src/vist/client/query.cpp
src/vist/client/tests/virtual_table_tests.cpp
src/vist/client/virtual-table.cpp
src/vist/common/CMakeLists.txt
src/vist/common/audit/logger.h [new file with mode: 0644]
src/vist/service/vist.cpp

index 498ea529656a9daf52d221df279db24f5b4af09d..4f73300e7295ce7e0d2091c36e5e0ca5b6a9263c 100644 (file)
@@ -16,8 +16,7 @@
 ADD_POLICYD_LIBRARY(policyd_core api.cpp
                                                                 policy-manager.cpp
                                                                 policy-loader.cpp
-                                                                policy-storage.cpp
-                                                                logger.cpp)
+                                                                policy-storage.cpp)
 
 FILE(GLOB SDK_TESTS "tests/*.cpp")
 ADD_POLICYD_TEST(${SDK_TESTS})
diff --git a/src/policyd/core/logger.cpp b/src/policyd/core/logger.cpp
deleted file mode 100644 (file)
index 927d83c..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- *  Copyright (c) 2017 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 "logger.h"
-
-#include <klay/audit/dlog-sink.h>
-
-namespace policyd {
-
-std::unique_ptr<audit::LogSink> Logger::logSink = nullptr;
-std::once_flag Logger::flag;
-
-audit::LogSink* Logger::GetLogSink(const std::string& tag)
-{
-       std::call_once(flag, [&]() {
-                       auto dlogSink = new audit::DlogLogSink(tag);
-                       logSink.reset(dynamic_cast<audit::LogSink*>(dlogSink));
-               });
-
-       return logSink.get();
-}
-
-} // namespace policyd
diff --git a/src/policyd/core/logger.h b/src/policyd/core/logger.h
deleted file mode 100644 (file)
index 2589467..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- *  Copyright (c) 2017 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 <klay/audit/logger.h>
-
-#include <memory>
-#include <mutex>
-
-#define VIST Logger::GetLogSink("VIST")
-
-namespace policyd {
-
-class Logger {
-public:
-       static audit::LogSink* GetLogSink(const std::string& tag);
-
-private:
-       static std::unique_ptr<audit::LogSink> logSink;
-       static std::once_flag flag;
-};
-
-} // namespace policyd
index 1b2b035538ef68d5c0d5d6d0ea910a8f2d005040..c08771e07f7b0140db5c2ff832520862818cefac 100644 (file)
@@ -16,7 +16,8 @@
 
 #include "policy-manager.h"
 #include "policy-loader.h"
-#include "logger.h"
+
+#include <vist/common/audit/logger.h>
 
 #include <klay/filesystem.h>
 
index 0029bf3204046a04382f37f4fbfffcecaa468597..31cdc7f8dc1c77f050daa822ae3a52eaafdff89b 100644 (file)
@@ -15,7 +15,8 @@
  */
 
 #include "policy-storage.h"
-#include "logger.h"
+
+#include <vist/common/audit/logger.h>
 
 #include <klay/db/column.h>
 #include <klay/db/query-builder.h>
index 2a012ce53b9bcb7d2b37bd0cc4376c8496661c7c..81124a1fa0ca967ca407de54795772c2fbf5ee65 100644 (file)
@@ -16,7 +16,8 @@
 
 #include "query.h"
 
-#include "ipc/client.h" 
+#include <vist/common/ipc/client.h>
+#include <vist/common/audit/logger.h>
 
 namespace {
        const std::string SOCK_ADDR = "/tmp/.vist";
@@ -26,6 +27,7 @@ namespace vist {
 
 Rows Query::Execute(const std::string& statement)
 {
+       INFO(VIST_CLIENT, "Query execution: " << statement);
        auto& client = ipc::Client::Instance(SOCK_ADDR);
 
        return client->methodCall<Rows>("Vist::query", statement);
index b30a4e5d366dea2f4fb0e644069bff88cf2c72d6..c3aaaec9987a4ad815e37b33a5ef19a0a15d68e6 100644 (file)
 
 #include <gtest/gtest.h>
 
-#include <osquery/logger.h>
-
 #include "../virtual-table.h"
 
 #include "../schema/time.h"
 #include "../schema/policy.h"
 #include "../schema/processes.h"
 
-using namespace osquery;
+#include <vist/common/audit/logger.h>
+
 using namespace vist;
 using namespace vist::schema;
 
@@ -39,16 +38,16 @@ TEST_F(VirtualTableTests, time_row_at) {
        result.seconds = time.at(&Time::seconds);
 
        /// Once query execution
-       LOG(INFO) << "[Test] time table:";
-       LOG(INFO) << "\t hour: " << result.hour;
-       LOG(INFO) << "\t minutes: " << result.minutes;
-       LOG(INFO) << "\t seconds: " << result.seconds;
+       INFO(VIST_CLIENT, "[Test] time table:");
+       INFO(VIST_CLIENT, "\t hour: " << result.hour);
+       INFO(VIST_CLIENT, "\t minutes: " << result.minutes);
+       INFO(VIST_CLIENT, "\t seconds: " << result.seconds);
 
        /// Each query execution
-       LOG(INFO) << "[Test] time table:";
-       LOG(INFO) << "\t hour: " << VirtualRow<Time>().at(&Time::hour);
-       LOG(INFO) << "\t minutes: " << VirtualRow<Time>().at(&Time::minutes);
-       LOG(INFO) << "\t seconds: " <<  VirtualRow<Time>().at(&Time::seconds);
+       INFO(VIST_CLIENT, "[Test] time table:");
+       INFO(VIST_CLIENT, "\t hour: " << VirtualRow<Time>().at(&Time::hour));
+       INFO(VIST_CLIENT, "\t minutes: " << VirtualRow<Time>().at(&Time::minutes));
+       INFO(VIST_CLIENT, "\t seconds: " <<  VirtualRow<Time>().at(&Time::seconds));
 
        EXPECT_NE(result.hour, -1);
        EXPECT_NE(result.minutes, -1);
@@ -64,10 +63,10 @@ TEST_F(VirtualTableTests, time_row_arry_op) {
        result.seconds = time[&Time::seconds];
 
        /// Once query execution
-       LOG(INFO) << "[Test] time table:";
-       LOG(INFO) << "\t hour: " << result.hour;
-       LOG(INFO) << "\t minutes: " << result.minutes;
-       LOG(INFO) << "\t seconds: " << result.seconds;
+       INFO(VIST_CLIENT, "[Test] time table:");
+       INFO(VIST_CLIENT, "\t hour: " << result.hour);
+       INFO(VIST_CLIENT, "\t minutes: " << result.minutes);
+       INFO(VIST_CLIENT, "\t seconds: " << result.seconds);
 
        EXPECT_NE(result.hour, -1);
        EXPECT_NE(result.minutes, -1);
@@ -92,17 +91,17 @@ TEST_F(VirtualTableTests, processes_table) {
                result.on_disk = p.at(&Processes::on_disk);
                result.parent = p.at(&Processes::parent);
 
-               LOG(INFO) << "[Test] Processes table:";
-               LOG(INFO) << "\t pid: " << result.pid;
-               LOG(INFO) << "\t name: " << result.name;
-               LOG(INFO) << "\t path: " << result.path;
-               LOG(INFO) << "\t cmdline: " << result.cmdline;
-               LOG(INFO) << "\t uid: " << result.uid;
-               LOG(INFO) << "\t gid: " << result.gid;
-               LOG(INFO) << "\t euid: " << result.euid;
-               LOG(INFO) << "\t egid: " << result.egid;
-               LOG(INFO) << "\t on_disk: " << result.on_disk;
-               LOG(INFO) << "\t parent: " << result.parent;
+               INFO(VIST_CLIENT, "[Test] Processes table:");
+               INFO(VIST_CLIENT, "\t pid: " << result.pid);
+               INFO(VIST_CLIENT, "\t name: " << result.name);
+               INFO(VIST_CLIENT, "\t path: " << result.path);
+               INFO(VIST_CLIENT, "\t cmdline: " << result.cmdline);
+               INFO(VIST_CLIENT, "\t uid: " << result.uid);
+               INFO(VIST_CLIENT, "\t gid: " << result.gid);
+               INFO(VIST_CLIENT, "\t euid: " << result.euid);
+               INFO(VIST_CLIENT, "\t egid: " << result.egid);
+               INFO(VIST_CLIENT, "\t on_disk: " << result.on_disk);
+               INFO(VIST_CLIENT, "\t parent: " << result.parent);
        }
 }
 
@@ -113,8 +112,8 @@ TEST_F(VirtualTableTests, policy_table) {
        for(const auto& row : table) {
                Policy policy = { row[&Policy::name], row[&Policy::value] };
 
-               LOG(INFO) << "[Test] Policy table:";
-               LOG(INFO) << "\t name: " << policy.name;
-               LOG(INFO) << "\t value: " << policy.value;
+               INFO(VIST_CLIENT, "[Test] Policy table:");
+               INFO(VIST_CLIENT, "\t name: " << policy.name);
+               INFO(VIST_CLIENT, "\t value: " << policy.value);
        }
 }
index a90d18ef7b802d46e0686065deb1e2cdc51d051a..2e6f372694a55ea3c70d7dfa14a609e16e60a410 100644 (file)
@@ -26,7 +26,7 @@
 #include "schema/policy.h"
 #include "schema/processes.h"
 
-#include <osquery/logger.h>
+#include <vist/common/audit/logger.h>
 
 #include <tsqb.hxx>
 
@@ -95,14 +95,14 @@ Member VirtualRow<T>::at(Member Struct::* field) const
 
        std::string value = this->data.at(key);
        if (value.empty()) {
-               LOG(ERROR) << "The value of key[" << key << "] is not exist.";
+               ERROR(VIST, "The value of key[" << key << "] is not exist.");
                return Member();
        }
 
        try {
                return boost::lexical_cast<Member>(value);
        } catch (...) {
-               LOG(ERROR) << "Failed to casting [key]: " << key;
+               ERROR(VIST, "Failed to casting [key]: " << key);
                return Member();
        }
 }
index 717a17fa8b8647c94c4873af4d48b36fbc2e0406..66c31497fee7299f893f10372af3687d5350daab 100644 (file)
@@ -12,5 +12,5 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License
 
-FILE(GLOB IPC_TESTS "ipc/tests/*.cpp")
-ADD_VIST_TEST(${IPC_TESTS})
+FILE(GLOB COMMON_TESTS "ipc/tests/*.cpp")
+ADD_VIST_TEST(${COMMON_TESTS})
diff --git a/src/vist/common/audit/logger.h b/src/vist/common/audit/logger.h
new file mode 100644 (file)
index 0000000..a4bf3ed
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  Copyright (c) 2017 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 <klay/audit/logger.h>
+#include <klay/audit/dlog-sink.h>
+
+#include <memory>
+
+#define VIST vist::Logger::Instance()
+#define VIST_CLIENT vist::Logger::ClientInstance()
+
+namespace vist {
+
+class Logger final {
+public:
+       Logger(const Logger&) = delete;
+       Logger& operator=(const Logger&) = delete;
+
+       Logger(Logger&&) = default;
+       Logger& operator=(Logger&&) = default;
+
+       static klay::LogSink* Instance()
+       {
+               static Logger instance("VIST");
+               return &instance.logSink;
+       }
+
+       static klay::LogSink* ClientInstance()
+       {
+               static Logger instance("VIST_CLIENT");
+               return &instance.logSink;
+       }
+
+private:
+       explicit Logger(const std::string& tag) : logSink(tag) {}
+       ~Logger() = default;
+
+       klay::DlogLogSink logSink;
+};
+
+} // namespace vist
index 9e78b0e242b5978365f3e408489a76950ed4c2a4..cb0ec41db064e71f588e0195f753fc8e09741465 100644 (file)
  */
 
 #include "vist.h"
-#include "ipc/server.h"
+
+#include <vist/common/ipc/server.h>
+#include <vist/common/audit/logger.h>
 
 #include <stdexcept>
 
-#include <osquery/registry.h>
+#include <osquery/registry_interface.h>
 #include <osquery/sql.h>
-#include <osquery/status.h>
 
 #define QUERY_RET_TYPE std::vector<std::map<std::string, std::string>>
 
@@ -38,6 +39,7 @@ Vist::Vist()
 
 void Vist::start()
 {
+       INFO(VIST, "Vist daemon starts.");
        auto& server = ipc::Server::Instance(SOCK_ADDR);
 
        server->expose(this, "", (QUERY_RET_TYPE)(Vist::query)(std::string));