Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: a TokenStore test double covers token mint and rotation

The forge token paths (ensure_*_forge_token, replace_token, observe_agent,
observe_hive) took the concrete SecretStore, so none of them could run
without OpenBao. They now take a TokenStore trait, implemented by
SecretStore, with a FakeTokens double in the store tests.

ensure_hive_forge_token keeps its order: the account and team steps, then
the store login, then the token.
This commit is contained in:
maia 2026-11-19 08:37:30 +01:00 • committed by mara
commit 7df2d646ff

View file

@ -3,14 +3,16 @@
//! Two paths log in: writing an agent's credential, and writing the read
//! grant that lets a hive fetch one back. Both present the same certificate
//! under the same role, so the role is named here rather than at each caller.
//! [`AccessStore`] is the subset of calls that give a principal its access, so
//! a test can stand in for the store.
//! [`AccessStore`] is the subset of calls that give a principal its access, and
//! [`TokenStore`] the two that keep a forge token's credential, so a test can
//! stand in for the store.
use std::future::Future;
use swarm_secret_client::{
Error, SecretStore,
client::{CertRoleTokenTtls, EntityRef},
forge,
};
/// The cert-auth role the controller logs in under.
@ -216,6 +218,39 @@ impl AccessStore for SecretStore {
}
}
/// The store calls that keep a forge token: read the credential stored at a
/// path, and write the one just minted. A test stands in for the store through
/// this trait.
pub(crate) trait TokenStore {
/// The credential at `path`, `None` when nothing is stored there. As
/// [`SecretStore::read_optional`].
fn read_credential(
&self,
path: &str,
) -> impl Future<Output = Result<Option<forge::Credential>, Error>> + Send;
/// Store `credential` at `path`. As [`SecretStore::write`].
fn write_credential(
&self,
path: &str,
credential: &forge::Credential,
) -> impl Future<Output = Result<(), Error>> + Send;
}
impl TokenStore for SecretStore {
async fn read_credential(&self, path: &str) -> Result<Option<forge::Credential>, Error> {
self.read_optional(path).await
}
async fn write_credential(
&self,
path: &str,
credential: &forge::Credential,
) -> Result<(), Error> {
self.write(path, credential).await
}
}
/// A store that records what it is asked to do, for tests of the order and the
/// outcome of the access calls.
#[cfg(test)]
@ -225,9 +260,10 @@ pub(crate) mod fake {
use swarm_secret_client::{
Error,
client::{CERT_ROLE_TOKEN_TTL_SECS, CertRoleTokenTtls, EntityRef},
forge,
};
use super::AccessStore;
use super::{AccessStore, TokenStore};
/// A cert-auth role as the fake keeps it: what a write of the role sets,
/// and the lifetimes a write of only those can change.
@ -540,4 +576,86 @@ pub(crate) mod fake {
Ok(())
}
}
/// A store of forge-token credentials: what each path holds, which calls
/// fail, and every call in order.
#[derive(Default)]
pub(crate) struct FakeTokens {
held: Mutex<BTreeMap<String, forge::Credential>>,
read_fails: bool,
write_fails: bool,
calls: Mutex<Vec<String>>,
}
impl FakeTokens {
/// A store holding `value` at `path` as the token named `name`.
pub(crate) fn holding(self, path: &str, name: &str, value: &str) -> Self {
self.held.lock().expect("not poisoned").insert(
path.to_owned(),
forge::Credential {
value: value.to_owned(),
name: name.to_owned(),
},
);
self
}
/// A store whose reads fail.
pub(crate) fn unreadable(mut self) -> Self {
self.read_fails = true;
self
}
/// A store whose writes fail.
pub(crate) fn unwritable(mut self) -> Self {
self.write_fails = true;
self
}
/// The value stored at `path`.
pub(crate) fn value_at(&self, path: &str) -> Option<String> {
self.held
.lock()
.expect("not poisoned")
.get(path)
.map(|c| c.value.clone())
}
/// The calls, as `read <path>` and `write <path>`, in order.
pub(crate) fn calls(&self) -> Vec<String> {
self.calls.lock().expect("not poisoned").clone()
}
}
impl TokenStore for FakeTokens {
async fn read_credential(&self, path: &str) -> Result<Option<forge::Credential>, Error> {
self.calls
.lock()
.expect("not poisoned")
.push(format!("read {path}"));
if self.read_fails {
return Err(Error::Settings("fake store: read refused".to_owned()));
}
Ok(self.held.lock().expect("not poisoned").get(path).cloned())
}
async fn write_credential(
&self,
path: &str,
credential: &forge::Credential,
) -> Result<(), Error> {
self.calls
.lock()
.expect("not poisoned")
.push(format!("write {path}"));
if self.write_fails {
return Err(Error::Settings("fake store: write refused".to_owned()));
}
self.held
.lock()
.expect("not poisoned")
.insert(path.to_owned(), credential.clone());
Ok(())
}
}
}