Verified Commit e6394298 authored by Pavel Doležal's avatar Pavel Doležal
Browse files

Make ConfigFile independent struct instead of a PollAble object

parent b7a55ec5
Pipeline #69353 passed with stage
in 2 minutes and 13 seconds
......@@ -21,88 +21,104 @@
* the two.
*/
#include <yaml-cpp/yaml.h>
#include <stdexcept>
#include <boost/any.hpp>
#include <utility>
#include <algorithm>
#include <bitset>
#include <yaml-cpp/yaml.h>
#include "ConfigItem.h"
#include "ConfigFile.h"
#include "core/Probe.h"
#include "utils/Logger.h"
void DDP::ConfigFile::load_configuration(Config& cfg, std::string conf_file, std::string instance)
{
try {
YAML::Node config = YAML::LoadFile(conf_file);
if (config["default"])
load_instance(cfg, config["default"]);
if (instance != "default" && config[instance])
load_instance(cfg, config[instance]);
}
catch (std::exception& e) {
Logger("YAML").warning() << "Couldn't load configuration file " << conf_file
<< " - " << e.what() << ". Using default configuration.";
}
}
void DDP::ConfigFile::load_instance(YAML::Node node)
void DDP::ConfigFile::load_instance(Config& cfg, YAML::Node node)
{
if (node["interface-list"]) {
for (auto item : node["interface-list"]) {
m_cfg.interface_list.add_value(item.as<std::string>());
cfg.interface_list.add_value(item.as<std::string>());
}
}
if (node["pcap-list"]) {
for (auto item : node["pcap-list"]) {
m_cfg.pcap_list.add_value(item.as<std::string>());
cfg.pcap_list.add_value(item.as<std::string>());
}
}
if (node["raw-pcap"])
m_cfg.raw_pcap.add_value(node["raw-pcap"].as<bool>());
cfg.raw_pcap.add_value(node["raw-pcap"].as<bool>());
if (node["log-file"])
m_cfg.log_file.add_value(node["log-file"].as<std::string>());
cfg.log_file.add_value(node["log-file"].as<std::string>());
if (node["coremask"])
m_cfg.coremask.add_value(node["coremask"].as<uint64_t>());
cfg.coremask.add_value(node["coremask"].as<uint64_t>());
if (node["dns-ports"]) {
for (auto item : node["dns-ports"]) {
m_cfg.dns_ports.add_value(item.as<Port_t>());
cfg.dns_ports.add_value(item.as<Port_t>());
}
}
if (node["ipv4-allowlist"]) {
for (auto item : node["ipv4-allowlist"]) {
m_cfg.ipv4_allowlist.add_value(item.as<std::string>());
cfg.ipv4_allowlist.add_value(item.as<std::string>());
}
}
if (node["ipv4-denylist"]) {
for (auto item : node["ipv4-denylist"]) {
m_cfg.ipv4_denylist.add_value(item.as<std::string>());
cfg.ipv4_denylist.add_value(item.as<std::string>());
}
}
if (node["ipv6-allowlist"]) {
for (auto item : node["ipv6-allowlist"]) {
m_cfg.ipv6_allowlist.add_value(item.as<std::string>());
cfg.ipv6_allowlist.add_value(item.as<std::string>());
}
}
if (node["ipv6-denylist"]) {
for (auto item : node["ipv6-denylist"]) {
m_cfg.ipv6_denylist.add_value(item.as<std::string>());
cfg.ipv6_denylist.add_value(item.as<std::string>());
}
}
if (node["export"]["location"])
m_cfg.export_location.add_value(node["export"]["location"].as<std::string>());
cfg.export_location.add_value(node["export"]["location"].as<std::string>());
if (node["export"]["export-dir"])
m_cfg.target_directory.add_value(node["export"]["export-dir"].as<std::string>());
cfg.target_directory.add_value(node["export"]["export-dir"].as<std::string>());
if (node["export"]["remote-ip-address"])
m_cfg.export_ip.add_value(node["export"]["remote-ip-address"].as<std::string>());
cfg.export_ip.add_value(node["export"]["remote-ip-address"].as<std::string>());
if (node["export"]["remote-port"])
m_cfg.export_port.add_value(node["export"]["remote-port"].as<uint16_t>());
cfg.export_port.add_value(node["export"]["remote-port"].as<uint16_t>());
if (node["export"]["remote-ca-cert"])
m_cfg.export_ca_cert.add_value(node["export"]["remote-ca-cert"].as<std::string>());
cfg.export_ca_cert.add_value(node["export"]["remote-ca-cert"].as<std::string>());
if (node["export"]["export-format"])
m_cfg.export_format.add_value(node["export"]["export-format"].as<std::string>());
cfg.export_format.add_value(node["export"]["export-format"].as<std::string>());
if (node["export"]["cdns-fields"] && node["export"]["cdns-fields"].IsSequence()) {
std::bitset<CdnsBits> fields;
......@@ -114,88 +130,54 @@ void DDP::ConfigFile::load_instance(YAML::Node node)
fields.set(found->second);
}
m_cfg.cdns_fields.add_value(fields);
cfg.cdns_fields.add_value(fields);
}
if (node["export"]["cdns-records-per-block"])
m_cfg.cdns_records_per_block.add_value(node["export"]["cdns-records-per-block"].as<uint64_t>());
cfg.cdns_records_per_block.add_value(node["export"]["cdns-records-per-block"].as<uint64_t>());
if (node["export"]["cdns-blocks-per-file"])
m_cfg.cdns_blocks_per_file.add_value(node["export"]["cdns-blocks-per-file"].as<uint64_t>());
cfg.cdns_blocks_per_file.add_value(node["export"]["cdns-blocks-per-file"].as<uint64_t>());
if (node["export"]["parquet-records-per-file"])
m_cfg.parquet_records.add_value(node["export"]["parquet-records-per-file"].as<uint64_t>());
cfg.parquet_records.add_value(node["export"]["parquet-records-per-file"].as<uint64_t>());
if (node["export"]["file-name-prefix"])
m_cfg.file_prefix.add_value(node["export"]["file-name-prefix"].as<std::string>());
cfg.file_prefix.add_value(node["export"]["file-name-prefix"].as<std::string>());
if (node["export"]["timeout"])
m_cfg.file_rot_timeout.add_value(node["export"]["timeout"].as<uint32_t>());
cfg.file_rot_timeout.add_value(node["export"]["timeout"].as<uint32_t>());
if (node["export"]["file-size-limit"])
m_cfg.file_rot_size.add_value(node["export"]["file-size-limit"].as<uint64_t>());
cfg.file_rot_size.add_value(node["export"]["file-size-limit"].as<uint64_t>());
if (node["export"]["file-compression"])
m_cfg.file_compression.add_value(node["export"]["file-compression"].as<bool>());
cfg.file_compression.add_value(node["export"]["file-compression"].as<bool>());
if (node["export"]["pcap-export"])
m_cfg.pcap_export.add_value(node["export"]["pcap-export"].as<std::string>());
cfg.pcap_export.add_value(node["export"]["pcap-export"].as<std::string>());
if (node["ip-anonymization"]["anonymize-ip"])
m_cfg.anonymize_ip.add_value(node["ip-anonymization"]["anonymize-ip"].as<bool>());
cfg.anonymize_ip.add_value(node["ip-anonymization"]["anonymize-ip"].as<bool>());
if (node["ip-anonymization"]["encryption"])
m_cfg.ip_encryption.add_value(node["ip-anonymization"]["encryption"].as<std::string>());
cfg.ip_encryption.add_value(node["ip-anonymization"]["encryption"].as<std::string>());
if (node["ip-anonymization"]["key-path"])
m_cfg.ip_enc_key.add_value(node["ip-anonymization"]["key-path"].as<std::string>());
cfg.ip_enc_key.add_value(node["ip-anonymization"]["key-path"].as<std::string>());
if (node["transaction-table"]["max-transactions"])
m_cfg.tt_size.add_value(node["transaction-table"]["max-transactions"].as<uint32_t>());
cfg.tt_size.add_value(node["transaction-table"]["max-transactions"].as<uint32_t>());
if (node["transaction-table"]["query-timeout"])
m_cfg.tt_timeout.add_value(node["transaction-table"]["query-timeout"].as<uint64_t>());
cfg.tt_timeout.add_value(node["transaction-table"]["query-timeout"].as<uint64_t>());
if (node["transaction-table"]["match-qname"])
m_cfg.match_qname.add_value(node["transaction-table"]["match-qname"].as<bool>());
cfg.match_qname.add_value(node["transaction-table"]["match-qname"].as<bool>());
if (node["tcp-table"]["concurrent-connections"])
m_cfg.tcp_ct_size.add_value(node["tcp-table"]["concurrent-connections"].as<uint32_t>());
cfg.tcp_ct_size.add_value(node["tcp-table"]["concurrent-connections"].as<uint32_t>());
if (node["tcp-table"]["timeout"])
m_cfg.tcp_ct_timeout.add_value(node["tcp-table"]["timeout"].as<uint64_t>());
}
DDP::ConfigFile::ConfigFile(Config& cfg, std::string conf_file, std::string instance) : PollAble(),
m_cfg(cfg), m_logger("YAML"), m_fd()
{
try {
YAML::Node config = YAML::LoadFile(conf_file);
if (config["default"])
load_instance(config["default"]);
if (instance != "default" && config[instance])
load_instance(config[instance]);
}
catch (std::exception& e) {
m_logger.warning() << "Couldn't load configuration file " << conf_file
<< " - " << e.what() << ". Using default configuration.";
}
}
void DDP::ConfigFile::ready_read()
{
}
void DDP::ConfigFile::error()
{
throw std::runtime_error("Server management socket failed!");
cfg.tcp_ct_timeout.add_value(node["tcp-table"]["timeout"].as<uint64_t>());
}
void DDP::ConfigFile::hup()
{
throw std::runtime_error("Server management HANG UP!");
}
......@@ -23,52 +23,15 @@
#pragma once
#include <iostream>
#include <memory>
#include <unistd.h>
#include <string>
#include <yaml-cpp/yaml.h>
#include "ConfigItem.h"
#include "Config.h"
#include "core/Statistics.h"
#include "utils/Poll.h"
#include "utils/FileDescriptor.h"
#include "utils/Logger.h"
namespace DDP {
class ConfigFile : public PollAble
struct ConfigFile
{
public:
explicit ConfigFile(Config& cfg, std::string conf_file, std::string instance = "default");
~ConfigFile() override = default;
/**
* Process request from config file when associated file descriptor is ready to read.
*/
void ready_read() override;
/**
* When connection between application and config file is broken process the error.
*/
void error() override;
/**
* Process closed connection from config file.
*/
void hup() override;
/**
* Provides access to underlying file descriptor.
* @return Associated file descriptor.
*/
int fd() override { return m_fd; }
private:
void load_instance(YAML::Node node);
Config& m_cfg; //!< Associated config.
Logger m_logger; //!< Logger for logging events.
int m_fd;
static void load_configuration(Config& cfg, std::string conf_file, std::string instance = "default");
static void load_instance(Config& cfg, YAML::Node node);
};
}
}
\ No newline at end of file
......@@ -84,7 +84,7 @@ namespace DDP {
DDP::Probe::Probe() : m_cfg_loaded(false), m_initialized(false), m_running(false), m_poll(), m_cfg(),
m_cfgfile(nullptr), m_aggregated_timer(nullptr), m_output_timer(nullptr), m_comm_links(),
m_aggregated_timer(nullptr), m_output_timer(nullptr), m_comm_links(),
m_log_link(), m_dns_record_mempool(), m_export_rings(), m_factory_rings(), m_stats(),
m_stopped_workers(0), m_ret_value(ReturnValue::STOP) {}
......@@ -175,7 +175,7 @@ void DDP::Probe::load_config(Arguments& args)
try {
// Init configuration
m_cfgfile = &m_poll.emplace<DDP::ConfigFile>(m_cfg, args.conf_file, args.instance_name);
ConfigFile::load_configuration(m_cfg, args.conf_file, args.instance_name);
if (args.raw_pcap)
m_cfg.raw_pcap.add_value(args.raw_pcap);
......
......@@ -42,7 +42,6 @@
namespace DDP {
class TimerInterface;
class Port;
class ConfigFile;
class CommLinkProxy;
/**
......@@ -184,7 +183,6 @@ namespace DDP {
Poll m_poll; //!< Backend for main loop.
Config m_cfg; //!< Application configuration.
ConfigFile* m_cfgfile;
TimerInterface* m_aggregated_timer; //!< Timer for automatic aggregating statistics and calculating qps.
TimerInterface* m_output_timer; //!< Timer for automatic rotation of output files
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment