mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-17 00:20:51 +00:00
fix: honor iam and azure workload identity on server-side pg connections (#12228)
* fix: honor iam and azure workload identity on server-side pg connections Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: refuse token auth modes for resource sources on the server Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: send the server's entra token only to verified azure pg hosts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: refuse a token-mode clone before creating its database 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
120415af15
commit
1f134e4127
@@ -3367,6 +3367,18 @@ pub(crate) async fn resolve_pg_source_checked_with_kind(
|
||||
// comes through this function, and `validate_dbname` only ever reaches a target. The check is
|
||||
// deliberately the narrow one — see `validate_dbname_not_option_like`.
|
||||
windmill_common::validate_dbname_not_option_like(&pg.dbname)?;
|
||||
// A token mode authenticates as the API server's own cloud identity. An admin chose to back a
|
||||
// data table with its resource; a `$res:` source only needs to be readable by the caller, so
|
||||
// it must not borrow that identity at all.
|
||||
if source.starts_with("$res:")
|
||||
&& windmill_common::PgAuthMode::of(&pg)? != windmill_common::PgAuthMode::Password
|
||||
{
|
||||
return Err(Error::BadRequest(
|
||||
"IAM and Azure workload identity authentication are only available to data tables \
|
||||
here, not to a resource source"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
Ok((pg, managed_kind))
|
||||
}
|
||||
|
||||
@@ -3456,6 +3468,19 @@ pub(crate) struct PgDumpOptions<'a> {
|
||||
pub(crate) no_acl: bool,
|
||||
}
|
||||
|
||||
/// The CLIs are handed the resource's password as is, so a token mode would reach the server as
|
||||
/// the literal sentinel. Minting the token for them is not supported.
|
||||
fn ensure_cli_password_auth(pg_db: &PgDatabase) -> Result<()> {
|
||||
match windmill_common::PgAuthMode::of(pg_db)? {
|
||||
windmill_common::PgAuthMode::Password => Ok(()),
|
||||
_ => Err(Error::BadRequest(
|
||||
"Copying, importing and exporting a database are not supported with IAM or Azure \
|
||||
workload identity authentication"
|
||||
.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// A pg_dump invocation against `pg_db` writing to `dump_path`, carrying the connection settings
|
||||
/// the CLI reads from the environment. The returned file, when there is one, holds the root
|
||||
/// certificate the command points at and must outlive it.
|
||||
@@ -3464,6 +3489,7 @@ fn pg_dump_command(
|
||||
opts: PgDumpOptions<'_>,
|
||||
dump_path: &std::path::Path,
|
||||
) -> Result<(tokio::process::Command, Option<DumpFile>)> {
|
||||
ensure_cli_password_auth(pg_db)?;
|
||||
let host = &pg_db.host;
|
||||
let port = pg_db.port.unwrap_or(5432).to_string();
|
||||
let user = pg_db.login_name();
|
||||
@@ -3630,6 +3656,7 @@ async fn comment_out_unsupported_settings(
|
||||
/// A psql invocation against `pg_db`, carrying the connection settings the CLI reads
|
||||
/// from the environment.
|
||||
fn psql_command(pg_db: &PgDatabase) -> Result<(tokio::process::Command, Option<DumpFile>)> {
|
||||
ensure_cli_password_auth(pg_db)?;
|
||||
let mut cmd = tokio::process::Command::new("psql");
|
||||
cmd.arg("--host")
|
||||
.arg(&pg_db.host)
|
||||
@@ -4057,6 +4084,9 @@ async fn create_pg_database(
|
||||
} else {
|
||||
let source_pg =
|
||||
resolve_pg_source_checked(&db, &user_db, &authed, &w_id, &req.source).await?;
|
||||
// The import that fills this database refuses a token-mode source, and would leave the
|
||||
// database behind empty.
|
||||
ensure_cli_password_auth(&source_pg)?;
|
||||
create_database_on_server(&db, &source_pg, &req.target_dbname).await?;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,23 @@ pub const AZURE_OSSRDBMS_SCOPE: &str = "https://ossrdbms-aad.database.windows.ne
|
||||
/// login, so the mode having been intended at all leaves no trace.
|
||||
pub const WORKLOAD_IDENTITY_PASSWORD: &str = "ms_entraid";
|
||||
|
||||
/// Host suffixes of Azure Database for PostgreSQL in the public, China and US Government
|
||||
/// clouds. Private endpoints resolve under the same names (`<server>.privatelink.…`).
|
||||
pub const AZURE_POSTGRES_HOST_SUFFIXES: &[&str] = &[
|
||||
".postgres.database.azure.com",
|
||||
".postgres.database.chinacloudapi.cn",
|
||||
".postgres.database.usgovcloudapi.net",
|
||||
];
|
||||
|
||||
/// Whether `host` names an Azure Database for PostgreSQL server. Such a server never hands
|
||||
/// the token it is presented to its owner, so sending it one cannot leak the token.
|
||||
pub fn is_azure_postgres_host(host: &str) -> bool {
|
||||
let host = host.trim().trim_end_matches('.').to_ascii_lowercase();
|
||||
AZURE_POSTGRES_HOST_SUFFIXES
|
||||
.iter()
|
||||
.any(|suffix| host.len() > suffix.len() && host.ends_with(suffix))
|
||||
}
|
||||
|
||||
/// Renew an access token this long before it expires.
|
||||
const TOKEN_REFRESH_BUFFER: Duration = Duration::from_secs(5 * 60);
|
||||
|
||||
@@ -102,7 +119,7 @@ impl WorkloadIdentityConfig {
|
||||
.filter(|v| !v.is_empty())
|
||||
.ok_or_else(|| {
|
||||
Error::BadConfig(format!(
|
||||
"Workload identity authentication requires the {} env var on the worker, \
|
||||
"Workload identity authentication requires the {} env var on the Windmill process connecting, \
|
||||
injected by the Azure workload identity webhook",
|
||||
env_var
|
||||
))
|
||||
@@ -257,6 +274,28 @@ impl WorkloadIdentityConfig {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The API server sends its Entra ID token only to hosts this accepts, so a lookalike
|
||||
/// must not pass.
|
||||
#[test]
|
||||
fn test_is_azure_postgres_host() {
|
||||
assert!(is_azure_postgres_host(
|
||||
"myserver.postgres.database.azure.com"
|
||||
));
|
||||
assert!(is_azure_postgres_host(
|
||||
"MyServer.privatelink.postgres.database.azure.com."
|
||||
));
|
||||
assert!(is_azure_postgres_host(
|
||||
"s.postgres.database.usgovcloudapi.net"
|
||||
));
|
||||
assert!(!is_azure_postgres_host("postgres.database.azure.com"));
|
||||
assert!(!is_azure_postgres_host(".postgres.database.azure.com"));
|
||||
assert!(!is_azure_postgres_host(
|
||||
"s.postgres.database.azure.com.attacker.io"
|
||||
));
|
||||
assert!(!is_azure_postgres_host("s-postgres.database.azure.com"));
|
||||
assert!(!is_azure_postgres_host("127.0.0.1"));
|
||||
}
|
||||
|
||||
fn config(authority_host: &str) -> WorkloadIdentityConfig {
|
||||
WorkloadIdentityConfig {
|
||||
tenant_id: "tenant".to_string(),
|
||||
|
||||
@@ -1151,6 +1151,62 @@ pub fn set_pg_keepalive(config: &mut tokio_postgres::Config) {
|
||||
.tcp_user_timeout(std::time::Duration::from_secs(120));
|
||||
}
|
||||
|
||||
/// How a [`PgDatabase`] authenticates, which also keys the worker's connection cache: a
|
||||
/// connection established under one mode must never be handed to a request asking for
|
||||
/// another.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum PgAuthMode {
|
||||
Password,
|
||||
/// AWS RDS IAM.
|
||||
Iam,
|
||||
/// Azure Entra ID, via the federated identity of the process connecting.
|
||||
WorkloadIdentity,
|
||||
}
|
||||
|
||||
impl PgAuthMode {
|
||||
pub fn of(database: &PgDatabase) -> error::Result<Self> {
|
||||
let workload_identity = database.password.as_deref().map(str::trim)
|
||||
== Some(azure_workload_identity::WORKLOAD_IDENTITY_PASSWORD);
|
||||
match (database.use_iam_auth == Some(true), workload_identity) {
|
||||
(true, true) => Err(error::Error::BadRequest(
|
||||
"IAM RDS authentication cannot use the Azure workload identity password"
|
||||
.to_string(),
|
||||
)),
|
||||
(true, false) => Ok(PgAuthMode::Iam),
|
||||
(false, true) => Ok(PgAuthMode::WorkloadIdentity),
|
||||
(false, false) => Ok(PgAuthMode::Password),
|
||||
}
|
||||
}
|
||||
|
||||
/// What to announce in the job log. Password auth is the default and stays silent.
|
||||
/// The token modes name the login they present, which is the one thing the token
|
||||
/// itself does not carry.
|
||||
pub fn log_name(&self, database: &PgDatabase) -> Option<String> {
|
||||
match self {
|
||||
PgAuthMode::Password => None,
|
||||
PgAuthMode::Iam => Some(format!(
|
||||
"IAM RDS authentication (login {})",
|
||||
database.login_name()
|
||||
)),
|
||||
PgAuthMode::WorkloadIdentity => Some(match database.entra_login() {
|
||||
Ok(login) => format!("Azure Workload Identity (login {login})"),
|
||||
// Connecting rejects a missing login; do not invent one here.
|
||||
Err(_) => "Azure Workload Identity".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cache_key_segment(&self) -> &'static str {
|
||||
match self {
|
||||
// Workload identity needs no segment of its own: to_uri() carries the raw
|
||||
// password and the mode is a pure function of it, so two modes can never
|
||||
// share a key even though the mode is selected on the trimmed value.
|
||||
PgAuthMode::Password | PgAuthMode::WorkloadIdentity => "",
|
||||
PgAuthMode::Iam => "&iam=true",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PgDatabase {
|
||||
/// The role the connection logs in as, whichever way it authenticates.
|
||||
pub fn login_name(&self) -> &str {
|
||||
@@ -1200,9 +1256,74 @@ impl PgDatabase {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Connect under whichever [`PgAuthMode`] the resource selects. Every connection to a
|
||||
/// user's database goes through here, so a token mode handled anywhere else would
|
||||
/// send its sentinel to the server as a literal password. The token modes authenticate
|
||||
/// as this process's own cloud identity: a server-side caller given a resource the
|
||||
/// requester can merely read must refuse them before connecting.
|
||||
///
|
||||
/// The Entra ID token is not bound to a host, so whoever can edit the resource could
|
||||
/// repoint it and collect the token. Here it is only sent to an Azure Database for
|
||||
/// PostgreSQL host whose certificate verifies; see [`Self::connect_for_job`].
|
||||
pub async fn connect(
|
||||
&self,
|
||||
main_db: Option<&DB>,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
self.connect_as(main_db, false).await
|
||||
}
|
||||
|
||||
/// [`Self::connect`] for a job's own connection on a worker, which sends the Entra ID
|
||||
/// token to whatever host the resource names. Anyone who can run a job there can use
|
||||
/// the worker's identity anyway; admins confine it with worker groups.
|
||||
pub async fn connect_for_job(
|
||||
&self,
|
||||
main_db: Option<&DB>,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
self.connect_as(main_db, true).await
|
||||
}
|
||||
|
||||
async fn connect_as(
|
||||
&self,
|
||||
main_db: Option<&DB>,
|
||||
trust_resource_host: bool,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
#[cfg(not(feature = "enterprise"))]
|
||||
let _ = trust_resource_host;
|
||||
match PgAuthMode::of(self)? {
|
||||
PgAuthMode::Iam => {
|
||||
#[cfg(all(feature = "enterprise", feature = "private"))]
|
||||
{
|
||||
self.connect_with_iam().await
|
||||
}
|
||||
#[cfg(not(all(feature = "enterprise", feature = "private")))]
|
||||
{
|
||||
Err(error::Error::ExecutionErr(
|
||||
"IAM RDS authentication requires Windmill Enterprise Edition".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
PgAuthMode::WorkloadIdentity => {
|
||||
#[cfg(feature = "enterprise")]
|
||||
{
|
||||
self.connect_with_workload_identity(trust_resource_host)
|
||||
.await
|
||||
}
|
||||
#[cfg(not(feature = "enterprise"))]
|
||||
{
|
||||
Err(error::Error::ExecutionErr(
|
||||
"Azure workload identity authentication requires Windmill Enterprise \
|
||||
Edition"
|
||||
.to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
PgAuthMode::Password => self.connect_with_password(main_db).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_with_password(
|
||||
&self,
|
||||
main_db: Option<&DB>,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
match self.connect_inner().await {
|
||||
Ok(result) => Ok(result),
|
||||
@@ -1410,7 +1531,7 @@ impl PgDatabase {
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "enterprise", feature = "private"))]
|
||||
pub async fn connect_with_iam(
|
||||
async fn connect_with_iam(
|
||||
&self,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
// Resolve region: resource field takes priority, then env var
|
||||
@@ -1432,7 +1553,9 @@ impl PgDatabase {
|
||||
error::Error::InternalErr(format!("IAM token generation failed: {e:#}"))
|
||||
})?;
|
||||
|
||||
self.connect_with_token("IAM RDS", user, &token).await
|
||||
// An IAM token is signed for this one host, so it is worthless anywhere else.
|
||||
self.connect_with_token("IAM RDS", user, &token, false)
|
||||
.await
|
||||
}
|
||||
|
||||
/// The role an Entra-authenticated connection logs in as. Azure maps each Entra
|
||||
@@ -1448,30 +1571,44 @@ impl PgDatabase {
|
||||
.ok_or_else(|| {
|
||||
error::Error::BadRequest(
|
||||
"Azure workload identity authentication requires `user` on the resource. \
|
||||
Set it to the Postgres role the worker's Entra principal is mapped to, \
|
||||
Set it to the Postgres role Windmill's Entra principal is mapped to, \
|
||||
as created by pgaadauth_create_principal."
|
||||
.to_string(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Connect to Azure Database for PostgreSQL as the worker's federated identity.
|
||||
/// Connect to Azure Database for PostgreSQL as this process's federated identity.
|
||||
/// The Entra ID access token replaces the password.
|
||||
#[cfg(feature = "enterprise")]
|
||||
pub async fn connect_with_workload_identity(
|
||||
async fn connect_with_workload_identity(
|
||||
&self,
|
||||
trust_resource_host: bool,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
// Before the token exchange: a missing login is worth reporting without first
|
||||
// spending a round trip to Entra ID on it.
|
||||
let user = self.entra_login()?;
|
||||
if !trust_resource_host && !azure_workload_identity::is_azure_postgres_host(&self.host) {
|
||||
return Err(error::Error::BadRequest(format!(
|
||||
"Azure workload identity authentication from the API server only connects to an \
|
||||
Azure Database for PostgreSQL host (*{}), not to '{}'",
|
||||
azure_workload_identity::AZURE_POSTGRES_HOST_SUFFIXES[0],
|
||||
self.host
|
||||
)));
|
||||
}
|
||||
|
||||
let workload_identity = azure_workload_identity::WorkloadIdentityConfig::resolve()?;
|
||||
let token = workload_identity
|
||||
.access_token(azure_workload_identity::AZURE_OSSRDBMS_SCOPE)
|
||||
.await?;
|
||||
|
||||
self.connect_with_token("Azure workload identity", user, &token)
|
||||
.await
|
||||
self.connect_with_token(
|
||||
"Azure workload identity",
|
||||
user,
|
||||
&token,
|
||||
!trust_resource_host,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Connect with an externally issued access token in place of the password.
|
||||
@@ -1479,11 +1616,15 @@ impl PgDatabase {
|
||||
/// regardless of the resource's sslmode; the sslmode still selects how far the
|
||||
/// server's certificate is verified.
|
||||
#[cfg(feature = "enterprise")]
|
||||
///
|
||||
/// `verify_host` verifies the certificate and hostname against the system roots alone,
|
||||
/// whatever the resource says: the resource's own CA would let it vouch for any host.
|
||||
async fn connect_with_token(
|
||||
&self,
|
||||
auth_kind: &str,
|
||||
user: &str,
|
||||
token: &str,
|
||||
verify_host: bool,
|
||||
) -> Result<(tokio_postgres::Client, TokioPgConnection), error::Error> {
|
||||
use native_tls::TlsConnector;
|
||||
use postgres_native_tls::MakeTlsConnector;
|
||||
@@ -1491,12 +1632,21 @@ impl PgDatabase {
|
||||
let port = self.port.unwrap_or(5432);
|
||||
|
||||
let mut connector = TlsConnector::builder();
|
||||
let verified = Self::configure_pg_tls_verification(
|
||||
&mut connector,
|
||||
self.sslmode.as_deref(),
|
||||
self.root_certificate_pem.as_deref(),
|
||||
self.accept_invalid_certs,
|
||||
)?;
|
||||
let verified = if verify_host {
|
||||
Self::configure_pg_tls_verification(
|
||||
&mut connector,
|
||||
Some("verify-full"),
|
||||
None,
|
||||
Some(false),
|
||||
)?
|
||||
} else {
|
||||
Self::configure_pg_tls_verification(
|
||||
&mut connector,
|
||||
self.sslmode.as_deref(),
|
||||
self.root_certificate_pem.as_deref(),
|
||||
self.accept_invalid_certs,
|
||||
)?
|
||||
};
|
||||
if !verified {
|
||||
tracing::warn!("{auth_kind} auth without certificate verification: TLS certificate verification is disabled. Provide root_certificate_pem (and set sslmode=verify-full) to enforce verification.");
|
||||
}
|
||||
@@ -1614,6 +1764,79 @@ pub async fn shutdown_pg_connection(
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pg_auth_mode_tests {
|
||||
use super::*;
|
||||
use azure_workload_identity::WORKLOAD_IDENTITY_PASSWORD;
|
||||
|
||||
/// The sentinel password is the whole opt-in: nothing else marks the resource, so a
|
||||
/// resource carrying it must not fall through to password auth.
|
||||
#[test]
|
||||
fn test_workload_identity_password_selects_the_auth_mode() {
|
||||
let db = |password: &str| {
|
||||
PgDatabase::parse_uri(&format!("postgres://someuser:{password}@host:5432/db")).unwrap()
|
||||
};
|
||||
assert_eq!(
|
||||
PgAuthMode::of(&db(WORKLOAD_IDENTITY_PASSWORD)).unwrap(),
|
||||
PgAuthMode::WorkloadIdentity
|
||||
);
|
||||
assert_eq!(
|
||||
PgAuthMode::of(&db("hunter2")).unwrap(),
|
||||
PgAuthMode::Password
|
||||
);
|
||||
// A pasted sentinel keeps its surrounding whitespace, and an unrecognized one is
|
||||
// forwarded to the server as a real password instead of selecting the mode.
|
||||
assert_eq!(
|
||||
PgAuthMode::of(&db("%20ms_entraid%0A")).unwrap(),
|
||||
PgAuthMode::WorkloadIdentity
|
||||
);
|
||||
}
|
||||
|
||||
/// The job log is the only place the presented login is visible, and the two token
|
||||
/// modes differ on whether a missing one has a default at all.
|
||||
#[test]
|
||||
fn test_log_name_reports_the_presented_login() {
|
||||
let db = |user: &str| {
|
||||
PgDatabase::parse_uri(&format!("postgres://{user}:pw@host:5432/db")).unwrap()
|
||||
};
|
||||
|
||||
assert_eq!(PgAuthMode::Password.log_name(&db("someuser")), None);
|
||||
assert_eq!(
|
||||
PgAuthMode::Iam.log_name(&db("someuser")).unwrap(),
|
||||
"IAM RDS authentication (login someuser)"
|
||||
);
|
||||
assert_eq!(
|
||||
PgAuthMode::Iam.log_name(&db("")).unwrap(),
|
||||
"IAM RDS authentication (login postgres)"
|
||||
);
|
||||
assert_eq!(
|
||||
PgAuthMode::WorkloadIdentity
|
||||
.log_name(&db("someuser"))
|
||||
.unwrap(),
|
||||
"Azure Workload Identity (login someuser)"
|
||||
);
|
||||
// Entra has no default login, so none is named rather than implying `postgres`.
|
||||
assert_eq!(
|
||||
PgAuthMode::WorkloadIdentity.log_name(&db("")).unwrap(),
|
||||
"Azure Workload Identity"
|
||||
);
|
||||
}
|
||||
|
||||
/// The datatable endpoints connect from the server, not through the worker's
|
||||
/// executor: the mode must be selected in `connect` itself, or the sentinel reaches the
|
||||
/// server as a literal password.
|
||||
#[tokio::test]
|
||||
async fn test_connect_does_not_send_the_sentinel_as_a_password() {
|
||||
// Nothing listens there, so falling through to password auth fails on the socket.
|
||||
let db = PgDatabase::parse_uri(&format!(
|
||||
"postgres://someuser:{WORKLOAD_IDENTITY_PASSWORD}@127.0.0.1:1/db"
|
||||
))
|
||||
.unwrap();
|
||||
let err = db.connect(None).await.err().unwrap().to_string();
|
||||
assert!(err.contains("orkload identity"), "{err}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pg_connection_shutdown_tests {
|
||||
#[tokio::test(start_paused = true)]
|
||||
|
||||
@@ -19,16 +19,16 @@ use tokio_postgres::{
|
||||
Column,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
use windmill_common::azure_workload_identity::WORKLOAD_IDENTITY_PASSWORD;
|
||||
use windmill_common::error::to_anyhow;
|
||||
use windmill_common::error::{self, Error};
|
||||
use windmill_common::worker::{
|
||||
to_raw_value, Connection, SqlAnnotations, SqlResultCollectionStrategy, CLOUD_HOSTED,
|
||||
};
|
||||
use windmill_common::workspaces::{
|
||||
get_datatable_resource_from_db, parse_datatable_ref, parse_datatable_ref_for, DatatableAccess, DatatableConnectAs,
|
||||
get_datatable_resource_from_db, parse_datatable_ref, parse_datatable_ref_for, DatatableAccess,
|
||||
DatatableConnectAs,
|
||||
};
|
||||
use windmill_common::{PgDatabase, PrepareQueryColumnInfo, PrepareQueryResult, DB};
|
||||
use windmill_common::{PgAuthMode, PgDatabase, PrepareQueryColumnInfo, PrepareQueryResult, DB};
|
||||
use windmill_parser::{Arg, Typ};
|
||||
use windmill_parser_sql::{
|
||||
parse_db_resource, parse_pg_statement_arg_positions, parse_pgsql_sig_with_typed_schema,
|
||||
@@ -502,95 +502,11 @@ pub async fn clear_pg_cache() {
|
||||
close_idle_pg_connections();
|
||||
}
|
||||
|
||||
/// How the connection authenticates, which also keys the connection cache: a
|
||||
/// connection established under one mode must never be handed to a request asking
|
||||
/// for another.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum PgAuthMode {
|
||||
Password,
|
||||
/// AWS RDS IAM.
|
||||
Iam,
|
||||
/// Azure Entra ID, via the worker's federated identity.
|
||||
WorkloadIdentity,
|
||||
}
|
||||
|
||||
impl PgAuthMode {
|
||||
fn of(database: &PgDatabase) -> error::Result<Self> {
|
||||
let workload_identity =
|
||||
database.password.as_deref().map(str::trim) == Some(WORKLOAD_IDENTITY_PASSWORD);
|
||||
match (database.use_iam_auth == Some(true), workload_identity) {
|
||||
(true, true) => Err(Error::BadRequest(
|
||||
"IAM RDS authentication cannot use the Azure workload identity password"
|
||||
.to_string(),
|
||||
)),
|
||||
(true, false) => Ok(PgAuthMode::Iam),
|
||||
(false, true) => Ok(PgAuthMode::WorkloadIdentity),
|
||||
(false, false) => Ok(PgAuthMode::Password),
|
||||
}
|
||||
}
|
||||
|
||||
/// What to announce in the job log. Password auth is the default and stays silent.
|
||||
/// The token modes name the login they present, which is the one thing the token
|
||||
/// itself does not carry.
|
||||
fn log_name(&self, database: &PgDatabase) -> Option<String> {
|
||||
match self {
|
||||
PgAuthMode::Password => None,
|
||||
PgAuthMode::Iam => Some(format!(
|
||||
"IAM RDS authentication (login {})",
|
||||
database.login_name()
|
||||
)),
|
||||
PgAuthMode::WorkloadIdentity => Some(match database.entra_login() {
|
||||
Ok(login) => format!("Azure Workload Identity (login {login})"),
|
||||
// Connecting rejects a missing login; do not invent one here.
|
||||
Err(_) => "Azure Workload Identity".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn cache_key_segment(&self) -> &'static str {
|
||||
match self {
|
||||
// Workload identity needs no segment of its own: to_uri() carries the raw
|
||||
// password and the mode is a pure function of it, so two modes can never
|
||||
// share a key even though the mode is selected on the trimmed value.
|
||||
PgAuthMode::Password | PgAuthMode::WorkloadIdentity => "",
|
||||
PgAuthMode::Iam => "&iam=true",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn new_pg_connection(
|
||||
database: &PgDatabase,
|
||||
auth_mode: PgAuthMode,
|
||||
main_db: Option<&DB>,
|
||||
) -> error::Result<(tokio_postgres::Client, tokio::task::JoinHandle<()>)> {
|
||||
let (client, connection) = match auth_mode {
|
||||
PgAuthMode::Iam => {
|
||||
#[cfg(all(feature = "enterprise", feature = "private"))]
|
||||
{
|
||||
database.connect_with_iam().await?
|
||||
}
|
||||
#[cfg(not(all(feature = "enterprise", feature = "private")))]
|
||||
{
|
||||
return Err(Error::ExecutionErr(
|
||||
"IAM RDS authentication requires Windmill Enterprise Edition".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
PgAuthMode::WorkloadIdentity => {
|
||||
#[cfg(feature = "enterprise")]
|
||||
{
|
||||
database.connect_with_workload_identity().await?
|
||||
}
|
||||
#[cfg(not(feature = "enterprise"))]
|
||||
{
|
||||
return Err(Error::ExecutionErr(
|
||||
"Azure workload identity authentication requires Windmill Enterprise Edition"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
PgAuthMode::Password => database.connect(main_db).await?,
|
||||
};
|
||||
let (client, connection) = database.connect_for_job(main_db).await?;
|
||||
let handle = tokio::spawn(async move {
|
||||
if let Err(e) = connection.await {
|
||||
tracing::error!("connection error: {}", e);
|
||||
@@ -1376,20 +1292,19 @@ pub async fn do_postgresql(
|
||||
}
|
||||
}
|
||||
if lease.conn.is_none() {
|
||||
let (client, connection_task) =
|
||||
match new_pg_connection(&database, auth_mode, conn.as_sql()).await {
|
||||
Ok(connected) => connected,
|
||||
// A server or pooler can also refuse outright past a per-login or
|
||||
// total connection cap, which pool scopes do not capture. Free the
|
||||
// idle connections of every scope and try once more.
|
||||
Err(e) => {
|
||||
if !is_connection_cap_refusal(&e) || close_idle_pg_connections() == 0 {
|
||||
return Err(e);
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(250)).await;
|
||||
new_pg_connection(&database, auth_mode, conn.as_sql()).await?
|
||||
let (client, connection_task) = match new_pg_connection(&database, conn.as_sql()).await {
|
||||
Ok(connected) => connected,
|
||||
// A server or pooler can also refuse outright past a per-login or
|
||||
// total connection cap, which pool scopes do not capture. Free the
|
||||
// idle connections of every scope and try once more.
|
||||
Err(e) => {
|
||||
if !is_connection_cap_refusal(&e) || close_idle_pg_connections() == 0 {
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
tokio::time::sleep(Duration::from_millis(250)).await;
|
||||
new_pg_connection(&database, conn.as_sql()).await?
|
||||
}
|
||||
};
|
||||
lease.conn = Some(PgConnection {
|
||||
key: database_string.clone(),
|
||||
scope: lease.scope.clone(),
|
||||
@@ -2738,59 +2653,6 @@ impl FromSql<'_> for StringCollector {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The sentinel password is the whole opt-in: nothing else marks the resource, so a
|
||||
/// resource carrying it must not fall through to password auth.
|
||||
#[test]
|
||||
fn test_workload_identity_password_selects_the_auth_mode() {
|
||||
let db = |password: &str| {
|
||||
PgDatabase::parse_uri(&format!("postgres://someuser:{password}@host:5432/db")).unwrap()
|
||||
};
|
||||
assert_eq!(
|
||||
PgAuthMode::of(&db(WORKLOAD_IDENTITY_PASSWORD)).unwrap(),
|
||||
PgAuthMode::WorkloadIdentity
|
||||
);
|
||||
assert_eq!(
|
||||
PgAuthMode::of(&db("hunter2")).unwrap(),
|
||||
PgAuthMode::Password
|
||||
);
|
||||
// A pasted sentinel keeps its surrounding whitespace, and an unrecognized one is
|
||||
// forwarded to the server as a real password instead of selecting the mode.
|
||||
assert_eq!(
|
||||
PgAuthMode::of(&db("%20ms_entraid%0A")).unwrap(),
|
||||
PgAuthMode::WorkloadIdentity
|
||||
);
|
||||
}
|
||||
|
||||
/// The job log is the only place the presented login is visible, and the two token
|
||||
/// modes differ on whether a missing one has a default at all.
|
||||
#[test]
|
||||
fn test_log_name_reports_the_presented_login() {
|
||||
let db = |user: &str| {
|
||||
PgDatabase::parse_uri(&format!("postgres://{user}:pw@host:5432/db")).unwrap()
|
||||
};
|
||||
|
||||
assert_eq!(PgAuthMode::Password.log_name(&db("someuser")), None);
|
||||
assert_eq!(
|
||||
PgAuthMode::Iam.log_name(&db("someuser")).unwrap(),
|
||||
"IAM RDS authentication (login someuser)"
|
||||
);
|
||||
assert_eq!(
|
||||
PgAuthMode::Iam.log_name(&db("")).unwrap(),
|
||||
"IAM RDS authentication (login postgres)"
|
||||
);
|
||||
assert_eq!(
|
||||
PgAuthMode::WorkloadIdentity
|
||||
.log_name(&db("someuser"))
|
||||
.unwrap(),
|
||||
"Azure Workload Identity (login someuser)"
|
||||
);
|
||||
// Entra has no default login, so none is named rather than implying `postgres`.
|
||||
assert_eq!(
|
||||
PgAuthMode::WorkloadIdentity.log_name(&db("")).unwrap(),
|
||||
"Azure Workload Identity"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_s3object_jsonb_overflow() {
|
||||
let pg_err = Error::ExecutionErr(
|
||||
|
||||
Reference in New Issue
Block a user