1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sandbox_config_kv_data_storage.h"
17 #include "dlp_permission_log.h"
18 #include "dlp_permission.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace DlpPermission {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION,
25 "SandboxConfigKvDataStorage"};
26 static const std::string APP_CONFIG_STORE_ID = "sandbox_app_config_info";
27 static const std::string KEY_SEPATATOR = "_";
28 std::mutex g_instanceMutex;
29 }
30
GetInstance()31 SandboxConfigKvDataStorage& SandboxConfigKvDataStorage::GetInstance()
32 {
33 static SandboxConfigKvDataStorage* instance = nullptr;
34 if (instance == nullptr) {
35 std::lock_guard<std::mutex> lock(g_instanceMutex);
36 if (instance == nullptr) {
37 KvDataStorageOptions options = { .autoSync = false };
38 instance = new (std::nothrow) SandboxConfigKvDataStorage(options);
39 }
40 }
41 return *instance;
42 }
43
SandboxConfigKvDataStorage(const KvDataStorageOptions & options)44 SandboxConfigKvDataStorage::SandboxConfigKvDataStorage(const KvDataStorageOptions& options)
45 : DlpKvDataStorage(APP_CONFIG_STORE_ID, options)
46 {}
47
~SandboxConfigKvDataStorage()48 SandboxConfigKvDataStorage::~SandboxConfigKvDataStorage()
49 {}
50
GetSandboxConfigFromDataStorage(int32_t userId,const std::string & bundleName,std::string & configInfo,const std::string tokenId)51 int32_t SandboxConfigKvDataStorage::GetSandboxConfigFromDataStorage(int32_t userId, const std::string& bundleName,
52 std::string& configInfo, const std::string tokenId)
53 {
54 std::string key;
55 bool res = GenerateKey(userId, bundleName, key, tokenId);
56 if (!res) {
57 DLP_LOG_ERROR(LABEL, "generate key error");
58 return DLP_SERVICE_ERROR_VALUE_INVALID;
59 }
60 res = IsKeyExists(key);
61 if (!res) {
62 DLP_LOG_ERROR(LABEL, "the key not exists.");
63 return DLP_KV_GET_DATA_NOT_FOUND;
64 }
65 int32_t result = GetValueFromKvStore(key, configInfo);
66 if (result != DLP_OK) {
67 DLP_LOG_ERROR(LABEL, "failed to get config info by key, result %{public}d.", result);
68 }
69 return result;
70 }
71
AddSandboxConfigIntoDataStorage(int32_t userId,const std::string & bundleName,const std::string & configInfo,const std::string tokenId)72 int32_t SandboxConfigKvDataStorage::AddSandboxConfigIntoDataStorage(int32_t userId, const std::string& bundleName,
73 const std::string& configInfo, const std::string tokenId)
74 {
75 std::string key;
76 bool res = GenerateKey(userId, bundleName, key, tokenId);
77 if (!res) {
78 DLP_LOG_ERROR(LABEL, "generate key error");
79 return DLP_SERVICE_ERROR_VALUE_INVALID;
80 }
81 int32_t result = AddOrUpdateValue(key, configInfo);
82 if (result != DLP_OK) {
83 DLP_LOG_ERROR(LABEL, "failed to add config info, result = %{public}d", result);
84 }
85 return result;
86 }
87
DeleteSandboxConfigFromDataStorage(int32_t userId,const std::string & bundleName,const std::string tokenId)88 int32_t SandboxConfigKvDataStorage::DeleteSandboxConfigFromDataStorage(int32_t userId,
89 const std::string& bundleName, const std::string tokenId)
90 {
91 std::string key;
92 bool res = GenerateKey(userId, bundleName, key, tokenId);
93 if (!res) {
94 DLP_LOG_ERROR(LABEL, "generate key error");
95 return DLP_SERVICE_ERROR_VALUE_INVALID;
96 }
97 res = IsKeyExists(key);
98 if (!res) {
99 DLP_LOG_ERROR(LABEL, "the key not exists.");
100 return DLP_OK;
101 }
102 int32_t ret = RemoveValueFromKvStore(key);
103 if (ret != DLP_OK) {
104 DLP_LOG_ERROR(LABEL, "RemoveValueFromKvStore failed! ret = %{public}d.", ret);
105 }
106 return ret;
107 }
108
GenerateKey(int32_t userId,const std::string & bundleName,std::string & key,const std::string tokenId)109 bool SandboxConfigKvDataStorage::GenerateKey(int32_t userId, const std::string& bundleName, std::string& key,
110 const std::string tokenId)
111 {
112 if (bundleName.empty()) {
113 DLP_LOG_ERROR(LABEL, "bundleName is empty");
114 return false;
115 }
116 key = std::to_string(userId) + KEY_SEPATATOR + bundleName + KEY_SEPATATOR + tokenId;
117 return true;
118 }
119
GetKeyMapByUserId(const int32_t userId,std::map<std::string,std::string> & keyMap)120 int32_t SandboxConfigKvDataStorage::GetKeyMapByUserId(const int32_t userId, std::map<std::string, std::string>& keyMap)
121 {
122 std::map<std::string, std::string> infos;
123 int32_t res = LoadAllData(infos);
124 if (res != DLP_OK) {
125 return res;
126 }
127 std::string prefix = std::to_string(userId) + KEY_SEPATATOR;
128 for (auto it = infos.begin(); it != infos.end(); ++it) {
129 std::size_t first = it->first.find_first_of(KEY_SEPATATOR);
130 std::size_t second = it->first.find_last_of(KEY_SEPATATOR);
131 if (it->first.find(prefix) != std::string::npos && first != second) {
132 std::string bundleName = it->first.substr(prefix.length(), second - first - 1);
133 std::string tokenId = it->first.substr(second + 1, it->first.length() - second - 1);
134 keyMap[bundleName] = tokenId;
135 }
136 }
137 return DLP_OK;
138 }
139
SaveEntries(const std::vector<OHOS::DistributedKv::Entry> & allEntries,std::map<std::string,std::string> & infos)140 void SandboxConfigKvDataStorage::SaveEntries(
141 const std::vector<OHOS::DistributedKv::Entry>& allEntries, std::map<std::string, std::string>& infos)
142 {
143 DLP_LOG_DEBUG(LABEL, "start, allEntries size is: %{public}zu", allEntries.size());
144 for (auto const& item : allEntries) {
145 infos.emplace(item.key.ToString(), item.value.ToString());
146 }
147 }
148 } // namespace DlpPermission
149 } // namespace Security
150 } // namespace OHOS
151