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 5 : Whitelist& Whitelist::GetInstance()
18 : {
19 5 : static Whitelist whitelist;
20 5 : 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 2 : void Whitelist::GetHostWhiteList(std::vector<IpAddress>& whiteList)
30 : {
31 2 : std::unique_lock<std::mutex> lock(whiteListsMutex);
32 2 : whiteList.clear();
33 2 : auto iter = whiteLists.find(WhiteListType::HCCL_WHITELIST_HOST);
34 2 : if (iter == whiteLists.end()) {
35 3 : HCCL_INFO("GetHostWhiteList: white list is empty.");
36 1 : return;
37 : }
38 1 : whiteList = whiteLists[WhiteListType::HCCL_WHITELIST_HOST];
39 3 : HCCL_INFO("GetHostWhiteList: whitelist length is %zu.", whiteList.size());
40 2 : }
41 :
42 3 : void Whitelist::LoadConfigFile(const std::string& realName)
43 : {
44 3 : if (realName.empty()) {
45 3 : HCCL_ERROR("Load ConfigFile whitelist file path is NULL.");
46 2 : THROW<InvalidParamsException>(StringFormat(
47 : "[Load][ConfigFile]errNo[0x%016llx] whitelist file path is NULL.",
48 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
49 : }
50 2 : std::unique_lock<std::mutex> lock(whiteListsMutex);
51 2 : whiteLists.clear();
52 :
53 2 : nlohmann::json fileContent;
54 2 : std::ifstream infile(realName.c_str(), std::ifstream::in);
55 2 : if (!infile) {
56 3 : HCCL_ERROR(
57 : "[Load][ConfigFile]errNo[0x%016llx] open file %s failed", HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL),
58 : realName.c_str());
59 2 : 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 1 : fileContent.clear();
65 : try {
66 1 : 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 1 : infile.close();
78 :
79 1 : nlohmann::json hostWhitelist = GetHostIp(fileContent);
80 :
81 2 : for (auto& ipJson : hostWhitelist) {
82 1 : std::string ipStr;
83 : try {
84 1 : 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 1 : IpAddress ip(ipStr);
94 1 : whiteLists[WhiteListType::HCCL_WHITELIST_HOST].push_back(ip);
95 1 : }
96 4 : }
97 :
98 1 : nlohmann::json Whitelist::GetHostIp(nlohmann::json fileContent) const
99 : {
100 1 : nlohmann::json hostWhitelist;
101 1 : 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 1 : hostWhitelist = fileContent["host_ip"];
110 1 : return hostWhitelist;
111 0 : }
112 :
113 : } // namespace Hccl
|