From 1419b4f5f4055bea5417723658c8f94f60af6b32 Mon Sep 17 00:00:00 2001 From: wangkun Date: Fri, 30 Jul 2021 15:29:07 +0800 Subject: [PATCH] Fix some bugs Signed-off-by: wangkun --- .../adapter/security/src/security.cpp | 117 +----------------- .../adapter/security/src/security.h | 8 -- .../adapter/security/src/security_adapter.cpp | 9 +- 3 files changed, 4 insertions(+), 130 deletions(-) diff --git a/services/distributeddataservice/adapter/security/src/security.cpp b/services/distributeddataservice/adapter/security/src/security.cpp index 11085c4bf..dd8ddf5ce 100755 --- a/services/distributeddataservice/adapter/security/src/security.cpp +++ b/services/distributeddataservice/adapter/security/src/security.cpp @@ -48,60 +48,13 @@ const char * const Security::DATA_CE[] = { }; Security::Security(const std::string &appId, const std::string &userId, const std::string &dir) - : delegateMgr_(appId, userId) { - delegateMgr_.SetKvStoreConfig({dir}); ZLOGD("constructor kvStore_ is %s", dir.c_str()); } Security::~Security() { - ZLOGD("destructor kvStore_ is null.%d", kvStore_ == nullptr); - delegateMgr_.CloseKvStore(kvStore_); - kvStore_ = nullptr; -} - -void Security::InitLocalCertData() const -{ - std::thread th = std::thread([keep = shared_from_this()] { - ZLOGI("Save sensitive to meta db"); - DBStatus status = DB_ERROR; - // retry after 10 second, 10 * 1000 * 1000 mains 1 second - BlockInteger retry(10 * 1000 * 1000); - auto &network = AppDistributedKv::CommunicationProvider::GetInstance(); - for (; retry < RETRY_MAX_TIMES; ++retry) { - auto info = network.GetLocalBasicInfo(); - - Sensitive sensitive(network.GetUdidByNodeId(info.deviceId), 0); - if (!sensitive.LoadData()) { - continue; - } - - if (keep->kvStore_ == nullptr) { - ZLOGE("The kvStore_ is null"); - break; - } - - std::string uuid = network.GetUuidByNodeId(info.deviceId); - status = keep->kvStore_->Put(keep->GenerateSecurityKey(uuid), sensitive.Marshal()); - if (status != OK) { - continue; - } - - keep->SyncMeta(); - break; - } - ZLOGI("Save sensitive finished! retry:%d, status: %d", static_cast(retry), status); - // sleep 1 second to avoid - ++retry; - }); - th.detach(); -} - -void Security::InitKvStore() -{ - kvStore_ = GetMetaKvStore(delegateMgr_); - ZLOGD("Init KvStore ,kvStore_ is null.%d", kvStore_ == nullptr); + ZLOGD("destructor kvStore_"); } DBStatus Security::RegOnAccessControlledEvent(const OnAccessControlledEvent &callback) @@ -293,24 +246,8 @@ DBStatus Security::SetFileSecurityOption(const std::string &filePath, const Secu bool Security::CheckDeviceSecurityAbility(const std::string &devId, const SecurityOption &option) const { - if (kvStore_ == nullptr) { - ZLOGD("The kv store is null, label:%d", option.securityLabel); - return GetDeviceNodeByUuid(devId, nullptr) >= option; - } - - auto getValue = [this, &devId, &option]() -> std::vector { - Value value; - DBStatus status = kvStore_->Get(GenerateSecurityKey(devId), value); - if (status != OK) { - ZLOGE("Can't get the peer(%.10s)'s cert key! label:%d", devId.c_str(), option.securityLabel); - return {}; - } - return value; - }; - Sensitive sensitive = GetDeviceNodeByUuid(devId, getValue); - - ZLOGD("Got the chain deviceId:%.10s, label:%d", devId.c_str(), option.securityLabel); - return sensitive >= option; + ZLOGD("The kv store is null, label:%d", option.securityLabel); + return GetDeviceNodeByUuid(devId, nullptr) >= option; } int32_t Security::GetCurrentUserId() const @@ -364,54 +301,6 @@ int Security::Convert2Security(const std::string &name) return NOT_SET; } -KvStoreNbDelegate *Security::GetMetaKvStore(KvStoreDelegateManager &delegateMgr) -{ - KvStoreNbDelegate::Option option; - option.createIfNecessary = true; - option.isMemoryDb = false; - option.createDirByStoreIdOnly = true; - option.isEncryptedDb = false; - KvStoreNbDelegate *delegate = nullptr; - delegateMgr.GetKvStore( - Constant::SERVICE_META_DB_NAME, option, - [&delegate](DBStatus status, KvStoreNbDelegate *kvStore) { - if (kvStore != nullptr) { - delegate = kvStore; - } - (void)status; - }); - return delegate; -} - -std::vector Security::GenerateSecurityKey(const std::string &deviceId) const -{ - std::string key = SECURITY_LABEL + Constant::KEY_SEPARATOR + deviceId + Constant::KEY_SEPARATOR + "default"; - return std::vector(key.begin(), key.end()); -} - -void Security::SyncMeta() const -{ - auto &network = AppDistributedKv::CommunicationProvider::GetInstance(); - auto nodeInfos = network.GetRemoteNodesBasicInfo(); - std::vector devices; - for (auto &node : nodeInfos) { - devices.push_back(network.GetUuidByNodeId(node.deviceId)); - } - - kvStore_->Sync(devices, SYNC_MODE_PUSH_ONLY, - [](const std::map &result) { - int count = 0; - for (const auto &[deviceId, status] : result) { - if (status != OK) { - count++; - } - } - if (count > 0) { - ZLOGE("Sync failed(%d), total(%d)!", count, int32_t(result.size())); - } - }); -} - bool Security::IsSupportSecurity() { return IsSupportIudf(); diff --git a/services/distributeddataservice/adapter/security/src/security.h b/services/distributeddataservice/adapter/security/src/security.h index a7baacc37..d7463f7d2 100644 --- a/services/distributeddataservice/adapter/security/src/security.h +++ b/services/distributeddataservice/adapter/security/src/security.h @@ -34,8 +34,6 @@ public: using SecurityOption = DistributedDB::SecurityOption; Security(const std::string &appId, const std::string &userId, const std::string &dir); ~Security() override; - void InitLocalCertData() const; - void InitKvStore(); static bool IsFirstInit(); static bool IsSupportSecurity(); @@ -66,7 +64,6 @@ private: }; // the key is security_chain/{deviceId} - static constexpr const char *SECURITY_LABEL = "SecurityLabel"; static const char * const LABEL_VALUES[DistributedDB::S4 + 1]; static const char * const DATA_DE[]; // = "/data/misc_de/", "/data/user_de/"; static const char * const DATA_CE[]; @@ -75,11 +72,8 @@ private: int32_t GetCurrentUserId() const; int32_t GetCurrentUserStatus() const; bool SubscribeUserStatus(std::function &observer) const; - static DistributedDB::KvStoreNbDelegate *GetMetaKvStore(DistributedDB::KvStoreDelegateManager &delegateMgr); bool IsExits(const std::string &file) const; bool InPathsBox(const std::string &file, const char * const pathsBox[]) const; - std::vector GenerateSecurityKey(const std::string &deviceId) const; - void SyncMeta() const; static Sensitive GetDeviceNodeByUuid(const std::string &uuid, const std::function(void)> &getValue); DBStatus GetDirSecurityOption(const std::string &filePath, SecurityOption &option) const; @@ -88,8 +82,6 @@ private: DBStatus SetFileSecurityOption(const std::string &filePath, const SecurityOption &option); std::map observers_ { }; - DistributedDB::KvStoreDelegateManager delegateMgr_; - DistributedDB::KvStoreNbDelegate *kvStore_ = nullptr; static std::atomic_bool isInitialized_; }; } diff --git a/services/distributeddataservice/adapter/security/src/security_adapter.cpp b/services/distributeddataservice/adapter/security/src/security_adapter.cpp index 96d9f7146..c49140690 100755 --- a/services/distributeddataservice/adapter/security/src/security_adapter.cpp +++ b/services/distributeddataservice/adapter/security/src/security_adapter.cpp @@ -42,8 +42,6 @@ InstallDevsl::InstallDevsl() auto status = DistributedDB::KvStoreDelegateManager::SetProcessSystemAPIAdapter(security_); ZLOGD("set distributed db system api adapter: %d.", static_cast(status)); - - security_->InitKvStore(); } InstallDevsl::~InstallDevsl() @@ -53,12 +51,7 @@ InstallDevsl::~InstallDevsl() void InstallDevsl::Initialize() { - if (security_ == nullptr) { - ZLOGD("Security is nullptr."); - return; - } - - security_->InitLocalCertData(); + return; } __attribute__((used)) InstallDevsl g_installDevsl; } -- Gitee