2015-01-23 22:52:07 +00:00
|
|
|
/*
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2015-01-23 22:52:07 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
2016-01-21 08:23:05 +00:00
|
|
|
|
2015-01-23 22:52:07 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <osquery/core.h>
|
2015-09-07 18:09:06 +00:00
|
|
|
#include <osquery/distributed.h>
|
2015-09-16 17:36:34 +00:00
|
|
|
#include <osquery/enroll.h>
|
2015-01-23 22:52:07 +00:00
|
|
|
#include <osquery/sql.h>
|
|
|
|
|
2016-09-02 22:04:03 +00:00
|
|
|
#include "osquery/core/json.h"
|
2015-02-23 05:56:52 +00:00
|
|
|
#include "osquery/sql/sqlite_util.h"
|
2016-11-03 00:08:44 +00:00
|
|
|
#include "osquery/tests/test_additional_util.h"
|
2016-09-02 22:04:03 +00:00
|
|
|
#include "osquery/tests/test_util.h"
|
2015-01-23 22:52:07 +00:00
|
|
|
|
|
|
|
namespace pt = boost::property_tree;
|
|
|
|
|
2015-09-16 17:36:34 +00:00
|
|
|
DECLARE_string(distributed_tls_read_endpoint);
|
|
|
|
DECLARE_string(distributed_tls_write_endpoint);
|
|
|
|
|
2015-01-23 22:52:07 +00:00
|
|
|
namespace osquery {
|
|
|
|
|
2015-09-16 17:36:34 +00:00
|
|
|
class DistributedTests : public testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() {
|
2015-09-07 18:09:06 +00:00
|
|
|
TLSServerRunner::start();
|
2016-01-21 08:23:05 +00:00
|
|
|
TLSServerRunner::setClientConfig();
|
2015-09-16 17:36:34 +00:00
|
|
|
clearNodeKey();
|
|
|
|
|
|
|
|
distributed_tls_read_endpoint_ =
|
|
|
|
Flag::getValue("distributed_tls_read_endpoint");
|
|
|
|
Flag::updateValue("distributed_tls_read_endpoint", "/distributed_read");
|
|
|
|
|
|
|
|
distributed_tls_write_endpoint_ =
|
|
|
|
Flag::getValue("distributed_tls_write_endpoint");
|
|
|
|
Flag::updateValue("distributed_tls_write_endpoint", "/distributed_write");
|
2015-01-23 22:52:07 +00:00
|
|
|
|
2015-09-16 17:36:34 +00:00
|
|
|
Registry::setActive("distributed", "tls");
|
2015-09-07 18:09:06 +00:00
|
|
|
}
|
2015-01-23 22:52:07 +00:00
|
|
|
|
2015-09-16 17:36:34 +00:00
|
|
|
void TearDown() {
|
|
|
|
TLSServerRunner::stop();
|
2016-01-21 08:23:05 +00:00
|
|
|
TLSServerRunner::unsetClientConfig();
|
2015-09-16 17:36:34 +00:00
|
|
|
clearNodeKey();
|
2016-01-21 08:23:05 +00:00
|
|
|
|
2015-09-16 17:36:34 +00:00
|
|
|
Flag::updateValue("distributed_tls_read_endpoint",
|
|
|
|
distributed_tls_read_endpoint_);
|
|
|
|
Flag::updateValue("distributed_tls_write_endpoint",
|
|
|
|
distributed_tls_write_endpoint_);
|
2015-09-07 18:09:06 +00:00
|
|
|
}
|
2015-01-23 22:52:07 +00:00
|
|
|
|
2016-01-21 08:23:05 +00:00
|
|
|
protected:
|
2015-09-16 17:36:34 +00:00
|
|
|
std::string distributed_tls_read_endpoint_;
|
|
|
|
std::string distributed_tls_write_endpoint_;
|
2015-09-07 18:09:06 +00:00
|
|
|
};
|
2015-01-23 22:52:07 +00:00
|
|
|
|
2016-03-05 17:29:51 +00:00
|
|
|
TEST_F(DistributedTests, test_serialize_distributed_query_request) {
|
|
|
|
DistributedQueryRequest r;
|
|
|
|
r.query = "foo";
|
|
|
|
r.id = "bar";
|
|
|
|
|
|
|
|
pt::ptree tree;
|
|
|
|
auto s = serializeDistributedQueryRequest(r, tree);
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(tree.get<std::string>("query"), "foo");
|
|
|
|
EXPECT_EQ(tree.get<std::string>("id"), "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DistributedTests, test_deserialize_distributed_query_request) {
|
|
|
|
pt::ptree tree;
|
|
|
|
tree.put<std::string>("query", "foo");
|
|
|
|
tree.put<std::string>("id", "bar");
|
|
|
|
|
|
|
|
DistributedQueryRequest r;
|
|
|
|
auto s = deserializeDistributedQueryRequest(tree, r);
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(r.query, "foo");
|
|
|
|
EXPECT_EQ(r.id, "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DistributedTests, test_deserialize_distributed_query_request_json) {
|
|
|
|
auto json =
|
|
|
|
"{"
|
|
|
|
" \"query\": \"foo\","
|
|
|
|
" \"id\": \"bar\""
|
|
|
|
"}";
|
|
|
|
|
|
|
|
DistributedQueryRequest r;
|
|
|
|
auto s = deserializeDistributedQueryRequestJSON(json, r);
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(r.query, "foo");
|
|
|
|
EXPECT_EQ(r.id, "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DistributedTests, test_serialize_distributed_query_result) {
|
|
|
|
DistributedQueryResult r;
|
|
|
|
r.request.query = "foo";
|
|
|
|
r.request.id = "bar";
|
|
|
|
|
|
|
|
Row r1;
|
|
|
|
r1["foo"] = "bar";
|
|
|
|
r.results = {r1};
|
|
|
|
|
|
|
|
pt::ptree tree;
|
|
|
|
auto s = serializeDistributedQueryResult(r, tree);
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(tree.get<std::string>("request.query"), "foo");
|
|
|
|
EXPECT_EQ(tree.get<std::string>("request.id"), "bar");
|
|
|
|
auto& results = tree.get_child("results");
|
|
|
|
for (const auto& q : results) {
|
|
|
|
for (const auto& row : q.second) {
|
|
|
|
EXPECT_EQ(row.first, "foo");
|
|
|
|
EXPECT_EQ(q.second.get<std::string>(row.first), "bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DistributedTests, test_deserialize_distributed_query_result) {
|
|
|
|
pt::ptree request;
|
|
|
|
request.put<std::string>("id", "foo");
|
|
|
|
request.put<std::string>("query", "bar");
|
|
|
|
|
|
|
|
pt::ptree row;
|
|
|
|
row.put<std::string>("foo", "bar");
|
|
|
|
pt::ptree results;
|
|
|
|
results.push_back(std::make_pair("", row));
|
|
|
|
|
|
|
|
pt::ptree query_result;
|
|
|
|
query_result.put_child("request", request);
|
|
|
|
query_result.put_child("results", results);
|
|
|
|
|
|
|
|
DistributedQueryResult r;
|
|
|
|
auto s = deserializeDistributedQueryResult(query_result, r);
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(r.request.id, "foo");
|
|
|
|
EXPECT_EQ(r.request.query, "bar");
|
|
|
|
EXPECT_EQ(r.results[0]["foo"], "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DistributedTests, test_deserialize_distributed_query_result_json) {
|
|
|
|
auto json =
|
|
|
|
"{"
|
|
|
|
" \"request\": {"
|
|
|
|
" \"id\": \"foo\","
|
|
|
|
" \"query\": \"bar\""
|
|
|
|
" },"
|
|
|
|
" \"results\": ["
|
|
|
|
" {"
|
|
|
|
" \"foo\": \"bar\""
|
|
|
|
" }"
|
|
|
|
" ]"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
DistributedQueryResult r;
|
|
|
|
auto s = deserializeDistributedQueryResultJSON(json, r);
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(r.request.id, "foo");
|
|
|
|
EXPECT_EQ(r.request.query, "bar");
|
|
|
|
EXPECT_EQ(r.results[0]["foo"], "bar");
|
|
|
|
}
|
|
|
|
|
2015-09-07 18:09:06 +00:00
|
|
|
TEST_F(DistributedTests, test_workflow) {
|
|
|
|
auto dist = Distributed();
|
|
|
|
auto s = dist.pullUpdates();
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(s.toString(), "OK");
|
2015-01-23 22:52:07 +00:00
|
|
|
|
2015-10-20 06:08:01 +00:00
|
|
|
EXPECT_EQ(dist.getPendingQueryCount(), 2U);
|
|
|
|
EXPECT_EQ(dist.results_.size(), 0U);
|
2015-09-07 18:09:06 +00:00
|
|
|
s = dist.runQueries();
|
|
|
|
EXPECT_TRUE(s.ok());
|
|
|
|
EXPECT_EQ(s.toString(), "OK");
|
2015-09-16 17:36:34 +00:00
|
|
|
|
2015-10-20 06:08:01 +00:00
|
|
|
EXPECT_EQ(dist.getPendingQueryCount(), 0U);
|
2016-03-25 18:17:48 +00:00
|
|
|
EXPECT_EQ(dist.results_.size(), 0U);
|
2015-01-23 22:52:07 +00:00
|
|
|
}
|
|
|
|
}
|