• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(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 GetSandboxAppConfig not found, will return ok and configInfo is empty.
55     if (!res) {
56         DLP_LOG_ERROR(LABEL, "the key not exists.");
57         return DLP_OK;
58     }
59     int32_t result = GetValueFromKvStore(key, configInfo);
60     if (result != DLP_OK) {
61         DLP_LOG_ERROR(LABEL, "failed to get config info by key, result %{public}d.", result);
62     }
63     return result;
64 }
65 
AddSandboxConfigIntoDataStorage(int32_t userId,const std::string & bundleName,const std::string & configInfo,const std::string tokenId)66 int32_t SandboxConfigKvDataStorage::AddSandboxConfigIntoDataStorage(int32_t userId, const std::string& bundleName,
67     const std::string& configInfo, const std::string tokenId)
68 {
69     std::string key;
70     bool res = GenerateKey(userId, bundleName, key, tokenId);
71     if (!res) {
72         DLP_LOG_ERROR(LABEL, "generate key error");
73         return DLP_SERVICE_ERROR_VALUE_INVALID;
74     }
75     int32_t result = AddOrUpdateValue(key, configInfo);
76     if (result != DLP_OK) {
77         DLP_LOG_ERROR(LABEL, "failed to add config info, result = %{public}d", result);
78     }
79     return result;
80 }
81 
DeleteSandboxConfigFromDataStorage(int32_t userId,const std::string & bundleName,const std::string tokenId)82 int32_t SandboxConfigKvDataStorage::DeleteSandboxConfigFromDataStorage(int32_t userId,
83     const std::string& bundleName, const std::string tokenId)
84 {
85     std::string key;
86     bool res = GenerateKey(userId, bundleName, key, tokenId);
87     if (!res) {
88         DLP_LOG_ERROR(LABEL, "generate key error");
89         return DLP_SERVICE_ERROR_VALUE_INVALID;
90     }
91     res = IsKeyExists(key);
92     if (!res) {
93         DLP_LOG_ERROR(LABEL, "the key not exists.");
94         return DLP_OK;
95     }
96     int32_t ret = RemoveValueFromKvStore(key);
97     if (ret != DLP_OK) {
98         DLP_LOG_ERROR(LABEL, "RemoveValueFromKvStore failed! ret = %{public}d.", ret);
99     }
100     return ret;
101 }
102 
GenerateKey(int32_t userId,const std::string & bundleName,std::string & key,const std::string tokenId)103 bool SandboxConfigKvDataStorage::GenerateKey(int32_t userId, const std::string& bundleName, std::string& key,
104     const std::string tokenId)
105 {
106     if (bundleName.empty()) {
107         DLP_LOG_ERROR(LABEL, "bundleName is empty");
108         return false;
109     }
110     key = std::to_string(userId) + KEY_SEPATATOR + bundleName + KEY_SEPATATOR + tokenId;
111     return true;
112 }
113 
GetKeyMapByUserId(const int32_t userId,std::map<std::string,std::string> & keyMap)114 int32_t SandboxConfigKvDataStorage::GetKeyMapByUserId(const int32_t userId, std::map<std::string, std::string>& keyMap)
115 {
116     std::map<std::string, std::string> infos;
117     int32_t res = LoadAllData(infos);
118     if (res != DLP_OK) {
119         return res;
120     }
121     std::string prefix = std::to_string(userId) + KEY_SEPATATOR;
122     for (auto it = infos.begin(); it != infos.end(); ++it) {
123         std::size_t first = it->first.find_first_of(KEY_SEPATATOR);
124         std::size_t second = it->first.find_last_of(KEY_SEPATATOR);
125         if (it->first.find(prefix) != std::string::npos && first != second) {
126             std::string bundleName = it->first.substr(prefix.length(), second - first - 1);
127             std::string tokenId = it->first.substr(second + 1, it->first.length() - second - 1);
128             keyMap[bundleName] = tokenId;
129         }
130     }
131     return DLP_OK;
132 }
133 
SaveEntries(const std::vector<OHOS::DistributedKv::Entry> & allEntries,std::map<std::string,std::string> & infos)134 void SandboxConfigKvDataStorage::SaveEntries(
135     const std::vector<OHOS::DistributedKv::Entry>& allEntries, std::map<std::string, std::string>& infos)
136 {
137     DLP_LOG_DEBUG(LABEL, "start, allEntries size is: %{public}zu", allEntries.size());
138     for (auto const& item : allEntries) {
139         infos.emplace(item.key.ToString(), item.value.ToString());
140     }
141 }
142 }  // namespace DlpPermission
143 }  // namespace Security
144 }  // namespace OHOS
145