dp fix trust group

Signed-off-by: zjucx <chengxiang4@huawei.com>
This commit is contained in:
zjucx
2022-01-24 20:53:01 +08:00
parent 68a1ef765a
commit d2fae2a9c7
8 changed files with 201 additions and 6 deletions
+1
View File
@@ -40,6 +40,7 @@ enum {
ERR_DP_POST_TASK_FAILED = 98566153,
ERR_DP_DEVICE_SYNC_BUSY = 98566154,
ERR_DP_PERMISSION_DENIED = 98566155,
ERR_DP_UNTRUSTED_GROUP = 98566156,
};
} // namespace DeviceProfile
} // namespace OHOS
+2
View File
@@ -38,6 +38,7 @@ ohos_shared_library("distributed_device_profile") {
install_enable = true
sources = [
"src/authority/authority_manager.cpp",
"src/authority/trust_group_manager.cpp",
"src/contentsensor/content_collector.cpp",
"src/contentsensor/content_sensor_manager.cpp",
"src/contentsensor/device_info_collector.cpp",
@@ -69,6 +70,7 @@ ohos_shared_library("distributed_device_profile") {
configs = [ ":device_profile_core_config" ]
external_deps = [
"deviceauth_standard:deviceauth_sdk",
"distributeddatamgr:distributeddata_inner",
"dsoftbus_standard:softbus_client",
"eventhandler:libeventhandler",
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* 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.
*/
#ifndef OHOS_DISTRIBUTED_DEVICE_PROFILE_TRUST_GROUP_MANAGER_H
#define OHOS_DISTRIBUTED_DEVICE_PROFILE_TRUST_GROUP_MANAGER_H
#include <string>
#include "device_auth.h"
#include "device_auth_defines.h"
#include "nlohmann/json.hpp"
#include "single_instance.h"
namespace OHOS {
namespace DeviceProfile {
struct GroupInfo {
std::string groupName;
std::string groupId;
std::string groupOwner;
int32_t groupType;
int32_t groupVisibility;
GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0) {}
};
void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo);
class TrustGroupManager {
DECLARE_SINGLE_INSTANCE(TrustGroupManager);
public:
void Init();
bool CheckTrustGroup(const std::string& deviceId);
private:
const DeviceGroupManager* hichainGmInstance_ = nullptr;
};
} // namespace DeviceProfile
} // namespace OHOS
#endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_TRUST_GROUP_MANAGER_H
@@ -64,6 +64,7 @@ private:
DistributedKv::Status GetKvStore();
void DeleteKvStore();
void SubscribeDeviceProfileKvStore();
bool CheckTrustGroup(const std::vector<std::string>& deviceIdList);
private:
DistributedKv::Options options_;
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* 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 "trust_group_manager.h"
#include "device_profile_errors.h"
#include "device_profile_log.h"
namespace OHOS {
namespace DeviceProfile {
namespace {
const std::string TAG = "TrustGroupManager";
constexpr int32_t VISIBILITY_PUBLIC = -1;
const std::string AUTH_APPID = "device_profile_auth";
}
IMPLEMENT_SINGLE_INSTANCE(TrustGroupManager);
void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo)
{
jsonObject.at(FIELD_GROUP_NAME).get_to(groupInfo.groupName);
jsonObject.at(FIELD_GROUP_ID).get_to(groupInfo.groupId);
jsonObject.at(FIELD_GROUP_OWNER).get_to(groupInfo.groupOwner);
jsonObject.at(FIELD_GROUP_TYPE).get_to(groupInfo.groupType);
jsonObject.at(FIELD_GROUP_VISIBILITY).get_to(groupInfo.groupVisibility);
}
void TrustGroupManager::Init()
{
if (InitDeviceAuthService() != ERR_OK) {
HILOGE("auth InitDeviceAuthService failed");
return;
}
hichainGmInstance_ = GetGmInstance();
if (hichainGmInstance_ == nullptr) {
HILOGE("auth GetGmInstance failed");
return;
}
HILOGI("init succeeded");
}
bool TrustGroupManager::CheckTrustGroup(const std::string& deviceId)
{
if (hichainGmInstance_ == nullptr) {
Init();
}
if (hichainGmInstance_ == nullptr) {
HILOGE("auth GetGmInstance failed");
return false;
}
uint32_t groupNum = 0;
char* returnGroups = nullptr;
int32_t ret = hichainGmInstance_->getRelatedGroups(AUTH_APPID.c_str(), deviceId.c_str(),
&returnGroups, &groupNum);
if (ret != ERR_OK) {
HILOGE("faild , ret: %d", ret);
return false;
}
if (returnGroups == nullptr || groupNum == 0) {
HILOGE("faild, returnGroups is nullptr");
return false;
}
nlohmann::json jsonObject = nlohmann::json::parse(returnGroups, nullptr, false);
if (jsonObject.is_discarded()) {
HILOGE("returnGroups parse error");
return false;
}
std::vector<GroupInfo> groupInfos = jsonObject.get<std::vector<GroupInfo>>();
if (groupInfos.empty()) {
HILOGE("failed, groupInfos is empty");
return false;
}
for (const auto& groupInfo : groupInfos) {
// check group visibility is whether public or not
if (groupInfo.groupVisibility != VISIBILITY_PUBLIC) {
continue;
}
// check group type is whether (same count or point to point) or not
if (groupInfo.groupType == GroupType::IDENTICAL_ACCOUNT_GROUP
|| groupInfo.groupType == GroupType::PEER_TO_PEER_GROUP) {
HILOGI("check success type = %{public}d", groupInfo.groupType);
return true;
}
}
HILOGE("check failed, not in trust group");
return false;
}
} // namespace DeviceProfile
} // namespace OHOS
@@ -18,10 +18,13 @@
#include <cinttypes>
#include <thread>
#include "device_manager.h"
#include "device_profile_errors.h"
#include "device_profile_log.h"
#include "device_profile_storage_manager.h"
#include "device_profile_utils.h"
#include "service_characteristic_profile.h"
#include "trust_group_manager.h"
#include "datetime_ex.h"
#include "profile_change_handler.h"
@@ -33,7 +36,6 @@ using namespace std::chrono_literals;
namespace {
const std::string TAG = "DeviceProfileStorage";
constexpr int32_t RETRY_TIMES_GET_KVSTORE = 10;
}
@@ -249,15 +251,41 @@ int32_t DeviceProfileStorage::SyncDeviceProfile(const std::vector<std::string>&
SyncMode syncMode)
{
HILOGI("called");
if (!CheckTrustGroup(deviceIdList)) {
return ERR_DP_UNTRUSTED_GROUP;
}
std::unique_lock<std::shared_mutex> writeLock(storageLock_);
if (kvStorePtr_ == nullptr) {
return ERR_DP_INVALID_PARAMS;
}
Status status = kvStorePtr_->Sync(deviceIdList, static_cast<DistributedKv::SyncMode>(syncMode));
if (status != Status::SUCCESS) {
HILOGE("sync failed, error = %{public}d", status);
}
return static_cast<int32_t>(status);
}
bool DeviceProfileStorage::CheckTrustGroup(const std::vector<std::string>& deviceIdList)
{
if (deviceIdList.empty()) {
HILOGE("device list is empty");
return false;
}
for (const auto& deviceId : deviceIdList) {
std::string udid;
if (!DeviceManager::GetInstance().TransformDeviceId(deviceId, udid, DeviceIdType::UDID)) {
HILOGE("%{public}s transform to udid failed", DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str());
return false;
}
if (!TrustGroupManager::GetInstance().CheckTrustGroup(udid)) {
HILOGE("%{public}s not in trust group", DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str());
return false;
}
}
return true;
}
} // namespace DeviceProfile
} // namespace OHOS
@@ -23,8 +23,8 @@
#include "device_profile_storage_manager.h"
#include "service_characteristic_profile.h"
#include "subscribe_manager.h"
#include "system_ability_definition.h"
#include "trust_group_manager.h"
namespace OHOS {
namespace DeviceProfile {
@@ -50,10 +50,6 @@ bool DistributedDeviceProfileService::Init()
HILOGE("DeviceProfileStorageManager init failed");
return false;
}
if (!ContentSensorManager::GetInstance().Init()) {
HILOGE("ContentSensorManager init failed");
return false;
}
if (!SubscribeManager::GetInstance().Init()) {
HILOGE("SubscribeManager init failed");
return false;
@@ -62,6 +58,8 @@ bool DistributedDeviceProfileService::Init()
HILOGE("AuthorityManager init failed");
return false;
}
TrustGroupManager::GetInstance().Init();
ContentSensorManager::GetInstance().Init();
HILOGI("init succeeded");
return true;
}
@@ -40,6 +40,10 @@ bool SubscribeManager::Init()
{
subscriberDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(
new (std::nothrow) SubscriberDeathRecipient);
if (subscriberDeathRecipient_ == nullptr) {
HILOGE("null death recipient");
return false;
}
HILOGI("init succeeded");
return true;
}