fix: let data table forks copy foreign servers (#12848)

* fix: let a data table fork copy foreign servers on a managed cluster

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: grant a fork's wrapper usage only where its role lacks it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: leave extension-owned foreign servers out of a fork's wrapper needs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-12-04 09:01:59 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent ccb00b6daa
commit 6877dd8ed6
2 changed files with 72 additions and 1 deletions
@@ -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<String> = 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(())
}
}
+30
View File
@@ -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<String>,
/// 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<String>,
}
/// 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<String> = 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