blob: db6b33da92d8c6081d78fed2d0b52bc297ddd823 [file] [log] [blame]
Václav Kubernátd386aba2021-01-19 10:03:28 +01001#include <boost/process.hpp>
2#include <docopt.h>
3#include <spdlog/sinks/ansicolor_sink.h>
4#include <spdlog/spdlog.h>
5#include <sysrepo-cpp/Session.hpp>
6#include <unistd.h>
7#include "VELIA_VERSION.h"
8#include "firewall/Firewall.h"
9#include "utils/exceptions.h"
10#include "utils/journal.h"
11#include "utils/log-init.h"
12#include "utils/waitUntilSignalled.h"
13
14/** @short Extract log level from a CLI option */
15spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
16{
17 long x;
18 try {
19 x = option.asLong();
20 } catch (std::invalid_argument&) {
21 throw std::runtime_error(name + " log level: expecting integer");
22 }
23 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
24 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
25 if (x < 0 || x > 5)
26 throw std::runtime_error(name + " log level invalid or out-of-range");
27
28 return static_cast<spdlog::level::level_enum>(5 - x);
29}
30
31static const char usage[] =
32 R"(Bridge between sysrepo and nftables.
33
34Usage:
35 veliad-firewall
36 [--sysrepo-log-level=<Level>]
37 [--firewall-log-level=<Level>]
38 veliad-firewall (-h | --help)
39 veliad-firewall --version
40
41Options:
42 -h --help Show this screen.
43 --version Show version.
44 --firewall-log-level=<N> Log level for the firewall [default: 3]
45 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
46)";
47
48int main(int argc, char* argv[])
49{
50 std::shared_ptr<spdlog::sinks::sink> loggingSink;
51 if (velia::utils::isJournaldActive()) {
52 loggingSink = velia::utils::create_journald_sink();
53 } else {
54 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
55 }
56
Tomáš Peckac50ec9e2021-01-27 12:22:42 +010057 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-firewall " VELIA_VERSION, true);
Václav Kubernátd386aba2021-01-19 10:03:28 +010058
59 velia::utils::initLogs(loggingSink);
60 spdlog::set_level(spdlog::level::info);
61
62 try {
63 spdlog::get("firewall")->set_level(parseLogLevel("Firewall logging", args["--firewall-log-level"]));
64 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
65
66 spdlog::get("main")->debug("Opening Sysrepo connection");
67 auto srConn = std::make_shared<sysrepo::Connection>();
68 auto srSess = std::make_shared<sysrepo::Session>(srConn);
69 velia::firewall::SysrepoFirewall firewall(srSess, [] (const auto& config) {
70 spdlog::get("firewall")->debug("running nft...");
71 namespace bp = boost::process;
72 bp::pipe stdinPipe;
73 bp::ipstream stderrStream;
74
75 bp::child c(bp::search_path("nft"), "-f" , "-", bp::std_in < stdinPipe, bp::std_err > stderrStream);
76 spdlog::get("firewall")->trace("nft started");
77
78 stdinPipe.write(config.c_str(), config.size());
79 // I immediately close the pipe so that nft stops reading.
80 stdinPipe.close();
81
82 c.wait();
83 spdlog::get("firewall")->trace("nft exited");
84
85 if (c.exit_code()) {
86 std::istreambuf_iterator<char> begin(stderrStream), end;
87 std::string stderrOutput(begin, end);
88 spdlog::get("firewall")->critical("nft ended with a non-zero exit code. stderr: {}", stderrOutput);
89
90 throw std::runtime_error("nft returned non-zero exit code " + std::to_string(c.exit_code()));
91 }
92 spdlog::get("firewall")->debug("nft config applied.");
93 });
94
95 waitUntilSignaled();
96
97 spdlog::get("main")->info("Exiting.");
98
99 return 0;
100 } catch (std::exception& e) {
101 velia::utils::fatalException(spdlog::get("main"), e, "main");
102 }
103}