NetworkedApplications: HTTP application layer with auth and admin views
This commit is contained in:
1 parent
0ef4e3b9ac
commit
11fea2566a
4 files changed
+707
-1
No files matched your search
@@ -0,0 +1,175 @@
|
||||
#include "networked_kv/http_api.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace NetworkedKv;
|
||||
|
||||
HttpRequest make_request(std::string method, std::string target,
|
||||
std::string body = {})
|
||||
{
|
||||
HttpRequest request;
|
||||
request.method = std::move(method);
|
||||
request.target = std::move(target);
|
||||
request.body = std::move(body);
|
||||
return request;
|
||||
}
|
||||
|
||||
HttpRequest authed(HttpRequest request)
|
||||
{
|
||||
request.headers["authorization"] = "Bearer secret";
|
||||
return request;
|
||||
}
|
||||
|
||||
void bearer_auth_is_required()
|
||||
{
|
||||
MemoryKvStore store;
|
||||
KvApi api{store, ApiConfig{.bearer_token = "secret"}};
|
||||
|
||||
assert(api.handle(make_request("GET", "/v1/kv/a")).status == 401);
|
||||
|
||||
auto wrong = make_request("GET", "/v1/kv/a");
|
||||
wrong.headers["authorization"] = "Bearer wrong";
|
||||
assert(api.handle(wrong).status == 401);
|
||||
assert(api.metrics().unauthorized == 2);
|
||||
|
||||
// Fail closed when no token is configured.
|
||||
KvApi unconfigured{store, ApiConfig{}};
|
||||
assert(unconfigured.handle(authed(make_request("GET", "/v1/kv/a")))
|
||||
.status == 401);
|
||||
}
|
||||
|
||||
void kv_lifecycle_over_http()
|
||||
{
|
||||
MemoryKvStore store;
|
||||
KvApi api{store, ApiConfig{.bearer_token = "secret"}};
|
||||
|
||||
auto put = authed(make_request("PUT", "/v1/kv/a", "one"));
|
||||
put.headers["x-request-id"] = "r1";
|
||||
put.headers["if-none-match"] = "*";
|
||||
auto created = api.handle(put);
|
||||
assert(created.status == 200);
|
||||
assert(created.headers.at("etag") == "\"1\"");
|
||||
|
||||
auto get = authed(make_request("GET", "/v1/kv/a"));
|
||||
auto got = api.handle(get);
|
||||
assert(got.status == 200);
|
||||
assert(got.body == "one");
|
||||
assert(got.headers.at("etag") == "\"1\"");
|
||||
|
||||
auto stale = authed(make_request("PUT", "/v1/kv/a", "two"));
|
||||
stale.headers["x-request-id"] = "r2";
|
||||
stale.headers["if-match"] = "\"9\"";
|
||||
assert(api.handle(stale).status == 412);
|
||||
|
||||
// Replaying the same request id returns the recorded outcome and does
|
||||
// not apply the mutation again.
|
||||
auto replay = authed(make_request("PUT", "/v1/kv/a", "ignored"));
|
||||
replay.headers["x-request-id"] = "r1";
|
||||
replay.headers["if-none-match"] = "*";
|
||||
auto replayed = api.handle(replay);
|
||||
assert(replayed.status == 200);
|
||||
assert(replayed.headers.at("etag") == "\"1\"");
|
||||
assert(api.handle(get).body == "one");
|
||||
|
||||
auto delete_stale = authed(make_request("DELETE", "/v1/kv/a"));
|
||||
delete_stale.headers["x-request-id"] = "r3";
|
||||
delete_stale.headers["if-match"] = "\"9\"";
|
||||
assert(api.handle(delete_stale).status == 412);
|
||||
|
||||
auto remove = authed(make_request("DELETE", "/v1/kv/a"));
|
||||
remove.headers["x-request-id"] = "r4";
|
||||
remove.headers["if-match"] = "\"1\"";
|
||||
assert(api.handle(remove).status == 204);
|
||||
assert(api.handle(get).status == 404);
|
||||
}
|
||||
|
||||
void request_validation()
|
||||
{
|
||||
MemoryKvStore store;
|
||||
KvApi api{store, ApiConfig{.bearer_token = "secret",
|
||||
.max_body_bytes = 4}};
|
||||
|
||||
auto oversized = authed(make_request("PUT", "/v1/kv/a", "12345"));
|
||||
oversized.headers["x-request-id"] = "r";
|
||||
oversized.headers["if-none-match"] = "*";
|
||||
assert(api.handle(oversized).status == 413);
|
||||
|
||||
auto no_id = authed(make_request("PUT", "/v1/kv/a", "x"));
|
||||
no_id.headers["if-none-match"] = "*";
|
||||
assert(api.handle(no_id).status == 428);
|
||||
|
||||
auto no_precondition = authed(make_request("PUT", "/v1/kv/a", "x"));
|
||||
no_precondition.headers["x-request-id"] = "r";
|
||||
assert(api.handle(no_precondition).status == 428);
|
||||
|
||||
assert(api.handle(authed(make_request("POST", "/v1/kv/a")))
|
||||
.status == 405);
|
||||
assert(api.handle(authed(make_request("GET", "/v1/nope"))).status ==
|
||||
404);
|
||||
}
|
||||
|
||||
void scan_is_bounded()
|
||||
{
|
||||
MemoryKvStore store{Limits{.max_scan_page = 2}};
|
||||
KvApi api{store, ApiConfig{.bearer_token = "secret",
|
||||
.max_scan_limit = 2}};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
auto put = authed(make_request(
|
||||
"PUT", "/v1/kv/" + std::string(1, static_cast<char>('a' + i)),
|
||||
"v"));
|
||||
put.headers["x-request-id"] = "s" + std::to_string(i);
|
||||
put.headers["if-none-match"] = "*";
|
||||
assert(api.handle(put).status == 200);
|
||||
}
|
||||
|
||||
auto page = api.handle(authed(make_request("GET", "/v1/scan")));
|
||||
assert(page.status == 200);
|
||||
// Two items retained, and a cursor is offered for the rest.
|
||||
std::size_t keys = 0;
|
||||
for (std::size_t pos = 0;
|
||||
(pos = page.body.find("\"key\":", pos)) != std::string::npos;
|
||||
pos += 6) {
|
||||
++keys;
|
||||
}
|
||||
assert(keys == 2);
|
||||
assert(page.body.find("\"next_cursor\":null") == std::string::npos);
|
||||
|
||||
assert(api.handle(authed(make_request("GET", "/v1/scan?limit=0")))
|
||||
.status == 400);
|
||||
}
|
||||
|
||||
void admin_views_are_bounded()
|
||||
{
|
||||
MemoryKvStore store;
|
||||
KvApi api{store, ApiConfig{.bearer_token = "secret"}};
|
||||
assert(api.admin(make_request("GET", "/live")).status == 200);
|
||||
assert(api.admin(make_request("GET", "/ready")).status == 200);
|
||||
api.set_ready(false);
|
||||
assert(api.admin(make_request("GET", "/ready")).status == 503);
|
||||
|
||||
(void)api.handle(authed(make_request("GET", "/v1/kv/a")));
|
||||
auto metrics = api.admin(make_request("GET", "/metrics"));
|
||||
assert(metrics.status == 200);
|
||||
assert(metrics.body.find("networked_kv_requests_total") !=
|
||||
std::string::npos);
|
||||
// No credential material in the admin output.
|
||||
assert(metrics.body.find("secret") == std::string::npos);
|
||||
assert(api.admin(make_request("POST", "/live")).status == 405);
|
||||
assert(api.admin(make_request("GET", "/unknown")).status == 404);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
bearer_auth_is_required();
|
||||
kv_lifecycle_over_http();
|
||||
request_validation();
|
||||
scan_is_bounded();
|
||||
admin_views_are_bounded();
|
||||
std::puts("networked-kv http api ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user