policy: Add update query to bluetooth vtab
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 24 Feb 2020 08:12:45 +0000 (17:12 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 25 Feb 2020 05:08:55 +0000 (14:08 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
plugins/bluetooth/CMakeLists.txt
plugins/bluetooth/bluetooth-test.cpp
specs/tizen/bluetooth.table
src/osquery/tables/tizen/bluetooth.cpp
src/vist/client/schema/bluetooth.hpp
src/vist/rmi/impl/server.cpp

index 9b4e231f0e2ab99ce2b9dff1a9f5158628a3bf03..07a23466154d07f63265bec8fda3e9b5a027e90d 100644 (file)
@@ -38,6 +38,7 @@ ADD_EXECUTABLE(${TARGET_TEST} ../tests.cpp
                                                          bluetooth-test.cpp)
 TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES}
                                                                         vist-common
+                                                                        vist-client
                                                                         gtest
                                                                         pthread)
 INSTALL(TARGETS ${TARGET_TEST}
index fcf8f3dbde9ba9858d46951c8242e333d4c8e976..c61ce4e4e99d0d5941e60fd996d6c84999f60746 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 #include "bluetooth.hpp"
 #include "../test-util.hpp"
 
+#include <vist/client/query.hpp>
 #include <vist/exception.hpp>
 
+#include <iostream>
+
 #include <gtest/gtest.h>
 
 using namespace vist;
@@ -35,3 +38,30 @@ TEST(BluetoothTests, change_policy_state)
                EXPECT_TRUE(false) << e.what();
        }
 }
+
+TEST(BluetoothTests, get_policies)
+{
+       auto rows = Query::Execute("SELECT * FROM bluetooth");
+
+       EXPECT_TRUE(rows.size() == 1);
+}
+
+TEST(BluetoothTests, set_policies)
+{
+       Query::Execute("INSERT INTO policy_admin (name) VALUES ('vist-plugin-bluetooth-test')");
+
+       Query::Execute("UPDATE bluetooth SET desktopConnectivity = 3, state = 7");
+       Query::Execute("UPDATE bluetooth SET pairing = 2, tethering = 9");
+
+       auto rows = Query::Execute("SELECT * FROM bluetooth");
+       if (rows.size() == 1) {
+               EXPECT_EQ(rows[0]["state"], "7");
+               EXPECT_EQ(rows[0]["desktopConnectivity"], "3");
+               EXPECT_EQ(rows[0]["pairing"], "2");
+               EXPECT_EQ(rows[0]["tethering"], "9");
+       } else {
+               EXPECT_TRUE(false);
+       }
+
+       Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-plugin-bluetooth-test'");
+}
index e4238c752ea936eef4119912cfec3f5b2fcf2c0e..a803826c39e3cbf032a96126edec25f6f76ad057 100644 (file)
@@ -1,9 +1,10 @@
 table_name("bluetooth")
 description("The policies related bluetooth.")
 schema([
-    Column("bluetooth", INTEGER, "The policy value about bluetooth state"),
-    Column("bluetooth-desktop-connectivity", INTEGER, "The policy value about bt desktop connectivity"),
-    Column("bluetooth-pairing", INTEGER, "The policy value about bluetooth pairing"),
-    Column("bluetooth-tethering", INTEGER, "The policy value about bluetooth tethering"),
+    Column("state", INTEGER, "The policy value about bluetooth state"),
+    Column("desktopConnectivity", INTEGER, "The policy value about bt desktop connectivity"),
+    Column("pairing", INTEGER, "The policy value about bluetooth pairing"),
+    Column("tethering", INTEGER, "The policy value about bluetooth tethering"),
 ])
 implementation("tizen/bluetooth@genBluetooth")
