Expose policyd API
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 14 Oct 2019 06:30:53 +0000 (15:30 +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>
src/apix/manager/CMakeLists.txt
src/apix/manager/tests/policy_tests.cpp [new file with mode: 0644]
src/apix/manager/tests/tizen/policy_tests.cpp [deleted file]
src/osquery/tables/tizen/policy.cpp
src/osquery/tables/tizen/tests/policy_tests.cpp
src/policyd/api.h [new file with mode: 0644]
src/policyd/core/CMakeLists.txt
src/policyd/core/api.cpp [new file with mode: 0644]

index cdaba61ce7070c07c53c3206d5ed2d478c359790..7c4cfac36c035ded48f36e1001bd2fb1b7c0522f 100644 (file)
 ADD_APIX_LIBRARY(manager manager.cpp
                                                 manager_impl.cpp)
 
-FILE(GLOB MANAGER_TESTS "tests/*.cpp")
+FILE(GLOB MANAGER_TESTS "tests/m*.cpp")
+ADD_APIX_TEST(${MANAGER_TESTS})
 
 IF(DEFINED GBS_BUILD)
-       FILE(GLOB TIZEN_POLICYD_TESTS "tests/tizen/*.cpp")
+       FILE(GLOB POLICYD_TESTS "tests/p*.cpp")
+       ADD_APIX_TEST(${POLICYD_TESTS})
 ENDIF(DEFINED GBS_BUILD)
-
-ADD_APIX_TEST(${MANAGER_TESTS})
diff --git a/src/apix/manager/tests/policy_tests.cpp b/src/apix/manager/tests/policy_tests.cpp
new file mode 100644 (file)
index 0000000..e3a5a57
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  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
+ */
+
+#include <gtest/gtest.h>
+
+#include <osquery_manager.h>
+
+#include <osquery/logger.h>
+
+#include <policyd/core/policy-manager.h>
+
+using namespace osquery;
+
+class PolicydTests : public testing::Test {};
+
+TEST_F(PolicydTests, select) {
+       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(PolicydTests, update) {
+       auto& manager = policyd::PolicyManager::Instance();
+       manager.enroll("admin", 0);
+
+       std::string query = "SELECT * FROM policy WHERE name = 'bluetooth'";
+       auto rows = OsqueryManager::execute(query);
+       /// Initial policy value
+       EXPECT_EQ(rows[0]["value"], std::to_string(1));
+
+       query = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'";
+       rows = OsqueryManager::execute(query);
+       EXPECT_EQ(rows.size(), 0);
+
+       query = "SELECT * FROM policy WHERE name = 'bluetooth'";
+       rows = OsqueryManager::execute(query);
+       /// Initial policy value
+       EXPECT_EQ(rows[0]["value"], std::to_string(3));
+
+       manager.disenroll("admin", 0);
+}
diff --git a/src/apix/manager/tests/tizen/policy_tests.cpp b/src/apix/manager/tests/tizen/policy_tests.cpp
deleted file mode 100644 (file)
index e3a5a57..0000000
+++ /dev/null
@@ -1,71 +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
- */
-
-#include <gtest/gtest.h>
-
-#include <osquery_manager.h>
-
-#include <osquery/logger.h>
-
-#include <policyd/core/policy-manager.h>
-
-using namespace osquery;
-
-class PolicydTests : public testing::Test {};
-
-TEST_F(PolicydTests, select) {
-       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(PolicydTests, update) {
-       auto& manager = policyd::PolicyManager::Instance();
-       manager.enroll("admin", 0);
-
-       std::string query = "SELECT * FROM policy WHERE name = 'bluetooth'";
-       auto rows = OsqueryManager::execute(query);
-       /// Initial policy value
-       EXPECT_EQ(rows[0]["value"], std::to_string(1));
-
-       query = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'";
-       rows = OsqueryManager::execute(query);
-       EXPECT_EQ(rows.size(), 0);
-
-       query = "SELECT * FROM policy WHERE name = 'bluetooth'";
-       rows = OsqueryManager::execute(query);
-       /// Initial policy value
-       EXPECT_EQ(rows[0]["value"], std::to_string(3));
-
-       manager.disenroll("admin", 0);
-}
index c6bd1d18d273327df4f7b20277e3c1f10e9aa2c1..a35b62b2c0b2f9b04669db67eaf7a73426f0444c 100644 (file)
  *  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 <osquery/logger.h>
 #include <osquery/tables.h>
 
-#include <policyd/core/policy-manager.h>
-
-using namespace policyd;
+#include <policyd/api.h>
 
 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);
+                       auto ret = policyd::API::Get(name);
 
                        Row r;
                        r["name"] = TEXT(name);
@@ -50,7 +41,7 @@ QueryData genPolicy(QueryContext& context) try {
                        results.emplace_back(std::move(r));
                }
        } else { /// select *;
-               auto policies = manager.getAll();
+               auto policies = policyd::API::GetAll();
                for (auto& policy : policies) {
                        Row r;
                        r["name"] = TEXT(policy.first);
@@ -82,9 +73,7 @@ QueryData updatePolicy(QueryContext& context, const PluginRequest& request) try
        std::string name = document[0].GetString();
        int value = std::stoi(document[1].GetString());
 
-       /// TODO(Sangwan): Get admin name from policyd
-       auto& manager = PolicyManager::Instance();
-       manager.set(name, PolicyValue(value), "admin");
+       policyd::API::Admin::Set(name, policyd::PolicyValue(value));
 
        Row r;
        r["status"] = "success";
index a021e6cad3251e4ae8159d87f82529c9262508c5..c4a6f87d204285aaa6f046c29910981302b6dea4 100644 (file)
 
 #include <gtest/gtest.h>
 
-#include <policyd/core/policy-manager.h>
+#include <policyd/api.h>
 
 class PolicyTests : public testing::Test {};
 
-using namespace policyd;
-
 TEST_F(PolicyTests, get_all) {
-       auto& manager = PolicyManager::Instance();
-       auto policies = manager.getAll();
+       auto policies = policyd::API::GetAll();
 
        EXPECT_TRUE(policies.size() > 0);
 }
diff --git a/src/policyd/api.h b/src/policyd/api.h
new file mode 100644 (file)
index 0000000..4632785
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ *  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
+ */
+
+#pragma once
+
+#include <policyd/sdk/policy-value.h>
+
+#include <string>
+#include <unordered_map>
+
+namespace policyd {
+
+struct API {
+       static PolicyValue Get(const std::string& policy);
+       static std::unordered_map<std::string, PolicyValue> GetAll();
+
+       struct Admin {
+               static void Set(const std::string& policy, const PolicyValue& value);
+
+               static void Enroll(const std::string& admin, uid_t uid);
+               static void Disenroll(const std::string& admin, uid_t uid);
+       };
+};
+
+} // namespace policyd
index 85dbb0e1d8daa0a1d3d6cd61dfcc4afa236fc041..498ea529656a9daf52d221df279db24f5b4af09d 100644 (file)
@@ -13,7 +13,8 @@
 # limitations under the License.
 #
 
-ADD_POLICYD_LIBRARY(policyd_core policy-manager.cpp
+ADD_POLICYD_LIBRARY(policyd_core api.cpp
+                                                                policy-manager.cpp
                                                                 policy-loader.cpp
                                                                 policy-storage.cpp
                                                                 logger.cpp)
diff --git a/src/policyd/core/api.cpp b/src/policyd/core/api.cpp
new file mode 100644 (file)
index 0000000..01757b5
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ *  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
+ */
+
+#include <policyd/api.h>
+
+#include "policy-manager.h"
+
+namespace policyd {
+
+PolicyValue API::Get(const std::string& policy)
+{
+       return PolicyManager::Instance().get(policy);
+}
+
+std::unordered_map<std::string, PolicyValue> API::GetAll()
+{
+       return PolicyManager::Instance().getAll();
+}
+
+void API::Admin::Set(const std::string& policy, const PolicyValue& value)
+{
+       // TODO(Sangwan): Get admin name from peer PID
+       PolicyManager::Instance().set(policy, value, "admin");
+}
+
+void API::Admin::Enroll(const std::string& admin, uid_t uid)
+{
+       PolicyManager::Instance().enroll(admin, uid);
+}
+
+void API::Admin::Disenroll(const std::string& admin, uid_t uid)
+{
+       PolicyManager::Instance().disenroll(admin, uid);
+}
+
+} // namespace policyd