diff --git a/backend/windmill-store/src/resources.rs b/backend/windmill-store/src/resources.rs index 3c4b032b93..fe13de0146 100644 --- a/backend/windmill-store/src/resources.rs +++ b/backend/windmill-store/src/resources.rs @@ -25,6 +25,7 @@ use windmill_common::workspaces::{check_deploy_rules, operator_can_draft, RuleCh use crate::secret_backend_ext::rename_vault_secret; use crate::var_resource_cache::{auth_identity, cache_resource, get_cached_resource}; +use crate::variables::{describe_authed, describe_extra_perms}; use windmill_common::utils::{ check_proper_path, check_proper_type_name, escape_ilike_pattern, sanitize_db_error, BulkDeleteRequest, @@ -727,13 +728,16 @@ pub async fn explain_resource_perm_error( .fetch_optional(db) .await?; return Err(Error::NotAuthorized(format!( - "Resource exists but you don't have access to it:\nresource perms: {}\nfolder perms: {}\nauthed as: {authed:?}", - serde_json::to_string_pretty(&extra_perms).unwrap_or_default(), serde_json::to_string_pretty(&folder_extra_perms).unwrap_or_default() + "Resource exists but you don't have access to it:\nresource perms: {}\nfolder perms: {}\nauthed as: {}", + describe_extra_perms(Some(&extra_perms)), + describe_extra_perms(folder_extra_perms.as_ref()), + describe_authed(Some(authed)) ))); } else { return Err(Error::NotAuthorized(format!( - "Resource exists but you don't have access to it:\nresource perms: {}\nauthed as: {authed:?}", - serde_json::to_string_pretty(&extra_perms).unwrap_or_default() + "Resource exists but you don't have access to it:\nresource perms: {}\nauthed as: {}", + describe_extra_perms(Some(&extra_perms)), + describe_authed(Some(authed)) ))); } } diff --git a/backend/windmill-store/src/variables.rs b/backend/windmill-store/src/variables.rs index ef70e59bb7..2e5c4902ba 100644 --- a/backend/windmill-store/src/variables.rs +++ b/backend/windmill-store/src/variables.rs @@ -480,7 +480,7 @@ async fn get_value( /// The grants alone can't explain a denial: a job started on behalf of another /// user is authorized as that user, so the error has to name who was actually /// asking or it reads as a grant bug. -fn describe_authed(authed: Option<&(impl Authable + Sync)>) -> String { +pub(crate) fn describe_authed(authed: Option<&(impl Authable + Sync)>) -> String { match authed { Some(authed) => format!( "username: {}, email: {}, groups: {:?}, folders: {:?}", @@ -493,6 +493,25 @@ fn describe_authed(authed: Option<&(impl Authable + Sync)>) -> String { } } +/// `extra_perms` maps a `u/` or `g/` to whether it may write; the raw `true` / +/// `false` reads as allowed / denied, so name the access level instead. +pub(crate) fn describe_extra_perms(perms: Option<&Value>) -> String { + let Some(perms) = perms else { + return "folder not found".to_string(); + }; + let Some(map) = perms.as_object().filter(|m| !m.is_empty()) else { + return "none".to_string(); + }; + map.iter() + .map(|(grantee, can_write)| match can_write.as_bool() { + Some(true) => format!("{grantee} (read & write)"), + Some(false) => format!("{grantee} (read only)"), + None => format!("{grantee} ({can_write})"), + }) + .collect::>() + .join(", ") +} + /// Including the variable's and its folder's `extra_perms` in the error is intended, not a /// leak: who holds access to an item is not secret within a workspace, and it tells the /// denied caller whom to ask. Keep the maps in the message. @@ -526,12 +545,14 @@ async fn explain_variable_perm_error( .await?; return Err(Error::NotAuthorized(format!( "Variable exists but you don't have access to it:\nvariable perms: {}\nfolder perms: {}\nauthed as: {}", - serde_json::to_string_pretty(&extra_perms).unwrap_or_default(), serde_json::to_string_pretty(&folder_extra_perms).unwrap_or_default(), describe_authed(authed) + describe_extra_perms(Some(&extra_perms)), + describe_extra_perms(folder_extra_perms.as_ref()), + describe_authed(authed) ))); } else { return Err(Error::NotAuthorized(format!( "Variable exists but you don't have access to it:\nvariable perms: {}\nauthed as: {}", - serde_json::to_string_pretty(&extra_perms).unwrap_or_default(), + describe_extra_perms(Some(&extra_perms)), describe_authed(authed) ))); }