From 4abed8036a2e7f1c0035901694f671d243996ce1 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Tue, 26 Nov 2019 17:22:24 +0900 Subject: [PATCH] Add string type of policy to client API Signed-off-by: Sangwan Kwon --- src/osquery/tables/tizen/policy.cpp | 30 +++++++++++++++++++----------- src/vist/client/tests/virtual-table.cpp | 20 ++++++++++++++++++-- src/vist/client/virtual-table.cpp | 27 +++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/osquery/tables/tizen/policy.cpp b/src/osquery/tables/tizen/policy.cpp index 083a409..7ba5544 100644 --- a/src/osquery/tables/tizen/policy.cpp +++ b/src/osquery/tables/tizen/policy.cpp @@ -25,6 +25,20 @@ #include 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)); } } diff --git a/src/vist/client/tests/virtual-table.cpp b/src/vist/client/tests/virtual-table.cpp index 5930c5a..6ba3920 100644 --- a/src/vist/client/tests/virtual-table.cpp +++ b/src/vist/client/tests/virtual-table.cpp @@ -105,14 +105,30 @@ TEST(VirtualTableTests, processes_table) { } } -TEST(VirtualTableTests, policy_table) { +TEST(VirtualTableTests, policy_int_table) { VirtualTable> table; EXPECT_TRUE(table.size() > 0); for(const auto& row : table) { Policy policy = { row[&Policy::name], row[&Policy::value] }; - INFO(VIST_CLIENT) << "[Test] Policy table:"; + INFO(VIST_CLIENT) << "[Test] Policy table:"; + INFO(VIST_CLIENT) << "\t name: " << policy.name; + INFO(VIST_CLIENT) << "\t value: " << policy.value; + } +} + +TEST(VirtualTableTests, policy_str_table) { + VirtualTable> table; + EXPECT_TRUE(table.size() > 0); + + for(const auto& row : table) { + Policy policy = { + row[&Policy::name], + row[&Policy::value] + }; + + INFO(VIST_CLIENT) << "[Test] Policy table:"; INFO(VIST_CLIENT) << "\t name: " << policy.name; INFO(VIST_CLIENT) << "\t value: " << policy.value; } diff --git a/src/vist/client/virtual-table.cpp b/src/vist/client/virtual-table.cpp index 8dd6e0c..b8deacf 100644 --- a/src/vist/client/virtual-table.cpp +++ b/src/vist/client/virtual-table.cpp @@ -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::name), make_column("value", &Policy::value)); -auto metaDB = make_database("db", time, processes, policy); +auto policyStr = make_table("policy", + make_column("name", &Policy::name), + make_column("value", &Policy::value)); + +auto metaDB = make_database("db", time, processes, policyInt, policyStr); } // anonymous namespace @@ -117,8 +121,16 @@ template VirtualTable::VirtualTable() { auto results = Query::Execute(metaDB.selectAll()); - for (auto& r : results) - this->dataset.emplace_back(VirtualRow(std::move(r))); + for (auto& row : results) { + /// Filter unsafe(unmatched) type + if (std::is_same>::value && row["value"].find("I/") == std::string::npos) + continue; + else if (std::is_same>::value && + row["value"].find("S/") == std::string::npos) + continue; + + this->dataset.emplace_back(VirtualRow(std::move(row))); + } } /// Explicit instantiation @@ -145,4 +157,11 @@ template std::string VirtualRow>::operator[](std::string Policy template int VirtualRow>::at(int Policy::*) const; template int VirtualRow>::operator[](int Policy::*) const; +template class VirtualTable>; +template class VirtualRow>; +template +std::string VirtualRow>::at(std::string Policy::*) const; +template +std::string VirtualRow>::operator[](std::string Policy::*) const; + } // namespace vist -- 2.7.4