Skip to content
API Documentation
BastionGuard™
bsc::config Namespace Reference

Namespaces

namespace  anonymous_namespace{Config.cpp}

Classes

struct  Config

Typedefs

using json = nlohmann::json

Functions

Config load_file (const std::string &path)
bool save_file (const Config &cfg, const std::string &path)
std::string read_file (const std::string &path)

Typedef Documentation

◆ json

using bsc::config::json = nlohmann::json

Definition at line 11 of file Config.cpp.

Function Documentation

◆ load_file()

Config bsc::config::load_file ( const std::string & path)

Definition at line 51 of file Config.cpp.

51 {
52 Config cfg;
53 cfg.source_path = expand_path(path);
54 auto raw = read_file(cfg.source_path);
55 auto j = json::parse(raw);
56 cfg.log_level = j.value("LogLevel", cfg.log_level);
57 cfg.firewall = j.value("Firewall", cfg.firewall);
58 cfg.default_action = j.value("DefaultAction", cfg.default_action);
59 cfg.default_duration = j.value("DefaultDuration", cfg.default_duration);
60 cfg.proc_monitor_method = j.value("ProcMonitorMethod", cfg.proc_monitor_method);
61 cfg.ebpf_obj_path = j.value("EBPFObjPath", cfg.ebpf_obj_path);
62 cfg.intercept_unknown = j.value("InterceptUnknown", cfg.intercept_unknown);
63 cfg.log_utc = j.value("LogUTC", cfg.log_utc);
64 cfg.log_micro = j.value("LogMicro", cfg.log_micro);
65 if (j.contains("Server")) {
66 const auto& s = j["Server"];
67 cfg.server.address = expand_path(s.value("Address", cfg.server.address));
68 cfg.server.log_file = expand_path(s.value("LogFile", cfg.server.log_file));
69 if (s.contains("Authentication")) {
70 const auto& a = s["Authentication"];
71 cfg.server.authentication.type = a.value("Type", cfg.server.authentication.type);
72 }
73 }
74 if (j.contains("Rules")) {
75 const auto& r = j["Rules"];
76 cfg.rules.path = expand_path(r.value("Path", cfg.rules.path));
77 cfg.rules.enable_checksums = r.value("EnableChecksums", cfg.rules.enable_checksums);
78 }
79 if (j.contains("FwOptions")) {
80 const auto& f = j["FwOptions"];
81 cfg.fw_options.config_path = expand_path(f.value("ConfigPath", cfg.fw_options.config_path));
82 cfg.fw_options.monitor_interval = f.value("MonitorInterval", cfg.fw_options.monitor_interval);
83 cfg.fw_options.queue_num = f.value("QueueNum", cfg.fw_options.queue_num);
84 cfg.fw_options.queue_bypass = f.value("QueueBypass", cfg.fw_options.queue_bypass);
85 }
86 if (j.contains("Stats")) {
87 const auto& s = j["Stats"];
88 cfg.stats.max_events = s.value("MaxEvents", cfg.stats.max_events);
89 cfg.stats.max_stats = s.value("MaxStats", cfg.stats.max_stats);
90 cfg.stats.workers = s.value("Workers", cfg.stats.workers);
91 }
92 if (j.contains("Internal")) {
93 const auto& i = j["Internal"];
94 cfg.internal.gc_percent = i.value("GCPercent", cfg.internal.gc_percent);
95 cfg.internal.flush_conns_on_start = i.value("FlushConnsOnStart", cfg.internal.flush_conns_on_start);
96 }
97 if (j.contains("Tasks")) {
98 const auto& t = j["Tasks"];
99 cfg.tasks.config_path = expand_path(t.value("ConfigPath", cfg.tasks.config_path));
100 }
101 return cfg;
102}
std::string expand_path(std::string path)
Definition Config.cpp:14
std::string read_file(const std::string &path)
Definition Config.cpp:43
StatsOptions stats
Definition Config.hpp:60
RulesOptions rules
Definition Config.hpp:58
std::string default_duration
Definition Config.hpp:53
std::string proc_monitor_method
Definition Config.hpp:54
std::string firewall
Definition Config.hpp:51
InternalOptions internal
Definition Config.hpp:59
std::int32_t log_level
Definition Config.hpp:50
std::string ebpf_obj_path
Definition Config.hpp:55
FwOptions fw_options
Definition Config.hpp:56
TasksOptions tasks
Definition Config.hpp:61
std::string source_path
Definition Config.hpp:49
ServerConfig server
Definition Config.hpp:57
std::string default_action
Definition Config.hpp:52

References bsc::config::Config::ServerConfig::address, bsc::config::Config::ServerConfig::authentication, bsc::config::Config::FwOptions::config_path, bsc::config::Config::TasksOptions::config_path, bsc::config::Config::default_action, bsc::config::Config::default_duration, bsc::config::Config::ebpf_obj_path, bsc::config::Config::RulesOptions::enable_checksums, bsc::config::Config::firewall, bsc::config::Config::InternalOptions::flush_conns_on_start, bsc::config::Config::fw_options, bsc::config::Config::InternalOptions::gc_percent, bsc::config::Config::intercept_unknown, bsc::config::Config::internal, load_file(), bsc::config::Config::ServerConfig::log_file, bsc::config::Config::log_level, bsc::config::Config::log_micro, bsc::config::Config::log_utc, bsc::config::Config::StatsOptions::max_events, bsc::config::Config::StatsOptions::max_stats, bsc::config::Config::FwOptions::monitor_interval, bsc::config::Config::RulesOptions::path, bsc::config::Config::proc_monitor_method, bsc::config::Config::FwOptions::queue_bypass, bsc::config::Config::FwOptions::queue_num, read_file(), bsc::config::Config::rules, bsc::config::Config::server, bsc::config::Config::source_path, bsc::config::Config::stats, bsc::config::Config::tasks, bsc::config::Config::ServerAuth::type, and bsc::config::Config::StatsOptions::workers.

Referenced by load_file(), and main().

◆ read_file()

std::string bsc::config::read_file ( const std::string & path)

Definition at line 43 of file Config.cpp.

43 {
44 std::ifstream in(path);
45 if (!in) throw std::runtime_error("cannot open config file: " + path);
46 std::ostringstream ss;
47 ss << in.rdbuf();
48 return ss.str();
49}

References read_file().

Referenced by load_file(), and read_file().

◆ save_file()

bool bsc::config::save_file ( const Config & cfg,
const std::string & path )

Definition at line 104 of file Config.cpp.

104 {
105 const auto out_path = expand_path(path);
106 std::filesystem::create_directories(std::filesystem::path(out_path).parent_path());
107 std::ofstream out(out_path);
108 if (!out) return false;
109 out << std::setw(2) << to_json(cfg) << "\n";
110 return true;
111}

References save_file().

Referenced by bsc::service::DaemonService::handle_notification(), and save_file().