NetworkedApplications: auth-scoped operation outcome lookup endpoint (P-NETWORKED-03)

This commit is contained in:
faumaray committed 2026-12-04 10:17:19 +03:00
1 parent 50595bdabc
commit 0f1cf89cfc
10 files changed
+175 -1

No files matched your search

@@ -14,6 +14,7 @@ namespace NetworkedKv
constexpr std::string_view kJsonType{"application/json"};
constexpr std::string_view kTextType{"text/plain; charset=utf-8"};
constexpr std::string_view kKvPrefix{"/v1/kv/"};
constexpr std::string_view kOperationsPrefix{"/v1/operations/"};
[[nodiscard]] bool iequals(std::string_view lhs,
std::string_view rhs) noexcept
@@ -227,7 +228,8 @@ namespace NetworkedKv
const auto path = path_of(request.target);
HttpResponse response;
if (path.starts_with(kKvPrefix) || path == "/v1/scan") {
if (path.starts_with(kKvPrefix) || path == "/v1/scan" ||
path.starts_with(kOperationsPrefix)) {
if (!authorized(request)) {
++m_metrics.unauthorized;
response = error_response(401, "missing or invalid bearer token");
@@ -235,7 +237,12 @@ namespace NetworkedKv
response = request.method == "GET"
? kv_scan(request)
: error_response(405, "method not allowed");
} else if (path.starts_with(kOperationsPrefix)) {
response = request.method == "GET"
? kv_operation(path.substr(kOperationsPrefix.size()))
: error_response(405, "method not allowed");
} else {
const auto key = path.substr(kKvPrefix.size());
if (key.empty()) {
response = error_response(400, "key required");
@@ -407,6 +414,40 @@ namespace NetworkedKv
return response;
}
HttpResponse KvApi::kv_operation(std::string_view request_id)
{
HttpResponse response;
response.status = 200;
response.headers["content-type"] = std::string{kJsonType};
response.headers["cache-control"] = "no-store";
if (request_id.empty()) {
return error_response(400, "operation id required");
}
auto recorded = m_store.lookup_operation(RequestId{std::string{request_id}});
if (!recorded) {
return error_response(status_for(recorded.error()),
recorded.error().message());
}
if (!recorded->has_value()) {
// Absence or expired retention is explicitly unknown, never proof
// that the write did not commit.
response.body = "{\"outcome\":\"unknown\"}";
return response;
}
std::string_view outcome;
switch ((*recorded)->outcome) {
case WriteOutcome::Applied: outcome = "applied"; break;
case WriteOutcome::Conflict: outcome = "conflict"; break;
case WriteOutcome::NotFound: outcome = "not_found"; break;
case WriteOutcome::Unknown: outcome = "unknown"; break;
}
response.body = "{\"outcome\":\"" + std::string{outcome} +
"\",\"version\":" +
std::to_string((*recorded)->version) + "}";
return response;
}
HttpResponse KvApi::admin(const HttpRequest& request) const
{
const auto path = path_of(request.target);