diff --git a/backend/windmill-api-integration-tests/tests/role_overrides_sync.rs b/backend/windmill-api-integration-tests/tests/role_overrides_sync.rs index 48a1aa38fb..cbc3facd8a 100644 --- a/backend/windmill-api-integration-tests/tests/role_overrides_sync.rs +++ b/backend/windmill-api-integration-tests/tests/role_overrides_sync.rs @@ -341,6 +341,20 @@ async fn a_pulled_file_gives_the_same_operator_role_on_every_push( if push == 1 { assert_eq!(result["changes"], json!([]), "{result}"); } + if push == 0 { + assert!( + result.get("warnings").is_none(), + "the legacy key was left as pulled: {result}" + ); + } else { + // Not pulled again, the file's legacy key now disagrees with the workspace's role. + assert!( + result["warnings"][0] + .as_str() + .is_some_and(|w| w.contains("pull again")), + "{result}" + ); + } assert_eq!( operator().await?["capabilities"]["flows"], "none", @@ -488,3 +502,45 @@ async fn a_role_is_added_and_removed_with_the_instance_group_naming_it( assert_eq!(stored_overrides(&db, "group-mapping").await, None); Ok(()) } + +/// A settings.yaml pulled before the roles were in it carries the legacy column as it was then. +/// After a role edit made since, pushing that file unchanged must not re-grant what the edit +/// withdrew. +#[sqlx::test(migrations = "../migrations", fixtures("base"))] +async fn an_unchanged_file_without_the_roles_does_not_undo_a_role_edit( + db: Pool, +) -> anyhow::Result<()> { + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + create_workspace(&db, "pre-fix-file").await; + let base = format!("http://localhost:{port}/api/w/pre-fix-file/workspaces"); + let resp = authed(client().post(format!("{base}/operator_settings"))) + .json(&json!({ "runs": true, "builder_flows": true })) + .send() + .await?; + assert_eq!(resp.status(), 200, "{}", resp.text().await?); + let column: Value = sqlx::query_scalar( + "SELECT operator_settings FROM workspace_settings WHERE workspace_id = 'pre-fix-file'", + ) + .fetch_one(&db) + .await?; + let old_file = json!({ "name": "pre-fix-file", "operator_settings": column }); + + let resp = authed(client().post(format!("{base}/role_overrides"))) + .json(&json!({ "operator": { "capabilities": { "flows": "none" } } })) + .send() + .await?; + assert_eq!(resp.status(), 200, "{}", resp.text().await?); + + let result: Value = apply(port, "pre-fix-file", old_file, false) + .await + .json() + .await?; + assert_eq!(result["changes"], json!([]), "{result}"); + assert_eq!( + stored_overrides(&db, "pre-fix-file").await, + Some(json!({ "operator": { "capabilities": { "flows": "none" } } })) + ); + Ok(()) +} diff --git a/backend/windmill-api/src/workspace_settings_document.rs b/backend/windmill-api/src/workspace_settings_document.rs index ae104aef6c..dfff5bb142 100644 --- a/backend/windmill-api/src/workspace_settings_document.rs +++ b/backend/windmill-api/src/workspace_settings_document.rs @@ -957,6 +957,26 @@ async fn plan(db: &DB, w_id: &str, mut desired: Map, replace: boo None => current.remove("git_sync"), }; } + // A document without the roles applies `operator_settings` through the legacy endpoint, so + // it diffs against the column that endpoint writes, as it always did: against the value + // derived from the role, an unchanged file with a stale column would re-grant what a role + // edit since has withdrawn. + if !["role_overrides", "workspace_only_roles"] + .iter() + .any(|k| desired.contains_key(*k)) + { + let column: Option = sqlx::query_scalar( + "SELECT operator_settings FROM workspace_settings WHERE workspace_id = $1", + ) + .bind(w_id) + .fetch_optional(db) + .await? + .flatten(); + match column { + Some(column) => current.insert("operator_settings".to_string(), column), + None => current.remove("operator_settings"), + }; + } let mc = build_crypt(db, w_id).await?; let names = match desired.get("datatable_permissions") { Some(v) if !v.is_null() => DatatableNames::load(db, w_id, v).await?, @@ -1761,7 +1781,8 @@ fn plan_role_overrides(p: &mut Plan, instance: &[RoleDefinition]) -> Result>() .map_err(|e| Error::internal_err(format!("re-reading role overrides: {e:#}")))?; if let Some(stated) = p.managed("operator_settings").filter(|o| o.is_object()) { - check_operator_settings(&mut p.warnings, &stated, instance, &next)?; + let before = p.current.get("operator_settings").cloned(); + check_operator_settings(&mut p.warnings, &stated, before.as_ref(), instance, &next)?; } if !changed { return Ok(None); @@ -1790,11 +1811,13 @@ fn plan_role_overrides(p: &mut Plan, instance: &[RoleDefinition]) -> Result, stated: &Value, + before: Option<&Value>, instance: &[RoleDefinition], next: &RoleOverrides, ) -> Result<()> { @@ -1805,8 +1828,21 @@ fn check_operator_settings( return Ok(()); }; let implied = roles::legacy_operator_settings(&operator.capabilities); - // As the legacy endpoint reads it: an omitted page is hidden, an omitted write right kept. - let differs = object_of(Some(&implied)).iter().any(|(key, value)| { + if legacy_differs(stated, &implied) && before.is_none_or(|b| legacy_differs(stated, b)) { + warnings.push( + "operator_settings was not applied: it disagrees with role_overrides.operator, which \ + is what applies. Edit the operator role there, or pull again to refresh \ + operator_settings." + .to_string(), + ); + } + Ok(()) +} + +/// Whether the legacy endpoint would read `stated` as other than `reference`: an omitted page is +/// hidden, an omitted write right kept. +fn legacy_differs(stated: &Value, reference: &Value) -> bool { + object_of(Some(reference)).iter().any(|(key, value)| { let keeps = matches!( key.as_str(), "builder_flows" | "builder_apps" | "manage_schedules" | "manage_triggers" @@ -1815,15 +1851,7 @@ fn check_operator_settings( Some(v) => v != value, None => !keeps && value != &Value::Bool(false), } - }); - if differs { - warnings.push( - "operator_settings was not applied: it differs from the operator role stated in \ - role_overrides, which is what applies. Edit role_overrides.operator instead." - .to_string(), - ); - } - Ok(()) + }) } fn plan_datatable_permissions( @@ -2707,12 +2735,28 @@ mod tests { ); } + /// The `operator_settings` a pull derives from an `operator` override. + fn derived_operator_settings(operator: &Value) -> Value { + let overrides: RoleOverrides = + serde_json::from_value(json!({ "operator": operator })).unwrap(); + let roles = roles::effective_roles(&roles::default_instance_roles(), &overrides).unwrap(); + let operator = roles + .iter() + .find(|r| r.name == roles::OPERATOR_ROLE) + .unwrap(); + roles::legacy_operator_settings(&operator.capabilities) + } + fn roles_current() -> Map { let mut cur = current(); cur.insert( "role_overrides".to_string(), json!({ "operator": { "capabilities": { "flows": "compose" } } }), ); + cur.insert( + "operator_settings".to_string(), + derived_operator_settings(&json!({ "capabilities": { "flows": "compose" } })), + ); cur.insert( "workspace_only_roles".to_string(), if cfg!(feature = "enterprise") { @@ -2836,17 +2880,7 @@ mod tests { fn operator_settings_is_not_applied_beside_the_roles() { let instance = roles::default_instance_roles(); let same = roles_current(); - let consistent = roles::legacy_operator_settings( - &roles::effective_roles( - &instance, - &serde_json::from_value(same["role_overrides"].clone()).unwrap(), - ) - .unwrap() - .into_iter() - .find(|r| r.name == roles::OPERATOR_ROLE) - .unwrap() - .capabilities, - ); + let consistent = same["operator_settings"].clone(); let p = plan_roles( json!({ "operator_settings": consistent, @@ -2891,6 +2925,11 @@ mod tests { role_write(&p).unwrap()["operator"], json!({ "capabilities": { "flows": "none" } }) ); + assert!( + p.warnings.is_empty(), + "the legacy key left as pulled is no edit: {:?}", + p.warnings + ); // A file without the roles keeps applying `operator_settings`. let p = plan_roles(json!({ "operator_settings": edited }), false, &instance).unwrap(); diff --git a/docs/workspace-roles.md b/docs/workspace-roles.md index 158563d830..a89603151b 100644 --- a/docs/workspace-roles.md +++ b/docs/workspace-roles.md @@ -63,7 +63,10 @@ same document can name a role it adds; a removed workspace-only role is written still names it. The document's `operator_settings` is derived from the `operator` role, not read from the column (only the legacy endpoint rewrites it, so it goes stale). A document that states the roles is not applied through it: one edited in only one of the two would flip the role on every -push. When it says something other than the roles do, the apply warns instead. +push. When it was edited to say something other than the roles do, the apply warns instead. A +document without the roles (pulled before they were in it) still applies `operator_settings`, and +diffs it against the column rather than the derived value: against that, an unchanged file with a +stale column would re-grant what a role edit since has withdrawn. ## Who is restricted