Add string type of policy to client API
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 26 Nov 2019 08:22:24 +0000 (17:22 +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>
src/osquery/tables/tizen/policy.cpp
src/vist/client/tests/virtual-table.cpp
src/vist/client/virtual-table.cpp

index 083a409..7ba5544 100644 (file)
 #include <vist/logger.hpp>
 
 namespace osquery {
+
+namespace {
+
+Row convert(const std::string& name, const vist::policy::PolicyValue& value)
+{
+       Row r;
+       r["name"] = name;
+       r["value"] = value.dump();
+
+       return r;
+}
+
+} // anonymous namespace
+
 namespace tables {
 
 QueryData genPolicy(QueryContext& context) try {
@@ -34,23 +48,17 @@ QueryData genPolicy(QueryContext& context) try {
        if (context.constraints["name"].exists(EQUALS)) { /// where clause
                auto names = context.constraints["name"].getAll(EQUALS);
                for (const auto& name : names) {
-                       auto ret = vist::policy::API::Get(name);
-
-                       Row r;
-                       r["name"] = TEXT(name);
-                       r["value"] = TEXT(ret.dump());
+                       auto value = vist::policy::API::Get(name);
+                       auto row = convert(name, value);
 
-                       results.emplace_back(std::move(r));
+                       results.emplace_back(std::move(row));
                }
        } else { /// select *;
                auto policies = vist::policy::API::GetAll();
                for (auto& policy : policies) {
-                       Row r;
-                       INFO(VIST) << "***";
-                       r["name"] = TEXT(policy.first);
-                       r["value"] = TEXT(policy.second.dump());
+                       auto row = convert(policy.first, policy.second);
 
-                       results.emplace_back(std::move(r));
+                       results.emplace_back(std::move(row));
                }
        }
 
index 5930c5a..6ba3920 100644 (file)
@@ -105,14 +105,30 @@ TEST(VirtualTableTests, processes_table) {
        }
 }
 
-TEST(VirtualTableTests, policy_table) {
+TEST(VirtualTableTests, policy_int_table) {
        VirtualTable<Policy<int>> table;
        EXPECT_TRUE(table.size() > 0);
 
        for(const auto& row : table) {
                Policy<int> policy = { row[&Policy<int>::name], row[&Policy<int>::value] };
 
-               INFO(VIST_CLIENT) << "[Test] Policy table:";
+               INFO(VIST_CLIENT) << "[Test] Policy<int> table:";
+               INFO(VIST_CLIENT) << "\t name: " << policy.name;
+               INFO(VIST_CLIENT) << "\t value: " << policy.value;
+       }
+}
+
+TEST(VirtualTableTests, policy_str_table) {
+       VirtualTable<Policy<std::string>> table;
+       EXPECT_TRUE(table.size() > 0);
+
+       for(const auto& row : table) {
+               Policy<std::string> policy = {
+                       row[&Policy<std::string>::name],
+                       row[&Policy<std::string>::value]
+               };
+
+               INFO(VIST_CLIENT) << "[Test] Policy<std::string> table:";
                INFO(VIST_CLIENT) << "\t name: " << policy.name;
                INFO(VIST_CLIENT) << "\t value: " << policy.value;
        }
index 8dd6e0c..b8deacf 100644 (file)
@@ -50,11 +50,15 @@ auto processes = make_table("processes",
                                                        make_column("on_disk", &Processes::on_disk),
                                                        make_column("parent", &Processes::parent));
 
-auto policy = make_table("policy",
+auto policyInt = make_table("policy",
                                                 make_column("name", &Policy<int>::name),
                                                 make_column("value", &Policy<int>::value));
 
-auto metaDB = make_database("db", time, processes, policy);
+auto policyStr = make_table("policy",
+                                                       make_column("name", &Policy<std::string>::name),
+                                                       make_column("value", &Policy<std::string>::value));
+
+auto metaDB = make_database("db", time, processes, policyInt, policyStr);
 
 } // anonymous namespace
 
@@ -117,8 +121,16 @@ template <typename T>
 VirtualTable<T>::VirtualTable()
 {
        auto results = Query::Execute(metaDB.selectAll<T>());
-       for (auto& r : results)
-               this->dataset.emplace_back(VirtualRow<T>(std::move(r)));
+       for (auto& row : results) {
+               /// Filter unsafe(unmatched) type
+               if (std::is_same<T, Policy<int>>::value && row["value"].find("I/") == std::string::npos)
+                       continue;
+               else if (std::is_same<T, Policy<std::string>>::value &&
+                                row["value"].find("S/") == std::string::npos)
+                       continue;
+
+               this->dataset.emplace_back(VirtualRow<T>(std::move(row)));
+       }
 }
 
 /// Explicit instantiation
@@ -145,4 +157,11 @@ template std::string VirtualRow<Policy<int>>::operator[](std::string Policy<int>
 template int VirtualRow<Policy<int>>::at(int Policy<int>::*) const;
 template int VirtualRow<Policy<int>>::operator[](int Policy<int>::*) const;
 
+template class VirtualTable<Policy<std::string>>;
+template class VirtualRow<Policy<std::string>>;
+template
+std::string VirtualRow<Policy<std::string>>::at(std::string Policy<std::string>::*) const;
+template
+std::string VirtualRow<Policy<std::string>>::operator[](std::string Policy<std::string>::*) const;
+
 } // namespace vist