diff --git a/backend/windmill-api-workspaces/src/datatable_extensions.rs b/backend/windmill-api-workspaces/src/datatable_extensions.rs index 0aaef0cbdd..f01aa155eb 100644 --- a/backend/windmill-api-workspaces/src/datatable_extensions.rs +++ b/backend/windmill-api-workspaces/src/datatable_extensions.rs @@ -101,7 +101,21 @@ async fn read_plan( SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_namespace'::regclass AND d.objid = n.oid AND d.deptype = 'e'), - 'pg_namespace'::regclass::oid, n.oid, obj_description(n.oid, 'pg_namespace') + 'pg_namespace'::regclass::oid, n.oid, obj_description(n.oid, 'pg_namespace'), + ARRAY( + SELECT w.fdwname::text + FROM pg_foreign_data_wrapper w JOIN pg_depend d + ON d.classid = 'pg_foreign_data_wrapper'::regclass AND d.objid = w.oid + WHERE d.refclassid = 'pg_extension'::regclass AND d.refobjid = e.oid + AND d.deptype = 'e' + AND has_foreign_data_wrapper_privilege(w.oid, 'USAGE') + AND EXISTS ( + SELECT 1 FROM pg_foreign_server s + WHERE s.srvfdw = w.oid AND NOT EXISTS ( + SELECT 1 FROM pg_depend sd + WHERE sd.classid = 'pg_foreign_server'::regclass + AND sd.objid = s.oid AND sd.deptype = 'e')) + ORDER BY 1) FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace WHERE e.extname <> 'plpgsql' ORDER BY e.oid", @@ -121,6 +135,7 @@ async fn read_plan( name: row.get(0), schema: row.get(1), schema_comment: if dumped_schema { row.get(5) } else { None }, + wrappers: row.get(6), }); } plan.extension_schemas = extension_schemas.into_iter().collect(); @@ -718,6 +733,32 @@ impl ExtensionPlan { )) })?; } + let wrappers: Vec<&String> = self.extensions.iter().flat_map(|e| &e.wrappers).collect(); + if wrappers.is_empty() { + return Ok(()); + } + let denied: Vec = client + .query( + "SELECT fdwname::text FROM pg_foreign_data_wrapper + WHERE fdwname = ANY($1) AND NOT has_foreign_data_wrapper_privilege(oid, 'USAGE') + ORDER BY 1", + &[&wrappers], + ) + .await + .map_err(target_err)? + .iter() + .map(|row| row.get(0)) + .collect(); + if !denied.is_empty() { + return Err(Error::BadRequest(format!( + "The copy's foreign servers use foreign-data wrappers ({}) that role '{}' may not \ + use in database '{}': grant the role USAGE on them in the server's template1 \ + database, which new databases are made from", + denied.join(", "), + target.login_name(), + target.dbname + ))); + } Ok(()) } } diff --git a/backend/windmill-common/src/lib.rs b/backend/windmill-common/src/lib.rs index 21179411bc..014211d5fe 100644 --- a/backend/windmill-common/src/lib.rs +++ b/backend/windmill-common/src/lib.rs @@ -2179,6 +2179,9 @@ pub struct DatabaseExtension { pub schema: String, /// The schema's comment, which the copy's own replay may not be allowed to set. pub schema_comment: Option, + /// The extension's foreign-data wrappers that the source's foreign servers use and its role may + /// use. Replaying those servers takes `USAGE` on them, which a copy's role may not grant itself. + pub wrappers: Vec, } /// Create `extensions` in database `dbname`, just created, on `client`, the cluster @@ -2190,6 +2193,7 @@ pub struct DatabaseExtension { /// must hold nothing that a role other than a superuser or the administrator owns, and a schema /// made for an extension is the administrator's until the extension is in it, then /// `custom_instance_user`'s, or failing that open to it, as in the database it was copied from. +/// `custom_instance_user` is also let use each extension's `wrappers`, as it could in the source. pub(crate) async fn provision_extensions( client: &tokio_postgres::Client, dbname: &str, @@ -2274,6 +2278,32 @@ pub(crate) async fn provision_extensions( .batch_execute(&format!("BEGIN;\n{setup}COMMIT;")) .await .map_err(|e| err("Creating the extensions of", e))?; + // Only where the role lacks it: a wrapper `template1` already grants it on may be one the + // administrator holds no rights on, so its grant would fail. + let wrappers: Vec<&String> = extensions.iter().flat_map(|e| &e.wrappers).collect(); + if !wrappers.is_empty() { + let lacking: Vec = client + .query( + "SELECT fdwname::text FROM pg_foreign_data_wrapper + WHERE fdwname = ANY($1) + AND NOT has_foreign_data_wrapper_privilege('custom_instance_user', oid, 'USAGE')", + &[&wrappers], + ) + .await + .map_err(|e| err("Checking", e))? + .iter() + .map(|row| row.get(0)) + .collect(); + for wrapper in lacking { + client + .batch_execute(&format!( + "GRANT USAGE ON FOREIGN DATA WRAPPER {} TO custom_instance_user", + datatable_roles::quote_ident(&wrapper) + )) + .await + .map_err(|e| err("Granting the foreign-data wrappers of", e))?; + } + } // While the administrator still owns the schemas it made: the copy's role may own neither one // the database already had nor one the handover below falls back from, and only an owner may // comment on a schema. The server quotes the comment, the source's text, so no setting of