agents get a per-agent repo limit in their permissions record, orgs are locked
max_repos (default 0, 0..=100) lives in the agent's agent-permissions record and is edited on its permissions panel. The controller applies each agent's own value as max_repo_creation at CreateForgeUser and to existing accounts on change, restart and every five minutes; agent accounts may not create organizations.
This commit is contained in:
parent
dc0d08b7fb
commit
1edeb5c55d
4 changed files with 763 additions and 58 deletions
223
swarm-controller/src/forge/agent_max_repos.rs
Normal file
223
swarm-controller/src/forge/agent_max_repos.rs
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
//! Each existing agent's forge account at its own repository limit.
|
||||
//!
|
||||
//! The limit is the `max_repos` of the agent's record in `agent-permissions`
|
||||
//! (`swarm_wire::agent_permissions`). A new account gets it from its
|
||||
//! `CreateForgeUser` node. This pass reaches the others: at start, when a
|
||||
//! change of an agent's `max_repos` wakes it
|
||||
//! ([`crate::agent_permissions::MAX_REPOS_CHANGED`]), and every
|
||||
//! [`RECONCILE_INTERVAL`] over every agent that holds a store identity.
|
||||
//!
|
||||
//! Forgejo never returns an account's `max_repo_creation`, so nothing can be
|
||||
//! compared against the forge. The pass remembers what it applied to each agent
|
||||
//! in this process and applies again only what differs, so a restart applies
|
||||
//! every agent's limit once, and an agent whose edit failed is retried at the
|
||||
//! next tick. The decision is pure ([`plan`]), so the tests pin it.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::Client;
|
||||
|
||||
/// How often [`spawn`] re-checks every agent.
|
||||
const RECONCILE_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5);
|
||||
|
||||
/// The agents of `desired` whose account is not known to be at their limit,
|
||||
/// with that limit.
|
||||
pub fn plan(
|
||||
desired: &BTreeMap<String, u32>,
|
||||
applied: &BTreeMap<String, u32>,
|
||||
) -> Vec<(String, u32)> {
|
||||
desired
|
||||
.iter()
|
||||
.filter(|(agent, limit)| applied.get(*agent) != Some(limit))
|
||||
.map(|(agent, limit)| (agent.clone(), *limit))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One pass: set each limit of `todo` on its agent's account, recording the
|
||||
/// ones the forge took in `applied`. An agent the forge refuses is logged and
|
||||
/// left out, so the next pass plans it again.
|
||||
async fn apply(client: &Client, todo: &[(String, u32)], applied: &mut BTreeMap<String, u32>) {
|
||||
for (agent, limit) in todo {
|
||||
match client.limit_agent_account(agent, *limit).await {
|
||||
Ok(()) => {
|
||||
applied.insert(agent.clone(), *limit);
|
||||
}
|
||||
Err(e) => tracing::warn!(
|
||||
%agent,
|
||||
limit,
|
||||
error = %format!("{e:#}"),
|
||||
"agent max repos: the forge account was not updated; retrying next pass"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every roster agent's limit. An agent whose record can't be read is left
|
||||
/// out, so nothing is applied for it this pass.
|
||||
async fn desired(queue: &async_nats::Client, roster: &[String]) -> BTreeMap<String, u32> {
|
||||
let mut limits = BTreeMap::new();
|
||||
for agent in roster {
|
||||
match crate::agent_permissions::max_repos(queue, agent).await {
|
||||
Ok(limit) => {
|
||||
limits.insert(agent.clone(), limit);
|
||||
}
|
||||
Err(e) => tracing::warn!(
|
||||
%agent,
|
||||
error = %format!("{e:#}"),
|
||||
"agent max repos: the agent's limit was not read; skipping it this pass"
|
||||
),
|
||||
}
|
||||
}
|
||||
limits
|
||||
}
|
||||
|
||||
/// Apply every agent's limit now and again on each wake and tick. A pass that
|
||||
/// fails is logged and retried on the next tick; it never stops the daemon.
|
||||
pub fn spawn(client: Arc<Client>, queue: async_nats::Client) {
|
||||
tokio::spawn(async move {
|
||||
let mut ticker = tokio::time::interval(RECONCILE_INTERVAL);
|
||||
let mut applied = BTreeMap::new();
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = ticker.tick() => {}
|
||||
() = crate::agent_permissions::MAX_REPOS_CHANGED.notified() => {}
|
||||
}
|
||||
let roster = match super::docs_grant::read_roster().await {
|
||||
Ok(roster) => roster,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %format!("{e:#}"), "agent max repos: no roster; retrying next tick");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
applied.retain(|agent, _| roster.contains(agent));
|
||||
let todo = plan(&desired(&queue, &roster).await, &applied);
|
||||
if todo.is_empty() {
|
||||
continue;
|
||||
}
|
||||
tracing::info!(agents = todo.len(), "agent max repos: applying the limits");
|
||||
super::within_forge_deadline("agent max repos", apply(&client, &todo, &mut applied))
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn limits(of: &[(&str, u32)]) -> BTreeMap<String, u32> {
|
||||
of.iter().map(|(a, n)| ((*a).to_owned(), *n)).collect()
|
||||
}
|
||||
|
||||
/// An agent at another value, or not yet applied, is planned with its own
|
||||
/// limit; one already at its limit is not.
|
||||
#[test]
|
||||
fn plan_names_the_agents_not_at_their_limit() {
|
||||
let desired = limits(&[("atlas", 2), ("iris", 0), ("sock", 5)]);
|
||||
let applied = limits(&[("atlas", 2), ("iris", 1)]);
|
||||
assert_eq!(
|
||||
plan(&desired, &applied),
|
||||
[("iris".to_owned(), 0), ("sock".to_owned(), 5)]
|
||||
);
|
||||
// Control: everything applied at its limit leaves nothing to do.
|
||||
assert!(plan(&desired, &desired).is_empty());
|
||||
}
|
||||
|
||||
/// A stand-in forge on a loopback port that answers each agent's own
|
||||
/// email and records every admin edit as (login, body).
|
||||
async fn stub_agents() -> (
|
||||
Client,
|
||||
Arc<std::sync::Mutex<Vec<(String, serde_json::Value)>>>,
|
||||
) {
|
||||
use axum::http::{Method, StatusCode, Uri, header::CONTENT_TYPE};
|
||||
|
||||
let edits: Arc<std::sync::Mutex<Vec<(String, serde_json::Value)>>> = Arc::default();
|
||||
let recorded = Arc::clone(&edits);
|
||||
let app = axum::Router::new().fallback(
|
||||
move |method: Method, uri: Uri, body: axum::body::Bytes| {
|
||||
let recorded = Arc::clone(&recorded);
|
||||
async move {
|
||||
let login = uri.path().rsplit('/').next().unwrap_or_default().to_owned();
|
||||
let body = match method {
|
||||
Method::GET => format!(r#"{{"email":"{login}@hyperhive.local"}}"#),
|
||||
Method::PATCH => {
|
||||
let json = serde_json::from_slice(&body).unwrap_or_default();
|
||||
recorded.lock().unwrap().push((login, json));
|
||||
"{}".to_owned()
|
||||
}
|
||||
_ => "{}".to_owned(),
|
||||
};
|
||||
(StatusCode::OK, [(CONTENT_TYPE, "application/json")], body)
|
||||
}
|
||||
},
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||
let api = crate::forge::api::tests::fixed(url.clone(), "stub-token");
|
||||
(Client { api, url }, edits)
|
||||
}
|
||||
|
||||
/// Each agent gets its own limit, none is skipped and none takes another's;
|
||||
/// each is recorded as applied, and a second plan has nothing left.
|
||||
#[tokio::test]
|
||||
async fn the_pass_sets_each_agent_its_own_limit() {
|
||||
let (client, edits) = stub_agents().await;
|
||||
let desired = limits(&[("atlas", 4), ("iris", 0), ("sock", 9)]);
|
||||
let mut applied = BTreeMap::new();
|
||||
|
||||
apply(&client, &plan(&desired, &applied), &mut applied).await;
|
||||
|
||||
let mut seen: Vec<(String, i64, bool)> = edits
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(login, body)| {
|
||||
(
|
||||
login.clone(),
|
||||
body["max_repo_creation"].as_i64().unwrap(),
|
||||
body["allow_create_organization"] == false,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
seen.sort();
|
||||
assert_eq!(
|
||||
seen,
|
||||
[
|
||||
("atlas".to_owned(), 4, true),
|
||||
("iris".to_owned(), 0, true),
|
||||
("sock".to_owned(), 9, true)
|
||||
]
|
||||
);
|
||||
assert_eq!(applied, desired);
|
||||
assert!(plan(&desired, &applied).is_empty());
|
||||
// One agent's changed limit plans that agent alone.
|
||||
let changed = limits(&[("atlas", 4), ("iris", 1), ("sock", 9)]);
|
||||
assert_eq!(plan(&changed, &applied), [("iris".to_owned(), 1)]);
|
||||
}
|
||||
|
||||
/// An agent whose edit the forge refuses is not recorded, so the next pass
|
||||
/// plans it again.
|
||||
#[tokio::test]
|
||||
async fn a_refused_agent_is_planned_again() {
|
||||
let (client, edits) = stub_agents().await;
|
||||
let mut applied = BTreeMap::new();
|
||||
apply(&client, &[("atlas".to_owned(), 2)], &mut applied).await;
|
||||
// Control: an account that is the agent's own is recorded.
|
||||
assert_eq!(applied.get("atlas"), Some(&2));
|
||||
assert_eq!(edits.lock().unwrap().len(), 1);
|
||||
|
||||
let (broken, _seen) = super::super::tests::stub_forge(
|
||||
(200, "{}"),
|
||||
(200, r#"{"email":"someone@example.com"}"#),
|
||||
(200, "{}"),
|
||||
)
|
||||
.await;
|
||||
let mut applied = BTreeMap::new();
|
||||
apply(&broken, &[("atlas".to_owned(), 2)], &mut applied).await;
|
||||
assert!(applied.is_empty(), "a refused agent must not be recorded");
|
||||
let desired = limits(&[("atlas", 2)]);
|
||||
assert_eq!(plan(&desired, &applied), [("atlas".to_owned(), 2)]);
|
||||
}
|
||||
}
|
||||
|
|
@ -134,7 +134,7 @@ impl Client {
|
|||
|
||||
/// The store's `hive-agent-*` cert-auth roles: exactly the agents that hold a
|
||||
/// store identity.
|
||||
async fn read_roster() -> Result<Vec<String>> {
|
||||
pub(super) async fn read_roster() -> Result<Vec<String>> {
|
||||
let store = crate::store::connect()
|
||||
.await
|
||||
.context("logging in to the swarm secret store")?;
|
||||
|
|
|
|||
|
|
@ -591,7 +591,7 @@ mod tests {
|
|||
#[test]
|
||||
fn an_agent_called_hive_x_is_not_hive_xs_account() {
|
||||
// Both ways round: the hive job refuses the agent's account, and the
|
||||
// agent's own guard (`disable_repo_creation`) refuses the hive's,
|
||||
// agent's own guard (`limit_agent_account`) refuses the hive's,
|
||||
// because neither accepts the other's email.
|
||||
let login = forge::hive_login("alpha");
|
||||
for agents in [
|
||||
|
|
@ -638,7 +638,7 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
client
|
||||
.ensure_agent_user("hive-alpha")
|
||||
.ensure_agent_user("hive-alpha", 0)
|
||||
.await
|
||||
.expect_err("the hive's account is not the agent's");
|
||||
assert!(lockdown_patch(&seen, "hive-alpha").is_none());
|
||||
|
|
|
|||
|
|
@ -38,9 +38,10 @@ use super::{
|
|||
folds_into_success, is_ambiguous_validation_failure, is_confirmed_conflict,
|
||||
};
|
||||
|
||||
/// Forgejo org that owns agent-created repos. Agents can't create repos with
|
||||
/// their own token (`max_repo_creation = 0`); a repo an agent asks for is
|
||||
/// created here and the agent added as a **write** member (not owner/admin).
|
||||
/// Forgejo org that owns agent-created repos. An agent can create repos with
|
||||
/// its own token only up to its own `max_repos` (default 0, so none); a repo
|
||||
/// an agent asks for is created here and the agent added as a **write**
|
||||
/// member (not owner/admin).
|
||||
/// Because the org, not the agent, owns the repo, perms stay centrally
|
||||
/// managed and branch protection (referencing [`OPERATORS_TEAM`]) can block
|
||||
/// the author from merging their own PR.
|
||||
|
|
|
|||
Loading…
Reference in a new issue