ReloadableApplications: authenticated bounded control endpoint
This commit is contained in:
1 parent
92b30d2b14
commit
30ba0159ee
4 files changed
+419
No files matched your search
@@ -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