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
1 changed files with 763 additions and 58 deletions
|
|
@ -44,6 +44,7 @@ use crate::webhook::DeliveryKind;
|
|||
use swarm_secret_client::stored_secret::StoredSecret;
|
||||
use swarm_wire::review_classifier::Commit;
|
||||
|
||||
pub mod agent_max_repos;
|
||||
pub mod agent_token;
|
||||
mod api;
|
||||
mod base_pulls;
|
||||
|
|
@ -749,27 +750,29 @@ impl Client {
|
|||
/// unconditionally, 422 only after a follow-up `user_get` confirms the
|
||||
/// user (see [`folds_into_success`] — not every 422 means it exists).
|
||||
///
|
||||
/// Then locks the account out of creating repos
|
||||
/// ([`Self::disable_repo_creation`]), whether it was just created or
|
||||
/// Then limits the account ([`Self::limit_agent_account`]) to `max_repos`
|
||||
/// repositories and no organizations, whether it was just created or
|
||||
/// already existed. Nothing else does that for a swarm-created agent, so
|
||||
/// a failure fails the node. Leaves an existing account's email as it
|
||||
/// finds it.
|
||||
pub async fn ensure_agent_user(&self, agent: &str) -> Result<()> {
|
||||
pub async fn ensure_agent_user(&self, agent: &str, max_repos: u32) -> Result<()> {
|
||||
self.create_agent_user(agent).await?;
|
||||
self.disable_repo_creation(agent).await
|
||||
self.limit_agent_account(agent, max_repos).await
|
||||
}
|
||||
|
||||
/// Set `max_repo_creation = 0` on `agent`'s account. Agents create repos
|
||||
/// through the hive, never with their own token — a write-scoped token
|
||||
/// can otherwise create and own a repo and self-merge in it, bypassing
|
||||
/// the operator-only-merge policy. Existing repos are untouched; push,
|
||||
/// PRs and clone keep working.
|
||||
/// Set `max_repo_creation = max_repos` and `allow_create_organization =
|
||||
/// false` on `agent`'s account. At 0 an agent creates repos only through
|
||||
/// the hive, never with its own token; above 0 a write-scoped token can
|
||||
/// create and own that many repos and self-merge in them, bypassing the
|
||||
/// operator-only-merge policy. Existing repos are untouched; push, PRs and
|
||||
/// clone keep working. An account that may not create organizations cannot
|
||||
/// get around the limit by making an organization to own repos in.
|
||||
///
|
||||
/// The PATCH itself is unconditional rather than verify-then-patch:
|
||||
/// Forgejo's API never returns `max_repo_creation` (it is a field of
|
||||
/// `EditUserOption` only, not of `User`), so there is no current value
|
||||
/// to compare against, and setting 0 on an account already at 0 is a
|
||||
/// no-op. But `create_agent_user`'s idempotent fold only proves *some*
|
||||
/// to compare against, and setting the same value again is a no-op. But
|
||||
/// `create_agent_user`'s idempotent fold only proves *some*
|
||||
/// account named `agent` exists — a 409 folds with no read at all, and
|
||||
/// a 422's follow-up `user_get` only confirms the username, not that
|
||||
/// swarm-controller created it — so before that PATCH this reads the
|
||||
|
|
@ -781,31 +784,35 @@ impl Client {
|
|||
/// name collided with an account this node didn't create, so it fails
|
||||
/// the node rather than sending the PATCH (or silently skipping) to an
|
||||
/// account nobody verified is agent-controlled.
|
||||
async fn disable_repo_creation(&self, agent: &str) -> Result<()> {
|
||||
pub(crate) async fn limit_agent_account(&self, agent: &str, max_repos: u32) -> Result<()> {
|
||||
let user = self
|
||||
.api
|
||||
.call(|api| api.user_get(agent).into_future())
|
||||
.await
|
||||
.with_context(|| format!("read forge user {agent} before locking it down"))?;
|
||||
.with_context(|| format!("read forge user {agent} before limiting it"))?;
|
||||
let expected = agent_email(agent);
|
||||
let legacy = legacy_agent_email(agent);
|
||||
if !matches!(user.email.as_deref(), Some(e) if e == expected || e == legacy) {
|
||||
anyhow::bail!(
|
||||
"forge account `{agent}` exists but is not this agent's (email {:?}, expected \
|
||||
`{expected}` or the legacy `{legacy}`); refusing to lock it down",
|
||||
`{expected}` or the legacy `{legacy}`); refusing to limit it",
|
||||
user.email
|
||||
);
|
||||
}
|
||||
self.api
|
||||
.call(|api| {
|
||||
api.admin_edit_user(agent, repo_creation_lockdown(agent))
|
||||
api.admin_edit_user(agent, agent_account_lockdown(agent, max_repos))
|
||||
.into_future()
|
||||
})
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("disable repo creation for forge user {agent} (max_repo_creation = 0)")
|
||||
format!("limit forge user {agent} (max_repo_creation = {max_repos})")
|
||||
})?;
|
||||
tracing::info!(%agent, "swarm forge: disabled repo creation (max_repo_creation = 0)");
|
||||
tracing::info!(
|
||||
%agent,
|
||||
max_repos,
|
||||
"swarm forge: limited the account (max_repo_creation, no organizations)"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -2167,7 +2174,7 @@ fn initial_flake_nix() -> &'static str {
|
|||
|
||||
/// Canonical email for an agent's Forgejo account, set by
|
||||
/// [`Client::create_agent_user`] at creation and checked by
|
||||
/// [`Client::disable_repo_creation`] before it sends the account's PATCH. Same
|
||||
/// [`Client::limit_agent_account`] before it sends the account's PATCH. Same
|
||||
/// format string as `hive-c0re::forge::users::agent_email` — not called
|
||||
/// across the crate boundary (see this module's doc comment), but both
|
||||
/// creators use this exact value, so it doubles as the
|
||||
|
|
@ -2179,7 +2186,7 @@ fn agent_email(agent: &str) -> String {
|
|||
|
||||
/// The email older hive-created agent accounts carry, and keep: nothing
|
||||
/// rewrites it to [`agent_email`]. Accepted by
|
||||
/// [`Client::disable_repo_creation`]'s guard alongside [`agent_email`] so
|
||||
/// [`Client::limit_agent_account`]'s guard alongside [`agent_email`] so
|
||||
/// such an account is still recognised as the agent's own.
|
||||
fn legacy_agent_email(agent: &str) -> String {
|
||||
format!("{agent}@hive.local")
|
||||
|
|
@ -2215,6 +2222,17 @@ fn repo_creation_lockdown(agent: &str) -> EditUserOption {
|
|||
}
|
||||
}
|
||||
|
||||
/// [`repo_creation_lockdown`] for an agent's account: `max_repos` repositories
|
||||
/// instead of none, and no organizations. The service accounts that share the
|
||||
/// lockdown stay at none and are not touched on organizations.
|
||||
fn agent_account_lockdown(agent: &str, max_repos: u32) -> EditUserOption {
|
||||
EditUserOption {
|
||||
allow_create_organization: Some(false),
|
||||
max_repo_creation: Some(i64::from(max_repos)),
|
||||
..repo_creation_lockdown(agent)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a create-style call failed because the object already
|
||||
/// exists. Forgejo signals this as HTTP 409 (conflict) or 422
|
||||
/// (validation) — match both defensively, same as
|
||||
|
|
@ -2842,10 +2860,11 @@ mod tests {
|
|||
async fn a_created_agent_user_gets_repo_creation_disabled() {
|
||||
let (client, seen) = stub_forge((201, "{}"), own_account_get("alice"), (200, "{}")).await;
|
||||
|
||||
client.ensure_agent_user("alice").await.unwrap();
|
||||
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||
|
||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||
assert_eq!(body["max_repo_creation"], 0);
|
||||
assert_eq!(body["allow_create_organization"], false);
|
||||
let set: Vec<&str> = body
|
||||
.as_object()
|
||||
.unwrap()
|
||||
|
|
@ -2853,11 +2872,35 @@ mod tests {
|
|||
.filter(|(_, v)| !v.is_null())
|
||||
.map(|(k, _)| k.as_str())
|
||||
.collect();
|
||||
assert_eq!(set, ["login_name", "max_repo_creation", "source_id"]);
|
||||
assert_eq!(
|
||||
set,
|
||||
[
|
||||
"allow_create_organization",
|
||||
"login_name",
|
||||
"max_repo_creation",
|
||||
"source_id"
|
||||
]
|
||||
);
|
||||
assert_eq!(body["login_name"], "alice");
|
||||
assert_eq!(body["source_id"], 0);
|
||||
}
|
||||
|
||||
/// The agent's limit is what goes out as `max_repo_creation`, on a created
|
||||
/// account and an existing one alike, and organizations stay refused at any
|
||||
/// limit.
|
||||
#[tokio::test]
|
||||
async fn the_agents_limit_is_what_the_account_is_set_to() {
|
||||
for create in [(201, "{}"), (409, r#"{"message":"user already exists"}"#)] {
|
||||
let (client, seen) = stub_forge(create, own_account_get("alice"), (200, "{}")).await;
|
||||
|
||||
client.ensure_agent_user("alice", 3).await.unwrap();
|
||||
|
||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||
assert_eq!(body["max_repo_creation"], 3, "{create:?}");
|
||||
assert_eq!(body["allow_create_organization"], false, "{create:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An account that already existed is locked down too: the node is
|
||||
/// re-run, and an account created before this lockdown existed must
|
||||
/// not keep repo creation just because the create step folded to
|
||||
|
|
@ -2872,7 +2915,7 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
|
||||
client.ensure_agent_user("alice").await.unwrap();
|
||||
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||
|
||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||
assert_eq!(body["max_repo_creation"], 0);
|
||||
|
|
@ -2891,7 +2934,7 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
|
||||
let err = client.ensure_agent_user("alice").await.unwrap_err();
|
||||
let err = client.ensure_agent_user("alice", 0).await.unwrap_err();
|
||||
|
||||
let msg = format!("{err:#}");
|
||||
assert!(msg.contains("max_repo_creation"), "{msg}");
|
||||
|
|
@ -2915,7 +2958,7 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
|
||||
let err = client.ensure_agent_user("alice").await.unwrap_err();
|
||||
let err = client.ensure_agent_user("alice", 0).await.unwrap_err();
|
||||
|
||||
let msg = format!("{err:#}");
|
||||
assert!(msg.contains("alice"), "{msg}");
|
||||
|
|
@ -2945,7 +2988,7 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
|
||||
client.ensure_agent_user("alice").await.unwrap();
|
||||
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||
|
||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||
assert_eq!(body["max_repo_creation"], 0);
|
||||
|
|
@ -2963,7 +3006,7 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
|
||||
client.ensure_agent_user("alice").await.unwrap();
|
||||
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||
|
||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||
assert_eq!(body["max_repo_creation"], 0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue