From b1d8f221d697f646b97493f09ef7d20a4635e2d1 Mon Sep 17 00:00:00 2001 From: Alexey <247128645+axkurcom@users.noreply.github.com> Date: Fri, 6 Nov 2026 22:37:15 +0300 Subject: [PATCH] API: add access policy management and source deny rules --- src/api/access_policy.rs | 350 +++++++++++ src/api/access_policy/tests.rs | 546 ++++++++++++++++++ src/api/config_edit/preflight.rs | 2 +- src/api/config_store.rs | 6 +- src/api/config_store/field_edits.rs | 152 +++++ src/api/config_store/persistence.rs | 78 ++- src/api/discovery/fields.json | 4 +- src/api/discovery/operations.json | 2 + src/api/discovery/schema.rs | 18 + src/api/discovery/schema/tests.rs | 8 +- src/api/handler.rs | 31 + src/api/handler/fixed_routes.rs | 2 +- src/api/handler/user_routes.rs | 8 +- src/api/mod.rs | 3 +- src/api/model/users.rs | 39 ++ src/api/users.rs | 9 +- src/api/users/create.rs | 32 +- src/api/users/source_deny.rs | 29 + src/api/users/source_deny_tests.rs | 393 +++++++++++++ src/api/users/update.rs | 6 +- src/api/users/view.rs | 83 +++ src/config/load/strict_keys.rs | 1 + .../tests/handshake_more_clever_tests.rs | 3 + .../tests/source_deny_handshake_tests.rs | 135 +++++ 24 files changed, 1885 insertions(+), 55 deletions(-) create mode 100644 src/api/access_policy.rs create mode 100644 src/api/access_policy/tests.rs create mode 100644 src/api/config_store/field_edits.rs create mode 100644 src/api/users/source_deny.rs create mode 100644 src/api/users/source_deny_tests.rs create mode 100644 src/proxy/tests/source_deny_handshake_tests.rs diff --git a/src/api/access_policy.rs b/src/api/access_policy.rs new file mode 100644 index 0000000..868116e --- /dev/null +++ b/src/api/access_policy.rs @@ -0,0 +1,350 @@ +//! Administrative access defaults and CIDR shaping; publication uses the watcher. + +use std::collections::{BTreeMap, HashMap, HashSet}; + +use serde::{Deserialize, Serialize}; + +use super::ApiShared; +use super::config_edit::{RuntimeBaseline, RuntimeFence}; +use super::config_store::{ + FieldEdit, compute_snapshot_revision, load_candidate_snapshot, load_config_snapshot, + prepare_field_edits, write_atomic_with_runtime_check, +}; +use super::model::ApiFailure; +use super::patch::{Patch, patch_field}; +use crate::config::{ + AccessConfig, CidrRateLimitKey, ProxyConfig, RateLimitBps, UserMaxUniqueIpsMode, +}; + +pub(super) const FIELDS: &[&str] = &[ + "user_max_tcp_conns_global_each", + "user_max_unique_ips_global_each", + "user_max_unique_ips_mode", + "user_max_unique_ips_window_secs", + "cidr_rate_limits", +]; + +#[derive(Debug, Serialize, PartialEq)] +pub(super) struct Policy { + user_max_tcp_conns_global_each: usize, + user_max_unique_ips_global_each: usize, + user_max_unique_ips_mode: UserMaxUniqueIpsMode, + user_max_unique_ips_window_secs: u64, + cidr_rate_limits: BTreeMap, +} + +impl From<&AccessConfig> for Policy { + fn from(access: &AccessConfig) -> Self { + Self { + user_max_tcp_conns_global_each: access.user_max_tcp_conns_global_each, + user_max_unique_ips_global_each: access.user_max_unique_ips_global_each, + user_max_unique_ips_mode: access.user_max_unique_ips_mode, + user_max_unique_ips_window_secs: access.user_max_unique_ips_window_secs, + cidr_rate_limits: access + .cidr_rate_limits + .iter() + .map(|(key, value)| (key.to_string(), *value)) + .collect(), + } + } +} + +#[derive(Debug, Serialize)] +pub(super) struct PolicyState { + desired: Policy, + effective: Policy, + runtime: RuntimeBaseline, + published_match: bool, + effective_scope: &'static str, + subsystem_convergence: &'static str, + apply_mode: &'static str, + #[serde(skip_serializing_if = "Option::is_none")] + changed_paths: Option>, +} + +fn view( + desired: &ProxyConfig, + fence: &RuntimeFence, + changed_paths: Option>, +) -> PolicyState { + let desired = Policy::from(&desired.access); + let effective = Policy::from(&fence.config.access); + PolicyState { + published_match: desired == effective, + desired, + effective, + runtime: fence.baseline(), + effective_scope: "active_config_snapshot", + subsystem_convergence: "unknown", + apply_mode: "watcher", + changed_paths, + } +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct PolicyPatch { + #[serde(default, deserialize_with = "patch_field")] + user_max_tcp_conns_global_each: Patch, + #[serde(default, deserialize_with = "patch_field")] + user_max_unique_ips_global_each: Patch, + #[serde(default, deserialize_with = "patch_field")] + user_max_unique_ips_mode: Patch, + #[serde(default, deserialize_with = "patch_field")] + user_max_unique_ips_window_secs: Patch, + #[serde(default, deserialize_with = "patch_field")] + cidr_rate_limits: Patch>>, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct RateInput { + #[serde(default)] + up_bps: u64, + #[serde(default)] + down_bps: u64, +} + +fn scalar( + field: &str, + patch: Patch, + current: &mut T, + default: T, + edits: &mut Vec, +) -> Result<(), ApiFailure> { + let value = match patch { + Patch::Unchanged => return Ok(()), + Patch::Remove => { + *current = default; + None + } + Patch::Set(value) => { + let rendered = toml::Value::try_from(&value).map_err(|_| { + ApiFailure::bad_request("Policy value cannot be represented in TOML") + })?; + *current = value; + Some(rendered) + } + }; + edits.push(FieldEdit::new(&["access", field], value)); + Ok(()) +} + +fn parse_key(key: &str) -> Result { + serde_json::from_value(serde_json::Value::String(key.to_owned())).map_err(|_| { + ApiFailure::bad_request("cidr_rate_limits keys must be CIDRs or *4/N, *6/N, */N templates") + }) +} + +fn apply( + loaded: &crate::config::ParsedConfigSource, + body: PolicyPatch, +) -> Result<(ProxyConfig, Vec), ApiFailure> { + let mut config = loaded.config.clone(); + let defaults = AccessConfig::default(); + let access = &mut config.access; + let mut edits = Vec::new(); + scalar( + "user_max_tcp_conns_global_each", + body.user_max_tcp_conns_global_each, + &mut access.user_max_tcp_conns_global_each, + defaults.user_max_tcp_conns_global_each, + &mut edits, + )?; + scalar( + "user_max_unique_ips_global_each", + body.user_max_unique_ips_global_each, + &mut access.user_max_unique_ips_global_each, + defaults.user_max_unique_ips_global_each, + &mut edits, + )?; + scalar( + "user_max_unique_ips_mode", + body.user_max_unique_ips_mode, + &mut access.user_max_unique_ips_mode, + defaults.user_max_unique_ips_mode, + &mut edits, + )?; + scalar( + "user_max_unique_ips_window_secs", + body.user_max_unique_ips_window_secs, + &mut access.user_max_unique_ips_window_secs, + defaults.user_max_unique_ips_window_secs, + &mut edits, + )?; + match body.cidr_rate_limits { + Patch::Unchanged => {} + Patch::Remove => { + access.cidr_rate_limits.clear(); + edits.push(FieldEdit::new(&["access", "cidr_rate_limits"], None)); + } + Patch::Set(changes) => { + let sources = loaded + .source_contents + .values() + .map(|source| { + toml::from_str::(source).map_err(|_| { + ApiFailure::new( + hyper::StatusCode::CONFLICT, + "config_patch_not_atomic", + "Invalid independent policy source", + ) + }) + }) + .collect::, _>>()?; + let mut authored: HashMap> = HashMap::new(); + for source in &sources { + if let Some(rows) = source + .get("access") + .and_then(|v| v.get("cidr_rate_limits")) + .and_then(toml::Value::as_table) + { + for raw in rows.keys() { + authored + .entry(parse_key(raw)?) + .or_default() + .push(raw.clone()); + } + } + } + let mut seen = HashSet::new(); + for (key, rate) in changes { + let parsed = parse_key(&key)?; + if !seen.insert(parsed.clone()) { + return Err(ApiFailure::bad_request( + "Duplicate normalized CIDR policy key", + )); + } + let mut names = authored.remove(&parsed).unwrap_or_default(); + if names.len() > 1 { + return Err(ApiFailure::new( + hyper::StatusCode::CONFLICT, + "config_patch_not_atomic", + "CIDR policy key has ambiguous source ownership", + )); + } + let raw_key = names.pop().unwrap_or_else(|| parsed.to_string()); + let value = if let Some(rate) = rate { + let rate = RateLimitBps { + up_bps: rate.up_bps, + down_bps: rate.down_bps, + }; + access.cidr_rate_limits.insert(parsed, rate); + Some( + toml::Value::try_from(rate) + .map_err(|_| ApiFailure::internal("Cannot render CIDR rate"))?, + ) + } else { + access.cidr_rate_limits.remove(&parsed); + None + }; + edits.push(FieldEdit::new( + &["access", "cidr_rate_limits", &raw_key], + value, + )); + } + } + } + if edits.is_empty() { + return Err(ApiFailure::bad_request("Empty access policy patch")); + } + config.validate().map_err(|_| ApiFailure::bad_request("Access policy violates configuration constraints; rates require at least one positive direction, at most 100000000000 bps, and unique auto templates; window_secs must be positive"))?; + Ok((config, edits)) +} + +pub(super) async fn read(shared: &ApiShared) -> Result<(PolicyState, String), ApiFailure> { + let loaded = load_config_snapshot(&shared.config_path, false) + .await + .map_err(|_| ApiFailure::internal("Cannot load access policy source"))?; + let fence = RuntimeFence::capture(shared, None)?; + Ok(( + view(&loaded.config, &fence, None), + compute_snapshot_revision(&loaded.source_contents), + )) +} + +pub(super) async fn patch( + body: PolicyPatch, + expected: Option, + runtime: Option, + shared: &ApiShared, +) -> Result<(PolicyState, String), ApiFailure> { + let shared = shared.clone(); + shared + .clone() + .run_mutation_completion(async move { + let _guard = shared.mutation_lock.lock().await; + let fence = RuntimeFence::capture(&shared, runtime.as_deref())?; + let path = shared.config_path.clone(); + let (loaded, desired, owned, changed) = tokio::task::spawn_blocking(move || { + let loaded = ProxyConfig::parse_source(&path) + .map_err(|_| ApiFailure::internal("Cannot load access policy source"))?; + let revision = compute_snapshot_revision(&loaded.source_contents); + if expected.is_some_and(|value| value != revision) { + return Err(ApiFailure::new( + hyper::StatusCode::CONFLICT, + "revision_conflict", + "Config revision mismatch", + )); + } + let (desired, edits) = apply(&loaded, body)?; + let owned = prepare_field_edits(&loaded, &path, &edits)?; + // Project only the five public policy fields, off the async executor. + let projection = |config: &ProxyConfig| { + serde_json::to_value(BTreeMap::from([("access", Policy::from(&config.access))])) + .map_err(|_| ApiFailure::internal("Cannot project access policy")) + }; + let changed = super::config_projection::changed_paths( + &projection(&loaded.config)?, + &projection(&desired)?, + ); + Ok::<_, ApiFailure>((loaded, desired, owned, changed)) + }) + .await + .map_err(|_| ApiFailure::internal("Policy preparation worker failed"))??; + let base_revision = compute_snapshot_revision(&loaded.source_contents); + let revision = if let Some(owned) = owned { + let candidate = load_candidate_snapshot( + &shared.config_path, + &loaded.source_contents, + owned.owner_path.clone(), + owned.owner_contents.clone(), + ) + .await + .map_err(|error| { + ApiFailure::new( + error.status, + error.code, + "Cannot prepare access policy candidate", + ) + })?; + if Policy::from(&candidate.config.access) != Policy::from(&desired.access) { + return Err(ApiFailure::internal( + "Persisted policy candidate differs from requested policy", + )); + } + fence.ensure_current()?; + write_atomic_with_runtime_check( + shared.config_path.clone(), + base_revision, + owned.owner_path, + owned.expected_owner_contents, + owned.owner_contents, + Some(fence.commit_check()), + ) + .await? + } else { + fence.ensure_current()?; + base_revision + }; + shared.runtime_events.record( + "api.access.policy.patch.ok", + format!("changed_paths={}", changed.len()), + ); + Ok((view(&desired, &fence, Some(changed)), revision)) + }) + .await +} + +#[cfg(test)] +mod tests; diff --git a/src/api/access_policy/tests.rs b/src/api/access_policy/tests.rs new file mode 100644 index 0000000..06eee7b --- /dev/null +++ b/src/api/access_policy/tests.rs @@ -0,0 +1,546 @@ +use super::*; +use crate::api::discovery::http_tests::Fixture; +use crate::maestro::generation::test_runtime_generation; +use serde_json::{Value, json}; + +const ROUTE: &str = "/v1/access/policy"; + +pub(in crate::api) fn fixture(source: &str) -> Fixture { + let fixture = Fixture::new(false); + std::fs::write(&fixture.shared.config_path, source).unwrap(); + let mut config = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + config.server.api = fixture + .shared + .active_runtime + .load() + .config() + .server + .api + .clone(); + config.server.api.read_only = false; + fixture + .shared + .active_runtime + .store(test_runtime_generation(1, config)); + fixture +} + +async fn write(fixture: &Fixture, body: Value) -> (u16, Value) { + let (status, _, data) = fixture + .request_body("PATCH", ROUTE, true, Some(&body), &[]) + .await; + (status, data) +} + +#[tokio::test] +async fn policy_http_gates_methods_queries_and_secret_exclusion() { + let fixture = Fixture::new(false); + for method in ["GET", "PATCH"] { + assert_eq!(fixture.request(method, ROUTE, false).await.0, 401); + } + assert_eq!( + write(&fixture, json!({"user_max_tcp_conns_global_each": 2})) + .await + .0, + 403 + ); + let (status, allow, _) = fixture.request("POST", ROUTE, true).await; + assert_eq!((status, allow.as_str()), (405, "GET, PATCH")); + assert_eq!( + fixture + .request("GET", "/v1/access/policy?reload=instant", true) + .await + .0, + 400 + ); + let denied = Fixture::new(true); + assert_eq!(denied.request("GET", ROUTE, true).await.0, 403); + let (status, _, data) = fixture.request("GET", ROUTE, true).await; + assert_eq!(status, 200); + assert_eq!( + data["data"]["desired"].as_object().unwrap().len(), + FIELDS.len() + ); + assert_eq!(data["data"]["effective_scope"], "active_config_snapshot"); + assert_eq!(data["data"]["subsystem_convergence"], "unknown"); + assert!(!data.to_string().contains("discovery-test")); +} + +#[tokio::test] +async fn scalar_defaults_reset_and_revision_runtime_fences() { + let fixture = fixture("# preserve comment\n"); + let (_, _, before) = fixture.request("GET", ROUTE, true).await; + let body = json!({"user_max_tcp_conns_global_each": 7, "user_max_unique_ips_global_each": 3, + "user_max_unique_ips_mode": "time_window", "user_max_unique_ips_window_secs": 91}); + let (status, _, after) = fixture + .request_body( + "PATCH", + ROUTE, + true, + Some(&body), + &[ + ("If-Match", before["revision"].as_str().unwrap()), + ( + "If-Runtime-Match", + before["data"]["runtime"]["runtime_match"].as_str().unwrap(), + ), + ], + ) + .await; + assert_eq!(status, 200, "{after}"); + assert_eq!( + after["data"]["desired"]["user_max_tcp_conns_global_each"], + 7 + ); + assert_eq!( + after["data"]["effective"]["user_max_tcp_conns_global_each"], + 0 + ); + assert_eq!(after["data"]["published_match"], false); + let source = std::fs::read(&fixture.shared.config_path).unwrap(); + for (header, token, code) in [ + ( + "If-Match", + before["revision"].as_str().unwrap(), + "revision_conflict", + ), + ("If-Runtime-Match", "dead:1:1", "runtime_conflict"), + ] { + let (status, _, error) = fixture + .request_body("PATCH", ROUTE, true, Some(&body), &[(header, token)]) + .await; + assert_eq!(status, 409); + assert_eq!(error["error"]["code"], code); + assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), source); + } + let (status, after) = write( + &fixture, + json!({"user_max_tcp_conns_global_each": null, + "user_max_unique_ips_global_each": null, "user_max_unique_ips_mode": null, + "user_max_unique_ips_window_secs": null}), + ) + .await; + assert_eq!(status, 200, "{after}"); + assert_eq!(after["data"]["desired"], before["data"]["desired"]); + let text = std::fs::read_to_string(&fixture.shared.config_path).unwrap(); + assert!(!text.contains("user_max_")); + assert!(text.contains("# preserve comment")); +} + +#[tokio::test] +async fn cidr_map_updates_replace_rows_and_null_removes_or_clears() { + let fixture = fixture("[access.cidr_rate_limits]\n'192.0.2.0/24'={up_bps=123, down_bps=456}\n"); + let (status, body) = write( + &fixture, + json!({"cidr_rate_limits": { + "192.0.2.0/24": {"up_bps": 8000}, "2001:db8::/32": {"down_bps": 9000}, + "*4/16": {"up_bps": 80}, "*6/64": {"down_bps": 160}, "*/24": {"up_bps": 320}}}), + ) + .await; + assert_eq!(status, 200, "{body}"); + let rates = &body["data"]["desired"]["cidr_rate_limits"]; + assert_eq!(rates.as_object().unwrap().len(), 5); + assert_eq!(rates["192.0.2.0/24"], json!({"up_bps": 8000,"down_bps": 0})); + let (status, body) = write( + &fixture, + json!({"cidr_rate_limits": {"192.0.2.0/24": null,"*4/16": null,"203.0.113.0/24": null}}), + ) + .await; + assert_eq!(status, 200, "{body}"); + assert_eq!( + body["data"]["desired"]["cidr_rate_limits"] + .as_object() + .unwrap() + .len(), + 3 + ); + let (status, body) = write(&fixture, json!({"cidr_rate_limits": null})).await; + assert_eq!(status, 200, "{body}"); + assert_eq!(body["data"]["desired"]["cidr_rate_limits"], json!({})); + assert!( + !std::fs::read_to_string(&fixture.shared.config_path) + .unwrap() + .contains("cidr_rate_limits") + ); +} + +#[tokio::test] +async fn invalid_and_non_policy_patches_never_write() { + let fixture = fixture(""); + let before = std::fs::read(&fixture.shared.config_path).unwrap(); + for body in [ + json!({}), + json!({"users": null}), + json!({"replay_check": false}), + json!({"user_max_unique_ips_window_secs":0}), + json!({"user_max_unique_ips_mode":"other"}), + json!({"user_max_tcp_conns_global_each":-1}), + json!({"user_max_tcp_conns_global_each":u64::MAX}), + json!({"cidr_rate_limits":[]}), + json!({"cidr_rate_limits":{"invalid":{"up_bps":1}}}), + json!({"cidr_rate_limits":{"*4/33":{"up_bps":1}}}), + json!({"cidr_rate_limits":{"*6/129":{"up_bps":1}}}), + json!({"cidr_rate_limits":{"*/33":{"up_bps":1}}}), + json!({"cidr_rate_limits":{"*/24":{"up_bps":1},"*4/24":{"up_bps":2}}}), + json!({"cidr_rate_limits":{"192.0.2.0/24":{}}}), + json!({"cidr_rate_limits":{"192.0.2.0/24":{"up_bps":100_000_000_001_u64}}}), + json!({"cidr_rate_limits":{"192.0.2.0/24":{"up_bps":1,"typo":null}}}), + ] { + let (status, error) = write(&fixture, body.clone()).await; + assert_eq!(status, 400, "{body}: {error}"); + assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), before); + } +} + +#[tokio::test] +async fn structural_edits_preserve_unrelated_values_and_literal_keys() { + for source in [ + "access = { user_max_tcp_conns_global_each=4, ignore_time_skew=false } # keep\n", + "access.user_max_tcp_conns_global_each=4\naccess.ignore_time_skew=false # keep\n", + "['access']\nuser_max_tcp_conns_global_each=4\nignore_time_skew=false # keep\n", + ] { + let fixture = fixture(source); + let (status, result) = write( + &fixture, + json!({"user_max_tcp_conns_global_each": null, + "cidr_rate_limits":{"192.0.2.0/24":{"up_bps":8000}}}), + ) + .await; + assert_eq!(status, 200, "{result}"); + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + assert!(!cfg.access.ignore_time_skew); + assert_eq!(cfg.access.cidr_rate_limits.len(), 1); + assert!( + std::fs::read_to_string(&fixture.shared.config_path) + .unwrap() + .contains("# keep") + ); + } +} + +#[tokio::test] +async fn include_owner_selection_and_cross_source_rejection() { + let fixture = fixture(""); + let root = &fixture.shared.config_path; + let include = root.with_file_name("policy.toml"); + let root_text = "include=\"policy.toml\"\n[access]\nuser_max_tcp_conns_global_each=5\n"; + let include_text = "[access.cidr_rate_limits]\n'192.0.2.0/24'={up_bps=8000}\n"; + std::fs::write(root, root_text).unwrap(); + std::fs::write(&include, include_text).unwrap(); + let (status, body) = write( + &fixture, + json!({"user_max_tcp_conns_global_each":null,"cidr_rate_limits":null}), + ) + .await; + assert_eq!(status, 409, "{body}"); + assert_eq!(body["error"]["code"], "config_patch_not_atomic"); + assert_eq!(std::fs::read_to_string(root).unwrap(), root_text); + assert_eq!(std::fs::read_to_string(&include).unwrap(), include_text); + let (status, body) = write( + &fixture, + json!({"cidr_rate_limits":{"2001:db8::/32":{"up_bps":16000}}}), + ) + .await; + assert_eq!(status, 200, "{body}"); + assert_eq!(std::fs::read_to_string(root).unwrap(), root_text); + assert!( + std::fs::read_to_string(include) + .unwrap() + .contains("2001:db8::/32") + ); +} + +#[tokio::test] +async fn concurrent_policy_writers_with_same_revision_have_one_winner() { + let fixture = fixture(""); + let (_, revision) = read(&fixture.shared).await.unwrap(); + let one = patch( + serde_json::from_value(json!({"user_max_tcp_conns_global_each":7})).unwrap(), + Some(revision.clone()), + None, + &fixture.shared, + ); + let two = patch( + serde_json::from_value(json!({"user_max_unique_ips_global_each":3})).unwrap(), + Some(revision), + None, + &fixture.shared, + ); + let (one, two) = tokio::join!(one, two); + assert_ne!(one.is_ok(), two.is_ok()); + let error = one.err().or(two.err()).unwrap(); + assert_eq!(error.code, "revision_conflict"); +} + +#[tokio::test] +async fn user_limit_origins_separate_desired_from_published_values() { + let fixture = fixture( + "[access]\nuser_max_tcp_conns_global_each=7\nuser_max_unique_ips_global_each=3\n[access.users]\nalice='0123456789abcdef0123456789abcdef'\n", + ); + for (override_value, origin, value) in [ + (json!(9), "user_override", json!(9)), + (json!(0), "global_default", json!(7)), + (Value::Null, "global_default", json!(7)), + ] { + let (status, _, body) = fixture + .request_body( + "PATCH", + "/v1/users/alice", + true, + Some(&json!({"max_tcp_conns":override_value,"max_unique_ips":override_value})), + &[], + ) + .await; + assert!([200, 202].contains(&status), "{body}"); + let desired = &body["data"]["limits"]["desired"]["max_tcp_conns"]; + assert_eq!(desired["origin"], origin); + assert_eq!(desired["value"], value); + assert_eq!(desired["configured_override"], override_value); + let ips = &body["data"]["limits"]["desired"]["max_unique_ips"]; + assert_eq!(ips["origin"], origin); + assert_eq!(ips["configured_override"], override_value); + assert_eq!( + ips["value"], + if origin == "user_override" { + json!(9) + } else { + json!(3) + } + ); + assert_eq!( + body["data"]["limits"]["effective"]["max_unique_ips"]["value"], + 3 + ); + assert_eq!( + body["data"]["limits"]["effective"]["max_tcp_conns"]["value"], + 7 + ); + } + assert_eq!( + write(&fixture, json!({"user_max_tcp_conns_global_each":0})) + .await + .0, + 200 + ); + let (_, _, body) = fixture.request("GET", "/v1/users/alice", true).await; + assert_eq!( + body["data"]["limits"]["desired"]["max_tcp_conns"]["origin"], + "unlimited" + ); + assert_eq!( + body["data"]["limits"]["desired"]["max_tcp_conns"]["value"], + Value::Null + ); + assert_eq!( + body["data"]["limits"]["effective"]["max_tcp_conns"]["value"], + 7 + ); +} + +#[tokio::test] +async fn persisted_cidr_policy_retains_precedence_units_and_unlimited_direction() { + use crate::proxy::traffic_limiter::{RateDirection, TrafficLimiter}; + let fixture = fixture(""); + let (status, body) = write( + &fixture, + json!({"cidr_rate_limits":{ + "192.0.2.0/24":{"up_bps":8000}, "192.0.2.7/32":{"up_bps":16000}, + "2001:db8::/32":{"up_bps":24000}, "2001:db8:1::/48":{"up_bps":32000}, + "*4/32":{"up_bps":40000}, "*6/64":{"up_bps":48000}, "*/8":{"up_bps":56000} + }}), + ) + .await; + assert_eq!(status, 200, "{body}"); + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + // A fresh bucket grants exactly one 20 ms epoch, expressed in bytes. + for (ip, grant) in [ + ("192.0.2.1", 20), + ("192.0.2.7", 40), + ("2001:db8::1", 60), + ("2001:db8:1::1", 80), + ("203.0.113.1", 100), + ("2001:db9::1", 120), + ] { + let limiter = TrafficLimiter::new(); + limiter.apply_policy(HashMap::new(), cfg.access.cidr_rate_limits.clone()); + let lease = limiter.acquire_lease("alice", ip.parse().unwrap()).unwrap(); + assert_eq!( + lease + .try_reserve(RateDirection::Up, 10_000) + .result() + .granted, + grant, + "{ip}" + ); + assert_eq!( + lease + .try_reserve(RateDirection::Down, 10_000) + .result() + .granted, + 10_000, + "{ip}" + ); + } + assert_eq!( + write( + &fixture, + json!({"cidr_rate_limits":{"*4/32":null,"*6/64":null}}) + ) + .await + .0, + 200 + ); + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + for ip in ["203.0.113.1", "2001:db9::1"] { + let limiter = TrafficLimiter::new(); + limiter.apply_policy(HashMap::new(), cfg.access.cidr_rate_limits.clone()); + let lease = limiter.acquire_lease("alice", ip.parse().unwrap()).unwrap(); + assert_eq!( + lease + .try_reserve(RateDirection::Up, 10_000) + .result() + .granted, + 140 + ); + } +} + +#[tokio::test] +async fn watcher_publishes_policy_without_quota_reset_or_session_cancellation() { + use crate::config::hot_reload::spawn_config_watcher; + use std::{sync::Arc, time::Duration}; + use tokio::sync::watch; + let mut fixture = fixture( + "[server.api]\nenabled=true\nread_only=false\nauth_header='Bearer discovery-test'\n[access.users]\nalice='0123456789abcdef0123456789abcdef'\n", + ); + let config = fixture.shared.active_runtime.load().config(); + let cancellation = tokio_util::sync::CancellationToken::new(); + let _cancel_on_drop = cancellation.clone().drop_guard(); + let (activation, activated) = watch::channel(false); + let (mut receiver, _, watcher) = spawn_config_watcher( + fixture.shared.config_path.clone(), + config.clone(), + None, + None, + cancellation.clone(), + None, + 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(); + runtime.stats.load_user_quota_state("alice", 12345, 6789); + runtime.stats.increment_user_curr_connects("alice"); + let stats = runtime.stats.clone(); + let quota_path = fixture.shared.quota_state.path().to_path_buf(); + Arc::get_mut(&mut fixture.shared).unwrap().quota_state = + crate::quota_state::QuotaStateOwner::new(quota_path.clone(), stats.quota_store()); + let checkpoint_before = std::fs::read("a_path).ok(); + fixture.shared.active_runtime.store(runtime); + let task = tokio::spawn(watcher); + for limit in [7, 2, 0] { + let (status, body) = write( + &fixture, + json!({"user_max_tcp_conns_global_each":limit,"user_max_unique_ips_global_each":limit}), + ) + .await; + assert_eq!(status, 200, "{body}"); + activation.send_replace(true); + tokio::time::timeout( + Duration::from_secs(5), + receiver.wait_for(|cfg| cfg.access.user_max_tcp_conns_global_each == limit), + ) + .await + .unwrap() + .unwrap(); + let (_, _, body) = fixture.request("GET", ROUTE, true).await; + assert_eq!(body["data"]["published_match"], true); + assert_eq!( + body["data"]["effective"]["user_max_unique_ips_global_each"], + limit + ); + assert_eq!(stats.get_user_quota_used("alice"), 12345); + assert_eq!( + stats.user_quota_snapshot()["alice"].last_reset_epoch_secs, + 6789 + ); + assert_eq!(stats.get_user_curr_connects("alice"), 1); + } + assert_eq!(std::fs::read("a_path).ok(), checkpoint_before); + cancellation.cancel(); + tokio::time::timeout(Duration::from_secs(5), task) + .await + .unwrap() + .unwrap(); +} + +#[tokio::test] +async fn prepared_policy_write_rechecks_external_source_and_runtime_publications() { + for runtime_change in [false, true] { + let fixture = fixture(""); + let fence = RuntimeFence::capture(&fixture.shared, None).unwrap(); + let loaded = ProxyConfig::parse_source(&fixture.shared.config_path).unwrap(); + let revision = compute_snapshot_revision(&loaded.source_contents); + let (_, edits) = apply( + &loaded, + serde_json::from_value(json!({"user_max_tcp_conns_global_each":7})).unwrap(), + ) + .unwrap(); + let owned = prepare_field_edits(&loaded, &fixture.shared.config_path, &edits) + .unwrap() + .unwrap(); + if runtime_change { + fixture + .shared + .active_runtime + .store(test_runtime_generation(2, (*fence.config).clone())); + } else { + std::fs::write(&fixture.shared.config_path, "# external operator edit\n").unwrap(); + } + let before = std::fs::read(&fixture.shared.config_path).unwrap(); + let error = write_atomic_with_runtime_check( + fixture.shared.config_path.clone(), + revision, + owned.owner_path, + owned.expected_owner_contents, + owned.owner_contents, + Some(fence.commit_check()), + ) + .await + .unwrap_err(); + assert_eq!( + error.code, + if runtime_change { + "runtime_conflict" + } else { + "revision_conflict" + } + ); + assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), before); + } +} + +#[tokio::test] +async fn unreadable_policy_source_does_not_echo_credentials_or_source_excerpts() { + let fixture = fixture(""); + let secret = "private-source-credential"; + std::fs::write( + &fixture.shared.config_path, + format!("[access]\nuser_max_tcp_conns_global_each='{secret}'\n"), + ) + .unwrap(); + for method in ["GET", "PATCH"] { + let (status, _, body) = fixture + .request_body( + method, + ROUTE, + true, + Some(&json!({"user_max_tcp_conns_global_each":1})), + &[], + ) + .await; + assert_eq!(status, 500); + assert!(!body.to_string().contains(secret)); + } +} diff --git a/src/api/config_edit/preflight.rs b/src/api/config_edit/preflight.rs index 329bfbf..bea5a7c 100644 --- a/src/api/config_edit/preflight.rs +++ b/src/api/config_edit/preflight.rs @@ -74,7 +74,7 @@ impl RuntimeFence { } } - pub(super) fn commit_check(&self) -> CommitCheck { + pub(in crate::api) fn commit_check(&self) -> CommitCheck { let fence = self.clone(); Arc::new(move || fence.matches()) } diff --git a/src/api/config_store.rs b/src/api/config_store.rs index 62f0c9e..14ee747 100644 --- a/src/api/config_store.rs +++ b/src/api/config_store.rs @@ -10,6 +10,7 @@ use super::model::ApiFailure; // Source-preserving TOML rendering and atomic persistence helpers. mod dns_overrides; +mod field_edits; mod persistence; // Compare-and-replace file persistence and metadata preservation. mod atomic; @@ -19,13 +20,14 @@ pub(in crate::api) use atomic::write_atomic; 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; +pub(in crate::api) use field_edits::{FieldEdit, prepare_field_edits}; #[cfg(test)] pub(in crate::api) use persistence::save_access_sections_to_disk; #[cfg(test)] use persistence::{find_toml_table_bounds, render_access_section, save_sections_to_disk}; pub(in crate::api) use persistence::{ - render_server_listeners, render_top_level_section, save_access_sections_to_disk_if_revision, - upsert_toml_table, + render_server_listeners, render_top_level_section, save_access_sections_and_fields_if_revision, + save_access_sections_to_disk_if_revision, upsert_toml_table, }; #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/src/api/config_store/field_edits.rs b/src/api/config_store/field_edits.rs new file mode 100644 index 0000000..852d594 --- /dev/null +++ b/src/api/config_store/field_edits.rs @@ -0,0 +1,152 @@ +//! Structural edits to allowlisted scalar fields and literal map keys. + +use super::*; +use toml_edit::{DocumentMut, Item, Table}; + +pub(in crate::api) struct FieldEdit { + pub path: Vec, + pub value: Option, +} + +impl FieldEdit { + pub fn new(path: &[&str], value: Option) -> Self { + Self { + path: path.iter().map(|part| (*part).to_owned()).collect(), + value, + } + } +} + +pub(in crate::api) struct OwnedSourceEdit { + pub owner_path: PathBuf, + pub expected_owner_contents: String, + pub owner_contents: String, +} + +fn conflict() -> ApiFailure { + ApiFailure::new( + hyper::StatusCode::CONFLICT, + "config_patch_not_atomic", + "Mutation requires one independently writable config source", + ) +} + +fn at_path<'a>(document: &'a DocumentMut, path: &[String]) -> Option<&'a Item> { + path.iter() + .try_fold(document.as_item(), |item, key| item.get(key)) +} + +fn edit(item: &mut Item, path: &[String], value: Option<&toml::Value>) -> Result<(), ApiFailure> { + let Some((key, rest)) = path.split_first() else { + return Err(ApiFailure::internal("Empty configuration edit path")); + }; + let table = item.as_table_like_mut().ok_or_else(conflict)?; + if rest.is_empty() { + if let Some(value) = value { + let mut value = value + .to_string() + .parse::() + .map_err(|_| ApiFailure::internal("Cannot render configuration value"))?; + if let Some(old) = table.get(key).and_then(Item::as_value) { + *value.decor_mut() = old.decor().clone(); + } + table.insert(key, Item::Value(value)); + } else { + table.remove(key); + } + } else { + if !table.contains_key(key) { + if value.is_none() { + return Ok(()); + } + table.insert(key, Item::Table(Table::new())); + } + edit(table.get_mut(key).ok_or_else(conflict)?, rest, value)?; + } + Ok(()) +} + +/// Exact leaf ownership wins; insertion uses the nearest existing parent. +/// Literal map keys are separate path components, including dots and CIDRs. +pub(in crate::api) fn prepare_field_edits( + loaded: &ParsedConfigSource, + config_path: &Path, + edits: &[FieldEdit], +) -> Result, ApiFailure> { + if edits.is_empty() { + return Ok(None); + } + if loaded + .source_contents + .values() + .any(|source| has_include_inside_table(source)) + { + return Err(conflict()); + } + let mut sources = loaded + .source_contents + .iter() + .map(|(path, source)| { + source + .parse::() + .map(|document| (path.clone(), document)) + .map_err(|_| conflict()) + }) + .collect::, _>>()?; + let mut owners = BTreeSet::new(); + let mut effective = Vec::new(); + for change in edits { + if change.path.is_empty() { + return Err(ApiFailure::internal("Empty configuration edit path")); + } + let mut found = BTreeSet::new(); + for length in (1..=change.path.len()).rev() { + found = sources + .iter() + .filter(|(_, document)| at_path(document, &change.path[..length]).is_some()) + .map(|(path, _)| path.clone()) + .collect(); + if !found.is_empty() { + break; + } + // Removing an absent field is a no-op, not an insertion into a parent. + if change.value.is_none() { + break; + } + } + if found.len() > 1 { + return Err(conflict()); + } + if found.is_empty() && change.value.is_none() { + continue; + } + owners.insert( + found + .into_iter() + .next() + .unwrap_or_else(|| normalize_source_path(config_path)), + ); + effective.push(change); + } + if effective.is_empty() { + return Ok(None); + } + if owners.len() != 1 { + return Err(conflict()); + } + let owner_path = owners.into_iter().next().ok_or_else(conflict)?; + let document = sources.get_mut(&owner_path).ok_or_else(conflict)?; + for change in effective { + edit(document.as_item_mut(), &change.path, change.value.as_ref())?; + } + let expected_owner_contents = loaded + .source_contents + .get(&owner_path) + .ok_or_else(conflict)? + .clone(); + Ok(Some(OwnedSourceEdit { + owner_path, + expected_owner_contents, + owner_contents: document.to_string(), + })) +} diff --git a/src/api/config_store/persistence.rs b/src/api/config_store/persistence.rs index 980b2c6..a20822f 100644 --- a/src/api/config_store/persistence.rs +++ b/src/api/config_store/persistence.rs @@ -12,8 +12,8 @@ use super::atomic::write_atomic_if_unchanged; #[cfg(test)] use super::compute_revision; use super::{ - AccessSection, compute_snapshot_revision, load_candidate_snapshot, load_config_snapshot, - resolve_single_source_owner, toml_path_exists, + AccessSection, FieldEdit, compute_snapshot_revision, load_candidate_snapshot, + load_config_snapshot, prepare_field_edits, resolve_single_source_owner, toml_path_exists, }; use crate::api::model::ApiFailure; @@ -112,6 +112,24 @@ pub(in crate::api) async fn save_access_sections_to_disk_if_revision( cfg: &ProxyConfig, sections: &[AccessSection], expected_revision: Option<&str>, +) -> Result { + save_access_sections_and_fields_if_revision( + config_path, + cfg, + sections, + Vec::new(), + expected_revision, + ) + .await +} + +/// Commits legacy user tables and precise source-deny edits in one owner file. +pub(in crate::api) async fn save_access_sections_and_fields_if_revision( + config_path: &Path, + cfg: &ProxyConfig, + sections: &[AccessSection], + fields: Vec, + expected_revision: Option<&str>, ) -> Result { let loaded = load_config_snapshot(config_path, false).await?; let loaded_revision = compute_snapshot_revision(&loaded.source_contents); @@ -133,7 +151,17 @@ pub(in crate::api) async fn save_access_sections_to_disk_if_revision( .is_some_and(|value| toml_path_exists(&value, section.table_name())) }) }); - if applied.is_empty() { + let compare_source_deny = !fields.is_empty(); + let precise = if fields.is_empty() { + None + } else { + let source = loaded.clone(); + let path = config_path.to_path_buf(); + tokio::task::spawn_blocking(move || prepare_field_edits(&source, &path, &fields)) + .await + .map_err(|_| ApiFailure::internal("Source edit worker failed"))?? + }; + if applied.is_empty() && precise.is_none() { return Ok(loaded_revision); } @@ -141,13 +169,31 @@ pub(in crate::api) async fn save_access_sections_to_disk_if_revision( .iter() .map(|section| section.table_name()) .collect::>(); - let owner_path = resolve_single_source_owner(&loaded, config_path, &targets)?; - let mut owner_contents = loaded + let table_owner = if targets.is_empty() { + None + } else { + Some(resolve_single_source_owner(&loaded, config_path, &targets)?) + }; + if let (Some(table_owner), Some(precise)) = (&table_owner, &precise) + && table_owner != &precise.owner_path + { + return Err(ApiFailure::new( + hyper::StatusCode::CONFLICT, + "config_patch_not_atomic", + "User tables and source-deny rule require different source owners", + )); + } + let owner_path = table_owner + .or_else(|| precise.as_ref().map(|edit| edit.owner_path.clone())) + .ok_or_else(|| ApiFailure::internal("Missing user mutation owner"))?; + let expected_owner_contents = loaded .source_contents .get(&owner_path) .cloned() .ok_or_else(|| ApiFailure::internal("config source owner is missing from snapshot"))?; - let expected_owner_contents = owner_contents.clone(); + let mut owner_contents = precise + .map(|edit| edit.owner_contents) + .unwrap_or_else(|| expected_owner_contents.clone()); for section in applied { let rendered = render_access_section(cfg, section)?; owner_contents = upsert_toml_table(&owner_contents, section.table_name(), &rendered); @@ -159,7 +205,25 @@ pub(in crate::api) async fn save_access_sections_to_disk_if_revision( owner_path.clone(), owner_contents.clone(), ) - .await?; + .await + .map_err(|error| { + if compare_source_deny { + ApiFailure::new( + error.status, + error.code, + "Cannot prepare user policy candidate", + ) + } else { + error + } + })?; + if compare_source_deny + && candidate.config.access.user_source_deny != cfg.access.user_source_deny + { + return Err(ApiFailure::internal( + "Persisted source-deny rules differ from requested policy", + )); + } let _candidate_revision = compute_snapshot_revision(&candidate.source_contents); let revision = write_atomic_if_unchanged( config_path.to_path_buf(), diff --git a/src/api/discovery/fields.json b/src/api/discovery/fields.json index 7e6e322..4d9af05 100644 --- a/src/api/discovery/fields.json +++ b/src/api/discovery/fields.json @@ -29,8 +29,8 @@ {"path":"access.user_rate_limits.{username}","value":{"type":"object"},"default_kind":"map_entry","sensitivity":"public","description":"Configuration value for access.user_rate_limits.{username}.","apply":{"watcher":"supported","reload":"supported","effect":"admission_or_accounting_policy_consumption_no_implicit_quota_reset"}}, {"path":"access.user_rate_limits.{username}.down_bps","value":{"type":"integer","minimum":0,"maximum":100000000000},"default_kind":"map_entry","sensitivity":"public","description":"Download limit in bits-per-second within `0..=100_000_000_000`; `0` means unlimited.","unit":"bits_per_second","apply":{"watcher":"supported","reload":"supported","effect":"admission_or_accounting_policy_consumption_no_implicit_quota_reset"},"constraints":["At least one direction must be nonzero; zero means unlimited for that direction."]}, {"path":"access.user_rate_limits.{username}.up_bps","value":{"type":"integer","minimum":0,"maximum":100000000000},"default_kind":"map_entry","sensitivity":"public","description":"Upload limit in bits-per-second within `0..=100_000_000_000`; `0` means unlimited.","unit":"bits_per_second","apply":{"watcher":"supported","reload":"supported","effect":"admission_or_accounting_policy_consumption_no_implicit_quota_reset"},"constraints":["At least one direction must be nonzero; zero means unlimited for that direction."]}, - {"path":"access.user_source_deny","value":{"type":"object","additionalProperties":{"type":"array","items":{"type":"string","format":"cidr"}}},"default_kind":"type_default","sensitivity":"public","description":"Per-username client source IP/CIDR deny list. Checked after successful authentication; matching IPs get the same rejection path as invalid auth (handshake fails closed for that connection).","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"},"constraints":["Current strict-key validation does not recognize access.user_source_deny; no watcher overlay."]}, - {"path":"access.user_source_deny.{username}","value":{"type":"array","items":{"type":"string","format":"cidr"}},"default_kind":"map_entry","sensitivity":"public","description":"Configuration value for access.user_source_deny.{username}.","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"},"constraints":["Current strict-key validation does not recognize access.user_source_deny; no watcher overlay."]}, + {"path":"access.user_source_deny","value":{"type":"object","additionalProperties":{"type":"array","items":{"type":"string","format":"cidr"}}},"default_kind":"type_default","sensitivity":"public","description":"Per-username client source IP/CIDR deny list. Checked after successful authentication; matching IPs get the same rejection path as invalid auth (handshake fails closed for that connection).","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"},"constraints":["Strict-key validation accepts this map. Generation reload is required; no watcher overlay. Deleting a user retains its rule."]}, + {"path":"access.user_source_deny.{username}","value":{"type":"array","items":{"type":"string","format":"cidr"}},"default_kind":"map_entry","sensitivity":"public","description":"Configuration value for access.user_source_deny.{username}.","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"},"constraints":["Strict-key validation accepts this map. Generation reload is required; no watcher overlay. Deleting a user retains its rule."]}, {"path":"access.users","value":{"type":"object","additionalProperties":{"type":"string"}},"default_kind":"redacted","sensitivity":"credential","description":"Configuration value for access.users.","apply":{"watcher":"supported","reload":"supported","effect":"admission_or_accounting_policy_consumption_no_implicit_quota_reset"}}, {"path":"access.users.{username}","value":{"type":"string","minLength":32,"maxLength":32,"pattern":"^[0-9A-Fa-f]{32}$"},"default_kind":"redacted","sensitivity":"credential","description":"Configuration value for access.users.{username}.","apply":{"watcher":"supported","reload":"supported","effect":"admission_or_accounting_policy_consumption_no_implicit_quota_reset"}}, {"path":"censorship","value":{"type":"object"},"default_kind":"nested_fields","sensitivity":"public","description":"Anti-censorship behavior and traffic shaping configuration.","apply":{"watcher":"unsupported","reload":"supported","effect":"new_generation_with_instant_or_drain_session_policy"}}, diff --git a/src/api/discovery/operations.json b/src/api/discovery/operations.json index 8dfdd7a..2711f47 100644 --- a/src/api/discovery/operations.json +++ b/src/api/discovery/operations.json @@ -40,6 +40,8 @@ {"id": "config_state","method": "GET","path": "/v1/config/state","gate": "always","success": [200]}, {"id": "config_validate","method": "POST","path": "/v1/config/validate","gate": "always","success": [200],"body": "PatchConfigRequest","if_match": true,"if_runtime_match": true,"read_only_operation": true}, {"id": "config_patch","if_runtime_match": true,"method": "PATCH","path": "/v1/config","gate": "always","success": [200,202],"body": "PatchConfigRequest","if_match": true}, + {"id": "access_policy_get","method": "GET","path": "/v1/access/policy","gate": "always","success": [200]}, + {"id": "access_policy_patch","method": "PATCH","path": "/v1/access/policy","gate": "always","success": [200],"body": "AccessPolicyPatch","if_match": true,"if_runtime_match": true}, {"id": "reload","method": "POST","path": "/v1/system/reload","gate": "always","success": [202],"body": "ReloadRequest","optional_body": true,"if_match": true}, {"id": "reload_status","method": "GET","path": "/v1/system/reload/{id}","gate": "always","success": [200]}, {"id": "users_list","method": "GET","path": "/v1/users","gate": "always","success": [200],"aliases": ["/v1/stats/users"],"credential_bearing": true}, diff --git a/src/api/discovery/schema.rs b/src/api/discovery/schema.rs index 82f782b..ad1789c 100644 --- a/src/api/discovery/schema.rs +++ b/src/api/discovery/schema.rs @@ -211,6 +211,13 @@ fn field_api(path: &str) -> FieldApi { || path.starts_with(&format!("{prefix}[")) || path.starts_with(&format!("{prefix}.")) })); + let policy_field = path.strip_prefix("access.").and_then(|path| { + let field = path.split('.').next()?; + crate::api::access_policy::FIELDS + .iter() + .copied() + .find(|candidate| *candidate == field) + }); let user_field = path .strip_prefix("access.") .and_then(|path| path.split('.').next()) @@ -223,6 +230,7 @@ fn field_api(path: &str) -> FieldApi { "user_data_quota" => Some("data_quota_bytes"), "user_quota_periods" => Some("quota_period"), "user_max_unique_ips" => Some("max_unique_ips"), + "user_source_deny" => Some("source_deny"), "user_rate_limits" if path.ends_with(".up_bps") => Some("rate_limit_up_bps"), "user_rate_limits" if path.ends_with(".down_bps") => Some("rate_limit_down_bps"), _ => None, @@ -240,6 +248,16 @@ fn field_api(path: &str) -> FieldApi { api.read_routes.push("GET /v1/config"); api.write_routes.push("PATCH /v1/config"); api.null_behavior = "ignored_member_then_validate_candidate"; + } else if let Some(field) = policy_field { + api.read_routes.push("GET /v1/access/policy"); + api.write_routes.push("PATCH /v1/access/policy"); + api.request_field = Some(field); + api.non_editable_reason = Some("managed_via_access_policy_api"); + api.null_behavior = if field == "cidr_rate_limits" { + "clear_map_or_remove_null_entries" + } else { + "remove_authored_value_restore_type_default" + }; } else if let Some(field) = user_field { api.read_routes.push("GET /v1/users/{username}"); api.write_routes diff --git a/src/api/discovery/schema/tests.rs b/src/api/discovery/schema/tests.rs index 69de87c..909ffcf 100644 --- a/src/api/discovery/schema/tests.rs +++ b/src/api/discovery/schema/tests.rs @@ -172,11 +172,9 @@ fn api_ownership_aliases_and_null_contracts_match_existing_surfaces() { .null_behavior, "remove_user_override_on_patch" ); - assert!( - field(schema, "access.user_source_deny") - .api - .write_routes - .is_empty() + assert_eq!( + field(schema, "access.user_source_deny").api.write_routes, + ["POST /v1/users", "PATCH /v1/users/{username}"] ); assert!( field(schema, "server.api.listen") diff --git a/src/api/handler.rs b/src/api/handler.rs index 21708f6..213d6b0 100644 --- a/src/api/handler.rs +++ b/src/api/handler.rs @@ -158,6 +158,37 @@ async fn dispatch( } match (method.as_str(), normalized_path) { + ("GET" | "PATCH", "/v1/access/policy") => { + if query.is_some_and(|query| !query.is_empty()) { + return Err(ApiFailure::bad_request( + "Access policy does not accept query parameters", + )); + } + let (data, revision) = if method == Method::GET { + access_policy::read(shared).await? + } else { + if cfg.server.api.read_only { + return Err(ApiFailure::new( + StatusCode::FORBIDDEN, + "read_only", + "API runs in read-only mode", + )); + } + let expected = parse_if_match(req.headers()); + let runtime = config_edit::parse_runtime_match(req.headers())?; + let body = + read_json::(req.into_body(), body_limit).await?; + access_policy::patch(body, expected, runtime, shared) + .await + .map_err(|error| { + shared + .runtime_events + .record("api.access.policy.patch.failed", error.code); + error + })? + }; + Ok(success_response(StatusCode::OK, data, revision)) + } ("POST", "/v1/users") => { fixed_routes::create_user_route(req, shared, cfg, config_rx, request_id, body_limit) .await diff --git a/src/api/handler/fixed_routes.rs b/src/api/handler/fixed_routes.rs index 7c1cdba..429092c 100644 --- a/src/api/handler/fixed_routes.rs +++ b/src/api/handler/fixed_routes.rs @@ -32,7 +32,7 @@ pub(super) async fn create_user_route( } }; let runtime_cfg = config_rx.borrow().clone(); - data.user.in_runtime = runtime_cfg.access.users.contains_key(&data.user.username); + users::refresh_runtime_view(&mut data.user, &runtime_cfg); shared.runtime_events.record( "api.user.create.ok", format!("username={}", data.user.username), diff --git a/src/api/handler/user_routes.rs b/src/api/handler/user_routes.rs index a5dfe3b..253e9d5 100644 --- a/src/api/handler/user_routes.rs +++ b/src/api/handler/user_routes.rs @@ -60,7 +60,7 @@ pub(super) async fn handle( } }; let runtime_cfg = config_rx.borrow().clone(); - data.in_runtime = runtime_cfg.access.users.contains_key(&data.username); + users::refresh_runtime_view(&mut data, &runtime_cfg); shared .runtime_events .record("api.user.enable.ok", format!("username={}", base_user)); @@ -102,7 +102,7 @@ pub(super) async fn handle( } }; let runtime_cfg = config_rx.borrow().clone(); - data.in_runtime = runtime_cfg.access.users.contains_key(&data.username); + users::refresh_runtime_view(&mut data, &runtime_cfg); shared .runtime_events .record("api.user.disable.ok", format!("username={}", base_user)); @@ -229,7 +229,7 @@ pub(super) async fn handle( } }; let runtime_cfg = config_rx.borrow().clone(); - data.user.in_runtime = runtime_cfg.access.users.contains_key(&data.user.username); + users::refresh_runtime_view(&mut data.user, &runtime_cfg); shared.runtime_events.record( "api.user.rotate_secret.ok", format!("username={}", base_user), @@ -293,7 +293,7 @@ pub(super) async fn handle( } }; let runtime_cfg = config_rx.borrow().clone(); - data.in_runtime = runtime_cfg.access.users.contains_key(&data.username); + users::refresh_runtime_view(&mut data, &runtime_cfg); shared .runtime_events .record("api.user.patch.ok", format!("username={}", data.username)); diff --git a/src/api/mod.rs b/src/api/mod.rs index 346ba42..029f15d 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -36,6 +36,7 @@ use crate::transport::middle_proxy::MePool; use crate::web::control::WebRuntimePublication; use crate::web::trace::WebTraceStore; +mod access_policy; mod config_edit; mod config_projection; mod config_state; @@ -306,7 +307,7 @@ fn allowed_methods_for_path(path: &str) -> Option<&'static str> { "/v1/system/reload" => Some(ALLOW_POST), "/v1/config/validate" => Some(ALLOW_POST), "/v1/users" => Some(ALLOW_GET_POST), - "/v1/config" => Some(ALLOW_GET_PATCH), + "/v1/config" | "/v1/access/policy" => Some(ALLOW_GET_PATCH), _ if user_action_route_matches(path, "/reset-quota") => Some(ALLOW_POST), _ if user_action_route_matches(path, "/rotate-secret") => Some(ALLOW_POST), _ if user_action_route_matches(path, "/enable") => Some(ALLOW_POST), diff --git a/src/api/model/users.rs b/src/api/model/users.rs index d2facfe..9596604 100644 --- a/src/api/model/users.rs +++ b/src/api/model/users.rs @@ -28,6 +28,9 @@ pub(in crate::api) struct UserInfo { pub(in crate::api) rate_limit_up_bps: Option, pub(in crate::api) rate_limit_down_bps: Option, pub(in crate::api) max_unique_ips: Option, + pub(in crate::api) limits: UserLimitState, + pub(in crate::api) source_deny: Vec, + pub(in crate::api) source_deny_status: SourceDenyState, pub(in crate::api) current_connections: u64, pub(in crate::api) active_unique_ips: usize, pub(in crate::api) active_unique_ips_list: Vec, @@ -37,6 +40,37 @@ pub(in crate::api) struct UserInfo { pub(in crate::api) links: UserLinks, } +#[derive(Serialize)] +pub(in crate::api) struct InheritedLimit { + pub(in crate::api) value: Option, + pub(in crate::api) configured_override: Option, + pub(in crate::api) global_value: usize, + pub(in crate::api) origin: &'static str, +} + +#[derive(Serialize)] +pub(in crate::api) struct UserLimitSnapshot { + pub(in crate::api) max_tcp_conns: InheritedLimit, + pub(in crate::api) max_unique_ips: InheritedLimit, +} + +#[derive(Serialize)] +pub(in crate::api) struct UserLimitState { + pub(in crate::api) desired: UserLimitSnapshot, + pub(in crate::api) effective: Option, + pub(in crate::api) effective_scope: &'static str, + pub(in crate::api) subsystem_convergence: &'static str, +} + +#[derive(Serialize)] +pub(in crate::api) struct SourceDenyState { + pub(in crate::api) effective: Option>, + pub(in crate::api) runtime_reload_required: Option, + pub(in crate::api) apply_mode: &'static str, + pub(in crate::api) reload_endpoint: &'static str, + pub(in crate::api) effective_scope: &'static str, +} + #[derive(Serialize)] pub(in crate::api) struct UserActiveIps { pub(in crate::api) username: String, @@ -94,6 +128,9 @@ pub(in crate::api) struct CreateUserRequest { pub(in crate::api) rate_limit_down_bps: Option, pub(in crate::api) max_unique_ips: Option, pub(in crate::api) enabled: Option, + /// Omission retains any orphan rule; null explicitly removes it on recreation. + #[serde(default, deserialize_with = "patch_field")] + pub(in crate::api) source_deny: Patch>, } #[derive(Deserialize)] @@ -118,6 +155,8 @@ pub(in crate::api) struct PatchUserRequest { pub(in crate::api) max_unique_ips: Patch, #[serde(default, deserialize_with = "patch_field")] pub(in crate::api) enabled: Patch, + #[serde(default, deserialize_with = "patch_field")] + pub(in crate::api) source_deny: Patch>, } #[derive(Default, Deserialize)] diff --git a/src/api/users.rs b/src/api/users.rs index bca151d..bf26069 100644 --- a/src/api/users.rs +++ b/src/api/users.rs @@ -11,7 +11,7 @@ use crate::stats::Stats; use super::ApiShared; use super::config_store::{ AccessSection, current_revision, load_config_for_mutation, - save_access_sections_to_disk_if_revision, + save_access_sections_and_fields_if_revision, save_access_sections_to_disk_if_revision, }; use super::model::{ ApiFailure, CreateUserRequest, CreateUserResponse, PatchUserRequest, RotateSecretRequest, @@ -24,17 +24,22 @@ use super::patch::Patch; mod create; mod lifecycle; mod links; +mod source_deny; mod update; mod view; pub(super) use create::create_user; pub(super) use lifecycle::{delete_user, rotate_secret}; use links::{build_user_links, empty_user_links}; +use source_deny::apply_source_deny; pub(super) use update::{patch_user, set_user_enabled}; -pub(super) use view::{build_user_quota_list, users_from_config}; +pub(super) use view::{build_user_quota_list, refresh_runtime_view, users_from_config}; #[cfg(test)] mod tests; #[cfg(test)] mod period_tests; + +#[cfg(test)] +mod source_deny_tests; diff --git a/src/api/users/create.rs b/src/api/users/create.rs index cebf8d2..b02efa8 100644 --- a/src/api/users/create.rs +++ b/src/api/users/create.rs @@ -119,6 +119,7 @@ async fn create_user_to_completion( .user_quota_periods .insert(body.username.clone(), period); } + let source_deny_edits = apply_source_deny(&mut cfg, &body.username, body.source_deny)?; cfg.validate() .map_err(|e| ApiFailure::bad_request(format!("config validation failed: {}", e)))?; @@ -154,10 +155,11 @@ async fn create_user_to_completion( .prepare_user_creation_locked(&body.username, quota_guard) .await .map_err(|error| ApiFailure::internal(format!("failed to prepare user quota: {error}")))?; - let revision = save_access_sections_to_disk_if_revision( + let revision = save_access_sections_and_fields_if_revision( &shared.config_path, &cfg, &touched_sections, + source_deny_edits, Some(&base_revision), ) .await?; @@ -196,33 +198,7 @@ async fn create_user_to_completion( let user = users .into_iter() .find(|entry| entry.username == body.username) - .unwrap_or(UserInfo { - username: body.username.clone(), - enabled: cfg.access.is_user_enabled(&body.username), - in_runtime: false, - user_ad_tag: None, - max_tcp_conns: cfg - .access - .user_max_tcp_conns - .get(&body.username) - .copied() - .filter(|limit| *limit > 0) - .or((cfg.access.user_max_tcp_conns_global_each > 0) - .then_some(cfg.access.user_max_tcp_conns_global_each)), - expiration_rfc3339: None, - data_quota_bytes: None, - quota_period: body.quota_period, - rate_limit_up_bps: body.rate_limit_up_bps.filter(|limit| *limit > 0), - rate_limit_down_bps: body.rate_limit_down_bps.filter(|limit| *limit > 0), - max_unique_ips: updated_limit, - current_connections: 0, - active_unique_ips: 0, - active_unique_ips_list: Vec::new(), - recent_unique_ips: 0, - recent_unique_ips_list: Vec::new(), - total_octets: 0, - links: build_user_links(&cfg, &secret, detected_ip_v4, detected_ip_v6), - }); + .ok_or_else(|| ApiFailure::internal("Created user is missing from config view"))?; Ok((CreateUserResponse { user, secret }, revision)) } diff --git a/src/api/users/source_deny.rs b/src/api/users/source_deny.rs new file mode 100644 index 0000000..d36239e --- /dev/null +++ b/src/api/users/source_deny.rs @@ -0,0 +1,29 @@ +use super::*; +use crate::api::config_store::FieldEdit; + +/// Source-deny intent changes only desired TOML; generation admission remains untouched. +pub(super) fn apply_source_deny( + cfg: &mut ProxyConfig, + username: &str, + patch: Patch>, +) -> Result, ApiFailure> { + let value = match patch { + Patch::Unchanged => return Ok(Vec::new()), + Patch::Remove => { + cfg.access.user_source_deny.remove(username); + None + } + Patch::Set(nets) => { + let value = toml::Value::try_from(&nets) + .map_err(|_| ApiFailure::internal("Cannot render source-deny list"))?; + cfg.access + .user_source_deny + .insert(username.to_owned(), nets); + Some(value) + } + }; + Ok(vec![FieldEdit::new( + &["access", "user_source_deny", username], + value, + )]) +} diff --git a/src/api/users/source_deny_tests.rs b/src/api/users/source_deny_tests.rs new file mode 100644 index 0000000..8be854b --- /dev/null +++ b/src/api/users/source_deny_tests.rs @@ -0,0 +1,393 @@ +use super::*; +use crate::api::discovery::http_tests::Fixture; +use crate::maestro::generation::test_runtime_generation; +use serde_json::{Value, json}; +use std::sync::Arc; + +const USERS: &str = "[general]\nconfig_strict=true\n[access.users]\nalice='0123456789abcdef0123456789abcdef'\nbob='abcdef0123456789abcdef0123456789'\n"; + +fn fixture(extra: &str) -> Fixture { + let fixture = Fixture::new(false); + std::fs::write(&fixture.shared.config_path, format!("{USERS}{extra}")).unwrap(); + publish(&fixture, 1); + fixture +} + +fn publish(fixture: &Fixture, generation: u64) { + let mut config = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + config.server.api = fixture + .shared + .active_runtime + .load() + .config() + .server + .api + .clone(); + config.server.api.read_only = false; + fixture + .shared + .active_runtime + .store(test_runtime_generation(generation, config)); +} + +async fn patch(fixture: &Fixture, username: &str, body: Value) -> Value { + let (status, _, result) = fixture + .request_body( + "PATCH", + &format!("/v1/users/{username}"), + true, + Some(&body), + &[], + ) + .await; + assert!([200, 202].contains(&status), "{result}"); + result +} + +#[tokio::test] +async fn source_deny_strict_create_get_replace_clear_and_reload_status() { + let fixture = fixture(""); + let (status, _, created) = fixture + .request_body( + "POST", + "/v1/users", + true, + Some(&json!({ + "username":"alice.two", "source_deny":["192.0.2.0/24","2001:db8::/32"]})), + &[], + ) + .await; + assert_eq!(status, 202, "{created}"); + assert_eq!( + created["data"]["user"]["source_deny_status"]["runtime_reload_required"], + true + ); + assert_eq!( + created["data"]["user"]["source_deny_status"]["reload_endpoint"], + "POST /v1/system/reload" + ); + let (_, _, get) = fixture.request("GET", "/v1/users/alice.two", true).await; + assert_eq!( + get["data"]["source_deny"], + json!(["192.0.2.0/24", "2001:db8::/32"]) + ); + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + assert!( + cfg.access + .is_user_source_ip_denied("alice.two", "192.0.2.12".parse().unwrap()) + ); + assert!( + cfg.access + .is_user_source_ip_denied("alice.two", "2001:db8::12".parse().unwrap()) + ); + assert!( + !cfg.access + .is_user_source_ip_denied("alice.two", "198.51.100.12".parse().unwrap()) + ); + publish(&fixture, 2); + let (_, _, get) = fixture.request("GET", "/v1/users/alice.two", true).await; + assert_eq!( + get["data"]["source_deny_status"]["runtime_reload_required"], + false + ); + for value in [json!(["203.0.113.0/24"]), json!([]), Value::Null] { + let body = patch(&fixture, "alice.two", json!({"source_deny":value})).await; + assert_eq!( + body["data"]["source_deny_status"]["runtime_reload_required"], + true + ); + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + assert_eq!( + cfg.access.user_source_deny.contains_key("alice.two"), + !value.is_null() + ); + assert_eq!( + body["data"]["source_deny"], + if value.is_null() { json!([]) } else { value } + ); + } +} + +#[tokio::test] +async fn source_deny_delete_retains_rules_and_recreation_can_remove_them_explicitly() { + let fixture = fixture( + "[access.user_source_deny]\nalice=['192.0.2.0/24']\nbob=['2001:db8::/32'] # retain bob\n", + ); + for clear in [false, true] { + let (status, _, deleted) = fixture.request("DELETE", "/v1/users/alice", true).await; + assert!([200, 202].contains(&status), "{deleted}"); + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + assert_eq!(cfg.access.user_source_deny["alice"].len(), 1); + let mut body = json!({"username":"alice"}); + if clear { + body["source_deny"] = Value::Null; + } + let (status, _, created) = fixture + .request_body("POST", "/v1/users", true, Some(&body), &[]) + .await; + assert!([201, 202].contains(&status), "{created}"); + let user = &created["data"]["user"]; + assert_eq!( + user["source_deny"], + if clear { + json!([]) + } else { + json!(["192.0.2.0/24"]) + } + ); + assert_eq!(user["source_deny_status"]["runtime_reload_required"], clear); + assert!( + std::fs::read_to_string(&fixture.shared.config_path) + .unwrap() + .contains("bob=['2001:db8::/32'] # retain bob") + ); + } +} + +#[tokio::test] +async fn source_deny_edits_exact_included_owner_and_rejects_cross_file_user_patch() { + let fixture = fixture(""); + let root = &fixture.shared.config_path; + let include = root.with_file_name("deny.toml"); + let root_text = format!("include = \"deny.toml\"\n{USERS}"); + let include_text = "[access.user_source_deny]\nalice=['192.0.2.0/24']\n'bob'= ['2001:db8::/32'] # keep literal\n"; + std::fs::write(root, &root_text).unwrap(); + std::fs::write(&include, include_text).unwrap(); + patch( + &fixture, + "alice", + json!({"source_deny":["198.51.100.0/24"]}), + ) + .await; + assert_eq!(std::fs::read_to_string(root).unwrap(), root_text); + assert!( + std::fs::read_to_string(&include) + .unwrap() + .contains("'bob'= ['2001:db8::/32'] # keep literal") + ); + let before = std::fs::read(&include).unwrap(); + let (status, _, error) = fixture + .request_body( + "PATCH", + "/v1/users/alice", + true, + Some(&json!({"secret":"11111111111111111111111111111111","source_deny":null})), + &[], + ) + .await; + assert_eq!(status, 409, "{error}"); + assert_eq!(error["error"]["code"], "config_patch_not_atomic"); + assert_eq!(std::fs::read(root).unwrap(), root_text.as_bytes()); + assert_eq!(std::fs::read(&include).unwrap(), before); + patch(&fixture, "alice", json!({"source_deny":null})).await; + assert!( + !ProxyConfig::load(root) + .unwrap() + .access + .user_source_deny + .contains_key("alice") + ); + assert!( + ProxyConfig::load(root) + .unwrap() + .access + .user_source_deny + .contains_key("bob") + ); +} + +#[tokio::test] +async fn source_deny_literal_username_supports_inline_dotted_and_quoted_sources() { + for declaration in [ + "access.user_source_deny = { 'alice.two'=['192.0.2.0/24'], bob=['2001:db8::/32'] } # keep\n", + "access.user_source_deny.'alice.two'=['192.0.2.0/24']\naccess.user_source_deny.bob=['2001:db8::/32'] # keep\n", + "[access.user_source_deny]\n'alice.two'=['192.0.2.0/24']\nbob=['2001:db8::/32'] # keep\n", + ] { + let fixture = fixture(""); + let text = format!("{declaration}{}", USERS.replace("alice=", "'alice.two'=")); + std::fs::write(&fixture.shared.config_path, text).unwrap(); + patch( + &fixture, + "alice.two", + json!({"source_deny":["203.0.113.0/24"]}), + ) + .await; + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + assert_eq!(cfg.access.user_source_deny.len(), 2); + assert_eq!( + cfg.access.user_source_deny["alice.two"][0].to_string(), + "203.0.113.0/24" + ); + patch(&fixture, "alice.two", json!({"source_deny":null})).await; + let cfg = ProxyConfig::load(&fixture.shared.config_path).unwrap(); + assert_eq!(cfg.access.user_source_deny.len(), 1); + assert_eq!( + cfg.access.user_source_deny["bob"][0].to_string(), + "2001:db8::/32" + ); + assert!( + std::fs::read_to_string(&fixture.shared.config_path) + .unwrap() + .contains("# keep") + ); + } +} + +#[tokio::test] +async fn source_deny_invalid_values_and_stale_revision_leave_source_unchanged() { + let fixture = fixture(""); + let before = std::fs::read(&fixture.shared.config_path).unwrap(); + for value in [ + json!("192.0.2.0/24"), + json!([1]), + json!(["192.0.2.0/33"]), + json!(["2001:db8::/129"]), + json!(["bad"]), + json!([null]), + ] { + let (status, _, error) = fixture + .request_body( + "PATCH", + "/v1/users/alice", + true, + Some(&json!({"source_deny":value})), + &[], + ) + .await; + assert_eq!(status, 400, "{error}"); + assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), before); + } + let (status, _, error) = fixture + .request_body( + "PATCH", + "/v1/users/alice", + true, + Some(&json!({"source_deny":[]})), + &[("If-Match", "stale")], + ) + .await; + assert_eq!(status, 409, "{error}"); + assert_eq!(error["error"]["code"], "revision_conflict"); + assert_eq!(std::fs::read(&fixture.shared.config_path).unwrap(), before); +} + +#[tokio::test] +async fn source_deny_prepared_reload_retains_old_rules_until_a_later_generation() { + let fixture = fixture(""); + let (control, mut commands) = crate::maestro::reload::ReloadControl::channel(1); + let (accepted, _) = crate::api::submit_reload_from_disk( + &fixture.shared.config_path, + &fixture.shared.mutation_lock, + &control, + None, + crate::maestro::reload::ReloadRequest::default(), + ) + .await + .unwrap(); + let captured_for_reload = (*commands.recv().await.unwrap().config).clone(); + let old_runtime = fixture.shared.active_runtime.load_full(); + let admission = old_runtime.proxy_shared.clone(); + let credential = credential_id_from_hex(&captured_for_reload.access.users["alice"]).unwrap(); + let incarnation = admission + .stage_user_credential("alice", credential, true) + .incarnation; + let registration = admission + .register_authenticated_user_session("alice", credential) + .unwrap(); + // The generation being prepared has already captured source before the API write. + let written = patch(&fixture, "alice", json!({"source_deny":["192.0.2.0/24"]})).await; + assert_ne!(written["revision"], accepted.config_revision); + assert!(!registration.is_cancelled()); + assert_eq!(registration.incarnation(), incarnation); + let mut intermediate = captured_for_reload; + intermediate.server.api = old_runtime.config().server.api.clone(); + fixture + .shared + .active_runtime + .store(test_runtime_generation(2, intermediate)); + let (_, _, view) = fixture.request("GET", "/v1/users/alice", true).await; + assert_eq!(view["data"]["source_deny_status"]["effective"], json!([])); + assert_eq!( + view["data"]["source_deny_status"]["runtime_reload_required"], + true + ); + publish(&fixture, 3); + let (_, _, view) = fixture.request("GET", "/v1/users/alice", true).await; + assert_eq!( + view["data"]["source_deny_status"]["runtime_reload_required"], + false + ); + assert!( + !old_runtime + .config() + .access + .is_user_source_ip_denied("alice", "192.0.2.1".parse().unwrap()) + ); + assert!(!registration.is_cancelled()); +} + +#[tokio::test] +async fn source_deny_watcher_retains_rules_and_preserves_quota_usage() { + use crate::config::hot_reload::spawn_config_watcher; + use std::time::Duration; + use tokio::sync::watch; + let mut fixture = fixture( + "[server.api]\nenabled=true\nread_only=false\nauth_header='Bearer discovery-test'\n", + ); + let config = fixture.shared.active_runtime.load().config(); + let cancellation = tokio_util::sync::CancellationToken::new(); + let _cancel_on_drop = cancellation.clone().drop_guard(); + let (activation, activated) = watch::channel(false); + let (mut receiver, _, watcher) = spawn_config_watcher( + fixture.shared.config_path.clone(), + config.clone(), + None, + None, + cancellation.clone(), + None, + 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(); + runtime.stats.load_user_quota_state("alice", 12345, 6789); + let stats = runtime.stats.clone(); + let quota_path = fixture.shared.quota_state.path().to_path_buf(); + Arc::get_mut(&mut fixture.shared).unwrap().quota_state = + crate::quota_state::QuotaStateOwner::new(quota_path.clone(), stats.quota_store()); + let checkpoint_before = std::fs::read("a_path).ok(); + fixture.shared.active_runtime.store(runtime); + let task = tokio::spawn(watcher); + patch( + &fixture, + "alice", + json!({"source_deny":["192.0.2.0/24"],"max_tcp_conns":7}), + ) + .await; + activation.send_replace(true); + tokio::time::timeout( + Duration::from_secs(5), + receiver.wait_for(|cfg| cfg.access.user_max_tcp_conns.get("alice") == Some(&7)), + ) + .await + .unwrap() + .unwrap(); + assert!(receiver.borrow().access.user_source_deny.is_empty()); + let (_, _, view) = fixture.request("GET", "/v1/users/alice", true).await; + assert_eq!( + view["data"]["source_deny_status"]["runtime_reload_required"], + true + ); + assert_eq!(stats.get_user_quota_used("alice"), 12345); + assert_eq!( + stats.user_quota_snapshot()["alice"].last_reset_epoch_secs, + 6789 + ); + assert_eq!(std::fs::read("a_path).ok(), checkpoint_before); + cancellation.cancel(); + tokio::time::timeout(Duration::from_secs(5), task) + .await + .unwrap() + .unwrap(); +} diff --git a/src/api/users/update.rs b/src/api/users/update.rs index 6111118..08ad43b 100644 --- a/src/api/users/update.rs +++ b/src/api/users/update.rs @@ -165,6 +165,7 @@ async fn patch_user_to_completion( } } + let source_deny_edits = apply_source_deny(&mut cfg, user, body.source_deny)?; cfg.validate() .map_err(|e| ApiFailure::bad_request(format!("config validation failed: {}", e)))?; let staged_credential = @@ -210,13 +211,14 @@ async fn patch_user_to_completion( touched_sections.push(AccessSection::UserEnabled); } - let revision = if touched_sections.is_empty() { + let revision = if touched_sections.is_empty() && source_deny_edits.is_empty() { current_revision(&shared.config_path).await? } else { - save_access_sections_to_disk_if_revision( + save_access_sections_and_fields_if_revision( &shared.config_path, &cfg, &touched_sections, + source_deny_edits, Some(&base_revision), ) .await? diff --git a/src/api/users/view.rs b/src/api/users/view.rs index 98094a2..acd5b2f 100644 --- a/src/api/users/view.rs +++ b/src/api/users/view.rs @@ -1,4 +1,64 @@ use super::*; +use crate::api::model::{InheritedLimit, SourceDenyState, UserLimitSnapshot, UserLimitState}; + +fn inherited_limit(configured_override: Option, global_value: usize) -> InheritedLimit { + let positive_override = configured_override.filter(|value| *value > 0); + InheritedLimit { + value: positive_override.or((global_value > 0).then_some(global_value)), + configured_override, + global_value, + origin: if positive_override.is_some() { + "user_override" + } else if global_value > 0 { + "global_default" + } else { + "unlimited" + }, + } +} + +fn limit_snapshot(cfg: &ProxyConfig, username: &str) -> UserLimitSnapshot { + UserLimitSnapshot { + max_tcp_conns: inherited_limit( + cfg.access.user_max_tcp_conns.get(username).copied(), + cfg.access.user_max_tcp_conns_global_each, + ), + max_unique_ips: inherited_limit( + cfg.access.user_max_unique_ips.get(username).copied(), + cfg.access.user_max_unique_ips_global_each, + ), + } +} + +pub(in crate::api) fn refresh_runtime_view(user: &mut UserInfo, runtime: &ProxyConfig) { + user.in_runtime = runtime.access.users.contains_key(&user.username); + user.limits.effective = user + .in_runtime + .then(|| limit_snapshot(runtime, &user.username)); + user.source_deny_status = source_deny_state(&user.username, &user.source_deny, Some(runtime)); +} + +fn source_deny_state( + username: &str, + desired: &[ipnetwork::IpNetwork], + runtime: Option<&ProxyConfig>, +) -> SourceDenyState { + // Retained rules can apply to a recreated user even before its membership is published. + let effective = runtime.map(|cfg| { + cfg.access + .user_source_deny + .get(username) + .cloned() + .unwrap_or_default() + }); + SourceDenyState { + runtime_reload_required: effective.as_ref().map(|nets| nets.as_slice() != desired), + effective, + apply_mode: "generation_reload", + reload_endpoint: "POST /v1/system/reload", + effective_scope: "request_generation_snapshot", + } +} pub(in crate::api) async fn users_from_config( cfg: &ProxyConfig, @@ -72,6 +132,29 @@ pub(in crate::api) async fn users_from_config( .filter(|limit| *limit > 0) .or((cfg.access.user_max_unique_ips_global_each > 0) .then_some(cfg.access.user_max_unique_ips_global_each)), + limits: UserLimitState { + desired: limit_snapshot(cfg, &username), + effective: runtime_cfg + .filter(|runtime| runtime.access.users.contains_key(&username)) + .map(|runtime| limit_snapshot(runtime, &username)), + effective_scope: "active_config_snapshot", + subsystem_convergence: "unknown", + }, + source_deny: cfg + .access + .user_source_deny + .get(&username) + .cloned() + .unwrap_or_default(), + source_deny_status: source_deny_state( + &username, + cfg.access + .user_source_deny + .get(&username) + .map(Vec::as_slice) + .unwrap_or_default(), + runtime_cfg, + ), current_connections: stats.get_process_user_curr_connects(&username), active_unique_ips: active_ip_list.len(), active_unique_ips_list: active_ip_list, diff --git a/src/config/load/strict_keys.rs b/src/config/load/strict_keys.rs index 72e519e..29b57a2 100644 --- a/src/config/load/strict_keys.rs +++ b/src/config/load/strict_keys.rs @@ -452,6 +452,7 @@ const TLS_FETCH_CONFIG_KEYS: &[&str] = &[ const ACCESS_CONFIG_KEYS: &[&str] = &[ "users", + "user_source_deny", "user_enabled", "user_ad_tags", "user_max_tcp_conns", diff --git a/src/proxy/tests/handshake_more_clever_tests.rs b/src/proxy/tests/handshake_more_clever_tests.rs index 4910115..c915809 100644 --- a/src/proxy/tests/handshake_more_clever_tests.rs +++ b/src/proxy/tests/handshake_more_clever_tests.rs @@ -746,3 +746,6 @@ fn ipv6_localhost_and_unspecified_normalization() { assert_eq!(norm_local, expected_bucket); assert_eq!(norm_unspec, expected_bucket); } + +#[path = "source_deny_handshake_tests.rs"] +mod source_deny_tests; diff --git a/src/proxy/tests/source_deny_handshake_tests.rs b/src/proxy/tests/source_deny_handshake_tests.rs new file mode 100644 index 0000000..9cd6000 --- /dev/null +++ b/src/proxy/tests/source_deny_handshake_tests.rs @@ -0,0 +1,135 @@ +use super::*; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +fn persisted_source_deny_config() -> ProxyConfig { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("strict.toml"); + std::fs::write(&path,"[general]\nconfig_strict=true\n[general.modes]\nclassic=true\nsecure=true\ntls=true\n[access]\nignore_time_skew=true\n[access.users]\nuser='11111111111111111111111111111111'\n[access.user_source_deny]\nuser=['192.0.2.0/24','2001:db8::/32']\n").unwrap(); + ProxyConfig::load(&path).unwrap() +} + +#[tokio::test] +async fn source_deny_mtproto_preserves_authenticated_and_invalid_auth_fallback_paths() { + let config = persisted_source_deny_config(); + for peer in [ + "192.0.2.1:1234", + "[2001:db8::1]:1234", + "198.51.100.1:1234", + "[2001:db9::1]:1234", + ] { + let peer: SocketAddr = peer.parse().unwrap(); + let denied = config.access.is_user_source_ip_denied("user", peer.ip()); + for (secret, valid) in [ + ("11111111111111111111111111111111", true), + ("22222222222222222222222222222222", false), + ] { + for tag in [ProtoTag::Abridged, ProtoTag::Secure] { + let bytes = make_valid_mtproto_handshake(secret, tag, 2); + let original = bytes; + let replay = ReplayChecker::new(64, Duration::from_secs(60)); + let (mut output, writer) = tokio::io::duplex(4096); + let result = handle_mtproto_handshake( + &bytes, + &b"tail"[..], + writer, + peer, + &config, + &replay, + false, + None, + ) + .await; + if valid && !denied { + assert!( + matches!(result, HandshakeResult::Success(_)), + "{peer}: valid authentication must succeed" + ); + } else { + let HandshakeResult::BadClient { + mut reader, + mut writer, + } = result + else { + panic!( + "{peer}: denied and invalid authentication must return the fallback streams" + ); + }; + let mut tail = [0; 4]; + reader.read_exact(&mut tail).await.unwrap(); + assert_eq!(&tail, b"tail"); + writer.write_all(b"fallback").await.unwrap(); + drop(writer); + let mut written = Vec::new(); + output.read_to_end(&mut written).await.unwrap(); + assert_eq!( + &written, b"fallback", + "no protocol response before fallback" + ); + } + assert_eq!(bytes, original); + } + } + } +} + +#[tokio::test] +async fn source_deny_tls_preserves_client_hello_and_returns_unmodified_fallback_streams() { + let config = persisted_source_deny_config(); + for peer in [ + "192.0.2.1:1234", + "[2001:db8::1]:1234", + "198.51.100.1:1234", + "[2001:db9::1]:1234", + ] { + let peer: SocketAddr = peer.parse().unwrap(); + let denied = config.access.is_user_source_ip_denied("user", peer.ip()); + for (secret, valid) in [([0x11; 16], true), ([0x22; 16], false)] { + let bytes = make_valid_tls_handshake(&secret, 0); + let original = bytes.clone(); + let replay = ReplayChecker::new(64, Duration::from_secs(60)); + let (mut output, writer) = tokio::io::duplex(8192); + let result = handle_tls_handshake( + &bytes, + &b"tail"[..], + writer, + peer, + &config, + &replay, + &SecureRandom::new(), + None, + ) + .await; + if valid && !denied { + assert!( + matches!(result, HandshakeResult::Success(_)), + "{peer}: valid TLS authentication must succeed" + ); + } else { + let HandshakeResult::BadClient { + mut reader, + mut writer, + } = result + else { + panic!( + "{peer}: denied and invalid TLS authentication must return the fallback streams" + ); + }; + let mut tail = [0; 4]; + reader.read_exact(&mut tail).await.unwrap(); + assert_eq!(&tail, b"tail"); + writer.write_all(b"fallback").await.unwrap(); + drop(writer); + let mut written = Vec::new(); + output.read_to_end(&mut written).await.unwrap(); + assert_eq!( + &written, b"fallback", + "no ServerHello or alert before fallback" + ); + } + assert_eq!( + bytes, original, + "the original ClientHello must reach fallback unchanged" + ); + } + } +}