|
@@ -11,7 +11,9 @@
|
|
|
// database client, which is what keeps run_service testable on a host where
|
|
// database client, which is what keeps run_service testable on a host where
|
|
|
// that client cannot compile at all (CLAUDE.md).
|
|
// that client cannot compile at all (CLAUDE.md).
|
|
|
#include "db_gateway.hpp"
|
|
#include "db_gateway.hpp"
|
|
|
|
|
+#include "function_env.hpp"
|
|
|
#include "function_record.hpp"
|
|
#include "function_record.hpp"
|
|
|
|
|
+#include "runner_auth.hpp"
|
|
|
#include "function_registry.hpp"
|
|
#include "function_registry.hpp"
|
|
|
#include "run_service.hpp"
|
|
#include "run_service.hpp"
|
|
|
#include "runner/host_context.hpp"
|
|
#include "runner/host_context.hpp"
|
|
@@ -53,11 +55,14 @@ void onSignal(int) { g_running = false; }
|
|
|
/// in the database, not here.
|
|
/// in the database, not here.
|
|
|
struct RunnerConfig {
|
|
struct RunnerConfig {
|
|
|
std::string logLevel = "info";
|
|
std::string logLevel = "info";
|
|
|
- /// Loopback by default. The runner has no authentication of its own - the
|
|
|
|
|
- /// protocol vectorapi speaks to it carries no credential - so it must not
|
|
|
|
|
- /// be reachable from anywhere an attacker is. Widening this bind address is
|
|
|
|
|
- /// a decision about network placement, and it should be a deliberate one.
|
|
|
|
|
|
|
+ /// Loopback by default: the single-host deployment, where no token is
|
|
|
|
|
+ /// needed because nothing else can reach the port. Binding anywhere else
|
|
|
|
|
+ /// REQUIRES a token - see startupRefusal().
|
|
|
std::string httpBind = "127.0.0.1";
|
|
std::string httpBind = "127.0.0.1";
|
|
|
|
|
+ /// Shared secret vectorapi presents as `Authorization: Bearer`. Prefer
|
|
|
|
|
+ /// SVAPI_RUNNER_TOKEN in runner.env, which can be mode 0640; this config
|
|
|
|
|
+ /// file is a conffile and tends to be world-readable.
|
|
|
|
|
+ std::string authToken;
|
|
|
uint16_t httpPort = 8090;
|
|
uint16_t httpPort = 8090;
|
|
|
std::string dbAddress = "localhost:9004";
|
|
std::string dbAddress = "localhost:9004";
|
|
|
|
|
|
|
@@ -81,6 +86,7 @@ struct RunnerConfig {
|
|
|
c.httpBind = h.value("bind_address", c.httpBind);
|
|
c.httpBind = h.value("bind_address", c.httpBind);
|
|
|
c.httpPort = h.value("port", c.httpPort);
|
|
c.httpPort = h.value("port", c.httpPort);
|
|
|
c.readTimeoutSec = h.value("read_timeout_sec", c.readTimeoutSec);
|
|
c.readTimeoutSec = h.value("read_timeout_sec", c.readTimeoutSec);
|
|
|
|
|
+ c.authToken = h.value("auth_token", c.authToken);
|
|
|
}
|
|
}
|
|
|
if (j.contains("database") && j["database"].is_object())
|
|
if (j.contains("database") && j["database"].is_object())
|
|
|
c.dbAddress = j["database"].value("address", c.dbAddress);
|
|
c.dbAddress = j["database"].value("address", c.dbAddress);
|
|
@@ -146,7 +152,8 @@ private:
|
|
|
/// RunService's ctx seam, over the real HostContext.
|
|
/// RunService's ctx seam, over the real HostContext.
|
|
|
class GatewayHostBindings : public svapi::runner::HostBindingsFactory {
|
|
class GatewayHostBindings : public svapi::runner::HostBindingsFactory {
|
|
|
public:
|
|
public:
|
|
|
- explicit GatewayHostBindings(svapi::DbGateway& db) : db_(db) {}
|
|
|
|
|
|
|
+ GatewayHostBindings(svapi::DbGateway& db, std::vector<std::string> environment)
|
|
|
|
|
+ : db_(db), environment_(std::move(environment)) {}
|
|
|
|
|
|
|
|
bool create(const Request& request, svapi::runner::HostBindingsLease& out,
|
|
bool create(const Request& request, svapi::runner::HostBindingsLease& out,
|
|
|
std::string& error) override {
|
|
std::string& error) override {
|
|
@@ -156,9 +163,11 @@ public:
|
|
|
cfg.policy = request.policy;
|
|
cfg.policy = request.policy;
|
|
|
cfg.trigger = request.trigger;
|
|
cfg.trigger = request.trigger;
|
|
|
cfg.deadlineMs = request.deadlineMs;
|
|
cfg.deadlineMs = request.deadlineMs;
|
|
|
- // cfg.env stays empty: ctx.env is secrets, and nothing has yet decided
|
|
|
|
|
- // where a project's secrets are stored or who may read them. An empty
|
|
|
|
|
- // env is the only answer that cannot leak one.
|
|
|
|
|
|
|
+ // Only this project's secrets, from the runner's own environment
|
|
|
|
|
+ // (systemd loads runner.env). The project is part of each variable's
|
|
|
|
|
+ // name, because one runner serves every project and nothing else would
|
|
|
|
|
+ // keep one tenant's key away from another's script.
|
|
|
|
|
+ cfg.env = svapi::runner::functionEnvFor(request.project, environment_);
|
|
|
try {
|
|
try {
|
|
|
auto host = std::make_unique<svapi::runner::HostContext>(db_, std::move(cfg));
|
|
auto host = std::make_unique<svapi::runner::HostContext>(db_, std::move(cfg));
|
|
|
svapi::runner::HostContext* raw = host.get();
|
|
svapi::runner::HostContext* raw = host.get();
|
|
@@ -181,6 +190,7 @@ public:
|
|
|
|
|
|
|
|
private:
|
|
private:
|
|
|
svapi::DbGateway& db_;
|
|
svapi::DbGateway& db_;
|
|
|
|
|
+ std::vector<std::string> environment_;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/// Parse a request body, or answer 400 with the same error envelope the rest of
|
|
/// Parse a request body, or answer 400 with the same error envelope the rest of
|
|
@@ -251,9 +261,27 @@ int main(int argc, char** argv) {
|
|
|
options.version = svapi::VERSION;
|
|
options.version = svapi::VERSION;
|
|
|
|
|
|
|
|
RegistryFunctionSource source(registry);
|
|
RegistryFunctionSource source(registry);
|
|
|
- GatewayHostBindings host(db);
|
|
|
|
|
|
|
+ // Snapshot the environment once: it does not change under a running
|
|
|
|
|
+ // process, and re-reading it per run would be work for no gain.
|
|
|
|
|
+ GatewayHostBindings host(db, svapi::runner::processEnvironment());
|
|
|
svapi::runner::RunService service(source, options, &host);
|
|
svapi::runner::RunService service(source, options, &host);
|
|
|
|
|
|
|
|
|
|
+ // The environment wins over the config file: a secret belongs in
|
|
|
|
|
+ // runner.env, which systemd can keep at mode 0640, not in a conffile.
|
|
|
|
|
+ std::string authToken = cfg.authToken;
|
|
|
|
|
+ if (const char* fromEnv = std::getenv(svapi::runner::kRunnerTokenEnvVar);
|
|
|
|
|
+ fromEnv != nullptr && *fromEnv != '\0') {
|
|
|
|
|
+ authToken = fromEnv;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (const auto refusal = svapi::runner::startupRefusal(cfg.httpBind, authToken)) {
|
|
|
|
|
+ spdlog::critical("{}", *refusal);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (authToken.empty()) {
|
|
|
|
|
+ spdlog::warn("no runner token set; serving unauthenticated on {} - safe only"
|
|
|
|
|
+ " because nothing off this host can reach it", cfg.httpBind);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
httplib::Server server;
|
|
httplib::Server server;
|
|
|
server.set_read_timeout(cfg.readTimeoutSec, 0);
|
|
server.set_read_timeout(cfg.readTimeoutSec, 0);
|
|
|
server.set_write_timeout(cfg.readTimeoutSec, 0);
|
|
server.set_write_timeout(cfg.readTimeoutSec, 0);
|
|
@@ -264,6 +292,24 @@ int main(int argc, char** argv) {
|
|
|
return new httplib::ThreadPool(static_cast<size_t>(cfg.poolSize) + 2);
|
|
return new httplib::ThreadPool(static_cast<size_t>(cfg.poolSize) + 2);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ // Authenticate everything, /health included. The health check is what
|
|
|
|
|
+ // vectorapi uses to decide a runner is usable, and it reports the pool's
|
|
|
|
|
+ // state - neither is something an unauthenticated caller should see.
|
|
|
|
|
+ if (!authToken.empty()) {
|
|
|
|
|
+ server.set_pre_routing_handler(
|
|
|
|
|
+ [&authToken](const httplib::Request& req, httplib::Response& res) {
|
|
|
|
|
+ const std::string presented =
|
|
|
|
|
+ svapi::runner::bearerToken(req.get_header_value("Authorization"));
|
|
|
|
|
+ if (svapi::runner::tokenMatches(authToken, presented))
|
|
|
|
|
+ return httplib::Server::HandlerResponse::Unhandled;
|
|
|
|
|
+ res.status = 401;
|
|
|
|
|
+ res.set_content(R"({"error":{"code":"unauthorized","message":)"
|
|
|
|
|
+ R"("missing or invalid runner token"}})",
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ return httplib::Server::HandlerResponse::Handled;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
server.Post("/run", [&service](const httplib::Request& req, httplib::Response& res) {
|
|
server.Post("/run", [&service](const httplib::Request& req, httplib::Response& res) {
|
|
|
nlohmann::json body;
|
|
nlohmann::json body;
|
|
|
if (!readJsonBody(req, res, body)) return;
|
|
if (!readJsonBody(req, res, body)) return;
|
|
@@ -293,18 +339,33 @@ int main(int argc, char** argv) {
|
|
|
res.set_content(nlohmann::json{{"ok", false}, {"error", what}}.dump(), "application/json");
|
|
res.set_content(nlohmann::json{{"ok", false}, {"error", what}}.dump(), "application/json");
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // Bind BEFORE notifying, on this thread. listen() binds and serves in one
|
|
|
|
|
+ // call, so notifying after merely spawning the thread would tell systemd we
|
|
|
|
|
+ // are ready while the bind was still pending - or had already failed. Under
|
|
|
|
|
+ // Type=notify that makes a runner that is about to exit look started, and
|
|
|
|
|
+ // anything ordered after it would briefly see success.
|
|
|
|
|
+ if (!server.bind_to_port(cfg.httpBind, cfg.httpPort)) {
|
|
|
|
|
+ spdlog::critical("cannot bind {}:{} - address in use, or not permitted", cfg.httpBind,
|
|
|
|
|
+ cfg.httpPort);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ std::atomic<bool> serveFailed{false};
|
|
|
std::thread http([&] {
|
|
std::thread http([&] {
|
|
|
- spdlog::info("runner listening on {}:{} (pool {}, wall limit {}ms)", cfg.httpBind,
|
|
|
|
|
- cfg.httpPort, cfg.poolSize, cfg.maxWallTimeMs);
|
|
|
|
|
- if (!server.listen(cfg.httpBind, cfg.httpPort)) {
|
|
|
|
|
- spdlog::error("bind failed");
|
|
|
|
|
|
|
+ if (!server.listen_after_bind()) {
|
|
|
|
|
+ spdlog::error("the listener stopped unexpectedly");
|
|
|
|
|
+ serveFailed = true;
|
|
|
g_running = false;
|
|
g_running = false;
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
+ spdlog::info("runner listening on {}:{} (pool {}, wall limit {}ms)", cfg.httpBind, cfg.httpPort,
|
|
|
|
|
+ cfg.poolSize, cfg.maxWallTimeMs);
|
|
|
sd_notify(0, "READY=1");
|
|
sd_notify(0, "READY=1");
|
|
|
while (g_running) { sd_notify(0, "WATCHDOG=1"); std::this_thread::sleep_for(std::chrono::milliseconds(200)); }
|
|
while (g_running) { sd_notify(0, "WATCHDOG=1"); std::this_thread::sleep_for(std::chrono::milliseconds(200)); }
|
|
|
sd_notify(0, "STOPPING=1");
|
|
sd_notify(0, "STOPPING=1");
|
|
|
server.stop();
|
|
server.stop();
|
|
|
if (http.joinable()) http.join();
|
|
if (http.joinable()) http.join();
|
|
|
- return 0;
|
|
|
|
|
|
|
+ // Non-zero on a listener failure, so Restart=on-failure actually fires. A
|
|
|
|
|
+ // signal-driven shutdown is a clean exit.
|
|
|
|
|
+ return serveFailed ? 1 : 0;
|
|
|
}
|
|
}
|