Add archive type to policy-value as buffer
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 18 Nov 2019 06:55:04 +0000 (15:55 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 22 Nov 2019 01:52:17 +0000 (10:52 +0900)
This is for supporting Generic Value Type.

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/osquery/CMakeLists.txt
src/osquery/tables/tizen/policy.cpp
src/vist/archive.hpp
src/vist/policy/policy-manager.cpp
src/vist/policy/policy-storage.cpp
src/vist/policy/tests/core.cpp
src/vist/policy/tests/storage.cpp
src/vist/sdk/policy-model.hpp
src/vist/sdk/policy-value.hpp
src/vist/sdk/tests/sdk.cpp

index 90e7f9a431651e58475bdf7c6eb43e1fd439fcbd..7cdc268abcc8fbd6f9adc99a0c8037ba47367fa5 100644 (file)
@@ -68,7 +68,8 @@ ADD_LIBRARY(${TARGET_OSQUERY_LIB}
 TARGET_LINK_LIBRARIES(${TARGET_OSQUERY_LIB} ${${TARGET_OSQUERY_LIB}_DEPS})
 
 IF(DEFINED GBS_BUILD)
-TARGET_LINK_LIBRARIES(${TARGET_OSQUERY_LIB} ${TARGET_VIST_POLICY_LIB})
+TARGET_LINK_LIBRARIES(${TARGET_OSQUERY_LIB} ${TARGET_VIST_COMMON_LIB}
+                                                                                       ${TARGET_VIST_POLICY_LIB})
 ENDIF(DEFINED GBS_BUILD)
 
 SET_TARGET_PROPERTIES(${TARGET_OSQUERY_LIB} PROPERTIES OUTPUT_NAME ${TARGET_OSQUERY_LIB})
index e2473f982f7da8ce49c409fa3416ff33086380c3..e29a5ce3cfdd7943777ad9bf1380c871066e7209 100644 (file)
@@ -35,19 +35,22 @@ QueryData genPolicy(QueryContext& context) try {
                auto names = context.constraints["name"].getAll(EQUALS);
                for (const auto& name : names) {
                        auto ret = vist::policy::API::Get(name);
+                       int value = ret;
 
                        Row r;
                        r["name"] = TEXT(name);
-                       r["value"] = TEXT(ret.value);
+                       r["value"] = TEXT(value);
 
                        results.emplace_back(std::move(r));
                }
        } else { /// select *;
                auto policies = vist::policy::API::GetAll();
                for (auto& policy : policies) {
+                       int value = policy.second;
+
                        Row r;
                        r["name"] = TEXT(policy.first);
-                       r["value"] = TEXT(policy.second);
+                       r["value"] = TEXT(value);
 
                        results.emplace_back(std::move(r));
                }
index 3d72296bb4784a150477b5487e049ea90e70ca05..f1d525abab7728cc66bf4f4e44fcec45380fcaf5 100644 (file)
@@ -42,10 +42,8 @@ using IsFundamental = typename std::enable_if<std::is_fundamental<T>::value, int
 template<typename T>
 using IsArchival = typename std::enable_if<std::is_base_of<Archival, T>::value, int>::type;
 
-class Archive {
+class Archive final {
 public:
-       virtual ~Archive() = default;
-
        template<typename Front, typename... Rest>
        void pack(const Front& front, const Rest&... rest);
        inline void pack(void) {}
index 6fbda6f939b2423c8328be763f72b9df5e981258..5c6670159ae97d4377d186c846ab609a99b5c072 100644 (file)
@@ -86,7 +86,8 @@ int PolicyManager::loadPolicies()
                        /// Check the policy is defined on policy-storage
                        if (!storage.exists(pair.first)) {
                                INFO(VIST) << "Define policy: " << pair.first;
-                               storage.define(pair.first, pair.second->getInitial().value);
+                               int value = pair.second->getInitial();
+                               storage.define(pair.first, value);
                                changed = true;
                        }
                }
index 596d3614bb8e3b805567d868b89ea45bf25f0795..22fb7f34b84e5dc8e7ba442c8fb39f4250490edc 100644 (file)
@@ -199,8 +199,9 @@ void PolicyStorage::update(const std::string& admin,
                                                   const std::string& policy,
                                                   const PolicyValue& value)
 {
+       int policyValue = value;
        DEBUG(VIST) << "Policy-update is called by admin: " << admin
-                               << ", about: " << policy << ", value: " << std::to_string(value);
+                               << ", about: " << policy << ", value: " << policyValue;
 
        if (std::find(admins.begin(), admins.end(), admin) == admins.end())
                THROW(ErrCode::LogicError) << "Not exist admin: " << admin;
@@ -208,7 +209,6 @@ void PolicyStorage::update(const std::string& admin,
        if (definitions.find(policy) == definitions.end())
                THROW(ErrCode::LogicError) << "Not exist policy: " << policy;
 
-       int policyValue = value;
        std::string query = polActivatedTable.update(&PolicyActivated::value)
                                                                                 .where(expr(&PolicyActivated::admin) == admin &&
                                                                                  expr(&PolicyActivated::policy) == policy);
@@ -235,22 +235,26 @@ PolicyValue PolicyStorage::strictest(const std::string& policy)
        std::shared_ptr<PolicyValue> strictestPtr = nullptr;
        auto range = activatedPolicies.equal_range(policy);
        for (auto iter = range.first; iter != range.second; iter++) {
+               int value = iter->second.value;
                DEBUG(VIST) << "Admin: " << iter->second.admin << ", "
                                        << "Policy: " << iter->second.policy  << ", "
-                                       << "Value: " << std::to_string(iter->second.value);
+                                       << "Value: " << value;
 
-               int value = iter->second.value;
-               if (strictestPtr == nullptr)
+               if (strictestPtr == nullptr) {
                        strictestPtr = std::make_shared<PolicyValue>(value);
-               else
-                       strictestPtr->value = (strictestPtr->value > value) ? strictestPtr->value : value;
+               } else {
+                       int strictestValue = *strictestPtr;
+                       if (strictestValue < value)
+                               strictestPtr.reset(new PolicyValue(value));
+               }
        }
 
        if (strictestPtr == nullptr)
                THROW(ErrCode::RuntimeError) << "Not exist managed policy: " << policy;
 
+       int strictestValue = *strictestPtr;
        DEBUG(VIST) << "The strictest value of [" << policy
-                               << "] is " << std::to_string(strictestPtr->value);
+                               << "] is " << strictestValue; 
 
        return std::move(*strictestPtr);
 }
index 0a764d98984971d7e8c47bbd3c258e9e93142501..0c0a416c4d1e423cdfbcec878090de05be62433d 100644 (file)
@@ -36,14 +36,14 @@ TEST_F(PolicyCoreTests, policy_set_get) {
        manager.set("bluetooth", PolicyValue(5), "testAdmin");
 
        auto policy = manager.get("bluetooth");
-       EXPECT_EQ(policy.value, 5);
+       EXPECT_EQ((int)policy, 5);
 
        manager.enroll("testAdmin1");
        manager.set("bluetooth", PolicyValue(10), "testAdmin1");
 
        /// Manager should return the strongest policy.
        policy = manager.get("bluetooth");
-       EXPECT_EQ(policy.value, 10);
+       EXPECT_EQ((int)policy, 10);
 
        manager.disenroll("testAdmin");
        manager.disenroll("testAdmin1");
index 026805584a6354b713ebdd8b922a7495136f00c1..58d9a1f961f2a110d576dbb5ebe0325df4069cdd 100644 (file)
@@ -118,7 +118,7 @@ TEST_F(PolicyStorageTests, strictest)
        EXPECT_TRUE(isRaised);
 
        auto policy = storage->strictest("bluetooth");
-       EXPECT_EQ(policy.value, 6);
+       EXPECT_EQ((int)policy, 6);
 
        storage->disenroll("testAdmin0");
        storage->disenroll("testAdmin1");
index 219495380890eef97483c0f5ecf2446487fe7f13..4cfbb538ad7c62732afdec3fa0692d8f04c255d6 100644 (file)
@@ -38,17 +38,14 @@ public:
 
        inline void set(const PolicyValue& value)
        {
-               auto rollback = current;
-               current = value;
-               ready = true;
-
                try {
                        this->onChanged(value);
                } catch (const std::exception& e) {
-                       current = rollback;
-                       ready = false;
                        std::rethrow_exception(std::current_exception());
                }
+
+               this->current = value;
+               this->ready = true;
        }
 
        inline const PolicyValue& get() const
@@ -62,7 +59,7 @@ public:
        virtual void onChanged(const PolicyValue& value) = 0;
 
        const std::string& getName() const noexcept { return name; }
-       const PolicyValue& getInitial() const noexcept { return initial; }
+       PolicyValue& getInitial() noexcept { return initial; }
 
 protected:
        std::string name;
index e1b2464385fa32f86ef7c1a65a8b605367fdca62..75f895ce913aeaaefd7b80a88b84c7f786f693fe 100644 (file)
 
 #pragma once
 
+#include <vist/archive.hpp>
+
+#include <string>
+
 namespace vist {
 namespace policy {
 
 // TODO: Support various value type
 struct PolicyValue final {
-       explicit PolicyValue(int value) noexcept : value(value) {}
+       enum class Type {
+               Integer,
+               String,
+               None
+       };
+
+       explicit PolicyValue(int value) : type(Type::Integer)
+       {
+               this->buffer << value;
+       }
+
+       explicit PolicyValue(const std::string& value) : type(Type::String)
+       {
+               this->buffer << value;
+       }
+
        explicit PolicyValue() noexcept = default;
        ~PolicyValue() = default;
 
-       PolicyValue(const PolicyValue&) noexcept = default;
-       PolicyValue& operator=(const PolicyValue&) noexcept = default;
+       PolicyValue(const PolicyValue&) = default;
+       PolicyValue& operator=(const PolicyValue&) = default;
 
        PolicyValue(PolicyValue&&) noexcept = default;
        PolicyValue& operator=(PolicyValue&&) noexcept = default;
 
-       PolicyValue& operator=(int val) {
-               value = val;
-               return *this;
+       inline Type getType() const noexcept
+       {
+               return this->type;
+       }
+
+       operator int() const
+       {
+               auto clone = this->buffer;
+               int out;
+               clone >> out;
+
+               return out;
        }
 
-       operator int() const { return value; }
-       bool operator==(const PolicyValue& rhs) const { return value == rhs.value; }
-       bool operator!=(const PolicyValue& rhs) const { return value != rhs.value; }
-       bool operator<(const PolicyValue& rhs) const { return value < rhs.value; }
+       operator std::string() const
+       {
+               auto clone = this->buffer;
+               std::string out;
+               clone >> out;
+
+               return out;
+       }
 
-       int value = -1;
+private:
+       Archive buffer;
+       Type type = Type::None;
 };
 
 } // namespace policy
index d6773b3a63a11ac2127bebfb9327821a469b6552..2c8942bfb1608cf94ca742050efe4f824c1ce453 100644 (file)
@@ -27,8 +27,6 @@ namespace {
 
 using namespace vist::policy;
 
-class PolicySDKTests : public testing::Test {};
-
 class TestPolicyModel : public PolicyModel {
 public:
        TestPolicyModel() : PolicyModel("test_policy", PolicyValue(1)) {}
@@ -49,7 +47,21 @@ public:
        }
 };
 
-TEST_F(PolicySDKTests, policy_model)
+TEST(PolicySDKTests, policy_value_int)
+{
+       PolicyValue value(1);
+       EXPECT_EQ(PolicyValue::Type::Integer, value.getType());
+       EXPECT_EQ((int)value, 1);
+}
+
+TEST(PolicySDKTests, policy_value_string)
+{
+       PolicyValue value(std::string("text"));
+       EXPECT_EQ(PolicyValue::Type::String, value.getType());
+       EXPECT_EQ((std::string)value, "text");
+}
+
+TEST(PolicySDKTests, policy_model)
 {
        TestPolicyModel policy;
 
@@ -71,7 +83,7 @@ TEST_F(PolicySDKTests, policy_model)
        EXPECT_EQ(3, policy.get());
 }
 
-TEST_F(PolicySDKTests, policy_model_failed)
+TEST(PolicySDKTests, policy_model_failed)
 {
        TestPolicyModelFailed policy;
 
@@ -88,7 +100,7 @@ TEST_F(PolicySDKTests, policy_model_failed)
        EXPECT_TRUE(isRaised);
 }
 
-TEST_F(PolicySDKTests, policy_provider)
+TEST(PolicySDKTests, policy_provider)
 {
        PolicyProvider provider("testProvider");
        provider.add(std::make_shared<TestPolicyModel>());