Skip to content

Commit ebe99a4

Browse files
jsibbison-squareImplementor
andauthored
fix(relay): fail startup on invalid operator listener config (#7933)
Addresses point 3 of [previous comment](#7793 (review)) "3. Fail fast on a bad BUZZ_OPERATOR_LISTENERS (config.rs:857)" ## Summary - Propagate malformed `BUZZ_OPERATOR_LISTENERS` values from `Config::from_env` as `ConfigError`, including invalid UTF-8. - Update startup config tests to assert returned errors and restore the original admin warning capture helper, removing the shared helper introduced only for listener log assertions. - Cover valid and absent listener values through `Config::from_env`. ## Context Follow-up to the review of #7793 specifically [reverting some config logging parts](https://github.com/block/buzz/pull/7793/changes#diff-eaa0eeb209ff1ad6212ffa56d3a983bb732858647c9fb227ef7483a3363b5a71). Invalid listener configuration currently logs an error and silently disables listener delivery. ## Verification - `cargo fmt --all -- --check` passed with Hermit. Signed-off-by: Implementor <691cca7a870db1dad6990d5938d1b4a2a2ca9647ca81290b536c17b06b2e473f@buzz.block.builderlab.xyz> Co-authored-by: Implementor <691cca7a870db1dad6990d5938d1b4a2a2ca9647ca81290b536c17b06b2e473f@buzz.block.builderlab.xyz>
1 parent 1846094 commit ebe99a4

1 file changed

Lines changed: 70 additions & 64 deletions

File tree

‎crates/buzz-relay/src/config.rs‎

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,12 @@ impl Config {
855855
}
856856

857857
let operator_listener_delivery_urls = match std::env::var("BUZZ_OPERATOR_LISTENERS") {
858-
Ok(raw) => match parse_operator_listener_delivery_urls(&raw) {
859-
Ok(urls) => urls,
860-
Err(parse_error) => {
861-
error!(
862-
error = %parse_error,
863-
"invalid BUZZ_OPERATOR_LISTENERS; operator-listener mention delivery is disabled"
864-
);
865-
HashMap::new()
866-
}
867-
},
858+
Ok(raw) => parse_operator_listener_delivery_urls(&raw)?,
868859
Err(std::env::VarError::NotPresent) => HashMap::new(),
869860
Err(error) => {
870-
error!(
871-
error = %error,
872-
"BUZZ_OPERATOR_LISTENERS must be valid UTF-8; operator-listener mention delivery is disabled"
873-
);
874-
HashMap::new()
861+
return Err(ConfigError::InvalidValue(format!(
862+
"BUZZ_OPERATOR_LISTENERS must be valid UTF-8: {error}"
863+
)));
875864
}
876865
};
877866
if !operator_listener_delivery_urls.is_empty() && relay_operator_api_origin.is_none() {
@@ -1542,8 +1531,14 @@ mod tests {
15421531
config
15431532
}
15441533

1545-
/// Capture the tracing output emitted during `Config::from_env()`.
1546-
fn capture_config_logs() -> (Result<Config, ConfigError>, String) {
1534+
/// Like `config_with_admin_env`, but also captures the tracing output
1535+
/// emitted during `Config::from_env()` so a test can assert the startup
1536+
/// warning fired. The `BUZZ_ADMIN_TOKEN` warning is the sole behavioral
1537+
/// value of retaining the guards (the variable is otherwise inert), so it
1538+
/// must be regression-protected: deleting a warn block has to fail a test.
1539+
fn config_with_admin_env_capturing_logs(
1540+
values: &[(&str, Option<&str>)],
1541+
) -> (Result<Config, ConfigError>, String) {
15471542
use std::sync::{Arc, Mutex};
15481543

15491544
#[derive(Clone)]
@@ -1578,49 +1573,19 @@ mod tests {
15781573
})
15791574
.with_ansi(false)
15801575
.finish();
1581-
let config = tracing::subscriber::with_default(subscriber, Config::from_env);
1576+
let config =
1577+
tracing::subscriber::with_default(subscriber, || config_with_admin_env(values));
15821578
let captured = String::from_utf8(buf.lock().unwrap().clone()).unwrap_or_default();
15831579
(config, captured)
15841580
}
15851581

1586-
/// Like `config_with_admin_env`, but also captures the tracing output
1587-
/// emitted during `Config::from_env()` so a test can assert the startup
1588-
/// warning fired. The `BUZZ_ADMIN_TOKEN` warning is the sole behavioral
1589-
/// value of retaining the guards (the variable is otherwise inert), so it
1590-
/// must be regression-protected: deleting a warn block has to fail a test.
1591-
fn config_with_admin_env_capturing_logs(
1592-
values: &[(&str, Option<&str>)],
1593-
) -> (Result<Config, ConfigError>, String) {
1594-
const KEYS: [&str; 3] = ["BUZZ_ADMIN_HOST", "BUZZ_ADMIN_TOKEN", "BUZZ_ADMIN_AUTH"];
1595-
let previous: Vec<_> = KEYS
1596-
.iter()
1597-
.map(|key| (*key, std::env::var_os(key)))
1598-
.collect();
1599-
for key in KEYS {
1600-
std::env::remove_var(key);
1601-
}
1602-
for (key, value) in values {
1603-
match value {
1604-
Some(value) => std::env::set_var(key, value),
1605-
None => std::env::remove_var(key),
1606-
}
1607-
}
1608-
let result = capture_config_logs();
1609-
for (key, value) in previous {
1610-
match value {
1611-
Some(value) => std::env::set_var(key, value),
1612-
None => std::env::remove_var(key),
1613-
}
1614-
}
1615-
result
1616-
}
1617-
1618-
fn config_with_operator_listeners_capturing_logs(
1619-
raw: &str,
1620-
) -> (Result<Config, ConfigError>, String) {
1582+
fn config_with_operator_listeners(raw: Option<&str>) -> Result<Config, ConfigError> {
16211583
let previous = std::env::var_os("BUZZ_OPERATOR_LISTENERS");
1622-
std::env::set_var("BUZZ_OPERATOR_LISTENERS", raw);
1623-
let result = capture_config_logs();
1584+
match raw {
1585+
Some(value) => std::env::set_var("BUZZ_OPERATOR_LISTENERS", value),
1586+
None => std::env::remove_var("BUZZ_OPERATOR_LISTENERS"),
1587+
}
1588+
let result = Config::from_env();
16241589
match previous {
16251590
Some(value) => std::env::set_var("BUZZ_OPERATOR_LISTENERS", value),
16261591
None => std::env::remove_var("BUZZ_OPERATOR_LISTENERS"),
@@ -2475,18 +2440,59 @@ mod tests {
24752440
}
24762441

24772442
#[test]
2478-
fn malformed_operator_listener_config_is_logged_and_disables_delivery() {
2443+
fn malformed_operator_listener_config_prevents_startup() {
24792444
let _guard = ENV_MUTEX.lock().unwrap();
2480-
let (result, logs) = config_with_operator_listeners_capturing_logs(";");
2481-
let config = result.expect("malformed listener config must not stop relay startup");
2445+
let result = config_with_operator_listeners(Some(";"));
24822446

2483-
assert!(config.operator_listener_delivery_urls.is_empty());
2484-
assert!(logs.contains("ERROR"), "expected an ERROR line: {logs:?}");
2485-
assert!(logs.contains("BUZZ_OPERATOR_LISTENERS"), "logs: {logs:?}");
2486-
assert!(
2487-
logs.contains("operator-listener mention delivery is disabled"),
2488-
"logs: {logs:?}"
2447+
assert!(matches!(
2448+
result,
2449+
Err(ConfigError::InvalidValue(ref message))
2450+
if message.contains("BUZZ_OPERATOR_LISTENERS")
2451+
));
2452+
}
2453+
2454+
#[cfg(unix)]
2455+
#[test]
2456+
fn non_utf8_operator_listener_config_prevents_startup() {
2457+
use std::os::unix::ffi::OsStringExt;
2458+
2459+
let _guard = ENV_MUTEX.lock().unwrap();
2460+
let previous = std::env::var_os("BUZZ_OPERATOR_LISTENERS");
2461+
std::env::set_var(
2462+
"BUZZ_OPERATOR_LISTENERS",
2463+
std::ffi::OsString::from_vec(vec![0xff]),
24892464
);
2465+
2466+
let result = Config::from_env();
2467+
2468+
match previous {
2469+
Some(value) => std::env::set_var("BUZZ_OPERATOR_LISTENERS", value),
2470+
None => std::env::remove_var("BUZZ_OPERATOR_LISTENERS"),
2471+
}
2472+
2473+
assert!(matches!(
2474+
result,
2475+
Err(ConfigError::InvalidValue(ref message))
2476+
if message.contains("BUZZ_OPERATOR_LISTENERS must be valid UTF-8")
2477+
));
2478+
}
2479+
2480+
#[test]
2481+
fn valid_and_absent_operator_listener_config_preserve_routes() {
2482+
let _guard = ENV_MUTEX.lock().unwrap();
2483+
let key = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
2484+
let raw = format!("{key}:https://listener.example/mentions");
2485+
let config = config_with_operator_listeners(Some(&raw)).expect("valid listener config");
2486+
assert_eq!(
2487+
config
2488+
.operator_listener_delivery_urls
2489+
.get(key)
2490+
.map(url::Url::as_str),
2491+
Some("https://listener.example/mentions")
2492+
);
2493+
2494+
let config = config_with_operator_listeners(None).expect("absent listener config");
2495+
assert!(config.operator_listener_delivery_urls.is_empty());
24902496
}
24912497

24922498
#[test]

0 commit comments

Comments
 (0)