mirror of
https://github.com/telemt/telemt.git
synced 2026-11-22 07:16:38 +03:00
API: add scoped DNS overrides management
This commit is contained in:
+46
-5
@@ -1,15 +1,16 @@
|
||||
//! Config-editing API: read managed sections and apply sparse field patches.
|
||||
//! `access.*` is intentionally not editable here (owned by the users API).
|
||||
//! `[server]` is only partially editable — see [`EDITABLE_SERVER_FIELDS`].
|
||||
//! `[network]` exposes only [`EDITABLE_NETWORK_FIELDS`].
|
||||
|
||||
use serde_json::Value as Json;
|
||||
use toml::Value as Toml;
|
||||
|
||||
use super::ApiShared;
|
||||
use super::config_store::{
|
||||
EDITABLE_SECTIONS, EDITABLE_SERVER_FIELDS, compute_snapshot_revision, is_editable_section,
|
||||
load_config_snapshot, render_server_listeners, render_top_level_section,
|
||||
resolve_single_source_owner, upsert_toml_table,
|
||||
EDITABLE_NETWORK_FIELDS, EDITABLE_SECTIONS, EDITABLE_SERVER_FIELDS, compute_snapshot_revision,
|
||||
is_editable_section, load_config_snapshot, render_server_listeners, render_top_level_section,
|
||||
resolve_single_source_owner, upsert_dns_overrides, upsert_toml_table,
|
||||
};
|
||||
#[cfg(test)]
|
||||
use super::config_store::{write_atomic, write_atomic_if_unchanged};
|
||||
@@ -208,8 +209,7 @@ pub(super) async fn read_managed_config(config_path: &Path) -> Result<(Toml, Str
|
||||
.unwrap_or_else(toml::value::Table::new);
|
||||
// Whitelist: return ONLY the editable sections. A blacklist (just removing
|
||||
// `access`) would leak `server.api` (auth_header) and `network` (per-node
|
||||
// addresses). Mirror the PATCH contract, including the nested server
|
||||
// field-level allowlist.
|
||||
// addresses). Mirror the PATCH contract, including both nested allowlists.
|
||||
let mut table = toml::value::Table::new();
|
||||
for section in EDITABLE_SECTIONS {
|
||||
if let Some(value) = parsed_table.get(*section) {
|
||||
@@ -221,6 +221,14 @@ pub(super) async fn read_managed_config(config_path: &Path) -> Result<(Toml, Str
|
||||
table.insert("server".to_string(), filtered);
|
||||
}
|
||||
}
|
||||
if let Some(network) = parsed_table.get("network").and_then(Toml::as_table) {
|
||||
let filtered = network
|
||||
.iter()
|
||||
.filter(|(key, _)| EDITABLE_NETWORK_FIELDS.contains(&key.as_str()))
|
||||
.map(|(key, value)| (key.clone(), value.clone()))
|
||||
.collect();
|
||||
table.insert("network".to_owned(), Toml::Table(filtered));
|
||||
}
|
||||
|
||||
Ok((Toml::Table(table), revision))
|
||||
}
|
||||
@@ -283,6 +291,36 @@ fn validate_server_patch(server: &Toml) -> Result<(), ApiFailure> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_network_patch(network: &Json) -> Result<(), ApiFailure> {
|
||||
let Some(table) = network.as_object() else {
|
||||
return Err(ApiFailure::new(
|
||||
hyper::StatusCode::BAD_REQUEST,
|
||||
"section_not_editable",
|
||||
"network patch must be a JSON object",
|
||||
));
|
||||
};
|
||||
// Check the original keys so even a forbidden null member cannot bypass
|
||||
// the network allowlist during JSON-to-TOML null removal.
|
||||
for key in table.keys() {
|
||||
if !EDITABLE_NETWORK_FIELDS.contains(&key.as_str()) {
|
||||
return Err(ApiFailure::new(
|
||||
hyper::StatusCode::BAD_REQUEST,
|
||||
"field_not_editable",
|
||||
format!(
|
||||
"network.{key} is not editable via the config API; allowed network fields: {}",
|
||||
EDITABLE_NETWORK_FIELDS.join(", ")
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
if table.values().all(Json::is_null) {
|
||||
return Err(ApiFailure::bad_request(
|
||||
"empty network patch: provide dns_overrides (use [] to clear)",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Convert a serde_json value to a toml value. `null` is dropped from objects
|
||||
/// (a patch never sets a key to TOML-null). Numbers become integers when exact,
|
||||
/// otherwise floats.
|
||||
@@ -363,3 +401,6 @@ mod quota_period_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod preflight_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod dns_overrides_tests;
|
||||
|
||||
@@ -0,0 +1,420 @@
|
||||
use super::*;
|
||||
use crate::api::discovery::http_tests::Fixture;
|
||||
use crate::config::hot_reload::spawn_config_watcher;
|
||||
use crate::maestro::generation::test_runtime_generation;
|
||||
use crate::network::dns_overrides::GenerationDnsResolver;
|
||||
use serde_json::json;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
const OLD: &str = "old.example:443:192.0.2.1";
|
||||
const NEW: &str = "new.example:443:192.0.2.2";
|
||||
|
||||
fn patch(entries: Json) -> Json {
|
||||
json!({"network": {"dns_overrides": entries}})
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_exposes_only_dns_overrides_including_the_empty_default() {
|
||||
for entries in [vec![], vec![OLD.to_owned()]] {
|
||||
let source = format!(
|
||||
"[network]\nipv4 = true\ncache_public_ip_path = 'private-path'\ndns_overrides = {}\n",
|
||||
json!(entries)
|
||||
);
|
||||
let (path, _directory) = tests::temp_config(&source);
|
||||
let (managed, _) = read_managed_config(&path).await.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_value(&managed["network"]).unwrap(),
|
||||
patch(json!(entries))["network"]
|
||||
);
|
||||
assert!(
|
||||
!serde_json::to_string(&managed)
|
||||
.unwrap()
|
||||
.contains("private-path")
|
||||
);
|
||||
}
|
||||
let (path, _directory) = tests::temp_config("");
|
||||
let (managed, _) = read_managed_config(&path).await.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_value(&managed["network"]).unwrap(),
|
||||
json!({"dns_overrides": []})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn every_network_sibling_is_rejected_even_as_null_without_writing() {
|
||||
let (path, _directory) = tests::temp_config("[network]\nipv4 = true\n");
|
||||
let original = std::fs::read(&path).unwrap();
|
||||
let network = serde_json::to_value(ProxyConfig::default().network).unwrap();
|
||||
for (key, value) in network.as_object().unwrap() {
|
||||
if key == "dns_overrides" {
|
||||
continue;
|
||||
}
|
||||
for value in [value.clone(), Json::Null] {
|
||||
let body = json!({"network": {key: value, "dns_overrides": [NEW]}});
|
||||
let error = apply_patch_to_path(&path, &body, None).await.unwrap_err();
|
||||
assert_eq!(error.code, "field_not_editable", "{key}: {error:?}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), original);
|
||||
}
|
||||
}
|
||||
for value in [
|
||||
json!([]),
|
||||
json!("bad"),
|
||||
json!({}),
|
||||
json!({"dns_overrides": null}),
|
||||
json!({"typo": []}),
|
||||
] {
|
||||
assert!(
|
||||
apply_patch_to_path(&path, &json!({"network": value}), None)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(std::fs::read(&path).unwrap(), original);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn malformed_dns_arrays_never_change_source_or_revision() {
|
||||
let (path, _directory) = tests::temp_config(&format!("[network]\ndns_overrides = ['{OLD}']\n"));
|
||||
let original = std::fs::read(&path).unwrap();
|
||||
let revision = super::super::config_store::current_revision(&path)
|
||||
.await
|
||||
.unwrap();
|
||||
for entries in [
|
||||
json!(NEW),
|
||||
json!([12]),
|
||||
json!([""]),
|
||||
json!(["host:443:bad-ip"]),
|
||||
json!(["host:65536:192.0.2.1"]),
|
||||
json!([":443:192.0.2.1"]),
|
||||
json!(["host:443:2001:db8::1"]),
|
||||
json!([null]),
|
||||
json!(vec![OLD; 4097]),
|
||||
] {
|
||||
let error = apply_patch_to_path(&path, &patch(entries), None)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.status, hyper::StatusCode::BAD_REQUEST);
|
||||
assert_eq!(std::fs::read(&path).unwrap(), original);
|
||||
assert_eq!(
|
||||
super::super::config_store::current_revision(&path)
|
||||
.await
|
||||
.unwrap(),
|
||||
revision
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn add_replace_and_clear_preserve_unrelated_source_bytes() {
|
||||
let sources = [
|
||||
"# root without network\n[general]\nlog_level = 'normal'\n".to_owned(),
|
||||
"[network] # identity\nipv4 = true # keep\n[general]\nlog_level = 'normal'\n".to_owned(),
|
||||
"network = { ipv4 = true } # keep\n[general]\nlog_level = 'normal'\n".to_owned(),
|
||||
"network = {}\n".to_owned(),
|
||||
"network.ipv4 = true # keep\n[general]\nlog_level = 'normal'\n".to_owned(),
|
||||
"['network'] # quoted\nipv4 = true\n".to_owned(),
|
||||
"[network] # no final newline".to_owned(),
|
||||
"[network]\r\nipv4 = true # keep\r\n".to_owned(),
|
||||
];
|
||||
for source in sources {
|
||||
let (path, _directory) = tests::temp_config(&source);
|
||||
let initial_network = ProxyConfig::load(&path).unwrap().network;
|
||||
for entries in [
|
||||
json!([OLD, "v6.example:8443:[2001:db8::1]"]),
|
||||
json!([NEW]),
|
||||
json!([]),
|
||||
] {
|
||||
let (before, revision) = read_managed_config(&path).await.unwrap();
|
||||
let response = apply_patch_to_path(&path, &patch(entries.clone()), Some(revision))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!response.restart_required);
|
||||
assert!(!response.process_restart_required);
|
||||
assert_eq!(response.changed, ["network"]);
|
||||
let expected_paths = if entries == json!([]) {
|
||||
vec!["network.dns_overrides[0]"]
|
||||
} else {
|
||||
vec!["network.dns_overrides[0]", "network.dns_overrides[1]"]
|
||||
};
|
||||
assert_eq!(response.changed_paths, expected_paths);
|
||||
let (managed, revision) = read_managed_config(&path).await.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_value(&managed["network"]["dns_overrides"]).unwrap(),
|
||||
entries
|
||||
);
|
||||
assert_eq!(response.revision, revision);
|
||||
assert_eq!(managed["general"], before["general"]);
|
||||
let mut network = ProxyConfig::load(&path).unwrap().network;
|
||||
network.dns_overrides = initial_network.dns_overrides.clone();
|
||||
assert_eq!(
|
||||
serde_json::to_value(network).unwrap(),
|
||||
serde_json::to_value(&initial_network).unwrap()
|
||||
);
|
||||
}
|
||||
let written = std::fs::read_to_string(&path).unwrap();
|
||||
// New declarations can be inserted, but every original line survives.
|
||||
for line in source.lines() {
|
||||
if line.starts_with("network = {") {
|
||||
assert!(
|
||||
written.contains(&line.replace("{", "{dns_overrides = [], "))
|
||||
|| written.contains(&line.replace("{}", "{dns_overrides = []}")),
|
||||
"{written}"
|
||||
);
|
||||
} else {
|
||||
assert!(written.contains(line), "{source:?}: {written}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn replacement_uses_parser_spans_for_multiline_dotted_and_inline_values() {
|
||||
let old_value = format!("[\n '{OLD}', # replace only this value\n]");
|
||||
for (prefix, value, suffix) in [
|
||||
(
|
||||
"# Unicode: ü\n[network]\n\"dns_overrides\" = ",
|
||||
old_value.as_str(),
|
||||
" # trailing\nipv4=true\n",
|
||||
),
|
||||
(
|
||||
"network = { ipv4=true, dns_overrides=",
|
||||
"['old.example:443:192.0.2.1']",
|
||||
" } # trailing\n",
|
||||
),
|
||||
(
|
||||
"'network'.'dns_overrides' = ",
|
||||
"['old.example:443:192.0.2.1']",
|
||||
"\nnetwork.ipv4=true\n",
|
||||
),
|
||||
] {
|
||||
let source = format!("{prefix}{value}{suffix}");
|
||||
let (path, _directory) = tests::temp_config(&source);
|
||||
apply_patch_to_path(&path, &patch(json!([NEW])), None)
|
||||
.await
|
||||
.unwrap();
|
||||
let written = std::fs::read_to_string(&path).unwrap();
|
||||
assert_eq!(written, format!("{prefix}[\"{NEW}\"]{suffix}"));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn included_parent_owns_new_dns_key_and_leaf_owner_precedes_other_network_fields() {
|
||||
for existing in [false, true] {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("config.toml");
|
||||
let included = directory.path().join("network.toml");
|
||||
let root_text = if existing {
|
||||
"include = \"network.toml\"\nnetwork.ipv4 = true # keep root identity\n[general]\nlog_level='normal'\n"
|
||||
} else {
|
||||
"include = \"network.toml\"\n[general]\nlog_level='normal'\n"
|
||||
};
|
||||
let included_text = if existing {
|
||||
format!("network.dns_overrides = ['{OLD}'] # retain\n")
|
||||
} else {
|
||||
"[network] # retain\nipv4 = true # identity\n".to_owned()
|
||||
};
|
||||
std::fs::write(&root, root_text).unwrap();
|
||||
std::fs::write(&included, &included_text).unwrap();
|
||||
for entries in [json!([NEW]), json!([])] {
|
||||
apply_patch_to_path(&root, &patch(entries.clone()), None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read_to_string(&root).unwrap(), root_text);
|
||||
assert_eq!(
|
||||
serde_json::to_value(ProxyConfig::load(&root).unwrap().network.dns_overrides)
|
||||
.unwrap(),
|
||||
entries
|
||||
);
|
||||
let written = std::fs::read_to_string(&included).unwrap();
|
||||
assert!(written.contains("# retain"));
|
||||
if !existing {
|
||||
assert!(written.contains("ipv4 = true # identity"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cross_source_and_ambiguous_parent_edits_are_atomic_rejections() {
|
||||
for (root_text, included_text, body) in [
|
||||
(
|
||||
"include = \"network.toml\"\n[censorship]\ntls_domain='old.example'\n".to_owned(),
|
||||
format!("[network]\ndns_overrides=['{OLD}']\n"),
|
||||
json!({"network":{"dns_overrides":[NEW]},"censorship":{"tls_domain":"new.example"}}),
|
||||
),
|
||||
(
|
||||
"include = \"network.toml\"\nnetwork.ipv4 = true\n".to_owned(),
|
||||
"network.stun_use = false\n".to_owned(),
|
||||
patch(json!([NEW])),
|
||||
),
|
||||
] {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let root = directory.path().join("config.toml");
|
||||
let included = directory.path().join("network.toml");
|
||||
std::fs::write(&root, &root_text).unwrap();
|
||||
std::fs::write(&included, &included_text).unwrap();
|
||||
let error = apply_patch_to_path(&root, &body, None).await.unwrap_err();
|
||||
assert_eq!(error.code, "config_patch_not_atomic");
|
||||
assert_eq!(std::fs::read_to_string(&root).unwrap(), root_text);
|
||||
assert_eq!(std::fs::read_to_string(&included).unwrap(), included_text);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn prepared_dns_patch_retains_source_revision_fencing() {
|
||||
let (path, _directory) = tests::temp_config(&format!("[network]\ndns_overrides=['{OLD}']\n"));
|
||||
let prepared = prepare_patch_to_path(&path, &patch(json!([NEW])), None)
|
||||
.await
|
||||
.unwrap();
|
||||
let external = "[network]\ndns_overrides=[]\n# external edit\n";
|
||||
std::fs::write(&path, external).unwrap();
|
||||
let error = write_atomic_if_unchanged(
|
||||
prepared.config_path,
|
||||
prepared.expected_revision,
|
||||
prepared.owner_path,
|
||||
prepared.expected_owner_contents,
|
||||
prepared.owner_contents,
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code, "revision_conflict");
|
||||
assert_eq!(std::fs::read_to_string(&path).unwrap(), external);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_preflight_commit_and_watcher_update_future_generation_resolutions() {
|
||||
let fixture = Fixture::new(false);
|
||||
let source = "[network]\nipv4 = true # retain\n[server.api]\nenabled=true\nread_only=false\nauth_header='Bearer discovery-test'\n";
|
||||
std::fs::write(&fixture.shared.config_path, source).unwrap();
|
||||
let config = Arc::new(
|
||||
ProxyConfig::load_prepared(fixture.shared.config_path.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
.config,
|
||||
);
|
||||
let resolver = Arc::new(GenerationDnsResolver::from_entries(&[]).unwrap());
|
||||
let cancellation = CancellationToken::new();
|
||||
let _cancel_on_drop = cancellation.clone().drop_guard();
|
||||
let (activation, activated) = watch::channel(false);
|
||||
let (mut receiver, _log_receiver, watcher) = spawn_config_watcher(
|
||||
fixture.shared.config_path.clone(),
|
||||
config.clone(),
|
||||
None,
|
||||
None,
|
||||
cancellation.clone(),
|
||||
Some(resolver.clone()),
|
||||
Some(activated),
|
||||
Some(fixture.shared.mutation_lock.clone()),
|
||||
None,
|
||||
);
|
||||
let mut runtime = test_runtime_generation(1, (*config).clone());
|
||||
Arc::get_mut(&mut runtime).unwrap().config_rx = receiver.clone();
|
||||
fixture.shared.active_runtime.store(runtime);
|
||||
let task = tokio::spawn(watcher);
|
||||
|
||||
for entries in [
|
||||
json!([OLD]),
|
||||
json!([NEW, "v6.example:8443:[2001:db8::1]"]),
|
||||
json!([]),
|
||||
] {
|
||||
let body = patch(entries.clone());
|
||||
let before = std::fs::read(&fixture.shared.config_path).unwrap();
|
||||
let preview = fixture
|
||||
.request_body("POST", "/v1/config/validate", true, Some(&body), &[])
|
||||
.await;
|
||||
assert_eq!(preview.0, 200, "{}", preview.2);
|
||||
let data = &preview.2["data"];
|
||||
let expected_paths = if entries == json!([OLD]) {
|
||||
json!(["network.dns_overrides[0]"])
|
||||
} else {
|
||||
json!(["network.dns_overrides[0]", "network.dns_overrides[1]"])
|
||||
};
|
||||
assert_eq!(data["apply"]["watcher_paths"], expected_paths);
|
||||
assert_eq!(data["changed_paths"], expected_paths);
|
||||
assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), before);
|
||||
let response = fixture
|
||||
.request_body(
|
||||
"PATCH",
|
||||
"/v1/config",
|
||||
true,
|
||||
Some(&body),
|
||||
&[
|
||||
("If-Match", data["base_revision"].as_str().unwrap()),
|
||||
(
|
||||
"If-Runtime-Match",
|
||||
data["runtime"]["runtime_match"].as_str().unwrap(),
|
||||
),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(response.0, 200, "{}", response.2);
|
||||
assert_eq!(response.2["data"]["restart_required"], false);
|
||||
assert_eq!(response.2["data"]["process_restart_required"], false);
|
||||
activation.send_replace(true);
|
||||
let wanted: Vec<String> = serde_json::from_value(entries.clone()).unwrap();
|
||||
tokio::time::timeout(
|
||||
Duration::from_secs(5),
|
||||
receiver.wait_for(|config| config.network.dns_overrides == wanted),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
resolver.resolve_socket_addr("OLD.EXAMPLE", 443),
|
||||
if entries == json!([OLD]) {
|
||||
Some("192.0.2.1:443".parse().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
resolver.resolve_socket_addr("new.example", 443),
|
||||
if wanted.iter().any(|entry| entry == NEW) {
|
||||
Some("192.0.2.2:443".parse().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
resolver.resolve_socket_addr("v6.example", 8443),
|
||||
if wanted.len() == 2 {
|
||||
Some("[2001:db8::1]:8443".parse().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
);
|
||||
assert!(resolver.resolve_socket_addr("new.example", 444).is_none());
|
||||
let get = fixture.request("GET", "/v1/config", true).await;
|
||||
assert_eq!(get.0, 200);
|
||||
assert_eq!(get.2["data"]["network"], body["network"]);
|
||||
assert!(fixture.shared.reload_control.in_progress().await.is_none());
|
||||
let current = std::fs::read(&fixture.shared.config_path).unwrap();
|
||||
for invalid in [
|
||||
patch(json!(["invalid"])),
|
||||
json!({"network":{"ipv4":false,"dns_overrides":[]}}),
|
||||
] {
|
||||
for method in ["POST", "PATCH"] {
|
||||
let route = if method == "POST" {
|
||||
"/v1/config/validate"
|
||||
} else {
|
||||
"/v1/config"
|
||||
};
|
||||
assert_eq!(
|
||||
fixture
|
||||
.request_body(method, route, true, Some(&invalid), &[])
|
||||
.await
|
||||
.0,
|
||||
400
|
||||
);
|
||||
assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), current);
|
||||
}
|
||||
}
|
||||
}
|
||||
cancellation.cancel();
|
||||
tokio::time::timeout(Duration::from_secs(5), task)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
}
|
||||
@@ -69,7 +69,7 @@ fn parse_patch(
|
||||
));
|
||||
}
|
||||
|
||||
// 2. convert + reject access / unknown sections / forbidden server fields
|
||||
// 2. Reject access, unknown sections and forbidden partial-section fields.
|
||||
let mut patch_toml = json_to_toml(patch_json)
|
||||
.map_err(|e| ApiFailure::bad_request(format!("invalid patch: {}", e)))?;
|
||||
let patch_table = patch_toml
|
||||
@@ -92,6 +92,8 @@ fn parse_patch(
|
||||
}
|
||||
if key == "server" {
|
||||
validate_server_patch(value)?;
|
||||
} else if key == "network" {
|
||||
validate_network_patch(&patch_json["network"])?;
|
||||
}
|
||||
}
|
||||
let touched: Vec<String> = patch_table
|
||||
@@ -130,6 +132,8 @@ fn parse_patch(
|
||||
.map(|section| {
|
||||
if section == "server" {
|
||||
"server.listeners"
|
||||
} else if section == "network" {
|
||||
"network.dns_overrides"
|
||||
} else {
|
||||
section.as_str()
|
||||
}
|
||||
@@ -147,6 +151,9 @@ fn parse_patch(
|
||||
if *section == "server" {
|
||||
let rendered = render_server_listeners(&requested_cfg)?;
|
||||
owner_contents = upsert_toml_table(&owner_contents, "server.listeners", &rendered);
|
||||
} else if *section == "network" {
|
||||
owner_contents =
|
||||
upsert_dns_overrides(&owner_contents, &requested_cfg.network.dns_overrides)?;
|
||||
} else {
|
||||
let rendered = render_top_level_section(&requested_cfg, section)?;
|
||||
owner_contents = upsert_toml_table(&owner_contents, section, &rendered);
|
||||
|
||||
@@ -73,7 +73,7 @@ fn deep_merge_overlays_tables_and_replaces_scalars() {
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn temp_config(body: &str) -> (PathBuf, tempfile::TempDir) {
|
||||
pub(super) fn temp_config(body: &str) -> (PathBuf, tempfile::TempDir) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("config.toml");
|
||||
std::fs::write(&path, body).unwrap();
|
||||
@@ -228,7 +228,7 @@ async fn invalid_web_patch_does_not_modify_the_source() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_managed_config_returns_only_editable_sections() {
|
||||
// Full server (api/port) and network must not leak. Listeners-only server
|
||||
// Server identity and network siblings must not leak. Listeners-only server
|
||||
// is returned via the nested allowlist (covered in a dedicated test).
|
||||
let (path, _d) = temp_config(concat!(
|
||||
"[censorship]\ntls_domain = \"a\"\n",
|
||||
@@ -243,7 +243,10 @@ async fn read_managed_config_returns_only_editable_sections() {
|
||||
assert!(server.contains_key("listeners"));
|
||||
assert!(!server.contains_key("api"));
|
||||
assert!(!server.contains_key("port"));
|
||||
assert!(!table.contains_key("network"));
|
||||
assert_eq!(
|
||||
serde_json::to_value(&table["network"]).unwrap(),
|
||||
serde_json::json!({"dns_overrides": []})
|
||||
);
|
||||
assert!(!table.contains_key("access"));
|
||||
}
|
||||
|
||||
|
||||
+19
-8
@@ -9,6 +9,7 @@ use crate::config::{ConfigSourceGraph, LoadedConfig, ParsedConfigSource, ProxyCo
|
||||
use super::model::ApiFailure;
|
||||
|
||||
// Source-preserving TOML rendering and atomic persistence helpers.
|
||||
mod dns_overrides;
|
||||
mod persistence;
|
||||
// Compare-and-replace file persistence and metadata preservation.
|
||||
mod atomic;
|
||||
@@ -17,6 +18,7 @@ pub(in crate::api) use atomic::write_atomic;
|
||||
#[cfg(test)]
|
||||
pub(in crate::api) use atomic::write_atomic_if_unchanged;
|
||||
pub(in crate::api) use atomic::{CommitCheck, write_atomic_with_runtime_check};
|
||||
pub(in crate::api) use dns_overrides::upsert_dns_overrides;
|
||||
#[cfg(test)]
|
||||
pub(in crate::api) use persistence::save_access_sections_to_disk;
|
||||
#[cfg(test)]
|
||||
@@ -156,6 +158,7 @@ pub(super) fn resolve_single_source_owner(
|
||||
|
||||
for target in targets {
|
||||
let mut owners = BTreeSet::new();
|
||||
let mut parent_owners = BTreeSet::new();
|
||||
for (path, content) in &loaded.source_contents {
|
||||
let parsed: toml::Value = toml::from_str(content).map_err(|error| {
|
||||
ApiFailure::new(
|
||||
@@ -170,6 +173,14 @@ pub(super) fn resolve_single_source_owner(
|
||||
if toml_path_exists(&parsed, target) {
|
||||
owners.insert(path.clone());
|
||||
}
|
||||
if *target == "network.dns_overrides" && toml_path_exists(&parsed, "network") {
|
||||
parent_owners.insert(path.clone());
|
||||
}
|
||||
}
|
||||
// A new DNS key belongs to the existing network table, which may live
|
||||
// in an include. Existing leaf ownership takes precedence over siblings.
|
||||
if owners.is_empty() && *target == "network.dns_overrides" {
|
||||
owners = parent_owners;
|
||||
}
|
||||
match owners.len() {
|
||||
0 => {
|
||||
@@ -306,7 +317,6 @@ pub(super) async fn save_config_to_disk(
|
||||
/// identity invariant at the Telemt layer too):
|
||||
///
|
||||
/// - `access` : owned by the users API.
|
||||
/// - `network` : carries per-node identity (`ipv4`/`ipv6`).
|
||||
/// - `show_link` : legacy top-level scalar/array (not a `[table]`), superseded
|
||||
/// by the editable `general.links.show` sub-table. The
|
||||
/// section-upsert machinery here only handles `[table]` /
|
||||
@@ -316,10 +326,8 @@ pub(super) async fn save_config_to_disk(
|
||||
/// `server` is partially editable: only the nested fields listed in
|
||||
/// [`EDITABLE_SERVER_FIELDS`] (currently `listeners`) may appear in GET/PATCH.
|
||||
/// Secrets and bind identity (`api`/`admin_api`, `port`, unix sockets, …) stay
|
||||
/// blocked. See also the field-level allowlist note below for `network.*`.
|
||||
///
|
||||
/// A future field-level allowlist can re-admit specific safe fields
|
||||
/// (e.g. `network.dns_overrides`) without opening the whole section.
|
||||
/// blocked. `network` is also partial: only [`EDITABLE_NETWORK_FIELDS`] are
|
||||
/// exposed, retaining the boundary around per-node identity and routing policy.
|
||||
pub(super) const EDITABLE_SECTIONS: &[&str] = &[
|
||||
"general",
|
||||
"timeouts",
|
||||
@@ -335,12 +343,15 @@ pub(super) const EDITABLE_SECTIONS: &[&str] = &[
|
||||
/// merge semantics for non-table values.
|
||||
pub(super) const EDITABLE_SERVER_FIELDS: &[&str] = &["listeners"];
|
||||
|
||||
/// Only the generation resolver's overrides may be read/patched under `network`.
|
||||
pub(super) const EDITABLE_NETWORK_FIELDS: &[&str] = &["dns_overrides"];
|
||||
|
||||
/// Whether `key` is an allowed top-level PATCH/GET section name.
|
||||
///
|
||||
/// Fully editable sections from [`EDITABLE_SECTIONS`], plus `server` which is
|
||||
/// further restricted by [`EDITABLE_SERVER_FIELDS`].
|
||||
/// Fully editable sections plus `server` and `network`, each further restricted
|
||||
/// by its nested field allowlist.
|
||||
pub(super) fn is_editable_section(key: &str) -> bool {
|
||||
EDITABLE_SECTIONS.contains(&key) || key == "server"
|
||||
EDITABLE_SECTIONS.contains(&key) || matches!(key, "server" | "network")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
//! Preserve sibling source bytes when editing the one API-owned network key.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::Deserialize;
|
||||
use toml::{Spanned, Value};
|
||||
|
||||
use crate::api::model::ApiFailure;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Source {
|
||||
network: Option<Spanned<BTreeMap<String, Spanned<Value>>>>,
|
||||
}
|
||||
|
||||
pub(in crate::api) fn upsert_dns_overrides(
|
||||
source: &str,
|
||||
entries: &[String],
|
||||
) -> Result<String, ApiFailure> {
|
||||
let parsed: Source = toml::from_str(source)
|
||||
.map_err(|_| ApiFailure::internal("Cannot locate network.dns_overrides in its source"))?;
|
||||
let value = Value::Array(entries.iter().cloned().map(Value::String).collect()).to_string();
|
||||
let newline = if source.contains("\r\n") {
|
||||
"\r\n"
|
||||
} else {
|
||||
"\n"
|
||||
};
|
||||
let mut out = source.to_owned();
|
||||
match parsed.network {
|
||||
Some(network) => {
|
||||
if let Some(existing) = network.get_ref().get("dns_overrides") {
|
||||
// Parser byte spans cover the value, including multiline arrays,
|
||||
// but exclude the key, surrounding comments and sibling fields.
|
||||
out.replace_range(existing.span(), &value);
|
||||
} else {
|
||||
let span = network.span();
|
||||
let representation = &source[span.clone()];
|
||||
if representation.starts_with('{') {
|
||||
let separator = if network.get_ref().is_empty() {
|
||||
""
|
||||
} else {
|
||||
", "
|
||||
};
|
||||
out.insert_str(
|
||||
span.start + 1,
|
||||
&format!("dns_overrides = {value}{separator}"),
|
||||
);
|
||||
} else if representation.starts_with('[') {
|
||||
// The table span ends at ']', before any trailing comment.
|
||||
let end = source[span.end..]
|
||||
.find('\n')
|
||||
.map(|offset| span.end + offset + 1);
|
||||
if let Some(end) = end {
|
||||
out.insert_str(end, &format!("dns_overrides = {value}{newline}"));
|
||||
} else {
|
||||
out.push_str(&format!("{newline}dns_overrides = {value}{newline}"));
|
||||
}
|
||||
} else {
|
||||
// A dotted network key creates an implicit table at root.
|
||||
// Insert beside that declaration, not inside a later table.
|
||||
let start = source[..span.start]
|
||||
.rfind('\n')
|
||||
.map_or(0, |offset| offset + 1);
|
||||
out.insert_str(start, &format!("network.dns_overrides = {value}{newline}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if !out.is_empty() && !out.ends_with('\n') {
|
||||
out.push_str(newline);
|
||||
}
|
||||
out.push_str(&format!(
|
||||
"[network]{newline}dns_overrides = {value}{newline}"
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -248,7 +248,7 @@
|
||||
{"path":"logging.rotation","value":{"type":"string","enum":["never","minutely","hourly","daily","weekly"]},"default_kind":"type_default","sensitivity":"public","description":"Time rotation interval for file logs.","apply":{"watcher":"unsupported","reload":"process_restart","effect":"after_process_restart"}},
|
||||
{"path":"network","value":{"type":"object"},"default_kind":"nested_fields","sensitivity":"public","description":"Network binding, routing, and socket-level configuration.","apply":{"watcher":"mixed","reload":"mixed","effect":"see_child_fields"}},
|
||||
{"path":"network.cache_public_ip_path","value":{"type":"string"},"default_kind":"type_default","sensitivity":"deployment","description":"Cache file path for detected public IP.","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"}},
|
||||
{"path":"network.dns_overrides","value":{"type":"array","items":{"type":"string"}},"default_kind":"type_default","sensitivity":"public","description":"Runtime DNS overrides in `host:port:ip` format. IPv6 IP values must be bracketed: `[2001:db8::1]`.","apply":{"watcher":"supported","reload":"supported","effect":"future_resolutions_existing_connections_unchanged"}},
|
||||
{"path":"network.dns_overrides","value":{"type":"array","items":{"type":"string"},"maxItems":4096},"default_kind":"type_default","sensitivity":"public","description":"Runtime DNS overrides in `host:port:ip` format. IPv6 IP values must be bracketed: `[2001:db8::1]`.","apply":{"watcher":"supported","reload":"supported","effect":"future_resolutions_existing_connections_unchanged"}},
|
||||
{"path":"network.http_ip_detect_urls","value":{"type":"array","items":{"type":"string"}},"default_kind":"type_default","sensitivity":"deployment","description":"HTTP-based public IP detection endpoints (fallback after STUN).","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"}},
|
||||
{"path":"network.ipv4","value":{"type":"boolean"},"default_kind":"type_default","sensitivity":"public","description":"Configuration value for network.ipv4.","apply":{"watcher":"unsupported","reload":"conditional","effect":"listener_identity_and_bind_policy_dependent"}},
|
||||
{"path":"network.ipv6","value":{"type":"boolean"},"default_kind":"type_default","sensitivity":"public","description":"None = auto-detect IPv6 availability.","apply":{"watcher":"unsupported","reload":"conditional","effect":"listener_identity_and_bind_policy_dependent"}},
|
||||
|
||||
@@ -5,7 +5,9 @@ use std::sync::OnceLock;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::super::config_store::{EDITABLE_SECTIONS, EDITABLE_SERVER_FIELDS};
|
||||
use super::super::config_store::{
|
||||
EDITABLE_NETWORK_FIELDS, EDITABLE_SECTIONS, EDITABLE_SERVER_FIELDS,
|
||||
};
|
||||
use super::super::model::ApiFailure;
|
||||
use super::SCHEMA_VERSION;
|
||||
|
||||
@@ -199,6 +201,9 @@ fn default_at<'a>(value: &'a Value, path: &str) -> Option<&'a Value> {
|
||||
fn field_api(path: &str) -> FieldApi {
|
||||
let top = path.split(['.', '[']).next().unwrap_or("");
|
||||
let config_editable = EDITABLE_SECTIONS.contains(&top)
|
||||
|| path
|
||||
.strip_prefix("network.")
|
||||
.is_some_and(|field| EDITABLE_NETWORK_FIELDS.contains(&field))
|
||||
|| (top == "server"
|
||||
&& EDITABLE_SERVER_FIELDS.iter().any(|field| {
|
||||
let prefix = format!("server.{field}");
|
||||
@@ -248,7 +253,7 @@ fn field_api(path: &str) -> FieldApi {
|
||||
} else {
|
||||
api.non_editable_reason = Some(match top {
|
||||
"access" => "access_policy_is_file_only",
|
||||
"network" => "network_section_is_file_only",
|
||||
"network" => "network_field_not_in_config_api_allowlist",
|
||||
"server" => "server_field_not_in_config_api_allowlist",
|
||||
"logging" => "logging_section_is_file_only",
|
||||
"show_link" => "use_general_links_show",
|
||||
|
||||
@@ -138,7 +138,28 @@ fn api_ownership_aliases_and_null_contracts_match_existing_surfaces() {
|
||||
.api
|
||||
.config_write
|
||||
);
|
||||
assert!(!field(schema, "network.dns_overrides").api.config_read);
|
||||
assert!(field(schema, "network.dns_overrides").api.config_read);
|
||||
assert!(field(schema, "network.dns_overrides").api.config_write);
|
||||
assert_eq!(
|
||||
field(schema, "network.dns_overrides")
|
||||
.api
|
||||
.non_editable_reason,
|
||||
None
|
||||
);
|
||||
for path in [
|
||||
"network",
|
||||
"network.ipv4",
|
||||
"network.ipv6",
|
||||
"network.prefer",
|
||||
"network.stun_servers",
|
||||
] {
|
||||
assert!(!field(schema, path).api.config_read);
|
||||
assert!(!field(schema, path).api.config_write);
|
||||
assert_eq!(
|
||||
field(schema, path).api.non_editable_reason,
|
||||
Some("network_field_not_in_config_api_allowlist")
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
field(schema, "access.user_rate_limits.{username}.up_bps")
|
||||
.api
|
||||
|
||||
Reference in New Issue
Block a user