feat(mcp): override execution policy per database
This commit is contained in:
24 files changed
+1279
-307
No files matched your search
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use axum::http::HeaderMap;
|
||||
use dbx_core::models::connection::ConnectionConfig;
|
||||
use dbx_core::storage::McpGlobalPolicy;
|
||||
use dbx_core::storage::{McpDatabaseScope, McpGlobalPolicy};
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::state::WebState;
|
||||
@@ -240,6 +240,53 @@ fn ensure_allowed(policy: &McpGlobalPolicy, connection_id: &str) -> Result<(), A
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_database_in_scope(policy: &McpGlobalPolicy, connection_id: &str, database: &str) -> Result<(), AppError> {
|
||||
let Some(rule) = policy.connection_policies.iter().find(|rule| rule.connection_id == connection_id) else {
|
||||
return Ok(());
|
||||
};
|
||||
match rule.database_scope {
|
||||
McpDatabaseScope::All => Ok(()),
|
||||
McpDatabaseScope::None => Err(AppError::from(format!(
|
||||
"DATABASE_OUT_OF_SCOPE: database '{database}' is not allowed by DBX MCP settings for connection '{connection_id}'"
|
||||
))),
|
||||
McpDatabaseScope::Selected if rule.allowed_databases.iter().any(|allowed| allowed == database) => Ok(()),
|
||||
McpDatabaseScope::Selected => Err(AppError::from(format!(
|
||||
"DATABASE_OUT_OF_SCOPE: database '{database}' is not allowed by DBX MCP settings for connection '{connection_id}'"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Execution modes are scoped defaults: a database setting overrides a
|
||||
/// configured connection default, which in turn overrides the global default.
|
||||
/// Connection read-only and production protections are checked separately.
|
||||
fn effective_database_execution_policy(policy: &McpGlobalPolicy, connection_id: &str, database: &str) -> (bool, bool) {
|
||||
dbx_core::mcp_policy::effective_database_execution_policy(policy, connection_id, database)
|
||||
}
|
||||
|
||||
fn ensure_sql_database_execution_scope(
|
||||
policy: &McpGlobalPolicy,
|
||||
connection: &ConnectionConfig,
|
||||
active_database: &str,
|
||||
sql: &str,
|
||||
) -> Result<(), AppError> {
|
||||
dbx_core::mcp_policy::ensure_sql_database_execution_scope(policy, connection, active_database, sql)
|
||||
.map_err(AppError::from)
|
||||
}
|
||||
|
||||
fn mongo_pipeline_output_databases(pipeline_json: &str, active_database: &str) -> Result<Vec<String>, AppError> {
|
||||
dbx_core::mcp_policy::mongo_pipeline_output_databases(pipeline_json, active_database).map_err(AppError::from)
|
||||
}
|
||||
|
||||
fn ensure_mongo_database_execution_scope(
|
||||
policy: &McpGlobalPolicy,
|
||||
connection_id: &str,
|
||||
active_database: &str,
|
||||
pipeline_json: &str,
|
||||
) -> Result<(), AppError> {
|
||||
dbx_core::mcp_policy::ensure_mongo_database_execution_scope(policy, connection_id, active_database, pipeline_json)
|
||||
.map_err(AppError::from)
|
||||
}
|
||||
|
||||
fn connection_read_only_error(message: impl Into<String>) -> AppError {
|
||||
AppError::from(format!("CONNECTION_READ_ONLY: {}", message.into()))
|
||||
}
|
||||
@@ -256,6 +303,19 @@ async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<C
|
||||
.ok_or_else(|| AppError::from(format!("Connection with id '{connection_id}' not found")))
|
||||
}
|
||||
|
||||
pub async fn resolve_database(
|
||||
state: &Arc<WebState>,
|
||||
headers: &HeaderMap,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
) -> Result<String, AppError> {
|
||||
if !is_mcp_request(headers) {
|
||||
return Ok(database.to_string());
|
||||
}
|
||||
let config = load_connection(state, connection_id).await?;
|
||||
Ok(dbx_core::mcp_policy::resolve_database(database, config.database.as_deref()))
|
||||
}
|
||||
|
||||
pub async fn ensure_scope(state: &Arc<WebState>, headers: &HeaderMap, connection_id: &str) -> Result<(), AppError> {
|
||||
if !is_mcp_request(headers) {
|
||||
return Ok(());
|
||||
@@ -289,17 +349,25 @@ pub async fn ensure_mongo_pipeline_target(
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
pipeline_json: &str,
|
||||
) -> Result<(), AppError> {
|
||||
) -> Result<String, AppError> {
|
||||
if !is_mcp_request(headers) {
|
||||
return Ok(());
|
||||
return Ok(database.to_string());
|
||||
}
|
||||
let policy = load_policy(state).await?;
|
||||
ensure_allowed(&policy, connection_id)?;
|
||||
let config = load_connection(state, connection_id).await?;
|
||||
if dbx_core::production_safety::mongo_pipeline_targets_production_database(&config, database, pipeline_json) {
|
||||
let database = dbx_core::mcp_policy::resolve_database(database, config.database.as_deref());
|
||||
ensure_database_in_scope(&policy, connection_id, &database)?;
|
||||
for target_database in mongo_pipeline_output_databases(pipeline_json, &database)? {
|
||||
ensure_database_in_scope(&policy, connection_id, &target_database)?;
|
||||
}
|
||||
ensure_mongo_database_execution_scope(&policy, connection_id, &database, pipeline_json)?;
|
||||
if dbx_core::production_safety::mongo_pipeline_targets_production_database(&config, &database, pipeline_json) {
|
||||
return Err(AppError::from(
|
||||
"PRODUCTION_DATABASE_READ_ONLY: MongoDB aggregate write targeting production scope is blocked.".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
Ok(database)
|
||||
}
|
||||
|
||||
async fn ensure_write_with_risk(
|
||||
@@ -315,22 +383,27 @@ async fn ensure_write_with_risk(
|
||||
}
|
||||
let policy = load_policy(state).await?;
|
||||
ensure_allowed(&policy, connection_id)?;
|
||||
if policy.read_only {
|
||||
return Err(AppError::from(format!("MCP_READ_ONLY: DBX MCP read-only mode is enabled. {action} blocked.")));
|
||||
let config = load_connection(state, connection_id).await?;
|
||||
let database = dbx_core::mcp_policy::resolve_database(database, config.database.as_deref());
|
||||
ensure_database_in_scope(&policy, connection_id, &database)?;
|
||||
let (read_only, allow_dangerous_sql) = effective_database_execution_policy(&policy, connection_id, &database);
|
||||
if read_only {
|
||||
return Err(AppError::from(format!(
|
||||
"MCP_READ_ONLY: MCP execution permission for database '{database}' is read-only. {action} blocked."
|
||||
)));
|
||||
}
|
||||
if dangerous && !policy.allow_dangerous_sql {
|
||||
if dangerous && !allow_dangerous_sql {
|
||||
return Err(AppError::from(format!(
|
||||
"SQL_BLOCKED: High-risk operation '{action}' is disabled in DBX MCP settings."
|
||||
)));
|
||||
}
|
||||
let config = load_connection(state, connection_id).await?;
|
||||
if config.read_only {
|
||||
return Err(connection_read_only_error(format!(
|
||||
"Connection '{}' has read-only protection enabled. {action} blocked.",
|
||||
config.name
|
||||
)));
|
||||
}
|
||||
if dbx_core::production_safety::is_production_database(&config, database) {
|
||||
if dbx_core::production_safety::is_production_database(&config, &database) {
|
||||
return Err(AppError::from(format!(
|
||||
"PRODUCTION_DATABASE_READ_ONLY: {action} blocked for production database '{database}'."
|
||||
)));
|
||||
@@ -345,40 +418,97 @@ pub async fn ensure_sql(
|
||||
database: &str,
|
||||
sql: &str,
|
||||
allow_database_switch: bool,
|
||||
) -> Result<(), AppError> {
|
||||
) -> Result<String, AppError> {
|
||||
if !is_mcp_request(headers) {
|
||||
return Ok(());
|
||||
return Ok(database.to_string());
|
||||
}
|
||||
let policy = load_policy(state).await?;
|
||||
ensure_allowed(&policy, connection_id)?;
|
||||
let config = load_connection(state, connection_id).await?;
|
||||
let database = dbx_core::mcp_policy::resolve_database(database, config.database.as_deref());
|
||||
ensure_database_in_scope(&policy, connection_id, &database)?;
|
||||
if !allow_database_switch && dbx_core::sql_risk::mcp_sql_has_forbidden_database_switch(sql, config.db_type) {
|
||||
return Err(AppError::from(
|
||||
"SQL_BLOCKED: MCP does not allow USE or persistent database switching.".to_string(),
|
||||
));
|
||||
}
|
||||
let is_write = dbx_core::query_execution_sql::is_write_sql_for_database(sql, config.db_type);
|
||||
if policy.read_only && is_write {
|
||||
return Err(AppError::from("MCP_READ_ONLY: DBX MCP read-only mode is enabled. SQL write blocked.".to_string()));
|
||||
ensure_sql_database_execution_scope(&policy, &config, &database, sql)?;
|
||||
let (read_only, allow_dangerous_sql) = effective_database_execution_policy(&policy, connection_id, &database);
|
||||
if read_only && is_write {
|
||||
return Err(AppError::from(format!(
|
||||
"MCP_READ_ONLY: MCP execution permission for database '{database}' is read-only. SQL write blocked."
|
||||
)));
|
||||
}
|
||||
if !policy.allow_dangerous_sql && dbx_core::sql_risk::is_dangerous_sql_for_database(sql, config.db_type) {
|
||||
if !allow_dangerous_sql && dbx_core::sql_risk::is_dangerous_sql_for_database(sql, config.db_type) {
|
||||
return Err(AppError::from("SQL_BLOCKED: High-risk SQL is disabled in DBX MCP settings.".to_string()));
|
||||
}
|
||||
if config.read_only {
|
||||
dbx_core::query_execution_sql::check_read_only(sql, &config.name, config.db_type)
|
||||
.map_err(connection_read_only_error)?;
|
||||
}
|
||||
if is_write && dbx_core::production_safety::targets_production_database(&config, database, sql) {
|
||||
if is_write && dbx_core::production_safety::targets_production_database(&config, &database, sql) {
|
||||
return Err(AppError::from(
|
||||
"PRODUCTION_DATABASE_READ_ONLY: SQL write targeting production scope is blocked.".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
Ok(database)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{connection_read_only_error, mongo_filter_is_effectively_unbounded, mongo_pipeline_has_write_stage};
|
||||
use super::{
|
||||
connection_read_only_error, effective_database_execution_policy, ensure_mongo_database_execution_scope,
|
||||
mongo_filter_is_effectively_unbounded, mongo_pipeline_has_write_stage,
|
||||
};
|
||||
use dbx_core::storage::{McpConnectionPolicy, McpDatabasePolicy, McpDatabaseScope, McpGlobalPolicy};
|
||||
|
||||
fn database_policy() -> McpGlobalPolicy {
|
||||
McpGlobalPolicy {
|
||||
read_only: true,
|
||||
allow_dangerous_sql: false,
|
||||
connection_policies: vec![McpConnectionPolicy {
|
||||
connection_id: "conn-1".to_string(),
|
||||
read_only: true,
|
||||
allow_dangerous_sql: false,
|
||||
execution_mode_configured: true,
|
||||
execution_mode_policy_version: Some(dbx_core::mcp_policy::MCP_EXECUTION_POLICY_VERSION),
|
||||
database_scope: McpDatabaseScope::Selected,
|
||||
allowed_databases: vec!["operations".to_string(), "reporting".to_string()],
|
||||
database_policies: vec![McpDatabasePolicy {
|
||||
database_name: "operations".to_string(),
|
||||
read_only: false,
|
||||
allow_dangerous_sql: true,
|
||||
}],
|
||||
}],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn database_execution_policy_overrides_global_and_connection_defaults() {
|
||||
let policy = database_policy();
|
||||
|
||||
assert_eq!(effective_database_execution_policy(&policy, "conn-1", "operations"), (false, true));
|
||||
assert_eq!(effective_database_execution_policy(&policy, "conn-1", "reporting"), (true, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn database_execution_policy_blocks_cross_database_mongo_aggregate_output() {
|
||||
let policy = database_policy();
|
||||
|
||||
assert!(
|
||||
ensure_mongo_database_execution_scope(&policy, "conn-1", "operations", r#"[{"$out":"archive"}]"#,).is_ok()
|
||||
);
|
||||
let error = ensure_mongo_database_execution_scope(
|
||||
&policy,
|
||||
"conn-1",
|
||||
"operations",
|
||||
r#"[{"$merge":{"into":{"db":"reporting","coll":"archive"}}}]"#,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.message.starts_with("DATABASE_EXECUTION_POLICY_OUT_OF_SCOPE:"), "{}", error.message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_read_only_errors_use_the_stable_mcp_code() {
|
||||
|
||||
@@ -338,7 +338,8 @@ pub async fn list_collections(
|
||||
Json(req): Json<MongoCollectionRequest>,
|
||||
) -> Result<Json<Vec<dbx_core::document_ops::CollectionInfo>>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let result = dbx_core::mongo_ops::mongo_list_collections_core(&state.app, &req.connection_id, &req.database)
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result = dbx_core::mongo_ops::mongo_list_collections_core(&state.app, &req.connection_id, &database)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(result))
|
||||
@@ -360,10 +361,10 @@ pub async fn drop_database(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop database")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop database").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Drop database").await?;
|
||||
dbx_core::mongo_ops::mongo_drop_database_core(&state.app, &req.connection_id, &req.database)
|
||||
dbx_core::mongo_ops::mongo_drop_database_core(&state.app, &req.connection_id, &database)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
@@ -374,10 +375,11 @@ pub async fn drop_collection(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCollectionNameRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop collection")
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop collection")
|
||||
.await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Drop collection").await?;
|
||||
dbx_core::mongo_ops::mongo_drop_collection_core(&state.app, &req.connection_id, &req.database, &req.collection)
|
||||
dbx_core::mongo_ops::mongo_drop_collection_core(&state.app, &req.connection_id, &database, &req.collection)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
@@ -388,13 +390,14 @@ pub async fn rename_collection(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoRenameCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Rename collection")
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Rename collection")
|
||||
.await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Rename collection").await?;
|
||||
dbx_core::mongo_ops::mongo_rename_collection_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.new_name,
|
||||
)
|
||||
@@ -408,13 +411,14 @@ pub async fn clone_collection(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCloneCollectionRequest>,
|
||||
) -> Result<Json<dbx_core::db::mongo_driver::MongoCloneCollectionResult>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Clone collection")
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Clone collection")
|
||||
.await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Clone collection").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_clone_collection_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.source_collection,
|
||||
&req.target_collection,
|
||||
)
|
||||
@@ -429,13 +433,14 @@ pub async fn find_documents(
|
||||
Json(req): Json<MongoFindRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
req.execution_id.clone(),
|
||||
dbx_core::document_ops::find_documents_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
req.skip.unwrap_or(0),
|
||||
req.limit.unwrap_or(50),
|
||||
@@ -457,6 +462,7 @@ pub async fn explain_find(
|
||||
Json(req): Json<MongoFindExplainRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let verbosity = req.verbosity.as_deref().unwrap_or("queryPlanner");
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
@@ -464,7 +470,7 @@ pub async fn explain_find(
|
||||
dbx_core::mongo_ops::mongo_explain_find_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
req.skip.unwrap_or(0),
|
||||
req.limit.unwrap_or(100),
|
||||
@@ -506,13 +512,14 @@ pub async fn count_documents(
|
||||
Json(req): Json<MongoCountRequest>,
|
||||
) -> Result<Json<u64>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
req.execution_id,
|
||||
dbx_core::mongo_ops::mongo_count_documents_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
req.filter.as_deref(),
|
||||
req.mode.as_deref(),
|
||||
@@ -528,10 +535,11 @@ pub async fn server_version(
|
||||
Json(req): Json<MongoServerVersionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
req.execution_id.clone(),
|
||||
dbx_core::mongo_ops::mongo_server_version_core(&state.app, &req.connection_id, &req.database),
|
||||
dbx_core::mongo_ops::mongo_server_version_core(&state.app, &req.connection_id, &database),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
|
||||
@@ -543,13 +551,14 @@ pub async fn collection_stats(
|
||||
Json(req): Json<MongoCollectionStatsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
req.execution_id.clone(),
|
||||
dbx_core::mongo_ops::mongo_collection_stats_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
req.scale,
|
||||
),
|
||||
@@ -564,23 +573,23 @@ pub async fn aggregate_documents(
|
||||
Json(req): Json<MongoAggregateRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::ensure_mongo_pipeline_target(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.pipeline_json,
|
||||
)
|
||||
.await?;
|
||||
if super::mcp_policy::mongo_pipeline_has_write_stage(&req.pipeline_json) {
|
||||
super::mcp_policy::ensure_dangerous_write(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
"MongoDB aggregate write",
|
||||
)
|
||||
.await?;
|
||||
super::mcp_policy::ensure_mongo_pipeline_target(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.pipeline_json,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
@@ -588,7 +597,7 @@ pub async fn aggregate_documents(
|
||||
dbx_core::mongo_ops::mongo_aggregate_documents_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.pipeline_json,
|
||||
req.max_rows,
|
||||
@@ -605,13 +614,14 @@ pub async fn distinct(
|
||||
Json(req): Json<MongoDistinctRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
req.execution_id.clone(),
|
||||
dbx_core::mongo_ops::mongo_distinct_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.field,
|
||||
req.filter.as_deref(),
|
||||
@@ -627,14 +637,11 @@ pub async fn list_index_specs(
|
||||
Json(req): Json<MongoCollectionNameRequest>,
|
||||
) -> Result<Json<Vec<dbx_core::db::mongo_driver::MongoIndexSpec>>, AppError> {
|
||||
super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
|
||||
let result = dbx_core::mongo_ops::mongo_list_index_specs_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.collection,
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
let result =
|
||||
dbx_core::mongo_ops::mongo_list_index_specs_core(&state.app, &req.connection_id, &database, &req.collection)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
@@ -643,13 +650,13 @@ pub async fn create_index(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCreateIndexRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Create index")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Create index").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Create index").await?;
|
||||
let name = dbx_core::mongo_ops::mongo_create_index_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.keys_json,
|
||||
req.options_json.as_deref(),
|
||||
@@ -664,13 +671,13 @@ pub async fn create_user(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCreateUserRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Create user")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Create user").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Create user").await?;
|
||||
let affected_rows = dbx_core::mongo_ops::mongo_create_user_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.user_json,
|
||||
req.write_concern_json.as_deref(),
|
||||
)
|
||||
@@ -684,19 +691,14 @@ pub async fn run_command(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoRunCommandRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
"Run MongoDB command",
|
||||
)
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Run MongoDB command")
|
||||
.await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Run MongoDB command").await?;
|
||||
let result = run_cancellable(
|
||||
&state,
|
||||
req.execution_id.clone(),
|
||||
dbx_core::mongo_ops::mongo_run_command_core(&state.app, &req.connection_id, &req.database, &req.command_json),
|
||||
dbx_core::mongo_ops::mongo_run_command_core(&state.app, &req.connection_id, &database, &req.command_json),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
|
||||
@@ -707,13 +709,13 @@ pub async fn drop_indexes(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoDropIndexesRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop indexes")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop indexes").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Drop indexes").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_drop_indexes_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
req.indexes_json.as_deref(),
|
||||
req.single,
|
||||
@@ -746,12 +748,13 @@ pub async fn insert_documents(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoInsertDocumentsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &req.database, "Insert").await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Insert").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Insert").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_insert_documents_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.docs_json,
|
||||
)
|
||||
@@ -784,17 +787,17 @@ pub async fn update_documents(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoUpdateDocumentsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
if super::mcp_policy::mongo_filter_is_effectively_unbounded(&req.filter_json) {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Update")
|
||||
.await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Update").await?;
|
||||
} else {
|
||||
super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &req.database, "Update").await?;
|
||||
super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Update").await?;
|
||||
}
|
||||
ensure_writable(&state.app, &req.connection_id, "Update").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_update_documents_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.filter_json,
|
||||
&req.update_json,
|
||||
@@ -811,13 +814,13 @@ pub async fn find_one_and_update(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoFindOneAndUpdateRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
ensure_find_one_write_policy(&state, &headers, &req.connection_id, &req.database, &req.filter_json, "Update")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
ensure_find_one_write_policy(&state, &headers, &req.connection_id, &database, &req.filter_json, "Update").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Update").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_find_one_and_update_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.filter_json,
|
||||
&req.update_json,
|
||||
@@ -833,13 +836,13 @@ pub async fn find_one_and_replace(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoFindOneAndReplaceRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
ensure_find_one_write_policy(&state, &headers, &req.connection_id, &req.database, &req.filter_json, "Replace")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
ensure_find_one_write_policy(&state, &headers, &req.connection_id, &database, &req.filter_json, "Replace").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Update").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_find_one_and_replace_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.filter_json,
|
||||
&req.replacement_json,
|
||||
@@ -855,13 +858,13 @@ pub async fn find_one_and_delete(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoFindOneAndDeleteRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
ensure_find_one_write_policy(&state, &headers, &req.connection_id, &req.database, &req.filter_json, "Delete")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
ensure_find_one_write_policy(&state, &headers, &req.connection_id, &database, &req.filter_json, "Delete").await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Delete").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_find_one_and_delete_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.filter_json,
|
||||
req.options_json.as_deref(),
|
||||
@@ -894,17 +897,17 @@ pub async fn delete_documents(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoDeleteDocumentsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
if super::mcp_policy::mongo_filter_is_effectively_unbounded(&req.filter_json) {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Delete")
|
||||
.await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Delete").await?;
|
||||
} else {
|
||||
super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &req.database, "Delete").await?;
|
||||
super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Delete").await?;
|
||||
}
|
||||
ensure_writable(&state.app, &req.connection_id, "Delete").await?;
|
||||
let result = dbx_core::mongo_ops::mongo_delete_documents_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.filter_json,
|
||||
req.many,
|
||||
|
||||
@@ -394,15 +394,22 @@ pub async fn execute_query(
|
||||
Json(req): Json<ExecuteQueryRequest>,
|
||||
) -> Result<Json<dbx_core::db::QueryResult>, AppError> {
|
||||
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
|
||||
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
|
||||
.await?;
|
||||
let database = super::mcp_policy::ensure_sql(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.sql,
|
||||
allow_database_switch,
|
||||
)
|
||||
.await?;
|
||||
let requested_execution_id = req.execution_id.filter(|id| !id.trim().is_empty());
|
||||
let keep_timeout_reachable = requested_execution_id.is_some();
|
||||
let execution_id = requested_execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
|
||||
|
||||
let registered = state.app.running_queries.register_task(
|
||||
execution_id.clone(),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), req.database.clone(), req.client_session_id.clone()),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), database.clone(), req.client_session_id.clone()),
|
||||
);
|
||||
let cancel_token = registered.token();
|
||||
|
||||
@@ -411,7 +418,7 @@ pub async fn execute_query(
|
||||
let result = dbx_core::query::execute_sql_statement_with_options_typed(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.sql,
|
||||
req.schema.as_deref(),
|
||||
Some(cancel_token),
|
||||
@@ -447,13 +454,20 @@ pub async fn execute_conditional_update(
|
||||
Json(req): Json<ExecuteQueryRequest>,
|
||||
) -> Result<Json<dbx_core::db::QueryResult>, AppError> {
|
||||
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
|
||||
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
|
||||
.await?;
|
||||
let database = super::mcp_policy::ensure_sql(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.sql,
|
||||
allow_database_switch,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let execution_id = req.execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
|
||||
let registered = state.app.running_queries.register_task_for_terminal_confirmation(
|
||||
execution_id.clone(),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), req.database.clone(), req.client_session_id.clone()),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), database.clone(), req.client_session_id.clone()),
|
||||
);
|
||||
let cancel_token = registered.token();
|
||||
let response_timeout = dbx_core::query::query_timeout_duration(req.timeout_secs);
|
||||
@@ -464,7 +478,7 @@ pub async fn execute_conditional_update(
|
||||
let result = dbx_core::query::execute_sql_statement_with_options_typed(
|
||||
&app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.sql,
|
||||
req.schema.as_deref(),
|
||||
Some(cancel_token),
|
||||
@@ -524,15 +538,22 @@ pub async fn execute_multi(
|
||||
Json(req): Json<ExecuteQueryRequest>,
|
||||
) -> Result<Response, AppError> {
|
||||
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
|
||||
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
|
||||
.await?;
|
||||
let database = super::mcp_policy::ensure_sql(
|
||||
&state,
|
||||
&headers,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.sql,
|
||||
allow_database_switch,
|
||||
)
|
||||
.await?;
|
||||
let requested_execution_id = req.execution_id.filter(|id| !id.trim().is_empty());
|
||||
let keep_timeout_reachable = requested_execution_id.is_some();
|
||||
let execution_id = requested_execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
|
||||
|
||||
let registered = state.app.running_queries.register_task(
|
||||
execution_id.clone(),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), req.database.clone(), req.client_session_id.clone()),
|
||||
RunningTaskMetadata::query(req.connection_id.clone(), database.clone(), req.client_session_id.clone()),
|
||||
);
|
||||
let cancel_token = registered.token();
|
||||
|
||||
@@ -542,7 +563,7 @@ pub async fn execute_multi(
|
||||
let result = dbx_core::query::execute_multi_core_with_options_for_client_typed(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.sql,
|
||||
req.schema.as_deref(),
|
||||
Some(cancel_token),
|
||||
@@ -599,14 +620,15 @@ pub async fn execute_batch(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<ExecuteBatchRequest>,
|
||||
) -> Result<Json<dbx_core::db::QueryResult>, AppError> {
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
for statement in &req.statements {
|
||||
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, statement, false).await?;
|
||||
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &database, statement, false).await?;
|
||||
}
|
||||
tracing::debug!(connection_id = %req.connection_id, "execute_batch");
|
||||
let result = dbx_core::query::execute_statements(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.statements,
|
||||
req.schema.as_deref(),
|
||||
req.timeout_secs,
|
||||
|
||||
@@ -57,10 +57,10 @@ pub async fn drop_database(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<VectorDatabaseRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop database")
|
||||
.await?;
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop database").await?;
|
||||
ensure_writable(&state, &req.connection_id, "Drop database").await?;
|
||||
dbx_core::schema::drop_vector_database_core(&state.app, &req.connection_id, &req.database)
|
||||
dbx_core::schema::drop_vector_database_core(&state.app, &req.connection_id, &database)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
@@ -71,10 +71,11 @@ pub async fn drop_collection(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<VectorCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop collection")
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop collection")
|
||||
.await?;
|
||||
ensure_writable(&state, &req.connection_id, "Drop collection").await?;
|
||||
dbx_core::schema::drop_vector_collection_core(&state.app, &req.connection_id, &req.database, &req.collection)
|
||||
dbx_core::schema::drop_vector_collection_core(&state.app, &req.connection_id, &database, &req.collection)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
@@ -85,13 +86,14 @@ pub async fn rename_collection(
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<VectorRenameCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Rename collection")
|
||||
let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Rename collection")
|
||||
.await?;
|
||||
ensure_writable(&state, &req.connection_id, "Rename collection").await?;
|
||||
dbx_core::schema::rename_vector_collection_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&database,
|
||||
&req.collection,
|
||||
&req.new_name,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user