mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-03 08:10:27 +00:00
fix: accept instance groups as g/ principals in deploy identity checks (#12823)
* fix: accept instance groups as g/ principals in deploy identity checks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep instance-group identities when cloning a workspace Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: drop unrelated formatting changes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: state group_exists authorization contract 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:
co-authored by
Claude Opus 5.5
parent
48d2b46a65
commit
5cf82bcbb8
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT EXISTS(SELECT 1 FROM group_ WHERE workspace_id = $1 AND name = $2)\n OR EXISTS(SELECT 1 FROM instance_group WHERE name = $2)",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "?column?",
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "93174731a58808fe9c6bef2451b35e5ff8c577c4bfbc08f5f975f38cf46ee7f5"
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "INSERT INTO script (\n workspace_id, hash, path, parent_hashes, summary, description, content,\n created_by, created_at, archived, schema, deleted, is_template,\n extra_perms, lock, lock_error_logs, language, kind, tag,\n envs, concurrent_limit, concurrency_time_window_s, cache_ttl,\n dedicated_worker, ws_error_handler_muted, priority, timeout,\n delete_after_use, delete_after_secs, restart_unless_cancelled, concurrency_key,\n visible_to_runner_only, auto_kind, codebase, has_preprocessor,\n on_behalf_of, on_behalf_of_email, assets, modules, job_token_scopes, labels\n )\n SELECT\n $1, hash, path, parent_hashes, summary, description, content,\n created_by, created_at, archived, schema, deleted, is_template,\n extra_perms, lock, lock_error_logs, language, kind, tag,\n envs, concurrent_limit, concurrency_time_window_s, cache_ttl,\n dedicated_worker, ws_error_handler_muted, priority, timeout,\n delete_after_use, delete_after_secs, restart_unless_cancelled, concurrency_key,\n visible_to_runner_only, auto_kind, codebase, has_preprocessor,\n -- Same three forms and the same prefix-first rule as permissioned_as_exists,\n -- superadmin fallback included: one acting outside their workspaces has no usr\n -- row but still authenticates.\n CASE WHEN on_behalf_of LIKE 'u/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $1::varchar\n AND u.username = substring(on_behalf_of from 3)\n UNION ALL\n SELECT 1 FROM password p WHERE p.super_admin\n AND (p.username = substring(on_behalf_of from 3)\n OR p.email = substring(on_behalf_of from 3))))\n WHEN on_behalf_of LIKE 'g/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM group_ g WHERE g.workspace_id = $1::varchar\n AND g.name = substring(on_behalf_of from 3)))\n ELSE\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $1::varchar\n AND u.username = on_behalf_of\n UNION ALL\n SELECT 1 FROM password p WHERE p.email = on_behalf_of\n AND p.super_admin))\n END, on_behalf_of_email, assets, modules, job_token_scopes, labels\n FROM script\n WHERE workspace_id = $2",
|
||||
"query": "INSERT INTO script (\n workspace_id, hash, path, parent_hashes, summary, description, content,\n created_by, created_at, archived, schema, deleted, is_template,\n extra_perms, lock, lock_error_logs, language, kind, tag,\n envs, concurrent_limit, concurrency_time_window_s, cache_ttl,\n dedicated_worker, ws_error_handler_muted, priority, timeout,\n delete_after_use, delete_after_secs, restart_unless_cancelled, concurrency_key,\n visible_to_runner_only, auto_kind, codebase, has_preprocessor,\n on_behalf_of, on_behalf_of_email, assets, modules, job_token_scopes, labels\n )\n SELECT\n $1, hash, path, parent_hashes, summary, description, content,\n created_by, created_at, archived, schema, deleted, is_template,\n extra_perms, lock, lock_error_logs, language, kind, tag,\n envs, concurrent_limit, concurrency_time_window_s, cache_ttl,\n dedicated_worker, ws_error_handler_muted, priority, timeout,\n delete_after_use, delete_after_secs, restart_unless_cancelled, concurrency_key,\n visible_to_runner_only, auto_kind, codebase, has_preprocessor,\n -- Same three forms and the same prefix-first rule as permissioned_as_exists,\n -- superadmin fallback included: one acting outside their workspaces has no usr\n -- row but still authenticates.\n CASE WHEN on_behalf_of LIKE 'u/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $1::varchar\n AND u.username = substring(on_behalf_of from 3)\n UNION ALL\n SELECT 1 FROM password p WHERE p.super_admin\n AND (p.username = substring(on_behalf_of from 3)\n OR p.email = substring(on_behalf_of from 3))))\n WHEN on_behalf_of LIKE 'g/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM group_ g WHERE g.workspace_id = $1::varchar\n AND g.name = substring(on_behalf_of from 3)\n UNION ALL\n SELECT 1 FROM instance_group ig\n WHERE ig.name = substring(on_behalf_of from 3)))\n ELSE\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $1::varchar\n AND u.username = on_behalf_of\n UNION ALL\n SELECT 1 FROM password p WHERE p.email = on_behalf_of\n AND p.super_admin))\n END, on_behalf_of_email, assets, modules, job_token_scopes, labels\n FROM script\n WHERE workspace_id = $2",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
@@ -11,5 +11,5 @@
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "78d53c53d22bdd8948a3375a19a2b3a87eb50150eb7b6888bc32a9d48a5ad21f"
|
||||
"hash": "9c05787fa5f0098ea1e9bc8cf2862075aadae26f1976626312d558393f785dbe"
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "INSERT INTO flow (\n workspace_id, path, summary, description, value, edited_by, edited_at,\n archived, schema, extra_perms, dependency_job, tag,\n ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only,\n concurrency_key, versions, on_behalf_of, on_behalf_of_email, lock_error_logs,\n job_token_scopes, labels\n )\n SELECT $2, path, summary, description, value, edited_by, edited_at,\n archived, schema, extra_perms, NULL, tag,\n ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only,\n concurrency_key, ARRAY[]::bigint[],\n -- Same predicate as clone_scripts.\n CASE WHEN on_behalf_of LIKE 'u/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $2::varchar\n AND u.username = substring(on_behalf_of from 3)\n UNION ALL\n SELECT 1 FROM password p WHERE p.super_admin\n AND (p.username = substring(on_behalf_of from 3)\n OR p.email = substring(on_behalf_of from 3))))\n WHEN on_behalf_of LIKE 'g/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM group_ g WHERE g.workspace_id = $2::varchar\n AND g.name = substring(on_behalf_of from 3)))\n ELSE\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $2::varchar\n AND u.username = on_behalf_of\n UNION ALL\n SELECT 1 FROM password p WHERE p.email = on_behalf_of\n AND p.super_admin))\n END, on_behalf_of_email, lock_error_logs, job_token_scopes, labels\n FROM flow\n WHERE workspace_id = $1",
|
||||
"query": "INSERT INTO flow (\n workspace_id, path, summary, description, value, edited_by, edited_at,\n archived, schema, extra_perms, dependency_job, tag,\n ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only,\n concurrency_key, versions, on_behalf_of, on_behalf_of_email, lock_error_logs,\n job_token_scopes, labels\n )\n SELECT $2, path, summary, description, value, edited_by, edited_at,\n archived, schema, extra_perms, NULL, tag,\n ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only,\n concurrency_key, ARRAY[]::bigint[],\n -- Same predicate as clone_scripts.\n CASE WHEN on_behalf_of LIKE 'u/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $2::varchar\n AND u.username = substring(on_behalf_of from 3)\n UNION ALL\n SELECT 1 FROM password p WHERE p.super_admin\n AND (p.username = substring(on_behalf_of from 3)\n OR p.email = substring(on_behalf_of from 3))))\n WHEN on_behalf_of LIKE 'g/%' THEN\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM group_ g WHERE g.workspace_id = $2::varchar\n AND g.name = substring(on_behalf_of from 3)\n UNION ALL\n SELECT 1 FROM instance_group ig\n WHERE ig.name = substring(on_behalf_of from 3)))\n ELSE\n (SELECT on_behalf_of WHERE EXISTS (\n SELECT 1 FROM usr u WHERE u.workspace_id = $2::varchar\n AND u.username = on_behalf_of\n UNION ALL\n SELECT 1 FROM password p WHERE p.email = on_behalf_of\n AND p.super_admin))\n END, on_behalf_of_email, lock_error_logs, job_token_scopes, labels\n FROM flow\n WHERE workspace_id = $1",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
@@ -11,5 +11,5 @@
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "4a5679413cd1f5b1b2822a4804a54b2a5a008d7c3313c90fb80d431057223b34"
|
||||
"hash": "e6cdb28cabb7dc63403c642b35a51bf8b75c788a06fb1adaa4c7f55bb716946b"
|
||||
}
|
||||
@@ -810,3 +810,145 @@ async fn test_folder_default_permissioned_as(db: Pool<Postgres>) -> anyhow::Resu
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An instance group (e.g. SCIM-provisioned) has no `group_` row in the workspace, yet is a
|
||||
/// valid `g/` principal: a folder rule or an explicit on_behalf_of naming it must deploy, and a
|
||||
/// fork must keep it.
|
||||
#[sqlx::test(fixtures("folder_default_permissioned_as"))]
|
||||
async fn test_instance_group_permissioned_as(db: Pool<Postgres>) -> anyhow::Result<()> {
|
||||
initialize_tracing().await;
|
||||
|
||||
let server = ApiServer::start(db.clone()).await?;
|
||||
let port = server.addr.port();
|
||||
let base = format!("http://localhost:{port}/api/w/test-workspace");
|
||||
|
||||
sqlx::query("INSERT INTO instance_group (name) VALUES ('scim-group')")
|
||||
.execute(&db)
|
||||
.await?;
|
||||
|
||||
let resp = authed(
|
||||
client().post(format!("{base}/folders/create")),
|
||||
"SECRET_TOKEN",
|
||||
)
|
||||
.json(&json!({
|
||||
"name": "hub",
|
||||
"default_permissioned_as": [{ "path_glob": "**", "permissioned_as": "g/scim-group" }]
|
||||
}))
|
||||
.send()
|
||||
.await?;
|
||||
assert_eq!(resp.status(), 200, "create folder: {}", resp.text().await?);
|
||||
|
||||
let stored_on_behalf_of_in = |w_id: &'static str, path: &'static str| {
|
||||
let db = db.clone();
|
||||
async move {
|
||||
sqlx::query!(
|
||||
"SELECT on_behalf_of FROM script WHERE path = $1 AND workspace_id = $2 ORDER BY created_at DESC LIMIT 1",
|
||||
path,
|
||||
w_id
|
||||
)
|
||||
.fetch_one(&db)
|
||||
.await
|
||||
.map(|r| r.on_behalf_of)
|
||||
}
|
||||
};
|
||||
let stored_on_behalf_of = |path: &'static str| stored_on_behalf_of_in("test-workspace", path);
|
||||
|
||||
let resp = authed(
|
||||
client().post(format!("{base}/scripts/create")),
|
||||
"SECRET_TOKEN",
|
||||
)
|
||||
.json(&new_script("f/hub/from_rule"))
|
||||
.send()
|
||||
.await?;
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
201,
|
||||
"folder rule naming an instance group: {}",
|
||||
resp.text().await?
|
||||
);
|
||||
assert_eq!(
|
||||
stored_on_behalf_of("f/hub/from_rule").await?.as_deref(),
|
||||
Some("g/scim-group")
|
||||
);
|
||||
|
||||
let mut explicit = new_script("u/test-user/explicit");
|
||||
explicit["on_behalf_of"] = json!("g/scim-group");
|
||||
explicit["preserve_on_behalf_of"] = json!(true);
|
||||
let resp = authed(
|
||||
client().post(format!("{base}/scripts/create")),
|
||||
"SECRET_TOKEN",
|
||||
)
|
||||
.json(&explicit)
|
||||
.send()
|
||||
.await?;
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
201,
|
||||
"explicit on_behalf_of naming an instance group: {}",
|
||||
resp.text().await?
|
||||
);
|
||||
assert_eq!(
|
||||
stored_on_behalf_of("u/test-user/explicit")
|
||||
.await?
|
||||
.as_deref(),
|
||||
Some("g/scim-group")
|
||||
);
|
||||
|
||||
let mut by_email = new_script("u/test-user/by_email");
|
||||
by_email["on_behalf_of_email"] = json!("group-scim-group@windmill.dev");
|
||||
by_email["preserve_on_behalf_of"] = json!(true);
|
||||
let resp = authed(
|
||||
client().post(format!("{base}/scripts/create")),
|
||||
"SECRET_TOKEN",
|
||||
)
|
||||
.json(&by_email)
|
||||
.send()
|
||||
.await?;
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
201,
|
||||
"on_behalf_of_email naming an instance group: {}",
|
||||
resp.text().await?
|
||||
);
|
||||
assert_eq!(
|
||||
stored_on_behalf_of("u/test-user/by_email")
|
||||
.await?
|
||||
.as_deref(),
|
||||
Some("g/scim-group")
|
||||
);
|
||||
|
||||
let mut unknown = new_script("u/test-user/unknown");
|
||||
unknown["on_behalf_of"] = json!("g/no-such-group");
|
||||
unknown["preserve_on_behalf_of"] = json!(true);
|
||||
let resp = authed(
|
||||
client().post(format!("{base}/scripts/create")),
|
||||
"SECRET_TOKEN",
|
||||
)
|
||||
.json(&unknown)
|
||||
.send()
|
||||
.await?;
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
400,
|
||||
"a group in neither table is still refused: {}",
|
||||
resp.text().await?
|
||||
);
|
||||
|
||||
let resp = authed(
|
||||
client().post(format!("{base}/workspaces/create_fork")),
|
||||
"SECRET_TOKEN",
|
||||
)
|
||||
.json(&json!({ "id": "wm-fork-ig", "name": "ig" }))
|
||||
.send()
|
||||
.await?;
|
||||
assert_eq!(resp.status(), 200, "create fork: {}", resp.text().await?);
|
||||
assert_eq!(
|
||||
stored_on_behalf_of_in("wm-fork-ig", "f/hub/from_rule")
|
||||
.await?
|
||||
.as_deref(),
|
||||
Some("g/scim-group"),
|
||||
"a fork keeps an instance-group identity"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -8581,7 +8581,10 @@ async fn clone_scripts(
|
||||
WHEN on_behalf_of LIKE 'g/%' THEN
|
||||
(SELECT on_behalf_of WHERE EXISTS (
|
||||
SELECT 1 FROM group_ g WHERE g.workspace_id = $1::varchar
|
||||
AND g.name = substring(on_behalf_of from 3)))
|
||||
AND g.name = substring(on_behalf_of from 3)
|
||||
UNION ALL
|
||||
SELECT 1 FROM instance_group ig
|
||||
WHERE ig.name = substring(on_behalf_of from 3)))
|
||||
ELSE
|
||||
(SELECT on_behalf_of WHERE EXISTS (
|
||||
SELECT 1 FROM usr u WHERE u.workspace_id = $1::varchar
|
||||
@@ -8828,7 +8831,10 @@ fn principal_resolves_sql(principal: &str) -> String {
|
||||
OR pw.email = substring({principal} from 3)))
|
||||
WHEN {principal} LIKE 'g/%' THEN EXISTS (
|
||||
SELECT 1 FROM group_ g WHERE g.workspace_id = $1
|
||||
AND g.name = substring({principal} from 3))
|
||||
AND g.name = substring({principal} from 3)
|
||||
UNION ALL
|
||||
SELECT 1 FROM instance_group ig
|
||||
WHERE ig.name = substring({principal} from 3))
|
||||
ELSE EXISTS (
|
||||
SELECT 1 FROM usr u WHERE u.workspace_id = $1 AND u.username = {principal}
|
||||
UNION ALL
|
||||
@@ -9010,7 +9016,10 @@ async fn clone_flows(
|
||||
WHEN on_behalf_of LIKE 'g/%' THEN
|
||||
(SELECT on_behalf_of WHERE EXISTS (
|
||||
SELECT 1 FROM group_ g WHERE g.workspace_id = $2::varchar
|
||||
AND g.name = substring(on_behalf_of from 3)))
|
||||
AND g.name = substring(on_behalf_of from 3)
|
||||
UNION ALL
|
||||
SELECT 1 FROM instance_group ig
|
||||
WHERE ig.name = substring(on_behalf_of from 3)))
|
||||
ELSE
|
||||
(SELECT on_behalf_of WHERE EXISTS (
|
||||
SELECT 1 FROM usr u WHERE u.workspace_id = $2::varchar
|
||||
|
||||
@@ -105,15 +105,7 @@ async fn ensure_permissioned_as_exists(
|
||||
)));
|
||||
}
|
||||
} else if let Some(group) = rule.permissioned_as.strip_prefix("g/") {
|
||||
let exists = sqlx::query_scalar!(
|
||||
"SELECT EXISTS(SELECT 1 FROM group_ WHERE workspace_id = $1 AND name = $2)",
|
||||
w_id,
|
||||
group
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?
|
||||
.unwrap_or(false);
|
||||
if !exists {
|
||||
if !crate::users::group_exists(w_id, group, db).await? {
|
||||
return Err(Error::BadRequest(format!(
|
||||
"Folder '{folder_name}' default_permissioned_as rule '{}' resolves to group '{}' which does not exist in this workspace. Fix the folder rule and try again.",
|
||||
rule.path_glob, rule.permissioned_as
|
||||
|
||||
@@ -160,11 +160,30 @@ pub async fn resolve_username_to_email<'c>(
|
||||
.flatten())
|
||||
}
|
||||
|
||||
/// Whether `g/{name}` names a group in this workspace. Instance groups are granted through the
|
||||
/// same `g/{name}` keys, so a name from either table is a group here. No authorization of its
|
||||
/// own: same contract as [`permissioned_as_exists`].
|
||||
pub async fn group_exists<'c>(
|
||||
workspace_id: &str,
|
||||
name: &str,
|
||||
db: impl sqlx::PgExecutor<'c>,
|
||||
) -> crate::error::Result<bool> {
|
||||
Ok(sqlx::query_scalar!(
|
||||
"SELECT EXISTS(SELECT 1 FROM group_ WHERE workspace_id = $1 AND name = $2)
|
||||
OR EXISTS(SELECT 1 FROM instance_group WHERE name = $2)",
|
||||
workspace_id,
|
||||
name
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?
|
||||
.unwrap_or(false))
|
||||
}
|
||||
|
||||
/// Whether a permissioned_as names something that exists in this workspace.
|
||||
///
|
||||
/// An existence probe over the non-RLS pool, no authorization of its own: callers must
|
||||
/// already be authorized for `w_id`. Same contract for [`permissioned_as_from_email`] and
|
||||
/// [`resolve_username_to_email`].
|
||||
/// already be authorized for `w_id`. Same contract for [`permissioned_as_from_email`],
|
||||
/// [`resolve_username_to_email`] and [`group_exists`].
|
||||
///
|
||||
/// Accepts the three forms `username_to_permissioned_as` can produce: `u/{username}`,
|
||||
/// `g/{group}`, and a bare address when the username is itself email-shaped. Anything else
|
||||
@@ -187,14 +206,7 @@ pub async fn permissioned_as_exists(
|
||||
.is_some());
|
||||
}
|
||||
if let Some(group) = permissioned_as.strip_prefix(PERMISSIONED_AS_GROUP_PREFIX) {
|
||||
return Ok(sqlx::query_scalar!(
|
||||
"SELECT EXISTS(SELECT 1 FROM group_ WHERE workspace_id = $1 AND name = $2)",
|
||||
workspace_id,
|
||||
group
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?
|
||||
.unwrap_or(false));
|
||||
return group_exists(workspace_id, group, db).await;
|
||||
}
|
||||
// The bare form names an account acting without a `usr` row, not anyone who merely holds
|
||||
// that address: `u/{username}` is the canonical principal for a member, and the bare branch
|
||||
@@ -285,14 +297,7 @@ pub async fn permissioned_as_from_email(
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let exists = sqlx::query_scalar!(
|
||||
"SELECT EXISTS(SELECT 1 FROM group_ WHERE workspace_id = $1 AND name = $2)",
|
||||
workspace_id,
|
||||
group
|
||||
)
|
||||
.fetch_one(&mut *conn)
|
||||
.await?
|
||||
.unwrap_or(false);
|
||||
let exists = group_exists(workspace_id, group, &mut *conn).await?;
|
||||
Ok(exists.then(|| format!("{}{}", PERMISSIONED_AS_GROUP_PREFIX, group)))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user