From 865fe8d1038d93f176f368e47953c50f7fc2490b Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Mon, 24 Feb 2020 17:12:45 +0900 Subject: [PATCH] policy: Add update query to bluetooth vtab Signed-off-by: Sangwan Kwon --- plugins/bluetooth/CMakeLists.txt | 1 + plugins/bluetooth/bluetooth-test.cpp | 32 ++++++++++++++- specs/tizen/bluetooth.table | 9 +++-- src/osquery/tables/tizen/bluetooth.cpp | 55 ++++++++++++++++++++++---- src/vist/client/schema/bluetooth.hpp | 4 +- src/vist/rmi/impl/server.cpp | 3 +- 6 files changed, 89 insertions(+), 15 deletions(-) diff --git a/plugins/bluetooth/CMakeLists.txt b/plugins/bluetooth/CMakeLists.txt index 9b4e231..07a2346 100644 --- a/plugins/bluetooth/CMakeLists.txt +++ b/plugins/bluetooth/CMakeLists.txt @@ -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} diff --git a/plugins/bluetooth/bluetooth-test.cpp b/plugins/bluetooth/bluetooth-test.cpp index fcf8f3d..c61ce4e 100644 --- a/plugins/bluetooth/bluetooth-test.cpp +++ b/plugins/bluetooth/bluetooth-test.cpp @@ -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. @@ -17,8 +17,11 @@ #include "bluetooth.hpp" #include "../test-util.hpp" +#include #include +#include + #include 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'"); +} diff --git a/specs/tizen/bluetooth.table b/specs/tizen/bluetooth.table index e4238c7..a803826 100644 --- a/specs/tizen/bluetooth.table +++ b/specs/tizen/bluetooth.table @@ -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") diff --git a/src/osquery/tables/tizen/bluetooth.cpp b/src/osquery/tables/tizen/bluetooth.cpp index fa8ad09..38aeace 100644 --- a/src/osquery/tables/tizen/bluetooth.cpp +++ b/src/osquery/tables/tizen/bluetooth.cpp @@ -29,10 +29,15 @@ namespace osquery { namespace { -void getPolicy(Row& row, const std::string& name) +std::map 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& 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 diff --git a/src/vist/client/schema/bluetooth.hpp b/src/vist/client/schema/bluetooth.hpp index 3b0a5b6..c44c393 100644 --- a/src/vist/client/schema/bluetooth.hpp +++ b/src/vist/client/schema/bluetooth.hpp @@ -16,6 +16,8 @@ #pragma once +#include + 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); }; diff --git a/src/vist/rmi/impl/server.cpp b/src/vist/rmi/impl/server.cpp index 53b1633..0ecf60a 100644 --- a/src/vist/rmi/impl/server.cpp +++ b/src/vist/rmi/impl/server.cpp @@ -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(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; -- 2.34.1