Skip to content

Commit abc4bcf

Browse files
authored
fix(cli): the operator's host name falls back to HOSTNAME only when it needs to (#645)
An invalid `HOSTNAME` (say `HOSTNAME=$'\xff'`) failed every CLI command, even on Linux where `/proc/sys/kernel/hostname` already named the host. And an empty host name file recorded the host as `unknown` without trying `HOSTNAME`. Both came from `Operator::terminal` reading `HOSTNAME` eagerly in `.or(…)` and dropping an empty name only after the fallback. A new function, `Operator::host_name` (file contents, lazy `HOSTNAME` read) → host, now reads `HOSTNAME` only when the file names no host: the file is missing or unreadable, or it is empty after trimming. An invalid `HOSTNAME` still fails a command when it is needed, and the host is `unknown` when neither names one. Its unit test fails on `main` in both ways and passes with the fix. Closes #636 > [!NOTE] > Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus 5.5 (via T3 Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 1b7dbda commit abc4bcf

1 file changed

Lines changed: 48 additions & 7 deletions

File tree

‎apps/auth-service/src/admin/operator.rs‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const OPERATOR: Variable = Variable::new("AUTH_OPERATOR");
2121
/// The operating system user, who runs commands when neither `--operator` nor
2222
/// `AUTH_OPERATOR` says who does.
2323
const OS_USER: Variable = Variable::new("USER");
24-
/// The machine's host name, for systems without `/proc`.
24+
/// The machine's host name, for systems without `/proc` or where its host name
25+
/// is blank.
2526
const HOST_NAME: Variable = Variable::new("HOSTNAME");
2627
/// Where Linux exposes the host name.
2728
const HOST_NAME_FILE: &str = "/proc/sys/kernel/hostname";
@@ -118,14 +119,27 @@ impl Operator {
118119
/// `name` running a command at this terminal, on this host, or `unknown` where
119120
/// its name cannot be read.
120121
pub(crate) fn terminal(name: OperatorName) -> Result<Self, ConfigurationError> {
121-
let host = std::fs::read_to_string(HOST_NAME_FILE)
122-
.ok()
123-
.map(|host| host.trim().to_owned())
124-
.or(HOST_NAME.optional()?)
125-
.filter(|host| !host.is_empty());
122+
let file = std::fs::read_to_string(HOST_NAME_FILE).ok();
126123
Ok(Self::Cli {
127124
name,
128-
host: host.unwrap_or_else(|| UNKNOWN.to_owned()),
125+
host: Self::host_name(file, || HOST_NAME.optional())?,
126+
})
127+
}
128+
129+
/// The host's name: `file`'s, the host name file's contents if it could be
130+
/// read, else `fallback`'s, which runs only when `file` names none, so an
131+
/// invalid `HOSTNAME` fails a command only when the host name file names no
132+
/// host; `unknown` when neither names one. Whitespace around a name is no part of it.
133+
fn host_name(
134+
file: Option<String>,
135+
fallback: impl FnOnce() -> Result<Option<String>, ConfigurationError>,
136+
) -> Result<String, ConfigurationError> {
137+
let named = |host: String| Some(host.trim().to_owned()).filter(|host| !host.is_empty());
138+
Ok(match file.and_then(named) {
139+
Some(host) => host,
140+
None => fallback()?
141+
.and_then(named)
142+
.unwrap_or_else(|| UNKNOWN.to_owned()),
129143
})
130144
}
131145

@@ -346,3 +360,30 @@ impl AdminSession {
346360
.map_err(AdminError::StepUpRequired)
347361
}
348362
}
363+
364+
#[cfg(test)]
365+
mod tests {
366+
use super::{ConfigurationError, HOST_NAME, Operator, UNKNOWN};
367+
368+
/// The host for a host name file of `file` and a `HOSTNAME` that reads as `env`.
369+
fn host(
370+
file: Option<&str>,
371+
env: Result<Option<&str>, ConfigurationError>,
372+
) -> Result<String, ConfigurationError> {
373+
Operator::host_name(file.map(str::to_owned), || {
374+
env.map(|v| v.map(str::to_owned))
375+
})
376+
}
377+
378+
/// The file's name wins without reading `HOSTNAME`, which may then be invalid;
379+
/// without one, `HOSTNAME` names the host, and fails when invalid.
380+
#[test]
381+
fn host_name_reads_hostname_only_without_a_name_in_the_file() {
382+
let invalid = || Err(HOST_NAME.invalid("must contain UTF-8 text"));
383+
assert_eq!(host(Some("box\n"), invalid()).expect("the file's"), "box");
384+
assert_eq!(host(Some(" \n"), Ok(Some("env"))).expect("HOSTNAME"), "env");
385+
assert_eq!(host(None, Ok(Some("env"))).expect("HOSTNAME"), "env");
386+
assert_eq!(host(Some(""), Ok(None)).expect("no name"), UNKNOWN);
387+
assert!(host(None, invalid()).is_err());
388+
}
389+
}

0 commit comments

Comments
 (0)