94 lines
2.8 KiB
C++
94 lines
2.8 KiB
C++
#include <Void/DiscoveryRpc/EndpointAdapter.hpp>
|
|
#include <Void/RPC/Rpc.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
|
|
Void::Testing::Test discovery_rpc_identity_preserving_refresh_and_roundtrip()
|
|
{
|
|
using namespace Void::Discovery;
|
|
using namespace Void::DiscoveryRpc;
|
|
|
|
Void::RPC::Server server{};
|
|
static constexpr Void::RPC::MethodId kEchoMethod{100U, 1U};
|
|
auto reg = server.bind(
|
|
Void::RPC::MethodDescriptor{
|
|
.id = kEchoMethod,
|
|
.service_name = "echo",
|
|
.method_name = "ping",
|
|
.schema_version = 1,
|
|
},
|
|
[](const Void::RPC::CallContext& ctx, Void::RPC::OwnedBytes req)
|
|
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
|
std::string auth_seen;
|
|
for (const auto& m : ctx.metadata) {
|
|
if (m.key == "void-expected-authority") {
|
|
auth_seen = m.value;
|
|
}
|
|
}
|
|
Void::RPC::OwnedBytes out = std::move(req);
|
|
for (char c : auth_seen) {
|
|
out.push_back(static_cast<std::byte>(c));
|
|
}
|
|
return out;
|
|
});
|
|
Void::Testing::require(reg.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
Catalog catalog{};
|
|
EndpointRecord rec{};
|
|
rec.service = "echo";
|
|
rec.instance = "echo-1";
|
|
rec.endpoint.scheme = "http";
|
|
rec.endpoint.host = "127.0.0.1";
|
|
rec.endpoint.port = server.bound_port();
|
|
rec.endpoint.expected_tls_name = "echo.internal";
|
|
rec.weight = 1;
|
|
Void::Testing::require(catalog.replace_static({rec}).has_value());
|
|
|
|
EndpointResolverClient client{
|
|
catalog,
|
|
EndpointResolverOptions{
|
|
.service = "echo",
|
|
.expected_tls_name = "echo.internal",
|
|
.require_tls = false,
|
|
},
|
|
};
|
|
|
|
Void::RPC::OwnedBytes payload{std::byte{'o'}, std::byte{'k'},
|
|
std::byte{':'}};
|
|
auto resp = client.call(kEchoMethod, std::move(payload));
|
|
Void::Testing::require(resp.has_value());
|
|
const std::string reply{
|
|
reinterpret_cast<const char*>(resp->data()), resp->size()};
|
|
Void::Testing::expect_eq(reply, std::string{"ok:echo.internal"});
|
|
|
|
// TLS enforcement rejects non-HTTPS downgrade and mismatched identity.
|
|
EndpointResolverClient tls_client{
|
|
catalog,
|
|
EndpointResolverOptions{
|
|
.service = "echo",
|
|
.expected_tls_name = "echo.internal",
|
|
.require_tls = true,
|
|
},
|
|
};
|
|
auto downgrade = tls_client.select_endpoint();
|
|
Void::Testing::expect(!downgrade.has_value());
|
|
Void::Testing::expect_eq(
|
|
downgrade.error().code().value,
|
|
static_cast<std::int32_t>(DiscoveryErrorCode::TlsIdentityMismatch));
|
|
|
|
// Identity mismatch on HTTPS refresh also fails closed.
|
|
rec.endpoint.scheme = "https";
|
|
rec.endpoint.expected_tls_name = "spoofed.evil";
|
|
Void::Testing::require(catalog.replace_static({rec}).has_value());
|
|
auto mismatch = tls_client.select_endpoint();
|
|
Void::Testing::expect(!mismatch.has_value());
|
|
Void::Testing::expect_eq(
|
|
mismatch.error().code().value,
|
|
static_cast<std::int32_t>(DiscoveryErrorCode::TlsIdentityMismatch));
|
|
|
|
server.shutdown();
|
|
}
|