Watch
0
0
Fork
You've already forked hyperhive
0

swarm-nats-auth: read the callout issuer and user seeds from the store

The responder loads the issuer and callout-user seeds from the secret
store under its own role, beside the xkey, and no longer takes
--issuer-seed-file / --user-seed-file.

`ensure-xkey` becomes `ensure-callout-keys`: it ensures the xkey as before,
and for each nkey creates the seed create-only when the store holds none,
from --import-{issuer,user}-seed-file when that file is present, so a key
kept on disk keeps its public half. Once a read of the store returns a seed,
the import file is removed; when that seed differs from the file the
store's is kept and a warning names the file. The public halves are
published create-only at swarm/public/swarm-nats/callout-{issuer,user}.

The KeyStore trait and its test double move to `keystore` so both key
modules share them.
This commit is contained in:
atlas 2026-10-06 10:13:34 +02:00 • committed by mara
commit 8a4bb97377
5 changed files with 610 additions and 174 deletions

View file

@ -11,11 +11,10 @@
//! responder that minted a new pair on its own start would hold a key the
//! running server does not know, and every request would fail to open.
use std::future::Future;
use anyhow::{Context, Result, anyhow};
use nkeys::XKey;
use swarm_secret_client::SecretStore;
use crate::keystore::KeyStore;
/// The seed's path under the store's KV mount. The nix half is
/// `xkeySeedLeaf` in `nix/host-modules/swarm-bao.nix`.
@ -30,44 +29,6 @@ pub const PUBLIC_PATH: &str = "swarm/public/swarm-nats/callout-xkey";
/// server's own public xkey, which the reply is sealed to.
pub const SERVER_XKEY_HEADER: &str = "Nats-Server-Xkey";
/// The two operations [`ensure`] and [`load`] need from the store, so a test
/// can stand in for it.
pub trait KeyStore {
/// The value at `path`, or `None` when nothing is stored there.
fn read(&self, path: &str) -> impl Future<Output = Result<Option<String>>> + Send;
/// Store `value` at `path`, failing when anything is already there.
fn create(&self, path: &str, value: &str) -> impl Future<Output = Result<()>> + Send;
}
/// One field, `value`, as every single-value secret in the store is kept.
/// No `Debug`: one of the values held here is the seed.
#[derive(serde::Serialize, serde::Deserialize)]
struct Stored {
value: String,
}
impl KeyStore for SecretStore {
async fn read(&self, path: &str) -> Result<Option<String>> {
let stored: Option<Stored> = self
.read_optional(path)
.await
.with_context(|| format!("reading {path}"))?;
Ok(stored.map(|s| s.value))
}
async fn create(&self, path: &str, value: &str) -> Result<()> {
SecretStore::create(
self,
path,
&Stored {
value: value.to_owned(),
},
)
.await
.with_context(|| format!("creating {path}"))
}
}
/// The stored pair, created first when no seed is stored.
///
/// Safe to run from two places at once. Each writes with a create that fails
@ -165,60 +126,8 @@ pub fn seal(own: &XKey, server: &XKey, reply: &[u8]) -> Result<Vec<u8>> {
#[cfg(test)]
pub(crate) mod tests {
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use super::*;
/// A store that keeps values in memory and counts writes. `rival` is a
/// value another start lands at a path between this one's read and its
/// create, which the create then loses to.
#[derive(Default)]
struct Fake {
values: Mutex<HashMap<String, String>>,
rival: Mutex<HashMap<String, String>>,
writes: AtomicUsize,
}
impl Fake {
fn holding(entries: &[(&str, &str)]) -> Self {
let fake = Self::default();
for (path, value) in entries {
fake.put(path, value);
}
fake
}
fn put(&self, path: &str, value: &str) {
self.values
.lock()
.expect("unpoisoned")
.insert(path.to_owned(), value.to_owned());
}
fn get(&self, path: &str) -> Option<String> {
self.values.lock().expect("unpoisoned").get(path).cloned()
}
}
impl KeyStore for Fake {
async fn read(&self, path: &str) -> Result<Option<String>> {
Ok(self.get(path))
}
async fn create(&self, path: &str, value: &str) -> Result<()> {
if let Some(theirs) = self.rival.lock().expect("unpoisoned").remove(path) {
self.put(path, &theirs);
}
if self.get(path).is_some() {
anyhow::bail!("403 permission denied");
}
self.writes.fetch_add(1, Ordering::SeqCst);
self.put(path, value);
Ok(())
}
}
use crate::keystore::tests::Fake;
fn seed_of(key: &XKey) -> String {
key.seed().expect("a generated key has a seed")
@ -231,7 +140,7 @@ pub(crate) mod tests {
assert_eq!(store.get(SEED_PATH), Some(seed_of(&key)));
assert_eq!(store.get(PUBLIC_PATH), Some(key.public_key()));
assert!(key.public_key().starts_with('X'), "a curve public key");
assert_eq!(store.writes.load(Ordering::SeqCst), 2);
assert_eq!(store.writes(), 2);
}
#[tokio::test]
@ -243,7 +152,7 @@ pub(crate) mod tests {
]);
let key = ensure(&store).await.expect("ensured");
assert_eq!(key.public_key(), existing.public_key());
assert_eq!(store.writes.load(Ordering::SeqCst), 0);
assert_eq!(store.writes(), 0);
}
/// A run that stopped between its two writes left a seed and no public
@ -255,7 +164,7 @@ pub(crate) mod tests {
let key = ensure(&store).await.expect("ensured");
assert_eq!(key.public_key(), existing.public_key());
assert_eq!(store.get(PUBLIC_PATH), Some(existing.public_key()));
assert_eq!(store.writes.load(Ordering::SeqCst), 1);
assert_eq!(store.writes(), 1);
}
/// Two starts read an empty store at once and both generate; the one
@ -264,21 +173,13 @@ pub(crate) mod tests {
async fn a_start_that_loses_the_create_race_takes_the_winners_seed() {
let winner = XKey::new();
let store = Fake::default();
store
.rival
.lock()
.expect("unpoisoned")
.insert(SEED_PATH.to_owned(), seed_of(&winner));
store
.rival
.lock()
.expect("unpoisoned")
.insert(PUBLIC_PATH.to_owned(), winner.public_key());
store.rival(SEED_PATH, &seed_of(&winner));
store.rival(PUBLIC_PATH, &winner.public_key());
let key = ensure(&store).await.expect("ensured");
assert_eq!(key.public_key(), winner.public_key());
assert_eq!(store.get(SEED_PATH), Some(seed_of(&winner)));
assert_eq!(store.get(PUBLIC_PATH), Some(winner.public_key()));
assert_eq!(store.writes.load(Ordering::SeqCst), 0);
assert_eq!(store.writes(), 0);
}
#[tokio::test]
@ -306,7 +207,7 @@ pub(crate) mod tests {
async fn loading_reads_the_stored_seed_and_never_creates_one() {
let store = Fake::default();
assert!(load(&store).await.is_err());
assert_eq!(store.writes.load(Ordering::SeqCst), 0);
assert_eq!(store.writes(), 0);
assert_eq!(store.get(SEED_PATH), None);
let existing = XKey::new();