Watch
0
0
Fork
You've already forked hyperhive
0

cut off hives that left the roster: forge token, team seat, token secret, login role

This commit is contained in:
atlas 2026-11-16 21:48:41 +01:00 • committed by mara
commit 11fec06a50

View file

@ -91,19 +91,51 @@ pub(crate) struct ApiDoc;
/// (this daemon has no "self" to exclude) rather than peers-minus-self.
const HIVES_ENV: &str = "SWARM_CONTROLLER_HIVES";
/// How [`load_hives`] read [`HIVES_ENV`].
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum HiveRoster {
Loaded(Vec<HiveEntry>),
/// [`HIVES_ENV`] is not set.
Unset,
/// [`HIVES_ENV`] is set and is not a hive directory.
Unparseable,
}
impl HiveRoster {
pub(crate) fn into_entries(self) -> Vec<HiveEntry> {
match self {
Self::Loaded(hives) => hives,
Self::Unset | Self::Unparseable => Vec::new(),
}
}
/// Whether an absence from the roster can be read as a removal: the roster
/// loaded and holds a hive. An unset, unparseable or empty roster reads
/// the same as "every hive removed", so it is refused with the reason.
pub(crate) fn absence_means_removal(&self) -> Result<(), &'static str> {
match self {
Self::Loaded(hives) if !hives.is_empty() => Ok(()),
Self::Loaded(_) => Err("the hive roster is empty"),
Self::Unset => Err("the hive roster env var is not set"),
Self::Unparseable => Err("the hive roster env var does not parse"),
}
}
}
/// Parses [`HIVES_ENV`] into the swarm's hive directory. Unset or
/// unparseable both fall back to an empty list with a warning rather than
/// failing startup — a swarm-controller that can't yet see its own config
/// (dev run, a module not wired up yet) should still serve `/health`.
pub(crate) fn load_hives() -> Vec<HiveEntry> {
/// unparseable both serve an empty list rather than failing startup — a
/// swarm-controller that can't yet see its own config (dev run, a module not
/// wired up yet) should still serve `/health` — and the [`HiveRoster`] says
/// which one it was.
pub(crate) fn load_hives() -> HiveRoster {
let Some(raw) = std::env::var_os(HIVES_ENV) else {
return Vec::new();
return HiveRoster::Unset;
};
match serde_json::from_str(&raw.to_string_lossy()) {
Ok(hives) => hives,
Ok(hives) => HiveRoster::Loaded(hives),
Err(e) => {
tracing::warn!(error = %e, env = HIVES_ENV, "failed to parse hive directory, serving an empty list");
Vec::new()
HiveRoster::Unparseable
}
}
}
@ -288,11 +320,26 @@ fn spawn_forge_workers(
}
let sched = Arc::clone(jobq);
let hive_names = hives.iter().map(|h| h.name.clone()).collect();
forge::hive_token::spawn(Arc::clone(&client), hive_names, move |hives| {
if let Err(e) = queue_hive_forge_token_mints(&sched, hives) {
tracing::warn!(error = %format!("{e:#}"), "hive forge tokens: queueing failed");
let cut_off_removed = match load_hives().absence_means_removal() {
Ok(()) => true,
Err(reason) => {
tracing::error!(
reason,
"removed hives are not cut off from the forge and the store until the roster loads"
);
false
}
});
};
forge::hive_token::spawn(
Arc::clone(&client),
hive_names,
cut_off_removed,
move |hives| {
if let Err(e) = queue_hive_forge_token_mints(&sched, hives) {
tracing::warn!(error = %format!("{e:#}"), "hive forge tokens: queueing failed");
}
},
);
let sched = Arc::clone(jobq);
forge::classifier_account::spawn(Arc::clone(&client), move || {
if let Err(e) = queue_classifier_forge_token_mint(&sched) {
@ -773,21 +820,37 @@ mod tests {
unsafe {
std::env::remove_var(HIVES_ENV);
}
let unset = load_hives();
assert_eq!(unset, HiveRoster::Unset);
assert_eq!(
load_hives(),
unset.clone().into_entries(),
Vec::<HiveEntry>::new(),
"unset env var is an empty directory, not a startup failure"
);
assert!(unset.absence_means_removal().is_err());
unsafe {
std::env::set_var(HIVES_ENV, "not json");
}
let garbled = load_hives();
assert_eq!(garbled, HiveRoster::Unparseable);
assert_eq!(
load_hives(),
garbled.clone().into_entries(),
Vec::<HiveEntry>::new(),
"unparseable env var falls back to empty rather than panicking — \
/health must still answer even if this daemon's own config is wrong"
);
assert!(garbled.absence_means_removal().is_err());
unsafe {
std::env::set_var(HIVES_ENV, "[]");
}
let empty = load_hives();
assert_eq!(empty, HiveRoster::Loaded(Vec::new()));
assert!(
empty.absence_means_removal().is_err(),
"a roster that loaded empty reads as every hive removed"
);
unsafe {
std::env::set_var(
@ -795,9 +858,10 @@ mod tests {
r#"[{"name":"pr1ma","domain":"pr1ma.example.com","dashboard":"pr1ma.hives.example"},{"name":"umbra","domain":"umbra.example.com","dashboard":"umbra.hives.example"}]"#,
);
}
let loaded = load_hives();
assert_eq!(
load_hives(),
vec![
loaded.clone().into_entries(),
[
HiveEntry {
name: "pr1ma".to_string(),
domain: "pr1ma.example.com".to_string(),
@ -810,6 +874,7 @@ mod tests {
},
]
);
assert_eq!(loaded.absence_means_removal(), Ok(()));
unsafe {
std::env::remove_var(HIVES_ENV);