Enable policyd to be called from osquery
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 10 Oct 2019 06:25:57 +0000 (15:25 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Thu, 17 Oct 2019 05:04:06 +0000 (14:04 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
19 files changed:
specs/CMakeLists.txt
specs/tizen/bluetooth_policy.table [deleted file]
specs/tizen/policy.table [new file with mode: 0644]
specs/tizen/usb_policy.table [deleted file]
specs/tizen/wifi_policy.table [deleted file]
src/apix/manager/tests/manager_tests.cpp
src/osquery/CMakeLists.txt
src/osquery/tables/CMakeLists.txt
src/osquery/tables/tizen/bluetooth_policy.cpp [deleted file]
src/osquery/tables/tizen/policy.cpp [new file with mode: 0644]
src/osquery/tables/tizen/tests/policy_tests.cpp
src/osquery/tables/tizen/usb_policy.cpp [deleted file]
src/osquery/tables/tizen/wifi_policy.cpp [deleted file]
src/policyd/core/policy-manager.cpp
src/policyd/core/policy-manager.h
src/policyd/core/policy-storage.cpp
src/policyd/core/policy-storage.h
src/policyd/core/tests/core-tests.cpp
src/policyd/core/tests/storage-tests.cpp

index 70c662bda0ca8f8b0210e365c5b0186d552fea5e..a7b5310d8727b333697b1c03927b2edaa8cef18d 100644 (file)
@@ -25,8 +25,8 @@ LIST(APPEND TABLE_FILES ${TABLE_FILES_LINUX})
 LIST(APPEND TABLE_FILES ${TABLE_FILES_UTILITY})
 
 IF(DEFINED GBS_BUILD)
-#      FILE(GLOB TABLE_FILES_TIZEN "${CMAKE_SOURCE_DIR}/specs/tizen/*.table")
-#      LIST(APPEND TABLE_FILES ${TABLE_FILES_TIZEN})
+       FILE(GLOB TABLE_FILES_TIZEN "${CMAKE_SOURCE_DIR}/specs/tizen/*.table")
+       LIST(APPEND TABLE_FILES ${TABLE_FILES_TIZEN})
 ENDIF(DEFINED GBS_BUILD)
 
 FILE(GLOB TABLE_FILES_TEMPLATES "${CMAKE_SOURCE_DIR}/tools/codegen/templates/*.in")
diff --git a/specs/tizen/bluetooth_policy.table b/specs/tizen/bluetooth_policy.table
deleted file mode 100644 (file)
index 40c2a7c..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-table_name("bluetooth_policy")
-description("A single row containing the bluetooth policy.")
-schema([
-  Column("mode_change_state", INTEGER, "Bluetooth policy state"),
-  Column("desktop_connectivity_state", INTEGER, "Desktop connectivity policy state"),
-  Column("tethering_state", INTEGER, "Tethering policy state"),
-  Column("paring_state", INTEGER, "Paring policy state"),
-])
-implementation("bluetooth_policy@genBluetoothPolicy")
diff --git a/specs/tizen/policy.table b/specs/tizen/policy.table
new file mode 100644 (file)
index 0000000..cb22e61
--- /dev/null
@@ -0,0 +1,10 @@
+table_name("policy")
+description("Device polices.")
+schema([
+    Column("name", TEXT, "Policy name"),
+    Column("value", TEXT, "Policy value"),
+])
+implementation("tizen/policy@genPolicy")
+examples([
+  "select * from policy where name = 'bluetooth'",
+])
diff --git a/specs/tizen/usb_policy.table b/specs/tizen/usb_policy.table
deleted file mode 100644 (file)
index 57a230d..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-table_name("usb_policy")
-description("A single row containing the usb policy.")
-schema([
-  Column("usb_debugging", INTEGER, "USB debugging mode policy state"),
-  Column("usb_tethering", INTEGER, "USB tethering policy state"),
-  Column("usb_client", INTEGER, "USB client policy state"),
-])
-implementation("usb_policy@genUsbPolicy")
diff --git a/specs/tizen/wifi_policy.table b/specs/tizen/wifi_policy.table
deleted file mode 100644 (file)
index f6aad48..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-table_name("wifi_policy")
-description("A single row containing the wifi policy.")
-schema([
-  Column("wifi", INTEGER, "Wi-Fi policy state"),
-  Column("wifi_profile_change", INTEGER, "Profile policy state"),
-  Column("wifi_hotspot", INTEGER, "Hotspot policy state"),
-])
-implementation("wifi_policy@genWifiPolicy")
index 30cac9a93dff68eed1cc383db54837c8d501f906..1a4ff23daf80a67c76d2ae1d596d249d6f5e5369 100644 (file)
@@ -36,6 +36,29 @@ TEST_F(ManagerTests, test_manager_execute) {
        LOG(INFO) << "\t seconds: " << rows[0]["seconds"];
 }
 
+TEST_F(ManagerTests, test_manager_execute_policy) {
+       std::string query = "SELECT * FROM policy";
+       auto rows = OsqueryManager::execute(query);
+       EXPECT_TRUE(rows.size() > 0);
+
+       LOG(INFO) << "[Test] policy table rows:";
+       for (auto& r : rows) {
+               LOG(INFO) << "\t name: " << r["name"];
+               LOG(INFO) << "\t value: " << r["value"];
+       }
+
+       query = "SELECT * FROM policy WHERE name = 'bluetooth'";
+       rows = OsqueryManager::execute(query);
+
+       LOG(INFO) << "[Test] policy table rows with where clause:";
+       for (auto& r : rows) {
+               LOG(INFO) << "\t name: " << r["name"];
+               LOG(INFO) << "\t value: " << r["value"];
+       }
+
+       EXPECT_EQ(rows.size(), 1);
+}
+
 TEST_F(ManagerTests, test_manager_subscribe) {
        int called = 0;
        auto callback = [&](const Row& row) {
index fbd2ba1038f869019f196c05babe23355d918b11..fdee1705f009e0028f9a74bb8505cd51dbcb7390 100644 (file)
@@ -65,10 +65,10 @@ ADD_LIBRARY(${TARGET_OSQUERY_LIB}
                                STATIC $<TARGET_OBJECTS:osquery_generated_tables>
                                           $<TARGET_OBJECTS:osquery_sqlite>
                                           ${${TARGET_OSQUERY_LIB}_SRCS})
-TARGET_LINK_LIBRARIES(${TARGET_OSQUERY_LIB} ${${TARGET_OSQUERY_LIB}_DEPS})
+TARGET_LINK_LIBRARIES(${TARGET_OSQUERY_LIB} ${${TARGET_OSQUERY_LIB}_DEPS}
+                                                                                       ${TARGET_POLICYD_LIB})
 SET_TARGET_PROPERTIES(${TARGET_OSQUERY_LIB} PROPERTIES OUTPUT_NAME ${TARGET_OSQUERY_LIB})
 
-
 ADD_EXECUTABLE(${TARGET_OSQUERY_TEST} main/tests.cpp
                                                                          ${${TARGET_OSQUERY_LIB}_TESTS})
 TARGET_LINK_WHOLE(${TARGET_OSQUERY_TEST} ${TARGET_OSQUERY_LIB})
index 73eab8eb7f3f6628ac4e3508f6182686ca080fe8..f55e3df290a6b0bdd85b8b260d16d681841586d2 100644 (file)
@@ -22,9 +22,9 @@ FILE(GLOB OSQUERY_CROSS_TABLES_TESTS "[!uot]*/tests/*.cpp")
 ADD_OSQUERY_TEST(${OSQUERY_CROSS_TABLES_TESTS})
 
 IF(DEFINED GBS_BUILD)
-#      FILE(GLOB OSQUERY_TIZEN_TABLES "tizen/*.cpp")
-#      ADD_OSQUERY_LIBRARY(osquery_tizen_tables ${OSQUERY_TIZEN_TABLES})
+       FILE(GLOB OSQUERY_TIZEN_TABLES "tizen/*.cpp")
+       ADD_OSQUERY_LIBRARY(osquery_tizen_tables ${OSQUERY_TIZEN_TABLES})
 
-#      FILE(GLOB OSQUERY_TIZEN_TESTS "tizen/tests/*.cpp")
-#      ADD_OSQUERY_TEST(${OSQUERY_TIZEN_TESTS})
+       FILE(GLOB OSQUERY_TIZEN_TESTS "tizen/tests/*.cpp")
+       ADD_OSQUERY_TEST(${OSQUERY_TIZEN_TESTS})
 ENDIF(DEFINED GBS_BUILD)
diff --git a/src/osquery/tables/tizen/bluetooth_policy.cpp b/src/osquery/tables/tizen/bluetooth_policy.cpp
deleted file mode 100644 (file)
index b285835..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- *  Copyright (c) 2019 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.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-/*
- * @file bluetooth_policy.cpp
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Implementation of bluetooth_policy table
- */
-
-#include <string>
-#include <memory>
-#include <stdexcept>
-
-#include <osquery/sql.h>
-#include <osquery/logger.h>
-#include <osquery/tables.h>
-
-#include <dpm/device-policy-manager.h>
-#include <dpm/pil/policy-client.h>
-
-namespace osquery {
-namespace tables {
-
-QueryData genBluetoothPolicy(QueryContext& context) try {
-       std::shared_ptr<void> handle(dpm_manager_create(), dpm_manager_destroy);
-       if (handle == nullptr)
-               throw std::runtime_error("Cannot create dpm-client handle.");
-
-       /// This status is defined at DPM
-       ::Status<bool> status { true };
-       Row r;
-
-       DevicePolicyClient &client = GetDevicePolicyClient(handle.get());
-       status = client.methodCall<bool>("Bluetooth::getModeChangeState");
-       r["mode_change_state"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Bluetooth::getDesktopConnectivityState");
-       r["desktop_connectivity_state"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Bluetooth::getTetheringState");
-       r["tethering_state"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Bluetooth::getPairingState");
-       r["paring_state"] =  INTEGER(status.get());
-
-       return { r };
-} catch (...) {
-// TODO(Sangwan): Resolve duplicated "ERROR" macro with DPM
-//    LOG(ERROR) << "Exception occured";
-       Row r;
-       return { r };
-}
-
-} // namespace tables
-} // namespace osquery
diff --git a/src/osquery/tables/tizen/policy.cpp b/src/osquery/tables/tizen/policy.cpp
new file mode 100644 (file)
index 0000000..b355f7d
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ *  Copyright (c) 2019 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.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file bluetooth_policy.cpp
+ * @author Sangwan Kwon (sangwan.kwon@samsung.com)
+ * @brief Implementation of bluetooth_policy table
+ */
+
+#include <string>
+#include <memory>
+#include <stdexcept>
+
+#include <osquery/sql.h>
+#include <osquery/logger.h>
+#include <osquery/tables.h>
+
+#include <policyd/core/policy-manager.h>
+
+using namespace policyd;
+
+namespace osquery {
+namespace tables {
+
+QueryData genPolicy(QueryContext& context) try {
+       auto& manager = PolicyManager::Instance();
+
+       QueryData results;
+       if (context.constraints["name"].exists(EQUALS)) { /// where clause
+               auto names = context.constraints["name"].getAll(EQUALS);
+               for (const auto& name : names) {
+                       auto ret = manager.get(name);
+
+                       Row r;
+                       r["name"] = TEXT(name);
+                       r["value"] = TEXT(ret.value);
+
+                       results.emplace_back(std::move(r));
+               }
+       } else { /// select *;
+               auto policies = manager.getAll();
+               for (auto& policy : policies) {
+                       Row r;
+                       r["name"] = TEXT(policy.first);
+                       r["value"] = TEXT(policy.second);
+
+                       results.emplace_back(std::move(r));
+               }
+       }
+
+       return results;
+} catch (...) {
+// TODO(Sangwan): Resolve duplicated "ERROR" macro with DPM
+//    LOG(ERROR) << "Exception occured";
+       Row r;
+       return { r };
+}
+
+} // namespace tables
+} // namespace osquery
index 13aad8fd7012bb78322cb94966ad10444b96bdf9..a021e6cad3251e4ae8159d87f82529c9262508c5 100644 (file)
 
 #include <gtest/gtest.h>
 
-#include <osquery/sql.h>
-#include <osquery/logger.h>
-
-#include <dpm/device-policy-manager.h>
-#include <dpm/pil/policy-client.h>
+#include <policyd/core/policy-manager.h>
 
 class PolicyTests : public testing::Test {};
 
-using namespace osquery;
-
-TEST_F(PolicyTests, Bluetooth) {
-       std::shared_ptr<void> handle(dpm_manager_create(), dpm_manager_destroy);
-       if (handle == nullptr)
-               throw std::runtime_error("Cannot create dpm-client handle.");
-
-       ::Status<bool> status { true };
-
-       DevicePolicyClient &client = GetDevicePolicyClient(handle.get());
-       status = client.methodCall<bool>("Bluetooth::getModeChangeState");
-       EXPECT_EQ(true, status.get());
-
-       status = client.methodCall<bool>("Bluetooth::getDesktopConnectivityState");
-       EXPECT_EQ(true, status.get());
-
-       status = client.methodCall<bool>("Bluetooth::getTetheringState");
-       EXPECT_EQ(true, status.get());
-
-       status = client.methodCall<bool>("Bluetooth::getPairingState");
-       EXPECT_EQ(true, status.get());
-}
-
-TEST_F(PolicyTests, Wifi) {
-       std::shared_ptr<void> handle(dpm_manager_create(), dpm_manager_destroy);
-       if (handle == nullptr)
-               throw std::runtime_error("Cannot create dpm-client handle.");
-
-       ::Status<bool> status { true };
-
-       DevicePolicyClient &client = GetDevicePolicyClient(handle.get());
-       status = client.methodCall<bool>("Wifi::getState");
-       EXPECT_EQ(true, status.get());
-
-       status = client.methodCall<bool>("Wifi::isProfileChangeRestricted");
-       EXPECT_EQ(true, status.get());
-
-       status = client.methodCall<bool>("Wifi::getHotspotState");
-       EXPECT_EQ(true, status.get());
-}
-
-TEST_F(PolicyTests, Usb) {
-       std::shared_ptr<void> handle(dpm_manager_create(), dpm_manager_destroy);
-       if (handle == nullptr)
-               throw std::runtime_error("Cannot create dpm-client handle.");
-
-       ::Status<bool> status { true };
-
-       DevicePolicyClient &client = GetDevicePolicyClient(handle.get());
-       status = client.methodCall<bool>("Usb::getDebuggingState");
-       EXPECT_EQ(true, status.get());
+using namespace policyd;
 
-       status = client.methodCall<bool>("Usb::getTetheringState");
-       EXPECT_EQ(true, status.get());
+TEST_F(PolicyTests, get_all) {
+       auto& manager = PolicyManager::Instance();
+       auto policies = manager.getAll();
 
-       status = client.methodCall<bool>("Usb::getClientState");
-       EXPECT_EQ(true, status.get());
+       EXPECT_TRUE(policies.size() > 0);
 }
diff --git a/src/osquery/tables/tizen/usb_policy.cpp b/src/osquery/tables/tizen/usb_policy.cpp
deleted file mode 100644 (file)
index e9ba03c..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- *  Copyright (c) 2019 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.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-/*
- * @file usb_policy.cpp
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Implementation of usb_policy table
- */
-
-#include <string>
-#include <memory>
-#include <stdexcept>
-
-#include <osquery/sql.h>
-#include <osquery/logger.h>
-#include <osquery/tables.h>
-
-#include <dpm/device-policy-manager.h>
-#include <dpm/pil/policy-client.h>
-
-namespace osquery {
-namespace tables {
-
-QueryData genUsbPolicy(QueryContext& context) try {
-       std::shared_ptr<void> handle(dpm_manager_create(), dpm_manager_destroy);
-       if (handle == nullptr)
-               throw std::runtime_error("Cannot create dpm-client handle.");
-
-       /// This status is defined at DPM
-       ::Status<bool> status { true };
-       Row r;
-
-       DevicePolicyClient &client = GetDevicePolicyClient(handle.get());
-       status = client.methodCall<bool>("Usb::getDebuggingState");
-       r["usb_debugging"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Usb::getTetheringState");
-       r["usb_tethering"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Usb::getClientState");
-       r["usb_client"] =  INTEGER(status.get());
-
-       return { r };
-} catch (...) {
-// TODO(Sangwan): Resolve duplicated "ERROR" macro with DPM
-       Row r;
-       return { r };
-}
-
-} // namespace tables
-} // namespace osquery
diff --git a/src/osquery/tables/tizen/wifi_policy.cpp b/src/osquery/tables/tizen/wifi_policy.cpp
deleted file mode 100644 (file)
index 2a45151..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- *  Copyright (c) 2019 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.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-/*
- * @file wifi_policy.cpp
- * @author Sangwan Kwon (sangwan.kwon@samsung.com)
- * @brief Implementation of wifi_policy table
- */
-
-#include <string>
-#include <memory>
-#include <stdexcept>
-
-#include <osquery/sql.h>
-#include <osquery/logger.h>
-#include <osquery/tables.h>
-
-#include <dpm/device-policy-manager.h>
-#include <dpm/pil/policy-client.h>
-
-namespace osquery {
-namespace tables {
-
-QueryData genWifiPolicy(QueryContext& context) try {
-       std::shared_ptr<void> handle(dpm_manager_create(), dpm_manager_destroy);
-       if (handle == nullptr)
-               throw std::runtime_error("Cannot create dpm-client handle.");
-
-       /// This status is defined at DPM
-       ::Status<bool> status { true };
-       Row r;
-
-       DevicePolicyClient &client = GetDevicePolicyClient(handle.get());
-       status = client.methodCall<bool>("Wifi::getState");
-       r["wifi"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Wifi::isProfileChangeRestricted");
-       r["wifi_profile_change"] =  INTEGER(status.get());
-
-       status = client.methodCall<bool>("Wifi::getHotspotState");
-       r["wifi_hotspot"] =  INTEGER(status.get());
-
-       return { r };
-} catch (...) {
-// TODO(Sangwan): Resolve duplicated "ERROR" macro with DPM
-       Row r;
-       return { r };
-}
-
-} // namespace tables
-} // namespace osquery
index 4e526ae564c9c129c183b9a76a1615477fbaa729..156f68f768568dce5869ea9ec61c8964fb486522 100644 (file)
 
 namespace policyd {
 
+PolicyManager::PolicyManager() : storage(DB_PATH)
+{
+       loadProviders(PLUGIN_INSTALL_DIR);
+       int cnt = loadPolicies();
+       INFO(DPM, std::to_string(cnt) + "-policies loaded");
+}
+
 std::pair<int, int> PolicyManager::loadProviders(const std::string& path)
 {
        INFO(DPM, "Load policies from :" << path);
@@ -105,4 +112,9 @@ PolicyValue PolicyManager::get(const std::string& policy, uid_t uid)
        return storage.strictest(policy, uid);
 }
 
+std::unordered_map<std::string, PolicyValue> PolicyManager::getAll(uid_t uid)
+{
+       return storage.strictest(uid);
+}
+
 } // namespace policyd
index 920156546e24d425c2a86ee86a0a51ea78ba9d29..9e8577992f98c3c2a7ade0f21e8448523fdd3472 100644 (file)
 
 #include "policy-storage.h"
 
-#include <string>
 #include <exception>
 #include <memory>
+#include <string>
+#include <unordered_map>
 #include <vector>
 
+#include <gtest/gtest_prod.h>
+
 namespace policyd {
 
 class PolicyManager final {
@@ -36,31 +39,34 @@ public:
        PolicyManager(PolicyManager&&) = delete;
        PolicyManager& operator=(PolicyManager&&) = delete;
 
-       static PolicyManager& instance() {
+       static PolicyManager& Instance() {
                static PolicyManager manager;
                return manager;
        }
 
-       std::pair<int, int> loadProviders(const std::string& path);
-       int loadPolicies();
-
        void enroll(const std::string& admin, uid_t uid);
        void disenroll(const std::string& admin, uid_t uid);
 
        void set(const std::string& policy, const PolicyValue& value,
-                        const std::string& admin, uid_t uid);
-       PolicyValue get(const std::string& policy, uid_t uid);
+                        const std::string& admin, uid_t uid = 0);
+       PolicyValue get(const std::string& policy, uid_t uid = 0);
 
+       std::unordered_map<std::string, PolicyValue> getAll(uid_t uid = 0);
 
 private:
-       explicit PolicyManager() : storage(DB_PATH) {}
+       explicit PolicyManager();
        ~PolicyManager() = default;
 
+       std::pair<int, int> loadProviders(const std::string& path);
+       int loadPolicies();
+
        PolicyStorage storage;
        std::vector<std::shared_ptr<PolicyProvider>> providers;
 
        std::unordered_map<std::string, std::shared_ptr<GlobalPolicy>> global;
        std::unordered_map<std::string, std::shared_ptr<DomainPolicy>> domain;
+
+       FRIEND_TEST(PolicyCoreTests, policy_loader);
 };
 
 } // namespace policyd
index 6d9cdff4973785bb447e2fab0e7c926d4d1b2b31..2fe2ece4b44cfffa6f63b4c72e8684c714803293 100644 (file)
@@ -206,6 +206,10 @@ PolicyValue PolicyStorage::strictest(const std::string& policy, uid_t uid)
        if (definitions.find(policy) == definitions.end())
                throw std::runtime_error("Not exist policy: " + policy);
 
+       // There is no enrolled admins.
+       if (managedPolicies.size() == 0)
+               return PolicyValue(definitions[policy].ivalue);
+
        std::shared_ptr<PolicyValue> strictest = nullptr;
        int policyId = definitions[policy].id;
        auto range = managedPolicies.equal_range(policyId);
@@ -232,6 +236,19 @@ PolicyValue PolicyStorage::strictest(const std::string& policy, uid_t uid)
        return std::move(*strictest);
 }
 
+std::unordered_map<std::string, PolicyValue> PolicyStorage::strictest(uid_t uid)
+{
+       std::unordered_map<std::string, PolicyValue> policies;
+       for (const auto& pair : definitions) {
+               std::string name = pair.first;
+               auto value = this->strictest(name, uid);
+
+               policies.emplace(std::move(name), std::move(value));
+       }
+
+       return policies;
+}
+
 std::string PolicyStorage::getAlias(const std::string& name, uid_t uid) const noexcept
 {
        return name + std::to_string(uid);
index 31a67b79de1e8183058b802e5e69aa1ca42c6fc4..697c899045680f815e54cfb5e950c7f832f96582 100644 (file)
@@ -50,7 +50,10 @@ public:
 
        void update(const std::string& admin, uid_t uid,
                                const std::string& policy, const PolicyValue& value);
+
        PolicyValue strictest(const std::string& policy, uid_t uid = 0);
+       /// Return all strictest policy values
+       std::unordered_map<std::string, PolicyValue> strictest(uid_t uid = 0);
 
 private:
        void syncPolicyDefinition();
index 81bd1368f38b8eb1b7bd4fc3e10e76d757a64e65..49fc46323ead1f841ed2e2ca14eff44b56e00bdd 100644 (file)
 
 #include "../policy-manager.h"
 
-using namespace policyd;
+namespace policyd {
 
 class PolicyCoreTests : public testing::Test {};
 
 TEST_F(PolicyCoreTests, policy_loader) {
-       auto& manager = PolicyManager::instance();
-       auto result = manager.loadProviders(PLUGIN_INSTALL_DIR);
+       auto& manager = PolicyManager::Instance();
+
+       /// Clearing for test
+       manager.providers.clear();
+       manager.global.clear();
+       manager.domain.clear();
 
+       auto result = manager.loadProviders(PLUGIN_INSTALL_DIR);
        EXPECT_TRUE(result.first > 0);
        EXPECT_TRUE(result.second == 0);
 
@@ -34,7 +39,7 @@ TEST_F(PolicyCoreTests, policy_loader) {
 }
 
 TEST_F(PolicyCoreTests, policy_set_get) {
-       auto& manager = PolicyManager::instance();
+       auto& manager = PolicyManager::Instance();
        manager.enroll("testAdmin", 0);
        manager.set("bluetooth", PolicyValue(5), "testAdmin", 0);
 
@@ -51,3 +56,5 @@ TEST_F(PolicyCoreTests, policy_set_get) {
        manager.disenroll("testAdmin", 0);
        manager.disenroll("testAdmin1", 0);
 }
+
+} // namespace policyd
index 6c1b5e925c0a7571a5c6ef74caea9c4508633a1e..311e83156bc2d774e312beef3f7256d16529dd38 100644 (file)
@@ -121,3 +121,18 @@ TEST_F(PolicyStorageTests, strictest) {
        storage->disenroll("testAdmin", 0);
        storage->disenroll("testAdmin", 1);
 }
+
+TEST_F(PolicyStorageTests, strictest_all) {
+       auto storage = getStorage();
+       storage->enroll("testAdmin", 1);
+
+       /// as global policy
+       auto policies = storage->strictest();
+       EXPECT_TRUE(policies.size() > 0);
+
+       /// as domain policy
+       policies = storage->strictest(1);
+       EXPECT_TRUE(policies.size() > 0);
+
+       storage->disenroll("testAdmin", 1);
+}