mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-20 08:08:58 +00:00
* feat: accept MCP access tokens from an external OIDC issuer With the mcp_external_idp instance setting, every MCP URL also accepts an access token of an external OIDC issuer, told apart from Windmill's own by its iss. The token's OAuth client is mapped to a workspace and a selection of tools; the request acts as the existing Windmill user with the token's email, under that selection. Runs record the client in mcp_client and give it to steps as WM_MCP_CLIENT; tool calls and refusals are audited with both identities. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: harden external MCP token verification Refuse ID tokens, tokens before their nbf, and emails the issuer marks unverified; let a cached signing key expire so a withdrawn key stops verifying; fall back to the token's asymmetric alg for keys published without one; reject clients configured without an issuer or with an id longer than the column recording it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: refuse deactivated accounts and untyped tokens on external MCP auth Read password.disabled, since instance deactivation has no Windmill token to revoke on this path. Without a configured audience, take only tokens typed as access tokens (at+jwt header or a Bearer typ claim), so an ID token cannot stand in for one. Require https for the issuer except on localhost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: refuse ID tokens with an audience set, parse the issuer host Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: take an external MCP token's groups for the request instead of storing them When sso_groups_claim is set, the IdP-managed instance groups of an external MCP request come from the token's claim alone, mapped through their external_id with nested paths counting for their ancestors. Nothing is written, and an absent claim means no IdP groups, so removing a user from every group revokes at the next token. Entra's overage pointer is refused rather than read as no groups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: give external MCP clients the token access levels, one card per client A client takes the same access as a Windmill token: read, read and run, build and run (the session toolset, bound to the client's workspace), or scripts and flows as tools. The settings editor reuses McpAccessPicker, one collapsible card per client. The caller's JWT carries mcp_client so runs the session toolset starts record the client too. With token groups in use, admin rights a managed group grants follow the token, and workspace groups are kept by source rather than by name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep a client's tool selection across reopening its card Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: tidy the MCP external identity provider form Show it under SSO only, shorten its description with details in a tooltip, and give its fields form labels, hints and tooltips. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: leave default claim names empty, shorten auth setting descriptions Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: report an external MCP token's groups in whoami Requests and jobs started through an external MCP token act with the token's groups; whoami returned the stored memberships instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep stored roles for external MCP requests Only group memberships follow the token. Admin and superadmin roles stay as stored, kept current by SCIM and SSO login: their source is not recorded, so a group-derived demotion also stripped directly granted admins and left operator restrictions unapplied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: record the MCP client on on-behalf-of runs and AI agent tool steps On-behalf-of runs now hand push the MCP caller, which push uses only to record the client while it fetches the run's own permissions. AI agent tool jobs read the client from job_perms, so their steps see WM_MCP_CLIENT. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep the MCP client off v2_job The client is needed only while a job is queued, which job_perms covers; a finished run's client is in its audit entry. This leaves the job history table unaltered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: update ee-repo-ref to e9e94030cebd5afb0c8156ee46af2025bbe8778a This commit updates the EE repository reference after PR #934 was merged in windmill-ee-private. Previous ee-repo-ref: 7bdd6e8067a72e847ed0372ddc79a84f288946cf New ee-repo-ref: e9e94030cebd5afb0c8156ee46af2025bbe8778a Automated by sync-ee-ref workflow. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com> Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
354 lines
11 KiB
Rust
354 lines
11 KiB
Rust
use sqlx::{Acquire, PgConnection, PgExecutor, Pool, Postgres, Transaction};
|
|
|
|
use crate::audit::AuditAuthor;
|
|
|
|
pub type DB = Pool<Postgres>;
|
|
|
|
/// Workspace ID resolved by gateway middleware (stored in request extensions).
|
|
/// Used by auth to resolve workspace when the URL path doesn't contain one.
|
|
#[derive(Clone, Debug)]
|
|
pub struct GatewayWorkspaceId(pub String);
|
|
|
|
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
|
|
pub struct Authed {
|
|
pub email: String,
|
|
pub username: String,
|
|
pub is_admin: bool,
|
|
pub is_operator: bool,
|
|
pub groups: Vec<String>,
|
|
// (folder name, can write, is owner)
|
|
pub folders: Vec<(String, bool, bool)>,
|
|
pub scopes: Option<Vec<String>>,
|
|
pub token_prefix: Option<String>,
|
|
/// See `ApiAuthed::mcp_client`.
|
|
pub mcp_client: Option<String>,
|
|
}
|
|
|
|
impl Authed {
|
|
pub fn to_authed_ref(&self) -> AuthedRef<'_> {
|
|
AuthedRef {
|
|
email: &self.email,
|
|
username: &self.username,
|
|
is_admin: &self.is_admin,
|
|
is_operator: &self.is_operator,
|
|
groups: &self.groups,
|
|
folders: &self.folders,
|
|
scopes: &self.scopes,
|
|
token_prefix: &self.token_prefix,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Hash)]
|
|
pub struct AuthedRef<'a> {
|
|
pub email: &'a str,
|
|
pub username: &'a str,
|
|
pub is_admin: &'a bool,
|
|
pub is_operator: &'a bool,
|
|
pub groups: &'a Vec<String>,
|
|
// (folder name, can write, is owner)
|
|
pub folders: &'a Vec<(String, bool, bool)>,
|
|
pub scopes: &'a Option<Vec<String>>,
|
|
pub token_prefix: &'a Option<String>,
|
|
}
|
|
|
|
impl Authable for AuthedRef<'_> {
|
|
fn email(&self) -> &str {
|
|
self.email
|
|
}
|
|
fn username(&self) -> &str {
|
|
self.username
|
|
}
|
|
fn is_admin(&self) -> bool {
|
|
*self.is_admin
|
|
}
|
|
fn is_operator(&self) -> bool {
|
|
*self.is_operator
|
|
}
|
|
fn groups(&self) -> &[String] {
|
|
self.groups
|
|
}
|
|
fn folders(&self) -> &[(String, bool, bool)] {
|
|
self.folders
|
|
}
|
|
fn scopes(&self) -> Option<&[String]> {
|
|
self.scopes.as_ref().map(|x| x.as_slice())
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct UserDB {
|
|
db: DB,
|
|
}
|
|
|
|
pub trait Authable {
|
|
fn email(&self) -> &str;
|
|
fn username(&self) -> &str;
|
|
fn is_admin(&self) -> bool;
|
|
fn is_operator(&self) -> bool;
|
|
fn groups(&self) -> &[String];
|
|
fn folders(&self) -> &[(String, bool, bool)];
|
|
fn scopes(&self) -> Option<&[String]>;
|
|
}
|
|
|
|
impl Authable for Authed {
|
|
fn is_admin(&self) -> bool {
|
|
self.is_admin
|
|
}
|
|
|
|
fn is_operator(&self) -> bool {
|
|
self.is_operator
|
|
}
|
|
|
|
fn groups(&self) -> &[String] {
|
|
&self.groups
|
|
}
|
|
|
|
fn folders(&self) -> &[(String, bool, bool)] {
|
|
&self.folders
|
|
}
|
|
|
|
fn scopes(&self) -> Option<&[std::string::String]> {
|
|
self.scopes.as_ref().map(|x| x.as_slice())
|
|
}
|
|
|
|
fn email(&self) -> &str {
|
|
&self.email
|
|
}
|
|
|
|
fn username(&self) -> &str {
|
|
&self.username
|
|
}
|
|
}
|
|
|
|
lazy_static::lazy_static! {
|
|
pub static ref PG_SCHEMA: Option<String> = std::env::var("PG_SCHEMA").ok();
|
|
}
|
|
|
|
pub struct UserDbWithAuthed<'c, T: Authable + Sync> {
|
|
pub authed: &'c T,
|
|
pub db: UserDB,
|
|
}
|
|
|
|
impl<'c, 'd, T: Authable + Sync> Acquire<'c> for &'c UserDbWithAuthed<'d, T> {
|
|
type Database = Postgres;
|
|
type Connection = Transaction<'c, Postgres>;
|
|
|
|
fn acquire(self) -> futures_core::future::BoxFuture<'c, Result<Self::Connection, sqlx::Error>> {
|
|
Box::pin(async move { self.db.clone().begin(self.authed).await })
|
|
}
|
|
|
|
fn begin(
|
|
self,
|
|
) -> futures_core::future::BoxFuture<'c, Result<Transaction<'c, Postgres>, sqlx::Error>> {
|
|
Box::pin(async move { self.db.clone().begin(self.authed).await })
|
|
}
|
|
}
|
|
|
|
pub enum DbWithOptAuthed<'a, T: Authable + Sync> {
|
|
UserDB { authed: &'a T, user_db: UserDB, db: DB },
|
|
DB { db: DB, audit_author: AuditAuthor },
|
|
}
|
|
impl<'a, T: Authable + Sync> DbWithOptAuthed<'a, T> {
|
|
pub fn from_authed(authed: &'a T, db: DB, user_db: Option<UserDB>) -> Self {
|
|
if let Some(user_db) = user_db {
|
|
Self::UserDB { authed, user_db, db }
|
|
} else {
|
|
Self::DB { db, audit_author: AuditAuthor::from(authed) }
|
|
}
|
|
}
|
|
pub fn db(&self) -> &DB {
|
|
match self {
|
|
DbWithOptAuthed::UserDB { db, .. } => db,
|
|
DbWithOptAuthed::DB { db, .. } => db,
|
|
}
|
|
}
|
|
|
|
pub fn authed(&self) -> Option<&T> {
|
|
match self {
|
|
DbWithOptAuthed::UserDB { authed, .. } => Some(authed),
|
|
DbWithOptAuthed::DB { .. } => None,
|
|
}
|
|
}
|
|
|
|
pub fn audit_author(&self) -> Option<&AuditAuthor> {
|
|
match self {
|
|
DbWithOptAuthed::UserDB { .. } => None,
|
|
DbWithOptAuthed::DB { audit_author, .. } => Some(audit_author),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'c, 'd, T: Authable + Sync> Acquire<'c> for &'c DbWithOptAuthed<'d, T> {
|
|
type Database = Postgres;
|
|
type Connection = Transaction<'c, Postgres>;
|
|
|
|
fn acquire(self) -> futures_core::future::BoxFuture<'c, Result<Self::Connection, sqlx::Error>> {
|
|
Box::pin(async move {
|
|
match self {
|
|
DbWithOptAuthed::UserDB { authed, user_db, .. } => {
|
|
user_db.clone().begin(&**authed).await
|
|
}
|
|
DbWithOptAuthed::DB { db, .. } => db.clone().begin().await,
|
|
}
|
|
})
|
|
}
|
|
|
|
fn begin(
|
|
self,
|
|
) -> futures_core::future::BoxFuture<'c, Result<Transaction<'c, Postgres>, sqlx::Error>> {
|
|
Box::pin(async move {
|
|
match self {
|
|
DbWithOptAuthed::UserDB { authed, user_db, .. } => {
|
|
user_db.clone().begin(&**authed).await
|
|
}
|
|
DbWithOptAuthed::DB { db, .. } => db.clone().begin().await,
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
impl UserDB {
|
|
pub fn new(db: DB) -> Self {
|
|
Self { db }
|
|
}
|
|
|
|
pub async fn begin<T>(self, authed: &T) -> Result<Transaction<'static, Postgres>, sqlx::Error>
|
|
where
|
|
T: Authable,
|
|
{
|
|
let (folders_write, folders_read): &(Vec<_>, Vec<_>) =
|
|
&authed.folders().into_iter().partition(|x| x.1);
|
|
|
|
let mut folders_read = folders_read.clone();
|
|
folders_read.extend(folders_write.clone());
|
|
|
|
// tracing::debug!(
|
|
// "Setting role to {} {:?} {:?} {:?} {:?}",
|
|
// user,
|
|
// authed.username(),
|
|
// authed.groups(),
|
|
// folders_read,
|
|
// folders_write
|
|
// );
|
|
|
|
let mut tx = self.db.begin().await?;
|
|
|
|
if let Some(schema) = PG_SCHEMA.as_ref() {
|
|
// SAFETY: `schema` is an operator-controlled environment variable (PG_SCHEMA), set at deploy time and never user-supplied.
|
|
sqlx::query(&format!("SET LOCAL search_path TO {}", schema))
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
}
|
|
|
|
sqlx::query!(
|
|
"SELECT set_session_context($1, $2, $3, $4, $5, $6)",
|
|
authed.is_admin(),
|
|
authed.username(),
|
|
authed.groups().join(","),
|
|
authed
|
|
.groups()
|
|
.iter()
|
|
.map(|x| format!("g/{}", x))
|
|
.collect::<Vec<_>>()
|
|
.join(","),
|
|
folders_read
|
|
.iter()
|
|
.map(|x| x.0.clone())
|
|
.collect::<Vec<_>>()
|
|
.join(","),
|
|
folders_write
|
|
.iter()
|
|
.map(|x| x.0.clone())
|
|
.collect::<Vec<_>>()
|
|
.join(",")
|
|
)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
|
|
Ok(tx)
|
|
}
|
|
}
|
|
|
|
pub trait DbExecutor<'b>: Send + Sized + PgExecutor<'b> {
|
|
fn executor<'a>(&'a mut self) -> impl PgExecutor<'a>;
|
|
fn populate<'a>(&'a mut self) -> impl DbExecutor<'a>;
|
|
}
|
|
|
|
impl<'b> DbExecutor<'b> for &DB {
|
|
fn executor<'a>(&'a mut self) -> impl PgExecutor<'a> {
|
|
&**self
|
|
}
|
|
fn populate<'a>(&'a mut self) -> impl DbExecutor<'a> {
|
|
&**self
|
|
}
|
|
}
|
|
|
|
impl<'b> DbExecutor<'b> for &'b mut PgConnection {
|
|
fn executor<'a>(&'a mut self) -> impl PgExecutor<'a> {
|
|
&mut **self
|
|
}
|
|
fn populate<'a>(&'a mut self) -> impl DbExecutor<'a> {
|
|
&mut **self
|
|
}
|
|
}
|
|
|
|
lazy_static::lazy_static! {
|
|
static ref PROCESS_START: std::time::Instant = std::time::Instant::now();
|
|
}
|
|
|
|
/// Milliseconds after [`PROCESS_START`] before which every pooled connection is retired;
|
|
/// 0 retires none.
|
|
static RETIRE_CONNECTIONS_OPENED_BEFORE_MS: std::sync::atomic::AtomicU64 =
|
|
std::sync::atomic::AtomicU64::new(0);
|
|
|
|
/// Postgres refuses a statement prepared before its result columns changed type (even a
|
|
/// varchar length) with this error, on every execution, for as long as the connection lives.
|
|
/// The routine is matched as well as the message, which `lc_messages` may translate.
|
|
pub fn is_stale_cached_plan(e: &sqlx::Error) -> bool {
|
|
let sqlx::Error::Database(d) = e else {
|
|
return false;
|
|
};
|
|
d.code().as_deref() == Some("0A000")
|
|
&& (d
|
|
.try_downcast_ref::<sqlx::postgres::PgDatabaseError>()
|
|
.is_some_and(|pg| pg.routine() == Some("RevalidateCachedQuery"))
|
|
|| d.message()
|
|
.contains("cached plan must not change result type"))
|
|
}
|
|
|
|
/// Retire every pooled connection opened up to now, so a schema change that poisoned their
|
|
/// prepared statements costs one failed query instead of failing until they expire.
|
|
pub fn retire_connections_opened_before_now() {
|
|
let now = PROCESS_START.elapsed().as_millis() as u64 + 1;
|
|
RETIRE_CONNECTIONS_OPENED_BEFORE_MS.fetch_max(now, std::sync::atomic::Ordering::Relaxed);
|
|
}
|
|
|
|
/// For the pool's `before_acquire`: whether a connection of this age must be closed.
|
|
pub fn connection_is_retired(age: std::time::Duration) -> bool {
|
|
let cutoff = RETIRE_CONNECTIONS_OPENED_BEFORE_MS.load(std::sync::atomic::Ordering::Relaxed);
|
|
cutoff != 0 && (PROCESS_START.elapsed().saturating_sub(age).as_millis() as u64) < cutoff
|
|
}
|
|
|
|
/// Servers report no version, so the worker version gate alone cannot see a server of the
|
|
/// previous release; its rollout is assumed done once this server has been up this long.
|
|
pub const PREVIOUS_RELEASE_ROLLOUT_UPTIME: std::time::Duration =
|
|
std::time::Duration::from_secs(30 * 60);
|
|
|
|
static PROCESS_UPTIME_OFFSET_SECS: std::sync::atomic::AtomicU64 =
|
|
std::sync::atomic::AtomicU64::new(0);
|
|
|
|
/// Time since this process first used its database pool.
|
|
pub fn process_uptime() -> std::time::Duration {
|
|
PROCESS_START.elapsed()
|
|
+ std::time::Duration::from_secs(
|
|
PROCESS_UPTIME_OFFSET_SECS.load(std::sync::atomic::Ordering::Relaxed),
|
|
)
|
|
}
|
|
|
|
/// For tests of what waits on [`process_uptime`]: adds `offset` to it.
|
|
#[doc(hidden)]
|
|
pub fn set_process_uptime_offset(offset: std::time::Duration) {
|
|
PROCESS_UPTIME_OFFSET_SECS.store(offset.as_secs(), std::sync::atomic::Ordering::Relaxed);
|
|
}
|