159 lines
4.7 KiB
C++
159 lines
4.7 KiB
C++
#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
|