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 }
29 
GetInstance()30 SandboxConfigKvDataStorage& SandboxConfigKvDataStorage::GetInstance()
31 {
32     KvDataStorageOptions options = { .autoSync = false };
33     static SandboxConfigKvDataStorage *instance = new (std::nothrow) SandboxConfigKvDataStorage(options);
34     return *instance;
35 }
36 
SandboxConfigKvDataStorage(const KvDataStorageOptions & options)37 SandboxConfigKvDataStorage::SandboxConfigKvDataStorage(const KvDataStorageOptions& options)
38     : DlpKvDataStorage(APP_CONFIG_STORE_ID, options)
39 {}
40 
~SandboxConfigKvDataStorage()41 SandboxConfigKvDataStorage::~SandboxConfigKvDataStorage()
42 {}
43 
GetSandboxConfigFromDataStorage(int32_t userId,const std::string & bundleName,std::string & configInfo,const std::string tokenId)44 int32_t SandboxConfigKvDataStorage::GetSandboxConfigFromDataStorage(int32_t userId, const std::string& bundleName,
45     std::string& configInfo, const std::string tokenId)
46 {
47     std::string key;
48     bool res = GenerateKey(userId, bundleName, key, tokenId);
49     if (!res) {
50         DLP_LOG_ERROR(LABEL, "generate key error");
51         return DLP_SERVICE_ERROR_VALUE_INVALID;
52     }
53     res = IsKeyExists(key);
54     if (!res) {
55         DLP_LOG_ERROR(LABEL, "the key not exists.");
56         return DLP_KV_GET_DATA_NOT_FOUND;
57     }
58     int32_t result = GetValueFromKvStore(key, configInfo);
59     if (result != DLP_OK) {
60         DLP_LOG_ERROR(LABEL, "failed to get config info by key, result %{public}d.", result);
61     }
62     return result;
63 }
64 
AddSandboxConfigIntoDataStorage(int32_t userId,const std::string & bundleName,const std::string & configInfo,const std::string tokenId)65 int32_t SandboxConfigKvDataStorage::AddSandboxConfigIntoDataStorage(int32_t userId, const std::string& bundleName,
66     const std::string& configInfo, const std::string tokenId)
67 {
68     std::string key;
69     bool res = GenerateKey(userId, bundleName, key, tokenId);
70     if (!res) {
71         DLP_LOG_ERROR(LABEL, "generate key error");
72         return DLP_SERVICE_ERROR_VALUE_INVALID;
73     }
74     int32_t result = AddOrUpdateValue(key, configInfo);
75     if (result != DLP_OK) {
76         DLP_LOG_ERROR(LABEL, "failed to add config info, result = %{public}d", result);
77     }
78     return result;
79 }
80 
DeleteSandboxConfigFromDataStorage(int32_t userId,const std::string & bundleName,const std::string tokenId)81 int32_t SandboxConfigKvDataStorage::DeleteSandboxConfigFromDataStorage(int32_t userId,
82     const std::string& bundleName, const std::string tokenId)
83 {
84     std::string key;
85     bool res = GenerateKey(userId, bundleName, key, tokenId);
86     if (!res) {
87         DLP_LOG_ERROR(LABEL, "generate key error");
88         return DLP_SERVICE_ERROR_VALUE_INVALID;
89     }
90     res = IsKeyExists(key);
91     if (!res) {
92         DLP_LOG_ERROR(LABEL, "the key not exists.");
93         return DLP_OK;
94     }
95     int32_t ret = RemoveValueFromKvStore(key);
96     if (ret != DLP_OK) {
97         DLP_LOG_ERROR(LABEL, "RemoveValueFromKvStore failed! ret = %{public}d.", ret);
98     }
99     return ret;
100 }
101 
GenerateKey(int32_t userId,const std::string & bundleName,std::string & key,const std::string tokenId)102 bool SandboxConfigKvDataStorage::GenerateKey(int32_t userId, const std::string& bundleName, std::string& key,
103     const std::string tokenId)
104 {
105     if (bundleName.empty()) {
106         DLP_LOG_ERROR(LABEL, "bundleName is empty");
107         return false;
108     }
109     key = std::to_string(userId) + KEY_SEPATATOR + bundleName + KEY_SEPATATOR + tokenId;
110     return true;
111 }
112 
GetKeyMapByUserId(const int32_t userId,std::map<std::string,std::string> & keyMap)113 int32_t SandboxConfigKvDataStorage::GetKeyMapByUserId(const int32_t userId, std::map<std::string, std::string>& keyMap)
114 {
115     std::map<std::string, std::string> infos;
116     int32_t res = LoadAllData(infos);
117     if (res != DLP_OK) {
118         return res;
119     }
120     std::string prefix = std::to_string(userId) + KEY_SEPATATOR;
121     for (auto it = infos.begin(); it != infos.end(); ++it) {
122         std::size_t first = it->first.find_first_of(KEY_SEPATATOR);
123         std::size_t second = it->first.find_last_of(KEY_SEPATATOR);
124         if (it->first.find(prefix) != std::string::npos && first != second) {
125             std::string bundleName = it->first.substr(prefix.length(), second - first - 1);
126             std::string tokenId = it->first.substr(second + 1, it->first.length() - second - 1);
127             keyMap[bundleName] = tokenId;
128         }
129     }
130     return DLP_OK;
131 }
132 
SaveEntries(const std::vector<OHOS::DistributedKv::Entry> & allEntries,std::map<std::string,std::string> & infos)133 void SandboxConfigKvDataStorage::SaveEntries(
134     const std::vector<OHOS::DistributedKv::Entry>& allEntries, std::map<std::string, std::string>& infos)
135 {
136     DLP_LOG_DEBUG(LABEL, "start, allEntries size is: %{public}zu", allEntries.size());
137     for (auto const& item : allEntries) {
138         infos.emplace(item.key.ToString(), item.value.ToString());
139     }
140 }
141 }  // namespace DlpPermission
142 }  // namespace Security
143 }  // namespace OHOS
144