Add sample plugin
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 26 Nov 2019 06:23:01 +0000 (15:23 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Thu, 28 Nov 2019 04:36:35 +0000 (13:36 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
packaging/vist.spec
plugins/CMakeLists.txt
plugins/sample/CMakeLists.txt [new file with mode: 0644]
plugins/sample/sample.cpp [new file with mode: 0644]
src/vist/policy/policy-storage.cpp
src/vist/policy/tests/core.cpp

index e902ba2..66e834b 100644 (file)
@@ -113,6 +113,7 @@ Provides internal testcases for ViST implementation.
 %files test
 %{_bindir}/osquery-test
 %{_bindir}/vist-test
+%{vist_plugin_dir}/sample
 
 ## ViST Plugins - ###########################################################
 %package plugins
index 4055891..c0bde83 100644 (file)
@@ -17,6 +17,8 @@ INCLUDE(FindPkgConfig)
 
 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack")
 
+ADD_SUBDIRECTORY(sample)
+
 ADD_SUBDIRECTORY(bluetooth)
 ADD_SUBDIRECTORY(wifi)
 #ADD_SUBDIRECTORY(usb)
diff --git a/plugins/sample/CMakeLists.txt b/plugins/sample/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e203969
--- /dev/null
@@ -0,0 +1,25 @@
+# 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.
+#
+SET(TARGET "vist-plugin-sample")
+
+INCLUDE_DIRECTORIES(SYSTEM)
+
+ADD_LIBRARY(${TARGET} SHARED sample.cpp)
+SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default")
+TARGET_LINK_LIBRARIES(${TARGET} vist-common)
+
+INSTALL(FILES libvist-plugin-sample.so
+               RENAME sample
+               DESTINATION ${PLUGIN_INSTALL_DIR})
diff --git a/plugins/sample/sample.cpp b/plugins/sample/sample.cpp
new file mode 100644 (file)
index 0000000..5327fb1
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ *  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/sdk/policy-model.hpp>
+#include <vist/sdk/policy-value.hpp>
+#include <vist/sdk/policy-provider.hpp>
+
+#include <vist/logger.hpp>
+
+#include <memory>
+#include <string>
+
+using namespace vist;
+using namespace vist::policy;
+
+class SampleIntPolicy : public PolicyModel {
+public:
+       SampleIntPolicy() : PolicyModel("sample-int-policy", PolicyValue(7))
+       {
+       }
+
+       void onChanged(const PolicyValue& value) override
+       {
+               INFO(VIST_PLUGIN) << "sample-int-policy is changed to "
+                                                 << static_cast<int>(value);
+       }
+};
+
+class SampleStrPolicy : public PolicyModel {
+public:
+       SampleStrPolicy() : PolicyModel("sample-str-policy", PolicyValue("TEST"))
+       {
+       }
+
+       void onChanged(const PolicyValue& value) override
+       {
+               INFO(VIST_PLUGIN) << "sample-str-policy is changed to "
+                                                 << static_cast<std::string>(value);
+       }
+
+       int compare(const PolicyValue& lhs, const PolicyValue& rhs) const override
+       {
+               std::string lvalue = lhs;
+               std::string rvalue = rhs;
+
+               INFO(VIST_PLUGIN) << "Custom compare method is called with [" << lvalue
+                                                 << "], [" << rvalue << "]";
+
+               return lvalue.compare(rvalue);
+       }
+};
+
+class Sample : public PolicyProvider {
+public:
+       Sample(const std::string& name) : PolicyProvider(name)
+       {
+       }
+
+       ~Sample()
+       {
+       }
+};
+
+extern "C" PolicyProvider* PolicyFactory()
+{
+       INFO(VIST_PLUGIN) << "Sample plugin loaded.";
+       Sample* provider = new Sample("sample");
+       provider->add(std::make_shared<SampleIntPolicy>());
+       provider->add(std::make_shared<SampleStrPolicy>());
+
+       return provider;
+}
index f7d777f..b51e202 100644 (file)
@@ -225,8 +225,6 @@ void PolicyStorage::update(const std::string& admin,
        this->syncPolicyActivated();
 }
 
-/// TODO(sangwan.kwon) Re-design strictest logic
-/// PolicyValue PolicyStorage::strictest(const PolicyValue& policy)
 PolicyValue PolicyStorage::strictest(const std::shared_ptr<PolicyModel>& policy)
 {
        if (this->definitions.find(policy->getName()) == this->definitions.end())
index 8c1ca83..3166887 100644 (file)
@@ -28,25 +28,44 @@ TEST(PolicyCoreTests, policy_loader) {
        EXPECT_TRUE(manager.policies.size() > 0);
 }
 
-TEST(PolicyCoreTests, policy_set_get) {
+TEST(PolicyCoreTests, policy_set_get_int) {
        auto& manager = PolicyManager::Instance();
        manager.enroll("testAdmin");
-       manager.set("bluetooth", PolicyValue(5), "testAdmin");
+       manager.set("sample-int-policy", PolicyValue(5), "testAdmin");
 
-       auto policy = manager.get("bluetooth");
+       auto policy = manager.get("sample-int-policy");
        EXPECT_EQ(static_cast<int>(policy), 5);
 
        manager.enroll("testAdmin1");
-       manager.set("bluetooth", PolicyValue(10), "testAdmin1");
+       manager.set("sample-int-policy", PolicyValue(10), "testAdmin1");
 
        /// Manager should return the strongest policy.
-       policy = manager.get("bluetooth");
+       policy = manager.get("sample-int-policy");
        EXPECT_EQ(static_cast<int>(policy), 10);
 
        manager.disenroll("testAdmin");
        manager.disenroll("testAdmin1");
 }
 
+TEST(PolicyCoreTests, policy_set_get_str) {
+       auto& manager = PolicyManager::Instance();
+       manager.enroll("testAdmin");
+       manager.set("sample-str-policy", PolicyValue("AAA"), "testAdmin");
+
+       auto policy = manager.get("sample-str-policy");
+       EXPECT_EQ(static_cast<std::string>(policy), "AAA");
+
+       manager.enroll("testAdmin1");
+       manager.set("sample-str-policy", PolicyValue("BBB"), "testAdmin1");
+
+       /// Manager should return the strongest policy.
+       policy = manager.get("sample-str-policy");
+       EXPECT_EQ(static_cast<std::string>(policy), "AAA");
+
+       manager.disenroll("testAdmin");
+       manager.disenroll("testAdmin1");
+}
+
 TEST(PolicyCoreTests, policy_get_all) {
        auto& manager = PolicyManager::Instance();
        auto policies = manager.getAll();
@@ -55,8 +74,8 @@ TEST(PolicyCoreTests, policy_get_all) {
 
 TEST(PolicyCoreTests, policy_get_policy) {
        auto& manager = PolicyManager::Instance();
-       const auto& policy = manager.getPolicy("bluetooth");
-       EXPECT_EQ(policy->getName(), "bluetooth");
+       const auto& policy = manager.getPolicy("sample-int-policy");
+       EXPECT_EQ(policy->getName(), "sample-int-policy");
 
        bool raised = false;
        try {