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,766 @@
|
||||
#include <Void/Configuration/Schema.hpp>
|
||||
|
||||
#include <Void/Data/Ini.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <system_error>
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
extern char** environ; // NOLINT
|
||||
#define VOID_SCHEMA_HAS_ENVIRON 1
|
||||
#endif
|
||||
|
||||
namespace Void::Configuration
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
Void::Result::Result<bool> parse_strict_bool(std::string_view raw)
|
||||
{
|
||||
std::string lower;
|
||||
lower.reserve(raw.size());
|
||||
for (const char c : raw) {
|
||||
lower += static_cast<char>(
|
||||
std::tolower(static_cast<unsigned char>(c)));
|
||||
}
|
||||
if (lower == "true" || lower == "1" || lower == "yes" ||
|
||||
lower == "on") {
|
||||
return true;
|
||||
}
|
||||
if (lower == "false" || lower == "0" || lower == "no" ||
|
||||
lower == "off") {
|
||||
return false;
|
||||
}
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch, "expected boolean value"));
|
||||
}
|
||||
|
||||
Void::Result::Result<std::int64_t> parse_strict_i64_or_bytes(
|
||||
std::string_view raw, bool allow_bytes, bool is_secret)
|
||||
{
|
||||
if (raw.empty()) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch, "empty integer value"));
|
||||
}
|
||||
std::int64_t exact = 0;
|
||||
const char* begin = raw.data();
|
||||
const char* end = raw.data() + raw.size();
|
||||
const auto [ptr, ec] = std::from_chars(begin, end, exact);
|
||||
if (ec == std::errc{} && ptr == end) {
|
||||
return exact;
|
||||
}
|
||||
if (ec == std::errc::result_out_of_range) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::range_error,
|
||||
std::string{"signed integer overflow: "} +
|
||||
redact_display(raw, is_secret)));
|
||||
}
|
||||
|
||||
if (allow_bytes) {
|
||||
double num = 0.0;
|
||||
const auto [bptr, bec] = std::from_chars(begin, end, num);
|
||||
if (bec == std::errc{} && bptr != begin && std::isfinite(num) &&
|
||||
num >= 0.0) {
|
||||
std::string_view suffix{bptr, end};
|
||||
while (!suffix.empty() && suffix.front() == ' ') {
|
||||
suffix.remove_prefix(1);
|
||||
}
|
||||
double mult = 0.0;
|
||||
if (suffix == "B" || suffix.empty()) {
|
||||
mult = 1.0;
|
||||
} else if (suffix == "KB") {
|
||||
mult = 1e3;
|
||||
} else if (suffix == "MB") {
|
||||
mult = 1e6;
|
||||
} else if (suffix == "GB") {
|
||||
mult = 1e9;
|
||||
} else if (suffix == "TB") {
|
||||
mult = 1e12;
|
||||
} else if (suffix == "KiB") {
|
||||
mult = 1024.0;
|
||||
} else if (suffix == "MiB") {
|
||||
mult = 1024.0 * 1024.0;
|
||||
} else if (suffix == "GiB") {
|
||||
mult = 1024.0 * 1024.0 * 1024.0;
|
||||
} else if (suffix == "TiB") {
|
||||
mult = 1024.0 * 1024.0 * 1024.0 * 1024.0;
|
||||
}
|
||||
if (mult > 0.0) {
|
||||
const double scaled = num * mult;
|
||||
if (std::isfinite(scaled) &&
|
||||
scaled <=
|
||||
static_cast<double>(
|
||||
std::numeric_limits<std::int64_t>::max())) {
|
||||
return static_cast<std::int64_t>(scaled);
|
||||
}
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::range_error,
|
||||
"byte-size value overflows int64_t"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch,
|
||||
std::string{"not a signed integer or byte size: "} +
|
||||
redact_display(raw, is_secret)));
|
||||
}
|
||||
|
||||
Void::Result::Result<std::uint64_t> parse_strict_u64_or_bytes(
|
||||
std::string_view raw, bool allow_bytes, bool is_secret)
|
||||
{
|
||||
if (raw.empty()) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch,
|
||||
"empty unsigned integer value"));
|
||||
}
|
||||
std::uint64_t exact = 0;
|
||||
const char* begin = raw.data();
|
||||
const char* end = raw.data() + raw.size();
|
||||
const auto [ptr, ec] = std::from_chars(begin, end, exact);
|
||||
if (ec == std::errc{} && ptr == end) {
|
||||
return exact;
|
||||
}
|
||||
if (ec == std::errc::result_out_of_range) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::range_error,
|
||||
std::string{"unsigned integer overflow: "} +
|
||||
redact_display(raw, is_secret)));
|
||||
}
|
||||
|
||||
if (allow_bytes) {
|
||||
double num = 0.0;
|
||||
const auto [bptr, bec] = std::from_chars(begin, end, num);
|
||||
if (bec == std::errc{} && bptr != begin && std::isfinite(num) &&
|
||||
num >= 0.0) {
|
||||
std::string_view suffix{bptr, end};
|
||||
while (!suffix.empty() && suffix.front() == ' ') {
|
||||
suffix.remove_prefix(1);
|
||||
}
|
||||
double mult = 0.0;
|
||||
if (suffix == "B" || suffix.empty()) {
|
||||
mult = 1.0;
|
||||
} else if (suffix == "KB") {
|
||||
mult = 1e3;
|
||||
} else if (suffix == "MB") {
|
||||
mult = 1e6;
|
||||
} else if (suffix == "GB") {
|
||||
mult = 1e9;
|
||||
} else if (suffix == "TB") {
|
||||
mult = 1e12;
|
||||
} else if (suffix == "KiB") {
|
||||
mult = 1024.0;
|
||||
} else if (suffix == "MiB") {
|
||||
mult = 1024.0 * 1024.0;
|
||||
} else if (suffix == "GiB") {
|
||||
mult = 1024.0 * 1024.0 * 1024.0;
|
||||
} else if (suffix == "TiB") {
|
||||
mult = 1024.0 * 1024.0 * 1024.0 * 1024.0;
|
||||
}
|
||||
if (mult > 0.0) {
|
||||
const double scaled = num * mult;
|
||||
if (std::isfinite(scaled) &&
|
||||
scaled < 18446744073709551616.0) {
|
||||
return static_cast<std::uint64_t>(scaled);
|
||||
}
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::range_error,
|
||||
"byte-size value overflows uint64_t"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch,
|
||||
std::string{"not an unsigned integer or byte size: "} +
|
||||
redact_display(raw, is_secret)));
|
||||
}
|
||||
|
||||
Void::Result::Result<double> parse_strict_real(std::string_view raw,
|
||||
bool is_secret)
|
||||
{
|
||||
if (raw.empty()) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch, "empty real value"));
|
||||
}
|
||||
double val = 0.0;
|
||||
const char* begin = raw.data();
|
||||
const char* end = raw.data() + raw.size();
|
||||
const auto [ptr, ec] = std::from_chars(begin, end, val);
|
||||
if (ec != std::errc{} || ptr != end || !std::isfinite(val)) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::type_mismatch,
|
||||
std::string{"invalid finite real: "} +
|
||||
redact_display(raw, is_secret)));
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
bool try_parse_vector_index(std::string_view suffix, std::size_t& out)
|
||||
{
|
||||
if (suffix.empty()) {
|
||||
return false;
|
||||
}
|
||||
if (suffix.size() > 1 && suffix.front() == '0') {
|
||||
return false;
|
||||
}
|
||||
std::size_t idx = 0;
|
||||
const auto [ptr, ec] = std::from_chars(
|
||||
suffix.data(), suffix.data() + suffix.size(), idx);
|
||||
if (ec != std::errc{} || ptr != suffix.data() + suffix.size()) {
|
||||
return false;
|
||||
}
|
||||
out = idx;
|
||||
return true;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
namespace
|
||||
{
|
||||
int precedence_rank(SourceKind kind) noexcept
|
||||
{
|
||||
switch (kind) {
|
||||
case SourceKind::Defaults:
|
||||
return 0;
|
||||
case SourceKind::FileKeyValue:
|
||||
case SourceKind::FileIni:
|
||||
case SourceKind::FileJson:
|
||||
return 1;
|
||||
case SourceKind::Environment:
|
||||
return 2;
|
||||
case SourceKind::CliArgs:
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct EmittedItem {
|
||||
std::string key{};
|
||||
ValueState state{ValueState::Present};
|
||||
std::string value{};
|
||||
std::uint32_t line{0};
|
||||
};
|
||||
|
||||
Void::Result::Result<void>
|
||||
flatten_json_checked(const Void::Json::Value& v,
|
||||
const std::string& prefix, std::size_t depth,
|
||||
const ConfigLimits& limits,
|
||||
std::vector<EmittedItem>& out,
|
||||
std::unordered_set<std::string>& dot_paths,
|
||||
std::unordered_set<std::string>& nested_paths)
|
||||
{
|
||||
if (depth > limits.max_nesting_depth) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: JSON nesting depth exceeds max_nesting_depth"));
|
||||
}
|
||||
if (v.is_object()) {
|
||||
for (const auto& [k, child] : v.as_object()) {
|
||||
if (k.size() > limits.max_key_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: JSON key exceeds max_key_bytes"));
|
||||
}
|
||||
const std::string full =
|
||||
prefix.empty() ? k : prefix + "." + k;
|
||||
if (k.find('.') != std::string::npos) {
|
||||
if (nested_paths.contains(full)) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::key_collision,
|
||||
"Configuration: key collision between literal dot '" +
|
||||
full + "' and nested JSON path"));
|
||||
}
|
||||
dot_paths.insert(full);
|
||||
} else if (!prefix.empty()) {
|
||||
if (dot_paths.contains(full)) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::key_collision,
|
||||
"Configuration: key collision between nested path '" +
|
||||
full + "' and literal dotted key"));
|
||||
}
|
||||
nested_paths.insert(full);
|
||||
}
|
||||
if (auto r = flatten_json_checked(
|
||||
child, full, depth + 1, limits, out, dot_paths,
|
||||
nested_paths);
|
||||
!r) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
if (v.is_array()) {
|
||||
const auto& arr = v.as_array();
|
||||
if (arr.size() > limits.max_vector_items) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: JSON array exceeds max_vector_items"));
|
||||
}
|
||||
for (std::size_t i = 0; i < arr.size(); ++i) {
|
||||
const std::string child_key =
|
||||
prefix + "." + std::to_string(i);
|
||||
if (dot_paths.contains(child_key)) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::key_collision,
|
||||
"Configuration: key collision with dotted path '" +
|
||||
child_key + "'"));
|
||||
}
|
||||
nested_paths.insert(child_key);
|
||||
if (auto r = flatten_json_checked(
|
||||
arr[i], child_key, depth + 1, limits, out,
|
||||
dot_paths, nested_paths);
|
||||
!r) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
if (prefix.empty()) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::parse_error,
|
||||
"Configuration: top-level JSON configuration must be an object"));
|
||||
}
|
||||
if (prefix.size() > limits.max_key_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: flattened key exceeds max_key_bytes"));
|
||||
}
|
||||
|
||||
if (v.is_null()) {
|
||||
out.push_back(EmittedItem{prefix, ValueState::Null, {}, 0});
|
||||
} else if (v.is_string()) {
|
||||
const auto& s = v.as_string();
|
||||
out.push_back(EmittedItem{
|
||||
prefix,
|
||||
s.empty() ? ValueState::Empty : ValueState::Present, s, 0});
|
||||
} else if (v.is_bool()) {
|
||||
out.push_back(EmittedItem{
|
||||
prefix, ValueState::Present,
|
||||
v.as_bool() ? "true" : "false", 0});
|
||||
} else if (v.is_int()) {
|
||||
out.push_back(EmittedItem{
|
||||
prefix, ValueState::Present, std::to_string(v.as_int()), 0});
|
||||
} else if (v.is_uint()) {
|
||||
out.push_back(EmittedItem{
|
||||
prefix, ValueState::Present, std::to_string(v.as_uint()),
|
||||
0});
|
||||
} else if (v.is_number()) {
|
||||
const double d = v.as_number();
|
||||
if (!std::isfinite(d)) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::parse_error,
|
||||
"Configuration: non-finite number for '" + prefix +
|
||||
"'"));
|
||||
}
|
||||
std::string s;
|
||||
if (!(d == 0.0 && std::signbit(d)) && std::floor(d) == d &&
|
||||
std::abs(d) < 1e15) {
|
||||
s = std::to_string(static_cast<long long>(d));
|
||||
} else {
|
||||
s = std::format("{}", d);
|
||||
}
|
||||
out.push_back(
|
||||
EmittedItem{prefix, ValueState::Present, std::move(s), 0});
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string>
|
||||
read_bounded_source_file(const std::filesystem::path& path,
|
||||
const ConfigLimits& limits)
|
||||
{
|
||||
std::error_code ec;
|
||||
const auto sz = std::filesystem::file_size(path, ec);
|
||||
if (ec) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::io_error,
|
||||
"Configuration: cannot stat source file '" + path.string() +
|
||||
"'"));
|
||||
}
|
||||
if (sz > limits.max_source_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: source file '" + path.string() +
|
||||
"' exceeds max_source_bytes"));
|
||||
}
|
||||
auto txt = Void::FileSystem::read_file(path);
|
||||
if (!txt) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::io_error,
|
||||
"Configuration: read error on '" + path.string() + "'"));
|
||||
}
|
||||
if (txt->size() > limits.max_source_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: source file '" + path.string() +
|
||||
"' exceeds max_source_bytes"));
|
||||
}
|
||||
return *txt;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<MergedConfig>
|
||||
load_sources(const SourceSet& sources, const ConfigLimits& limits,
|
||||
const std::unordered_set<std::string>& secret_keys,
|
||||
std::vector<Diagnostic>* diagnostics_out)
|
||||
{
|
||||
const auto& specs = sources.specs();
|
||||
if (specs.size() > limits.max_sources) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration::load_sources: number of sources exceeds max_sources"));
|
||||
}
|
||||
|
||||
std::vector<std::size_t> order(specs.size());
|
||||
std::iota(order.begin(), order.end(), 0);
|
||||
std::stable_sort(order.begin(), order.end(), [&](std::size_t a,
|
||||
std::size_t b) {
|
||||
const int ra = precedence_rank(specs[a].kind);
|
||||
const int rb = precedence_rank(specs[b].kind);
|
||||
return ra < rb;
|
||||
});
|
||||
|
||||
MergedConfig merged;
|
||||
std::size_t merged_bytes = 0;
|
||||
|
||||
auto record_diag = [&](const Diagnostic& d) {
|
||||
if (diagnostics_out != nullptr &&
|
||||
diagnostics_out->size() < limits.max_diagnostics) {
|
||||
diagnostics_out->push_back(d);
|
||||
}
|
||||
};
|
||||
|
||||
auto apply_item =
|
||||
[&](const SourceSpec& spec,
|
||||
const EmittedItem& item) -> Void::Result::Result<void> {
|
||||
const bool is_secret = secret_keys.contains(item.key);
|
||||
if (item.key.size() > limits.max_key_bytes) {
|
||||
Diagnostic d{ConfigErrorCode::limit_exceeded, "limit",
|
||||
SourceLocation{spec.id, item.key, item.line},
|
||||
"Configuration: key exceeds max_key_bytes",
|
||||
is_secret};
|
||||
record_diag(d);
|
||||
return Void::Result::fail(
|
||||
make_config_error(d.code, d.message));
|
||||
}
|
||||
if (item.value.size() > limits.max_scalar_bytes) {
|
||||
Diagnostic d{
|
||||
ConfigErrorCode::limit_exceeded, "limit",
|
||||
SourceLocation{spec.id, item.key, item.line},
|
||||
"Configuration: scalar value for '" + item.key + "' (" +
|
||||
detail::redact_display(
|
||||
std::to_string(item.value.size()) + "B",
|
||||
is_secret) +
|
||||
") exceeds max_scalar_bytes",
|
||||
is_secret};
|
||||
record_diag(d);
|
||||
return Void::Result::fail(
|
||||
make_config_error(d.code, d.message));
|
||||
}
|
||||
|
||||
const std::size_t delta =
|
||||
item.key.size() + (is_secret ? 0 : item.value.size()) +
|
||||
spec.id.size();
|
||||
if (merged_bytes + delta > limits.max_merged_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: merged configuration exceeds max_merged_bytes"));
|
||||
}
|
||||
if (!merged.entries.contains(item.key) &&
|
||||
merged.entries.size() >= limits.max_keys) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: key count exceeds max_keys"));
|
||||
}
|
||||
merged_bytes += delta;
|
||||
|
||||
const SourceLocation loc{spec.id, item.key, item.line};
|
||||
merged.entries[item.key] =
|
||||
MergedEntry{item.state, item.value, loc};
|
||||
|
||||
SourceOrigin origin{spec.id, spec.kind, item.line, item.state};
|
||||
auto& prov = merged.provenance->entries[item.key];
|
||||
if (!prov.key.empty()) {
|
||||
prov.shadowed.push_back(std::move(prov.winner));
|
||||
} else {
|
||||
prov.key = item.key;
|
||||
}
|
||||
prov.winner = std::move(origin);
|
||||
prov.redacted = prov.redacted || is_secret;
|
||||
return {};
|
||||
};
|
||||
|
||||
for (const std::size_t idx : order) {
|
||||
const auto& spec = specs[idx];
|
||||
std::vector<EmittedItem> items;
|
||||
|
||||
for (const auto& rm_key : spec.removals) {
|
||||
items.push_back(EmittedItem{rm_key, ValueState::Removed, {}, 0});
|
||||
}
|
||||
|
||||
switch (spec.kind) {
|
||||
case SourceKind::Defaults: {
|
||||
for (const auto& [k, v] : spec.entries) {
|
||||
items.push_back(EmittedItem{
|
||||
k, v.empty() ? ValueState::Empty : ValueState::Present,
|
||||
v, 0});
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SourceKind::FileKeyValue: {
|
||||
std::string text;
|
||||
if (spec.is_inline) {
|
||||
if (spec.inline_text.size() > limits.max_source_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: inline KV exceeds max_source_bytes"));
|
||||
}
|
||||
text = spec.inline_text;
|
||||
} else {
|
||||
auto r =
|
||||
read_bounded_source_file(spec.file_path, limits);
|
||||
if (!r) return Void::Result::fail(std::move(r).error());
|
||||
text = std::move(*r);
|
||||
}
|
||||
std::size_t pos = 0;
|
||||
std::uint32_t lineno = 0;
|
||||
while (pos <= text.size()) {
|
||||
++lineno;
|
||||
const auto nl = text.find('\n', pos);
|
||||
std::string_view line =
|
||||
nl == std::string::npos
|
||||
? std::string_view{text}.substr(pos)
|
||||
: std::string_view{text}.substr(pos, nl - pos);
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.remove_suffix(1);
|
||||
}
|
||||
while (!line.empty() && line.front() == ' ') {
|
||||
line.remove_prefix(1);
|
||||
}
|
||||
if (!line.empty() && line.front() != '#') {
|
||||
const auto eq = line.find('=');
|
||||
if (eq == std::string_view::npos) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::parse_error,
|
||||
std::format(
|
||||
"Configuration: source '{}' line {}: missing '='",
|
||||
spec.id, lineno)));
|
||||
}
|
||||
auto k = line.substr(0, eq);
|
||||
while (!k.empty() && k.back() == ' ') {
|
||||
k.remove_suffix(1);
|
||||
}
|
||||
auto v = line.substr(eq + 1);
|
||||
items.push_back(EmittedItem{
|
||||
std::string{k},
|
||||
v.empty() ? ValueState::Empty
|
||||
: ValueState::Present,
|
||||
std::string{v}, lineno});
|
||||
}
|
||||
if (nl == std::string::npos) break;
|
||||
pos = nl + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SourceKind::FileIni: {
|
||||
std::string text;
|
||||
if (spec.is_inline) {
|
||||
if (spec.inline_text.size() > limits.max_source_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: inline INI exceeds max_source_bytes"));
|
||||
}
|
||||
text = spec.inline_text;
|
||||
} else {
|
||||
auto r =
|
||||
read_bounded_source_file(spec.file_path, limits);
|
||||
if (!r) return Void::Result::fail(std::move(r).error());
|
||||
text = std::move(*r);
|
||||
}
|
||||
Void::Ini::IniFile ini;
|
||||
if (auto err = Void::Ini::IniFile::parse(ini, text);
|
||||
err.has_value()) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::parse_error,
|
||||
std::format(
|
||||
"Configuration: INI source '{}' line {}: {}",
|
||||
spec.id, err->line, err->message)));
|
||||
}
|
||||
for (const auto& sec : ini.sections()) {
|
||||
const auto* tbl = ini.section(sec);
|
||||
if (tbl == nullptr) continue;
|
||||
for (const auto& [k, v] : *tbl) {
|
||||
std::string full =
|
||||
sec.empty() ? k : sec + "." + k;
|
||||
items.push_back(EmittedItem{
|
||||
std::move(full),
|
||||
v.empty() ? ValueState::Empty
|
||||
: ValueState::Present,
|
||||
v, 0});
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SourceKind::FileJson: {
|
||||
std::string text;
|
||||
if (spec.is_inline) {
|
||||
if (spec.inline_text.size() > limits.max_source_bytes) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::limit_exceeded,
|
||||
"Configuration: inline JSON exceeds max_source_bytes"));
|
||||
}
|
||||
text = spec.inline_text;
|
||||
} else {
|
||||
auto r =
|
||||
read_bounded_source_file(spec.file_path, limits);
|
||||
if (!r) return Void::Result::fail(std::move(r).error());
|
||||
text = std::move(*r);
|
||||
}
|
||||
auto parsed = Void::Json::parse(text);
|
||||
if (!parsed) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::parse_error,
|
||||
std::format(
|
||||
"Configuration: JSON source '{}' line {} col {}: {}",
|
||||
spec.id, parsed.error().line,
|
||||
parsed.error().col, parsed.error().message)));
|
||||
}
|
||||
if (!parsed->is_object()) {
|
||||
return Void::Result::fail(make_config_error(
|
||||
ConfigErrorCode::parse_error,
|
||||
"Configuration: JSON root must be an object in '" +
|
||||
spec.id + "'"));
|
||||
}
|
||||
std::unordered_set<std::string> dot_paths;
|
||||
std::unordered_set<std::string> nested_paths;
|
||||
if (auto fr = flatten_json_checked(*parsed, "", 0, limits,
|
||||
items, dot_paths,
|
||||
nested_paths);
|
||||
!fr) {
|
||||
return Void::Result::fail(std::move(fr).error());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SourceKind::Environment: {
|
||||
for (const auto& [k, v] : spec.entries) {
|
||||
items.push_back(EmittedItem{
|
||||
k, v.empty() ? ValueState::Empty : ValueState::Present,
|
||||
v, 0});
|
||||
}
|
||||
for (const auto& k : spec.env_keys) {
|
||||
if (auto ev = get_env(k); ev) {
|
||||
items.push_back(EmittedItem{
|
||||
k,
|
||||
ev->empty() ? ValueState::Empty
|
||||
: ValueState::Present,
|
||||
std::move(*ev), 0});
|
||||
}
|
||||
}
|
||||
if (!spec.env_prefix.empty()) {
|
||||
#ifdef VOID_SCHEMA_HAS_ENVIRON
|
||||
for (char** env = environ; *env != nullptr; ++env) {
|
||||
const std::string_view kv{*env};
|
||||
const auto eq = kv.find('=');
|
||||
if (eq == std::string_view::npos ||
|
||||
!kv.starts_with(spec.env_prefix)) {
|
||||
continue;
|
||||
}
|
||||
const auto raw_k = kv.substr(
|
||||
spec.env_prefix.size(),
|
||||
eq - spec.env_prefix.size());
|
||||
std::string normalized;
|
||||
normalized.reserve(raw_k.size());
|
||||
for (std::size_t i = 0; i < raw_k.size(); ++i) {
|
||||
if (raw_k[i] == '_' && i + 1 < raw_k.size() &&
|
||||
raw_k[i + 1] == '_') {
|
||||
normalized += '.';
|
||||
++i;
|
||||
} else {
|
||||
normalized += static_cast<char>(
|
||||
std::tolower(
|
||||
static_cast<unsigned char>(
|
||||
raw_k[i])));
|
||||
}
|
||||
}
|
||||
const auto val_sv = kv.substr(eq + 1);
|
||||
items.push_back(EmittedItem{
|
||||
std::move(normalized),
|
||||
val_sv.empty() ? ValueState::Empty
|
||||
: ValueState::Present,
|
||||
std::string{val_sv}, 0});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SourceKind::CliArgs: {
|
||||
for (std::size_t i = 0; i < spec.cli_args.size(); ++i) {
|
||||
const std::string_view arg{spec.cli_args[i]};
|
||||
if (!arg.starts_with("--") || arg.size() <= 2) {
|
||||
continue;
|
||||
}
|
||||
const auto body = arg.substr(2);
|
||||
if (const auto eq = body.find('=');
|
||||
eq != std::string_view::npos) {
|
||||
const auto val_sv = body.substr(eq + 1);
|
||||
items.push_back(EmittedItem{
|
||||
std::string{body.substr(0, eq)},
|
||||
val_sv.empty() ? ValueState::Empty
|
||||
: ValueState::Present,
|
||||
std::string{val_sv}, 0});
|
||||
} else if (i + 1 < spec.cli_args.size() &&
|
||||
!std::string_view{spec.cli_args[i + 1]}
|
||||
.starts_with("--")) {
|
||||
const auto val_sv =
|
||||
std::string_view{spec.cli_args[i + 1]};
|
||||
items.push_back(EmittedItem{
|
||||
std::string{body},
|
||||
val_sv.empty() ? ValueState::Empty
|
||||
: ValueState::Present,
|
||||
std::string{val_sv}, 0});
|
||||
++i;
|
||||
} else {
|
||||
items.push_back(EmittedItem{
|
||||
std::string{body}, ValueState::Present, "true",
|
||||
0});
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& item : items) {
|
||||
if (auto r = apply_item(spec, item); !r) {
|
||||
return Void::Result::fail(std::move(r).error());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deterministic FNV-1a digest over sorted entries.
|
||||
std::uint64_t h = 14695981039346656037ULL;
|
||||
auto mix = [&](std::string_view s) {
|
||||
for (const char ch : s) {
|
||||
h ^= static_cast<unsigned char>(ch);
|
||||
h *= 1099511628211ULL;
|
||||
}
|
||||
h ^= 0xff;
|
||||
h *= 1099511628211ULL;
|
||||
};
|
||||
for (const auto& [k, e] : merged.entries) {
|
||||
mix(k);
|
||||
h ^= static_cast<std::uint64_t>(e.state);
|
||||
h *= 1099511628211ULL;
|
||||
mix(e.value);
|
||||
}
|
||||
merged.revision_digest = h;
|
||||
return merged;
|
||||
}
|
||||
} // namespace Void::Configuration
|
||||
@@ -1,3 +1,4 @@
|
||||
void_add_module(Configuration
|
||||
INTERNAL_DEPS Core Functional System Data Time LogState
|
||||
EXCLUDE_HEADERS Void/Configuration/ReloadController.hpp
|
||||
)
|
||||
Reference in new issue
Block a user