Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/bin/fleet-connect-udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
use std::process::ExitCode;

fn main() -> ExitCode {
// The argument surface comes first (#6134): --help/-h print
// the usage and exit 0, and any other argument is a usage
// error — both without loading config or binding anything,
// so an accidental argument vector can never start the
// endpoint. Bare invocation is unchanged.
let argv: Vec<String> = std::env::args().skip(1).collect();
match fleet_proxy::connect_udp::classify_bin_args(&argv) {
fleet_proxy::connect_udp::BinArgs::Help => {
println!("{}", fleet_proxy::connect_udp::CONNECT_UDP_USAGE);
return ExitCode::SUCCESS;
}
fleet_proxy::connect_udp::BinArgs::UsageError(arg) => {
eprintln!("fleet-connect-udp: unknown argument {arg:?}");
eprintln!("{}", fleet_proxy::connect_udp::CONNECT_UDP_USAGE);
return ExitCode::from(fleet_proxy::EXIT_USAGE as u8);
}
fleet_proxy::connect_udp::BinArgs::Run => {}
}
let config = match fleet_config::Config::load() {
Ok(c) => c,
Err(e) => {
Expand Down
75 changes: 75 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/connect_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,48 @@ pub fn serve(listener: TcpListener) {
}
}

/// The `fleet-connect-udp` usage text (#6134), printed to
/// stdout by the bin help intercept at exit 0 and to stderr
/// after a usage error. The bin takes no arguments at all:
/// behaviour is governed entirely by the loaded fleet config.
pub const CONNECT_UDP_USAGE: &str = "usage: fleet-connect-udp

The standalone CONNECT-UDP endpoint (RFC 9298). Takes no
arguments: whether the endpoint serves, and what it binds,
is governed by the loaded fleet config ($FLEET_CONFIG, else
~/.config/fleet/fleet.toml) — it serves only when that config
opts in with [proxy] connect_udp = true, and stays dark
(exit 69) otherwise.";

/// What the standalone bin should do with its argument vector,
/// decided before any config is loaded or anything is bound
/// (#6134, the #5488/#5568 class: a --help or a typo must never
/// start a network endpoint).
#[derive(Debug, PartialEq, Eq)]
pub enum BinArgs {
/// No arguments: run the endpoint (bare behaviour unchanged).
Run,
/// -h/--help present: print the usage to stdout, exit 0.
Help,
/// Any other argument: a usage error naming that argument,
/// exit EXIT_USAGE (2).
UsageError(String),
}

/// Classify the bin argument vector. A help flag anywhere wins
/// (the `run_init` precedent, #5619); otherwise the first
/// argument — any argument, since the bin takes none — is the
/// usage error.
pub fn classify_bin_args(argv: &[String]) -> BinArgs {
if argv.iter().any(|a| matches!(a.as_str(), "-h" | "--help")) {
return BinArgs::Help;
}
match argv.first() {
None => BinArgs::Run,
Some(first) => BinArgs::UsageError(first.clone()),
}
}

/// The serve-side entry: bind per config and serve. A bind
/// failure is returned for the caller to log — an opt-in spike
/// reports its own failure and never takes the broker down
Expand Down Expand Up @@ -738,6 +780,39 @@ mod tests {
use super::*;
use std::io::ErrorKind;

#[test]
fn bin_args_help_usage_error_and_bare() {
// #6134: the three argument shapes are pinned before any
// config load or bind can happen — help succeeds, an
// unknown argument is a usage error naming it, and bare
// runs the endpoint exactly as before.
let strings = |list: &[&str]| list.iter().map(|s| s.to_string()).collect::<Vec<_>>();
for flag in ["--help", "-h"] {
assert_eq!(
classify_bin_args(&strings(&[flag])),
BinArgs::Help,
"{flag}"
);
}
assert_eq!(classify_bin_args(&strings(&[])), BinArgs::Run);
assert_eq!(
classify_bin_args(&strings(&["--bogus-junk"])),
BinArgs::UsageError("--bogus-junk".to_string())
);
assert_eq!(
classify_bin_args(&strings(&["extra"])),
BinArgs::UsageError("extra".to_string())
);
// Help anywhere wins, even alongside another argument.
assert_eq!(
classify_bin_args(&strings(&["--bogus-junk", "--help"])),
BinArgs::Help
);
// The usage states the contract the help shape promises.
assert!(CONNECT_UDP_USAGE.contains("Takes no"));
assert!(CONNECT_UDP_USAGE.contains("connect_udp = true"));
}

#[test]
fn varint_round_trip() {
for value in [
Expand Down