#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 {
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));
}
}
}
}
-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;
}
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
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
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