From 2a2915b543d16a62296e12f549096da7f4981731 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Tue, 28 Oct 2025 14:20:55 -0300 Subject: [PATCH] - Migration to Open ADAL. --- fix-errors.sh | 8 +++++--- src/basic/keywords/get.rs | 32 +++++++------------------------- src/bootstrap/mod.rs | 29 ++++++++++++----------------- src/drive_monitor/mod.rs | 10 ++++------ src/file/mod.rs | 5 +---- src/kb/minio_handler.rs | 7 +++---- src/kb/mod.rs | 5 ++--- 7 files changed, 34 insertions(+), 62 deletions(-) diff --git a/fix-errors.sh b/fix-errors.sh index 52e53ef44..b003f0a63 100755 --- a/fix-errors.sh +++ b/fix-errors.sh @@ -19,8 +19,8 @@ done dirs=( # "auth" - "automation" - "basic" + # "automation" + #"basic" # "bot" "bootstrap" # "package_manager" @@ -37,7 +37,7 @@ dirs=( "file" "kb" "shared" - "tests" + #"tests" # "tools" # "web_automation" # "whatsapp" @@ -54,6 +54,8 @@ done echo "$PROJECT_ROOT/src/main.rs" >> "$OUTPUT_FILE" cat "$PROJECT_ROOT/src/main.rs" >> "$OUTPUT_FILE" +echo "$PROJECT_ROOT/src/basic/keywords/get.rs" >> "$OUTPUT_FILE" +cat "$PROJECT_ROOT/src/basic/keywords/get.rs" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" echo "Compiling..." diff --git a/src/basic/keywords/get.rs b/src/basic/keywords/get.rs index 5b81b641c..089c75f76 100644 --- a/src/basic/keywords/get.rs +++ b/src/basic/keywords/get.rs @@ -28,7 +28,6 @@ pub fn get_keyword(state: Arc, _user: UserSession, engine: &mut Engine let state_for_blocking = Arc::clone(&state_clone); let url_for_blocking = url_str.clone(); - // ---- fixed section: spawn on separate thread runtime ---- let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { let rt = tokio::runtime::Builder::new_multi_thread() @@ -76,7 +75,6 @@ pub fn get_keyword(state: Arc, _user: UserSession, engine: &mut Engine .unwrap(); } -/// Enhanced security check for path traversal and unsafe paths fn is_safe_path(path: &str) -> bool { if path.starts_with("https://") || path.starts_with("http://") { return true; @@ -189,38 +187,22 @@ pub async fn get_from_bucket( bucket }; - -let get_object_future = s3_operator - .read(&bucket_name) - .key(file_path) - .send(); - - let response = match tokio::time::timeout(Duration::from_secs(30), get_object_future).await { + let response = match tokio::time::timeout( + Duration::from_secs(30), + s3_operator.read(&format!("{}/{}", bucket_name, file_path)) + ).await { Ok(Ok(response)) => response, Ok(Err(e)) => { - error!("S3 get_object failed: {}", e); + error!("S3 read failed: {}", e); return Err(format!("S3 operation failed: {}", e).into()); } Err(_) => { - error!("S3 get_object timed out"); + error!("S3 read timed out"); return Err("S3 operation timed out".into()); } }; - let body_future = response.body.collect(); - let data = match tokio::time::timeout(Duration::from_secs(30), body_future).await { - Ok(Ok(data)) => data, - Ok(Err(e)) => { - error!("Failed to collect S3 response body: {}", e); - return Err(format!("Failed to read S3 response: {}", e).into()); - } - Err(_) => { - error!("Timeout collecting S3 response body"); - return Err("Timeout reading S3 response body".into()); - } - }; - - let bytes = data.into_bytes().to_vec(); + let bytes = response.to_vec(); debug!( "Retrieved {} bytes from S3 for key: {}", bytes.len(), diff --git a/src/bootstrap/mod.rs b/src/bootstrap/mod.rs index 74699064c..25ea6fe03 100644 --- a/src/bootstrap/mod.rs +++ b/src/bootstrap/mod.rs @@ -1,14 +1,15 @@ use crate::config::AppConfig; use crate::package_manager::{InstallMode, PackageManager}; +use actix_web::http::uri::Builder; use anyhow::Result; -use csv; use diesel::connection::SimpleConnection; use diesel::Connection; use diesel::RunQueryDsl; use dotenvy::dotenv; -use log::{error, info, trace}; +use log::{error, info}; use opendal::services::S3; -use opendal::{Operator, OperatorBuilder}; +use opendal::Operator; +use rand::Rng; use rand::distr::Alphanumeric; use sha2::{Digest, Sha256}; use std::io::{self, Write}; @@ -278,7 +279,6 @@ impl BootstrapManager { } fn generate_secure_password(&self, length: usize) -> String { - use rand::Rng; let mut rng = rand::rng(); std::iter::repeat_with(|| rng.sample(Alphanumeric) as char) .take(length) @@ -315,18 +315,13 @@ impl BootstrapManager { let mut conn = diesel::PgConnection::establish(&database_url)?; self.create_bots_from_templates(&mut conn)?; - let builder = S3::default(); - builder - .root("/") - .endpoint(&config.minio.server) - .access_key_id(&config.minio.access_key) - .secret_access_key(&config.minio.secret_key); - - // if !config.minio.use_ssl { - // builder.disable_ssl_verification(true); - // } - - let client = Operator::new(builder)?.finish(); + let client = Operator::new( + S3::default() + .root("/") + .endpoint(&config.minio.server) + .access_key_id(&config.minio.access_key) + .secret_access_key(&config.minio.secret_key) + )?.finish(); let templates_dir = Path::new("templates"); if !templates_dir.exists() { @@ -391,7 +386,7 @@ impl BootstrapManager { .bind::(format!("Bot for {} template", bot_name)) .execute(conn)?; } else { - trace!("Bot {} already exists", formatted_name); + log::trace!("Bot {} already exists", formatted_name); } } } diff --git a/src/drive_monitor/mod.rs b/src/drive_monitor/mod.rs index a78754788..b617eac7c 100644 --- a/src/drive_monitor/mod.rs +++ b/src/drive_monitor/mod.rs @@ -72,15 +72,14 @@ impl DriveMonitor { let mut current_files = HashMap::new(); let mut lister = op.lister_with(prefix).recursive(true).await?; - while let Some(entry) = lister.next().await { - let entry = entry?; + while let Some(entry) = lister.try_next().await? { let path = entry.path().to_string(); if path.ends_with('/') || !path.ends_with(".bas") { continue; } - let meta = entry.metadata().await?; + let meta = op.stat(&path).await?; let file_state = FileState { path: path.clone(), size: meta.content_length() as i64, @@ -133,8 +132,7 @@ impl DriveMonitor { let mut current_files = HashMap::new(); let mut lister = op.lister_with(prefix).recursive(true).await?; - while let Some(entry) = lister.next().await { - let entry = entry?; + while let Some(entry) = lister.try_next().await? { let path = entry.path().to_string(); if path.ends_with('/') { @@ -146,7 +144,7 @@ impl DriveMonitor { continue; } - let meta = entry.metadata().await?; + let meta = op.stat(&path).await?; let file_state = FileState { path: path.clone(), size: meta.content_length() as i64, diff --git a/src/file/mod.rs b/src/file/mod.rs index c3b247045..ae28856c5 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -1,7 +1,6 @@ use actix_multipart::Multipart; use actix_web::web; use actix_web::{post, HttpResponse}; -use log::{error, info}; use opendal::Operator; use std::io::Write; use tempfile::NamedTempFile; @@ -76,14 +75,12 @@ pub async fn init_drive(cfg: &DriveConfig) -> Result Result<(), opendal::Error> { +) -> Result<(), Box> { let data = std::fs::read(file_path)?; op.write(key, data).await?; Ok(()) diff --git a/src/kb/minio_handler.rs b/src/kb/minio_handler.rs index 4ac206ba9..deedb907b 100644 --- a/src/kb/minio_handler.rs +++ b/src/kb/minio_handler.rs @@ -85,18 +85,17 @@ impl MinIOHandler { let mut current_files = HashMap::new(); let mut lister = op.lister_with(prefix).recursive(true).await?; - while let Some(entry) = lister.next().await { - let entry = entry?; + while let Some(entry) = lister.try_next().await? { let path = entry.path().to_string(); if path.ends_with('/') { continue; } - let meta = entry.metadata().await?; + let meta = op.stat(&path).await?; let file_state = FileState { path: path.clone(), - size: meta.content_length().parse::().unwrap_or(0), + size: meta.content_length() as i64, etag: meta.etag().unwrap_or_default().to_string(), last_modified: meta.last_modified().map(|dt| dt.to_rfc3339()), }; diff --git a/src/kb/mod.rs b/src/kb/mod.rs index 9888d5c72..62af851a2 100644 --- a/src/kb/mod.rs +++ b/src/kb/mod.rs @@ -104,15 +104,14 @@ impl KBManager { }; let mut lister = op.lister_with(&collection.folder_path).recursive(true).await?; - while let Some(entry) = lister.next().await { - let entry = entry?; + while let Some(entry) = lister.try_next().await? { let path = entry.path().to_string(); if path.ends_with('/') { continue; } - let meta = entry.metadata().await?; + let meta = op.stat(&path).await?; if let Err(e) = self .process_file( &collection,