Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include <fstream>
12 : #include "whitelist.h"
13 : #include "internal_exception.h"
14 : #include "invalid_params_exception.h"
15 :
16 : namespace Hccl {
17 1 : Whitelist& Whitelist::GetInstance()
18 : {
19 1 : static Whitelist whitelist;
20 1 : return whitelist;
21 : }
22 :
23 1 : Whitelist::~Whitelist()
24 : {
25 1 : std::unique_lock<std::mutex> lock(whiteListsMutex);
26 1 : whiteLists.clear();
27 1 : }
28 :
29 0 : void Whitelist::GetHostWhiteList(std::vector<IpAddress>& whiteList)
30 : {
31 0 : std::unique_lock<std::mutex> lock(whiteListsMutex);
32 0 : whiteList.clear();
33 0 : auto iter = whiteLists.find(WhiteListType::HCCL_WHITELIST_HOST);
34 0 : if (iter == whiteLists.end()) {
35 0 : HCCL_INFO("GetHostWhiteList: white list is empty.");
36 0 : return;
37 : }
38 0 : whiteList = whiteLists[WhiteListType::HCCL_WHITELIST_HOST];
39 0 : HCCL_INFO("GetHostWhiteList: whitelist length is %zu.", whiteList.size());
40 0 : }
41 :
42 0 : void Whitelist::LoadConfigFile(const std::string& realName)
43 : {
44 0 : if (realName.empty()) {
45 0 : HCCL_ERROR("Load ConfigFile whitelist file path is NULL.");
46 0 : THROW<InvalidParamsException>(StringFormat(
47 : "[Load][ConfigFile]errNo[0x%016llx] whitelist file path is NULL.",
48 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
49 : }
50 0 : std::unique_lock<std::mutex> lock(whiteListsMutex);
51 0 : whiteLists.clear();
52 :
53 0 : nlohmann::json fileContent;
54 0 : std::ifstream infile(realName.c_str(), std::ifstream::in);
55 0 : if (!infile) {
56 0 : HCCL_ERROR(
57 : "[Load][ConfigFile]errNo[0x%016llx] open file %s failed", HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL),
58 : realName.c_str());
59 0 : THROW<InternalException>(StringFormat(
60 : "[Load][ConfigFile]errNo[0x%016llx] open file %s failed", HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL),
61 : realName.c_str()));
62 : return;
63 : } else {
64 0 : fileContent.clear();
65 : try {
66 0 : infile >> fileContent; // 将文件内容读取到json对象内
67 0 : } catch (...) {
68 0 : HCCL_ERROR(
69 : "[Load][ConfigFile]errNo[0x%016llx] load file[%s] to json fail. please check json file format.",
70 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL), realName.c_str());
71 0 : infile.close();
72 0 : THROW<InternalException>(StringFormat(
73 : "[Load][ConfigFile]errNo[0x%016llx] load file[%s] to json fail. please check json file format.",
74 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL), realName.c_str()));
75 0 : }
76 : }
77 0 : infile.close();
78 :
79 0 : nlohmann::json hostWhitelist = GetHostIp(fileContent);
80 :
81 0 : for (auto& ipJson : hostWhitelist) {
82 0 : std::string ipStr;
83 : try {
84 0 : ipStr = ipJson.get<std::string>();
85 0 : } catch (...) {
86 0 : HCCL_ERROR(
87 : "[Load][ConfigFile]errNo[0x%016llx]get ipStr from ipJson failed, please check host white list",
88 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL));
89 0 : THROW<InternalException>(StringFormat(
90 : "[Load][ConfigFile]errNo[0x%016llx]get ipStr from ipJson failed, please check host white list",
91 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL)));
92 0 : }
93 0 : IpAddress ip(ipStr);
94 0 : whiteLists[WhiteListType::HCCL_WHITELIST_HOST].push_back(ip);
95 0 : }
96 0 : }
97 :
98 0 : nlohmann::json Whitelist::GetHostIp(nlohmann::json fileContent) const
99 : {
100 0 : nlohmann::json hostWhitelist;
101 0 : if (fileContent.find("host_ip") == fileContent.end()) {
102 0 : HCCL_ERROR(
103 : "[Get][JsonProperty]errNo[0x%016llx] json object has no property called host_ip",
104 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL));
105 0 : THROW<InternalException>(StringFormat(
106 : "[Get][JsonProperty]errNo[0x%016llx] json object has no property called host_ip",
107 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL)));
108 : }
109 0 : hostWhitelist = fileContent["host_ip"];
110 0 : return hostWhitelist;
111 0 : }
112 :
113 : } // namespace Hccl
|