ReloadableApplications: authenticated bounded control endpoint
This commit is contained in:
1 parent
92b30d2b14
commit
30ba0159ee
4 files changed
+419
No files matched your search
@@ -8,6 +8,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Functional)
|
||||
|
||||
add_library(reloadable_host STATIC
|
||||
src/control.cpp
|
||||
src/release_registry.cpp
|
||||
src/reload_transaction.cpp)
|
||||
target_include_directories(reloadable_host PUBLIC include)
|
||||
@@ -19,6 +20,10 @@ target_link_libraries(reloadable_host_test PRIVATE reloadable_host)
|
||||
add_executable(reloadable_host_release_test tests/release_registry_test.cpp)
|
||||
target_link_libraries(reloadable_host_release_test PRIVATE reloadable_host)
|
||||
|
||||
add_executable(reloadable_host_control_test tests/control_test.cpp)
|
||||
target_link_libraries(reloadable_host_control_test PRIVATE reloadable_host)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME reloadable_host_transaction COMMAND reloadable_host_test)
|
||||
add_test(NAME reloadable_host_release COMMAND reloadable_host_release_test)
|
||||
add_test(NAME reloadable_host_control COMMAND reloadable_host_control_test)
|
||||
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace ReloadableHost
|
||||
{
|
||||
enum class ControlStatus : std::uint16_t {
|
||||
Ok = 0,
|
||||
Unauthenticated = 1,
|
||||
InvalidArgument = 2,
|
||||
NotFound = 3,
|
||||
FailedPrecondition = 4,
|
||||
Overloaded = 5,
|
||||
Timeout = 6,
|
||||
Internal = 7,
|
||||
Unimplemented = 8
|
||||
};
|
||||
|
||||
struct ControlRequest {
|
||||
std::string command;
|
||||
std::string candidate;
|
||||
std::string token;
|
||||
std::chrono::milliseconds deadline{0};
|
||||
std::string payload;
|
||||
};
|
||||
|
||||
struct ControlReply {
|
||||
ControlStatus status{ControlStatus::Ok};
|
||||
std::string body;
|
||||
std::uint64_t transaction_id{0};
|
||||
};
|
||||
|
||||
struct ControlLimits {
|
||||
std::size_t max_request_bytes{64ULL * 1024ULL};
|
||||
std::size_t max_reply_bytes{64ULL * 1024ULL};
|
||||
};
|
||||
|
||||
/// Host operations exposed over the authenticated local control endpoint.
|
||||
class ControlHandler
|
||||
{
|
||||
public:
|
||||
|
||||
ControlHandler() = default;
|
||||
ControlHandler(const ControlHandler&) = delete;
|
||||
ControlHandler& operator=(const ControlHandler&) = delete;
|
||||
virtual ~ControlHandler() = default;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<std::string> inspect() = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<bool> health() const = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<void>
|
||||
validate_candidate(std::string_view candidate) = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<std::uint64_t>
|
||||
reload(std::string_view candidate,
|
||||
std::chrono::milliseconds deadline) = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<void>
|
||||
drain(std::chrono::milliseconds deadline) = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<void> shutdown() = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<void> recover() = 0;
|
||||
};
|
||||
|
||||
/// Authenticated, bounded control dispatcher.
|
||||
///
|
||||
/// The transport (local IPC) owns framing; this validates the token in
|
||||
/// constant time, enforces request/reply bounds, and maps host errors to
|
||||
/// stable statuses. An empty configured token authorizes nothing.
|
||||
class ControlEndpoint
|
||||
{
|
||||
public:
|
||||
|
||||
ControlEndpoint(std::string token, ControlHandler& handler,
|
||||
ControlLimits limits = {});
|
||||
|
||||
[[nodiscard]] ControlReply handle(const ControlRequest& request);
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] bool authorized(std::string_view token) const noexcept;
|
||||
[[nodiscard]] ControlReply dispatch(const ControlRequest& request);
|
||||
|
||||
std::string m_token;
|
||||
ControlHandler& m_handler;
|
||||
ControlLimits m_limits;
|
||||
};
|
||||
} // namespace ReloadableHost
|
||||
@@ -0,0 +1,158 @@
|
||||
#include "reloadable_host/control.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace ReloadableHost
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] ControlStatus status_for(const Void::Result::Error& error) noexcept
|
||||
{
|
||||
using Kind = Void::Result::ErrorKind;
|
||||
switch (error.kind()) {
|
||||
case Kind::invalid_argument: return ControlStatus::InvalidArgument;
|
||||
case Kind::not_found: return ControlStatus::NotFound;
|
||||
case Kind::conflict: return ControlStatus::FailedPrecondition;
|
||||
case Kind::exhausted:
|
||||
case Kind::overloaded: return ControlStatus::Overloaded;
|
||||
case Kind::timeout: return ControlStatus::Timeout;
|
||||
case Kind::unsupported: return ControlStatus::Unimplemented;
|
||||
default: return ControlStatus::Internal;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool constant_time_equal(std::string_view lhs,
|
||||
std::string_view rhs) noexcept
|
||||
{
|
||||
if (lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
unsigned int diff = 0;
|
||||
for (std::size_t i = 0; i < lhs.size(); ++i) {
|
||||
diff |= static_cast<unsigned int>(
|
||||
static_cast<unsigned char>(lhs[i]) ^
|
||||
static_cast<unsigned char>(rhs[i]));
|
||||
}
|
||||
return diff == 0;
|
||||
}
|
||||
|
||||
void truncate(std::string& body, std::size_t limit)
|
||||
{
|
||||
if (body.size() > limit) {
|
||||
body.resize(limit);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ControlEndpoint::ControlEndpoint(std::string token, ControlHandler& handler,
|
||||
ControlLimits limits)
|
||||
: m_token(std::move(token))
|
||||
, m_handler(handler)
|
||||
, m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
bool ControlEndpoint::authorized(std::string_view token) const noexcept
|
||||
{
|
||||
if (m_token.empty()) {
|
||||
return false;
|
||||
}
|
||||
return constant_time_equal(token, m_token);
|
||||
}
|
||||
|
||||
ControlReply ControlEndpoint::handle(const ControlRequest& request)
|
||||
{
|
||||
const std::size_t request_bytes = request.command.size() +
|
||||
request.candidate.size() +
|
||||
request.token.size() +
|
||||
request.payload.size();
|
||||
if (request_bytes > m_limits.max_request_bytes) {
|
||||
return ControlReply{ControlStatus::InvalidArgument,
|
||||
"control request exceeds the size limit"};
|
||||
}
|
||||
if (!authorized(request.token)) {
|
||||
return ControlReply{ControlStatus::Unauthenticated,
|
||||
"missing or invalid control token"};
|
||||
}
|
||||
ControlReply reply = dispatch(request);
|
||||
truncate(reply.body, m_limits.max_reply_bytes);
|
||||
return reply;
|
||||
}
|
||||
|
||||
ControlReply ControlEndpoint::dispatch(const ControlRequest& request)
|
||||
{
|
||||
if (request.command == "inspect") {
|
||||
auto inspected = m_handler.inspect();
|
||||
if (!inspected) {
|
||||
return ControlReply{status_for(inspected.error()),
|
||||
inspected.error().message()};
|
||||
}
|
||||
return ControlReply{ControlStatus::Ok, std::move(*inspected)};
|
||||
}
|
||||
if (request.command == "health") {
|
||||
auto healthy = m_handler.health();
|
||||
if (!healthy) {
|
||||
return ControlReply{status_for(healthy.error()),
|
||||
healthy.error().message()};
|
||||
}
|
||||
return ControlReply{ControlStatus::Ok,
|
||||
*healthy ? "ready" : "not ready"};
|
||||
}
|
||||
if (request.command == "validate-candidate") {
|
||||
if (request.candidate.empty()) {
|
||||
return ControlReply{ControlStatus::InvalidArgument,
|
||||
"candidate release id required"};
|
||||
}
|
||||
auto validated =
|
||||
m_handler.validate_candidate(request.candidate);
|
||||
if (!validated) {
|
||||
return ControlReply{status_for(validated.error()),
|
||||
validated.error().message()};
|
||||
}
|
||||
return ControlReply{ControlStatus::Ok, "candidate valid"};
|
||||
}
|
||||
if (request.command == "reload") {
|
||||
if (request.candidate.empty()) {
|
||||
return ControlReply{ControlStatus::InvalidArgument,
|
||||
"candidate release id required"};
|
||||
}
|
||||
auto reloaded =
|
||||
m_handler.reload(request.candidate, request.deadline);
|
||||
if (!reloaded) {
|
||||
return ControlReply{status_for(reloaded.error()),
|
||||
reloaded.error().message()};
|
||||
}
|
||||
ControlReply reply{ControlStatus::Ok, "reload accepted"};
|
||||
reply.transaction_id = *reloaded;
|
||||
return reply;
|
||||
}
|
||||
if (request.command == "drain") {
|
||||
auto drained = m_handler.drain(request.deadline);
|
||||
if (!drained) {
|
||||
return ControlReply{status_for(drained.error()),
|
||||
drained.error().message()};
|
||||
}
|
||||
return ControlReply{ControlStatus::Ok, "drained"};
|
||||
}
|
||||
if (request.command == "shutdown") {
|
||||
auto stopped = m_handler.shutdown();
|
||||
if (!stopped) {
|
||||
return ControlReply{status_for(stopped.error()),
|
||||
stopped.error().message()};
|
||||
}
|
||||
return ControlReply{ControlStatus::Ok, "shutting down"};
|
||||
}
|
||||
if (request.command == "recover") {
|
||||
auto recovered = m_handler.recover();
|
||||
if (!recovered) {
|
||||
return ControlReply{status_for(recovered.error()),
|
||||
recovered.error().message()};
|
||||
}
|
||||
return ControlReply{ControlStatus::Ok, "recovered"};
|
||||
}
|
||||
return ControlReply{ControlStatus::InvalidArgument,
|
||||
"unknown control command"};
|
||||
}
|
||||
} // namespace ReloadableHost
|
||||
@@ -0,0 +1,166 @@
|
||||
#include "reloadable_host/control.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace ReloadableHost;
|
||||
|
||||
struct FakeHandler final : ControlHandler {
|
||||
std::string inspected{
|
||||
"generation=2 release=rel-b ready=1 pins=0 phase=Completed"};
|
||||
bool ready{true};
|
||||
int inspect_calls{0};
|
||||
int reload_calls{0};
|
||||
int drain_calls{0};
|
||||
int shutdown_calls{0};
|
||||
int validate_calls{0};
|
||||
int recover_calls{0};
|
||||
std::string last_candidate;
|
||||
std::chrono::milliseconds last_deadline{0};
|
||||
Void::Result::Result<void> drain_result{};
|
||||
|
||||
Void::Result::Result<std::string> inspect() override
|
||||
{
|
||||
++inspect_calls;
|
||||
return inspected;
|
||||
}
|
||||
Void::Result::Result<bool> health() const override { return ready; }
|
||||
Void::Result::Result<void>
|
||||
validate_candidate(std::string_view candidate) override
|
||||
{
|
||||
++validate_calls;
|
||||
last_candidate = std::string{candidate};
|
||||
return {};
|
||||
}
|
||||
Void::Result::Result<std::uint64_t>
|
||||
reload(std::string_view candidate,
|
||||
std::chrono::milliseconds deadline) override
|
||||
{
|
||||
++reload_calls;
|
||||
last_candidate = std::string{candidate};
|
||||
last_deadline = deadline;
|
||||
return 42;
|
||||
}
|
||||
Void::Result::Result<void>
|
||||
drain(std::chrono::milliseconds) override
|
||||
{
|
||||
++drain_calls;
|
||||
return drain_result;
|
||||
}
|
||||
Void::Result::Result<void> shutdown() override
|
||||
{
|
||||
++shutdown_calls;
|
||||
return {};
|
||||
}
|
||||
Void::Result::Result<void> recover() override
|
||||
{
|
||||
++recover_calls;
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
ControlRequest request(std::string command, std::string token = "secret")
|
||||
{
|
||||
ControlRequest out;
|
||||
out.command = std::move(command);
|
||||
out.token = std::move(token);
|
||||
return out;
|
||||
}
|
||||
|
||||
void authentication_fails_closed()
|
||||
{
|
||||
FakeHandler handler;
|
||||
ControlEndpoint endpoint{"secret", handler};
|
||||
assert(endpoint.handle(request("inspect", "")).status ==
|
||||
ControlStatus::Unauthenticated);
|
||||
assert(endpoint.handle(request("inspect", "wrong")).status ==
|
||||
ControlStatus::Unauthenticated);
|
||||
assert(handler.inspect_calls == 0);
|
||||
|
||||
// An empty configured token authorizes nothing.
|
||||
ControlEndpoint unconfigured{"", handler};
|
||||
assert(unconfigured.handle(request("inspect", "")).status ==
|
||||
ControlStatus::Unauthenticated);
|
||||
}
|
||||
|
||||
void commands_dispatch_to_the_host()
|
||||
{
|
||||
FakeHandler handler;
|
||||
ControlEndpoint endpoint{"secret", handler};
|
||||
|
||||
auto inspected = endpoint.handle(request("inspect"));
|
||||
assert(inspected.status == ControlStatus::Ok);
|
||||
assert(inspected.body == handler.inspected);
|
||||
|
||||
auto healthy = endpoint.handle(request("health"));
|
||||
assert(healthy.status == ControlStatus::Ok);
|
||||
assert(healthy.body == "ready");
|
||||
handler.ready = false;
|
||||
assert(endpoint.handle(request("health")).body == "not ready");
|
||||
|
||||
auto reload = request("reload");
|
||||
reload.candidate = "rel-c";
|
||||
reload.deadline = std::chrono::seconds{5};
|
||||
auto reloaded = endpoint.handle(reload);
|
||||
assert(reloaded.status == ControlStatus::Ok);
|
||||
assert(reloaded.transaction_id == 42);
|
||||
assert(handler.last_candidate == "rel-c");
|
||||
assert(handler.last_deadline == std::chrono::seconds{5});
|
||||
|
||||
assert(endpoint.handle(request("reload")).status ==
|
||||
ControlStatus::InvalidArgument);
|
||||
|
||||
auto validate = request("validate-candidate");
|
||||
validate.candidate = "rel-d";
|
||||
assert(endpoint.handle(validate).status == ControlStatus::Ok);
|
||||
assert(handler.last_candidate == "rel-d");
|
||||
|
||||
assert(endpoint.handle(request("drain")).status == ControlStatus::Ok);
|
||||
assert(endpoint.handle(request("shutdown")).status ==
|
||||
ControlStatus::Ok);
|
||||
assert(endpoint.handle(request("recover")).status ==
|
||||
ControlStatus::Ok);
|
||||
assert(handler.shutdown_calls == 1);
|
||||
assert(handler.recover_calls == 1);
|
||||
}
|
||||
|
||||
void bounds_and_error_mapping()
|
||||
{
|
||||
FakeHandler handler;
|
||||
ControlEndpoint bounded{
|
||||
"secret", handler,
|
||||
ControlLimits{.max_request_bytes = 8, .max_reply_bytes = 64}};
|
||||
auto oversized = request("inspect");
|
||||
oversized.payload = "0123456789";
|
||||
assert(bounded.handle(oversized).status ==
|
||||
ControlStatus::InvalidArgument);
|
||||
|
||||
ControlEndpoint small_reply{
|
||||
"secret", handler, ControlLimits{.max_reply_bytes = 4}};
|
||||
assert(small_reply.handle(request("inspect")).body.size() == 4);
|
||||
|
||||
handler.drain_result = std::unexpected{
|
||||
Void::Result::Error{Void::Result::ErrorKind::timeout,
|
||||
"drain timed out"}};
|
||||
ControlEndpoint normal{"secret", handler};
|
||||
assert(normal.handle(request("drain")).status ==
|
||||
ControlStatus::Timeout);
|
||||
|
||||
assert(normal.handle(request("bogus")).status ==
|
||||
ControlStatus::InvalidArgument);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
authentication_fails_closed();
|
||||
commands_dispatch_to_the_host();
|
||||
bounds_and_error_mapping();
|
||||
std::puts("reloadable-host control ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user