-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2655b43
commit b9244c2
Showing
7 changed files
with
318 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
/** | ||
* Copyright 2024 Centreon | ||
* Licensed under the Apache License, Version 2.0(the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* For more information : [email protected] | ||
*/ | ||
#ifndef CENTREON_AGENT_CONFIG_HH | ||
#define CENTREON_AGENT_CONFIG_HH | ||
|
||
#include "com/centreon/common/grpc/grpc_config.hh" | ||
|
||
namespace com::centreon::agent { | ||
|
||
class config { | ||
public: | ||
enum log_type { stdout, file }; | ||
|
||
private: | ||
std::string _endpoint; | ||
spdlog::level::level_enum _log_level; | ||
log_type _log_type; | ||
std::string _log_file; | ||
unsigned _log_max_file_size; | ||
unsigned _log_max_files; | ||
|
||
bool _encryption; | ||
std::string _certificate_file; | ||
std::string _private_key_file; | ||
std::string _ca_certificate_file; | ||
std::string _ca_name; | ||
std::string _host; | ||
bool _reverse_connection; | ||
|
||
public: | ||
config(const std::string& path); | ||
|
||
const std::string& get_endpoint() const { return _endpoint; } | ||
spdlog::level::level_enum get_log_level() const { return _log_level; }; | ||
log_type get_log_type() const { return _log_type; } | ||
const std::string& get_log_file() const { return _log_file; } | ||
unsigned get_log_max_file_size() const { return _log_max_file_size; } | ||
unsigned get_log_max_files() const { return _log_max_files; } | ||
|
||
bool use_encryption() const { return _encryption; } | ||
const std::string& get_certificate_file() const { return _certificate_file; } | ||
const std::string& get_private_key_file() const { return _private_key_file; } | ||
const std::string& get_ca_certificate_file() const { | ||
return _ca_certificate_file; | ||
} | ||
const std::string& get_ca_name() const { return _ca_name; } | ||
const std::string& get_host() const { return _host; } | ||
bool use_reverse_connection() const { return _reverse_connection; } | ||
}; | ||
}; // namespace com::centreon::agent | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
* Copyright 2024 Centreon | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* For more information : [email protected] | ||
*/ | ||
|
||
#include <rapidjson/document.h> | ||
#include <re2/re2.h> | ||
|
||
#include "com/centreon/common/rapidjson_helper.hh" | ||
#include "com/centreon/exceptions/msg_fmt.hh" | ||
#include "config.hh" | ||
|
||
using namespace com::centreon::agent; | ||
using com::centreon::common::rapidjson_helper; | ||
|
||
static constexpr std::string_view _config_schema(R"( | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "agent config", | ||
"properties": { | ||
"host": { | ||
"description": "IP or dns name, if not given, hostname will be used", | ||
"type": "string", | ||
"minLength": 5 | ||
}, | ||
"endpoint": { | ||
"description": "endpoint of poller where agent have to connect or listen endpoint in case of reverse_connection", | ||
"type": "string", | ||
"pattern": "[\\w\\.:]+:\\w+" | ||
}, | ||
"encryption": { | ||
"description": "true if https, default: false", | ||
"type": "boolean" | ||
}, | ||
"public_cert": { | ||
"description": "path of certificate file .crt", | ||
"type": "string" | ||
}, | ||
"private_key": { | ||
"description": "path of certificate file .key", | ||
"type": "string" | ||
}, | ||
"ca_certificate": { | ||
"description": "path of authority certificate file .crt", | ||
"type": "string" | ||
}, | ||
"ca_name": { | ||
"description": "name of authority certificate", | ||
"type": "string" | ||
}, | ||
"reverse_connection": { | ||
"description": "if true, centagent is a server and centengine will connect to, default:false", | ||
"type": "boolean" | ||
}, | ||
"log_level": { | ||
"description": "log level, may be critical, error, info, debug, trace", | ||
"type": "string", | ||
"pattern": "critical|error|info|debug|trace" | ||
}, | ||
"log_type": { | ||
"description": "stdout or file if log to a file (path must be given by log_file), default: stdout", | ||
"type": "string", | ||
"pattern": "stdout|file" | ||
}, | ||
"log_file": { | ||
"description": "path of the log file", | ||
"type": "string", | ||
"minLength": 5 | ||
}, | ||
"log_max_file_size": { | ||
"description:": "max size in Mo of the log file before rotate, to be valid log_max_files must be also be specified", | ||
"type": "integer", | ||
"min": 1 | ||
}, | ||
"log_max_files": { | ||
"description:": "max of log files before remove, to be valid log_max_file_size must be also be specified", | ||
"type": "integer", | ||
"min": 1 | ||
} | ||
}, | ||
"required": [ | ||
"endpoint" | ||
], | ||
"type": "object" | ||
} | ||
)"); | ||
|
||
config::config(const std::string& path) { | ||
static common::json_validator validator(_config_schema); | ||
rapidjson::Document file_content_d; | ||
try { | ||
file_content_d = rapidjson_helper::read_from_file(path); | ||
} catch (const std::exception& e) { | ||
SPDLOG_ERROR("incorrect json file{}: {} ", path, e.what()); | ||
throw; | ||
} | ||
|
||
common::rapidjson_helper json_config(file_content_d); | ||
|
||
try { | ||
json_config.validate(validator); | ||
} catch (const std::exception& e) { | ||
SPDLOG_ERROR("forbidden values in agent config: {}", e.what()); | ||
throw; | ||
} | ||
|
||
_endpoint = json_config.get_string("endpoint"); | ||
|
||
// pattern schema doesn't work so we do it ourselves | ||
if (!RE2::FullMatch(_endpoint, "[\\w\\.:]+:\\w+")) { | ||
throw exceptions::msg_fmt( | ||
"bad format for endpoint {}, it must match to the regex: " | ||
"[\\w\\.:]+:\\w+", | ||
_endpoint); | ||
} | ||
_log_level = | ||
spdlog::level::from_str(json_config.get_string("log_level", "info")); | ||
_log_type = | ||
json_config.get_string("log_type", "stdout") == "file" ? file : stdout; | ||
_log_file = json_config.get_string("log_file", ""); | ||
_log_max_file_size = json_config.get_unsigned("log_max_file_size", 0); | ||
_log_max_files = json_config.get_unsigned("log_max_files", 0); | ||
_encryption = json_config.get_bool("encryption", false); | ||
_certificate_file = json_config.get_string("certificate_file", ""); | ||
_private_key_file = json_config.get_string("private_key_file", ""); | ||
_ca_certificate_file = json_config.get_string("ca_certificate_file", ""); | ||
_ca_name = json_config.get_string("ca_name", ""); | ||
_host = json_config.get_string("host", ""); | ||
if (_host.empty()) { | ||
_host = boost::asio::ip::host_name(); | ||
} | ||
_reverse_connection = json_config.get_bool("reverse_connection", false); | ||
} |
Oops, something went wrong.