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 16 : Whitelist &Whitelist::GetInstance()
18 : {
19 16 : static Whitelist whitelist;
20 16 : 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 8 : void Whitelist::GetHostWhiteList(std::vector<IpAddress> &whiteList)
30 : {
31 8 : std::unique_lock<std::mutex> lock(whiteListsMutex);
32 8 : whiteList.clear();
33 8 : auto iter = whiteLists.find(WhiteListType::HCCL_WHITELIST_HOST);
34 8 : if (iter == whiteLists.end()) {
35 3 : HCCL_INFO("GetHostWhiteList: white list is empty.");
36 1 : return;
37 : }
38 7 : whiteList = whiteLists[WhiteListType::HCCL_WHITELIST_HOST];
39 21 : HCCL_INFO("GetHostWhiteList: whitelist length is %zu.", whiteList.size());
40 8 : }
41 :
42 8 : void Whitelist::LoadConfigFile(const std::string &realName)
43 : {
44 8 : if (realName.empty()) {
45 3 : HCCL_ERROR("Load ConfigFile whitelist file path is NULL.");
46 2 : THROW<InvalidParamsException>(StringFormat("[Load][ConfigFile]errNo[0x%016llx] whitelist file path is NULL.",
47 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
48 : }
49 7 : std::unique_lock<std::mutex> lock(whiteListsMutex);
50 7 : whiteLists.clear();
51 :
52 7 : nlohmann::json fileContent;
53 7 : std::ifstream infile(realName.c_str(), std::ifstream::in);
54 7 : if (!infile) {
55 3 : HCCL_ERROR("[Load][ConfigFile]errNo[0x%016llx] open file %s failed",
56 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL), realName.c_str());
57 2 : THROW<InternalException>(StringFormat("[Load][ConfigFile]errNo[0x%016llx] open file %s failed",
58 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL), realName.c_str()));
59 : return;
60 : } else {
61 6 : fileContent.clear();
62 : try {
63 6 : infile >> fileContent; // 将文件内容读取到json对象内
64 0 : } catch (...) {
65 0 : HCCL_ERROR("[Load][ConfigFile]errNo[0x%016llx] load file[%s] to json fail. please check json file format.",
66 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL), realName.c_str());
67 0 : infile.close();
68 0 : THROW<InternalException>(StringFormat(
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 : }
72 : }
73 6 : infile.close();
74 :
75 6 : nlohmann::json hostWhitelist = GetHostIp(fileContent);
76 :
77 12 : for (auto &ipJson : hostWhitelist) {
78 6 : std::string ipStr;
79 : try {
80 6 : ipStr = ipJson.get<std::string>();
81 0 : } catch (...) {
82 0 : HCCL_ERROR("[Load][ConfigFile]errNo[0x%016llx]get ipStr from ipJson failed, please check host white list",
83 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL));
84 0 : THROW<InternalException>(StringFormat(
85 : "[Load][ConfigFile]errNo[0x%016llx]get ipStr from ipJson failed, please check host white list",
86 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL)));
87 0 : }
88 6 : IpAddress ip(ipStr);
89 6 : whiteLists[WhiteListType::HCCL_WHITELIST_HOST].push_back(ip);
90 6 : }
91 9 : }
92 :
93 6 : nlohmann::json Whitelist::GetHostIp(nlohmann::json fileContent) const
94 : {
95 6 : nlohmann::json hostWhitelist;
96 6 : if (fileContent.find("host_ip") == fileContent.end()) {
97 0 : HCCL_ERROR("[Get][JsonProperty]errNo[0x%016llx] json object has no property called host_ip",
98 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL));
99 0 : THROW<InternalException>(
100 0 : StringFormat("[Get][JsonProperty]errNo[0x%016llx] json object has no property called host_ip",
101 : HCOM_ERROR_CODE(HcclResult::HCCL_E_INTERNAL)));
102 : }
103 6 : hostWhitelist = fileContent["host_ip"];
104 6 : return hostWhitelist;
105 0 : }
106 :
107 : } // namespace Hccl
|