• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "event_group_config.h"
17 
18 #include "file_ex.h"
19 #include "config_data_manager.h"
20 #include "json_cfg.h"
21 #include "security_guard_log.h"
22 #include "security_guard_utils.h"
23 #include "config_define.h"
24 namespace OHOS::Security::SecurityGuard {
Load(int mode)25 bool EventGroupConfig::Load(int mode)
26 {
27     std::string path;
28     if (mode == INIT_MODE) {
29         if (FileExists(CONFIG_PRESET_FILES[EVENT_GROUP_CFG_INDEX])) {
30             path = CONFIG_PRESET_FILES[EVENT_GROUP_CFG_INDEX];
31         }
32     }
33     SGLOGD("path=%{public}s", path.c_str());
34     if (path.empty()) {
35         SGLOGE("path is empty");
36         return false;
37     }
38     stream_ = std::ifstream(path, std::ios::in);
39     if (!stream_.is_open()) {
40         SGLOGE("stream error, %{public}s", strerror(errno));
41         return false;
42     }
43     return true;
44 }
45 
Parse()46 bool EventGroupConfig::Parse()
47 {
48     if (!stream_.is_open()) {
49         SGLOGE("stream error");
50         return false;
51     }
52     nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
53     stream_.close();
54 
55     if (jsonObj.is_discarded()) {
56         SGLOGI("json is discarded");
57         return false;
58     }
59 
60     bool success = ParseEventGroupConfig(jsonObj);
61     if (!success) {
62         SGLOGE("parse EventGroupConfig error");
63         return false;
64     }
65     return true;
66 }
67 
Update()68 bool EventGroupConfig::Update()
69 {
70     return true;
71 }
72 
ParseEventGroupConfig(const nlohmann::json & jsonObj)73 bool EventGroupConfig::ParseEventGroupConfig(const nlohmann::json &jsonObj)
74 {
75     if (jsonObj.find("eventGroupList") == jsonObj.end() || !jsonObj.at("eventGroupList").is_array()) {
76         SGLOGE("not find eventGroupList or type err");
77         return false;
78     }
79     std::unordered_map<std::string, EventGroupCfg> eventGroupMap;
80     for (const auto &iter : jsonObj["eventGroupList"]) {
81         if (!iter.is_object()) {
82             return false;
83         }
84         std::vector<std::string> eventList;
85         EventGroupCfg cfg {};
86         if (!JsonCfg::Unmarshal(cfg.eventGroupName, iter, "eventGroupName") || cfg.eventGroupName == "") {
87             SGLOGE("fail to parse eventGroupName");
88             return false;
89         }
90         if (!JsonCfg::Unmarshal(eventList, iter, "eventList")) {
91             SGLOGE("fail to parse eventList");
92             return false;
93         }
94         for (const auto &event : eventList) {
95             int64_t tmp = 0;
96             if (event == "" || !SecurityGuardUtils::StrToI64Hex(event, tmp)) {
97                 continue;
98             }
99             cfg.eventList.insert(tmp);
100         }
101         std::vector<std::string> permissonList;
102         if (!JsonCfg::Unmarshal(permissonList, iter, "permission")) {
103             SGLOGE("fail to parse permission");
104             return false;
105         }
106         for (const auto &it : permissonList) {
107             cfg.permissionList.insert(it);
108         }
109         int32_t isBatchUpload = 0;
110         if (!JsonCfg::Unmarshal(isBatchUpload, iter, "isBatchUpload")) {
111             SGLOGE("fail to parse isBatchUpload");
112             return false;
113         }
114         cfg.isBatchUpload = isBatchUpload;
115         eventGroupMap[cfg.eventGroupName] = cfg;
116     }
117     ConfigDataManager::GetInstance().InsertEventGroupMap(eventGroupMap);
118     return true;
119 }
120 
121 } // OHOS::Security::SecurityGuard
122