osquery-1/osquery/config/config.cpp

86 lines
2.3 KiB
C++
Raw Normal View History

2014-07-31 00:35:19 +00:00
// Copyright 2004-present Facebook. All Rights Reserved.
#include <algorithm>
#include <future>
#include <sstream>
#include <string>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <glog/logging.h>
#include "osquery/config.h"
#include "osquery/config/plugin.h"
#include "osquery/flags.h"
#include "osquery/status.h"
2014-07-31 00:35:19 +00:00
using osquery::Status;
2014-07-31 00:35:19 +00:00
namespace pt = boost::property_tree;
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-07-31 00:35:19 +00:00
DEFINE_osquery_flag(string,
config_retriever,
"filesystem",
"The config mechanism to retrieve config content via.");
2014-07-31 00:35:19 +00:00
boost::shared_mutex rw_lock;
std::shared_ptr<Config> Config::getInstance() {
static std::shared_ptr<Config> config = std::shared_ptr<Config>(new Config());
return config;
}
Config::Config() {
boost::unique_lock<boost::shared_mutex> lock(rw_lock);
OsqueryConfig conf;
auto s = Config::genConfig(conf);
if (!s.ok()) {
LOG(ERROR) << "error retrieving config: " << s.toString();
}
cfg_ = conf;
}
Status Config::genConfig(OsqueryConfig& conf) {
std::stringstream json;
pt::ptree tree;
if (REGISTERED_CONFIG_PLUGINS.find(FLAGS_config_retriever) ==
REGISTERED_CONFIG_PLUGINS.end()) {
LOG(ERROR) << "Config retriever " << FLAGS_config_retriever << " not found";
return Status(1, "Config retriever not found");
}
auto config_data =
2014-08-15 07:25:30 +00:00
REGISTERED_CONFIG_PLUGINS.at(FLAGS_config_retriever)->genConfig();
2014-07-31 00:35:19 +00:00
if (!config_data.first.ok()) {
return config_data.first;
}
json << config_data.second;
pt::read_json(json, tree);
try {
2014-08-15 07:25:30 +00:00
for (const pt::ptree::value_type& v : tree.get_child("scheduledQueries")) {
2014-07-31 00:35:19 +00:00
OsqueryScheduledQuery q;
q.name = (v.second).get<std::string>("name");
q.query = (v.second).get<std::string>("query");
q.interval = (v.second).get<int>("interval");
conf.scheduledQueries.push_back(q);
}
2014-09-21 21:29:28 +00:00
} catch (const std::exception& e) {
2014-07-31 00:35:19 +00:00
LOG(ERROR) << "Error parsing config JSON: " << e.what();
return Status(1, e.what());
}
return Status(0, "OK");
}
2014-09-15 18:17:48 +00:00
std::vector<OsqueryScheduledQuery> Config::getScheduledQueries() {
2014-07-31 00:35:19 +00:00
boost::shared_lock<boost::shared_mutex> lock(rw_lock);
return cfg_.scheduledQueries;
}
2014-08-15 07:25:30 +00:00
}