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:
parent
b0239914b8
commit
7df2d646ff
3 changed files with 485 additions and 32 deletions
|
|
@ -25,6 +25,7 @@ use reqwest::StatusCode;
|
||||||
use swarm_secret_client::{client::DEFAULT_CERT_MOUNT, forge, policy};
|
use swarm_secret_client::{client::DEFAULT_CERT_MOUNT, forge, policy};
|
||||||
|
|
||||||
use super::Client;
|
use super::Client;
|
||||||
|
use crate::store::TokenStore;
|
||||||
|
|
||||||
/// The forge's name for every agent token this module mints.
|
/// The forge's name for every agent token this module mints.
|
||||||
///
|
///
|
||||||
|
|
@ -206,12 +207,25 @@ impl Client {
|
||||||
/// When the store or the forge refuses a step, when the agent has no forge
|
/// When the store or the forge refuses a step, when the agent has no forge
|
||||||
/// user, or when the new token authenticates as someone else.
|
/// user, or when the new token authenticates as someone else.
|
||||||
pub async fn ensure_agent_forge_token(&self, agent: &str) -> Result<()> {
|
pub async fn ensure_agent_forge_token(&self, agent: &str) -> Result<()> {
|
||||||
let path = forge::agent_token_path(agent)?;
|
|
||||||
let store = crate::store::connect()
|
let store = crate::store::connect()
|
||||||
.await
|
.await
|
||||||
.context("logging in to the swarm secret store")?;
|
.context("logging in to the swarm secret store")?;
|
||||||
let stored: Option<forge::Credential> = store
|
self.ensure_agent_forge_token_in(&store, agent).await
|
||||||
.read_optional(&path)
|
}
|
||||||
|
|
||||||
|
/// [`Client::ensure_agent_forge_token`] against any store.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// As [`Client::ensure_agent_forge_token`]. A store that can't be read is
|
||||||
|
/// one of them: nothing is minted on a token that might exist.
|
||||||
|
pub(super) async fn ensure_agent_forge_token_in(
|
||||||
|
&self,
|
||||||
|
store: &impl TokenStore,
|
||||||
|
agent: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
let path = forge::agent_token_path(agent)?;
|
||||||
|
let stored = store
|
||||||
|
.read_credential(&path)
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("reading {path}"))?;
|
.with_context(|| format!("reading {path}"))?;
|
||||||
let Some(listed) = self.list_agent_tokens(agent).await? else {
|
let Some(listed) = self.list_agent_tokens(agent).await? else {
|
||||||
|
|
@ -234,7 +248,7 @@ impl Client {
|
||||||
|
|
||||||
let credential = self
|
let credential = self
|
||||||
.replace_token(
|
.replace_token(
|
||||||
&store,
|
store,
|
||||||
agent,
|
agent,
|
||||||
AGENT_TOKEN_NAME,
|
AGENT_TOKEN_NAME,
|
||||||
AGENT_TOKEN_SCOPES,
|
AGENT_TOKEN_SCOPES,
|
||||||
|
|
@ -265,7 +279,7 @@ impl Client {
|
||||||
/// with no value, or the store refuses the write.
|
/// with no value, or the store refuses the write.
|
||||||
pub(super) async fn replace_token(
|
pub(super) async fn replace_token(
|
||||||
&self,
|
&self,
|
||||||
store: &swarm_secret_client::SecretStore,
|
store: &impl TokenStore,
|
||||||
user: &str,
|
user: &str,
|
||||||
name: &str,
|
name: &str,
|
||||||
scopes: &str,
|
scopes: &str,
|
||||||
|
|
@ -308,20 +322,16 @@ impl Client {
|
||||||
name: name.to_owned(),
|
name: name.to_owned(),
|
||||||
};
|
};
|
||||||
store
|
store
|
||||||
.write(path, &credential)
|
.write_credential(path, &credential)
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("storing {user}'s forge token at {path}"))?;
|
.with_context(|| format!("storing {user}'s forge token at {path}"))?;
|
||||||
Ok(credential)
|
Ok(credential)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// What the forge and the store say about `agent`'s token.
|
/// What the forge and the store say about `agent`'s token.
|
||||||
pub(super) async fn observe_agent(
|
pub(super) async fn observe_agent(&self, store: &impl TokenStore, agent: &str) -> Observed {
|
||||||
&self,
|
|
||||||
store: &swarm_secret_client::SecretStore,
|
|
||||||
agent: &str,
|
|
||||||
) -> Observed {
|
|
||||||
let stored = match forge::agent_token_path(agent) {
|
let stored = match forge::agent_token_path(agent) {
|
||||||
Ok(path) => store.read_optional::<forge::Credential>(&path).await,
|
Ok(path) => store.read_credential(&path).await,
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
};
|
};
|
||||||
let stored = match stored {
|
let stored = match stored {
|
||||||
|
|
@ -422,6 +432,7 @@ pub fn spawn(client: Arc<Client>, enqueue: impl Fn(Vec<String>) + Send + 'static
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::store::fake::FakeTokens;
|
||||||
|
|
||||||
const VALUE: &str = "0123456789abcdef0123456789abcdef01234567";
|
const VALUE: &str = "0123456789abcdef0123456789abcdef01234567";
|
||||||
|
|
||||||
|
|
@ -617,4 +628,153 @@ mod tests {
|
||||||
StatusCode::FORBIDDEN
|
StatusCode::FORBIDDEN
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
/// A stand-in forge for the agent `atlas`: it lists `listed` as the
|
||||||
|
/// agent's tokens, answers a create with a token whose value is
|
||||||
|
/// `NEW_VALUE`, and takes every other request.
|
||||||
|
async fn token_forge(listed: serde_json::Value) -> (Client, super::super::tests::Seen) {
|
||||||
|
super::super::tests::routed_forge(move |method, path, _| match method {
|
||||||
|
"GET" if path.contains("tokens") => (200, listed.to_string()),
|
||||||
|
"POST" if path.contains("tokens") => {
|
||||||
|
(201, serde_json::json!({"sha1": NEW_VALUE}).to_string())
|
||||||
|
}
|
||||||
|
"GET" if path == "/api/v1/user" => {
|
||||||
|
(200, serde_json::json!({"login": "atlas"}).to_string())
|
||||||
|
}
|
||||||
|
"DELETE" => (204, String::new()),
|
||||||
|
"POST" => (201, "{}".to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
const NEW_VALUE: &str = "fedcba9876543210fedcba9876543210fedcba98";
|
||||||
|
|
||||||
|
fn listed_json(last_eight: &str) -> serde_json::Value {
|
||||||
|
serde_json::json!([{
|
||||||
|
"id": 1,
|
||||||
|
"name": AGENT_TOKEN_NAME,
|
||||||
|
"token_last_eight": last_eight,
|
||||||
|
"scopes": LISTED_SCOPES,
|
||||||
|
}])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The token requests the forge saw that change something.
|
||||||
|
fn token_writes(seen: &super::super::tests::Seen) -> Vec<String> {
|
||||||
|
super::super::tests::writes(seen)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|w| w.contains("tokens"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn agent_path() -> String {
|
||||||
|
forge::agent_token_path("atlas").expect("a valid agent name")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_token_the_store_does_not_hold_is_replaced_and_stored() {
|
||||||
|
let (client, seen) = token_forge(listed_json("01234567")).await;
|
||||||
|
let store = FakeTokens::default();
|
||||||
|
client
|
||||||
|
.ensure_agent_forge_token_in(&store, "atlas")
|
||||||
|
.await
|
||||||
|
.expect("mints");
|
||||||
|
assert_eq!(store.value_at(&agent_path()).as_deref(), Some(NEW_VALUE));
|
||||||
|
let writes = token_writes(&seen);
|
||||||
|
assert!(writes.iter().any(|w| w.starts_with("DELETE")), "{writes:?}");
|
||||||
|
assert!(writes.iter().any(|w| w.starts_with("POST")), "{writes:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_token_the_forge_no_longer_lists_is_created_without_a_delete() {
|
||||||
|
// Revoked or expired on the forge: the store's copy is dead.
|
||||||
|
let (client, seen) = token_forge(serde_json::json!([])).await;
|
||||||
|
let store = FakeTokens::default().holding(&agent_path(), AGENT_TOKEN_NAME, VALUE);
|
||||||
|
client
|
||||||
|
.ensure_agent_forge_token_in(&store, "atlas")
|
||||||
|
.await
|
||||||
|
.expect("mints");
|
||||||
|
assert_eq!(store.value_at(&agent_path()).as_deref(), Some(NEW_VALUE));
|
||||||
|
let writes = token_writes(&seen);
|
||||||
|
assert!(
|
||||||
|
writes.iter().all(|w| !w.starts_with("DELETE")),
|
||||||
|
"{writes:?}"
|
||||||
|
);
|
||||||
|
assert_eq!(writes.len(), 1, "{writes:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_token_whose_forge_copy_differs_is_rotated() {
|
||||||
|
let (client, seen) = token_forge(listed_json("ffffffff")).await;
|
||||||
|
let store = FakeTokens::default().holding(&agent_path(), AGENT_TOKEN_NAME, VALUE);
|
||||||
|
client
|
||||||
|
.ensure_agent_forge_token_in(&store, "atlas")
|
||||||
|
.await
|
||||||
|
.expect("rotates");
|
||||||
|
assert_eq!(store.value_at(&agent_path()).as_deref(), Some(NEW_VALUE));
|
||||||
|
assert_eq!(token_writes(&seen).len(), 2, "a delete, then a create");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_valid_token_is_kept_and_nothing_is_written() {
|
||||||
|
let (client, seen) = token_forge(listed_json("01234567")).await;
|
||||||
|
let store = FakeTokens::default().holding(&agent_path(), AGENT_TOKEN_NAME, VALUE);
|
||||||
|
client
|
||||||
|
.ensure_agent_forge_token_in(&store, "atlas")
|
||||||
|
.await
|
||||||
|
.expect("keeps");
|
||||||
|
assert!(token_writes(&seen).is_empty(), "{:?}", token_writes(&seen));
|
||||||
|
assert_eq!(store.calls(), [format!("read {}", agent_path())]);
|
||||||
|
assert_eq!(store.value_at(&agent_path()).as_deref(), Some(VALUE));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_store_that_cannot_be_read_never_mints() {
|
||||||
|
// The token may well exist: a blind mint would rotate a live one.
|
||||||
|
let (client, seen) = token_forge(listed_json("ffffffff")).await;
|
||||||
|
let store = FakeTokens::default().unreadable();
|
||||||
|
client
|
||||||
|
.ensure_agent_forge_token_in(&store, "atlas")
|
||||||
|
.await
|
||||||
|
.expect_err("the read failed");
|
||||||
|
assert!(token_writes(&seen).is_empty(), "{:?}", token_writes(&seen));
|
||||||
|
assert!(store.calls().iter().all(|c| c.starts_with("read")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_token_the_store_will_not_take_is_an_error_not_a_success() {
|
||||||
|
let (client, _seen) = token_forge(serde_json::json!([])).await;
|
||||||
|
let store = FakeTokens::default().unwritable();
|
||||||
|
let err = client
|
||||||
|
.ensure_agent_forge_token_in(&store, "atlas")
|
||||||
|
.await
|
||||||
|
.expect_err("the write failed");
|
||||||
|
assert!(format!("{err:#}").contains("storing"), "{err:#}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_pass_reads_a_stored_token_and_a_failed_read_decides_nothing() {
|
||||||
|
let (client, seen) = token_forge(listed_json("01234567")).await;
|
||||||
|
let held = FakeTokens::default().holding(&agent_path(), AGENT_TOKEN_NAME, VALUE);
|
||||||
|
assert_eq!(
|
||||||
|
client.observe_agent(&held, "atlas").await,
|
||||||
|
Observed::Decided(Decision::Keep)
|
||||||
|
);
|
||||||
|
let empty = FakeTokens::default();
|
||||||
|
assert_eq!(
|
||||||
|
client.observe_agent(&empty, "atlas").await,
|
||||||
|
Observed::Decided(Decision::Mint(MintReason::NotStored))
|
||||||
|
);
|
||||||
|
// The failure: unknown, and the forge is not even asked.
|
||||||
|
seen.lock().unwrap().clear();
|
||||||
|
let broken = FakeTokens::default().unreadable();
|
||||||
|
assert_eq!(
|
||||||
|
client.observe_agent(&broken, "atlas").await,
|
||||||
|
Observed::Unknown
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
seen.lock().unwrap().is_empty(),
|
||||||
|
"{:?}",
|
||||||
|
seen.lock().unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ use swarm_secret_client::forge;
|
||||||
|
|
||||||
use super::agent_token::{Decision, Observed, classify_named, is_not_found};
|
use super::agent_token::{Decision, Observed, classify_named, is_not_found};
|
||||||
use super::{CONFIG_ORG, Client, folds_into_success};
|
use super::{CONFIG_ORG, Client, folds_into_success};
|
||||||
|
use crate::store::TokenStore;
|
||||||
|
|
||||||
/// The forge's name for every hive token this module mints.
|
/// The forge's name for every hive token this module mints.
|
||||||
pub const HIVE_TOKEN_NAME: &str = "swarm-hive";
|
pub const HIVE_TOKEN_NAME: &str = "swarm-hive";
|
||||||
|
|
@ -139,6 +140,17 @@ impl Client {
|
||||||
/// hive's name exists and is not the hive's, or when the read-back does
|
/// hive's name exists and is not the hive's, or when the read-back does
|
||||||
/// not show the new token.
|
/// not show the new token.
|
||||||
pub async fn ensure_hive_forge_token(&self, hive: &str) -> Result<()> {
|
pub async fn ensure_hive_forge_token(&self, hive: &str) -> Result<()> {
|
||||||
|
let (login, path) = self.ensure_hive_account(hive).await?;
|
||||||
|
let store = crate::store::connect()
|
||||||
|
.await
|
||||||
|
.context("logging in to the swarm secret store")?;
|
||||||
|
self.mint_hive_token_in(&store, hive, &login, &path).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The hive's forge account, with its seat in [`HIVES_TEAM`]; the account
|
||||||
|
/// is made before the store is touched, as it always was. Answers the
|
||||||
|
/// account's login and the store path of its token.
|
||||||
|
async fn ensure_hive_account(&self, hive: &str) -> Result<(String, String)> {
|
||||||
let login = forge::hive_login(hive);
|
let login = forge::hive_login(hive);
|
||||||
let path = forge::hive_token_path(hive)?;
|
let path = forge::hive_token_path(hive)?;
|
||||||
self.ensure_hive_user(hive, &login).await?;
|
self.ensure_hive_user(hive, &login).await?;
|
||||||
|
|
@ -147,11 +159,23 @@ impl Client {
|
||||||
.call(|api| api.org_add_team_member(team, &login).into_future())
|
.call(|api| api.org_add_team_member(team, &login).into_future())
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("adding {login} to {CONFIG_ORG}/{HIVES_TEAM}"))?;
|
.with_context(|| format!("adding {login} to {CONFIG_ORG}/{HIVES_TEAM}"))?;
|
||||||
|
Ok((login, path))
|
||||||
|
}
|
||||||
|
|
||||||
let store = crate::store::connect()
|
/// The token half of [`Client::ensure_hive_forge_token`], against any
|
||||||
.await
|
/// store.
|
||||||
.context("logging in to the swarm secret store")?;
|
///
|
||||||
let (stored, listed) = self.read_hive_token(&store, &login, &path).await?;
|
/// # Errors
|
||||||
|
/// As [`Client::ensure_hive_forge_token`]. A store that can't be read is
|
||||||
|
/// one of them: nothing is minted on a token that might exist.
|
||||||
|
async fn mint_hive_token_in(
|
||||||
|
&self,
|
||||||
|
store: &impl TokenStore,
|
||||||
|
hive: &str,
|
||||||
|
login: &str,
|
||||||
|
path: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
let (stored, listed) = self.read_hive_token(store, login, path).await?;
|
||||||
let reason =
|
let reason =
|
||||||
match classify_named(HIVE_TOKEN_NAME, HIVE_TOKEN_SCOPES, stored.as_ref(), &listed) {
|
match classify_named(HIVE_TOKEN_NAME, HIVE_TOKEN_SCOPES, stored.as_ref(), &listed) {
|
||||||
Decision::Keep => {
|
Decision::Keep => {
|
||||||
|
|
@ -166,12 +190,12 @@ impl Client {
|
||||||
};
|
};
|
||||||
let credential = self
|
let credential = self
|
||||||
.replace_token(
|
.replace_token(
|
||||||
&store,
|
store,
|
||||||
&login,
|
login,
|
||||||
HIVE_TOKEN_NAME,
|
HIVE_TOKEN_NAME,
|
||||||
HIVE_TOKEN_SCOPES,
|
HIVE_TOKEN_SCOPES,
|
||||||
&listed,
|
&listed,
|
||||||
&path,
|
path,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
@ -180,7 +204,7 @@ impl Client {
|
||||||
.repo_search(RepoSearchQuery::default())
|
.repo_search(RepoSearchQuery::default())
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("using {login}'s new forge token"))?;
|
.with_context(|| format!("using {login}'s new forge token"))?;
|
||||||
let (stored, listed) = self.read_hive_token(&store, &login, &path).await?;
|
let (stored, listed) = self.read_hive_token(store, login, path).await?;
|
||||||
let now = classify_named(HIVE_TOKEN_NAME, HIVE_TOKEN_SCOPES, stored.as_ref(), &listed);
|
let now = classify_named(HIVE_TOKEN_NAME, HIVE_TOKEN_SCOPES, stored.as_ref(), &listed);
|
||||||
if now != Decision::Keep {
|
if now != Decision::Keep {
|
||||||
bail!("{login}'s new forge token does not read back as current: {now:?}");
|
bail!("{login}'s new forge token does not read back as current: {now:?}");
|
||||||
|
|
@ -193,12 +217,12 @@ impl Client {
|
||||||
/// `login`, which must exist.
|
/// `login`, which must exist.
|
||||||
async fn read_hive_token(
|
async fn read_hive_token(
|
||||||
&self,
|
&self,
|
||||||
store: &swarm_secret_client::SecretStore,
|
store: &impl TokenStore,
|
||||||
login: &str,
|
login: &str,
|
||||||
path: &str,
|
path: &str,
|
||||||
) -> Result<(Option<forge::Credential>, Vec<AccessToken>)> {
|
) -> Result<(Option<forge::Credential>, Vec<AccessToken>)> {
|
||||||
let stored = store
|
let stored = store
|
||||||
.read_optional(path)
|
.read_credential(path)
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("reading {path}"))?;
|
.with_context(|| format!("reading {path}"))?;
|
||||||
let Some(listed) = self.list_agent_tokens(login).await? else {
|
let Some(listed) = self.list_agent_tokens(login).await? else {
|
||||||
|
|
@ -323,15 +347,10 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// What the store, the forge and the team say about `hive`.
|
/// What the store, the forge and the team say about `hive`.
|
||||||
async fn observe_hive(
|
async fn observe_hive(&self, store: &impl TokenStore, team: TeamSeen, hive: &str) -> HiveSeen {
|
||||||
&self,
|
|
||||||
store: &swarm_secret_client::SecretStore,
|
|
||||||
team: TeamSeen,
|
|
||||||
hive: &str,
|
|
||||||
) -> HiveSeen {
|
|
||||||
let login = forge::hive_login(hive);
|
let login = forge::hive_login(hive);
|
||||||
let stored = match forge::hive_token_path(hive) {
|
let stored = match forge::hive_token_path(hive) {
|
||||||
Ok(path) => store.read_optional::<forge::Credential>(&path).await,
|
Ok(path) => store.read_credential(&path).await,
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
};
|
};
|
||||||
let token = match stored {
|
let token = match stored {
|
||||||
|
|
@ -709,6 +728,7 @@ mod tests {
|
||||||
|
|
||||||
// The forge side, against a stand-in forge that answers by path.
|
// The forge side, against a stand-in forge that answers by path.
|
||||||
use super::super::tests::{routed_forge, sent, writes};
|
use super::super::tests::{routed_forge, sent, writes};
|
||||||
|
use crate::store::fake::FakeTokens;
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
|
@ -881,4 +901,159 @@ mod tests {
|
||||||
assert!(plan(TeamSeen::Absent, &[]).is_empty());
|
assert!(plan(TeamSeen::Absent, &[]).is_empty());
|
||||||
assert!(plan(TeamSeen::Matches(1), &[("removed".to_owned(), stale)]).is_empty());
|
assert!(plan(TeamSeen::Matches(1), &[("removed".to_owned(), stale)]).is_empty());
|
||||||
}
|
}
|
||||||
|
const NEW_HIVE_VALUE: &str = "fedcba9876543210fedcba9876543210fedcba98";
|
||||||
|
const HIVE_VALUE: &str = "0123456789abcdef0123456789abcdef01234567";
|
||||||
|
|
||||||
|
/// A stand-in forge for the hive `alpha`: its account is the hive's, the
|
||||||
|
/// hives team is as it should be, `listed` is the hive's tokens, and a
|
||||||
|
/// create answers with `NEW_HIVE_VALUE`.
|
||||||
|
async fn hive_forge(listed: Value) -> (Client, super::super::tests::Seen) {
|
||||||
|
let login = forge::hive_login("alpha");
|
||||||
|
let account = json!({"email": hive_email("alpha")});
|
||||||
|
let team = json!([hives_team_json("read", &HIVES_TEAM_UNITS)]);
|
||||||
|
// Once the create has happened the forge lists the new token, as the
|
||||||
|
// job's read-back expects.
|
||||||
|
let created = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||||
|
routed_forge(move |method, path, _| match method {
|
||||||
|
"GET" if path == format!("/api/v1/users/{login}") => (200, account.to_string()),
|
||||||
|
"GET" if path == TEAMS => (200, team.to_string()),
|
||||||
|
"GET" if path.contains("tokens") => {
|
||||||
|
if created.load(Ordering::SeqCst) {
|
||||||
|
(
|
||||||
|
200,
|
||||||
|
hive_listed(&NEW_HIVE_VALUE[NEW_HIVE_VALUE.len() - 8..]).to_string(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(200, listed.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"POST" if path.contains("tokens") => {
|
||||||
|
created.store(true, Ordering::SeqCst);
|
||||||
|
(201, json!({"sha1": NEW_HIVE_VALUE}).to_string())
|
||||||
|
}
|
||||||
|
"GET" if path.contains("repos/search") => {
|
||||||
|
(200, json!({"ok": true, "data": []}).to_string())
|
||||||
|
}
|
||||||
|
"DELETE" | "PUT" => (204, String::new()),
|
||||||
|
"POST" => (201, "{}".to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hive_listed(last_eight: &str) -> Value {
|
||||||
|
json!([{
|
||||||
|
"id": 1,
|
||||||
|
"name": HIVE_TOKEN_NAME,
|
||||||
|
"token_last_eight": last_eight,
|
||||||
|
"scopes": HIVE_TOKEN_SCOPES.split(',').collect::<Vec<_>>(),
|
||||||
|
}])
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client {
|
||||||
|
/// The whole pass against `store`: the account, then the token.
|
||||||
|
async fn ensure_hive_forge_token_in(&self, store: &FakeTokens, hive: &str) -> Result<()> {
|
||||||
|
let (login, path) = self.ensure_hive_account(hive).await?;
|
||||||
|
self.mint_hive_token_in(store, hive, &login, &path).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hive_path() -> String {
|
||||||
|
forge::hive_token_path("alpha").expect("a valid hive name")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn token_writes(seen: &super::super::tests::Seen) -> Vec<String> {
|
||||||
|
writes(seen)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|w| w.contains("tokens"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hive_with_no_stored_token_gets_one_minted_and_stored() {
|
||||||
|
let (client, seen) = hive_forge(hive_listed("01234567")).await;
|
||||||
|
let store = FakeTokens::default();
|
||||||
|
client
|
||||||
|
.ensure_hive_forge_token_in(&store, "alpha")
|
||||||
|
.await
|
||||||
|
.expect("mints");
|
||||||
|
assert_eq!(
|
||||||
|
store.value_at(&hive_path()).as_deref(),
|
||||||
|
Some(NEW_HIVE_VALUE)
|
||||||
|
);
|
||||||
|
assert_eq!(token_writes(&seen).len(), 2, "a delete, then a create");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hives_valid_token_is_kept() {
|
||||||
|
let (client, seen) = hive_forge(hive_listed("01234567")).await;
|
||||||
|
let store = FakeTokens::default().holding(&hive_path(), HIVE_TOKEN_NAME, HIVE_VALUE);
|
||||||
|
client
|
||||||
|
.ensure_hive_forge_token_in(&store, "alpha")
|
||||||
|
.await
|
||||||
|
.expect("keeps");
|
||||||
|
assert!(token_writes(&seen).is_empty(), "{:?}", token_writes(&seen));
|
||||||
|
assert_eq!(store.value_at(&hive_path()).as_deref(), Some(HIVE_VALUE));
|
||||||
|
assert_eq!(store.calls(), [format!("read {}", hive_path())]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hives_token_the_forge_does_not_list_is_created_and_stored() {
|
||||||
|
let (client, seen) = hive_forge(json!([])).await;
|
||||||
|
let store = FakeTokens::default().holding(&hive_path(), HIVE_TOKEN_NAME, HIVE_VALUE);
|
||||||
|
client
|
||||||
|
.ensure_hive_forge_token_in(&store, "alpha")
|
||||||
|
.await
|
||||||
|
.expect("mints");
|
||||||
|
assert_eq!(
|
||||||
|
store.value_at(&hive_path()).as_deref(),
|
||||||
|
Some(NEW_HIVE_VALUE)
|
||||||
|
);
|
||||||
|
assert!(token_writes(&seen).iter().all(|w| !w.starts_with("DELETE")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hive_whose_store_cannot_be_read_is_never_minted_for() {
|
||||||
|
let (client, seen) = hive_forge(hive_listed("ffffffff")).await;
|
||||||
|
let store = FakeTokens::default().unreadable();
|
||||||
|
client
|
||||||
|
.ensure_hive_forge_token_in(&store, "alpha")
|
||||||
|
.await
|
||||||
|
.expect_err("the read failed");
|
||||||
|
assert!(token_writes(&seen).is_empty(), "{:?}", token_writes(&seen));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_pass_reads_a_hives_token_from_the_store_and_a_failed_read_is_unknown() {
|
||||||
|
let (client, seen) = hive_forge(hive_listed("01234567")).await;
|
||||||
|
let held = FakeTokens::default().holding(&hive_path(), HIVE_TOKEN_NAME, HIVE_VALUE);
|
||||||
|
let seen_hive = client
|
||||||
|
.observe_hive(&held, TeamSeen::Matches(9), "alpha")
|
||||||
|
.await;
|
||||||
|
assert_eq!(seen_hive.token, Observed::Decided(Decision::Keep));
|
||||||
|
let empty = FakeTokens::default();
|
||||||
|
let seen_hive = client
|
||||||
|
.observe_hive(&empty, TeamSeen::Matches(9), "alpha")
|
||||||
|
.await;
|
||||||
|
assert_eq!(
|
||||||
|
seen_hive.token,
|
||||||
|
Observed::Decided(Decision::Mint(MintReason::NotStored))
|
||||||
|
);
|
||||||
|
seen.lock().unwrap().clear();
|
||||||
|
let broken = FakeTokens::default().unreadable();
|
||||||
|
let seen_hive = client
|
||||||
|
.observe_hive(&broken, TeamSeen::Matches(9), "alpha")
|
||||||
|
.await;
|
||||||
|
assert_eq!(seen_hive.token, Observed::Unknown);
|
||||||
|
// The token is unknown without asking the forge for its tokens; the
|
||||||
|
// team-membership read is a separate question.
|
||||||
|
assert!(
|
||||||
|
seen.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.all(|(_, path, _)| !path.contains("tokens")),
|
||||||
|
"{:?}",
|
||||||
|
seen.lock().unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,16 @@
|
||||||
//! Two paths log in: writing an agent's credential, and writing the read
|
//! 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
|
//! 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.
|
//! 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
|
//! [`AccessStore`] is the subset of calls that give a principal its access, and
|
||||||
//! a test can stand in for the store.
|
//! [`TokenStore`] the two that keep a forge token's credential, so a test can
|
||||||
|
//! stand in for the store.
|
||||||
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
|
||||||
use swarm_secret_client::{
|
use swarm_secret_client::{
|
||||||
Error, SecretStore,
|
Error, SecretStore,
|
||||||
client::{CertRoleTokenTtls, EntityRef},
|
client::{CertRoleTokenTtls, EntityRef},
|
||||||
|
forge,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The cert-auth role the controller logs in under.
|
/// 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
|
/// A store that records what it is asked to do, for tests of the order and the
|
||||||
/// outcome of the access calls.
|
/// outcome of the access calls.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -225,9 +260,10 @@ pub(crate) mod fake {
|
||||||
use swarm_secret_client::{
|
use swarm_secret_client::{
|
||||||
Error,
|
Error,
|
||||||
client::{CERT_ROLE_TOKEN_TTL_SECS, CertRoleTokenTtls, EntityRef},
|
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,
|
/// 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.
|
/// and the lifetimes a write of only those can change.
|
||||||
|
|
@ -540,4 +576,86 @@ pub(crate) mod fake {
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue