feat(configuration): bounded schemas, provenance/redaction, and async transactional ReloadController
This commit is contained in:
1 parent
a24e086e47
commit
1dc51d5c43
14 files changed
+3351
No files matched your search
@@ -0,0 +1,256 @@
|
||||
#include <Void/Configuration/Bind.hpp>
|
||||
#include <Void/Configuration/Config.hpp>
|
||||
#include <Void/Configuration/Schema.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace C = Void::Configuration;
|
||||
|
||||
struct StrictServerConfig {
|
||||
[[= C::Required{}]] std::string host{};
|
||||
[[= C::IntRange{1, 65535}]] int port{8080};
|
||||
[[= C::Secret{}]] std::string api_token{};
|
||||
[[= C::RestartRequired{}]] std::string bind_interface{"0.0.0.0"};
|
||||
[[= C::Deprecated{"use tls_min_version"}]] bool legacy_tls{false};
|
||||
std::vector<int> shard_ids{};
|
||||
std::optional<std::string> note{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_schema_precedence_provenance_and_redaction()
|
||||
{
|
||||
C::SourceSet ss;
|
||||
// Passed out of order on purpose: CLI must win over Env, which wins over
|
||||
// File, which wins over Defaults.
|
||||
ss.add_cli_args("cli", {"prog", "--host=cli.internal", "--port", "9090"});
|
||||
ss.add_env_map("env",
|
||||
{{"host", "env.internal"},
|
||||
{"port", "7070"},
|
||||
{"api_token", "super-secret-value-123"}});
|
||||
ss.add_json_text(
|
||||
"file.json",
|
||||
R"({"host":"json.internal","port":6060,"shard_ids":[10,20]})");
|
||||
ss.add_defaults_map("defaults",
|
||||
{{"host", "def.internal"}, {"port", "8080"}});
|
||||
|
||||
auto schema = C::Schema<StrictServerConfig>::from_reflection();
|
||||
auto merged = C::load_sources(ss, schema.limits(), schema.secret_keys());
|
||||
Void::Testing::require(merged.has_value());
|
||||
|
||||
auto res = C::validate<StrictServerConfig>(*merged, schema);
|
||||
Void::Testing::require(res.has_value());
|
||||
Void::Testing::expect_eq(res->value.host, std::string{"cli.internal"});
|
||||
Void::Testing::expect_eq(res->value.port, 9090);
|
||||
Void::Testing::expect_eq(res->value.api_token,
|
||||
std::string{"super-secret-value-123"});
|
||||
Void::Testing::expect_eq(res->value.shard_ids.size(), std::size_t{2});
|
||||
Void::Testing::expect_eq(res->value.shard_ids[0], 10);
|
||||
Void::Testing::expect_eq(res->value.shard_ids[1], 20);
|
||||
|
||||
// Provenance tracks winning origin and shadowed origins with redaction flag.
|
||||
const auto* host_prov = res->provenance->find("host");
|
||||
Void::Testing::require(host_prov != nullptr);
|
||||
Void::Testing::expect_eq(host_prov->winner.source, std::string{"cli"});
|
||||
Void::Testing::expect_eq(host_prov->shadowed.size(), std::size_t{3});
|
||||
|
||||
const auto* tok_prov = res->provenance->find("api_token");
|
||||
Void::Testing::require(tok_prov != nullptr);
|
||||
Void::Testing::expect(tok_prov->redacted);
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_schema_secret_redaction_and_bounds()
|
||||
{
|
||||
auto schema = C::Schema<StrictServerConfig>::from_reflection();
|
||||
|
||||
// Bad secret value (overflow / out-of-range) must redact secret value.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}});
|
||||
// Force a scalar limit violation with secret key.
|
||||
C::ConfigLimits tiny;
|
||||
tiny.max_scalar_bytes = 8;
|
||||
C::SourceSet big_sec;
|
||||
big_sec.add_defaults_map(
|
||||
"d", {{"host", "h"}, {"api_token", "VERY_SECRET_XYZ_TOKEN"}});
|
||||
auto m = C::load_sources(big_sec, tiny, schema.secret_keys());
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect(m.error().message().find("VERY_SECRET_XYZ") ==
|
||||
std::string::npos);
|
||||
Void::Testing::expect(m.error().message().find("[REDACTED]") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
// Out-of-range port (0 or 70000) fails cleanly with range_error.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}, {"port", "70000"}});
|
||||
auto m = C::load_sources(ss, schema.limits(), schema.secret_keys());
|
||||
Void::Testing::require(m.has_value());
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::range_error));
|
||||
}
|
||||
|
||||
// Exceeding max_source_bytes / max_nesting_depth rejected.
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_cfg_limits"};
|
||||
const auto file = tmp.path() / "big.json";
|
||||
std::string junk(256, 'a');
|
||||
Void::Testing::require(
|
||||
Void::FileSystem::write_file(file, "{\"host\":\"" + junk + "\"}")
|
||||
.has_value());
|
||||
C::ConfigLimits small_src;
|
||||
small_src.max_source_bytes = 64;
|
||||
C::SourceSet ss;
|
||||
ss.add_json_file("f", file);
|
||||
auto m = C::load_sources(ss, small_src);
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
m.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::limit_exceeded));
|
||||
}
|
||||
|
||||
{
|
||||
std::string nested;
|
||||
for (int i = 0; i < 40; ++i) nested += "{\"a\":";
|
||||
nested += "1";
|
||||
for (int i = 0; i < 40; ++i) nested += "}";
|
||||
C::SourceSet ss;
|
||||
ss.add_json_text("deep", std::move(nested));
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
m.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::limit_exceeded));
|
||||
}
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_schema_collisions_gaps_null_empty_and_constraints()
|
||||
{
|
||||
auto schema = C::Schema<StrictServerConfig>::from_reflection();
|
||||
schema.add_constraint("no_root_interface_in_prod",
|
||||
[](const StrictServerConfig& cfg)
|
||||
-> Void::Result::Result<void> {
|
||||
if (cfg.host == "prod" && cfg.port < 1024) {
|
||||
return Void::Result::fail(
|
||||
C::make_config_error(
|
||||
C::ConfigErrorCode::constraint_failed,
|
||||
"prod requires non-privileged port"));
|
||||
}
|
||||
return {};
|
||||
});
|
||||
|
||||
// Dotted key vs nested path collision rejected.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_json_text("coll",
|
||||
R"({"a":{"b":"nested"},"a.b":"literal-dot"})");
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
m.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::key_collision));
|
||||
}
|
||||
|
||||
// Unknown key rejected by default.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}, {"typo_key", "1"}});
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::unknown_key));
|
||||
}
|
||||
|
||||
// Vector index gap rejected.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"},
|
||||
{"shard_ids.0", "1"},
|
||||
{"shard_ids.2", "3"}});
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::vector_gap));
|
||||
}
|
||||
|
||||
// Null vs Empty vs Missing vs Removed distinction + no fallback on bad port.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_json_text("j", R"({"host":"","port":null,"note":null})");
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
Void::Testing::expect(m->state_of("host") == C::ValueState::Empty);
|
||||
Void::Testing::expect(m->state_of("port") == C::ValueState::Null);
|
||||
Void::Testing::expect(m->state_of("missing_x") == C::ValueState::Missing);
|
||||
// null on non-optional int fails instead of falling back to 8080.
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
}
|
||||
|
||||
// Removed required field fails required validation.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}});
|
||||
ss.add_removals("r", C::SourceKind::Environment, {"host"});
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
Void::Testing::expect(m->state_of("host") == C::ValueState::Removed);
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::missing_required));
|
||||
}
|
||||
|
||||
// Cross-field constraint + deprecated warning + restart_required diff.
|
||||
{
|
||||
C::SourceSet s1;
|
||||
s1.add_defaults_map(
|
||||
"d", {{"host", "prod"}, {"port", "80"}});
|
||||
auto m1 = C::load_sources(s1);
|
||||
auto v1 = C::validate<StrictServerConfig>(*m1, schema);
|
||||
Void::Testing::expect(!v1.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v1.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::constraint_failed));
|
||||
|
||||
C::SourceSet s2;
|
||||
s2.add_defaults_map("d", {{"host", "prod"},
|
||||
{"port", "8443"},
|
||||
{"legacy_tls", "true"},
|
||||
{"bind_interface", "127.0.0.1"}});
|
||||
C::SourceSet s3;
|
||||
s3.add_defaults_map("d", {{"host", "prod"},
|
||||
{"port", "8443"},
|
||||
{"bind_interface", "10.0.0.1"}});
|
||||
auto m2 = C::load_sources(s2);
|
||||
auto m3 = C::load_sources(s3);
|
||||
auto v2 = C::validate<StrictServerConfig>(*m2, schema);
|
||||
Void::Testing::require(v2.has_value());
|
||||
Void::Testing::expect(!v2->warnings.empty());
|
||||
auto rr = C::detect_restart_required_changes(*m2, *m3, schema);
|
||||
Void::Testing::expect_eq(rr.size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(rr[0], std::string{"bind_interface"});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user