+implementation_update("tizen/bluetooth@updateBluetooth")
index fa8ad0901bc1bf46775f4fbe8485753cf30ddce9..38aeacefeadff0eaa1888d3498844878a904ff71 100644 (file)
@@ -29,10 +29,15 @@ namespace osquery {
 
 namespace {
 
-void getPolicy(Row& row, const std::string& name)
+std::map<std::string, std::string> ALIAS = {
+       { "state", "bluetooth" },
+       { "desktopConnectivity", "bluetooth-desktop-connectivity" },
+       { "pairing", "bluetooth-pairing" },
+       { "tethering", "bluetooth-tethering"} };
+
+void setPolicy(const std::string& name, int value)
 {
-       int value = vist::policy::API::Get(name);
-       row[name] = std::to_string(value);
+       vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value));
 }
 
 } // anonymous namespace
@@ -42,15 +47,16 @@ namespace tables {
 using namespace vist;
 
 QueryData genBluetooth(QueryContext& context) try {
-       INFO(VIST) << "Select query about policy table.";
+       INFO(VIST) << "Select query about bluetooth table.";
 
        QueryData results;
 
        Row row;
-       getPolicy(row, "bluetooth");
-       getPolicy(row, "bluetooth-desktop-connectivity");
-       getPolicy(row, "bluetooth-pairing");
-       getPolicy(row, "bluetooth-tethering");
+
+       for (const auto&[schemaName, policyName]: ALIAS) {
+               int value = vist::policy::API::Get(policyName);
+               row[schemaName] = std::to_string(value);
+       }
 
        results.emplace_back(std::move(row));
 
@@ -65,5 +71,38 @@ QueryData genBluetooth(QueryContext& context) try {
        return { r };
 }
 
+QueryData updateBluetooth(QueryContext& context, const PluginRequest& request) try {
+       INFO(VIST) << "Update query about bluetooth table.";
+       if (request.count("json_value_array") == 0)
+               throw std::runtime_error("Wrong request format. Not found json value.");
+
+       std::string str = request.at("json_value_array");
+       rapidjson::Document document;
+       document.Parse(str.c_str());
+       if (document.HasParseError() || !document.IsArray())
+               throw std::runtime_error("Cannot parse request.");
+
+       if (document.Size() != 4)
+               throw std::runtime_error("Wrong request format.");
+
+       /// TODO(Sangwan): Sync vtab schema with policy definition
+       setPolicy("bluetooth", document[0].GetInt());
+       setPolicy("bluetooth-desktop-connectivity", document[1].GetInt());
+       setPolicy("bluetooth-pairing", document[2].GetInt());
+       setPolicy("bluetooth-tethering", document[3].GetInt());
+
+       Row r;
+       r["status"] = "success";
+       return { r };
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
+} catch (...) {
+       ERROR(VIST) << "Failed to query with unknown exception.";
+       Row r;
+       return { r };
+}
+
 } // namespace tables
 } // namespace osquery
index 3b0a5b6846e4c7a20da16c18ee013d84b2e155aa..c44c39366102b4ead779e954ec4d8f131765771a 100644 (file)
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <vist/query-builder.hpp>
+
 namespace vist {
 namespace schema {
 
@@ -26,7 +28,7 @@ namespace schema {
                int tethering; 
 
                DECLARE_COLUMN(State, "state", &Bluetooth::state);
-               DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::DesktopConnectivity);
+               DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::desktopConnectivity);
                DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing);
                DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering);
        };
index 53b1633db95f4622437523fe6aab7c63be9c2208..0ecf60a4edd1e7426d97ff045f203b4524278f61 100644 (file)
@@ -58,13 +58,14 @@ void Server::stop(void)
 void Server::accept(const Task& task)
 {
        auto handler = [this, task]() {
-               DEBUG(VIST) << "New session is accepted.";
                auto connection = std::make_shared<Connection>(this->socket->accept());
+               DEBUG(VIST) << "New session is accepted: fd[" << connection->getFd() << "]";
 
                /// process task per thread
                this->worker.submit([this, connection, task]{
                        auto onRead = [connection, task]() {
                                Server::peer.reset(new Credentials(Credentials::Peer(connection->getFd())));
+                               DEBUG(VIST) << "Read event occured: pid[" << Server::peer->pid << "]";
 
                                Message request = connection->recv();
                                DEBUG(VIST) << "Session header: " << request.signature;