feat(salesforce): add Salesforce SOQL data source
This commit is contained in:
125 files changed
+9999
-117
No files matched your search
@@ -166,6 +166,21 @@ fn apply_ceiling(current: (bool, bool), ceiling: (bool, bool)) -> (bool, bool) {
|
||||
(read_only, !read_only && current.1 && ceiling.1)
|
||||
}
|
||||
|
||||
/// Whether an AI agent may write to this Salesforce connection through MCP.
|
||||
///
|
||||
/// A pure per-connection opt-in: no global or group setting can turn it on, and
|
||||
/// a connection without a saved rule stays off. Callers still run every write
|
||||
/// through the ordinary execution policy, so the global/connection read-only
|
||||
/// modes, connection read-only protection and production protection all remain
|
||||
/// upper bounds on top of this switch.
|
||||
pub fn connection_allows_salesforce_dml(policy: &McpGlobalPolicy, connection_id: &str) -> bool {
|
||||
policy
|
||||
.connection_policies
|
||||
.iter()
|
||||
.find(|rule| rule.connection_id == connection_id)
|
||||
.is_some_and(|rule| rule.allow_salesforce_dml && !rule.read_only)
|
||||
}
|
||||
|
||||
/// Reject qualified SQL references when database-specific execution rules are
|
||||
/// present and individual referenced databases have not been evaluated.
|
||||
pub fn ensure_sql_database_execution_scope(
|
||||
@@ -251,7 +266,10 @@ pub fn ensure_mongo_database_execution_scope(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{effective_database_execution_policy, resolve_database, MCP_EXECUTION_POLICY_VERSION};
|
||||
use super::{
|
||||
connection_allows_salesforce_dml, effective_database_execution_policy, resolve_database,
|
||||
MCP_EXECUTION_POLICY_VERSION,
|
||||
};
|
||||
use crate::storage::{McpConnectionPolicy, McpDatabasePolicy, McpGlobalPolicy};
|
||||
|
||||
fn policy(version: Option<u8>) -> McpGlobalPolicy {
|
||||
@@ -270,6 +288,7 @@ mod tests {
|
||||
read_only: false,
|
||||
allow_dangerous_sql: true,
|
||||
}],
|
||||
allow_salesforce_dml: false,
|
||||
}],
|
||||
..Default::default()
|
||||
}
|
||||
@@ -316,4 +335,26 @@ mod tests {
|
||||
assert_eq!(resolve_database(" ", Some("sample")), "sample");
|
||||
assert_eq!(resolve_database("analytics", Some("sample")), "analytics");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn salesforce_dml_needs_an_explicit_connection_opt_in() {
|
||||
// No rule for the connection at all → off, whatever the global mode says.
|
||||
let mut open = policy(Some(MCP_EXECUTION_POLICY_VERSION));
|
||||
open.read_only = false;
|
||||
assert!(!connection_allows_salesforce_dml(&open, "other"));
|
||||
assert!(!connection_allows_salesforce_dml(&open, "conn"));
|
||||
|
||||
open.connection_policies[0].allow_salesforce_dml = true;
|
||||
assert!(connection_allows_salesforce_dml(&open, "conn"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn salesforce_dml_opt_in_is_void_on_a_read_only_connection_rule() {
|
||||
let mut policy = policy(Some(MCP_EXECUTION_POLICY_VERSION));
|
||||
policy.connection_policies[0].allow_salesforce_dml = true;
|
||||
policy.connection_policies[0].read_only = true;
|
||||
// `normalized()` already clears the flag for read-only rules; this keeps a
|
||||
// hand-built or legacy policy from sneaking a write through.
|
||||
assert!(!connection_allows_salesforce_dml(&policy, "conn"));
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,10 @@ pub const MQTT_AUTH_PASSWORD_KEY: &str = "mqtt.auth.password";
|
||||
pub const CASSANDRA_TLS_SECRET_PREFIX: &str = "cassandra.tls.";
|
||||
pub const CASSANDRA_TRUSTSTORE_PASSWORD_KEY: &str = "cassandra.tls.truststore_password";
|
||||
pub const CASSANDRA_KEYSTORE_PASSWORD_KEY: &str = "cassandra.tls.keystore_password";
|
||||
pub const SALESFORCE_AUTH_SECRET_PREFIX: &str = "salesforce.auth.";
|
||||
pub const SALESFORCE_AUTH_CLIENT_SECRET_KEY: &str = "salesforce.auth.client_secret";
|
||||
pub const SALESFORCE_AUTH_REFRESH_TOKEN_KEY: &str = "salesforce.auth.refresh_token";
|
||||
pub const SALESFORCE_AUTH_PASSWORD_KEY: &str = "salesforce.auth.password";
|
||||
pub const PLUGIN_CONNECTION_SECRET_PREFIX: &str = "plugin_connection.";
|
||||
|
||||
/// Storage-level secret key for one plugin connection field: namespaced under
|
||||
@@ -127,6 +131,7 @@ pub fn save_connections_to_file(
|
||||
persist_mq_token_signing_secret(store, config)?;
|
||||
persist_mqtt_auth_secrets(store, config)?;
|
||||
persist_cassandra_tls_secrets(store, config)?;
|
||||
persist_salesforce_auth_secrets(store, config)?;
|
||||
delete_secret_prefix(store, &config.id, PLUGIN_CONNECTION_SECRET_PREFIX)?;
|
||||
for (key, secret) in &config.connection_secrets {
|
||||
persist_secret(store, &config.id, &plugin_connection_secret_key(key)?, secret)?;
|
||||
@@ -201,6 +206,7 @@ pub fn load_connections_from_file(
|
||||
hydrate_mq_token_signing_secret(store, config, &mut needs_rewrite)?;
|
||||
hydrate_mqtt_auth_secrets(store, config, &mut needs_rewrite)?;
|
||||
hydrate_cassandra_tls_secrets(store, config, &mut needs_rewrite)?;
|
||||
hydrate_salesforce_auth_secrets(store, config, &mut needs_rewrite)?;
|
||||
let plugin_secret_keys = config.connection_secrets.keys().cloned().collect::<Vec<_>>();
|
||||
for key in plugin_secret_keys {
|
||||
let storage_key = plugin_connection_secret_key(&key)?;
|
||||
@@ -394,6 +400,7 @@ fn delete_removed_connection_secrets(
|
||||
delete_secret_prefix(store, &config.id, MQ_TOKEN_SIGNING_SECRET_PREFIX)?;
|
||||
delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
|
||||
delete_secret_prefix(store, &config.id, CASSANDRA_TLS_SECRET_PREFIX)?;
|
||||
delete_secret_prefix(store, &config.id, SALESFORCE_AUTH_SECRET_PREFIX)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -653,6 +660,96 @@ fn cassandra_tls_object_mut(
|
||||
value?.get_mut("tls")?.as_object_mut()
|
||||
}
|
||||
|
||||
// ── Salesforce OAuth 密钥持久化 ─────────────────────────────────
|
||||
//
|
||||
// Persist semantics (mirrors MQ's replace_mq_auth_secret):
|
||||
// 1. Incoming empty/missing + secret already stored → preserve existing secret.
|
||||
// 2. Incoming non-empty → overwrite.
|
||||
// 3. external_config.auth block absent (user switched off OAuth) → delete both secrets.
|
||||
// This is required because the frontend edits an existing connection with the
|
||||
// sensitive fields blanked out — an empty value must not clobber the stored secret.
|
||||
|
||||
fn persist_salesforce_auth_secrets(store: &dyn ConnectionSecretStore, config: &ConnectionConfig) -> Result<(), String> {
|
||||
if config.db_type != DatabaseType::Salesforce {
|
||||
delete_secret_prefix(store, &config.id, SALESFORCE_AUTH_SECRET_PREFIX)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let Some(auth) = salesforce_auth_object(config.external_config.as_ref()) else {
|
||||
delete_secret_prefix(store, &config.id, SALESFORCE_AUTH_SECRET_PREFIX)?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
replace_salesforce_auth_secret(store, &config.id, SALESFORCE_AUTH_CLIENT_SECRET_KEY, auth, "clientSecret")?;
|
||||
replace_salesforce_auth_secret(store, &config.id, SALESFORCE_AUTH_REFRESH_TOKEN_KEY, auth, "refreshToken")?;
|
||||
replace_salesforce_auth_secret(store, &config.id, SALESFORCE_AUTH_PASSWORD_KEY, auth, "password")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn replace_salesforce_auth_secret(
|
||||
store: &dyn ConnectionSecretStore,
|
||||
connection_id: &str,
|
||||
key: &str,
|
||||
auth: &serde_json::Map<String, serde_json::Value>,
|
||||
field: &str,
|
||||
) -> Result<(), String> {
|
||||
let current = auth.get(field).and_then(serde_json::Value::as_str).filter(|secret| !secret.is_empty());
|
||||
let existing = if current.is_none() { store.get_secret(connection_id, key)? } else { None };
|
||||
// Per-key delete: we only manage the exact key we own, so two parallel
|
||||
// replace calls don't wipe each other out.
|
||||
store.delete_secret(connection_id, key)?;
|
||||
match current {
|
||||
Some(secret) => store.set_secret(connection_id, key, secret),
|
||||
None => match existing {
|
||||
Some(secret) => store.set_secret(connection_id, key, &secret),
|
||||
None => Ok(()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn hydrate_salesforce_auth_secrets(
|
||||
store: &dyn ConnectionSecretStore,
|
||||
config: &mut ConnectionConfig,
|
||||
needs_rewrite: &mut bool,
|
||||
) -> Result<(), String> {
|
||||
if config.db_type != DatabaseType::Salesforce {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let Some(auth) = salesforce_auth_object_mut(config.external_config.as_mut()) else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
hydrate_json_secret(store, &config.id, SALESFORCE_AUTH_CLIENT_SECRET_KEY, auth, "clientSecret", needs_rewrite)?;
|
||||
hydrate_json_secret(store, &config.id, SALESFORCE_AUTH_REFRESH_TOKEN_KEY, auth, "refreshToken", needs_rewrite)?;
|
||||
hydrate_json_secret(store, &config.id, SALESFORCE_AUTH_PASSWORD_KEY, auth, "password", needs_rewrite)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn scrub_salesforce_auth_secrets(config: &mut ConnectionConfig) {
|
||||
if config.db_type != DatabaseType::Salesforce {
|
||||
return;
|
||||
}
|
||||
let Some(auth) = salesforce_auth_object_mut(config.external_config.as_mut()) else {
|
||||
return;
|
||||
};
|
||||
scrub_json_secret(auth, "clientSecret");
|
||||
scrub_json_secret(auth, "refreshToken");
|
||||
scrub_json_secret(auth, "password");
|
||||
}
|
||||
|
||||
fn salesforce_auth_object(value: Option<&serde_json::Value>) -> Option<&serde_json::Map<String, serde_json::Value>> {
|
||||
value?.get("auth")?.as_object()
|
||||
}
|
||||
|
||||
fn salesforce_auth_object_mut(
|
||||
value: Option<&mut serde_json::Value>,
|
||||
) -> Option<&mut serde_json::Map<String, serde_json::Value>> {
|
||||
value?.get_mut("auth")?.as_object_mut()
|
||||
}
|
||||
|
||||
// ── MQTT 密钥持久化 ──────────────────────────────────────────────
|
||||
|
||||
fn persist_mqtt_auth_secrets(store: &dyn ConnectionSecretStore, config: &ConnectionConfig) -> Result<(), String> {
|
||||
@@ -845,6 +942,7 @@ fn sanitize_connections(configs: &[ConnectionConfig]) -> Vec<ConnectionConfig> {
|
||||
scrub_mq_token_signing_secret(&mut config);
|
||||
scrub_mqtt_auth_secrets(&mut config);
|
||||
scrub_cassandra_tls_secrets(&mut config);
|
||||
scrub_salesforce_auth_secrets(&mut config);
|
||||
scrub_plugin_connection_secrets(&mut config);
|
||||
config
|
||||
})
|
||||
|
||||
@@ -43,6 +43,7 @@ use crate::plugins::{
|
||||
PluginRuntimeEnv, PluginRuntimeProxy,
|
||||
};
|
||||
use crate::query_cancel::RunningQueries;
|
||||
use crate::salesforce_oauth::SfBrowserOpener;
|
||||
use crate::session_credentials::SessionCredentialStore;
|
||||
use crate::storage::{normalize_duckdb_worker_max_processes, Storage, DUCKDB_WORKER_MAX_PROCESSES_DEFAULT};
|
||||
use crate::task_supervisor::TaskSupervisor;
|
||||
@@ -118,6 +119,7 @@ pub enum PoolKind {
|
||||
Easysearch(db::easysearch_driver::EasysearchClient),
|
||||
Solr(db::solr_driver::SolrClient),
|
||||
Meilisearch(db::meilisearch_driver::MeilisearchClient),
|
||||
Salesforce(db::salesforce_driver::SfClient),
|
||||
HBase(db::hbase_driver::HBaseClient),
|
||||
VectorDb(db::vector_driver::VectorClient),
|
||||
InfluxDb(db::influxdb_driver::InfluxdbClient),
|
||||
@@ -367,6 +369,24 @@ struct SharedResourceBudget {
|
||||
semaphore: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
/// Cached Salesforce connected-user identity + org display name, serialized
|
||||
/// with camelCase field names for the frontend. `Deserialize` is derived too so
|
||||
/// the Web-mode MCP backend can decode the same JSON the desktop route emits.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SalesforceCurrentUser {
|
||||
pub user_id: String,
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
pub organization_id: String,
|
||||
pub username: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub profile_name: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub is_admin: Option<bool>,
|
||||
pub org_name: String,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
connections: Arc<RwLock<ConnectionPoolRegistry>>,
|
||||
task_supervisor: TaskSupervisor,
|
||||
@@ -407,6 +427,7 @@ pub struct AppState {
|
||||
pub write_unlock_windows: crate::write_unlock::WriteUnlockWindows,
|
||||
metadata_gates: Arc<Mutex<HashMap<String, Arc<Semaphore>>>>,
|
||||
mongo_oidc_browser_opener: std::sync::RwLock<Option<MongoOidcBrowserOpener>>,
|
||||
salesforce_browser_opener: std::sync::RwLock<Option<SfBrowserOpener>>,
|
||||
#[cfg(feature = "mq-admin")]
|
||||
pub mq_registry: crate::mq::MqAdminRegistry,
|
||||
}
|
||||
@@ -1601,6 +1622,7 @@ impl AppState {
|
||||
write_unlock_windows: crate::write_unlock::WriteUnlockWindows::default(),
|
||||
metadata_gates: Arc::new(Mutex::new(HashMap::new())),
|
||||
mongo_oidc_browser_opener: std::sync::RwLock::new(None),
|
||||
salesforce_browser_opener: std::sync::RwLock::new(None),
|
||||
#[cfg(feature = "mq-admin")]
|
||||
mq_registry: crate::mq::MqAdminRegistry::new(),
|
||||
}
|
||||
@@ -1614,6 +1636,14 @@ impl AppState {
|
||||
self.mongo_oidc_browser_opener.read().expect("MongoDB OIDC browser opener lock poisoned").clone()
|
||||
}
|
||||
|
||||
pub fn set_salesforce_browser_opener(&self, opener: SfBrowserOpener) {
|
||||
*self.salesforce_browser_opener.write().expect("Salesforce browser opener lock poisoned") = Some(opener);
|
||||
}
|
||||
|
||||
pub fn salesforce_browser_opener(&self) -> Option<SfBrowserOpener> {
|
||||
self.salesforce_browser_opener.read().expect("Salesforce browser opener lock poisoned").clone()
|
||||
}
|
||||
|
||||
pub(crate) async fn acquire_metadata_permit(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
@@ -2844,6 +2874,16 @@ impl AppState {
|
||||
db::meilisearch_driver::test_connection(&client, connect_timeout).await?;
|
||||
PoolKind::Meilisearch(client)
|
||||
}
|
||||
DatabaseType::Salesforce => {
|
||||
let client = db::salesforce_driver::SfClient::from_config(
|
||||
&url,
|
||||
Some(&db_config.password),
|
||||
db_config.external_config.as_ref(),
|
||||
connect_timeout,
|
||||
)?;
|
||||
db::salesforce_driver::SfClient::test_connection(&client, connect_timeout).await?;
|
||||
PoolKind::Salesforce(client)
|
||||
}
|
||||
DatabaseType::Hbase => {
|
||||
let client = db::hbase_driver::HBaseClient::new(
|
||||
&url,
|
||||
@@ -4214,6 +4254,17 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::Salesforce(client) => {
|
||||
let client = client.clone();
|
||||
let timeout = crate::db::connection_timeout();
|
||||
match db::salesforce_driver::SfClient::test_connection(&client, timeout).await {
|
||||
Ok(()) => false,
|
||||
Err(err) => {
|
||||
log::warn!("Salesforce connection pool '{pool_key}' is stale: {err}");
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::HBase(client) => {
|
||||
let client = client.clone();
|
||||
let timeout = crate::db::connection_timeout();
|
||||
@@ -5249,6 +5300,38 @@ impl AppState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Cached connected-user identity for a Salesforce connection. Returns a
|
||||
/// camelCase-serializable struct for the frontend (identity badge, admin
|
||||
/// warning). Errors when the connection is not Salesforce or has no pool.
|
||||
pub async fn salesforce_current_user(&self, connection_id: &str) -> Result<SalesforceCurrentUser, String> {
|
||||
let db_type = {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).map(|c| c.db_type)
|
||||
};
|
||||
if db_type != Some(DatabaseType::Salesforce) {
|
||||
return Err("Not a Salesforce connection".to_string());
|
||||
}
|
||||
let pool_key = base_pool_key_for(db_type, connection_id, None, false);
|
||||
let pool = self.pool_handle(&pool_key).await.ok_or_else(|| "Connection not found".to_string())?;
|
||||
match pool {
|
||||
PoolKind::Salesforce(client) => {
|
||||
let user = client.cached_current_user().await?;
|
||||
let org_name = client.org_display_name().await;
|
||||
Ok(SalesforceCurrentUser {
|
||||
user_id: user.user_id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
organization_id: user.organization_id,
|
||||
username: user.username,
|
||||
profile_name: user.profile_name,
|
||||
is_admin: user.is_admin,
|
||||
org_name,
|
||||
})
|
||||
}
|
||||
_ => Err("Not a Salesforce connection".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Warm the driver/pool a tab is about to use, off the user's critical path.
|
||||
///
|
||||
/// The first statement of a session pays costs that the user perceives as
|
||||
@@ -5405,6 +5488,16 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::Salesforce(client) => {
|
||||
let client = client.clone();
|
||||
match db::salesforce_driver::SfClient::test_connection(&client, timeout).await {
|
||||
Ok(()) => true,
|
||||
Err(e) => {
|
||||
log::warn!("Salesforce connection pool '{key}' is unhealthy: {e}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::HBase(client) => match db::hbase_driver::test_connection(client, timeout).await {
|
||||
Ok(_) => true,
|
||||
Err(e) => {
|
||||
@@ -5796,6 +5889,7 @@ enum KeepaliveTarget {
|
||||
SqlServer(Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>),
|
||||
Elasticsearch(db::elasticsearch_driver::EsClient),
|
||||
Easysearch(db::easysearch_driver::EasysearchClient),
|
||||
Salesforce(db::salesforce_driver::SfClient),
|
||||
Solr(db::solr_driver::SolrClient),
|
||||
HBase(db::hbase_driver::HBaseClient),
|
||||
VectorDb(db::vector_driver::VectorClient),
|
||||
@@ -5898,6 +5992,7 @@ fn keepalive_target_from_pool(pool: &PoolKind, config: &ConnectionConfig) -> Opt
|
||||
PoolKind::SqlServer(client) => Some(KeepaliveTarget::SqlServer(client.clone())),
|
||||
PoolKind::Elasticsearch(client) => Some(KeepaliveTarget::Elasticsearch(client.clone())),
|
||||
PoolKind::Easysearch(client) => Some(KeepaliveTarget::Easysearch(client.clone())),
|
||||
PoolKind::Salesforce(client) => Some(KeepaliveTarget::Salesforce(client.clone())),
|
||||
PoolKind::Solr(client) => Some(KeepaliveTarget::Solr(client.clone())),
|
||||
PoolKind::HBase(client) => Some(KeepaliveTarget::HBase(client.clone())),
|
||||
PoolKind::VectorDb(client) => Some(KeepaliveTarget::VectorDb(client.clone())),
|
||||
@@ -5947,6 +6042,9 @@ async fn ping_keepalive_target(target: &mut KeepaliveTarget, timeout: Duration)
|
||||
KeepaliveTarget::Easysearch(client) => {
|
||||
db::easysearch_driver::test_connection(client, timeout).await.map_err(Into::into)
|
||||
}
|
||||
KeepaliveTarget::Salesforce(client) => {
|
||||
db::salesforce_driver::SfClient::test_connection(client, timeout).await.map_err(Into::into)
|
||||
}
|
||||
KeepaliveTarget::Solr(client) => db::solr_driver::test_connection(client, timeout).await.map_err(Into::into),
|
||||
KeepaliveTarget::HBase(client) => {
|
||||
db::hbase_driver::test_connection(client, timeout).await.map(|_| ()).map_err(Into::into)
|
||||
@@ -6370,6 +6468,7 @@ fn clone_pool_kind(pool: &PoolKind) -> PoolKind {
|
||||
PoolKind::Easysearch(client) => PoolKind::Easysearch(client.clone()),
|
||||
PoolKind::Solr(client) => PoolKind::Solr(client.clone()),
|
||||
PoolKind::Meilisearch(client) => PoolKind::Meilisearch(client.clone()),
|
||||
PoolKind::Salesforce(client) => PoolKind::Salesforce(client.clone()),
|
||||
PoolKind::HBase(client) => PoolKind::HBase(client.clone()),
|
||||
PoolKind::VectorDb(client) => PoolKind::VectorDb(client.clone()),
|
||||
PoolKind::InfluxDb(client) => PoolKind::InfluxDb(client.clone()),
|
||||
@@ -6434,6 +6533,9 @@ async fn close_pool_kind(pool: PoolKind) -> Result<(), String> {
|
||||
PoolKind::Meilisearch(client) => {
|
||||
drop(client);
|
||||
}
|
||||
PoolKind::Salesforce(client) => {
|
||||
drop(client);
|
||||
}
|
||||
PoolKind::HBase(client) => {
|
||||
drop(client);
|
||||
}
|
||||
@@ -6534,6 +6636,7 @@ fn base_pool_key_for_with_catalog(
|
||||
| DatabaseType::Milvus
|
||||
| DatabaseType::Weaviate
|
||||
| DatabaseType::ChromaDb
|
||||
| DatabaseType::Salesforce
|
||||
));
|
||||
is_single && (!database_capabilities::is_agent_type(db_type) || shares_database_pool_with_connection(db_type))
|
||||
});
|
||||
@@ -8997,6 +9100,20 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn salesforce_connections_never_use_a_tcp_probe() {
|
||||
// Salesforce reaches a cloud HTTPS endpoint through one pooled client, so the
|
||||
// manifest sets skipTcpProbe: a raw TCP pre-flight is meaningless even for a
|
||||
// forwarded local endpoint.
|
||||
let mut config = mysql_config(Some("app"));
|
||||
config.db_type = DatabaseType::Salesforce;
|
||||
config.host = "acme.my.salesforce.com".to_string();
|
||||
config.port = 443;
|
||||
|
||||
assert!(!uses_tcp_probe(&config, "acme.my.salesforce.com", 443), "instance url");
|
||||
assert!(!uses_tcp_probe(&config, "127.0.0.1", 54000), "forwarded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn h2_agent_connections_skip_tcp_probe_for_file_and_tcp_modes() {
|
||||
let mut file_config = mysql_config(None);
|
||||
|
||||
@@ -42,6 +42,16 @@ async fn connection_database_type(state: &AppState, connection_id: &str) -> Resu
|
||||
|
||||
pub async fn export_table_data_csv_core(state: &AppState, options: TableCsvExportOptions) -> Result<u64, String> {
|
||||
let database_type = connection_database_type(state, &options.connection_id).await?;
|
||||
// This loop pages with `LIMIT <page_size> OFFSET <n>` and stops at the first
|
||||
// short page. Neither half holds for SOQL: a `/query` response carries at most
|
||||
// 2000 rows and hands the rest back as a QueryLocator (`has_more` +
|
||||
// `session_id`), and OFFSET is capped at 2000, so the first page always looks
|
||||
// short and the export would stop there — a CSV silently missing the rest of
|
||||
// the object. Refuse instead of truncating; a Salesforce export has to follow
|
||||
// the QueryLocator (`fetch_more`) the way the grid's "load more" does.
|
||||
if database_type == DatabaseType::Salesforce {
|
||||
return Err("Exporting a Salesforce object to CSV is not supported yet: SOQL pages through a QueryLocator, not LIMIT/OFFSET. Run a SOQL query and export its result instead.".to_string());
|
||||
}
|
||||
let page_size = options.page_size.unwrap_or(TABLE_DATA_EXPORT_PAGE_SIZE).max(1);
|
||||
let mut writer =
|
||||
BufWriter::new(File::create(&options.file_path).map_err(|err| format!("Failed to write CSV file: {err}"))?);
|
||||
@@ -109,3 +119,59 @@ pub async fn export_table_data_csv_core(state: &AppState, options: TableCsvExpor
|
||||
writer.flush().map_err(|err| err.to_string())?;
|
||||
Ok(rows_exported)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{export_table_data_csv_core, CsvQuoteMode, TableCsvExportOptions};
|
||||
use crate::connection::AppState;
|
||||
use crate::models::connection::{ConnectionConfig, DatabaseType};
|
||||
use crate::storage::Storage;
|
||||
|
||||
fn salesforce_config(id: &str) -> ConnectionConfig {
|
||||
let mut config = serde_json::from_value::<ConnectionConfig>(serde_json::json!({
|
||||
"id": id,
|
||||
"name": "SFDC QA",
|
||||
"db_type": "postgres",
|
||||
"host": "example.my.salesforce.com",
|
||||
"port": 443,
|
||||
"username": "user@example.com",
|
||||
"password": "",
|
||||
"database": ""
|
||||
}))
|
||||
.unwrap();
|
||||
config.db_type = DatabaseType::Salesforce;
|
||||
config
|
||||
}
|
||||
|
||||
/// The refusal has to land before `File::create`, otherwise the user is left
|
||||
/// holding a header-only CSV that looks like an object with no records.
|
||||
#[tokio::test]
|
||||
async fn salesforce_table_csv_export_is_refused_before_a_file_is_created() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let storage = Storage::open(&directory.path().join("storage.db")).await.unwrap();
|
||||
let state = AppState::new(storage);
|
||||
let config = salesforce_config("sfdc-csv");
|
||||
state.configs.write().await.insert(config.id.clone(), config);
|
||||
|
||||
let csv_path = directory.path().join("Account.csv");
|
||||
let error = export_table_data_csv_core(
|
||||
&state,
|
||||
TableCsvExportOptions {
|
||||
file_path: csv_path.to_string_lossy().into_owned(),
|
||||
connection_id: "sfdc-csv".to_string(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
table_name: "Account".to_string(),
|
||||
columns: vec!["Id".to_string(), "Name".to_string()],
|
||||
page_size: None,
|
||||
timeout_secs: None,
|
||||
csv_quote_mode: CsvQuoteMode::default(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect_err("SOQL cannot page an export with LIMIT/OFFSET");
|
||||
|
||||
assert!(error.contains("QueryLocator"), "unexpected error: {error}");
|
||||
assert!(!csv_path.exists(), "a refused export must not leave a truncated CSV behind");
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,7 @@ pub use data::mongodb_dump;
|
||||
pub use data::mongodb_import_export;
|
||||
pub use dbx_drivers::mongo_oidc;
|
||||
pub use dbx_drivers::mongo_shell;
|
||||
pub use dbx_drivers::salesforce_oauth;
|
||||
pub use dbx_formats::export_split_zip;
|
||||
pub use dbx_plugin_runtime::plugins;
|
||||
pub use dbx_sql::db_admin_sql;
|
||||
|
||||
@@ -93,12 +93,30 @@ impl SecretCodec {
|
||||
data_dir: &std::path::Path,
|
||||
allow_create: bool,
|
||||
) -> Result<SecretKeyResolution, String> {
|
||||
if let Some(path) = std::env::var_os("DBX_SECRET_KEY_FILE") {
|
||||
Self::resolve_with_env(policy, data_dir, allow_create, |name| std::env::var_os(name))
|
||||
}
|
||||
|
||||
/// [`Self::resolve`] with the environment lookup injected. `DBX_SECRET_KEY`
|
||||
/// and `DBX_SECRET_KEY_FILE` are process global, so tests drive the explicit
|
||||
/// key branches through here instead of mutating the environment: a mutation
|
||||
/// in flight would silently change the key every other test in the same
|
||||
/// binary resolves, and the tests that encrypt with one key would fail to
|
||||
/// decrypt once it is restored.
|
||||
fn resolve_with_env<F>(
|
||||
policy: SecretKeyPolicy,
|
||||
data_dir: &std::path::Path,
|
||||
allow_create: bool,
|
||||
env_lookup: F,
|
||||
) -> Result<SecretKeyResolution, String>
|
||||
where
|
||||
F: Fn(&str) -> Option<std::ffi::OsString>,
|
||||
{
|
||||
if let Some(path) = env_lookup("DBX_SECRET_KEY_FILE") {
|
||||
let path = std::path::PathBuf::from(path);
|
||||
let codec = Self::read_key_file(&path, false)?;
|
||||
return Ok(SecretKeyResolution { codec, source: SecretKeySource::ExplicitFile });
|
||||
}
|
||||
if let Some(value) = std::env::var_os("DBX_SECRET_KEY") {
|
||||
if let Some(value) = env_lookup("DBX_SECRET_KEY") {
|
||||
let value = value.to_str().ok_or_else(|| "SECRET_KEY_INVALID".to_string())?;
|
||||
let codec = Self::from_key_material(value.trim()).map_err(|_| "SECRET_KEY_INVALID".to_string())?;
|
||||
return Ok(SecretKeyResolution { codec, source: SecretKeySource::ExplicitEnv });
|
||||
@@ -142,7 +160,9 @@ impl SecretCodec {
|
||||
F: FnOnce(bool) -> Result<Option<SecretCodec>, String>,
|
||||
{
|
||||
let compatibility_error = if let Some(path) = compatibility_path.as_ref().filter(|path| path.exists()) {
|
||||
match Self::read_key_file(path, false) {
|
||||
// Another process may have just created this file and not finished
|
||||
// writing it yet; retry briefly before treating it as corrupt.
|
||||
match read_key_file_with_retry(path, false) {
|
||||
Ok(codec) => return Ok(SecretKeyResolution { codec, source: SecretKeySource::ManagedDataDir }),
|
||||
Err(error) => Some(error),
|
||||
}
|
||||
@@ -417,13 +437,9 @@ fn create_key_file(path: &std::path::Path, managed: bool) -> Result<SecretCodec,
|
||||
}
|
||||
match path.symlink_metadata() {
|
||||
Ok(metadata) if managed && metadata.file_type().is_symlink() => Err("KEY_FILE_UNAVAILABLE".to_string()),
|
||||
Ok(_) => {
|
||||
if managed {
|
||||
read_key_file_with_retry(path, managed)
|
||||
} else {
|
||||
SecretCodec::read_key_file(path, managed)
|
||||
}
|
||||
}
|
||||
// The winner of the `create_new` race may still be mid-write, so read
|
||||
// through the same retry loop the managed key path uses.
|
||||
Ok(_) => read_key_file_with_retry(path, managed),
|
||||
Err(_) => Err("KEY_FILE_UNAVAILABLE".to_string()),
|
||||
}
|
||||
}
|
||||
@@ -433,16 +449,24 @@ pub fn managed_key_path(data_dir: &std::path::Path) -> std::path::PathBuf {
|
||||
}
|
||||
|
||||
fn read_key_file_with_retry(path: &std::path::Path, reject_symlink: bool) -> Result<SecretCodec, String> {
|
||||
let mut last_error = "KEY_FILE_UNAVAILABLE".to_string();
|
||||
for attempt in 0..20 {
|
||||
match SecretCodec::read_key_file(path, reject_symlink) {
|
||||
Ok(codec) => return Ok(codec),
|
||||
Err(error) if attempt < 19 && error == "SECRET_KEY_INVALID" && path.metadata().is_ok() => {
|
||||
Err(error) => {
|
||||
let retryable = attempt < 19 && error == "SECRET_KEY_INVALID" && path.metadata().is_ok();
|
||||
last_error = error;
|
||||
if !retryable {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(5));
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
Err("KEY_FILE_UNAVAILABLE".to_string())
|
||||
// Surface what the file actually said: callers tell a corrupt key
|
||||
// (SECRET_KEY_INVALID) apart from an unreadable one, and retrying must not
|
||||
// blur that distinction once the retries are exhausted.
|
||||
Err(last_error)
|
||||
}
|
||||
|
||||
fn default_key_path() -> Option<std::path::PathBuf> {
|
||||
@@ -470,7 +494,7 @@ fn aad(namespace: &str, key: &str) -> String {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{managed_key_path, SecretCodec, SecretKeyPolicy, SecretKeySource};
|
||||
use super::{managed_key_path, read_key_file_with_retry, SecretCodec, SecretKeyPolicy, SecretKeySource};
|
||||
use base64::Engine as _;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
@@ -555,6 +579,46 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compatibility_key_still_being_written_by_a_concurrent_starter_is_retried() {
|
||||
// The shared compatibility path (`~/.config/dbx/secret.key`) is global, so
|
||||
// two starters can race: the `create_new` winner leaves an empty file on
|
||||
// disk for a moment. A reader that gives up immediately reports
|
||||
// SECRET_KEY_INVALID for a key that is fine a millisecond later.
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("secret.key");
|
||||
std::fs::write(&path, "").unwrap();
|
||||
let writer_path = path.clone();
|
||||
let writer = std::thread::spawn(move || {
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
let material = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([7u8; 32]);
|
||||
std::fs::write(&writer_path, material).unwrap();
|
||||
});
|
||||
|
||||
// allow_create=false covers the read-only probe every write goes through;
|
||||
// allow_create=true covers the losing side of the create_new race.
|
||||
for allow_create in [false, true] {
|
||||
let resolved =
|
||||
SecretCodec::resolve_platform_default(Some(path.clone()), allow_create, |_| Ok(None)).unwrap();
|
||||
assert_eq!(resolved.source, SecretKeySource::ManagedDataDir);
|
||||
let expected = SecretCodec::new([7u8; 32]);
|
||||
let envelope = expected.encrypt("n", "k", "value").unwrap();
|
||||
assert_eq!(resolved.codec.decrypt("n", "k", &envelope).unwrap(), "value");
|
||||
}
|
||||
writer.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exhausted_key_file_retries_keep_the_original_error_code() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("secret.key");
|
||||
std::fs::write(&path, "\n").unwrap();
|
||||
// A key file that stays corrupt must not be laundered into the generic
|
||||
// "unavailable" code: the migration preflight reports Invalid vs
|
||||
// Unavailable differently.
|
||||
assert!(matches!(read_key_file_with_retry(&path, false), Err(error) if error == "SECRET_KEY_INVALID"));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn managed_key_symlink_is_rejected() {
|
||||
@@ -576,39 +640,37 @@ mod tests {
|
||||
#[test]
|
||||
fn explicit_key_file_symlink_is_allowed() {
|
||||
use std::os::unix::fs::symlink;
|
||||
let _guard = env_lock().lock().unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let real = dir.path().join("real.key");
|
||||
let link = dir.path().join("external.key");
|
||||
std::fs::write(&real, base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([5u8; 32])).unwrap();
|
||||
symlink(&real, &link).unwrap();
|
||||
|
||||
let previous_file = std::env::var_os("DBX_SECRET_KEY_FILE");
|
||||
let previous_env = std::env::var_os("DBX_SECRET_KEY");
|
||||
std::env::set_var("DBX_SECRET_KEY_FILE", &link);
|
||||
std::env::remove_var("DBX_SECRET_KEY");
|
||||
let resolved = SecretCodec::resolve(SecretKeyPolicy::ManagedDataDir, dir.path(), false).unwrap();
|
||||
let resolved = SecretCodec::resolve_with_env(
|
||||
SecretKeyPolicy::ManagedDataDir,
|
||||
dir.path(),
|
||||
false,
|
||||
explicit_env(&[("DBX_SECRET_KEY_FILE", std::ffi::OsString::from(&link))]),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(resolved.source, SecretKeySource::ExplicitFile);
|
||||
restore_env("DBX_SECRET_KEY_FILE", previous_file);
|
||||
restore_env("DBX_SECRET_KEY", previous_env);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn invalid_non_utf8_explicit_environment_key_does_not_fallback() {
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
let _guard = env_lock().lock().unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let previous_file = std::env::var_os("DBX_SECRET_KEY_FILE");
|
||||
let previous_env = std::env::var_os("DBX_SECRET_KEY");
|
||||
std::env::remove_var("DBX_SECRET_KEY_FILE");
|
||||
std::env::set_var("DBX_SECRET_KEY", std::ffi::OsString::from_vec(vec![0xff, 0xfe]));
|
||||
let invalid = std::ffi::OsString::from_vec(vec![0xff, 0xfe]);
|
||||
assert!(matches!(
|
||||
SecretCodec::resolve(SecretKeyPolicy::ManagedDataDir, dir.path(), true),
|
||||
SecretCodec::resolve_with_env(
|
||||
SecretKeyPolicy::ManagedDataDir,
|
||||
dir.path(),
|
||||
true,
|
||||
explicit_env(&[("DBX_SECRET_KEY", invalid)])
|
||||
),
|
||||
Err(error) if error == "SECRET_KEY_INVALID"
|
||||
));
|
||||
restore_env("DBX_SECRET_KEY_FILE", previous_file);
|
||||
restore_env("DBX_SECRET_KEY", previous_env);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -673,40 +735,38 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn explicit_sources_follow_precedence_without_fallback() {
|
||||
let _guard = env_lock().lock().unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let file = dir.path().join("external.key");
|
||||
let previous_file = std::env::var_os("DBX_SECRET_KEY_FILE");
|
||||
let previous_env = std::env::var("DBX_SECRET_KEY").ok();
|
||||
std::env::remove_var("DBX_SECRET_KEY_FILE");
|
||||
std::env::remove_var("DBX_SECRET_KEY");
|
||||
SecretCodec::resolve(SecretKeyPolicy::ManagedDataDir, dir.path(), true).unwrap();
|
||||
let env_key = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([2u8; 32]);
|
||||
let resolve = |allow_create: bool, entries: &[(&str, std::ffi::OsString)]| {
|
||||
let env = explicit_env(entries);
|
||||
SecretCodec::resolve_with_env(SecretKeyPolicy::ManagedDataDir, dir.path(), allow_create, env)
|
||||
};
|
||||
|
||||
// Nothing explicit is configured, so the managed data-dir key is provisioned.
|
||||
resolve(true, &[]).unwrap();
|
||||
|
||||
// A corrupt explicit file outranks a valid explicit key and must not fall back.
|
||||
std::fs::write(&file, "\n").unwrap();
|
||||
std::env::set_var("DBX_SECRET_KEY_FILE", &file);
|
||||
std::env::set_var("DBX_SECRET_KEY", base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([2u8; 32]));
|
||||
assert!(matches!(
|
||||
SecretCodec::resolve(SecretKeyPolicy::ManagedDataDir, dir.path(), false),
|
||||
Err(error) if error == "SECRET_KEY_INVALID"
|
||||
));
|
||||
let both = [
|
||||
("DBX_SECRET_KEY_FILE", std::ffi::OsString::from(&file)),
|
||||
("DBX_SECRET_KEY", std::ffi::OsString::from(env_key.as_str())),
|
||||
];
|
||||
assert!(matches!(resolve(false, &both), Err(error) if error == "SECRET_KEY_INVALID"));
|
||||
|
||||
std::fs::write(&file, base64::engine::general_purpose::URL_SAFE_NO_PAD.encode([1u8; 32])).unwrap();
|
||||
let resolved = SecretCodec::resolve(SecretKeyPolicy::ManagedDataDir, dir.path(), false).unwrap();
|
||||
assert_eq!(resolved.source, SecretKeySource::ExplicitFile);
|
||||
assert_eq!(resolve(false, &both).unwrap().source, SecretKeySource::ExplicitFile);
|
||||
|
||||
std::env::remove_var("DBX_SECRET_KEY_FILE");
|
||||
let resolved = SecretCodec::resolve(SecretKeyPolicy::ManagedDataDir, dir.path(), false).unwrap();
|
||||
assert_eq!(resolved.source, SecretKeySource::ExplicitEnv);
|
||||
|
||||
restore_env("DBX_SECRET_KEY_FILE", previous_file);
|
||||
restore_env("DBX_SECRET_KEY", previous_env.map(std::ffi::OsString::from));
|
||||
let key_only = [("DBX_SECRET_KEY", std::ffi::OsString::from(env_key.as_str()))];
|
||||
assert_eq!(resolve(false, &key_only).unwrap().source, SecretKeySource::ExplicitEnv);
|
||||
}
|
||||
|
||||
fn restore_env(name: &str, value: Option<std::ffi::OsString>) {
|
||||
if let Some(value) = value {
|
||||
std::env::set_var(name, value);
|
||||
} else {
|
||||
std::env::remove_var(name);
|
||||
}
|
||||
/// Environment lookup for [`SecretCodec::resolve_with_env`] that never
|
||||
/// touches the process environment, so these tests cannot change the key
|
||||
/// another test in the same binary resolves while they run.
|
||||
fn explicit_env<'a>(
|
||||
entries: &'a [(&'a str, std::ffi::OsString)],
|
||||
) -> impl Fn(&str) -> Option<std::ffi::OsString> + 'a {
|
||||
move |name| entries.iter().find(|(key, _)| *key == name).map(|(_, value)| value.clone())
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,8 @@ use crate::connection_secrets::{
|
||||
CASSANDRA_TRUSTSTORE_PASSWORD_KEY, MQTT_AUTH_PASSWORD_KEY, MQTT_AUTH_SECRET_PREFIX, MQ_AUTH_API_KEY_VALUE_KEY,
|
||||
MQ_AUTH_CLIENT_SECRET_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_SECRET_PREFIX, MQ_AUTH_TOKEN_KEY, MQ_TOKEN_SIGNING_KEY,
|
||||
MQ_TOKEN_SIGNING_SECRET_PREFIX, NACOS_AUTH_PASSWORD_KEY, NACOS_AUTH_SECRET_PREFIX,
|
||||
NACOS_RNACOS_CONSOLE_PASSWORD_KEY, PLUGIN_CONNECTION_SECRET_PREFIX,
|
||||
NACOS_RNACOS_CONSOLE_PASSWORD_KEY, PLUGIN_CONNECTION_SECRET_PREFIX, SALESFORCE_AUTH_CLIENT_SECRET_KEY,
|
||||
SALESFORCE_AUTH_PASSWORD_KEY, SALESFORCE_AUTH_REFRESH_TOKEN_KEY, SALESFORCE_AUTH_SECRET_PREFIX,
|
||||
};
|
||||
use crate::db::sqlite::{connect_path_create_if_missing, SqliteHandle};
|
||||
use crate::history::{
|
||||
@@ -471,6 +472,15 @@ pub struct McpConnectionPolicy {
|
||||
/// connection default, while a present entry takes priority over it.
|
||||
#[serde(default)]
|
||||
pub database_policies: Vec<McpDatabasePolicy>,
|
||||
/// Opt-in switch letting an AI agent write to this Salesforce org through
|
||||
/// MCP. Absent on every policy saved before the switch existed, and `false`
|
||||
/// by default: SOQL reads need no permission beyond the execution mode, but
|
||||
/// Salesforce DML has no transaction and no rollback, so an agent may only
|
||||
/// reach it after a person turns this on for the connection and confirms
|
||||
/// each write (`dbx_salesforce_prepare_write` → `dbx_salesforce_apply_write`).
|
||||
/// Forced back to false whenever `read_only` is set.
|
||||
#[serde(default)]
|
||||
pub allow_salesforce_dml: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -646,6 +656,9 @@ impl McpGlobalPolicy {
|
||||
current.allowed_databases = databases;
|
||||
current.database_policies =
|
||||
merge_mcp_database_policies(¤t.database_policies, &rule.database_policies);
|
||||
// Same conjunction as high-risk SQL: every duplicate rule has
|
||||
// to opt in before an agent may write to the org.
|
||||
current.allow_salesforce_dml &= rule.allow_salesforce_dml;
|
||||
})
|
||||
.or_insert_with(|| McpConnectionPolicy {
|
||||
connection_id: connection_id.to_string(),
|
||||
@@ -656,6 +669,7 @@ impl McpGlobalPolicy {
|
||||
database_scope: rule.database_scope,
|
||||
allowed_databases: normalize_mcp_database_names(&rule.allowed_databases),
|
||||
database_policies: normalize_mcp_database_policies(&rule.database_policies),
|
||||
allow_salesforce_dml: rule.allow_salesforce_dml,
|
||||
});
|
||||
}
|
||||
let mut connection_policies = policies.into_values().collect::<Vec<_>>();
|
||||
@@ -663,6 +677,9 @@ impl McpGlobalPolicy {
|
||||
for rule in &mut connection_policies {
|
||||
if rule.read_only {
|
||||
rule.allow_dangerous_sql = false;
|
||||
// A read-only connection cannot carry a Salesforce write opt-in,
|
||||
// however the saved rules were merged.
|
||||
rule.allow_salesforce_dml = false;
|
||||
}
|
||||
rule.allowed_databases = normalize_mcp_database_names(&rule.allowed_databases);
|
||||
if rule.database_scope != McpDatabaseScope::Selected {
|
||||
@@ -2947,6 +2964,18 @@ fn scrub_cassandra_tls_secrets(config: &mut ConnectionConfig) {
|
||||
scrub_json_secret(tls, "keystore_password");
|
||||
}
|
||||
|
||||
fn scrub_salesforce_auth_secrets(config: &mut ConnectionConfig) {
|
||||
if config.db_type != DatabaseType::Salesforce {
|
||||
return;
|
||||
}
|
||||
let Some(auth) = salesforce_auth_object_mut(config.external_config.as_mut()) else {
|
||||
return;
|
||||
};
|
||||
scrub_json_secret(auth, "clientSecret");
|
||||
scrub_json_secret(auth, "refreshToken");
|
||||
scrub_json_secret(auth, "password");
|
||||
}
|
||||
|
||||
fn delete_secret_prefix_in_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
connection_id: &str,
|
||||
@@ -5209,6 +5238,7 @@ fn sanitized_connection_config(config: &ConnectionConfig) -> ConnectionConfig {
|
||||
scrub_mq_token_signing_secret(&mut sanitized);
|
||||
scrub_nacos_auth_secrets(&mut sanitized);
|
||||
scrub_cassandra_tls_secrets(&mut sanitized);
|
||||
scrub_salesforce_auth_secrets(&mut sanitized);
|
||||
sanitized.connection_secrets.clear();
|
||||
sanitized
|
||||
}
|
||||
@@ -5306,6 +5336,7 @@ fn persist_connection_in_tx(
|
||||
persist_mq_token_signing_secret_in_tx(tx, codec, &config)?;
|
||||
persist_nacos_auth_secrets_in_tx(tx, codec, &config)?;
|
||||
persist_cassandra_tls_secrets_in_tx(tx, codec, &config)?;
|
||||
persist_salesforce_auth_secrets_in_tx(tx, codec, &config)?;
|
||||
delete_secret_prefix_in_tx(tx, &config.id, PLUGIN_CONNECTION_SECRET_PREFIX)?;
|
||||
for (key, secret) in &config.connection_secrets {
|
||||
if !key.is_empty() {
|
||||
@@ -5576,6 +5607,7 @@ impl Storage {
|
||||
scrub_mq_token_signing_secret(&mut sanitized);
|
||||
scrub_nacos_auth_secrets(&mut sanitized);
|
||||
scrub_cassandra_tls_secrets(&mut sanitized);
|
||||
scrub_salesforce_auth_secrets(&mut sanitized);
|
||||
sanitized.connection_secrets.clear();
|
||||
let json = serde_json::to_string(&sanitized).map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -5964,6 +5996,7 @@ impl Storage {
|
||||
let needs_mq_token_signing_rewrite = self.hydrate_mq_token_signing_secret(&id, &mut config).await?;
|
||||
let needs_nacos_auth_rewrite = self.hydrate_nacos_auth_secret(&id, &mut config).await?;
|
||||
let needs_cassandra_tls_rewrite = self.hydrate_cassandra_tls_secrets(&id, &mut config).await?;
|
||||
let needs_salesforce_auth_rewrite = self.hydrate_salesforce_auth_secrets(&id, &mut config).await?;
|
||||
let mut needs_plugin_secret_rewrite = false;
|
||||
let plugin_secret_keys = config.connection_secrets.keys().cloned().collect::<Vec<_>>();
|
||||
for key in plugin_secret_keys {
|
||||
@@ -5983,6 +6016,7 @@ impl Storage {
|
||||
|| needs_mq_token_signing_rewrite
|
||||
|| needs_nacos_auth_rewrite
|
||||
|| needs_cassandra_tls_rewrite
|
||||
|| needs_salesforce_auth_rewrite
|
||||
|| needs_plugin_secret_rewrite;
|
||||
if needs_external_secret_rewrite {
|
||||
// `config` is hydrated above, so always use the canonical
|
||||
@@ -6140,6 +6174,28 @@ impl Storage {
|
||||
.await?;
|
||||
Ok(truststore_rewrite || keystore_rewrite)
|
||||
}
|
||||
|
||||
async fn hydrate_salesforce_auth_secrets(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
config: &mut ConnectionConfig,
|
||||
) -> Result<bool, String> {
|
||||
if config.db_type != DatabaseType::Salesforce {
|
||||
return Ok(false);
|
||||
}
|
||||
let Some(auth) = salesforce_auth_object_mut(config.external_config.as_mut()) else {
|
||||
return Ok(false);
|
||||
};
|
||||
let client_secret_rewrite =
|
||||
hydrate_mq_json_secret(self, connection_id, SALESFORCE_AUTH_CLIENT_SECRET_KEY, auth, "clientSecret")
|
||||
.await?;
|
||||
let refresh_token_rewrite =
|
||||
hydrate_mq_json_secret(self, connection_id, SALESFORCE_AUTH_REFRESH_TOKEN_KEY, auth, "refreshToken")
|
||||
.await?;
|
||||
let password_rewrite =
|
||||
hydrate_mq_json_secret(self, connection_id, SALESFORCE_AUTH_PASSWORD_KEY, auth, "password").await?;
|
||||
Ok(client_secret_rewrite || refresh_token_rewrite || password_rewrite)
|
||||
}
|
||||
}
|
||||
|
||||
// Saved SQL
|
||||
@@ -7862,6 +7918,60 @@ fn persist_cassandra_tls_secrets_in_tx(
|
||||
)
|
||||
}
|
||||
|
||||
fn persist_salesforce_auth_secrets_in_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
codec: &SecretCodec,
|
||||
config: &ConnectionConfig,
|
||||
) -> Result<(), String> {
|
||||
if config.db_type != DatabaseType::Salesforce {
|
||||
delete_secret_prefix_in_tx(tx, &config.id, SALESFORCE_AUTH_SECRET_PREFIX)?;
|
||||
return Ok(());
|
||||
}
|
||||
let Some(auth) = salesforce_auth_object(config.external_config.as_ref()) else {
|
||||
delete_secret_prefix_in_tx(tx, &config.id, SALESFORCE_AUTH_SECRET_PREFIX)?;
|
||||
return Ok(());
|
||||
};
|
||||
replace_salesforce_auth_secret_in_tx(
|
||||
tx,
|
||||
codec,
|
||||
&config.id,
|
||||
SALESFORCE_AUTH_CLIENT_SECRET_KEY,
|
||||
auth,
|
||||
"clientSecret",
|
||||
)?;
|
||||
replace_salesforce_auth_secret_in_tx(
|
||||
tx,
|
||||
codec,
|
||||
&config.id,
|
||||
SALESFORCE_AUTH_REFRESH_TOKEN_KEY,
|
||||
auth,
|
||||
"refreshToken",
|
||||
)?;
|
||||
replace_salesforce_auth_secret_in_tx(tx, codec, &config.id, SALESFORCE_AUTH_PASSWORD_KEY, auth, "password")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn replace_salesforce_auth_secret_in_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
codec: &SecretCodec,
|
||||
connection_id: &str,
|
||||
key: &str,
|
||||
auth: &serde_json::Map<String, serde_json::Value>,
|
||||
field: &str,
|
||||
) -> Result<(), String> {
|
||||
let current = auth.get(field).and_then(serde_json::Value::as_str).filter(|secret| !secret.is_empty());
|
||||
let existing = if current.is_none() { get_secret_in_tx(tx, codec, connection_id, key)? } else { None };
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id = ?1 AND key = ?2", params![connection_id, key])
|
||||
.map_err(|e| e.to_string())?;
|
||||
match current {
|
||||
Some(secret) => persist_secret_in_tx(tx, codec, connection_id, key, secret),
|
||||
None => match existing {
|
||||
Some(secret) => persist_secret_in_tx(tx, codec, connection_id, key, &secret),
|
||||
None => Ok(()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn persist_json_secret_if_present_in_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
codec: &SecretCodec,
|
||||
@@ -7934,6 +8044,16 @@ fn cassandra_tls_object_mut(
|
||||
value?.get_mut("tls")?.as_object_mut()
|
||||
}
|
||||
|
||||
fn salesforce_auth_object(value: Option<&serde_json::Value>) -> Option<&serde_json::Map<String, serde_json::Value>> {
|
||||
value?.get("auth")?.as_object()
|
||||
}
|
||||
|
||||
fn salesforce_auth_object_mut(
|
||||
value: Option<&mut serde_json::Value>,
|
||||
) -> Option<&mut serde_json::Map<String, serde_json::Value>> {
|
||||
value?.get_mut("auth")?.as_object_mut()
|
||||
}
|
||||
|
||||
fn nacos_auth_object(value: Option<&serde_json::Value>) -> Option<&serde_json::Map<String, serde_json::Value>> {
|
||||
value?.get("auth")?.as_object()
|
||||
}
|
||||
|
||||
@@ -2078,6 +2078,23 @@ async fn do_execute_typed(
|
||||
}
|
||||
result
|
||||
}
|
||||
PoolKind::Salesforce(client) => {
|
||||
let client = client.clone();
|
||||
let sql = sql.to_string();
|
||||
let max_rows = options.max_rows;
|
||||
let result = wait_for_query_opt(cancel_token, query_timeout, async move {
|
||||
if let Some(cursor) = options.result_session_id.as_deref() {
|
||||
client.fetch_more(cursor).await
|
||||
} else {
|
||||
client.execute_query(&sql, max_rows).await
|
||||
}
|
||||
})
|
||||
.await;
|
||||
if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
|
||||
state.remove_pool_by_key(pool_key).await;
|
||||
}
|
||||
result
|
||||
}
|
||||
PoolKind::VectorDb(client) => {
|
||||
let client = client.clone();
|
||||
let sql = sql.to_string();
|
||||
@@ -4704,6 +4721,7 @@ fn pool_kind_has_transactional_path(pool: &PoolKind) -> bool {
|
||||
| PoolKind::Easysearch(_)
|
||||
| PoolKind::Solr(_)
|
||||
| PoolKind::Meilisearch(_)
|
||||
| PoolKind::Salesforce(_)
|
||||
| PoolKind::VectorDb(_)
|
||||
| PoolKind::InfluxDb(_)
|
||||
| PoolKind::InfluxDb3(_)
|
||||
@@ -5108,6 +5126,7 @@ fn batch_transaction_path(pool: &PoolKind) -> BatchTransactionPath {
|
||||
| PoolKind::Easysearch(_)
|
||||
| PoolKind::Solr(_)
|
||||
| PoolKind::Meilisearch(_)
|
||||
| PoolKind::Salesforce(_)
|
||||
| PoolKind::VectorDb(_)
|
||||
| PoolKind::InfluxDb(_)
|
||||
| PoolKind::InfluxDb3(_)
|
||||
|
||||
@@ -676,6 +676,12 @@ async fn list_databases_once(state: &AppState, connection_id: &str) -> Result<Ve
|
||||
if let Some(client) = extract_pool!(pool_handle.as_ref(), VictoriaMetrics) {
|
||||
return db::victoriametrics_driver::list_databases(&client).await;
|
||||
}
|
||||
if let Some(client) = extract_pool!(pool_handle.as_ref(), Salesforce) {
|
||||
// singleDatabase trait: the whole org is one synthesized database
|
||||
// node; sObjects are listed as its tables.
|
||||
let name = client.org_display_name().await;
|
||||
return Ok(vec![db::DatabaseInfo { name, ..Default::default() }]);
|
||||
}
|
||||
try_sqlserver!(pool_handle, list_databases);
|
||||
if let Some(client) = extract_pool!(pool_handle.as_ref(), Agent) {
|
||||
let is_mongo = db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::MongoDb);
|
||||
@@ -2683,6 +2689,10 @@ async fn list_tables_once(
|
||||
.await
|
||||
.map(|names| collection_names_to_tables(names, "INDEX"))
|
||||
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)),
|
||||
PoolKind::Salesforce(client) => db::salesforce_driver::SfClient::list_tables(client)
|
||||
.await
|
||||
.map(|names| collection_names_to_tables(names, "SOBJECT"))
|
||||
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)),
|
||||
PoolKind::HBase(client) => db::hbase_driver::list_tables(client, database)
|
||||
.await
|
||||
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)),
|
||||
@@ -7456,6 +7466,9 @@ async fn get_columns_core_for_session_inner_with_pool(
|
||||
PoolKind::Meilisearch(client) => {
|
||||
db::meilisearch_driver::get_columns(client, table).await.map(deduplicate_column_infos)
|
||||
}
|
||||
PoolKind::Salesforce(client) => {
|
||||
db::salesforce_driver::SfClient::get_columns(client, table).await.map(deduplicate_column_infos)
|
||||
}
|
||||
PoolKind::HBase(client) => {
|
||||
db::hbase_driver::get_columns(client, database, table).await.map(deduplicate_column_infos)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user