From 02475e05ccd1aa15c6a6163834f28bfebbef796f Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 11:47:36 +0100 Subject: RWGPS webhook and such --- server/src/main.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 194 insertions(+), 25 deletions(-) (limited to 'server/src/main.cpp') diff --git a/server/src/main.cpp b/server/src/main.cpp index 3ef45c6..df750d9 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -1,5 +1,6 @@ #include #include // for malloc_trim(3) +#include import std; import routemon; @@ -7,27 +8,79 @@ import routemon; namespace chrono = std::chrono; namespace net = boost::asio; +using namespace std::literals::string_view_literals; + enum class exit_status { + success, failure, bad_usage, }; -auto real_main(std::span args) -> exit_status +struct secure_buf { - auto sink = routemon::log::make_sink(routemon::log::level::info); - auto l = routemon::log::logger{sink}; + void* p; + std::size_t size = 0; - if (args.size() != 2) + secure_buf(secure_buf const&) = delete; + secure_buf(secure_buf&&) = delete; + + auto operator=(secure_buf const&) -> secure_buf& = delete; + auto operator=(secure_buf&&) -> secure_buf& = delete; + + secure_buf(std::size_t size) : p{OPENSSL_malloc(size)} { - l.error( - "Fatal: expected exactly one argument (the configuration file " - "location), got {}", - args.size() - 1); - return exit_status::bad_usage; + if (!this->p) + throw std::bad_alloc{}; } - auto const* config_filename = args[1]; + ~secure_buf() { OPENSSL_clear_free(this->p, this->size); } +}; + +auto ltrim_ws(std::string_view s) -> std::string_view +{ + if (auto i = s.find_first_not_of(" \t\r\n"); i != std::string_view::npos) + s.remove_prefix(i); + return s; +} +auto rtrim_ws(std::string_view s) -> std::string_view +{ + if (auto i = s.find_last_not_of(" \t\r\n"); i != std::string_view::npos) + return s.substr(0, i + 1); + return s; +} +auto trim_ws(std::string_view s) -> std::string_view +{ + return rtrim_ws(ltrim_ws(s)); +} + +// TODO: make own secure string type using the OpenSSL primitives +template +auto read_secret_to_string(std::string const& filename) -> std::string +{ + static_assert(max_size <= 65536); // sounds reasonable enough? + + auto f = std::ifstream{filename, std::ios::binary}; + if (!f) + throw std::system_error{ + errno, std::generic_category(), + std::format("opening secret located at {}", filename) + }; + auto buf_cap = max_size; + auto buf = secure_buf{max_size}; + f.read(static_cast(buf.p), static_cast(buf_cap)); + buf.size = static_cast(f.gcount()); + f.seekg(0, std::ios::end); + if (f.peek() != std::ifstream::traits_type::eof()) + throw std::runtime_error{"secret is bigger than allowed"}; + + return std::string{trim_ws( + std::string_view{static_cast(buf.p), buf.size})}; +} + +auto server_main(routemon::log::logger& l, routemon::config::app config) + -> exit_status +{ auto lgen = routemon::locale::make_generator(); auto default_locale = lgen->generate("en_US.UTF-8"); auto locales = { @@ -39,28 +92,32 @@ auto real_main(std::span args) -> exit_status auto ioc = net::io_context{1 /* concurrency hint */}; - auto config = routemon::config::app{}; + auto dbc = std::shared_ptr{}; try { - config = routemon::config::load_file(config_filename); + dbc = routemon::database::open(config.database.sqlite3_filename); } catch (std::exception const& e) { - l.with("filename", std::string_view{config_filename}) - .error("Failed to load configuration: {}", e.what()); + l.with("filename", config.database.sqlite3_filename) + .error("Failed to open database: {}", e.what()); return exit_status::failure; } - sink->set_level(config.logger.level); - auto dbc = std::shared_ptr{}; + auto rwgps_creds = std::shared_ptr{}; try { - dbc = routemon::database::open(config.database.sqlite3_filename); + rwgps_creds.reset(new routemon::rwgps::credentials{ + // TODO: bad secrets handling. Too little information for proper error + // reporting. + read_secret_to_string<4096>(config.rwgps.api_key_filename), + read_secret_to_string<4096>(config.rwgps.api_secret_filename), + read_secret_to_string<4096>(config.rwgps.auth_token_filename), + }); } catch (std::exception const& e) { - l.with("filename", config.database.sqlite3_filename) - .error("Failed to open database: {}", e.what()); + l.error("Failed to read RWGPS credentials: {}", e.what()); return exit_status::failure; } @@ -94,7 +151,7 @@ auto real_main(std::span args) -> exit_status } } // Processing the feed is by far the most memory-intensive operation - // during the run time of this application (at the moment), the + // during the run time of this application (at the moment); the // resident set will likely never be this big again. So we ask libc // to return as much memory as possible to the OS. malloc_trim(0); @@ -103,9 +160,9 @@ auto real_main(std::span args) -> exit_status chrono::duration_cast(after_load - before_load); l.info("Loading situations finished in {}", dur_load); - auto handler = routemon::api::handler{l, std::move(pub)}; + auto handler = routemon::api::handler{l, dbc, std::move(pub)}; auto http_server = routemon::srv::server{ - l, config.http_server, std::move(lsel), std::move(handler) + l, config.http_server, rwgps_creds, std::move(lsel), std::move(handler), }; http_server.spawn(ioc); ioc.run(); @@ -114,19 +171,131 @@ auto real_main(std::span args) -> exit_status return exit_status::failure; } +auto migrate_main( + routemon::log::logger& l, routemon::config::app config, + std::int64_t target_db_version) -> exit_status +{ + auto m = std::unique_ptr{}; + try + { + m.reset( + new routemon::migrate::migrator{l, config.database.sqlite3_filename}); + } + catch (std::exception const& e) + { + l.with("filename", config.database.sqlite3_filename) + .error("Failed to open database: {}", e.what()); + return exit_status::failure; + } + + try + { + m->migrate_to(target_db_version); + } + catch (std::exception const& e) + { + l.error("Failed to migrate database: {}", e.what()); + return exit_status::failure; + } + + return exit_status::success; +} + +// Arguments not including own name (so argv[1..]). +auto real_main(std::span args) -> exit_status +{ + auto const sink = routemon::log::make_sink(routemon::log::level::info); + auto l = routemon::log::logger{sink}; + + auto load_config = [&](char const* config_filename) + -> std::expected + { + try + { + auto const config = routemon::config::load_file(config_filename); + sink->set_level(config.logger.level); + return config; + } + catch (std::exception const& e) + { + l.with("filename", std::string_view{config_filename}) + .error("Failed to load configuration: {}", e.what()); + return std::unexpected{exit_status::failure}; + } + }; + + if (args.size() <= 0) + { + l.error( + "Fatal: expected at least a subcommand argument (∈ {{migrate, " + "server}}), got {} arguments", + args.size()); + return exit_status::bad_usage; + } + + if (args[0] == "server"sv) + { + args = args.subspan<1>(); + if (args.size() != 1) + { + l.error( + "Fatal: expected exactly one argument (the configuration file " + "location), got {}", + args.size()); + return exit_status::bad_usage; + } + auto mconfig = load_config(args[0]); + if (!mconfig) + return mconfig.error(); + return server_main(l, std::move(*mconfig)); + } + else if (args[0] == "migrate"sv) + { + args = args.subspan<1>(); + if (args.size() != 2) + { + l.error( + "Fatal: expected exactly two arguments (the configuration file " + "location and database version to migrate to), got {}", + args.size()); + return exit_status::bad_usage; + } + auto mconfig = load_config(args[0]); + if (!mconfig) + return mconfig.error(); + auto mtarget_db_version = routemon::util::parse_int(args[1]); + if (!mtarget_db_version || *mtarget_db_version < 0) + { + l.error( + "Fatal: could not parse target database version ({}) as a " + "non-negative integer", + args[1]); + return exit_status::bad_usage; + } + return migrate_main(l, std::move(*mconfig), *mtarget_db_version); + } + else + { + l.error("Fatal: unrecognized subcommand {}", args[0]); + return exit_status::bad_usage; + } +} + auto main(int argc, char* argv[]) -> int { - if (argc < 0) + if (argc < 1) { - std::cout << "Fatal: argument count below zero" << std::endl; + std::cout << "Fatal: argument count below one" << std::endl; return EXIT_FAILURE; } auto res = real_main( std::span{ - const_cast(argv), static_cast(argc) + const_cast(argv + 1), static_cast(argc - 1) }); switch (res) { + case exit_status::success: + return EXIT_SUCCESS; case exit_status::failure: return EXIT_FAILURE; case exit_status::bad_usage: -- cgit v1.3.1