fix(iris): qualify selected schema tables
This commit is contained in:
1 parent
1991bf118b
commit
3574113feb
3 files changed
+180
-9
No files matched your search
@@ -28,7 +28,7 @@ tokio-postgres-rustls = "0.13"
|
||||
webpki-roots = "0.26"
|
||||
rusqlite = { version = "0.32", features = ["bundled", "load_extension", "backup"] }
|
||||
mysql_async = { version = "0.36", default-features = false, features = ["default-rustls", "client_ed25519", "chrono", "rust_decimal"] }
|
||||
sqlparser = "0.62.0"
|
||||
sqlparser = { version = "0.62.0", features = ["visitor"] }
|
||||
redis = { version = "0.32.2", features = ["tokio-comp", "tls-rustls", "tls-rustls-insecure", "tokio-rustls-comp", "sentinel", "cluster-async"] }
|
||||
rustls = { version = "0.23", features = ["aws-lc-rs"] }
|
||||
rustls-pemfile = "2.2"
|
||||
|
||||
@@ -3,10 +3,12 @@ use chrono::{DateTime, Duration as ChronoDuration, NaiveDate, NaiveDateTime, Nai
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
use duckdb::types::{TimeUnit, Value, ValueRef};
|
||||
use mysql_async::prelude::Queryable;
|
||||
use sqlparser::ast::{ObjectType, Statement};
|
||||
use sqlparser::dialect::PostgreSqlDialect;
|
||||
use sqlparser::ast::{visit_relations_mut, Ident, ObjectName, ObjectNamePart, ObjectType, Statement};
|
||||
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
|
||||
use sqlparser::parser::Parser;
|
||||
use std::collections::HashSet;
|
||||
use std::future::Future;
|
||||
use std::ops::ControlFlow;
|
||||
use std::time::Duration;
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
use tokio::task::JoinHandle;
|
||||
@@ -99,9 +101,94 @@ async fn connection_database_type_for_pool_key(state: &AppState, pool_key: &str)
|
||||
}
|
||||
|
||||
fn schema_for_execution_context(db_type: Option<DatabaseType>, schema: Option<&str>) -> Option<&str> {
|
||||
match db_type {
|
||||
Some(DatabaseType::Iris) => None,
|
||||
_ => schema,
|
||||
if matches!(db_type, Some(DatabaseType::Iris)) {
|
||||
None
|
||||
} else {
|
||||
schema
|
||||
}
|
||||
}
|
||||
|
||||
fn sql_for_execution_context(db_type: Option<DatabaseType>, sql: &str, schema: Option<&str>) -> String {
|
||||
if matches!(db_type, Some(DatabaseType::Iris)) {
|
||||
if let Some(schema) = schema.map(str::trim).filter(|schema| !schema.is_empty()) {
|
||||
return qualify_iris_unqualified_dml(sql, schema).unwrap_or_else(|| sql.to_string());
|
||||
}
|
||||
}
|
||||
sql.to_string()
|
||||
}
|
||||
|
||||
fn qualify_iris_unqualified_dml(sql: &str, schema: &str) -> Option<String> {
|
||||
let dialect = GenericDialect {};
|
||||
let mut statements = Parser::parse_sql(&dialect, sql).ok()?;
|
||||
if statements.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut changed = false;
|
||||
for statement in &mut statements {
|
||||
if !iris_statement_uses_schema_search_path(statement) {
|
||||
continue;
|
||||
}
|
||||
let cte_names = iris_statement_cte_names(statement);
|
||||
let _ = visit_relations_mut(statement, |name| {
|
||||
if qualify_iris_relation_name(name, schema, &cte_names) {
|
||||
changed = true;
|
||||
}
|
||||
ControlFlow::<()>::Continue(())
|
||||
});
|
||||
}
|
||||
|
||||
changed.then(|| statements.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))
|
||||
}
|
||||
|
||||
fn iris_statement_uses_schema_search_path(statement: &Statement) -> bool {
|
||||
matches!(
|
||||
statement,
|
||||
Statement::Query(_)
|
||||
| Statement::Insert(_)
|
||||
| Statement::Update(_)
|
||||
| Statement::Delete(_)
|
||||
| Statement::Truncate(_)
|
||||
)
|
||||
}
|
||||
|
||||
fn qualify_iris_relation_name(name: &mut ObjectName, schema: &str, cte_names: &HashSet<String>) -> bool {
|
||||
let [ObjectNamePart::Identifier(table)] = name.0.as_slice() else {
|
||||
return false;
|
||||
};
|
||||
if cte_names.contains(&table.value.to_ascii_uppercase()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let table = table.clone();
|
||||
name.0 = vec![ObjectNamePart::Identifier(Ident::with_quote('"', schema)), ObjectNamePart::Identifier(table)];
|
||||
true
|
||||
}
|
||||
|
||||
fn iris_statement_cte_names(statement: &Statement) -> HashSet<String> {
|
||||
let mut names = HashSet::new();
|
||||
collect_iris_statement_cte_names(statement, &mut names);
|
||||
names
|
||||
}
|
||||
|
||||
fn collect_iris_statement_cte_names(statement: &Statement, names: &mut HashSet<String>) {
|
||||
match statement {
|
||||
Statement::Query(query) => collect_iris_query_cte_names(query, names),
|
||||
Statement::Insert(insert) => {
|
||||
if let Some(source) = &insert.source {
|
||||
collect_iris_query_cte_names(source, names);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_iris_query_cte_names(query: &sqlparser::ast::Query, names: &mut HashSet<String>) {
|
||||
if let Some(with) = &query.with {
|
||||
for cte in &with.cte_tables {
|
||||
names.insert(cte.alias.name.value.to_ascii_uppercase());
|
||||
collect_iris_query_cte_names(&cte.query, names);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -829,7 +916,7 @@ pub async fn do_execute(
|
||||
}
|
||||
PoolKind::Agent(client) => {
|
||||
let client = client.clone();
|
||||
let sql = sql.to_string();
|
||||
let sql = sql_for_execution_context(pool_db_type, sql, schema);
|
||||
let database = database.map(|s| s.to_string());
|
||||
let schema = schema_for_execution_context(pool_db_type, schema).map(|s| s.to_string());
|
||||
let max_rows = options.max_rows;
|
||||
@@ -1655,9 +1742,17 @@ async fn exec_tx_explicit_inner(
|
||||
let conns = state.connections.read().await;
|
||||
if let Some(crate::connection::PoolKind::Agent(client)) = conns.get(pool_key) {
|
||||
let db_type = connection_database_type_for_pool_key(state, pool_key).await;
|
||||
let schema = schema_for_execution_context(db_type, schema);
|
||||
let execution_schema = schema_for_execution_context(db_type, schema);
|
||||
let rewritten_statements;
|
||||
let statements = if matches!(db_type, Some(DatabaseType::Iris)) {
|
||||
rewritten_statements =
|
||||
statements.iter().map(|sql| sql_for_execution_context(db_type, sql, schema)).collect::<Vec<_>>();
|
||||
rewritten_statements.as_slice()
|
||||
} else {
|
||||
statements
|
||||
};
|
||||
let mut client = client.lock().await;
|
||||
let result: db::QueryResult = client.execute_transaction(database, statements, schema).await?;
|
||||
let result: db::QueryResult = client.execute_transaction(database, statements, execution_schema).await?;
|
||||
return Ok(db::QueryResult { execution_time_ms: start.elapsed().as_millis(), ..result });
|
||||
}
|
||||
drop(conns);
|
||||
@@ -2133,6 +2228,70 @@ mod tests {
|
||||
assert_eq!(schema_for_execution_context(None, Some("APP")), Some("APP"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_execution_context_qualifies_unqualified_dml_tables() {
|
||||
assert_eq!(
|
||||
sql_for_execution_context(Some(DatabaseType::Iris), "SELECT * FROM TABLES", Some("INFORMATION_SCHEMA")),
|
||||
"SELECT * FROM \"INFORMATION_SCHEMA\".TABLES"
|
||||
);
|
||||
assert_eq!(
|
||||
sql_for_execution_context(
|
||||
Some(DatabaseType::Iris),
|
||||
"SELECT * FROM orders o JOIN customers c ON c.id = o.customer_id",
|
||||
Some("Sales")
|
||||
),
|
||||
"SELECT * FROM \"Sales\".orders AS o JOIN \"Sales\".customers AS c ON c.id = o.customer_id"
|
||||
);
|
||||
assert_eq!(
|
||||
sql_for_execution_context(Some(DatabaseType::Iris), "SELECT * FROM INFORMATION_SCHEMA.TABLES", Some("APP")),
|
||||
"SELECT * FROM INFORMATION_SCHEMA.TABLES"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_execution_context_qualifies_nested_dml_tables_but_not_ctes() {
|
||||
assert_eq!(
|
||||
sql_for_execution_context(
|
||||
Some(DatabaseType::Iris),
|
||||
"WITH recent AS (SELECT * FROM events) SELECT * FROM recent WHERE EXISTS (SELECT 1 FROM audits)",
|
||||
Some("APP")
|
||||
),
|
||||
"WITH recent AS (SELECT * FROM \"APP\".events) SELECT * FROM recent WHERE EXISTS (SELECT 1 FROM \"APP\".audits)"
|
||||
);
|
||||
assert_eq!(
|
||||
sql_for_execution_context(
|
||||
Some(DatabaseType::Iris),
|
||||
"INSERT INTO events SELECT * FROM staging_events",
|
||||
Some("APP")
|
||||
),
|
||||
"INSERT INTO \"APP\".events SELECT * FROM \"APP\".staging_events"
|
||||
);
|
||||
assert_eq!(
|
||||
sql_for_execution_context(
|
||||
Some(DatabaseType::Iris),
|
||||
"UPDATE events SET status = 'done' WHERE id IN (SELECT event_id FROM audit_events)",
|
||||
Some("APP")
|
||||
),
|
||||
"UPDATE \"APP\".events SET status = 'done' WHERE id IN (SELECT event_id FROM \"APP\".audit_events)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_execution_context_leaves_ddl_and_unparseable_sql_unchanged() {
|
||||
assert_eq!(
|
||||
sql_for_execution_context(Some(DatabaseType::Iris), "CREATE TABLE events (id INT)", Some("APP")),
|
||||
"CREATE TABLE events (id INT)"
|
||||
);
|
||||
assert_eq!(
|
||||
sql_for_execution_context(Some(DatabaseType::Iris), "SELECT %ID FROM", Some("APP")),
|
||||
"SELECT %ID FROM"
|
||||
);
|
||||
assert_eq!(
|
||||
sql_for_execution_context(Some(DatabaseType::Postgres), "SELECT * FROM events", Some("APP")),
|
||||
"SELECT * FROM events"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_postgres_drop_database_target() {
|
||||
assert_eq!(parse_drop_database_target("DROP DATABASE vaultwarden;"), Some("vaultwarden".to_string()));
|
||||
|
||||
Reference in new issue
Block a user