diff --git a/docs/4-implementation/swarm/review-classifier.md b/docs/4-implementation/swarm/review-classifier.md index 974d39699..1fc51f1e5 100644 --- a/docs/4-implementation/swarm/review-classifier.md +++ b/docs/4-implementation/swarm/review-classifier.md @@ -330,9 +330,9 @@ authors every classifier review, and it pushes the branches the try-out section creates. `swarm-controller` creates it as `review-classifier`, with the email -`review-classifier@services.hyperhive.local` and no repo creation, and mints -its one token, `swarm-review-classifier` with scope `write:repository`, into -the store at `swarm/services/review-classifier/forge/token`. A pass at start +`review-classifier@services.hyperhive.local`, no repo creation and no +organizations, and mints its one token, `swarm-review-classifier` with scope +`write:repository`, into the store at `swarm/services/review-classifier/forge/token`. A pass at start and every five minutes re-mints when the token is missing or no longer matches the forge. The controller refuses an existing account by that name with any other email. The controller posts the reviews and pushes the try-out diff --git a/swarm-controller/src/forge.rs b/swarm-controller/src/forge.rs index ce5c787b5..e4a57a2fb 100644 --- a/swarm-controller/src/forge.rs +++ b/swarm-controller/src/forge.rs @@ -2183,16 +2183,17 @@ fn legacy_agent_email(agent: &str) -> String { format!("{agent}@hive.local") } -/// The `admin_edit_user` body that sets `max_repo_creation = 0` on `agent` -/// and nothing else. `login_name` + `source_id = 0` (local auth, -/// which is what [`Client::create_agent_user`] creates) ride along because -/// Forgejo resets `use_custom_avatar` on an admin edit that omits -/// `login_name`; every other field is left unset so nothing else is touched. +/// The `admin_edit_user` body that sets `max_repo_creation = 0` and +/// `allow_create_organization = false` on `agent` and nothing else. +/// `login_name` + `source_id = 0` (local auth, which is what +/// [`Client::create_agent_user`] creates) ride along because Forgejo resets +/// `use_custom_avatar` on an admin edit that omits `login_name`; every other +/// field is left unset so nothing else is touched. fn repo_creation_lockdown(agent: &str) -> EditUserOption { EditUserOption { active: None, admin: None, - allow_create_organization: None, + allow_create_organization: Some(false), allow_git_hook: None, allow_import_local: None, description: None, @@ -2214,11 +2215,9 @@ 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. +/// instead of none. 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) } diff --git a/swarm-controller/src/forge/builder_account.rs b/swarm-controller/src/forge/builder_account.rs index c54536791..c50d214a8 100644 --- a/swarm-controller/src/forge/builder_account.rs +++ b/swarm-controller/src/forge/builder_account.rs @@ -288,4 +288,25 @@ mod tests { assert!(format!("{err:#}").contains("not the swarm builder's")); assert!(lockdown_patch(&seen, LOGIN).is_none()); } + + /// An existing account is locked out of repo creation and of + /// organizations on every pass. + #[tokio::test] + async fn the_job_locks_the_builder_out_of_repos_and_organizations() { + use super::super::tests::{lockdown_patch, stub_forge}; + + let (client, seen) = stub_forge( + (409, r#"{"message":"user already exists"}"#), + (200, r#"{"email":"builder@services.hyperhive.local"}"#), + (200, "{}"), + ) + .await; + client + .ensure_builder_user() + .await + .expect("the builder's own account"); + let body = lockdown_patch(&seen, LOGIN).expect("no lockdown PATCH sent"); + assert_eq!(body["max_repo_creation"], 0); + assert_eq!(body["allow_create_organization"], false); + } } diff --git a/swarm-controller/src/forge/ci_metrics_account.rs b/swarm-controller/src/forge/ci_metrics_account.rs index 248244d2b..f5f5c45fb 100644 --- a/swarm-controller/src/forge/ci_metrics_account.rs +++ b/swarm-controller/src/forge/ci_metrics_account.rs @@ -473,4 +473,25 @@ mod tests { assert!(format!("{err:#}").contains("not the CI metrics poller's")); assert!(lockdown_patch(&seen, LOGIN).is_none()); } + + /// An existing account is locked out of repo creation and of + /// organizations on every pass. + #[tokio::test] + async fn the_job_locks_the_poller_out_of_repos_and_organizations() { + use super::super::tests::{lockdown_patch, stub_forge}; + + let (client, seen) = stub_forge( + (409, r#"{"message":"user already exists"}"#), + (200, r#"{"email":"ci-metrics@services.hyperhive.local"}"#), + (200, "{}"), + ) + .await; + client + .ensure_ci_metrics_user() + .await + .expect("the poller's own account"); + let body = lockdown_patch(&seen, LOGIN).expect("no lockdown PATCH sent"); + assert_eq!(body["max_repo_creation"], 0); + assert_eq!(body["allow_create_organization"], false); + } } diff --git a/swarm-controller/src/forge/classifier_account.rs b/swarm-controller/src/forge/classifier_account.rs index e9113df05..d54d5699f 100644 --- a/swarm-controller/src/forge/classifier_account.rs +++ b/swarm-controller/src/forge/classifier_account.rs @@ -582,5 +582,6 @@ mod tests { .expect("the classifier's own account"); let body = lockdown_patch(&seen, LOGIN).expect("no lockdown PATCH sent"); assert_eq!(body["max_repo_creation"], 0); + assert_eq!(body["allow_create_organization"], false); } } diff --git a/swarm-controller/src/forge/hive_token.rs b/swarm-controller/src/forge/hive_token.rs index 820381530..3434d6e8f 100644 --- a/swarm-controller/src/forge/hive_token.rs +++ b/swarm-controller/src/forge/hive_token.rs @@ -660,6 +660,7 @@ mod tests { .expect("the hive's own account"); let body = lockdown_patch(&seen, "hive-alpha").expect("no lockdown PATCH sent"); assert_eq!(body["max_repo_creation"], 0); + assert_eq!(body["allow_create_organization"], false); } fn team(permission: TeamPermission, units: &[&str]) -> Team {