repo
stringlengths
6
65
file_url
stringlengths
81
311
file_path
stringlengths
6
227
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:31:58
2026-01-04 20:25:31
truncated
bool
2 classes
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/tier_last_day_stats.rs
crates/ecstore/src/bucket/lifecycle/tier_last_day_stats.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use sha2::Sha256; use std::collections::HashMap; use std::ops::Sub; use time::OffsetDateTime; use tracing::{error, warn}; use rustfs_common::data_usage::TierStats; pub type DailyAllTierStats = HashMap<String, LastDayTierStats>; #[derive(Clone)] pub struct LastDayTierStats { bins: [TierStats; 24], updated_at: OffsetDateTime, } impl Default for LastDayTierStats { fn default() -> Self { Self { bins: Default::default(), updated_at: OffsetDateTime::now_utc(), } } } impl LastDayTierStats { pub fn add_stats(&mut self, ts: TierStats) { let mut now = OffsetDateTime::now_utc(); self.forward_to(&mut now); let now_idx = now.hour() as usize; self.bins[now_idx] = self.bins[now_idx].add(&ts); } fn forward_to(&mut self, t: &mut OffsetDateTime) { if t.unix_timestamp() == 0 { *t = OffsetDateTime::now_utc(); } let since = t.sub(self.updated_at).whole_hours(); if since < 1 { return; } let (idx, mut last_idx) = (t.hour(), self.updated_at.hour()); self.updated_at = *t; if since >= 24 { self.bins = [TierStats::default(); 24]; return; } while last_idx != idx { last_idx = (last_idx + 1) % 24; self.bins[last_idx as usize] = TierStats::default(); } } #[allow(dead_code)] fn merge(&self, m: LastDayTierStats) -> LastDayTierStats { let mut cl = self.clone(); let mut cm = m.clone(); let mut merged = LastDayTierStats::default(); if cl.updated_at.unix_timestamp() > cm.updated_at.unix_timestamp() { cm.forward_to(&mut cl.updated_at); merged.updated_at = cl.updated_at; } else { cl.forward_to(&mut cm.updated_at); merged.updated_at = cm.updated_at; } for (i, _) in cl.bins.iter().enumerate() { merged.bins[i] = cl.bins[i].add(&cm.bins[i]); } merged } } #[cfg(test)] mod test {}
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/bucket_lifecycle_ops.rs
crates/ecstore/src/bucket/lifecycle/bucket_lifecycle_ops.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::bucket::lifecycle::bucket_lifecycle_audit::{LcAuditEvent, LcEventSrc}; use crate::bucket::lifecycle::lifecycle::{self, ExpirationOptions, Lifecycle, TransitionOptions}; use crate::bucket::lifecycle::tier_last_day_stats::{DailyAllTierStats, LastDayTierStats}; use crate::bucket::lifecycle::tier_sweeper::{Jentry, delete_object_from_remote_tier}; use crate::bucket::object_lock::objectlock_sys::enforce_retention_for_deletion; use crate::bucket::{metadata_sys::get_lifecycle_config, versioning_sys::BucketVersioningSys}; use crate::client::object_api_utils::new_getobjectreader; use crate::error::Error; use crate::error::StorageError; use crate::error::{error_resp_to_object_err, is_err_object_not_found, is_err_version_not_found, is_network_or_host_down}; use crate::event::name::EventName; use crate::event_notification::{EventArgs, send_event}; use crate::global::GLOBAL_LocalNodeName; use crate::global::{GLOBAL_LifecycleSys, GLOBAL_TierConfigMgr, get_global_deployment_id}; use crate::store::ECStore; use crate::store_api::StorageAPI; use crate::store_api::{GetObjectReader, HTTPRangeSpec, ObjectInfo, ObjectOptions, ObjectToDelete}; use crate::tier::warm_backend::WarmBackendGetOpts; use async_channel::{Receiver as A_Receiver, Sender as A_Sender, bounded}; use bytes::BytesMut; use futures::Future; use http::HeaderMap; use lazy_static::lazy_static; use rustfs_common::data_usage::TierStats; use rustfs_common::heal_channel::rep_has_active_rules; use rustfs_common::metrics::{IlmAction, Metrics}; use rustfs_filemeta::{NULL_VERSION_ID, RestoreStatusOps, is_restored_object_on_disk}; use rustfs_utils::path::encode_dir_object; use rustfs_utils::string::strings_has_prefix_fold; use s3s::Body; use s3s::dto::{ BucketLifecycleConfiguration, DefaultRetention, ReplicationConfiguration, RestoreRequest, RestoreRequestType, RestoreStatus, ServerSideEncryption, Timestamp, }; use s3s::header::{X_AMZ_RESTORE, X_AMZ_SERVER_SIDE_ENCRYPTION, X_AMZ_STORAGE_CLASS}; use sha2::{Digest, Sha256}; use std::any::Any; use std::collections::HashMap; use std::env; use std::io::Write; use std::pin::Pin; use std::sync::atomic::{AtomicI64, Ordering}; use std::sync::{Arc, Mutex}; use time::OffsetDateTime; use tokio::select; use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::{RwLock, mpsc}; use tracing::{debug, error, info}; use uuid::Uuid; use xxhash_rust::xxh64; pub type TimeFn = Arc<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static>; pub type TraceFn = Arc<dyn Fn(String, HashMap<String, String>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static>; pub type ExpiryOpType = Box<dyn ExpiryOp + Send + Sync + 'static>; static XXHASH_SEED: u64 = 0; pub const AMZ_OBJECT_TAGGING: &str = "X-Amz-Tagging"; pub const AMZ_TAG_COUNT: &str = "x-amz-tagging-count"; pub const AMZ_TAG_DIRECTIVE: &str = "X-Amz-Tagging-Directive"; pub const AMZ_ENCRYPTION_AES: &str = "AES256"; pub const AMZ_ENCRYPTION_KMS: &str = "aws:kms"; pub const ERR_INVALID_STORAGECLASS: &str = "invalid tier."; lazy_static! { pub static ref GLOBAL_ExpiryState: Arc<RwLock<ExpiryState>> = ExpiryState::new(); pub static ref GLOBAL_TransitionState: Arc<TransitionState> = TransitionState::new(); } pub struct LifecycleSys; impl LifecycleSys { pub fn new() -> Arc<Self> { Arc::new(Self) } pub async fn get(&self, bucket: &str) -> Option<BucketLifecycleConfiguration> { let lc = get_lifecycle_config(bucket).await.expect("get_lifecycle_config err!").0; Some(lc) } pub fn trace(_oi: &ObjectInfo) -> TraceFn { todo!(); } } struct ExpiryTask { obj_info: ObjectInfo, event: lifecycle::Event, src: LcEventSrc, } impl ExpiryOp for ExpiryTask { fn op_hash(&self) -> u64 { let mut hasher = Sha256::new(); hasher.update(format!("{}", self.obj_info.bucket).as_bytes()); hasher.update(format!("{}", self.obj_info.name).as_bytes()); xxh64::xxh64(hasher.finalize().as_slice(), XXHASH_SEED) } fn as_any(&self) -> &dyn Any { self } } struct ExpiryStats { missed_expiry_tasks: AtomicI64, missed_freevers_tasks: AtomicI64, missed_tier_journal_tasks: AtomicI64, workers: AtomicI64, } #[allow(dead_code)] impl ExpiryStats { pub fn missed_tasks(&self) -> i64 { self.missed_expiry_tasks.load(Ordering::SeqCst) } fn missed_free_vers_tasks(&self) -> i64 { self.missed_freevers_tasks.load(Ordering::SeqCst) } fn missed_tier_journal_tasks(&self) -> i64 { self.missed_tier_journal_tasks.load(Ordering::SeqCst) } fn num_workers(&self) -> i64 { self.workers.load(Ordering::SeqCst) } } pub trait ExpiryOp: 'static { fn op_hash(&self) -> u64; fn as_any(&self) -> &dyn Any; } #[derive(Debug, Default, Clone)] pub struct TransitionedObject { pub name: String, pub version_id: String, pub tier: String, pub free_version: bool, pub status: String, } struct FreeVersionTask(ObjectInfo); impl ExpiryOp for FreeVersionTask { fn op_hash(&self) -> u64 { let mut hasher = Sha256::new(); hasher.update(format!("{}", self.0.transitioned_object.tier).as_bytes()); hasher.update(format!("{}", self.0.transitioned_object.name).as_bytes()); xxh64::xxh64(hasher.finalize().as_slice(), XXHASH_SEED) } fn as_any(&self) -> &dyn Any { self } } struct NewerNoncurrentTask { bucket: String, versions: Vec<ObjectToDelete>, event: lifecycle::Event, } impl ExpiryOp for NewerNoncurrentTask { fn op_hash(&self) -> u64 { let mut hasher = Sha256::new(); hasher.update(format!("{}", self.bucket).as_bytes()); hasher.update(format!("{}", self.versions[0].object_name).as_bytes()); xxh64::xxh64(hasher.finalize().as_slice(), XXHASH_SEED) } fn as_any(&self) -> &dyn Any { self } } pub struct ExpiryState { tasks_tx: Vec<Sender<Option<ExpiryOpType>>>, tasks_rx: Vec<Arc<tokio::sync::Mutex<Receiver<Option<ExpiryOpType>>>>>, stats: Option<ExpiryStats>, } impl ExpiryState { #[allow(clippy::new_ret_no_self)] pub fn new() -> Arc<RwLock<Self>> { Arc::new(RwLock::new(Self { tasks_tx: vec![], tasks_rx: vec![], stats: Some(ExpiryStats { missed_expiry_tasks: AtomicI64::new(0), missed_freevers_tasks: AtomicI64::new(0), missed_tier_journal_tasks: AtomicI64::new(0), workers: AtomicI64::new(0), }), })) } pub async fn pending_tasks(&self) -> usize { let rxs = &self.tasks_rx; if rxs.len() == 0 { return 0; } let mut tasks = 0; for rx in rxs.iter() { tasks += rx.lock().await.len(); } tasks } pub async fn enqueue_tier_journal_entry(&mut self, je: &Jentry) -> Result<(), std::io::Error> { let wrkr = self.get_worker_ch(je.op_hash()); if wrkr.is_none() { *self.stats.as_mut().expect("err").missed_tier_journal_tasks.get_mut() += 1; } let wrkr = wrkr.expect("err"); select! { //_ -> GlobalContext.Done() => () _ = wrkr.send(Some(Box::new(je.clone()))) => (), else => { *self.stats.as_mut().expect("err").missed_tier_journal_tasks.get_mut() += 1; } } return Ok(()); } pub async fn enqueue_free_version(&mut self, oi: ObjectInfo) { let task = FreeVersionTask(oi); let wrkr = self.get_worker_ch(task.op_hash()); if wrkr.is_none() { *self.stats.as_mut().expect("err").missed_freevers_tasks.get_mut() += 1; return; } let wrkr = wrkr.expect("err!"); select! { //_ -> GlobalContext.Done() => {} _ = wrkr.send(Some(Box::new(task))) => (), else => { *self.stats.as_mut().expect("err").missed_freevers_tasks.get_mut() += 1; } } } pub async fn enqueue_by_days(&mut self, oi: &ObjectInfo, event: &lifecycle::Event, src: &LcEventSrc) { let task = ExpiryTask { obj_info: oi.clone(), event: event.clone(), src: src.clone(), }; let wrkr = self.get_worker_ch(task.op_hash()); if wrkr.is_none() { *self.stats.as_mut().expect("err").missed_expiry_tasks.get_mut() += 1; return; } let wrkr = wrkr.expect("err!"); select! { //_ -> GlobalContext.Done() => {} _ = wrkr.send(Some(Box::new(task))) => (), else => { *self.stats.as_mut().expect("err").missed_expiry_tasks.get_mut() += 1; } } } pub async fn enqueue_by_newer_noncurrent(&mut self, bucket: &str, versions: Vec<ObjectToDelete>, lc_event: lifecycle::Event) { if versions.len() == 0 { return; } let task = NewerNoncurrentTask { bucket: String::from(bucket), versions, event: lc_event, }; let wrkr = self.get_worker_ch(task.op_hash()); if wrkr.is_none() { *self.stats.as_mut().expect("err").missed_expiry_tasks.get_mut() += 1; return; } let wrkr = wrkr.expect("err!"); select! { //_ -> GlobalContext.Done() => {} _ = wrkr.send(Some(Box::new(task))) => (), else => { *self.stats.as_mut().expect("err").missed_expiry_tasks.get_mut() += 1; } } } pub fn get_worker_ch(&self, h: u64) -> Option<Sender<Option<ExpiryOpType>>> { if self.tasks_tx.len() == 0 { return None; } Some(self.tasks_tx[h as usize % self.tasks_tx.len()].clone()) } pub async fn resize_workers(n: usize, api: Arc<ECStore>) { if n == GLOBAL_ExpiryState.read().await.tasks_tx.len() || n < 1 { return; } let mut state = GLOBAL_ExpiryState.write().await; while state.tasks_tx.len() < n { let (tx, rx) = mpsc::channel(1000); let api = api.clone(); let rx = Arc::new(tokio::sync::Mutex::new(rx)); state.tasks_tx.push(tx); state.tasks_rx.push(rx.clone()); *state.stats.as_mut().expect("err").workers.get_mut() += 1; tokio::spawn(async move { let mut rx = rx.lock().await; //let mut expiry_state = GLOBAL_ExpiryState.read().await; ExpiryState::worker(&mut *rx, api).await; }); } let mut l = state.tasks_tx.len(); while l > n { let worker = state.tasks_tx[l - 1].clone(); worker.send(None).await.unwrap_or(()); state.tasks_tx.remove(l - 1); state.tasks_rx.remove(l - 1); *state.stats.as_mut().expect("err").workers.get_mut() -= 1; l -= 1; } } pub async fn worker(rx: &mut Receiver<Option<ExpiryOpType>>, api: Arc<ECStore>) { //let cancel_token = // get_background_services_cancel_token().ok_or_else(|| Error::other("Background services not initialized"))?; loop { select! { //_ = cancel_token.cancelled() => { _ = tokio::signal::ctrl_c() => { info!("got ctrl+c, exits"); break; } v = rx.recv() => { if v.is_none() { break; } let v = v.expect("err!"); if v.is_none() { //rx.close(); //drop(rx); let _ = rx; return; } let v = v.expect("err!"); if v.as_any().is::<ExpiryTask>() { let v = v.as_any().downcast_ref::<ExpiryTask>().expect("err!"); if v.obj_info.transitioned_object.status != "" { apply_expiry_on_transitioned_object(api.clone(), &v.obj_info, &v.event, &v.src).await; } else { apply_expiry_on_non_transitioned_objects(api.clone(), &v.obj_info, &v.event, &v.src).await; } } else if v.as_any().is::<NewerNoncurrentTask>() { let _v = v.as_any().downcast_ref::<NewerNoncurrentTask>().expect("err!"); //delete_object_versions(api, &v.bucket, &v.versions, v.event).await; } else if v.as_any().is::<Jentry>() { //transitionLogIf(es.ctx, deleteObjectFromRemoteTier(es.ctx, v.ObjName, v.VersionID, v.TierName)) } else if v.as_any().is::<FreeVersionTask>() { let v = v.as_any().downcast_ref::<FreeVersionTask>().expect("err!"); let _oi = v.0.clone(); } else { //info!("Invalid work type - {:?}", v); todo!(); } } } } } } struct TransitionTask { obj_info: ObjectInfo, src: LcEventSrc, event: lifecycle::Event, } impl ExpiryOp for TransitionTask { fn op_hash(&self) -> u64 { let mut hasher = Sha256::new(); hasher.update(format!("{}", self.obj_info.bucket).as_bytes()); // hasher.update(format!("{}", self.obj_info.versions[0].object_name).as_bytes()); xxh64::xxh64(hasher.finalize().as_slice(), XXHASH_SEED) } fn as_any(&self) -> &dyn Any { self } } pub struct TransitionState { transition_tx: A_Sender<Option<TransitionTask>>, transition_rx: A_Receiver<Option<TransitionTask>>, pub num_workers: AtomicI64, kill_tx: A_Sender<()>, kill_rx: A_Receiver<()>, active_tasks: AtomicI64, missed_immediate_tasks: AtomicI64, last_day_stats: Arc<Mutex<HashMap<String, LastDayTierStats>>>, } impl TransitionState { #[allow(clippy::new_ret_no_self)] pub fn new() -> Arc<Self> { let (tx1, rx1) = bounded(1000); let (tx2, rx2) = bounded(1); Arc::new(Self { transition_tx: tx1, transition_rx: rx1, num_workers: AtomicI64::new(0), kill_tx: tx2, kill_rx: rx2, active_tasks: AtomicI64::new(0), missed_immediate_tasks: AtomicI64::new(0), last_day_stats: Arc::new(Mutex::new(HashMap::new())), }) } pub async fn queue_transition_task(&self, oi: &ObjectInfo, event: &lifecycle::Event, src: &LcEventSrc) { let task = TransitionTask { obj_info: oi.clone(), src: src.clone(), event: event.clone(), }; select! { //_ -> t.ctx.Done() => (), _ = self.transition_tx.send(Some(task)) => (), else => { match src { LcEventSrc::S3PutObject | LcEventSrc::S3CopyObject | LcEventSrc::S3CompleteMultipartUpload => { self.missed_immediate_tasks.fetch_add(1, Ordering::SeqCst); } _ => () } }, } } pub async fn init(api: Arc<ECStore>) { let max_workers = std::env::var("RUSTFS_MAX_TRANSITION_WORKERS") .ok() .and_then(|s| s.parse::<i64>().ok()) .unwrap_or_else(|| std::cmp::min(num_cpus::get() as i64, 16)); let mut n = max_workers; let tw = 8; //globalILMConfig.getTransitionWorkers(); if tw > 0 { n = tw; } //let mut transition_state = GLOBAL_TransitionState.write().await; //self.objAPI = objAPI Self::update_workers(api, n).await; } pub fn pending_tasks(&self) -> usize { //let transition_rx = GLOBAL_TransitionState.transition_rx.lock().unwrap(); let transition_rx = &GLOBAL_TransitionState.transition_rx; transition_rx.len() } pub fn active_tasks(&self) -> i64 { self.active_tasks.load(Ordering::SeqCst) } pub fn missed_immediate_tasks(&self) -> i64 { self.missed_immediate_tasks.load(Ordering::SeqCst) } pub async fn worker(api: Arc<ECStore>) { loop { select! { _ = GLOBAL_TransitionState.kill_rx.recv() => { return; } task = GLOBAL_TransitionState.transition_rx.recv() => { if task.is_err() { break; } let task = task.expect("err!"); if task.is_none() { //self.transition_rx.close(); //drop(self.transition_rx); return; } let task = task.expect("err!"); if task.as_any().is::<TransitionTask>() { let task = task.as_any().downcast_ref::<TransitionTask>().expect("err!"); GLOBAL_TransitionState.active_tasks.fetch_add(1, Ordering::SeqCst); if let Err(err) = transition_object(api.clone(), &task.obj_info, LcAuditEvent::new(task.event.clone(), task.src.clone())).await { if !is_err_version_not_found(&err) && !is_err_object_not_found(&err) && !is_network_or_host_down(&err.to_string(), false) && !err.to_string().contains("use of closed network connection") { error!("Transition to {} failed for {}/{} version:{} with {}", task.event.storage_class, task.obj_info.bucket, task.obj_info.name, task.obj_info.version_id.map(|v| v.to_string()).unwrap_or_default(), err.to_string()); } } else { let mut ts = TierStats { total_size: task.obj_info.size as u64, num_versions: 1, ..Default::default() }; if task.obj_info.is_latest { ts.num_objects = 1; } GLOBAL_TransitionState.add_lastday_stats(&task.event.storage_class, ts); } GLOBAL_TransitionState.active_tasks.fetch_add(-1, Ordering::SeqCst); } } else => () } } } pub fn add_lastday_stats(&self, tier: &str, ts: TierStats) { let mut tier_stats = self.last_day_stats.lock().unwrap(); tier_stats .entry(tier.to_string()) .and_modify(|e| e.add_stats(ts)) .or_insert(LastDayTierStats::default()); } pub fn get_daily_all_tier_stats(&self) -> DailyAllTierStats { let tier_stats = self.last_day_stats.lock().unwrap(); let mut res = DailyAllTierStats::with_capacity(tier_stats.len()); for (tier, st) in tier_stats.iter() { res.insert(tier.clone(), st.clone()); } res } pub async fn update_workers(api: Arc<ECStore>, n: i64) { Self::update_workers_inner(api, n).await; } pub async fn update_workers_inner(api: Arc<ECStore>, n: i64) { let mut n = n; if n == 0 { let max_workers = std::env::var("RUSTFS_MAX_TRANSITION_WORKERS") .ok() .and_then(|s| s.parse::<i64>().ok()) .unwrap_or_else(|| std::cmp::min(num_cpus::get() as i64, 16)); n = max_workers; } // Allow environment override of maximum workers let absolute_max = std::env::var("RUSTFS_ABSOLUTE_MAX_WORKERS") .ok() .and_then(|s| s.parse::<i64>().ok()) .unwrap_or(32); n = std::cmp::min(n, absolute_max); let mut num_workers = GLOBAL_TransitionState.num_workers.load(Ordering::SeqCst); while num_workers < n { let clone_api = api.clone(); tokio::spawn(async move { TransitionState::worker(clone_api).await; }); num_workers = num_workers + 1; GLOBAL_TransitionState.num_workers.fetch_add(1, Ordering::SeqCst); } let mut num_workers = GLOBAL_TransitionState.num_workers.load(Ordering::SeqCst); while num_workers > n { let worker = GLOBAL_TransitionState.kill_tx.clone(); worker.send(()).await; num_workers = num_workers - 1; GLOBAL_TransitionState.num_workers.fetch_add(-1, Ordering::SeqCst); } } } pub async fn init_background_expiry(api: Arc<ECStore>) { let mut workers = std::env::var("RUSTFS_MAX_EXPIRY_WORKERS") .ok() .and_then(|s| s.parse::<usize>().ok()) .unwrap_or_else(|| std::cmp::min(num_cpus::get(), 16)); //globalILMConfig.getExpirationWorkers() if let Ok(env_expiration_workers) = env::var("_RUSTFS_ILM_EXPIRATION_WORKERS") { if let Ok(num_expirations) = env_expiration_workers.parse::<usize>() { workers = num_expirations; } } if workers == 0 { workers = std::env::var("RUSTFS_DEFAULT_EXPIRY_WORKERS") .ok() .and_then(|s| s.parse::<usize>().ok()) .unwrap_or(8); } //let expiry_state = GLOBAL_ExpiryStSate.write().await; ExpiryState::resize_workers(workers, api).await; } pub async fn validate_transition_tier(lc: &BucketLifecycleConfiguration) -> Result<(), std::io::Error> { for rule in &lc.rules { if let Some(transitions) = &rule.transitions { for transition in transitions { if let Some(storage_class) = &transition.storage_class { if storage_class.as_str() != "" { let valid = GLOBAL_TierConfigMgr.read().await.is_tier_valid(storage_class.as_str()); if !valid { return Err(std::io::Error::other(ERR_INVALID_STORAGECLASS)); } } } } } if let Some(noncurrent_version_transitions) = &rule.noncurrent_version_transitions { for noncurrent_version_transition in noncurrent_version_transitions { if let Some(storage_class) = &noncurrent_version_transition.storage_class { if storage_class.as_str() != "" { let valid = GLOBAL_TierConfigMgr.read().await.is_tier_valid(storage_class.as_str()); if !valid { return Err(std::io::Error::other(ERR_INVALID_STORAGECLASS)); } } } } } } Ok(()) } pub async fn enqueue_transition_immediate(oi: &ObjectInfo, src: LcEventSrc) { let lc = GLOBAL_LifecycleSys.get(&oi.bucket).await; if !lc.is_none() { let event = lc.expect("err").eval(&oi.to_lifecycle_opts()).await; match event.action { IlmAction::TransitionAction | IlmAction::TransitionVersionAction => { if oi.delete_marker || oi.is_dir { return; } GLOBAL_TransitionState.queue_transition_task(oi, &event, &src).await; } _ => (), } } } pub async fn expire_transitioned_object( api: Arc<ECStore>, oi: &ObjectInfo, lc_event: &lifecycle::Event, _src: &LcEventSrc, ) -> Result<ObjectInfo, std::io::Error> { //let traceFn = GLOBAL_LifecycleSys.trace(oi); let mut opts = ObjectOptions { versioned: BucketVersioningSys::prefix_enabled(&oi.bucket, &oi.name).await, expiration: ExpirationOptions { expire: true }, ..Default::default() }; if lc_event.action == IlmAction::DeleteVersionAction { opts.version_id = oi.version_id.map(|id| id.to_string()); } //let tags = LcAuditEvent::new(src, lcEvent).Tags(); if lc_event.action == IlmAction::DeleteRestoredAction { opts.transition.expire_restored = true; match api.delete_object(&oi.bucket, &oi.name, opts).await { Ok(dobj) => { //audit_log_lifecycle(*oi, ILMExpiry, tags, traceFn); return Ok(dobj); } Err(err) => return Err(std::io::Error::other(err)), } } let ret = delete_object_from_remote_tier( &oi.transitioned_object.name, &oi.transitioned_object.version_id, &oi.transitioned_object.tier, ) .await; if ret.is_ok() { opts.skip_decommissioned = true; } else { //transitionLogIf(ctx, err); } let dobj = match api.delete_object(&oi.bucket, &oi.name, opts).await { Ok(obj) => obj, Err(e) => { error!("Failed to delete transitioned object {}/{}: {:?}", oi.bucket, oi.name, e); // Return the original object info if deletion fails oi.clone() } }; //defer auditLogLifecycle(ctx, *oi, ILMExpiry, tags, traceFn) let mut event_name = EventName::ObjectRemovedDelete; if oi.delete_marker { event_name = EventName::ObjectRemovedDeleteMarkerCreated; } let obj_info = ObjectInfo { name: oi.name.clone(), version_id: oi.version_id, delete_marker: oi.delete_marker, ..Default::default() }; send_event(EventArgs { event_name: event_name.as_ref().to_string(), bucket_name: obj_info.bucket.clone(), object: obj_info, user_agent: "Internal: [ILM-Expiry]".to_string(), host: GLOBAL_LocalNodeName.to_string(), ..Default::default() }); /*let system = match notification_system() { Some(sys) => sys, None => { let config = Config::new(); initialize(config).await?; notification_system().expect("Failed to initialize notification system") } }; let event = Arc::new(Event::new_test_event("my-bucket", "document.pdf", EventName::ObjectCreatedPut)); system.send_event(event).await;*/ Ok(dobj) } pub fn gen_transition_objname(bucket: &str) -> Result<String, Error> { let us = Uuid::new_v4().to_string(); let mut hasher = Sha256::new(); hasher.update(format!("{}/{}", get_global_deployment_id().unwrap_or_default(), bucket).as_bytes()); let hash = rustfs_utils::crypto::hex(hasher.finalize().as_slice()); let obj = format!("{}/{}/{}/{}", &hash[0..16], &us[0..2], &us[2..4], &us); Ok(obj) } pub async fn transition_object(api: Arc<ECStore>, oi: &ObjectInfo, lae: LcAuditEvent) -> Result<(), Error> { let time_ilm = Metrics::time_ilm(lae.event.action); let etag = if let Some(etag) = &oi.etag { etag } else { "" }; let etag = etag.to_string(); let opts = ObjectOptions { transition: TransitionOptions { status: lifecycle::TRANSITION_PENDING.to_string(), tier: lae.event.storage_class, etag, ..Default::default() }, //lifecycle_audit_event: lae, version_id: oi.version_id.map(|v| v.to_string()), versioned: BucketVersioningSys::prefix_enabled(&oi.bucket, &oi.name).await, version_suspended: BucketVersioningSys::prefix_suspended(&oi.bucket, &oi.name).await, mod_time: oi.mod_time, ..Default::default() }; time_ilm(1); api.transition_object(&oi.bucket, &oi.name, &opts).await } pub fn audit_tier_actions(_api: ECStore, _tier: &str, _bytes: i64) -> TimeFn { todo!(); } pub async fn get_transitioned_object_reader( bucket: &str, object: &str, rs: &Option<HTTPRangeSpec>, h: &HeaderMap, oi: &ObjectInfo, opts: &ObjectOptions, ) -> Result<GetObjectReader, std::io::Error> { let mut tier_config_mgr = GLOBAL_TierConfigMgr.write().await; let tgt_client = match tier_config_mgr.get_driver(&oi.transitioned_object.tier).await { Ok(d) => d, Err(err) => return Err(std::io::Error::other(err)), }; let ret = new_getobjectreader(rs, &oi, opts, &h); if let Err(err) = ret { return Err(error_resp_to_object_err(err, vec![bucket, object])); } let (get_fn, off, length) = ret.expect("err"); let mut gopts = WarmBackendGetOpts::default(); if off >= 0 && length >= 0 { gopts.start_offset = off; gopts.length = length; } //return Ok(HttpFileReader::new(rs, &oi, opts, &h)); //timeTierAction := auditTierActions(oi.transitioned_object.Tier, length) let reader = tgt_client .get(&oi.transitioned_object.name, &oi.transitioned_object.version_id, gopts) .await?; Ok(get_fn(reader, h.clone())) } pub async fn post_restore_opts(version_id: &str, bucket: &str, object: &str) -> Result<ObjectOptions, std::io::Error> { let versioned = BucketVersioningSys::prefix_enabled(bucket, object).await; let version_suspended = BucketVersioningSys::prefix_suspended(bucket, object).await; let vid = version_id.trim(); if vid != "" && vid != NULL_VERSION_ID { if let Err(err) = Uuid::parse_str(vid) { return Err(std::io::Error::other( StorageError::InvalidVersionID(bucket.to_string(), object.to_string(), vid.to_string()).to_string(), )); } if !versioned && !version_suspended { return Err(std::io::Error::other( StorageError::InvalidArgument( bucket.to_string(), object.to_string(), format!("version-id specified {} but versioning is not enabled on {}", vid, bucket), ) .to_string(), )); } } Ok(ObjectOptions { versioned: versioned, version_suspended: version_suspended, version_id: Some(vid.to_string()), ..Default::default() }) } pub async fn put_restore_opts( bucket: &str, object: &str, rreq: &RestoreRequest, oi: &ObjectInfo, ) -> Result<ObjectOptions, std::io::Error> { let mut meta = HashMap::<String, String>::new(); /*let mut b = false; let Some(Some(Some(mut sc))) = rreq.output_location.s3.storage_class else { b = true; }; if b || sc == "" { //sc = oi.storage_class; sc = oi.transitioned_object.tier; } meta.insert(X_AMZ_STORAGE_CLASS.as_str().to_lowercase(), sc);*/ if let Some(type_) = &rreq.type_ && type_.as_str() == RestoreRequestType::SELECT { for v in rreq .output_location .as_ref() .unwrap() .s3 .as_ref() .unwrap() .user_metadata .as_ref() .unwrap() { if !strings_has_prefix_fold(&v.name.clone().unwrap(), "x-amz-meta") { meta.insert( format!("x-amz-meta-{}", v.name.as_ref().unwrap()), v.value.clone().unwrap_or("".to_string()), ); continue; } meta.insert(v.name.clone().unwrap(), v.value.clone().unwrap_or("".to_string())); } if let Some(output_location) = rreq.output_location.as_ref() { if let Some(s3) = &output_location.s3 { if let Some(tags) = &s3.tagging { meta.insert( AMZ_OBJECT_TAGGING.to_string(), serde_urlencoded::to_string(tags.tag_set.clone()).unwrap_or("".to_string()), ); } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
true
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/lifecycle.rs
crates/ecstore/src/bucket/lifecycle/lifecycle.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use s3s::dto::{ BucketLifecycleConfiguration, ExpirationStatus, LifecycleExpiration, LifecycleRule, NoncurrentVersionTransition, ObjectLockConfiguration, ObjectLockEnabled, RestoreRequest, Transition, }; use std::cmp::Ordering; use std::env; use std::fmt::Display; use std::sync::Arc; use time::macros::{datetime, offset}; use time::{self, Duration, OffsetDateTime}; use tracing::info; use crate::bucket::lifecycle::rule::TransitionOps; pub const TRANSITION_COMPLETE: &str = "complete"; pub const TRANSITION_PENDING: &str = "pending"; const ERR_LIFECYCLE_TOO_MANY_RULES: &str = "Lifecycle configuration allows a maximum of 1000 rules"; const ERR_LIFECYCLE_NO_RULE: &str = "Lifecycle configuration should have at least one rule"; const ERR_LIFECYCLE_DUPLICATE_ID: &str = "Rule ID must be unique. Found same ID for more than one rule"; const _ERR_XML_NOT_WELL_FORMED: &str = "The XML you provided was not well-formed or did not validate against our published schema"; const ERR_LIFECYCLE_BUCKET_LOCKED: &str = "ExpiredObjectAllVersions element and DelMarkerExpiration action cannot be used on an retention bucket"; pub use rustfs_common::metrics::IlmAction; #[async_trait::async_trait] pub trait RuleValidate { fn validate(&self) -> Result<(), std::io::Error>; } #[async_trait::async_trait] impl RuleValidate for LifecycleRule { /*fn validate_id(&self) -> Result<()> { if self.id.len() > 255 { return errInvalidRuleID; } Ok(()) } fn validate_status(&self) -> Result<()> { if self.status.len() == 0 { return ErrEmptyRuleStatus; } if self.status != Enabled && self.status != Disabled { return ErrInvalidRuleStatus; } Ok(()) } fn validate_expiration(&self) -> Result<()> { self.expiration.validate(); } fn validate_noncurrent_expiration(&self) -> Result<()> { self.noncurrent_version_expiration.validate() } fn validate_prefix_and_filter(&self) -> Result<()> { if !self.prefix.set && self.Filter.isempty() || self.prefix.set && !self.filter.isempty() { return ErrXMLNotWellFormed; } if !self.prefix.set { return self.filter.validate(); } Ok(()) } fn validate_transition(&self) -> Result<()> { self.Transition.Validate() } fn validate_noncurrent_transition(&self) -> Result<()> { self.NoncurrentVersionTransition.Validate() } fn get_prefix(&self) -> String { if p := self.Prefix.String(); p != "" { return p } if p := self.Filter.Prefix.String(); p != "" { return p } if p := self.Filter.And.Prefix.String(); p != "" { return p } "".to_string() }*/ fn validate(&self) -> Result<(), std::io::Error> { /*self.validate_id()?; self.validate_status()?; self.validate_expiration()?; self.validate_noncurrent_expiration()?; self.validate_prefix_and_filter()?; self.validate_transition()?; self.validate_noncurrent_transition()?; if (!self.Filter.Tag.IsEmpty() || len(self.Filter.And.Tags) != 0) && !self.delmarker_expiration.Empty() { return errInvalidRuleDelMarkerExpiration } if !self.expiration.set && !self.transition.set && !self.noncurrent_version_expiration.set && !self.noncurrent_version_transitions.unwrap()[0].set && self.delmarker_expiration.Empty() { return errXMLNotWellFormed }*/ Ok(()) } } #[async_trait::async_trait] pub trait Lifecycle { async fn has_transition(&self) -> bool; fn has_expiry(&self) -> bool; async fn has_active_rules(&self, prefix: &str) -> bool; async fn validate(&self, lr: &ObjectLockConfiguration) -> Result<(), std::io::Error>; async fn filter_rules(&self, obj: &ObjectOpts) -> Option<Vec<LifecycleRule>>; async fn eval(&self, obj: &ObjectOpts) -> Event; async fn eval_inner(&self, obj: &ObjectOpts, now: OffsetDateTime) -> Event; //fn set_prediction_headers(&self, w: http.ResponseWriter, obj: ObjectOpts); async fn noncurrent_versions_expiration_limit(self: Arc<Self>, obj: &ObjectOpts) -> Event; } #[async_trait::async_trait] impl Lifecycle for BucketLifecycleConfiguration { async fn has_transition(&self) -> bool { for rule in self.rules.iter() { if !rule.transitions.is_none() { return true; } } false } fn has_expiry(&self) -> bool { for rule in self.rules.iter() { if !rule.expiration.is_none() || !rule.noncurrent_version_expiration.is_none() { return true; } } false } async fn has_active_rules(&self, prefix: &str) -> bool { if self.rules.len() == 0 { return false; } for rule in self.rules.iter() { if rule.status.as_str() == ExpirationStatus::DISABLED { continue; } let rule_prefix = rule.prefix.as_ref().expect("err!"); if prefix.len() > 0 && rule_prefix.len() > 0 && !prefix.starts_with(rule_prefix) && !rule_prefix.starts_with(&prefix) { continue; } let rule_noncurrent_version_expiration = rule.noncurrent_version_expiration.as_ref().expect("err!"); if rule_noncurrent_version_expiration.noncurrent_days.expect("err!") > 0 { return true; } if rule_noncurrent_version_expiration.newer_noncurrent_versions.expect("err!") > 0 { return true; } if !rule.noncurrent_version_transitions.is_none() { return true; } let rule_expiration = rule.expiration.as_ref().expect("err!"); if !rule_expiration.date.is_none() && OffsetDateTime::from(rule_expiration.date.clone().expect("err!")).unix_timestamp() < OffsetDateTime::now_utc().unix_timestamp() { return true; } if !rule_expiration.date.is_none() { return true; } if rule_expiration.expired_object_delete_marker.expect("err!") { return true; } let rule_transitions: &[Transition] = &rule.transitions.as_ref().expect("err!"); let rule_transitions_0 = rule_transitions[0].clone(); if !rule_transitions_0.date.is_none() && OffsetDateTime::from(rule_transitions_0.date.expect("err!")).unix_timestamp() < OffsetDateTime::now_utc().unix_timestamp() { return true; } if !rule.transitions.is_none() { return true; } } false } async fn validate(&self, lr: &ObjectLockConfiguration) -> Result<(), std::io::Error> { if self.rules.len() > 1000 { return Err(std::io::Error::other(ERR_LIFECYCLE_TOO_MANY_RULES)); } if self.rules.len() == 0 { return Err(std::io::Error::other(ERR_LIFECYCLE_NO_RULE)); } for r in &self.rules { r.validate()?; /*if let Some(object_lock_enabled) = lr.object_lock_enabled.as_ref() { if let Some(expiration) = r.expiration.as_ref() { if let Some(expired_object_delete_marker) = expiration.expired_object_delete_marker { if object_lock_enabled.as_str() == ObjectLockEnabled::ENABLED && (expired_object_delete_marker) { return Err(std::io::Error::other(ERR_LIFECYCLE_BUCKET_LOCKED)); } } } }*/ } for (i, _) in self.rules.iter().enumerate() { if i == self.rules.len() - 1 { break; } let other_rules = &self.rules[i + 1..]; for other_rule in other_rules { if self.rules[i].id == other_rule.id { return Err(std::io::Error::other(ERR_LIFECYCLE_DUPLICATE_ID)); } } } Ok(()) } async fn filter_rules(&self, obj: &ObjectOpts) -> Option<Vec<LifecycleRule>> { if obj.name == "" { return None; } let mut rules = Vec::<LifecycleRule>::new(); for rule in self.rules.iter() { if rule.status.as_str() == ExpirationStatus::DISABLED { continue; } if let Some(prefix) = rule.prefix.clone() { if !obj.name.starts_with(prefix.as_str()) { continue; } } /*if !rule.filter.test_tags(obj.user_tags) { continue; }*/ //if !obj.delete_marker && !rule.filter.BySize(obj.size) { if !obj.delete_marker && false { continue; } rules.push(rule.clone()); } Some(rules) } async fn eval(&self, obj: &ObjectOpts) -> Event { self.eval_inner(obj, OffsetDateTime::now_utc()).await } async fn eval_inner(&self, obj: &ObjectOpts, now: OffsetDateTime) -> Event { let mut events = Vec::<Event>::new(); info!( "eval_inner: object={}, mod_time={:?}, now={:?}, is_latest={}, delete_marker={}", obj.name, obj.mod_time, now, obj.is_latest, obj.delete_marker ); // Gracefully handle missing mod_time instead of panicking let mod_time = match obj.mod_time { Some(t) => t, None => { info!("eval_inner: mod_time is None for object={}, returning default event", obj.name); return Event::default(); } }; if mod_time.unix_timestamp() == 0 { info!("eval_inner: mod_time is 0, returning default event"); return Event::default(); } if let Some(restore_expires) = obj.restore_expires { if !restore_expires.unix_timestamp() == 0 && now.unix_timestamp() > restore_expires.unix_timestamp() { let mut action = IlmAction::DeleteRestoredAction; if !obj.is_latest { action = IlmAction::DeleteRestoredVersionAction; } events.push(Event { action, due: Some(now), rule_id: "".into(), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }); } } if let Some(ref lc_rules) = self.filter_rules(obj).await { for rule in lc_rules.iter() { if obj.expired_object_deletemarker() { if let Some(expiration) = rule.expiration.as_ref() { if let Some(expired_object_delete_marker) = expiration.expired_object_delete_marker { events.push(Event { action: IlmAction::DeleteVersionAction, rule_id: rule.id.clone().expect("err!"), due: Some(now), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }); break; } if let Some(days) = expiration.days { let expected_expiry = expected_expiry_time(mod_time, days /*, date*/); if now.unix_timestamp() >= expected_expiry.unix_timestamp() { events.push(Event { action: IlmAction::DeleteVersionAction, rule_id: rule.id.clone().expect("err!"), due: Some(expected_expiry), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }); break; } } } } if obj.is_latest { if let Some(ref expiration) = rule.expiration { if let Some(expired_object_delete_marker) = expiration.expired_object_delete_marker { if obj.delete_marker && expired_object_delete_marker { let due = expiration.next_due(obj); if let Some(due) = due { if now.unix_timestamp() >= due.unix_timestamp() { events.push(Event { action: IlmAction::DelMarkerDeleteAllVersionsAction, rule_id: rule.id.clone().expect("err!"), due: Some(due), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }); } } continue; } } } } if !obj.is_latest { if let Some(ref noncurrent_version_expiration) = rule.noncurrent_version_expiration { if let Some(newer_noncurrent_versions) = noncurrent_version_expiration.newer_noncurrent_versions { if newer_noncurrent_versions > 0 { continue; } } } } if !obj.is_latest { if let Some(ref noncurrent_version_expiration) = rule.noncurrent_version_expiration { if let Some(noncurrent_days) = noncurrent_version_expiration.noncurrent_days { if noncurrent_days != 0 { if let Some(successor_mod_time) = obj.successor_mod_time { let expected_expiry = expected_expiry_time(successor_mod_time, noncurrent_days); if now.unix_timestamp() >= expected_expiry.unix_timestamp() { events.push(Event { action: IlmAction::DeleteVersionAction, rule_id: rule.id.clone().expect("err!"), due: Some(expected_expiry), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }); } } } } } } if !obj.is_latest { if let Some(ref noncurrent_version_transitions) = rule.noncurrent_version_transitions { if let Some(ref storage_class) = noncurrent_version_transitions[0].storage_class { if storage_class.as_str() != "" && !obj.delete_marker && obj.transition_status != TRANSITION_COMPLETE { let due = rule.noncurrent_version_transitions.as_ref().unwrap()[0].next_due(obj); if let Some(due0) = due { if now.unix_timestamp() == 0 || now.unix_timestamp() > due0.unix_timestamp() { events.push(Event { action: IlmAction::TransitionVersionAction, rule_id: rule.id.clone().expect("err!"), due, storage_class: rule.noncurrent_version_transitions.as_ref().unwrap()[0] .storage_class .clone() .unwrap() .as_str() .to_string(), ..Default::default() }); } } } } } } info!( "eval_inner: checking expiration condition - is_latest={}, delete_marker={}, version_id={:?}, condition_met={}", obj.is_latest, obj.delete_marker, obj.version_id, (obj.is_latest || obj.version_id.is_empty()) && !obj.delete_marker ); // Allow expiration for latest objects OR non-versioned objects (empty version_id) if (obj.is_latest || obj.version_id.is_empty()) && !obj.delete_marker { info!("eval_inner: entering expiration check"); if let Some(ref expiration) = rule.expiration { if let Some(ref date) = expiration.date { let date0 = OffsetDateTime::from(date.clone()); if date0.unix_timestamp() != 0 && (now.unix_timestamp() >= date0.unix_timestamp()) { info!("eval_inner: expiration by date - date0={:?}", date0); events.push(Event { action: IlmAction::DeleteAction, rule_id: rule.id.clone().expect("err!"), due: Some(date0), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }); } } else if let Some(days) = expiration.days { let expected_expiry: OffsetDateTime = expected_expiry_time(mod_time, days); info!( "eval_inner: expiration check - days={}, obj_time={:?}, expiry_time={:?}, now={:?}, should_expire={}", days, mod_time, expected_expiry, now, now.unix_timestamp() > expected_expiry.unix_timestamp() ); if now.unix_timestamp() >= expected_expiry.unix_timestamp() { info!("eval_inner: object should expire, adding DeleteAction"); let mut event = Event { action: IlmAction::DeleteAction, rule_id: rule.id.clone().expect("err!"), due: Some(expected_expiry), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), }; /*if rule.expiration.expect("err!").delete_all.val { event.action = IlmAction::DeleteAllVersionsAction }*/ events.push(event); } } else { info!("eval_inner: expiration.days is None"); } } else { info!("eval_inner: rule.expiration is None"); } if obj.transition_status != TRANSITION_COMPLETE { if let Some(ref transitions) = rule.transitions { let due = transitions[0].next_due(obj); if let Some(due0) = due { if now.unix_timestamp() == 0 || now.unix_timestamp() > due0.unix_timestamp() { events.push(Event { action: IlmAction::TransitionAction, rule_id: rule.id.clone().expect("err!"), due, storage_class: transitions[0].storage_class.clone().expect("err!").as_str().to_string(), noncurrent_days: 0, newer_noncurrent_versions: 0, }); } } } } } } } if events.len() > 0 { events.sort_by(|a, b| { if now.unix_timestamp() > a.due.expect("err!").unix_timestamp() && now.unix_timestamp() > b.due.expect("err").unix_timestamp() || a.due.expect("err").unix_timestamp() == b.due.expect("err").unix_timestamp() { match a.action { IlmAction::DeleteAllVersionsAction | IlmAction::DelMarkerDeleteAllVersionsAction | IlmAction::DeleteAction | IlmAction::DeleteVersionAction => { return Ordering::Less; } _ => (), } match b.action { IlmAction::DeleteAllVersionsAction | IlmAction::DelMarkerDeleteAllVersionsAction | IlmAction::DeleteAction | IlmAction::DeleteVersionAction => { return Ordering::Greater; } _ => (), } return Ordering::Less; } if a.due.expect("err").unix_timestamp() < b.due.expect("err").unix_timestamp() { return Ordering::Less; } return Ordering::Greater; }); return events[0].clone(); } Event::default() } async fn noncurrent_versions_expiration_limit(self: Arc<Self>, obj: &ObjectOpts) -> Event { if let Some(filter_rules) = self.filter_rules(obj).await { for rule in filter_rules.iter() { if let Some(ref noncurrent_version_expiration) = rule.noncurrent_version_expiration { if let Some(newer_noncurrent_versions) = noncurrent_version_expiration.newer_noncurrent_versions { if newer_noncurrent_versions == 0 { continue; } return Event { action: IlmAction::DeleteVersionAction, rule_id: rule.id.clone().expect("err"), noncurrent_days: noncurrent_version_expiration.noncurrent_days.expect("noncurrent_days err.") as u32, newer_noncurrent_versions: newer_noncurrent_versions as usize, due: Some(OffsetDateTime::UNIX_EPOCH), storage_class: "".into(), }; } else { return Event { action: IlmAction::DeleteVersionAction, rule_id: rule.id.clone().expect("err"), noncurrent_days: noncurrent_version_expiration.noncurrent_days.expect("noncurrent_days err.") as u32, newer_noncurrent_versions: 0, due: Some(OffsetDateTime::UNIX_EPOCH), storage_class: "".into(), }; } } } } Event::default() } } #[async_trait::async_trait] pub trait LifecycleCalculate { fn next_due(&self, obj: &ObjectOpts) -> Option<OffsetDateTime>; } #[async_trait::async_trait] impl LifecycleCalculate for LifecycleExpiration { fn next_due(&self, obj: &ObjectOpts) -> Option<OffsetDateTime> { if !obj.is_latest || !obj.delete_marker { return None; } match self.days { Some(days) => Some(expected_expiry_time(obj.mod_time.unwrap(), days)), None => None, } } } #[async_trait::async_trait] impl LifecycleCalculate for NoncurrentVersionTransition { fn next_due(&self, obj: &ObjectOpts) -> Option<OffsetDateTime> { if obj.is_latest || self.storage_class.is_none() { return None; } match self.noncurrent_days { Some(noncurrent_days) => { if let Some(successor_mod_time) = obj.successor_mod_time { Some(expected_expiry_time(successor_mod_time, noncurrent_days)) } else { Some(expected_expiry_time(OffsetDateTime::now_utc(), noncurrent_days)) } } None => obj.successor_mod_time, } } } #[async_trait::async_trait] impl LifecycleCalculate for Transition { fn next_due(&self, obj: &ObjectOpts) -> Option<OffsetDateTime> { if !obj.is_latest || self.days.is_none() { return None; } if let Some(date) = self.date.clone() { return Some(date.into()); } match self.days { Some(days) => Some(expected_expiry_time(obj.mod_time.unwrap(), days)), None => obj.mod_time, } } } pub fn expected_expiry_time(mod_time: OffsetDateTime, days: i32) -> OffsetDateTime { if days == 0 { info!("expected_expiry_time: days=0, returning UNIX_EPOCH for immediate expiry"); return OffsetDateTime::UNIX_EPOCH; // Return epoch time to ensure immediate expiry } let t = mod_time .to_offset(offset!(-0:00:00)) .saturating_add(Duration::days(days as i64)); let mut hour = 3600; if let Ok(env_ilm_hour) = env::var("_RUSTFS_ILM_PROCESS_TIME") { if let Ok(num_hour) = env_ilm_hour.parse::<usize>() { hour = num_hour; } } //t.Truncate(24 * hour) info!("expected_expiry_time: mod_time={:?}, days={}, result={:?}", mod_time, days, t); t } #[derive(Default)] pub struct ObjectOpts { pub name: String, pub user_tags: String, pub mod_time: Option<OffsetDateTime>, pub size: usize, pub version_id: String, pub is_latest: bool, pub delete_marker: bool, pub num_versions: usize, pub successor_mod_time: Option<OffsetDateTime>, pub transition_status: String, pub restore_ongoing: bool, pub restore_expires: Option<OffsetDateTime>, pub versioned: bool, pub version_suspended: bool, } impl ObjectOpts { pub fn expired_object_deletemarker(&self) -> bool { self.delete_marker && self.num_versions == 1 } } #[derive(Debug, Clone)] pub struct Event { pub action: IlmAction, pub rule_id: String, pub due: Option<OffsetDateTime>, pub noncurrent_days: u32, pub newer_noncurrent_versions: usize, pub storage_class: String, } impl Default for Event { fn default() -> Self { Self { action: IlmAction::NoneAction, rule_id: "".into(), due: Some(OffsetDateTime::UNIX_EPOCH), noncurrent_days: 0, newer_noncurrent_versions: 0, storage_class: "".into(), } } } #[derive(Debug, Clone, Default)] pub struct ExpirationOptions { pub expire: bool, } #[derive(Debug, Clone)] pub struct TransitionOptions { pub status: String, pub tier: String, pub etag: String, pub restore_request: RestoreRequest, pub restore_expiry: OffsetDateTime, pub expire_restored: bool, } impl Default for TransitionOptions { fn default() -> Self { Self { status: Default::default(), tier: Default::default(), etag: Default::default(), restore_request: Default::default(), restore_expiry: OffsetDateTime::now_utc(), expire_restored: Default::default(), } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/tier_sweeper.rs
crates/ecstore/src/bucket/lifecycle/tier_sweeper.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use sha2::{Digest, Sha256}; use std::any::Any; use std::io::Write; use xxhash_rust::xxh64; use super::bucket_lifecycle_ops::{ExpiryOp, GLOBAL_ExpiryState, TransitionedObject}; use super::lifecycle::{self, ObjectOpts}; use crate::global::GLOBAL_TierConfigMgr; static XXHASH_SEED: u64 = 0; #[derive(Default)] #[allow(dead_code)] struct ObjSweeper { object: String, bucket: String, version_id: String, versioned: bool, suspended: bool, transition_status: String, transition_tier: String, transition_version_id: String, remote_object: String, } #[allow(dead_code)] impl ObjSweeper { #[allow(clippy::new_ret_no_self)] pub async fn new(bucket: &str, object: &str) -> Result<Self, std::io::Error> { Ok(Self { object: object.into(), bucket: bucket.into(), ..Default::default() }) } pub fn with_version(&mut self, vid: String) -> &Self { self.version_id = vid; self } pub fn with_versioning(&mut self, versioned: bool, suspended: bool) -> &Self { self.versioned = versioned; self.suspended = suspended; self } pub fn get_opts(&self) -> lifecycle::ObjectOpts { let mut opts = ObjectOpts { version_id: self.version_id.clone(), versioned: self.versioned, version_suspended: self.suspended, ..Default::default() }; if self.suspended && self.version_id == "" { opts.version_id = String::from(""); } opts } pub fn set_transition_state(&mut self, info: TransitionedObject) { self.transition_tier = info.tier; self.transition_status = info.status; self.remote_object = info.name; self.transition_version_id = info.version_id; } pub fn should_remove_remote_object(&self) -> Option<Jentry> { if self.transition_status != lifecycle::TRANSITION_COMPLETE { return None; } let mut del_tier = false; if !self.versioned || self.suspended { // 1, 2.a, 2.b del_tier = true; } else if self.versioned && self.version_id != "" { // 3.a del_tier = true; } if del_tier { return Some(Jentry { obj_name: self.remote_object.clone(), version_id: self.transition_version_id.clone(), tier_name: self.transition_tier.clone(), }); } None } pub async fn sweep(&self) { let je = self.should_remove_remote_object(); if !je.is_none() { let mut expiry_state = GLOBAL_ExpiryState.write().await; expiry_state.enqueue_tier_journal_entry(&je.expect("err!")); } } } #[derive(Debug, Clone)] #[allow(unused_assignments)] pub struct Jentry { obj_name: String, version_id: String, tier_name: String, } impl ExpiryOp for Jentry { fn op_hash(&self) -> u64 { let mut hasher = Sha256::new(); hasher.update(format!("{}", self.tier_name).as_bytes()); hasher.update(format!("{}", self.obj_name).as_bytes()); xxh64::xxh64(hasher.finalize().as_slice(), XXHASH_SEED) } fn as_any(&self) -> &dyn Any { self } } pub async fn delete_object_from_remote_tier(obj_name: &str, rv_id: &str, tier_name: &str) -> Result<(), std::io::Error> { let mut config_mgr = GLOBAL_TierConfigMgr.write().await; let w = match config_mgr.get_driver(tier_name).await { Ok(w) => w, Err(e) => return Err(std::io::Error::other(e)), }; w.remove(obj_name, rv_id).await } #[cfg(test)] mod test {}
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/mod.rs
crates/ecstore/src/bucket/lifecycle/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub mod bucket_lifecycle_audit; pub mod bucket_lifecycle_ops; pub mod lifecycle; pub mod rule; pub mod tier_last_day_stats; pub mod tier_sweeper;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/rule.rs
crates/ecstore/src/bucket/lifecycle/rule.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use s3s::dto::{LifecycleRuleFilter, Transition}; const _ERR_TRANSITION_INVALID_DAYS: &str = "Days must be 0 or greater when used with Transition"; const _ERR_TRANSITION_INVALID_DATE: &str = "Date must be provided in ISO 8601 format"; const ERR_TRANSITION_INVALID: &str = "Exactly one of Days (0 or greater) or Date (positive ISO 8601 format) should be present in Transition."; const _ERR_TRANSITION_DATE_NOT_MIDNIGHT: &str = "'Date' must be at midnight GMT"; pub trait Filter { fn test_tags(&self, user_tags: &str) -> bool; fn by_size(&self, sz: i64) -> bool; } impl Filter for LifecycleRuleFilter { fn test_tags(&self, user_tags: &str) -> bool { true } fn by_size(&self, sz: i64) -> bool { true } } pub trait TransitionOps { fn validate(&self) -> Result<(), std::io::Error>; } impl TransitionOps for Transition { fn validate(&self) -> Result<(), std::io::Error> { if !self.date.is_none() && self.days.expect("err!") > 0 { return Err(std::io::Error::other(ERR_TRANSITION_INVALID)); } if self.storage_class.is_none() { return Err(std::io::Error::other("ERR_XML_NOT_WELL_FORMED")); } Ok(()) } } #[cfg(test)] mod test { use super::*; #[tokio::test] async fn test_rule() { //assert!(skip_access_checks(p.to_str().unwrap())); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/lifecycle/bucket_lifecycle_audit.rs
crates/ecstore/src/bucket/lifecycle/bucket_lifecycle_audit.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use super::lifecycle; #[derive(Debug, Clone, Default)] pub enum LcEventSrc { #[default] None, Heal, Scanner, Decom, Rebal, S3HeadObject, S3GetObject, S3ListObjects, S3PutObject, S3CopyObject, S3CompleteMultipartUpload, } #[derive(Clone, Debug, Default)] pub struct LcAuditEvent { pub event: lifecycle::Event, pub source: LcEventSrc, } impl LcAuditEvent { pub fn new(event: lifecycle::Event, source: LcEventSrc) -> Self { Self { event, source } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/object_lock/objectlock.rs
crates/ecstore/src/bucket/object_lock/objectlock.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::collections::HashMap; use time::{OffsetDateTime, format_description}; use s3s::dto::{Date, ObjectLockLegalHold, ObjectLockLegalHoldStatus, ObjectLockRetention, ObjectLockRetentionMode}; use s3s::header::{X_AMZ_OBJECT_LOCK_LEGAL_HOLD, X_AMZ_OBJECT_LOCK_MODE, X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE}; const _ERR_MALFORMED_BUCKET_OBJECT_CONFIG: &str = "invalid bucket object lock config"; const _ERR_INVALID_RETENTION_DATE: &str = "date must be provided in ISO 8601 format"; const _ERR_PAST_OBJECTLOCK_RETAIN_DATE: &str = "the retain until date must be in the future"; const _ERR_UNKNOWN_WORMMODE_DIRECTIVE: &str = "unknown WORM mode directive"; const _ERR_OBJECTLOCK_MISSING_CONTENT_MD5: &str = "content-MD5 HTTP header is required for Put Object requests with Object Lock parameters"; const _ERR_OBJECTLOCK_INVALID_HEADERS: &str = "x-amz-object-lock-retain-until-date and x-amz-object-lock-mode must both be supplied"; const _ERR_MALFORMED_XML: &str = "the XML you provided was not well-formed or did not validate against our published schema"; pub fn utc_now_ntp() -> OffsetDateTime { OffsetDateTime::now_utc() } pub fn get_object_retention_meta(meta: HashMap<String, String>) -> ObjectLockRetention { let mut retain_until_date: Date = Date::from(OffsetDateTime::UNIX_EPOCH); let mut mode_str = meta.get(X_AMZ_OBJECT_LOCK_MODE.as_str().to_lowercase().as_str()); if mode_str.is_none() { mode_str = Some(&meta[X_AMZ_OBJECT_LOCK_MODE.as_str()]); } let mode = if let Some(mode_str) = mode_str { parse_ret_mode(mode_str.as_str()) } else { return ObjectLockRetention { mode: None, retain_until_date: None, }; }; let mut till_str = meta.get(X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE.as_str().to_lowercase().as_str()); if till_str.is_none() { till_str = Some(&meta[X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE.as_str()]); } if let Some(till_str) = till_str { let t = OffsetDateTime::parse(till_str, &format_description::well_known::Iso8601::DEFAULT); if let Ok(parsed_time) = t { retain_until_date = Date::from(parsed_time); } } ObjectLockRetention { mode: Some(mode), retain_until_date: Some(retain_until_date), } } pub fn get_object_legalhold_meta(meta: HashMap<String, String>) -> ObjectLockLegalHold { let mut hold_str = meta.get(X_AMZ_OBJECT_LOCK_LEGAL_HOLD.as_str().to_lowercase().as_str()); if hold_str.is_none() { hold_str = Some(&meta[X_AMZ_OBJECT_LOCK_LEGAL_HOLD.as_str()]); } if let Some(hold_str) = hold_str { return ObjectLockLegalHold { status: Some(parse_legalhold_status(hold_str)), }; } ObjectLockLegalHold { status: None } } pub fn parse_ret_mode(mode_str: &str) -> ObjectLockRetentionMode { match mode_str.to_uppercase().as_str() { "GOVERNANCE" => ObjectLockRetentionMode::from_static(ObjectLockRetentionMode::GOVERNANCE), "COMPLIANCE" => ObjectLockRetentionMode::from_static(ObjectLockRetentionMode::COMPLIANCE), _ => unreachable!(), } } pub fn parse_legalhold_status(hold_str: &str) -> ObjectLockLegalHoldStatus { match hold_str { "ON" => ObjectLockLegalHoldStatus::from_static(ObjectLockLegalHoldStatus::ON), "OFF" => ObjectLockLegalHoldStatus::from_static(ObjectLockLegalHoldStatus::OFF), _ => unreachable!(), } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/object_lock/mod.rs
crates/ecstore/src/bucket/object_lock/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub mod objectlock; pub mod objectlock_sys; use s3s::dto::{ObjectLockConfiguration, ObjectLockEnabled}; pub trait ObjectLockApi { fn enabled(&self) -> bool; } impl ObjectLockApi for ObjectLockConfiguration { fn enabled(&self) -> bool { self.object_lock_enabled .as_ref() .is_some_and(|v| v.as_str() == ObjectLockEnabled::ENABLED) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/object_lock/objectlock_sys.rs
crates/ecstore/src/bucket/object_lock/objectlock_sys.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::sync::Arc; use time::OffsetDateTime; use s3s::dto::{DefaultRetention, ObjectLockLegalHoldStatus, ObjectLockRetentionMode}; use crate::bucket::metadata_sys::get_object_lock_config; use crate::store_api::ObjectInfo; use super::objectlock; pub struct BucketObjectLockSys {} impl BucketObjectLockSys { #[allow(clippy::new_ret_no_self)] pub async fn new() -> Arc<Self> { Arc::new(Self {}) } pub async fn get(bucket: &str) -> Option<DefaultRetention> { if let Ok(object_lock_config) = get_object_lock_config(bucket).await && let Some(object_lock_rule) = object_lock_config.0.rule { return object_lock_rule.default_retention; } None } } pub fn enforce_retention_for_deletion(obj_info: &ObjectInfo) -> bool { if obj_info.delete_marker { return false; } let lhold = objectlock::get_object_legalhold_meta(obj_info.user_defined.clone()); match lhold.status { Some(st) if st.as_str() == ObjectLockLegalHoldStatus::ON => { return true; } _ => (), } let ret = objectlock::get_object_retention_meta(obj_info.user_defined.clone()); match ret.mode { Some(r) if (r.as_str() == ObjectLockRetentionMode::COMPLIANCE || r.as_str() == ObjectLockRetentionMode::GOVERNANCE) => { let t = objectlock::utc_now_ntp(); if OffsetDateTime::from(ret.retain_until_date.expect("err!")).unix_timestamp() > t.unix_timestamp() { return true; } } _ => (), } false }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/tagging/mod.rs
crates/ecstore/src/bucket/tagging/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::collections::HashMap; use s3s::dto::Tag; use url::form_urlencoded; pub fn decode_tags(tags: &str) -> Vec<Tag> { let values = form_urlencoded::parse(tags.as_bytes()); let mut list = Vec::new(); for (k, v) in values { if k.is_empty() || v.is_empty() { continue; } list.push(Tag { key: Some(k.to_string()), value: Some(v.to_string()), }); } list } pub fn decode_tags_to_map(tags: &str) -> HashMap<String, String> { let mut list = HashMap::new(); for (k, v) in form_urlencoded::parse(tags.as_bytes()) { if k.is_empty() || v.is_empty() { continue; } list.insert(k.to_string(), v.to_string()); } list } pub fn encode_tags(tags: Vec<Tag>) -> String { let mut encoded = form_urlencoded::Serializer::new(String::new()); for tag in tags.iter() { if let (Some(k), Some(v)) = (tag.key.as_ref(), tag.value.as_ref()) { //encoded.append_pair(k.as_ref().unwrap().as_str(), v.as_ref().unwrap().as_str()); encoded.append_pair(k.as_str(), v.as_str()); } } encoded.finish() }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/target/bucket_target.rs
crates/ecstore/src/bucket/target/bucket_target.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::error::{Error, Result}; use rmp_serde::Serializer as rmpSerializer; use serde::{Deserialize, Serialize}; use std::{ fmt::{self, Display}, str::FromStr, time::Duration, }; use time::OffsetDateTime; use url::Url; #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct Credentials { #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub session_token: Option<String>, pub expiration: Option<chrono::DateTime<chrono::Utc>>, } #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub enum ServiceType { #[default] Replication, } #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct LatencyStat { #[serde(with = "duration_milliseconds")] pub curr: Duration, // Current latency #[serde(with = "duration_milliseconds")] pub avg: Duration, // Average latency #[serde(with = "duration_milliseconds")] pub max: Duration, // Maximum latency } mod duration_milliseconds { use serde::{Deserialize, Deserializer, Serializer}; use std::time::Duration; pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { serializer.serialize_u64(duration.as_millis() as u64) } pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error> where D: Deserializer<'de>, { let millis = u64::deserialize(deserializer)?; Ok(Duration::from_millis(millis)) } } mod duration_seconds { use serde::{Deserialize, Deserializer, Serializer}; use std::time::Duration; pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { serializer.serialize_u64(duration.as_secs()) } pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error> where D: Deserializer<'de>, { let secs = u64::deserialize(deserializer)?; Ok(Duration::from_secs(secs)) } } #[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)] pub enum BucketTargetType { #[default] None, #[serde(rename = "replication")] ReplicationService, #[serde(rename = "ilm")] IlmService, } impl BucketTargetType { pub fn is_valid(&self) -> bool { match self { BucketTargetType::None => false, BucketTargetType::ReplicationService | BucketTargetType::IlmService => true, } } } impl FromStr for BucketTargetType { type Err = std::io::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { match s { "replication" => Ok(BucketTargetType::ReplicationService), "ilm" => Ok(BucketTargetType::IlmService), _ => Ok(BucketTargetType::None), } } } impl fmt::Display for BucketTargetType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { BucketTargetType::None => write!(f, ""), BucketTargetType::ReplicationService => write!(f, "replication"), BucketTargetType::IlmService => write!(f, "ilm"), } } } // Define BucketTarget structure #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct BucketTarget { #[serde(rename = "sourcebucket", default)] pub source_bucket: String, #[serde(default)] pub endpoint: String, #[serde(default)] pub credentials: Option<Credentials>, #[serde(rename = "targetbucket", default)] pub target_bucket: String, #[serde(default)] pub secure: bool, #[serde(default)] pub path: String, #[serde(default)] pub api: String, #[serde(default)] pub arn: String, #[serde(rename = "type", default)] pub target_type: BucketTargetType, #[serde(default)] pub region: String, #[serde(alias = "bandwidth", default)] pub bandwidth_limit: i64, #[serde(rename = "replicationSync", default)] pub replication_sync: bool, #[serde(default)] pub storage_class: String, #[serde(rename = "healthCheckDuration", with = "duration_seconds", default)] pub health_check_duration: Duration, #[serde(rename = "disableProxy", default)] pub disable_proxy: bool, #[serde(rename = "resetBeforeDate", with = "time::serde::rfc3339::option", default)] pub reset_before_date: Option<OffsetDateTime>, #[serde(default)] pub reset_id: String, #[serde(rename = "totalDowntime", with = "duration_seconds", default)] pub total_downtime: Duration, #[serde(rename = "lastOnline", with = "time::serde::rfc3339::option", default)] pub last_online: Option<OffsetDateTime>, #[serde(rename = "isOnline", default)] pub online: bool, #[serde(default)] pub latency: LatencyStat, #[serde(default)] pub deployment_id: String, #[serde(default)] pub edge: bool, #[serde(rename = "edgeSyncBeforeExpiry", default)] pub edge_sync_before_expiry: bool, #[serde(rename = "offlineCount", default)] pub offline_count: u64, } impl BucketTarget { pub fn is_empty(self) -> bool { self.target_bucket.is_empty() && self.endpoint.is_empty() && self.arn.is_empty() } pub fn url(&self) -> Result<Url> { let scheme = if self.secure { "https" } else { "http" }; Url::parse(&format!("{}://{}", scheme, self.endpoint)).map_err(Error::other) } } impl Display for BucketTarget { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} ", self.endpoint)?; write!(f, "{}", self.target_bucket.clone())?; Ok(()) } } #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct BucketTargets { pub targets: Vec<BucketTarget>, } impl BucketTargets { pub fn marshal_msg(&self) -> Result<Vec<u8>> { let mut buf = Vec::new(); self.serialize(&mut rmpSerializer::new(&mut buf).with_struct_map())?; Ok(buf) } pub fn unmarshal(buf: &[u8]) -> Result<Self> { let t: BucketTargets = rmp_serde::from_slice(buf)?; Ok(t) } pub fn is_empty(&self) -> bool { if self.targets.is_empty() { return true; } for target in &self.targets { if !target.clone().is_empty() { return false; } } true } } #[cfg(test)] mod tests { use super::*; use serde_json; use std::time::Duration; use time::OffsetDateTime; #[test] fn test_bucket_target_json_deserialize() { let json = r#" { "sourcebucket": "source-bucket-name", "endpoint": "s3.amazonaws.com", "credentials": { "accessKey": "test-access-key", "secretKey": "test-secret-key", "session_token": "test-session-token", "expiration": "2024-12-31T23:59:59Z" }, "targetbucket": "target-bucket-name", "secure": true, "path": "/api/v1", "api": "s3v4", "arn": "arn:aws:s3:::target-bucket-name", "type": "replication", "region": "us-east-1", "bandwidth_limit": 1000000, "replicationSync": true, "storage_class": "STANDARD", "healthCheckDuration": 30, "disableProxy": false, "resetBeforeDate": null, "reset_id": "reset-123", "totalDowntime": 3600, "last_online": null, "isOnline": true, "latency": { "curr": 100, "avg": 150, "max": 300 }, "deployment_id": "deployment-456", "edge": false, "edgeSyncBeforeExpiry": true, "offlineCount": 5 } "#; let result: std::result::Result<BucketTarget, _> = serde_json::from_str(json); assert!(result.is_ok(), "Failed to deserialize BucketTarget: {:?}", result.err()); let target = result.unwrap(); // Verify basic fields assert_eq!(target.source_bucket, "source-bucket-name"); assert_eq!(target.endpoint, "s3.amazonaws.com"); assert_eq!(target.target_bucket, "target-bucket-name"); assert!(target.secure); assert_eq!(target.path, "/api/v1"); assert_eq!(target.api, "s3v4"); assert_eq!(target.arn, "arn:aws:s3:::target-bucket-name"); assert_eq!(target.target_type, BucketTargetType::ReplicationService); assert_eq!(target.region, "us-east-1"); assert_eq!(target.bandwidth_limit, 1000000); assert!(target.replication_sync); assert_eq!(target.storage_class, "STANDARD"); assert_eq!(target.health_check_duration, Duration::from_secs(30)); assert!(!target.disable_proxy); assert_eq!(target.reset_id, "reset-123"); assert_eq!(target.total_downtime, Duration::from_secs(3600)); assert!(target.online); assert_eq!(target.deployment_id, "deployment-456"); assert!(!target.edge); assert!(target.edge_sync_before_expiry); assert_eq!(target.offline_count, 5); // Verify credentials assert!(target.credentials.is_some()); let credentials = target.credentials.unwrap(); assert_eq!(credentials.access_key, "test-access-key"); assert_eq!(credentials.secret_key, "test-secret-key"); assert_eq!(credentials.session_token, Some("test-session-token".to_string())); assert!(credentials.expiration.is_some()); // Verify latency statistics assert_eq!(target.latency.curr, Duration::from_millis(100)); assert_eq!(target.latency.avg, Duration::from_millis(150)); assert_eq!(target.latency.max, Duration::from_millis(300)); // Verify time fields assert!(target.reset_before_date.is_none()); assert!(target.last_online.is_none()); } #[test] fn test_bucket_target_json_serialize_deserialize_roundtrip() { let original = BucketTarget { source_bucket: "test-source".to_string(), endpoint: "rustfs.example.com".to_string(), credentials: Some(Credentials { access_key: "rustfsaccess".to_string(), secret_key: "rustfssecret".to_string(), session_token: None, expiration: None, }), target_bucket: "test-target".to_string(), secure: false, path: "/".to_string(), api: "s3v4".to_string(), arn: "arn:rustfs:s3:::test-target".to_string(), target_type: BucketTargetType::ReplicationService, region: "us-west-2".to_string(), bandwidth_limit: 500000, replication_sync: false, storage_class: "REDUCED_REDUNDANCY".to_string(), health_check_duration: Duration::from_secs(60), disable_proxy: true, reset_before_date: Some(OffsetDateTime::now_utc()), reset_id: "reset-456".to_string(), total_downtime: Duration::from_secs(1800), last_online: Some(OffsetDateTime::now_utc()), online: false, latency: LatencyStat { curr: Duration::from_millis(250), avg: Duration::from_millis(200), max: Duration::from_millis(500), }, deployment_id: "deploy-789".to_string(), edge: true, edge_sync_before_expiry: false, offline_count: 10, }; // Serialize to JSON let json = serde_json::to_string(&original).expect("Failed to serialize to JSON"); // Deserialize from JSON let deserialized: BucketTarget = serde_json::from_str(&json).expect("Failed to deserialize from JSON"); // Verify key fields are equal assert_eq!(original.source_bucket, deserialized.source_bucket); assert_eq!(original.endpoint, deserialized.endpoint); assert_eq!(original.target_bucket, deserialized.target_bucket); assert_eq!(original.secure, deserialized.secure); assert_eq!(original.target_type, deserialized.target_type); assert_eq!(original.region, deserialized.region); assert_eq!(original.bandwidth_limit, deserialized.bandwidth_limit); assert_eq!(original.replication_sync, deserialized.replication_sync); assert_eq!(original.health_check_duration, deserialized.health_check_duration); assert_eq!(original.online, deserialized.online); assert_eq!(original.edge, deserialized.edge); assert_eq!(original.offline_count, deserialized.offline_count); } #[test] fn test_bucket_target_type_json_deserialize() { // Test BucketTargetType JSON deserialization let replication_json = r#""replication""#; let ilm_json = r#""ilm""#; let replication_type: BucketTargetType = serde_json::from_str(replication_json).expect("Failed to deserialize replication type"); let ilm_type: BucketTargetType = serde_json::from_str(ilm_json).expect("Failed to deserialize ilm type"); assert_eq!(replication_type, BucketTargetType::ReplicationService); assert_eq!(ilm_type, BucketTargetType::IlmService); // Verify type validity assert!(replication_type.is_valid()); assert!(ilm_type.is_valid()); assert!(!BucketTargetType::None.is_valid()); } #[test] fn test_credentials_json_deserialize() { let json = r#" { "accessKey": "AKIAIOSFODNN7EXAMPLE", "secretKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "session_token": "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT", "expiration": "2024-12-31T23:59:59Z" } "#; let credentials: Credentials = serde_json::from_str(json).expect("Failed to deserialize credentials"); assert_eq!(credentials.access_key, "AKIAIOSFODNN7EXAMPLE"); assert_eq!(credentials.secret_key, "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); assert_eq!( credentials.session_token, Some("AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT".to_string()) ); assert!(credentials.expiration.is_some()); } #[test] fn test_latency_stat_json_deserialize() { let json = r#" { "curr": 50, "avg": 75, "max": 200 } "#; let latency: LatencyStat = serde_json::from_str(json).expect("Failed to deserialize latency stat"); assert_eq!(latency.curr, Duration::from_millis(50)); assert_eq!(latency.avg, Duration::from_millis(75)); assert_eq!(latency.max, Duration::from_millis(200)); } #[test] fn test_bucket_targets_json_deserialize() { let json = r#" { "targets": [ { "sourcebucket": "bucket1", "endpoint": "s3.amazonaws.com", "targetbucket": "target1", "secure": true, "path": "/", "api": "s3v4", "arn": "arn:aws:s3:::target1", "type": "replication", "region": "us-east-1", "bandwidth_limit": 0, "replicationSync": false, "storage_class": "", "healthCheckDuration": 0, "disableProxy": false, "resetBeforeDate": null, "reset_id": "", "totalDowntime": 0, "lastOnline": null, "isOnline": false, "latency": { "curr": 0, "avg": 0, "max": 0 }, "deployment_id": "", "edge": false, "edgeSyncBeforeExpiry": false, "offlineCount": 0 } ] } "#; let targets: BucketTargets = serde_json::from_str(json).expect("Failed to deserialize bucket targets"); assert_eq!(targets.targets.len(), 1); assert_eq!(targets.targets[0].source_bucket, "bucket1"); assert_eq!(targets.targets[0].endpoint, "s3.amazonaws.com"); assert_eq!(targets.targets[0].target_bucket, "target1"); assert!(!targets.is_empty()); } #[test] fn test_user_provided_json_deserialize() { // Test the specific JSON provided by the user with missing required fields added let json = r#" { "sourcebucket": "mc-test-bucket-22139", "endpoint": "localhost:8000", "credentials": { "accessKey": "rustfsadmin", "secretKey": "rustfsadmin", "expiration": "0001-01-01T00:00:00Z" }, "targetbucket": "test", "secure": false, "path": "auto", "api": "s3v4", "type": "replication", "replicationSync": false, "healthCheckDuration": 60, "disableProxy": false, "resetBeforeDate": "0001-01-01T00:00:00Z", "totalDowntime": 0, "lastOnline": "0001-01-01T00:00:00Z", "isOnline": false, "latency": { "curr": 0, "avg": 0, "max": 0 }, "deployment_id": "", "edge": false, "edgeSyncBeforeExpiry": false, "offlineCount": 0, "bandwidth": 107374182400 } "#; let target: BucketTarget = serde_json::from_str(json).expect("Failed to deserialize user provided JSON to BucketTarget"); // Verify the deserialized values match the original JSON assert_eq!(target.source_bucket, "mc-test-bucket-22139"); assert_eq!(target.endpoint, "localhost:8000"); assert_eq!(target.target_bucket, "test"); assert!(!target.secure); assert_eq!(target.path, "auto"); assert_eq!(target.api, "s3v4"); assert_eq!(target.target_type, BucketTargetType::ReplicationService); assert!(!target.replication_sync); assert_eq!(target.health_check_duration, Duration::from_secs(60)); assert!(!target.disable_proxy); assert!(!target.online); assert!(!target.edge); assert!(!target.edge_sync_before_expiry); assert_eq!(target.bandwidth_limit, 107374182400); // bandwidth field mapped to bandwidth_limit // Verify credentials assert!(target.credentials.is_some()); let credentials = target.credentials.unwrap(); assert_eq!(credentials.access_key, "rustfsadmin"); assert_eq!(credentials.secret_key, "rustfsadmin"); // Verify latency statistics assert_eq!(target.latency.curr, Duration::from_millis(0)); assert_eq!(target.latency.avg, Duration::from_millis(0)); assert_eq!(target.latency.max, Duration::from_millis(0)); // Verify time fields parsing (should handle "0001-01-01T00:00:00Z" as None due to being the zero time) assert!(target.reset_before_date.is_some()); assert!(target.last_online.is_some()); println!("βœ… User provided JSON successfully deserialized to BucketTarget"); } #[test] fn test_user_provided_json_as_bucket_targets() { // Test wrapping the user JSON in BucketTargets structure let json = r#" { "targets": [ { "sourcebucket": "mc-test-bucket-22139", "endpoint": "localhost:8000", "credentials": { "accessKey": "rustfsadmin", "secretKey": "rustfsadmin", "expiration": "0001-01-01T00:00:00Z" }, "targetbucket": "test", "secure": false, "path": "auto", "api": "s3v4", "arn": "", "type": "replication", "region": "", "replicationSync": false, "storage_class": "", "healthCheckDuration": 60, "disableProxy": false, "resetBeforeDate": "0001-01-01T00:00:00Z", "reset_id": "", "totalDowntime": 0, "lastOnline": "0001-01-01T00:00:00Z", "isOnline": false, "latency": { "curr": 0, "avg": 0, "max": 0 }, "deployment_id": "", "edge": false, "edgeSyncBeforeExpiry": false, "offlineCount": 0, "bandwidth": 107374182400 } ] } "#; let bucket_targets: BucketTargets = serde_json::from_str(json).expect("Failed to deserialize user provided JSON to BucketTargets"); assert_eq!(bucket_targets.targets.len(), 1); assert!(!bucket_targets.is_empty()); let target = &bucket_targets.targets[0]; assert_eq!(target.source_bucket, "mc-test-bucket-22139"); assert_eq!(target.endpoint, "localhost:8000"); assert_eq!(target.target_bucket, "test"); assert_eq!(target.bandwidth_limit, 107374182400); println!("βœ… User provided JSON successfully deserialized to BucketTargets"); } #[test] fn test_bucket_target_minimal_json_with_defaults() { // Test that BucketTarget can be deserialized with minimal JSON using defaults let minimal_json = r#" { "sourcebucket": "test-source", "endpoint": "localhost:9000", "targetbucket": "test-target" } "#; let target: BucketTarget = serde_json::from_str(minimal_json).expect("Failed to deserialize minimal JSON to BucketTarget"); // Verify required fields assert_eq!(target.source_bucket, "test-source"); assert_eq!(target.endpoint, "localhost:9000"); assert_eq!(target.target_bucket, "test-target"); // Verify default values assert!(!target.secure); // bool default is false assert_eq!(target.path, ""); // String default is empty assert_eq!(target.api, ""); // String default is empty assert_eq!(target.arn, ""); // String default is empty assert_eq!(target.target_type, BucketTargetType::None); // enum default assert_eq!(target.region, ""); // String default is empty assert_eq!(target.bandwidth_limit, 0); // i64 default is 0 assert!(!target.replication_sync); // bool default is false assert_eq!(target.storage_class, ""); // String default is empty assert_eq!(target.health_check_duration, Duration::from_secs(0)); // Duration default assert!(!target.disable_proxy); // bool default is false assert!(target.reset_before_date.is_none()); // Option default is None assert_eq!(target.reset_id, ""); // String default is empty assert_eq!(target.total_downtime, Duration::from_secs(0)); // Duration default assert!(target.last_online.is_none()); // Option default is None assert!(!target.online); // bool default is false assert_eq!(target.latency.curr, Duration::from_millis(0)); // LatencyStat default assert_eq!(target.latency.avg, Duration::from_millis(0)); assert_eq!(target.latency.max, Duration::from_millis(0)); assert_eq!(target.deployment_id, ""); // String default is empty assert!(!target.edge); // bool default is false assert!(!target.edge_sync_before_expiry); // bool default is false assert_eq!(target.offline_count, 0); // u64 default is 0 assert!(target.credentials.is_none()); // Option default is None println!("βœ… Minimal JSON with defaults successfully deserialized to BucketTarget"); } #[test] fn test_bucket_target_empty_json_with_defaults() { // Test that BucketTarget can be deserialized with completely empty JSON using all defaults let empty_json = r#"{}"#; let target: BucketTarget = serde_json::from_str(empty_json).expect("Failed to deserialize empty JSON to BucketTarget"); // Verify all fields use default values assert_eq!(target.source_bucket, ""); assert_eq!(target.endpoint, ""); assert_eq!(target.target_bucket, ""); assert!(!target.secure); assert_eq!(target.path, ""); assert_eq!(target.api, ""); assert_eq!(target.arn, ""); assert_eq!(target.target_type, BucketTargetType::None); assert_eq!(target.region, ""); assert_eq!(target.bandwidth_limit, 0); assert!(!target.replication_sync); assert_eq!(target.storage_class, ""); assert_eq!(target.health_check_duration, Duration::from_secs(0)); assert!(!target.disable_proxy); assert!(target.reset_before_date.is_none()); assert_eq!(target.reset_id, ""); assert_eq!(target.total_downtime, Duration::from_secs(0)); assert!(target.last_online.is_none()); assert!(!target.online); assert_eq!(target.latency.curr, Duration::from_millis(0)); assert_eq!(target.latency.avg, Duration::from_millis(0)); assert_eq!(target.latency.max, Duration::from_millis(0)); assert_eq!(target.deployment_id, ""); assert!(!target.edge); assert!(!target.edge_sync_before_expiry); assert_eq!(target.offline_count, 0); assert!(target.credentials.is_none()); println!("βœ… Empty JSON with all defaults successfully deserialized to BucketTarget"); } #[test] fn test_original_user_json_with_defaults() { // Test the original user JSON without extra required fields let json = r#" { "sourcebucket": "mc-test-bucket-22139", "endpoint": "localhost:8000", "credentials": { "accessKey": "rustfsadmin", "secretKey": "rustfsadmin", "expiration": "0001-01-01T00:00:00Z" }, "targetbucket": "test", "secure": false, "path": "auto", "api": "s3v4", "type": "replication", "replicationSync": false, "healthCheckDuration": 60, "disableProxy": false, "resetBeforeDate": "0001-01-01T00:00:00Z", "totalDowntime": 0, "lastOnline": "0001-01-01T00:00:00Z", "isOnline": false, "latency": { "curr": 0, "avg": 0, "max": 0 }, "edge": false, "edgeSyncBeforeExpiry": false, "bandwidth": 107374182400 } "#; let target: BucketTarget = serde_json::from_str(json).expect("Failed to deserialize original user JSON to BucketTarget"); // Verify the deserialized values assert_eq!(target.source_bucket, "mc-test-bucket-22139"); assert_eq!(target.endpoint, "localhost:8000"); assert_eq!(target.target_bucket, "test"); assert!(!target.secure); assert_eq!(target.path, "auto"); assert_eq!(target.api, "s3v4"); assert_eq!(target.target_type, BucketTargetType::ReplicationService); assert!(!target.replication_sync); assert_eq!(target.health_check_duration, Duration::from_secs(60)); assert!(!target.disable_proxy); assert!(!target.online); assert!(!target.edge); assert!(!target.edge_sync_before_expiry); assert_eq!(target.bandwidth_limit, 107374182400); // Fields not specified should use defaults assert_eq!(target.arn, ""); // default empty string assert_eq!(target.region, ""); // default empty string assert_eq!(target.storage_class, ""); // default empty string assert_eq!(target.reset_id, ""); // default empty string assert_eq!(target.deployment_id, ""); // default empty string assert_eq!(target.offline_count, 0); // default u64 // Verify credentials assert!(target.credentials.is_some()); let credentials = target.credentials.unwrap(); assert_eq!(credentials.access_key, "rustfsadmin"); assert_eq!(credentials.secret_key, "rustfsadmin"); println!("βœ… Original user JSON with defaults successfully deserialized to BucketTarget"); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/target/mod.rs
crates/ecstore/src/bucket/target/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. mod arn; mod bucket_target; pub use arn::*; pub use bucket_target::*;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/target/arn.rs
crates/ecstore/src/bucket/target/arn.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use super::BucketTargetType; use std::fmt::Display; use std::str::FromStr; pub struct ARN { pub arn_type: BucketTargetType, pub id: String, pub region: String, pub bucket: String, } impl ARN { pub fn new(arn_type: BucketTargetType, id: String, region: String, bucket: String) -> Self { Self { arn_type, id, region, bucket, } } pub fn is_empty(&self) -> bool { self.arn_type.is_valid() } } impl Display for ARN { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "arn:rustfs:{}:{}:{}:{}", self.arn_type, self.region, self.id, self.bucket) } } impl FromStr for ARN { type Err = std::io::Error; fn from_str(s: &str) -> Result<Self, Self::Err> { if !s.starts_with("arn:rustfs:") { return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid ARN format")); } let parts: Vec<&str> = s.split(':').collect(); if parts.len() != 6 { return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid ARN format")); } Ok(ARN { arn_type: BucketTargetType::from_str(parts[2]).unwrap_or_default(), id: parts[3].to_string(), region: parts[4].to_string(), bucket: parts[5].to_string(), }) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/config.rs
crates/ecstore/src/bucket/replication/config.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use super::ReplicationRuleExt as _; use crate::bucket::tagging::decode_tags_to_map; use rustfs_filemeta::ReplicationType; use s3s::dto::DeleteMarkerReplicationStatus; use s3s::dto::DeleteReplicationStatus; use s3s::dto::Destination; use s3s::dto::{ExistingObjectReplicationStatus, ReplicationConfiguration, ReplicationRuleStatus, ReplicationRules}; use serde::{Deserialize, Serialize}; use std::collections::HashSet; use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct ObjectOpts { pub name: String, pub user_tags: String, pub version_id: Option<Uuid>, pub delete_marker: bool, pub ssec: bool, pub op_type: ReplicationType, pub replica: bool, pub existing_object: bool, pub target_arn: String, } pub trait ReplicationConfigurationExt { fn replicate(&self, opts: &ObjectOpts) -> bool; fn has_existing_object_replication(&self, arn: &str) -> (bool, bool); fn filter_actionable_rules(&self, obj: &ObjectOpts) -> ReplicationRules; fn get_destination(&self) -> Destination; fn has_active_rules(&self, prefix: &str, recursive: bool) -> bool; fn filter_target_arns(&self, obj: &ObjectOpts) -> Vec<String>; } impl ReplicationConfigurationExt for ReplicationConfiguration { /// Check whether any object-replication rules exist fn has_existing_object_replication(&self, arn: &str) -> (bool, bool) { let mut has_arn = false; for rule in &self.rules { if rule.destination.bucket == arn || self.role == arn { if !has_arn { has_arn = true; } if let Some(status) = &rule.existing_object_replication && status.status == ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::ENABLED) { return (true, true); } } } (has_arn, false) } fn filter_actionable_rules(&self, obj: &ObjectOpts) -> ReplicationRules { if obj.name.is_empty() && obj.op_type != ReplicationType::Resync && obj.op_type != ReplicationType::All { return vec![]; } let mut rules = ReplicationRules::default(); for rule in &self.rules { if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) { continue; } if !obj.target_arn.is_empty() && rule.destination.bucket != obj.target_arn && self.role != obj.target_arn { continue; } if obj.op_type == ReplicationType::Resync || obj.op_type == ReplicationType::All { rules.push(rule.clone()); continue; } if let Some(status) = &rule.existing_object_replication && obj.existing_object && status.status == ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::DISABLED) { continue; } if !obj.name.starts_with(rule.prefix()) { continue; } if let Some(filter) = &rule.filter { let object_tags = decode_tags_to_map(&obj.user_tags); if filter.test_tags(&object_tags) { rules.push(rule.clone()); } } else { rules.push(rule.clone()); } } rules.sort_by(|a, b| { if a.destination == b.destination { a.priority.cmp(&b.priority) } else { std::cmp::Ordering::Equal } }); rules } /// Retrieve the destination configuration fn get_destination(&self) -> Destination { if !self.rules.is_empty() { self.rules[0].destination.clone() } else { Destination { account: None, bucket: "".to_string(), encryption_configuration: None, metrics: None, replication_time: None, access_control_translation: None, storage_class: None, } } } /// Determine whether an object should be replicated fn replicate(&self, obj: &ObjectOpts) -> bool { let rules = self.filter_actionable_rules(obj); for rule in rules.iter() { if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) { continue; } if let Some(status) = &rule.existing_object_replication && obj.existing_object && status.status == ExistingObjectReplicationStatus::from_static(ExistingObjectReplicationStatus::DISABLED) { return false; } if obj.op_type == ReplicationType::Delete { if obj.version_id.is_some() { return rule .delete_replication .clone() .is_some_and(|d| d.status == DeleteReplicationStatus::from_static(DeleteReplicationStatus::ENABLED)); } else { return rule.delete_marker_replication.clone().is_some_and(|d| { d.status == Some(DeleteMarkerReplicationStatus::from_static(DeleteMarkerReplicationStatus::ENABLED)) }); } } // Regular object/metadata replication return rule.metadata_replicate(obj); } false } /// Check for an active rule /// Optionally accept a prefix /// When recursive is true, return true if any level under the prefix has an active rule /// Without a prefix, recursive behaves as true fn has_active_rules(&self, prefix: &str, recursive: bool) -> bool { if self.rules.is_empty() { return false; } for rule in &self.rules { if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) { continue; } if let Some(filter) = &rule.filter && let Some(filter_prefix) = &filter.prefix { if !prefix.is_empty() && !filter_prefix.is_empty() { // The provided prefix must fall within the rule prefix if !recursive && !prefix.starts_with(filter_prefix) { continue; } } // When recursive, skip this rule if it does not match the test prefix or hierarchy if recursive && !rule.prefix().starts_with(prefix) && !prefix.starts_with(rule.prefix()) { continue; } } return true; } false } /// Filter target ARNs and return a slice of the distinct values in the config fn filter_target_arns(&self, obj: &ObjectOpts) -> Vec<String> { let mut arns = Vec::new(); let mut targets_map: HashSet<String> = HashSet::new(); let rules = self.filter_actionable_rules(obj); for rule in rules { if rule.status == ReplicationRuleStatus::from_static(ReplicationRuleStatus::DISABLED) { continue; } if !self.role.is_empty() { arns.push(self.role.clone()); // Use the legacy RoleArn when present return arns; } if !targets_map.contains(&rule.destination.bucket) { targets_map.insert(rule.destination.bucket.clone()); } } for arn in targets_map { arns.push(arn); } arns } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/replication_resyncer.rs
crates/ecstore/src/bucket/replication/replication_resyncer.rs
use crate::bucket::bucket_target_sys::{ AdvancedPutOptions, BucketTargetSys, PutObjectOptions, PutObjectPartOptions, RemoveObjectOptions, TargetClient, }; use crate::bucket::metadata_sys; use crate::bucket::replication::ResyncStatusType; use crate::bucket::replication::{ObjectOpts, ReplicationConfigurationExt as _}; use crate::bucket::tagging::decode_tags_to_map; use crate::bucket::target::BucketTargets; use crate::bucket::versioning_sys::BucketVersioningSys; use crate::client::api_get_options::{AdvancedGetOptions, StatObjectOptions}; use crate::config::com::save_config; use crate::disk::BUCKET_META_PREFIX; use crate::error::{Error, Result, is_err_object_not_found, is_err_version_not_found}; use crate::event::name::EventName; use crate::event_notification::{EventArgs, send_event}; use crate::global::GLOBAL_LocalNodeName; use crate::store_api::{DeletedObject, ObjectInfo, ObjectOptions, ObjectToDelete, WalkOptions}; use crate::{StorageAPI, new_object_layer_fn}; use aws_sdk_s3::error::SdkError; use aws_sdk_s3::operation::head_object::HeadObjectOutput; use aws_sdk_s3::primitives::ByteStream; use aws_sdk_s3::types::{CompletedPart, ObjectLockLegalHoldStatus}; use byteorder::ByteOrder; use futures::future::join_all; use http::HeaderMap; use regex::Regex; use rustfs_filemeta::{ MrfReplicateEntry, REPLICATE_EXISTING, REPLICATE_EXISTING_DELETE, REPLICATION_RESET, ReplicateDecision, ReplicateObjectInfo, ReplicateTargetDecision, ReplicatedInfos, ReplicatedTargetInfo, ReplicationAction, ReplicationState, ReplicationStatusType, ReplicationType, ReplicationWorkerOperation, ResyncDecision, ResyncTargetDecision, VersionPurgeStatusType, get_replication_state, parse_replicate_decision, replication_statuses_map, target_reset_header, version_purge_statuses_map, }; use rustfs_utils::http::{ AMZ_BUCKET_REPLICATION_STATUS, AMZ_OBJECT_TAGGING, AMZ_TAGGING_DIRECTIVE, CONTENT_ENCODING, HeaderExt as _, RESERVED_METADATA_PREFIX, RESERVED_METADATA_PREFIX_LOWER, RUSTFS_REPLICATION_ACTUAL_OBJECT_SIZE, RUSTFS_REPLICATION_RESET_STATUS, SSEC_ALGORITHM_HEADER, SSEC_KEY_HEADER, SSEC_KEY_MD5_HEADER, headers, }; use rustfs_utils::path::path_join_buf; use rustfs_utils::string::strings_has_prefix_fold; use rustfs_utils::{DEFAULT_SIP_HASH_KEY, sip_hash}; use s3s::dto::ReplicationConfiguration; use serde::Deserialize; use serde::Serialize; use std::any::Any; use std::collections::HashMap; use std::sync::Arc; use time::OffsetDateTime; use time::format_description::well_known::Rfc3339; use tokio::io::{AsyncRead, AsyncReadExt}; use tokio::sync::RwLock; use tokio::task::JoinSet; use tokio::time::Duration as TokioDuration; use tokio_util::sync::CancellationToken; use tracing::{error, info, warn}; const REPLICATION_DIR: &str = ".replication"; const RESYNC_FILE_NAME: &str = "resync.bin"; const RESYNC_META_FORMAT: u16 = 1; const RESYNC_META_VERSION: u16 = 1; const RESYNC_TIME_INTERVAL: TokioDuration = TokioDuration::from_secs(60); #[derive(Debug, Clone, Default)] pub struct ResyncOpts { pub bucket: String, pub arn: String, pub resync_id: String, pub resync_before: Option<OffsetDateTime>, } #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct TargetReplicationResyncStatus { pub start_time: Option<OffsetDateTime>, pub last_update: Option<OffsetDateTime>, pub resync_id: String, pub resync_before_date: Option<OffsetDateTime>, pub resync_status: ResyncStatusType, pub failed_size: i64, pub failed_count: i64, pub replicated_size: i64, pub replicated_count: i64, pub bucket: String, pub object: String, pub error: Option<String>, } impl TargetReplicationResyncStatus { pub fn new() -> Self { Self::default() } } #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct BucketReplicationResyncStatus { pub version: u16, pub targets_map: HashMap<String, TargetReplicationResyncStatus>, pub id: i32, pub last_update: Option<OffsetDateTime>, } impl BucketReplicationResyncStatus { pub fn new() -> Self { Self { version: RESYNC_META_VERSION, ..Default::default() } } pub fn clone_tgt_stats(&self) -> HashMap<String, TargetReplicationResyncStatus> { self.targets_map.clone() } pub fn marshal_msg(&self) -> Result<Vec<u8>> { Ok(rmp_serde::to_vec(&self)?) } pub fn unmarshal_msg(data: &[u8]) -> Result<Self> { Ok(rmp_serde::from_slice(data)?) } } static RESYNC_WORKER_COUNT: usize = 10; #[derive(Debug)] pub struct ReplicationResyncer { pub status_map: Arc<RwLock<HashMap<String, BucketReplicationResyncStatus>>>, pub worker_size: usize, pub resync_cancel_tx: CancellationToken, pub resync_cancel_rx: CancellationToken, pub worker_tx: tokio::sync::broadcast::Sender<()>, pub worker_rx: tokio::sync::broadcast::Receiver<()>, } impl ReplicationResyncer { pub async fn new() -> Self { let resync_cancel_tx = CancellationToken::new(); let resync_cancel_rx = resync_cancel_tx.clone(); let (worker_tx, worker_rx) = tokio::sync::broadcast::channel(RESYNC_WORKER_COUNT); for _ in 0..RESYNC_WORKER_COUNT { if let Err(err) = worker_tx.send(()) { error!("Failed to send worker message: {}", err); } } Self { status_map: Arc::new(RwLock::new(HashMap::new())), worker_size: RESYNC_WORKER_COUNT, resync_cancel_tx, resync_cancel_rx, worker_tx, worker_rx, } } pub async fn mark_status<S: StorageAPI>(&self, status: ResyncStatusType, opts: ResyncOpts, obj_layer: Arc<S>) -> Result<()> { let bucket_status = { let mut status_map = self.status_map.write().await; let bucket_status = if let Some(bucket_status) = status_map.get_mut(&opts.bucket) { bucket_status } else { let mut bucket_status = BucketReplicationResyncStatus::new(); bucket_status.id = 0; status_map.insert(opts.bucket.clone(), bucket_status); status_map.get_mut(&opts.bucket).unwrap() }; let state = if let Some(state) = bucket_status.targets_map.get_mut(&opts.arn) { state } else { let state = TargetReplicationResyncStatus::new(); bucket_status.targets_map.insert(opts.arn.clone(), state); bucket_status.targets_map.get_mut(&opts.arn).unwrap() }; state.resync_status = status; state.last_update = Some(OffsetDateTime::now_utc()); bucket_status.last_update = Some(OffsetDateTime::now_utc()); bucket_status.clone() }; save_resync_status(&opts.bucket, &bucket_status, obj_layer).await?; Ok(()) } pub async fn inc_stats(&self, status: &TargetReplicationResyncStatus, opts: ResyncOpts) { let mut status_map = self.status_map.write().await; let bucket_status = if let Some(bucket_status) = status_map.get_mut(&opts.bucket) { bucket_status } else { let mut bucket_status = BucketReplicationResyncStatus::new(); bucket_status.id = 0; status_map.insert(opts.bucket.clone(), bucket_status); status_map.get_mut(&opts.bucket).unwrap() }; let state = if let Some(state) = bucket_status.targets_map.get_mut(&opts.arn) { state } else { let state = TargetReplicationResyncStatus::new(); bucket_status.targets_map.insert(opts.arn.clone(), state); bucket_status.targets_map.get_mut(&opts.arn).unwrap() }; state.object = status.object.clone(); state.replicated_count += status.replicated_count; state.replicated_size += status.replicated_size; state.failed_count += status.failed_count; state.failed_size += status.failed_size; state.last_update = Some(OffsetDateTime::now_utc()); bucket_status.last_update = Some(OffsetDateTime::now_utc()); } pub async fn persist_to_disk<S: StorageAPI>(&self, cancel_token: CancellationToken, api: Arc<S>) { let mut interval = tokio::time::interval(RESYNC_TIME_INTERVAL); let mut last_update_times = HashMap::new(); loop { tokio::select! { _ = cancel_token.cancelled() => { return; } _ = interval.tick() => { let status_map = self.status_map.read().await; let mut update = false; for (bucket, status) in status_map.iter() { for target in status.targets_map.values() { if target.last_update.is_none() { update = true; break; } } if let Some(last_update) = status.last_update && last_update > *last_update_times.get(bucket).unwrap_or(&OffsetDateTime::UNIX_EPOCH) { update = true; } if update { if let Err(err) = save_resync_status(bucket, status, api.clone()).await { error!("Failed to save resync status: {}", err); } else { last_update_times.insert(bucket.clone(), status.last_update.unwrap()); } } } interval.reset(); } } } } async fn resync_bucket_mark_status<S: StorageAPI>(&self, status: ResyncStatusType, opts: ResyncOpts, storage: Arc<S>) { if let Err(err) = self.mark_status(status, opts.clone(), storage.clone()).await { error!("Failed to mark resync status: {}", err); } if let Err(err) = self.worker_tx.send(()) { error!("Failed to send worker message: {}", err); } // TODO: Metrics } pub async fn resync_bucket<S: StorageAPI>( self: Arc<Self>, cancellation_token: CancellationToken, storage: Arc<S>, heal: bool, opts: ResyncOpts, ) { let mut worker_rx = self.worker_rx.resubscribe(); tokio::select! { _ = cancellation_token.cancelled() => { return; } _ = worker_rx.recv() => {} } let cfg = match get_replication_config(&opts.bucket).await { Ok(cfg) => cfg, Err(err) => { error!("Failed to get replication config: {}", err); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } }; let targets = match BucketTargetSys::get().list_bucket_targets(&opts.bucket).await { Ok(targets) => targets, Err(err) => { warn!("Failed to list bucket targets: {}", err); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } }; let rcfg = ReplicationConfig::new(cfg.clone(), Some(targets)); let target_arns = if let Some(cfg) = cfg { cfg.filter_target_arns(&ObjectOpts { op_type: ReplicationType::Resync, target_arn: opts.arn.clone(), ..Default::default() }) } else { vec![] }; if target_arns.len() != 1 { error!( "replication resync failed for {} - arn specified {} is missing in the replication config", opts.bucket, opts.arn ); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } let Some(target_client) = BucketTargetSys::get() .get_remote_target_client(&opts.bucket, &target_arns[0]) .await else { error!( "replication resync failed for {} - arn specified {} is missing in the bucket targets", opts.bucket, opts.arn ); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; }; if !heal && let Err(e) = self .mark_status(ResyncStatusType::ResyncStarted, opts.clone(), storage.clone()) .await { error!("Failed to mark resync status: {}", e); } let (tx, mut rx) = tokio::sync::mpsc::channel(100); if let Err(err) = storage .clone() .walk(cancellation_token.clone(), &opts.bucket, "", tx.clone(), WalkOptions::default()) .await { error!("Failed to walk bucket {}: {}", opts.bucket, err); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } let status = { self.status_map .read() .await .get(&opts.bucket) .and_then(|status| status.targets_map.get(&opts.arn)) .cloned() .unwrap_or_default() }; let mut last_checkpoint = if status.resync_status == ResyncStatusType::ResyncStarted || status.resync_status == ResyncStatusType::ResyncFailed { Some(status.object) } else { None }; let mut worker_txs = Vec::new(); let (results_tx, mut results_rx) = tokio::sync::broadcast::channel::<TargetReplicationResyncStatus>(1); let opts_clone = opts.clone(); let self_clone = self.clone(); let mut futures = Vec::new(); let results_fut = tokio::spawn(async move { while let Ok(st) = results_rx.recv().await { self_clone.inc_stats(&st, opts_clone.clone()).await; } }); futures.push(results_fut); for _ in 0..RESYNC_WORKER_COUNT { let (tx, mut rx) = tokio::sync::mpsc::channel::<ReplicateObjectInfo>(100); worker_txs.push(tx); let cancel_token = cancellation_token.clone(); let target_client = target_client.clone(); let resync_cancel_rx = self.resync_cancel_rx.clone(); let storage = storage.clone(); let results_tx = results_tx.clone(); let bucket_name = opts.bucket.clone(); let f = tokio::spawn(async move { while let Some(mut roi) = rx.recv().await { if cancel_token.is_cancelled() { return; } if roi.delete_marker || !roi.version_purge_status.is_empty() { let (version_id, dm_version_id) = if roi.version_purge_status.is_empty() { (None, roi.version_id) } else { (roi.version_id, None) }; let doi = DeletedObjectReplicationInfo { delete_object: DeletedObject { object_name: roi.name.clone(), delete_marker_version_id: dm_version_id, version_id, replication_state: roi.replication_state.clone(), delete_marker: roi.delete_marker, delete_marker_mtime: roi.mod_time, ..Default::default() }, bucket: roi.bucket.clone(), event_type: REPLICATE_EXISTING_DELETE.to_string(), op_type: ReplicationType::ExistingObject, ..Default::default() }; replicate_delete(doi, storage.clone()).await; } else { roi.op_type = ReplicationType::ExistingObject; roi.event_type = REPLICATE_EXISTING.to_string(); replicate_object(roi.clone(), storage.clone()).await; } let mut st = TargetReplicationResyncStatus { object: roi.name.clone(), bucket: roi.bucket.clone(), ..Default::default() }; let reset_id = target_client.reset_id.clone(); let (size, err) = if let Err(err) = target_client .head_object(&target_client.bucket, &roi.name, roi.version_id.map(|v| v.to_string())) .await { if roi.delete_marker { st.replicated_count += 1; } else { st.failed_count += 1; } (0, Some(err)) } else { st.replicated_count += 1; st.replicated_size += roi.size; (roi.size, None) }; info!( "resynced reset_id:{} object: {}/{}-{} size:{} err:{:?}", reset_id, bucket_name, roi.name, roi.version_id.unwrap_or_default(), size, err, ); if resync_cancel_rx.is_cancelled() { return; } if cancel_token.is_cancelled() { return; } if let Err(err) = results_tx.send(st) { error!("Failed to send resync status: {}", err); } } }); futures.push(f); } let resync_cancel_rx = self.resync_cancel_rx.clone(); while let Some(res) = rx.recv().await { if let Some(err) = res.err { error!("Failed to get object info: {}", err); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } if resync_cancel_rx.is_cancelled() { self.resync_bucket_mark_status(ResyncStatusType::ResyncCanceled, opts.clone(), storage.clone()) .await; return; } if cancellation_token.is_cancelled() { self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } let Some(object) = res.item else { continue; }; if heal && let Some(checkpoint) = &last_checkpoint && &object.name != checkpoint { continue; } last_checkpoint = None; let roi = get_heal_replicate_object_info(&object, &rcfg).await; if !roi.existing_obj_resync.must_resync() { continue; } if resync_cancel_rx.is_cancelled() { self.resync_bucket_mark_status(ResyncStatusType::ResyncCanceled, opts.clone(), storage.clone()) .await; return; } if cancellation_token.is_cancelled() { self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } let worker_idx = sip_hash(&roi.name, RESYNC_WORKER_COUNT, &DEFAULT_SIP_HASH_KEY) as usize; if let Err(err) = worker_txs[worker_idx].send(roi).await { error!("Failed to send object info to worker: {}", err); self.resync_bucket_mark_status(ResyncStatusType::ResyncFailed, opts.clone(), storage.clone()) .await; return; } } for worker_tx in worker_txs { drop(worker_tx); } join_all(futures).await; self.resync_bucket_mark_status(ResyncStatusType::ResyncCompleted, opts.clone(), storage.clone()) .await; } } pub async fn get_heal_replicate_object_info(oi: &ObjectInfo, rcfg: &ReplicationConfig) -> ReplicateObjectInfo { let mut oi = oi.clone(); let mut user_defined = oi.user_defined.clone(); if let Some(rc) = rcfg.config.as_ref() && !rc.role.is_empty() { if !oi.replication_status.is_empty() { oi.replication_status_internal = Some(format!("{}={};", rc.role, oi.replication_status.as_str())); } if !oi.replication_status.is_empty() { oi.replication_status_internal = Some(format!("{}={};", rc.role, oi.replication_status.as_str())); } let keys_to_update: Vec<_> = user_defined .iter() .filter(|(k, _)| k.eq_ignore_ascii_case(format!("{RESERVED_METADATA_PREFIX_LOWER}{REPLICATION_RESET}").as_str())) .map(|(k, v)| (k.clone(), v.clone())) .collect(); for (k, v) in keys_to_update { user_defined.remove(&k); user_defined.insert(target_reset_header(rc.role.as_str()), v); } } let dsc = if oi.delete_marker || !oi.replication_status.is_empty() { check_replicate_delete( oi.bucket.as_str(), &ObjectToDelete { object_name: oi.name.clone(), version_id: oi.version_id, ..Default::default() }, &oi, &ObjectOptions { versioned: BucketVersioningSys::prefix_enabled(&oi.bucket, &oi.name).await, version_suspended: BucketVersioningSys::prefix_suspended(&oi.bucket, &oi.name).await, ..Default::default() }, None, ) .await } else { must_replicate( oi.bucket.as_str(), &oi.name, MustReplicateOptions::new( &user_defined, oi.user_tags.clone(), ReplicationStatusType::Empty, ReplicationType::Heal, ObjectOptions::default(), ), ) .await }; let target_statuses = replication_statuses_map(&oi.replication_status_internal.clone().unwrap_or_default()); let target_purge_statuses = version_purge_statuses_map(&oi.version_purge_status_internal.clone().unwrap_or_default()); let existing_obj_resync = rcfg.resync(oi.clone(), dsc.clone(), &target_statuses).await; let mut replication_state = oi.replication_state(); replication_state.replicate_decision_str = dsc.to_string(); let actual_size = oi.get_actual_size().unwrap_or_default(); ReplicateObjectInfo { name: oi.name.clone(), size: oi.size, actual_size, bucket: oi.bucket.clone(), version_id: oi.version_id, etag: oi.etag.clone(), mod_time: oi.mod_time, replication_status: oi.replication_status, replication_status_internal: oi.replication_status_internal.clone(), delete_marker: oi.delete_marker, version_purge_status_internal: oi.version_purge_status_internal.clone(), version_purge_status: oi.version_purge_status, replication_state: Some(replication_state), op_type: ReplicationType::Heal, event_type: "".to_string(), dsc, existing_obj_resync, target_statuses, target_purge_statuses, replication_timestamp: None, ssec: false, // TODO: add ssec support user_tags: oi.user_tags.clone(), checksum: oi.checksum.clone(), retry_count: 0, } } async fn save_resync_status<S: StorageAPI>(bucket: &str, status: &BucketReplicationResyncStatus, api: Arc<S>) -> Result<()> { let buf = status.marshal_msg()?; let mut data = Vec::new(); let mut major = [0u8; 2]; byteorder::LittleEndian::write_u16(&mut major, RESYNC_META_FORMAT); data.extend_from_slice(&major); let mut minor = [0u8; 2]; byteorder::LittleEndian::write_u16(&mut minor, RESYNC_META_VERSION); data.extend_from_slice(&minor); data.extend_from_slice(&buf); let config_file = path_join_buf(&[BUCKET_META_PREFIX, bucket, REPLICATION_DIR, RESYNC_FILE_NAME]); save_config(api, &config_file, data).await?; Ok(()) } async fn get_replication_config(bucket: &str) -> Result<Option<ReplicationConfiguration>> { let config = match metadata_sys::get_replication_config(bucket).await { Ok((config, _)) => Some(config), Err(err) => { if err != Error::ConfigNotFound { return Err(err); } None } }; Ok(config) } #[derive(Debug, Clone, Default)] pub struct DeletedObjectReplicationInfo { pub delete_object: DeletedObject, pub bucket: String, pub event_type: String, pub op_type: ReplicationType, pub reset_id: String, pub target_arn: String, } impl ReplicationWorkerOperation for DeletedObjectReplicationInfo { fn as_any(&self) -> &dyn Any { self } fn to_mrf_entry(&self) -> MrfReplicateEntry { MrfReplicateEntry { bucket: self.bucket.clone(), object: self.delete_object.object_name.clone(), version_id: None, retry_count: 0, size: 0, } } fn get_bucket(&self) -> &str { &self.bucket } fn get_object(&self) -> &str { &self.delete_object.object_name } fn get_size(&self) -> i64 { 0 } fn is_delete_marker(&self) -> bool { true } fn get_op_type(&self) -> ReplicationType { self.op_type } } #[derive(Debug, Clone, Default)] pub struct ReplicationConfig { pub config: Option<ReplicationConfiguration>, pub remotes: Option<BucketTargets>, } impl ReplicationConfig { pub fn new(config: Option<ReplicationConfiguration>, remotes: Option<BucketTargets>) -> Self { Self { config, remotes } } pub fn is_empty(&self) -> bool { self.config.is_none() } pub fn replicate(&self, obj: &ObjectOpts) -> bool { self.config.as_ref().is_some_and(|config| config.replicate(obj)) } pub async fn resync( &self, oi: ObjectInfo, dsc: ReplicateDecision, status: &HashMap<String, ReplicationStatusType>, ) -> ResyncDecision { if self.is_empty() { return ResyncDecision::default(); } let mut dsc = dsc; if oi.delete_marker { let opts = ObjectOpts { name: oi.name.clone(), version_id: oi.version_id, delete_marker: true, op_type: ReplicationType::Delete, existing_object: true, ..Default::default() }; let arns = self .config .as_ref() .map(|config| config.filter_target_arns(&opts)) .unwrap_or_default(); if arns.is_empty() { return ResyncDecision::default(); } for arn in arns { let mut opts = opts.clone(); opts.target_arn = arn; dsc.set(ReplicateTargetDecision::new(opts.target_arn.clone(), self.replicate(&opts), false)); } return self.resync_internal(oi, dsc, status); } let mut user_defined = oi.user_defined.clone(); user_defined.remove(AMZ_BUCKET_REPLICATION_STATUS); let dsc = must_replicate( oi.bucket.as_str(), &oi.name, MustReplicateOptions::new( &user_defined, oi.user_tags.clone(), ReplicationStatusType::Empty, ReplicationType::ExistingObject, ObjectOptions::default(), ), ) .await; self.resync_internal(oi, dsc, status) } fn resync_internal( &self, oi: ObjectInfo, dsc: ReplicateDecision, status: &HashMap<String, ReplicationStatusType>, ) -> ResyncDecision { let Some(remotes) = self.remotes.as_ref() else { return ResyncDecision::default(); }; if remotes.is_empty() { return ResyncDecision::default(); } let mut resync_decision = ResyncDecision::default(); for target in remotes.targets.iter() { if let Some(decision) = dsc.targets_map.get(&target.arn) && decision.replicate { resync_decision.targets.insert( decision.arn.clone(), resync_target( &oi, &target.arn, &target.reset_id, target.reset_before_date, status.get(&decision.arn).unwrap_or(&ReplicationStatusType::Empty).clone(), ), ); } } resync_decision } } pub fn resync_target( oi: &ObjectInfo, arn: &str, reset_id: &str, reset_before_date: Option<OffsetDateTime>, status: ReplicationStatusType, ) -> ResyncTargetDecision { let rs = oi .user_defined .get(target_reset_header(arn).as_str()) .or(oi.user_defined.get(RUSTFS_REPLICATION_RESET_STATUS)) .map(|s| s.to_string()); let mut dec = ResyncTargetDecision::default(); let mod_time = oi.mod_time.unwrap_or(OffsetDateTime::UNIX_EPOCH); if rs.is_none() { let reset_before_date = reset_before_date.unwrap_or(OffsetDateTime::UNIX_EPOCH); if !reset_id.is_empty() && mod_time < reset_before_date { dec.replicate = true; return dec; } dec.replicate = status == ReplicationStatusType::Empty; return dec; } if reset_id.is_empty() || reset_before_date.is_none() { return dec; } let rs = rs.unwrap(); let reset_before_date = reset_before_date.unwrap(); let parts: Vec<&str> = rs.splitn(2, ';').collect(); if parts.len() != 2 { return dec; } let new_reset = parts[0] == reset_id; if !new_reset && status == ReplicationStatusType::Completed { return dec; } dec.replicate = new_reset && mod_time < reset_before_date; dec } pub struct MustReplicateOptions { meta: HashMap<String, String>, status: ReplicationStatusType, op_type: ReplicationType, replication_request: bool, } impl MustReplicateOptions { pub fn new( meta: &HashMap<String, String>, user_tags: String, status: ReplicationStatusType, op_type: ReplicationType, opts: ObjectOptions, ) -> Self { let mut meta = meta.clone(); if !user_tags.is_empty() { meta.insert(AMZ_OBJECT_TAGGING.to_string(), user_tags); } Self { meta, status, op_type, replication_request: opts.replication_request, } } pub fn from_object_info(oi: &ObjectInfo, op_type: ReplicationType, opts: ObjectOptions) -> Self { Self::new(&oi.user_defined, oi.user_tags.clone(), oi.replication_status.clone(), op_type, opts) } pub fn replication_status(&self) -> ReplicationStatusType { if let Some(rs) = self.meta.get(AMZ_BUCKET_REPLICATION_STATUS) { return ReplicationStatusType::from(rs.as_str()); } ReplicationStatusType::default() } pub fn is_existing_object_replication(&self) -> bool { self.op_type == ReplicationType::ExistingObject } pub fn is_metadata_replication(&self) -> bool { self.op_type == ReplicationType::Metadata } } pub fn get_must_replicate_options(
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
true
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/mod.rs
crates/ecstore/src/bucket/replication/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. mod config; pub mod datatypes; mod replication_pool; mod replication_resyncer; mod replication_state; mod rule; pub use config::*; pub use datatypes::*; pub use replication_pool::*; pub use replication_resyncer::*; pub use rule::*;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/datatypes.rs
crates/ecstore/src/bucket/replication/datatypes.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use serde::{Deserialize, Serialize}; use std::fmt; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] pub enum ResyncStatusType { #[default] NoResync, ResyncPending, ResyncCanceled, ResyncStarted, ResyncCompleted, ResyncFailed, } impl ResyncStatusType { pub fn is_valid(&self) -> bool { *self != ResyncStatusType::NoResync } } impl fmt::Display for ResyncStatusType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { ResyncStatusType::ResyncStarted => "Ongoing", ResyncStatusType::ResyncCompleted => "Completed", ResyncStatusType::ResyncFailed => "Failed", ResyncStatusType::ResyncPending => "Pending", ResyncStatusType::ResyncCanceled => "Canceled", ResyncStatusType::NoResync => "", }; write!(f, "{s}") } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/replication_state.rs
crates/ecstore/src/bucket/replication/replication_state.rs
use crate::error::Error; use rustfs_filemeta::{ReplicatedTargetInfo, ReplicationStatusType, ReplicationType}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::Arc; use std::sync::atomic::{AtomicI64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering}; use std::time::{Duration, SystemTime}; use tokio::sync::{Mutex, RwLock}; use tokio::time::interval; /// Exponential Moving Average with thread-safe interior mutability #[derive(Debug)] pub struct ExponentialMovingAverage { pub alpha: f64, pub value: AtomicU64, // Store f64 as u64 bits pub last_update: Arc<Mutex<SystemTime>>, } impl ExponentialMovingAverage { pub fn new() -> Self { let now = SystemTime::now(); Self { alpha: 0.1, // smoothing factor value: AtomicU64::new(0_f64.to_bits()), last_update: Arc::new(Mutex::new(now)), } } pub fn add_value(&self, value: f64, timestamp: SystemTime) { let current_value = f64::from_bits(self.value.load(AtomicOrdering::Relaxed)); let new_value = if current_value == 0.0 { value } else { self.alpha * value + (1.0 - self.alpha) * current_value }; self.value.store(new_value.to_bits(), AtomicOrdering::Relaxed); // Update timestamp (this is async, but we'll use try_lock to avoid blocking) if let Ok(mut last_update) = self.last_update.try_lock() { *last_update = timestamp; } } pub fn get_current_average(&self) -> f64 { f64::from_bits(self.value.load(AtomicOrdering::Relaxed)) } pub fn update_exponential_moving_average(&self, now: SystemTime) { if let Ok(mut last_update_guard) = self.last_update.try_lock() { let last_update = *last_update_guard; if let Ok(duration) = now.duration_since(last_update) && duration.as_secs() > 0 { let decay = (-duration.as_secs_f64() / 60.0).exp(); // 1 minute decay let current_value = f64::from_bits(self.value.load(AtomicOrdering::Relaxed)); self.value.store((current_value * decay).to_bits(), AtomicOrdering::Relaxed); *last_update_guard = now; } } } pub fn merge(&self, other: &ExponentialMovingAverage) -> Self { let now = SystemTime::now(); let self_value = f64::from_bits(self.value.load(AtomicOrdering::Relaxed)); let other_value = f64::from_bits(other.value.load(AtomicOrdering::Relaxed)); let merged_value = (self_value + other_value) / 2.0; // Get timestamps (use current time as fallback) let self_time = self.last_update.try_lock().map(|t| *t).unwrap_or(now); let other_time = other.last_update.try_lock().map(|t| *t).unwrap_or(now); let merged_time = self_time.max(other_time); Self { alpha: self.alpha, value: AtomicU64::new(merged_value.to_bits()), last_update: Arc::new(Mutex::new(merged_time)), } } } impl Clone for ExponentialMovingAverage { fn clone(&self) -> Self { let now = SystemTime::now(); let value = self.value.load(AtomicOrdering::Relaxed); let last_update = self.last_update.try_lock().map(|t| *t).unwrap_or(now); Self { alpha: self.alpha, value: AtomicU64::new(value), last_update: Arc::new(Mutex::new(last_update)), } } } impl Default for ExponentialMovingAverage { fn default() -> Self { Self::new() } } impl Serialize for ExponentialMovingAverage { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { use serde::ser::SerializeStruct; let mut state = serializer.serialize_struct("ExponentialMovingAverage", 3)?; state.serialize_field("alpha", &self.alpha)?; state.serialize_field("value", &f64::from_bits(self.value.load(AtomicOrdering::Relaxed)))?; let last_update = self.last_update.try_lock().map(|t| *t).unwrap_or(SystemTime::UNIX_EPOCH); state.serialize_field("last_update", &last_update)?; state.end() } } impl<'de> Deserialize<'de> for ExponentialMovingAverage { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de>, { #[derive(Deserialize)] struct ExponentialMovingAverageData { alpha: f64, value: f64, last_update: SystemTime, } let data = ExponentialMovingAverageData::deserialize(deserializer)?; Ok(Self { alpha: data.alpha, value: AtomicU64::new(data.value.to_bits()), last_update: Arc::new(Mutex::new(data.last_update)), }) } } /// Transfer statistics #[derive(Debug, Clone, Serialize, Deserialize)] pub struct XferStats { pub avg: f64, pub curr: f64, pub peak: f64, pub measure: ExponentialMovingAverage, } impl XferStats { pub fn new() -> Self { Self { avg: 0.0, curr: 0.0, peak: 0.0, measure: ExponentialMovingAverage::new(), } } pub fn add_size(&mut self, size: i64, duration: Duration) { if duration.as_nanos() > 0 { let rate = (size as f64) / duration.as_secs_f64(); self.curr = rate; if rate > self.peak { self.peak = rate; } self.measure.add_value(rate, SystemTime::now()); self.avg = self.measure.get_current_average(); } } pub fn clone_stats(&self) -> Self { Self { avg: self.avg, curr: self.curr, peak: self.peak, measure: self.measure.clone(), } } pub fn merge(&self, other: &XferStats) -> Self { Self { avg: (self.avg + other.avg) / 2.0, curr: self.curr + other.curr, peak: self.peak.max(other.peak), measure: self.measure.merge(&other.measure), } } pub fn update_exponential_moving_average(&mut self, now: SystemTime) { self.measure.update_exponential_moving_average(now); self.avg = self.measure.get_current_average(); } } impl Default for XferStats { fn default() -> Self { Self::new() } } #[derive(Debug, Clone)] pub struct ReplStat { pub arn: String, pub completed: bool, pub pending: bool, pub failed: bool, pub op_type: ReplicationType, pub transfer_size: i64, pub transfer_duration: Duration, pub endpoint: String, pub secure: bool, pub err: Option<Error>, } impl ReplStat { pub fn new() -> Self { Self { arn: String::new(), completed: false, pending: false, failed: false, op_type: ReplicationType::default(), transfer_size: 0, transfer_duration: Duration::default(), endpoint: String::new(), secure: false, err: None, } } pub fn endpoint(&self) -> String { let scheme = if self.secure { "https" } else { "http" }; format!("{}://{}", scheme, self.endpoint) } #[allow(clippy::too_many_arguments)] pub fn set( &mut self, arn: String, size: i64, duration: Duration, status: ReplicationStatusType, op_type: ReplicationType, endpoint: String, secure: bool, err: Option<Error>, ) { self.arn = arn; self.transfer_size = size; self.transfer_duration = duration; self.op_type = op_type; self.endpoint = endpoint; self.secure = secure; self.err = err; // Reset status self.completed = false; self.pending = false; self.failed = false; match status { ReplicationStatusType::Completed => self.completed = true, ReplicationStatusType::Pending => self.pending = true, ReplicationStatusType::Failed => self.failed = true, _ => {} } } } impl Default for ReplStat { fn default() -> Self { Self::new() } } /// Site replication statistics #[derive(Debug, Default)] pub struct SRStats { pub replica_size: AtomicI64, pub replica_count: AtomicI64, // More site replication related statistics fields can be added here } impl SRStats { pub fn new() -> Self { Self::default() } pub fn update(&self, rs: &ReplStat, _depl_id: &str) { // Update site replication statistics // In actual implementation, statistics would be updated based on deployment ID if rs.completed { self.replica_size.fetch_add(rs.transfer_size, Ordering::Relaxed); self.replica_count.fetch_add(1, Ordering::Relaxed); } } pub fn get(&self) -> HashMap<String, i64> { // Return current statistics let mut stats = HashMap::new(); stats.insert("replica_size".to_string(), self.replica_size.load(Ordering::Relaxed)); stats.insert("replica_count".to_string(), self.replica_count.load(Ordering::Relaxed)); stats } } /// Statistics in queue #[derive(Debug, Default, Serialize, Deserialize)] pub struct InQueueStats { pub bytes: i64, pub count: i64, #[serde(skip)] pub now_bytes: AtomicI64, #[serde(skip)] pub now_count: AtomicI64, } impl Clone for InQueueStats { fn clone(&self) -> Self { Self { bytes: self.bytes, count: self.count, now_bytes: AtomicI64::new(self.now_bytes.load(Ordering::Relaxed)), now_count: AtomicI64::new(self.now_count.load(Ordering::Relaxed)), } } } impl InQueueStats { pub fn new() -> Self { Self::default() } pub fn get_current_bytes(&self) -> i64 { self.now_bytes.load(Ordering::Relaxed) } pub fn get_current_count(&self) -> i64 { self.now_count.load(Ordering::Relaxed) } } /// Metrics in queue #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct InQueueMetric { pub curr: InQueueStats, pub avg: InQueueStats, pub max: InQueueStats, } impl InQueueMetric { pub fn merge(&self, other: &InQueueMetric) -> Self { Self { curr: InQueueStats { bytes: self.curr.bytes + other.curr.bytes, count: self.curr.count + other.curr.count, now_bytes: AtomicI64::new( self.curr.now_bytes.load(Ordering::Relaxed) + other.curr.now_bytes.load(Ordering::Relaxed), ), now_count: AtomicI64::new( self.curr.now_count.load(Ordering::Relaxed) + other.curr.now_count.load(Ordering::Relaxed), ), }, avg: InQueueStats { bytes: (self.avg.bytes + other.avg.bytes) / 2, count: (self.avg.count + other.avg.count) / 2, ..Default::default() }, max: InQueueStats { bytes: self.max.bytes.max(other.max.bytes), count: self.max.count.max(other.max.count), ..Default::default() }, } } } /// Queue cache #[derive(Debug, Default)] pub struct QueueCache { pub bucket_stats: HashMap<String, InQueueStats>, pub sr_queue_stats: InQueueStats, } impl QueueCache { pub fn new() -> Self { Self::default() } pub fn update(&mut self) { // Update queue statistics cache // In actual implementation, this would get latest statistics from queue system } pub fn get_bucket_stats(&self, bucket: &str) -> InQueueMetric { if let Some(bucket_stat) = self.bucket_stats.get(bucket) { InQueueMetric { curr: InQueueStats { bytes: bucket_stat.now_bytes.load(Ordering::Relaxed), count: bucket_stat.now_count.load(Ordering::Relaxed), ..Default::default() }, avg: InQueueStats::default(), // simplified implementation max: InQueueStats::default(), // simplified implementation } } else { InQueueMetric::default() } } pub fn get_site_stats(&self) -> InQueueMetric { InQueueMetric { curr: InQueueStats { bytes: self.sr_queue_stats.now_bytes.load(Ordering::Relaxed), count: self.sr_queue_stats.now_count.load(Ordering::Relaxed), ..Default::default() }, avg: InQueueStats::default(), // simplified implementation max: InQueueStats::default(), // simplified implementation } } } #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ProxyMetric { pub get_total: i64, pub get_failed: i64, pub put_total: i64, pub put_failed: i64, pub head_total: i64, pub head_failed: i64, } impl ProxyMetric { pub fn add(&mut self, other: &ProxyMetric) { self.get_total += other.get_total; self.get_failed += other.get_failed; self.put_total += other.put_total; self.put_failed += other.put_failed; self.head_total += other.head_total; self.head_failed += other.head_failed; } } /// Proxy statistics cache #[derive(Debug, Clone, Default)] pub struct ProxyStatsCache { bucket_stats: HashMap<String, ProxyMetric>, } impl ProxyStatsCache { pub fn new() -> Self { Self::default() } pub fn inc(&mut self, bucket: &str, api: &str, is_err: bool) { let metric = self.bucket_stats.entry(bucket.to_string()).or_default(); match api { "GetObject" => { metric.get_total += 1; if is_err { metric.get_failed += 1; } } "PutObject" => { metric.put_total += 1; if is_err { metric.put_failed += 1; } } "HeadObject" => { metric.head_total += 1; if is_err { metric.head_failed += 1; } } _ => {} } } pub fn get_bucket_stats(&self, bucket: &str) -> ProxyMetric { self.bucket_stats.get(bucket).cloned().unwrap_or_default() } pub fn get_site_stats(&self) -> ProxyMetric { let mut total = ProxyMetric::default(); for metric in self.bucket_stats.values() { total.add(metric); } total } } /// Failure statistics #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct FailStats { pub count: i64, pub size: i64, } impl FailStats { pub fn new() -> Self { Self::default() } pub fn add_size(&mut self, size: i64, _err: Option<&Error>) { self.count += 1; self.size += size; } pub fn merge(&self, other: &FailStats) -> Self { Self { count: self.count + other.count, size: self.size + other.size, } } pub fn to_metric(&self) -> FailedMetric { FailedMetric { count: self.count, size: self.size, } } } /// Failed metric #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct FailedMetric { pub count: i64, pub size: i64, } /// Latency statistics #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct LatencyStats { pub avg: f64, pub curr: f64, pub max: f64, } impl LatencyStats { pub fn new() -> Self { Self::default() } pub fn update(&mut self, _size: i64, duration: Duration) { let latency = duration.as_millis() as f64; self.curr = latency; if latency > self.max { self.max = latency; } // Simple moving average (simplified implementation) self.avg = (self.avg + latency) / 2.0; } pub fn merge(&self, other: &LatencyStats) -> Self { Self { avg: (self.avg + other.avg) / 2.0, curr: self.curr.max(other.curr), max: self.max.max(other.max), } } } /// Bucket replication statistics for a single target #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct BucketReplicationStat { pub replicated_size: i64, pub replicated_count: i64, pub failed: FailedMetric, pub fail_stats: FailStats, pub latency: LatencyStats, pub xfer_rate_lrg: XferStats, pub xfer_rate_sml: XferStats, } impl BucketReplicationStat { pub fn new() -> Self { Self::default() } pub fn update_xfer_rate(&mut self, size: i64, duration: Duration) { // Classify as large or small transfer based on size if size > 1024 * 1024 { // > 1MB self.xfer_rate_lrg.add_size(size, duration); } else { self.xfer_rate_sml.add_size(size, duration); } } } /// Queue statistics for nodes #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct QueueStats { pub nodes: Vec<QueueNode>, } /// Queue node statistics #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct QueueNode { pub q_stats: InQueueMetric, } /// Bucket replication statistics #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct BucketReplicationStats { pub stats: HashMap<String, BucketReplicationStat>, pub replica_size: i64, pub replica_count: i64, pub replicated_size: i64, pub replicated_count: i64, pub q_stat: InQueueMetric, } impl BucketReplicationStats { pub fn new() -> Self { Self::default() } pub fn is_empty(&self) -> bool { self.stats.is_empty() && self.replica_size == 0 && self.replicated_size == 0 } pub fn has_replication_usage(&self) -> bool { self.replica_size > 0 || self.replicated_size > 0 || !self.stats.is_empty() } pub fn clone_stats(&self) -> Self { self.clone() } } /// Bucket statistics #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct BucketStats { pub uptime: i64, pub replication_stats: BucketReplicationStats, pub queue_stats: QueueStats, pub proxy_stats: ProxyMetric, } /// Site replication metrics summary #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct SRMetricsSummary { pub uptime: i64, pub queued: InQueueMetric, pub active_workers: ActiveWorkerStat, pub metrics: HashMap<String, i64>, pub proxied: ProxyMetric, pub replica_size: i64, pub replica_count: i64, } /// Active worker statistics #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ActiveWorkerStat { pub curr: i32, pub max: i32, pub avg: f64, } impl ActiveWorkerStat { pub fn new() -> Self { Self::default() } pub fn get(&self) -> Self { self.clone() } pub fn update(&mut self) { // Simulate worker statistics update logic // In actual implementation, this would get current active count from worker pool } } /// Global replication statistics #[derive(Debug)] pub struct ReplicationStats { // Site replication statistics - maintain global level statistics pub sr_stats: Arc<SRStats>, // Active worker statistics pub workers: Arc<Mutex<ActiveWorkerStat>>, // Queue statistics cache pub q_cache: Arc<Mutex<QueueCache>>, // Proxy statistics cache pub p_cache: Arc<Mutex<ProxyStatsCache>>, // MRF backlog statistics (simplified) pub mrf_stats: HashMap<String, i64>, // Bucket replication cache pub cache: Arc<RwLock<HashMap<String, BucketReplicationStats>>>, pub most_recent_stats: Arc<Mutex<HashMap<String, BucketStats>>>, } impl ReplicationStats { pub fn new() -> Self { Self { sr_stats: Arc::new(SRStats::new()), workers: Arc::new(Mutex::new(ActiveWorkerStat::new())), q_cache: Arc::new(Mutex::new(QueueCache::new())), p_cache: Arc::new(Mutex::new(ProxyStatsCache::new())), mrf_stats: HashMap::new(), cache: Arc::new(RwLock::new(HashMap::new())), most_recent_stats: Arc::new(Mutex::new(HashMap::new())), } } /// Initialize background tasks pub async fn start_background_tasks(&self) { // Start moving average calculation task let cache_clone = Arc::clone(&self.cache); tokio::spawn(async move { let mut interval = interval(Duration::from_secs(5)); loop { interval.tick().await; Self::update_moving_avg_static(&cache_clone).await; } }); // Start worker statistics collection task let workers_clone = Arc::clone(&self.workers); tokio::spawn(async move { let mut interval = interval(Duration::from_secs(2)); loop { interval.tick().await; let mut workers = workers_clone.lock().await; workers.update(); } }); // Start queue statistics collection task let q_cache_clone = Arc::clone(&self.q_cache); tokio::spawn(async move { let mut interval = interval(Duration::from_secs(2)); loop { interval.tick().await; let mut cache = q_cache_clone.lock().await; cache.update(); } }); } async fn update_moving_avg_static(cache: &Arc<RwLock<HashMap<String, BucketReplicationStats>>>) { // This is a simplified implementation // In actual implementation, exponential moving averages need to be updated let now = SystemTime::now(); let cache_read = cache.read().await; for (_bucket, stats) in cache_read.iter() { for stat in stats.stats.values() { // Now we can update the moving averages using interior mutability stat.xfer_rate_lrg.measure.update_exponential_moving_average(now); stat.xfer_rate_sml.measure.update_exponential_moving_average(now); } } } /// Check if bucket replication statistics have usage pub fn has_replication_usage(&self, bucket: &str) -> bool { if let Ok(cache) = self.cache.try_read() && let Some(stats) = cache.get(bucket) { return stats.has_replication_usage(); } false } /// Get active worker statistics pub fn active_workers(&self) -> ActiveWorkerStat { // This should be called from an async context // For now, use try_lock to avoid blocking self.workers.try_lock().map(|w| w.get()).unwrap_or_default() } /// Delete bucket's memory replication statistics pub async fn delete(&self, bucket: &str) { let mut cache = self.cache.write().await; cache.remove(bucket); } /// Update replica statistics pub async fn update_replica_stat(&self, bucket: &str, size: i64) { let mut cache = self.cache.write().await; let stats = cache.entry(bucket.to_string()).or_insert_with(BucketReplicationStats::new); stats.replica_size += size; stats.replica_count += 1; // Update site replication statistics self.sr_stats.replica_size.fetch_add(size, Ordering::Relaxed); self.sr_stats.replica_count.fetch_add(1, Ordering::Relaxed); } /// Site replication update replica statistics fn sr_update_replica_stat(&self, size: i64) { self.sr_stats.replica_size.fetch_add(size, Ordering::Relaxed); self.sr_stats.replica_count.fetch_add(1, Ordering::Relaxed); } /// Site replication update fn sr_update(&self, rs: &ReplStat) { // In actual implementation, deployment ID would be obtained here let depl_id = "default"; // simplified implementation self.sr_stats.update(rs, depl_id); } /// Update replication statistics pub async fn update( &self, bucket: &str, ri: &ReplicatedTargetInfo, status: ReplicationStatusType, prev_status: ReplicationStatusType, ) { let mut rs = ReplStat::new(); match status { ReplicationStatusType::Pending => { if ri.op_type.is_data_replication() && prev_status != status { rs.set( ri.arn.clone(), ri.size, Duration::default(), status, ri.op_type, ri.endpoint.clone(), ri.secure, ri.error.as_ref().map(|e| crate::error::Error::other(e.clone())), ); } } ReplicationStatusType::Completed => { if ri.op_type.is_data_replication() { rs.set( ri.arn.clone(), ri.size, ri.duration, status, ri.op_type, ri.endpoint.clone(), ri.secure, ri.error.as_ref().map(|e| crate::error::Error::other(e.clone())), ); } } ReplicationStatusType::Failed => { if ri.op_type.is_data_replication() && prev_status == ReplicationStatusType::Pending { rs.set( ri.arn.clone(), ri.size, ri.duration, status, ri.op_type, ri.endpoint.clone(), ri.secure, ri.error.as_ref().map(|e| crate::error::Error::other(e.clone())), ); } } ReplicationStatusType::Replica => { if ri.op_type == ReplicationType::Object { rs.set( ri.arn.clone(), ri.size, Duration::default(), status, ri.op_type, String::new(), false, ri.error.as_ref().map(|e| crate::error::Error::other(e.clone())), ); } } _ => {} } // Update site replication memory statistics if rs.completed || rs.failed { self.sr_update(&rs); } // Update bucket replication memory statistics let mut cache = self.cache.write().await; let bucket_stats = cache.entry(bucket.to_string()).or_insert_with(BucketReplicationStats::new); let stat = bucket_stats .stats .entry(ri.arn.clone()) .or_insert_with(|| BucketReplicationStat { xfer_rate_lrg: XferStats::new(), xfer_rate_sml: XferStats::new(), ..Default::default() }); match (rs.completed, rs.failed, rs.pending) { (true, false, false) => { stat.replicated_size += rs.transfer_size; stat.replicated_count += 1; if rs.transfer_duration > Duration::default() { stat.latency.update(rs.transfer_size, rs.transfer_duration); stat.update_xfer_rate(rs.transfer_size, rs.transfer_duration); } } (false, true, false) => { stat.fail_stats.add_size(rs.transfer_size, rs.err.as_ref()); } (false, false, true) => { // Pending status, no processing for now } _ => {} } } /// Get replication metrics for all buckets pub async fn get_all(&self) -> HashMap<String, BucketReplicationStats> { let cache = self.cache.read().await; let mut result = HashMap::new(); for (bucket, stats) in cache.iter() { let mut cloned_stats = stats.clone_stats(); // Add queue statistics let q_cache = self.q_cache.lock().await; cloned_stats.q_stat = q_cache.get_bucket_stats(bucket); result.insert(bucket.clone(), cloned_stats); } result } /// Get replication metrics for a single bucket pub async fn get(&self, bucket: &str) -> BucketReplicationStats { let cache = self.cache.read().await; if let Some(stats) = cache.get(bucket) { stats.clone_stats() } else { BucketReplicationStats::new() } } /// Get metrics summary for site replication node pub async fn get_sr_metrics_for_node(&self) -> SRMetricsSummary { let boot_time = SystemTime::UNIX_EPOCH; // simplified implementation let uptime = SystemTime::now().duration_since(boot_time).unwrap_or_default().as_secs() as i64; let q_cache = self.q_cache.lock().await; let queued = q_cache.get_site_stats(); let p_cache = self.p_cache.lock().await; let proxied = p_cache.get_site_stats(); SRMetricsSummary { uptime, queued, active_workers: self.active_workers(), metrics: self.sr_stats.get(), proxied, replica_size: self.sr_stats.replica_size.load(Ordering::Relaxed), replica_count: self.sr_stats.replica_count.load(Ordering::Relaxed), } } /// Calculate bucket replication statistics pub async fn calculate_bucket_replication_stats(&self, bucket: &str, bucket_stats: Vec<BucketStats>) -> BucketStats { if bucket_stats.is_empty() { return BucketStats { uptime: 0, replication_stats: BucketReplicationStats::new(), queue_stats: Default::default(), proxy_stats: ProxyMetric::default(), }; } // Accumulate cluster bucket statistics let mut stats = HashMap::new(); let mut tot_replica_size = 0i64; let mut tot_replica_count = 0i64; let mut tot_replicated_size = 0i64; let mut tot_replicated_count = 0i64; let mut tq = InQueueMetric::default(); for bucket_stat in &bucket_stats { tot_replica_size += bucket_stat.replication_stats.replica_size; tot_replica_count += bucket_stat.replication_stats.replica_count; for q in &bucket_stat.queue_stats.nodes { tq = tq.merge(&q.q_stats); } for (arn, stat) in &bucket_stat.replication_stats.stats { let old_stat = stats.entry(arn.clone()).or_insert_with(|| BucketReplicationStat { xfer_rate_lrg: XferStats::new(), xfer_rate_sml: XferStats::new(), ..Default::default() }); let f_stats = stat.fail_stats.merge(&old_stat.fail_stats); let lrg = old_stat.xfer_rate_lrg.merge(&stat.xfer_rate_lrg); let sml = old_stat.xfer_rate_sml.merge(&stat.xfer_rate_sml); *old_stat = BucketReplicationStat { failed: f_stats.to_metric(), fail_stats: f_stats, replicated_size: stat.replicated_size + old_stat.replicated_size, replicated_count: stat.replicated_count + old_stat.replicated_count, latency: stat.latency.merge(&old_stat.latency), xfer_rate_lrg: lrg, xfer_rate_sml: sml, }; tot_replicated_size += stat.replicated_size; tot_replicated_count += stat.replicated_count; } } let s = BucketReplicationStats { stats, q_stat: tq, replica_size: tot_replica_size, replica_count: tot_replica_count, replicated_size: tot_replicated_size, replicated_count: tot_replicated_count, }; let qs = Default::default(); let mut ps = ProxyMetric::default(); for bs in &bucket_stats { // qs.nodes.extend(bs.queue_stats.nodes.clone()); // simplified implementation ps.add(&bs.proxy_stats); } let uptime = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH)
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
true
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/rule.rs
crates/ecstore/src/bucket/replication/rule.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use s3s::dto::ReplicaModificationsStatus; use s3s::dto::ReplicationRule; use super::ObjectOpts; pub trait ReplicationRuleExt { fn prefix(&self) -> &str; fn metadata_replicate(&self, obj: &ObjectOpts) -> bool; } impl ReplicationRuleExt for ReplicationRule { fn prefix(&self) -> &str { if let Some(filter) = &self.filter { if let Some(prefix) = &filter.prefix { prefix } else if let Some(and) = &filter.and { and.prefix.as_deref().unwrap_or("") } else { "" } } else { "" } } fn metadata_replicate(&self, obj: &ObjectOpts) -> bool { if !obj.replica { return true; } self.source_selection_criteria.as_ref().is_some_and(|s| { s.replica_modifications .clone() .is_some_and(|r| r.status == ReplicaModificationsStatus::from_static(ReplicaModificationsStatus::ENABLED)) }) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/bucket/replication/replication_pool.rs
crates/ecstore/src/bucket/replication/replication_pool.rs
use crate::StorageAPI; use crate::bucket::replication::ResyncOpts; use crate::bucket::replication::ResyncStatusType; use crate::bucket::replication::replicate_delete; use crate::bucket::replication::replicate_object; use crate::disk::BUCKET_META_PREFIX; use std::any::Any; use std::sync::Arc; use std::sync::atomic::AtomicI32; use std::sync::atomic::Ordering; use crate::bucket::replication::replication_resyncer::{ BucketReplicationResyncStatus, DeletedObjectReplicationInfo, ReplicationResyncer, }; use crate::bucket::replication::replication_state::ReplicationStats; use crate::config::com::read_config; use crate::error::Error as EcstoreError; use crate::store_api::ObjectInfo; use lazy_static::lazy_static; use rustfs_filemeta::MrfReplicateEntry; use rustfs_filemeta::ReplicateDecision; use rustfs_filemeta::ReplicateObjectInfo; use rustfs_filemeta::ReplicatedTargetInfo; use rustfs_filemeta::ReplicationStatusType; use rustfs_filemeta::ReplicationType; use rustfs_filemeta::ReplicationWorkerOperation; use rustfs_filemeta::ResyncDecision; use rustfs_filemeta::replication_statuses_map; use rustfs_filemeta::version_purge_statuses_map; use rustfs_utils::http::RESERVED_METADATA_PREFIX_LOWER; use time::OffsetDateTime; use time::format_description::well_known::Rfc3339; use tokio::sync::Mutex; use tokio::sync::RwLock; use tokio::sync::mpsc; use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Sender; use tokio::task::JoinHandle; use tokio::time::Duration; use tokio_util::sync::CancellationToken; use tracing::info; use tracing::warn; // Worker limits pub const WORKER_MAX_LIMIT: usize = 500; pub const WORKER_MIN_LIMIT: usize = 50; pub const WORKER_AUTO_DEFAULT: usize = 100; pub const MRF_WORKER_MAX_LIMIT: usize = 8; pub const MRF_WORKER_MIN_LIMIT: usize = 2; pub const MRF_WORKER_AUTO_DEFAULT: usize = 4; pub const LARGE_WORKER_COUNT: usize = 10; pub const MIN_LARGE_OBJ_SIZE: i64 = 128 * 1024 * 1024; // 128MiB /// Priority levels for replication #[derive(Debug, Clone, PartialEq)] pub enum ReplicationPriority { Fast, Slow, Auto, } impl std::str::FromStr for ReplicationPriority { type Err = (); fn from_str(s: &str) -> Result<Self, Self::Err> { match s { "fast" => Ok(ReplicationPriority::Fast), "slow" => Ok(ReplicationPriority::Slow), "auto" => Ok(ReplicationPriority::Auto), _ => Ok(ReplicationPriority::Auto), // Default to Auto for unknown values } } } impl ReplicationPriority { pub fn as_str(&self) -> &'static str { match self { ReplicationPriority::Fast => "fast", ReplicationPriority::Slow => "slow", ReplicationPriority::Auto => "auto", } } } /// Enum for different types of replication operations #[derive(Debug)] pub enum ReplicationOperation { Object(Box<ReplicateObjectInfo>), Delete(Box<DeletedObjectReplicationInfo>), } impl ReplicationWorkerOperation for ReplicationOperation { fn as_any(&self) -> &dyn Any { self } fn to_mrf_entry(&self) -> MrfReplicateEntry { match self { ReplicationOperation::Object(obj) => obj.to_mrf_entry(), ReplicationOperation::Delete(del) => del.to_mrf_entry(), } } fn get_bucket(&self) -> &str { match self { ReplicationOperation::Object(obj) => obj.get_bucket(), ReplicationOperation::Delete(del) => del.get_bucket(), } } fn get_object(&self) -> &str { match self { ReplicationOperation::Object(obj) => obj.get_object(), ReplicationOperation::Delete(del) => del.get_object(), } } fn get_size(&self) -> i64 { match self { ReplicationOperation::Object(obj) => obj.get_size(), ReplicationOperation::Delete(del) => del.get_size(), } } fn is_delete_marker(&self) -> bool { match self { ReplicationOperation::Object(obj) => obj.is_delete_marker(), ReplicationOperation::Delete(del) => del.is_delete_marker(), } } fn get_op_type(&self) -> ReplicationType { match self { ReplicationOperation::Object(obj) => obj.get_op_type(), ReplicationOperation::Delete(del) => del.get_op_type(), } } } /// Replication pool options #[derive(Debug, Clone)] pub struct ReplicationPoolOpts { pub priority: ReplicationPriority, pub max_workers: Option<usize>, pub max_l_workers: Option<usize>, } impl Default for ReplicationPoolOpts { fn default() -> Self { Self { priority: ReplicationPriority::Auto, max_workers: None, max_l_workers: None, } } } /// Main replication pool structure #[derive(Debug)] pub struct ReplicationPool<S: StorageAPI> { // Atomic counters for active workers active_workers: Arc<AtomicI32>, active_lrg_workers: Arc<AtomicI32>, active_mrf_workers: Arc<AtomicI32>, storage: Arc<S>, // Configuration priority: RwLock<ReplicationPriority>, max_workers: RwLock<usize>, max_l_workers: RwLock<usize>, // Statistics stats: Arc<ReplicationStats>, // Worker channels workers: RwLock<Vec<Sender<ReplicationOperation>>>, lrg_workers: RwLock<Vec<Sender<ReplicationOperation>>>, // MRF (Most Recent Failures) channels mrf_replica_tx: Sender<ReplicationOperation>, mrf_replica_rx: Mutex<Option<Receiver<ReplicationOperation>>>, mrf_save_tx: Sender<MrfReplicateEntry>, mrf_save_rx: Mutex<Option<Receiver<MrfReplicateEntry>>>, // Control channels mrf_worker_kill_tx: Sender<()>, mrf_stop_tx: Sender<()>, // Worker size tracking mrf_worker_size: AtomicI32, // Task handles for cleanup task_handles: Mutex<Vec<JoinHandle<()>>>, // Replication resyncer for handling bucket resync operations resyncer: Arc<ReplicationResyncer>, } impl<S: StorageAPI> ReplicationPool<S> { /// Creates a new replication pool with specified options pub async fn new(opts: ReplicationPoolOpts, stats: Arc<ReplicationStats>, storage: Arc<S>) -> Arc<Self> { let max_workers = opts.max_workers.unwrap_or(WORKER_MAX_LIMIT); let (workers, failed_workers) = match opts.priority { ReplicationPriority::Fast => (WORKER_MAX_LIMIT, MRF_WORKER_MAX_LIMIT), ReplicationPriority::Slow => (WORKER_MIN_LIMIT, MRF_WORKER_MIN_LIMIT), ReplicationPriority::Auto => (WORKER_AUTO_DEFAULT, MRF_WORKER_AUTO_DEFAULT), }; let workers = std::cmp::min(workers, max_workers); let failed_workers = std::cmp::min(failed_workers, max_workers); let max_l_workers = opts.max_l_workers.unwrap_or(LARGE_WORKER_COUNT); // Create MRF channels let (mrf_replica_tx, mrf_replica_rx) = mpsc::channel(100000); let (mrf_save_tx, mrf_save_rx) = mpsc::channel(100000); let (mrf_worker_kill_tx, _mrf_worker_kill_rx) = mpsc::channel(failed_workers); let (mrf_stop_tx, _mrf_stop_rx) = mpsc::channel(1); let pool = Arc::new(Self { active_workers: Arc::new(AtomicI32::new(0)), active_lrg_workers: Arc::new(AtomicI32::new(0)), active_mrf_workers: Arc::new(AtomicI32::new(0)), priority: RwLock::new(opts.priority), max_workers: RwLock::new(max_workers), max_l_workers: RwLock::new(max_l_workers), stats, storage, workers: RwLock::new(Vec::new()), lrg_workers: RwLock::new(Vec::new()), mrf_replica_tx, mrf_replica_rx: Mutex::new(Some(mrf_replica_rx)), mrf_save_tx, mrf_save_rx: Mutex::new(Some(mrf_save_rx)), mrf_worker_kill_tx, mrf_stop_tx, mrf_worker_size: AtomicI32::new(0), task_handles: Mutex::new(Vec::new()), resyncer: Arc::new(ReplicationResyncer::new().await), }); // Initialize workers pool.resize_lrg_workers(max_l_workers, 0).await; pool.resize_workers(workers, 0).await; pool.resize_failed_workers(failed_workers as i32).await; // Start background tasks pool.start_mrf_processor().await; pool.start_mrf_persister().await; pool } /// Returns the number of active workers handling replication traffic pub fn active_workers(&self) -> i32 { self.active_workers.load(Ordering::SeqCst) } /// Returns the number of active workers handling replication failures pub fn active_mrf_workers(&self) -> i32 { self.active_mrf_workers.load(Ordering::SeqCst) } /// Returns the number of active workers handling traffic > 128MiB object size pub fn active_lrg_workers(&self) -> i32 { self.active_lrg_workers.load(Ordering::SeqCst) } /// Resizes the large workers pool pub async fn resize_lrg_workers(&self, n: usize, check_old: usize) { let mut lrg_workers = self.lrg_workers.write().await; if (check_old > 0 && lrg_workers.len() != check_old) || n == lrg_workers.len() || n < 1 { return; } // Add workers if needed while lrg_workers.len() < n { let (tx, rx) = mpsc::channel(100000); lrg_workers.push(tx); let active_counter = self.active_lrg_workers.clone(); let storage = self.storage.clone(); let handle = tokio::spawn(async move { let mut rx = rx; while let Some(operation) = rx.recv().await { active_counter.fetch_add(1, Ordering::SeqCst); match operation { ReplicationOperation::Object(obj_info) => { replicate_object(*obj_info, storage.clone()).await; } ReplicationOperation::Delete(del_info) => { replicate_delete(*del_info, storage.clone()).await; } } active_counter.fetch_sub(1, Ordering::SeqCst); } }); self.task_handles.lock().await.push(handle); } // Remove workers if needed while lrg_workers.len() > n { if let Some(worker) = lrg_workers.pop() { drop(worker); // Closing the channel will terminate the worker } } } /// Resizes the regular workers pool pub async fn resize_workers(&self, n: usize, check_old: usize) { let mut workers = self.workers.write().await; if (check_old > 0 && workers.len() != check_old) || n == workers.len() || n < 1 { warn!( "resize_workers: skipping resize - check_old_mismatch={}, same_size={}, invalid_n={}", check_old > 0 && workers.len() != check_old, n == workers.len(), n < 1 ); return; } // Add workers if needed if workers.len() < n { info!("resize_workers: adding workers from {} to {}", workers.len(), n); } while workers.len() < n { let (tx, rx) = mpsc::channel(10000); workers.push(tx); let active_counter = self.active_workers.clone(); let stats = self.stats.clone(); let storage = self.storage.clone(); let handle = tokio::spawn(async move { let mut rx = rx; while let Some(operation) = rx.recv().await { active_counter.fetch_add(1, Ordering::SeqCst); match operation { ReplicationOperation::Object(obj_info) => { stats .inc_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; // Perform actual replication (placeholder) replicate_object(obj_info.as_ref().clone(), storage.clone()).await; stats .dec_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; } ReplicationOperation::Delete(del_info) => { stats.inc_q(&del_info.bucket, 0, true, del_info.op_type).await; // Perform actual delete replication (placeholder) replicate_delete(del_info.as_ref().clone(), storage.clone()).await; stats.dec_q(&del_info.bucket, 0, true, del_info.op_type).await; } } active_counter.fetch_sub(1, Ordering::SeqCst); } }); self.task_handles.lock().await.push(handle); } // Remove workers if needed if workers.len() > n { warn!("resize_workers: removing workers from {} to {}", workers.len(), n); } while workers.len() > n { if let Some(worker) = workers.pop() { drop(worker); // Closing the channel will terminate the worker } } } /// Resizes the failed workers pool pub async fn resize_failed_workers(&self, n: i32) { // Add workers if needed while self.mrf_worker_size.load(Ordering::SeqCst) < n { self.mrf_worker_size.fetch_add(1, Ordering::SeqCst); let active_counter = self.active_mrf_workers.clone(); let stats = self.stats.clone(); let storage = self.storage.clone(); let mrf_rx = self.mrf_replica_rx.lock().await.take(); if let Some(rx) = mrf_rx { let handle = tokio::spawn(async move { let mut rx = rx; while let Some(operation) = rx.recv().await { active_counter.fetch_add(1, Ordering::SeqCst); match operation { ReplicationOperation::Object(obj_info) => { stats .inc_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; replicate_object(obj_info.as_ref().clone(), storage.clone()).await; stats .dec_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; } ReplicationOperation::Delete(del_info) => { replicate_delete(*del_info, storage.clone()).await; } } active_counter.fetch_sub(1, Ordering::SeqCst); } }); self.task_handles.lock().await.push(handle); break; // Only one receiver can be taken } } // Remove workers if needed while self.mrf_worker_size.load(Ordering::SeqCst) > n { self.mrf_worker_size.fetch_sub(1, Ordering::SeqCst); let _ = self.mrf_worker_kill_tx.try_send(()); // Signal worker to stop } } /// Resizes worker priority and counts pub async fn resize_worker_priority( &self, pri: ReplicationPriority, max_workers: Option<usize>, max_l_workers: Option<usize>, ) { let (workers, mrf_workers) = match pri { ReplicationPriority::Fast => (WORKER_MAX_LIMIT, MRF_WORKER_MAX_LIMIT), ReplicationPriority::Slow => (WORKER_MIN_LIMIT, MRF_WORKER_MIN_LIMIT), ReplicationPriority::Auto => { let mut workers = WORKER_AUTO_DEFAULT; let mut mrf_workers = MRF_WORKER_AUTO_DEFAULT; let current_workers = self.workers.read().await.len(); if current_workers < WORKER_AUTO_DEFAULT { workers = std::cmp::min(current_workers + 1, WORKER_AUTO_DEFAULT); } let current_mrf = self.mrf_worker_size.load(Ordering::SeqCst) as usize; if current_mrf < MRF_WORKER_AUTO_DEFAULT { mrf_workers = std::cmp::min(current_mrf + 1, MRF_WORKER_AUTO_DEFAULT); } (workers, mrf_workers) } }; let (final_workers, final_mrf_workers) = if let Some(max_w) = max_workers { *self.max_workers.write().await = max_w; (std::cmp::min(workers, max_w), std::cmp::min(mrf_workers, max_w)) } else { (workers, mrf_workers) }; let max_l_workers_val = max_l_workers.unwrap_or(LARGE_WORKER_COUNT); *self.max_l_workers.write().await = max_l_workers_val; *self.priority.write().await = pri; self.resize_workers(final_workers, 0).await; self.resize_failed_workers(final_mrf_workers as i32).await; self.resize_lrg_workers(max_l_workers_val, 0).await; } /// Gets a worker channel deterministically based on bucket and object names async fn get_worker_ch(&self, bucket: &str, object: &str, _size: i64) -> Option<Sender<ReplicationOperation>> { use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; let mut hasher = DefaultHasher::new(); format!("{bucket}{object}").hash(&mut hasher); let hash = hasher.finish(); let workers = self.workers.read().await; if workers.is_empty() { return None; } let index = (hash as usize) % workers.len(); workers.get(index).cloned() } /// Queues a replica task pub async fn queue_replica_task(&self, ri: ReplicateObjectInfo) { // If object is large, queue it to a static set of large workers if ri.size >= MIN_LARGE_OBJ_SIZE { use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; let mut hasher = DefaultHasher::new(); format!("{}{}", ri.bucket, ri.name).hash(&mut hasher); let hash = hasher.finish(); let lrg_workers = self.lrg_workers.read().await; if !lrg_workers.is_empty() { let index = (hash as usize) % lrg_workers.len(); if let Some(worker) = lrg_workers.get(index) && worker.try_send(ReplicationOperation::Object(Box::new(ri.clone()))).is_err() { // Queue to MRF if worker is busy let _ = self.mrf_save_tx.try_send(ri.to_mrf_entry()); // Try to add more workers if possible let max_l_workers = *self.max_l_workers.read().await; let existing = lrg_workers.len(); if self.active_lrg_workers() < std::cmp::min(max_l_workers, LARGE_WORKER_COUNT) as i32 { let workers = std::cmp::min(existing + 1, max_l_workers); drop(lrg_workers); self.resize_lrg_workers(workers, existing).await; } } } return; } // Handle regular sized objects let ch = match ri.op_type { ReplicationType::Heal | ReplicationType::ExistingObject => Some(self.mrf_replica_tx.clone()), _ => self.get_worker_ch(&ri.bucket, &ri.name, ri.size).await, }; if let Some(channel) = ch && channel.try_send(ReplicationOperation::Object(Box::new(ri.clone()))).is_err() { // Queue to MRF if all workers are busy let _ = self.mrf_save_tx.try_send(ri.to_mrf_entry()); // Try to scale up workers based on priority let priority = self.priority.read().await.clone(); let max_workers = *self.max_workers.read().await; match priority { ReplicationPriority::Fast => { // Log warning about unable to keep up info!("Warning: Unable to keep up with incoming traffic"); } ReplicationPriority::Slow => { info!("Warning: Unable to keep up with incoming traffic - recommend increasing replication priority to auto"); } ReplicationPriority::Auto => { let max_w = std::cmp::min(max_workers, WORKER_MAX_LIMIT); let active_workers = self.active_workers(); if active_workers < max_w as i32 { let workers = self.workers.read().await; let new_count = std::cmp::min(workers.len() + 1, max_w); let existing = workers.len(); drop(workers); self.resize_workers(new_count, existing).await; } let max_mrf_workers = std::cmp::min(max_workers, MRF_WORKER_MAX_LIMIT); let active_mrf = self.active_mrf_workers(); if active_mrf < max_mrf_workers as i32 { let current_mrf = self.mrf_worker_size.load(Ordering::SeqCst); let new_mrf = std::cmp::min(current_mrf + 1, max_mrf_workers as i32); self.resize_failed_workers(new_mrf).await; } } } } } /// Queues a replica delete task pub async fn queue_replica_delete_task(&self, doi: DeletedObjectReplicationInfo) { let ch = match doi.op_type { ReplicationType::Heal | ReplicationType::ExistingObject => Some(self.mrf_replica_tx.clone()), _ => self.get_worker_ch(&doi.bucket, &doi.delete_object.object_name, 0).await, }; if let Some(channel) = ch && channel.try_send(ReplicationOperation::Delete(Box::new(doi.clone()))).is_err() { let _ = self.mrf_save_tx.try_send(doi.to_mrf_entry()); let priority = self.priority.read().await.clone(); let max_workers = *self.max_workers.read().await; match priority { ReplicationPriority::Fast => { info!("Warning: Unable to keep up with incoming deletes"); } ReplicationPriority::Slow => { info!("Warning: Unable to keep up with incoming deletes - recommend increasing replication priority to auto"); } ReplicationPriority::Auto => { let max_w = std::cmp::min(max_workers, WORKER_MAX_LIMIT); if self.active_workers() < max_w as i32 { let workers = self.workers.read().await; let new_count = std::cmp::min(workers.len() + 1, max_w); let existing = workers.len(); drop(workers); self.resize_workers(new_count, existing).await; } } } } } /// Queues an MRF save operation async fn queue_mrf_save(&self, entry: MrfReplicateEntry) { let _ = self.mrf_save_tx.try_send(entry); } /// Starts the MRF processor background task async fn start_mrf_processor(&self) { // This would start a background task to process MRF entries // Implementation depends on the actual MRF processing logic } /// Starts the MRF persister background task async fn start_mrf_persister(&self) { // This would start a background task to persist MRF entries to disk // Implementation depends on the actual persistence logic } /// Worker function for handling regular replication operations async fn add_worker( &self, mut rx: Receiver<ReplicationOperation>, active_counter: Arc<AtomicI32>, stats: Arc<ReplicationStats>, ) { while let Some(operation) = rx.recv().await { active_counter.fetch_add(1, Ordering::SeqCst); match operation { ReplicationOperation::Object(obj_info) => { stats .inc_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; // Perform actual replication (placeholder) replicate_object(obj_info.as_ref().clone(), self.storage.clone()).await; stats .dec_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; } ReplicationOperation::Delete(del_info) => { stats.inc_q(&del_info.bucket, 0, true, del_info.op_type).await; // Perform actual delete replication (placeholder) replicate_delete(del_info.as_ref().clone(), self.storage.clone()).await; stats.dec_q(&del_info.bucket, 0, true, del_info.op_type).await; } } active_counter.fetch_sub(1, Ordering::SeqCst); } } /// Worker function for handling large object replication operations async fn add_large_worker(&self, mut rx: Receiver<ReplicationOperation>, active_counter: Arc<AtomicI32>, storage: Arc<S>) { while let Some(operation) = rx.recv().await { active_counter.fetch_add(1, Ordering::SeqCst); match operation { ReplicationOperation::Object(obj_info) => { replicate_object(*obj_info, storage.clone()).await; } ReplicationOperation::Delete(del_info) => { replicate_delete(*del_info, storage.clone()).await; } } active_counter.fetch_sub(1, Ordering::SeqCst); } } /// Worker function for handling MRF (Most Recent Failures) operations async fn add_mrf_worker( &self, mut rx: Receiver<ReplicationOperation>, active_counter: Arc<AtomicI32>, stats: Arc<ReplicationStats>, ) { while let Some(operation) = rx.recv().await { active_counter.fetch_add(1, Ordering::SeqCst); match operation { ReplicationOperation::Object(obj_info) => { stats .inc_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; replicate_object(obj_info.as_ref().clone(), self.storage.clone()).await; stats .dec_q(&obj_info.bucket, obj_info.size, obj_info.delete_marker, obj_info.op_type) .await; } ReplicationOperation::Delete(del_info) => { replicate_delete(*del_info, self.storage.clone()).await; } } active_counter.fetch_sub(1, Ordering::SeqCst); } } /// Delete resync metadata from replication resync state in memory pub async fn delete_resync_metadata(&self, bucket: &str) { let mut status_map = self.resyncer.status_map.write().await; status_map.remove(bucket); // Note: global site resync metrics deletion would be handled here // global_site_resync_metrics.delete_bucket(bucket); } /// Initialize bucket replication resync for all buckets pub async fn init_resync_internal( self: Arc<Self>, cancellation_token: CancellationToken, buckets: Vec<String>, ) -> Result<(), EcstoreError> { // Load bucket metadata system in background let pool_clone = self.clone(); tokio::spawn(async move { pool_clone.start_resync_routine(buckets, cancellation_token).await; }); Ok(()) } /// Start the resync routine that runs in a loop async fn start_resync_routine(self: Arc<Self>, buckets: Vec<String>, cancellation_token: CancellationToken) { // Run the replication resync in a loop loop { let self_clone = self.clone(); let ctx = cancellation_token.clone(); tokio::select! { _ = cancellation_token.cancelled() => { return; } result = self_clone.load_resync(&buckets, ctx) => { if result.is_ok() { return; } } } // Generate random duration between 0 and 1 minute use rand::Rng; let duration_millis = rand::rng().random_range(0..60_000); let mut duration = Duration::from_millis(duration_millis); // Make sure to sleep at least a second to avoid high CPU ticks if duration < Duration::from_secs(1) { duration = Duration::from_secs(1); } tokio::time::sleep(duration).await; } } /// Load bucket replication resync statuses into memory async fn load_resync(self: Arc<Self>, buckets: &[String], cancellation_token: CancellationToken) -> Result<(), EcstoreError> { // TODO: add leader_lock // Make sure only one node running resync on the cluster // Note: Leader lock implementation would be needed here // let _lock_guard = global_leader_lock.get_lock().await?; for bucket in buckets { let meta = match load_bucket_resync_metadata(bucket, self.storage.clone()).await { Ok(meta) => meta, Err(err) => { if !matches!(err, EcstoreError::VolumeNotFound) { warn!("Error loading resync metadata for bucket {bucket}: {err:?}"); } continue; } }; // Store metadata in resyncer { let mut status_map = self.resyncer.status_map.write().await; status_map.insert(bucket.clone(), meta.clone()); } // Process target statistics let target_stats = meta.clone_tgt_stats(); for (arn, stats) in target_stats { match stats.resync_status { ResyncStatusType::ResyncFailed | ResyncStatusType::ResyncStarted | ResyncStatusType::ResyncPending => { // Note: This would spawn a resync task in a real implementation // For now, we just log the resync request let ctx = cancellation_token.clone(); let bucket_clone = bucket.clone(); let resync = self.resyncer.clone(); let storage = self.storage.clone(); tokio::spawn(async move { resync .resync_bucket( ctx, storage, true, ResyncOpts { bucket: bucket_clone, arn, resync_id: stats.resync_id, resync_before: stats.resync_before_date, }, ) .await; }); } _ => {} } } } Ok(()) } } /// Load bucket resync metadata from disk async fn load_bucket_resync_metadata<S: StorageAPI>( bucket: &str, obj_api: Arc<S>, ) -> Result<BucketReplicationResyncStatus, EcstoreError> { use std::convert::TryInto; let mut brs = BucketReplicationResyncStatus::new(); // Constants that would be defined elsewhere const REPLICATION_DIR: &str = "replication"; const RESYNC_FILE_NAME: &str = "resync.bin"; const RESYNC_META_FORMAT: u16 = 1; const RESYNC_META_VERSION: u16 = 1; const RESYNC_META_VERSION_V1: u16 = 1; let resync_dir_path = format!("{BUCKET_META_PREFIX}/{bucket}/{REPLICATION_DIR}"); let resync_file_path = format!("{resync_dir_path}/{RESYNC_FILE_NAME}"); let data = match read_config(obj_api, &resync_file_path).await { Ok(data) => data,
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
true
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/event/targetlist.rs
crates/ecstore/src/event/targetlist.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::sync::atomic::AtomicI64; use super::targetid::TargetID; #[derive(Default)] pub struct TargetList { pub current_send_calls: AtomicI64, pub total_events: AtomicI64, pub events_skipped: AtomicI64, pub events_errors_total: AtomicI64, //pub targets: HashMap<TargetID, Target>, //pub queue: AsyncEvent, //pub targetStats: HashMap<TargetID, TargetStat>, } impl TargetList { pub fn new() -> TargetList { TargetList::default() } } struct TargetStat { current_send_calls: i64, total_events: i64, failed_events: i64, } struct TargetIDResult { id: TargetID, err: std::io::Error, }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/event/mod.rs
crates/ecstore/src/event/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub mod name; pub mod targetid; pub mod targetlist;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/event/targetid.rs
crates/ecstore/src/event/targetid.rs
#![allow(clippy::all)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub struct TargetID { id: String, name: String, } impl TargetID { fn to_string(&self) -> String { format!("{}:{}", self.id, self.name) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/event/name.rs
crates/ecstore/src/event/name.rs
#![allow(unused_variables)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #[derive(Default)] pub enum EventName { ObjectAccessedGet, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold, ObjectAccessedHead, ObjectAccessedAttributes, ObjectCreatedCompleteMultipartUpload, ObjectCreatedCopy, ObjectCreatedPost, ObjectCreatedPut, ObjectCreatedPutRetention, ObjectCreatedPutLegalHold, ObjectCreatedPutTagging, ObjectCreatedDeleteTagging, ObjectRemovedDelete, ObjectRemovedDeleteMarkerCreated, ObjectRemovedDeleteAllVersions, ObjectRemovedNoOP, BucketCreated, BucketRemoved, ObjectReplicationFailed, ObjectReplicationComplete, ObjectReplicationMissedThreshold, ObjectReplicationReplicatedAfterThreshold, ObjectReplicationNotTracked, ObjectRestorePost, ObjectRestoreCompleted, ObjectTransitionFailed, ObjectTransitionComplete, ObjectManyVersions, ObjectLargeVersions, PrefixManyFolders, ILMDelMarkerExpirationDelete, ObjectSingleTypesEnd, ObjectAccessedAll, ObjectCreatedAll, ObjectRemovedAll, ObjectReplicationAll, ObjectRestoreAll, ObjectTransitionAll, ObjectScannerAll, #[default] Everything, } impl EventName { fn expand(&self) -> Vec<EventName> { todo!(); } fn mask(&self) -> u64 { todo!(); } } impl AsRef<str> for EventName { fn as_ref(&self) -> &str { match self { EventName::BucketCreated => "s3:BucketCreated:*", EventName::BucketRemoved => "s3:BucketRemoved:*", EventName::ObjectAccessedAll => "s3:ObjectAccessed:*", EventName::ObjectAccessedGet => "s3:ObjectAccessed:Get", EventName::ObjectAccessedGetRetention => "s3:ObjectAccessed:GetRetention", EventName::ObjectAccessedGetLegalHold => "s3:ObjectAccessed:GetLegalHold", EventName::ObjectAccessedHead => "s3:ObjectAccessed:Head", EventName::ObjectAccessedAttributes => "s3:ObjectAccessed:Attributes", EventName::ObjectCreatedAll => "s3:ObjectCreated:*", EventName::ObjectCreatedCompleteMultipartUpload => "s3:ObjectCreated:CompleteMultipartUpload", EventName::ObjectCreatedCopy => "s3:ObjectCreated:Copy", EventName::ObjectCreatedPost => "s3:ObjectCreated:Post", EventName::ObjectCreatedPut => "s3:ObjectCreated:Put", EventName::ObjectCreatedPutTagging => "s3:ObjectCreated:PutTagging", EventName::ObjectCreatedDeleteTagging => "s3:ObjectCreated:DeleteTagging", EventName::ObjectCreatedPutRetention => "s3:ObjectCreated:PutRetention", EventName::ObjectCreatedPutLegalHold => "s3:ObjectCreated:PutLegalHold", EventName::ObjectRemovedAll => "s3:ObjectRemoved:*", EventName::ObjectRemovedDelete => "s3:ObjectRemoved:Delete", EventName::ObjectRemovedDeleteMarkerCreated => "s3:ObjectRemoved:DeleteMarkerCreated", EventName::ObjectRemovedNoOP => "s3:ObjectRemoved:NoOP", EventName::ObjectRemovedDeleteAllVersions => "s3:ObjectRemoved:DeleteAllVersions", EventName::ILMDelMarkerExpirationDelete => "s3:LifecycleDelMarkerExpiration:Delete", EventName::ObjectReplicationAll => "s3:Replication:*", EventName::ObjectReplicationFailed => "s3:Replication:OperationFailedReplication", EventName::ObjectReplicationComplete => "s3:Replication:OperationCompletedReplication", EventName::ObjectReplicationNotTracked => "s3:Replication:OperationNotTracked", EventName::ObjectReplicationMissedThreshold => "s3:Replication:OperationMissedThreshold", EventName::ObjectReplicationReplicatedAfterThreshold => "s3:Replication:OperationReplicatedAfterThreshold", EventName::ObjectRestoreAll => "s3:ObjectRestore:*", EventName::ObjectRestorePost => "s3:ObjectRestore:Post", EventName::ObjectRestoreCompleted => "s3:ObjectRestore:Completed", EventName::ObjectTransitionAll => "s3:ObjectTransition:*", EventName::ObjectTransitionFailed => "s3:ObjectTransition:Failed", EventName::ObjectTransitionComplete => "s3:ObjectTransition:Complete", EventName::ObjectManyVersions => "s3:Scanner:ManyVersions", EventName::ObjectLargeVersions => "s3:Scanner:LargeVersions", EventName::PrefixManyFolders => "s3:Scanner:BigPrefix", _ => "", } } } impl From<&str> for EventName { fn from(s: &str) -> Self { match s { "s3:BucketCreated:*" => EventName::BucketCreated, "s3:BucketRemoved:*" => EventName::BucketRemoved, "s3:ObjectAccessed:*" => EventName::ObjectAccessedAll, "s3:ObjectAccessed:Get" => EventName::ObjectAccessedGet, "s3:ObjectAccessed:GetRetention" => EventName::ObjectAccessedGetRetention, "s3:ObjectAccessed:GetLegalHold" => EventName::ObjectAccessedGetLegalHold, "s3:ObjectAccessed:Head" => EventName::ObjectAccessedHead, "s3:ObjectAccessed:Attributes" => EventName::ObjectAccessedAttributes, "s3:ObjectCreated:*" => EventName::ObjectCreatedAll, "s3:ObjectCreated:CompleteMultipartUpload" => EventName::ObjectCreatedCompleteMultipartUpload, "s3:ObjectCreated:Copy" => EventName::ObjectCreatedCopy, "s3:ObjectCreated:Post" => EventName::ObjectCreatedPost, "s3:ObjectCreated:Put" => EventName::ObjectCreatedPut, "s3:ObjectCreated:PutRetention" => EventName::ObjectCreatedPutRetention, "s3:ObjectCreated:PutLegalHold" => EventName::ObjectCreatedPutLegalHold, "s3:ObjectCreated:PutTagging" => EventName::ObjectCreatedPutTagging, "s3:ObjectCreated:DeleteTagging" => EventName::ObjectCreatedDeleteTagging, "s3:ObjectRemoved:*" => EventName::ObjectRemovedAll, "s3:ObjectRemoved:Delete" => EventName::ObjectRemovedDelete, "s3:ObjectRemoved:DeleteMarkerCreated" => EventName::ObjectRemovedDeleteMarkerCreated, "s3:ObjectRemoved:NoOP" => EventName::ObjectRemovedNoOP, "s3:ObjectRemoved:DeleteAllVersions" => EventName::ObjectRemovedDeleteAllVersions, "s3:LifecycleDelMarkerExpiration:Delete" => EventName::ILMDelMarkerExpirationDelete, "s3:Replication:*" => EventName::ObjectReplicationAll, "s3:Replication:OperationFailedReplication" => EventName::ObjectReplicationFailed, "s3:Replication:OperationCompletedReplication" => EventName::ObjectReplicationComplete, "s3:Replication:OperationMissedThreshold" => EventName::ObjectReplicationMissedThreshold, "s3:Replication:OperationReplicatedAfterThreshold" => EventName::ObjectReplicationReplicatedAfterThreshold, "s3:Replication:OperationNotTracked" => EventName::ObjectReplicationNotTracked, "s3:ObjectRestore:*" => EventName::ObjectRestoreAll, "s3:ObjectRestore:Post" => EventName::ObjectRestorePost, "s3:ObjectRestore:Completed" => EventName::ObjectRestoreCompleted, "s3:ObjectTransition:Failed" => EventName::ObjectTransitionFailed, "s3:ObjectTransition:Complete" => EventName::ObjectTransitionComplete, "s3:ObjectTransition:*" => EventName::ObjectTransitionAll, "s3:Scanner:ManyVersions" => EventName::ObjectManyVersions, "s3:Scanner:LargeVersions" => EventName::ObjectLargeVersions, "s3:Scanner:BigPrefix" => EventName::PrefixManyFolders, _ => EventName::Everything, } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_s3.rs
crates/ecstore/src/tier/warm_backend_s3.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use url::Url; use crate::client::{ api_get_options::GetObjectOptions, api_put_object::PutObjectOptions, api_remove::RemoveObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{Options, TransitionClient, TransitionCore}, transition_api::{ReadCloser, ReaderImpl}, }; use crate::error::ErrorResponse; use crate::error::error_resp_to_object_err; use crate::tier::{ tier_config::TierS3, warm_backend::{WarmBackend, WarmBackendGetOpts}, }; use rustfs_utils::path::SLASH_SEPARATOR; pub struct WarmBackendS3 { pub client: Arc<TransitionClient>, pub core: TransitionCore, pub bucket: String, pub prefix: String, pub storage_class: String, } impl WarmBackendS3 { pub async fn new(conf: &TierS3, tier: &str) -> Result<Self, std::io::Error> { let u = match Url::parse(&conf.endpoint) { Ok(u) => u, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; if conf.aws_role_web_identity_token_file == "" && conf.aws_role_arn != "" || conf.aws_role_web_identity_token_file != "" && conf.aws_role_arn == "" { return Err(std::io::Error::other("both the token file and the role ARN are required")); } else if conf.access_key == "" && conf.secret_key != "" || conf.access_key != "" && conf.secret_key == "" { return Err(std::io::Error::other("both the access and secret keys are required")); } else if conf.aws_role && (conf.aws_role_web_identity_token_file != "" || conf.aws_role_arn != "" || conf.access_key != "" || conf.secret_key != "") { return Err(std::io::Error::other( "AWS Role cannot be activated with static credentials or the web identity token file", )); } else if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let creds: Credentials<Static>; if conf.access_key != "" && conf.secret_key != "" { //creds = Credentials::new_static_v4(conf.access_key, conf.secret_key, ""); creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); } else { return Err(std::io::Error::other("insufficient parameters for S3 backend authentication")); } let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, region: conf.region.clone(), ..Default::default() }; let client = TransitionClient::new(&u.host().expect("err").to_string(), opts, "s3").await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.clone().trim_matches('/').to_string(), storage_class: conf.storage_class.clone(), }) } pub fn get_dest(&self, object: &str) -> String { let mut dest_obj = object.to_string(); if self.prefix != "" { dest_obj = format!("{}/{}", &self.prefix, object); } return dest_obj; } } #[async_trait::async_trait] impl WarmBackend for WarmBackendS3 { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let client = self.client.clone(); let res = client .put_object( &self.bucket, &self.get_dest(object), r, length, &PutObjectOptions { send_content_md5: true, storage_class: self.storage_class.clone(), user_metadata: meta, ..Default::default() }, ) .await?; Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { let mut gopts = GetObjectOptions::default(); if rv != "" { gopts.version_id = rv.to_string(); } if opts.start_offset >= 0 && opts.length > 0 { if let Err(err) = gopts.set_range(opts.start_offset, opts.start_offset + opts.length - 1) { return Err(std::io::Error::other(err)); } } let c = TransitionCore(Arc::clone(&self.client)); let (_, _, r) = c.get_object(&self.bucket, &self.get_dest(object), &gopts).await?; Ok(r) } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { let mut ropts = RemoveObjectOptions::default(); if rv != "" { ropts.version_id = rv.to_string(); } let client = self.client.clone(); let err = client.remove_object(&self.bucket, &self.get_dest(object), ropts).await; Err(std::io::Error::other(err.expect("err"))) } async fn in_use(&self) -> Result<bool, std::io::Error> { let result = self .core .list_objects_v2(&self.bucket, &self.prefix, "", "", SLASH_SEPARATOR, 1) .await?; Ok(result.common_prefixes.len() > 0 || result.contents.len() > 0) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/tier_config.rs
crates/ecstore/src/tier/tier_config.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use serde::{Deserialize, Serialize}; use std::fmt::Display; use tracing::info; const C_TIER_CONFIG_VER: &str = "v1"; const ERR_TIER_NAME_EMPTY: &str = "remote tier name empty"; #[derive(Serialize, Deserialize, Default, Debug, Clone)] pub enum TierType { #[default] Unsupported, #[serde(rename = "s3")] S3, #[serde(rename = "rustfs")] RustFS, #[serde(rename = "minio")] MinIO, #[serde(rename = "aliyun")] Aliyun, #[serde(rename = "tencent")] Tencent, #[serde(rename = "huaweicloud")] Huaweicloud, #[serde(rename = "azure")] Azure, #[serde(rename = "gcs")] GCS, #[serde(rename = "r2")] R2, } impl Display for TierType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { TierType::S3 => { write!(f, "S3") } TierType::RustFS => { write!(f, "RustFS") } TierType::MinIO => { write!(f, "MinIO") } TierType::Aliyun => { write!(f, "Aliyun") } TierType::Tencent => { write!(f, "Tencent") } TierType::Huaweicloud => { write!(f, "Huaweicloud") } TierType::Azure => { write!(f, "Azure") } TierType::GCS => { write!(f, "GCS") } TierType::R2 => { write!(f, "R2") } _ => { write!(f, "Unsupported") } } } } impl TierType { pub fn new(sc_type: &str) -> Self { match sc_type { "S3" => TierType::S3, "RustFS" => TierType::RustFS, "MinIO" => TierType::MinIO, "Aliyun" => TierType::Aliyun, "Tencent" => TierType::Tencent, "Huaweicloud" => TierType::Huaweicloud, "Azure" => TierType::Azure, "GCS" => TierType::GCS, "R2" => TierType::R2, _ => TierType::Unsupported, } } pub fn as_lowercase(&self) -> String { match self { TierType::S3 => "s3".to_string(), TierType::RustFS => "rustfs".to_string(), TierType::MinIO => "minio".to_string(), TierType::Aliyun => "aliyun".to_string(), TierType::Tencent => "tencent".to_string(), TierType::Huaweicloud => "huaweicloud".to_string(), TierType::Azure => "azure".to_string(), TierType::GCS => "gcs".to_string(), TierType::R2 => "r2".to_string(), _ => "unsupported".to_string(), } } } #[derive(Default, Debug, Serialize, Deserialize)] #[serde(default)] pub struct TierConfig { #[serde(skip)] pub version: String, #[serde(rename = "type")] pub tier_type: TierType, #[serde(skip)] pub name: String, #[serde(rename = "s3", skip_serializing_if = "Option::is_none")] pub s3: Option<TierS3>, #[serde(rename = "aliyun", skip_serializing_if = "Option::is_none")] pub aliyun: Option<TierAliyun>, #[serde(rename = "tencent", skip_serializing_if = "Option::is_none")] pub tencent: Option<TierTencent>, #[serde(rename = "huaweicloud", skip_serializing_if = "Option::is_none")] pub huaweicloud: Option<TierHuaweicloud>, #[serde(rename = "azure", skip_serializing_if = "Option::is_none")] pub azure: Option<TierAzure>, #[serde(rename = "gcs", skip_serializing_if = "Option::is_none")] pub gcs: Option<TierGCS>, #[serde(rename = "r2", skip_serializing_if = "Option::is_none")] pub r2: Option<TierR2>, #[serde(rename = "rustfs", skip_serializing_if = "Option::is_none")] pub rustfs: Option<TierRustFS>, #[serde(rename = "minio", skip_serializing_if = "Option::is_none")] pub minio: Option<TierMinIO>, } impl Clone for TierConfig { fn clone(&self) -> TierConfig { let mut s3 = None; let mut r = None; let mut m = None; let mut aliyun = None; let mut tencent = None; let mut huaweicloud = None; let mut azure = None; let mut gcs = None; let mut r2 = None; match self.tier_type { TierType::S3 => { let mut s3_ = self.s3.as_ref().expect("err").clone(); s3_.secret_key = "REDACTED".to_string(); s3 = Some(s3_); } TierType::RustFS => { let mut r_ = self.rustfs.as_ref().expect("err").clone(); r_.secret_key = "REDACTED".to_string(); r = Some(r_); } TierType::MinIO => { let mut m_ = self.minio.as_ref().expect("err").clone(); m_.secret_key = "REDACTED".to_string(); m = Some(m_); } TierType::Aliyun => { let mut aliyun_ = self.aliyun.as_ref().expect("err").clone(); aliyun_.secret_key = "REDACTED".to_string(); aliyun = Some(aliyun_); } TierType::Tencent => { let mut tencent_ = self.tencent.as_ref().expect("err").clone(); tencent_.secret_key = "REDACTED".to_string(); tencent = Some(tencent_); } TierType::Huaweicloud => { let mut huaweicloud_ = self.huaweicloud.as_ref().expect("err").clone(); huaweicloud_.secret_key = "REDACTED".to_string(); huaweicloud = Some(huaweicloud_); } TierType::Azure => { let mut azure_ = self.azure.as_ref().expect("err").clone(); azure_.secret_key = "REDACTED".to_string(); azure = Some(azure_); } TierType::GCS => { let mut gcs_ = self.gcs.as_ref().expect("err").clone(); gcs_.creds = "REDACTED".to_string(); gcs = Some(gcs_); } TierType::R2 => { let mut r2_ = self.r2.as_ref().expect("err").clone(); r2_.secret_key = "REDACTED".to_string(); r2 = Some(r2_); } _ => (), } TierConfig { version: self.version.clone(), tier_type: self.tier_type.clone(), name: self.name.clone(), s3, rustfs: r, minio: m, aliyun, tencent, huaweicloud, azure, gcs, r2, } } } #[allow(dead_code)] impl TierConfig { fn endpoint(&self) -> String { match self.tier_type { TierType::S3 => self.s3.as_ref().expect("err").endpoint.clone(), TierType::RustFS => self.rustfs.as_ref().expect("err").endpoint.clone(), TierType::MinIO => self.minio.as_ref().expect("err").endpoint.clone(), TierType::Aliyun => self.aliyun.as_ref().expect("err").endpoint.clone(), TierType::Tencent => self.tencent.as_ref().expect("err").endpoint.clone(), TierType::Huaweicloud => self.huaweicloud.as_ref().expect("err").endpoint.clone(), TierType::Azure => self.azure.as_ref().expect("err").endpoint.clone(), TierType::GCS => self.gcs.as_ref().expect("err").endpoint.clone(), TierType::R2 => self.r2.as_ref().expect("err").endpoint.clone(), _ => { info!("unexpected tier type {}", self.tier_type); "".to_string() } } } fn bucket(&self) -> String { match self.tier_type { TierType::S3 => self.s3.as_ref().expect("err").bucket.clone(), TierType::RustFS => self.rustfs.as_ref().expect("err").bucket.clone(), TierType::MinIO => self.minio.as_ref().expect("err").bucket.clone(), TierType::Aliyun => self.aliyun.as_ref().expect("err").bucket.clone(), TierType::Tencent => self.tencent.as_ref().expect("err").bucket.clone(), TierType::Huaweicloud => self.huaweicloud.as_ref().expect("err").bucket.clone(), TierType::Azure => self.azure.as_ref().expect("err").bucket.clone(), TierType::GCS => self.gcs.as_ref().expect("err").bucket.clone(), TierType::R2 => self.r2.as_ref().expect("err").bucket.clone(), _ => { info!("unexpected tier type {}", self.tier_type); "".to_string() } } } fn prefix(&self) -> String { match self.tier_type { TierType::S3 => self.s3.as_ref().expect("err").prefix.clone(), TierType::RustFS => self.rustfs.as_ref().expect("err").prefix.clone(), TierType::MinIO => self.minio.as_ref().expect("err").prefix.clone(), TierType::Aliyun => self.aliyun.as_ref().expect("err").prefix.clone(), TierType::Tencent => self.tencent.as_ref().expect("err").prefix.clone(), TierType::Huaweicloud => self.huaweicloud.as_ref().expect("err").prefix.clone(), TierType::Azure => self.azure.as_ref().expect("err").prefix.clone(), TierType::GCS => self.gcs.as_ref().expect("err").prefix.clone(), TierType::R2 => self.r2.as_ref().expect("err").prefix.clone(), _ => { info!("unexpected tier type {}", self.tier_type); "".to_string() } } } fn region(&self) -> String { match self.tier_type { TierType::S3 => self.s3.as_ref().expect("err").region.clone(), TierType::RustFS => self.rustfs.as_ref().expect("err").region.clone(), TierType::MinIO => self.minio.as_ref().expect("err").region.clone(), TierType::Aliyun => self.aliyun.as_ref().expect("err").region.clone(), TierType::Tencent => self.tencent.as_ref().expect("err").region.clone(), TierType::Huaweicloud => self.huaweicloud.as_ref().expect("err").region.clone(), TierType::Azure => self.azure.as_ref().expect("err").region.clone(), TierType::GCS => self.gcs.as_ref().expect("err").region.clone(), TierType::R2 => self.r2.as_ref().expect("err").region.clone(), _ => { info!("unexpected tier type {}", self.tier_type); "".to_string() } } } } //type S3Options = impl Fn(TierS3) -> Pin<Box<Result<()>>> + Send + Sync + 'static; #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierS3 { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, #[serde(rename = "storageClass")] pub storage_class: String, #[serde(skip)] pub aws_role: bool, #[serde(skip)] pub aws_role_web_identity_token_file: String, #[serde(skip)] pub aws_role_arn: String, #[serde(skip)] pub aws_role_session_name: String, #[serde(skip)] pub aws_role_duration_seconds: i32, } impl TierS3 { #[allow(dead_code)] fn create<F>( name: &str, access_key: &str, secret_key: &str, bucket: &str, options: Vec<F>, ) -> Result<TierConfig, std::io::Error> where F: Fn(TierS3) -> Box<Result<(), std::io::Error>> + Send + Sync + 'static, { if name.is_empty() { return Err(std::io::Error::other(ERR_TIER_NAME_EMPTY)); } let sc = TierS3 { access_key: access_key.to_string(), secret_key: secret_key.to_string(), bucket: bucket.to_string(), endpoint: "https://s3.amazonaws.com".to_string(), region: "".to_string(), storage_class: "".to_string(), ..Default::default() }; for option in options { let option = option(sc.clone()); let option = *option; option?; } Ok(TierConfig { version: C_TIER_CONFIG_VER.to_string(), tier_type: TierType::S3, name: name.to_string(), s3: Some(sc), ..Default::default() }) } } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierRustFS { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, #[serde(rename = "storageClass")] pub storage_class: String, } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierMinIO { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, } impl TierMinIO { #[allow(dead_code)] fn create<F>( name: &str, endpoint: &str, access_key: &str, secret_key: &str, bucket: &str, options: Vec<F>, ) -> Result<TierConfig, std::io::Error> where F: Fn(TierMinIO) -> Box<Result<(), std::io::Error>> + Send + Sync + 'static, { if name.is_empty() { return Err(std::io::Error::other(ERR_TIER_NAME_EMPTY)); } let m = TierMinIO { access_key: access_key.to_string(), secret_key: secret_key.to_string(), bucket: bucket.to_string(), endpoint: endpoint.to_string(), ..Default::default() }; for option in options { let option = option(m.clone()); let option = *option; option?; } Ok(TierConfig { version: C_TIER_CONFIG_VER.to_string(), tier_type: TierType::MinIO, name: name.to_string(), minio: Some(m), ..Default::default() }) } } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierAliyun { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierTencent { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierHuaweicloud { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct ServicePrincipalAuth { pub tenant_id: String, pub client_id: String, pub client_secret: String, } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierAzure { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, #[serde(rename = "storageClass")] pub storage_class: String, #[serde(rename = "spAuth")] pub sp_auth: ServicePrincipalAuth, } impl TierAzure { pub fn is_sp_enabled(&self) -> bool { !self.sp_auth.tenant_id.is_empty() && !self.sp_auth.client_id.is_empty() && !self.sp_auth.client_secret.is_empty() } } /* fn AzureServicePrincipal(tenantID, clientID, clientSecret string) func(az *TierAzure) error { return func(az *TierAzure) error { if tenantID == "" { return errors.New("empty tenant ID unsupported") } if clientID == "" { return errors.New("empty client ID unsupported") } if clientSecret == "" { return errors.New("empty client secret unsupported") } az.SPAuth.TenantID = tenantID az.SPAuth.ClientID = clientID az.SPAuth.ClientSecret = clientSecret return nil } } fn AzurePrefix(prefix string) func(az *TierAzure) error { return func(az *TierAzure) error { az.Prefix = prefix return nil } } fn AzureEndpoint(endpoint string) func(az *TierAzure) error { return func(az *TierAzure) error { az.Endpoint = endpoint return nil } } fn AzureRegion(region string) func(az *TierAzure) error { return func(az *TierAzure) error { az.Region = region return nil } } fn AzureStorageClass(sc string) func(az *TierAzure) error { return func(az *TierAzure) error { az.StorageClass = sc return nil } }*/ #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierGCS { pub name: String, pub endpoint: String, #[serde(rename = "creds")] pub creds: String, pub bucket: String, pub prefix: String, pub region: String, #[serde(rename = "storageClass")] pub storage_class: String, } #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierR2 { pub name: String, pub endpoint: String, #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, pub bucket: String, pub prefix: String, pub region: String, }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/tier_admin.rs
crates/ecstore/src/tier/tier_admin.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Default, Debug, Clone)] #[serde(default)] pub struct TierCreds { #[serde(rename = "accessKey")] pub access_key: String, #[serde(rename = "secretKey")] pub secret_key: String, #[serde(rename = "awsRole")] pub aws_role: bool, #[serde(rename = "awsRoleWebIdentityTokenFile")] pub aws_role_web_identity_token_file: String, #[serde(rename = "awsRoleArn")] pub aws_role_arn: String, //azsp: ServicePrincipalAuth, //#[serde(rename = "credsJson")] pub creds_json: Vec<u8>, }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_aliyun.rs
crates/ecstore/src/tier/warm_backend_aliyun.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{BucketLookupType, Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierAliyun, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendAliyun(WarmBackendS3); impl WarmBackendAliyun { pub async fn new(conf: &TierAliyun, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => { return Err(std::io::Error::other(e.to_string())); } }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), bucket_lookup: BucketLookupType::BucketLookupDNS, ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "aliyun", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendAliyun { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_tencent.rs
crates/ecstore/src/tier/warm_backend_tencent.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{BucketLookupType, Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierTencent, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendTencent(WarmBackendS3); impl WarmBackendTencent { pub async fn new(conf: &TierTencent, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => { return Err(std::io::Error::other(e.to_string())); } }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), bucket_lookup: BucketLookupType::BucketLookupDNS, ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "tencent", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendTencent { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_rustfs.rs
crates/ecstore/src/tier/warm_backend_rustfs.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierRustFS, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendRustFS(WarmBackendS3); impl WarmBackendRustFS { pub async fn new(conf: &TierRustFS, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => return Err(std::io::Error::other(e)), }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "rustfs", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendRustFS { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/tier.rs
crates/ecstore/src/tier/tier.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::status::StatusCode; use lazy_static::lazy_static; use rand::Rng; use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, hash_map::Entry}, io::Cursor, sync::Arc, time::Duration, }; use time::OffsetDateTime; use tokio::io::BufReader; use tokio::{select, sync::RwLock, time::interval}; use tracing::{debug, error, info, warn}; use crate::client::admin_handler_utils::AdminError; use crate::error::{Error, Result, StorageError}; use crate::new_object_layer_fn; use crate::tier::{ tier_admin::TierCreds, tier_config::{TierConfig, TierType}, tier_handlers::{ERR_TIER_ALREADY_EXISTS, ERR_TIER_NAME_NOT_UPPERCASE, ERR_TIER_NOT_FOUND}, warm_backend::{check_warm_backend, new_warm_backend}, }; use crate::{ StorageAPI, config::com::{CONFIG_PREFIX, read_config}, disk::RUSTFS_META_BUCKET, store::ECStore, store_api::{ObjectOptions, PutObjReader}, }; use rustfs_rio::HashReader; use rustfs_utils::path::{SLASH_SEPARATOR, path_join}; use s3s::S3ErrorCode; use super::{ tier_handlers::{ERR_TIER_BUCKET_NOT_FOUND, ERR_TIER_CONNECT_ERR, ERR_TIER_INVALID_CREDENTIALS, ERR_TIER_PERM_ERR}, warm_backend::WarmBackendImpl, }; const TIER_CFG_REFRESH: Duration = Duration::from_secs(15 * 60); pub const TIER_CONFIG_FILE: &str = "tier-config.json"; pub const TIER_CONFIG_FORMAT: u16 = 1; pub const TIER_CONFIG_V1: u16 = 1; pub const TIER_CONFIG_VERSION: u16 = 1; const _TIER_CFG_REFRESH_AT_HDR: &str = "X-RustFS-TierCfg-RefreshedAt"; lazy_static! { pub static ref ERR_TIER_MISSING_CREDENTIALS: AdminError = AdminError { code: "XRustFSAdminTierMissingCredentials".to_string(), message: "Specified remote credentials are empty".to_string(), status_code: StatusCode::FORBIDDEN, }; pub static ref ERR_TIER_BACKEND_IN_USE: AdminError = AdminError { code: "XRustFSAdminTierBackendInUse".to_string(), message: "Specified remote tier is already in use".to_string(), status_code: StatusCode::CONFLICT, }; pub static ref ERR_TIER_TYPE_UNSUPPORTED: AdminError = AdminError { code: "XRustFSAdminTierTypeUnsupported".to_string(), message: "Specified tier type is unsupported".to_string(), status_code: StatusCode::BAD_REQUEST, }; pub static ref ERR_TIER_BACKEND_NOT_EMPTY: AdminError = AdminError { code: "XRustFSAdminTierBackendNotEmpty".to_string(), message: "Specified remote backend is not empty".to_string(), status_code: StatusCode::BAD_REQUEST, }; pub static ref ERR_TIER_INVALID_CONFIG: AdminError = AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: "Unable to setup remote tier, check tier configuration".to_string(), status_code: StatusCode::BAD_REQUEST, }; } #[derive(Serialize, Deserialize)] pub struct TierConfigMgr { #[serde(skip)] pub driver_cache: HashMap<String, WarmBackendImpl>, pub tiers: HashMap<String, TierConfig>, pub last_refreshed_at: OffsetDateTime, } impl TierConfigMgr { pub fn new() -> Arc<RwLock<Self>> { Arc::new(RwLock::new(Self { driver_cache: HashMap::new(), tiers: HashMap::new(), last_refreshed_at: OffsetDateTime::now_utc(), })) } pub fn unmarshal(data: &[u8]) -> std::result::Result<TierConfigMgr, std::io::Error> { let cfg: TierConfigMgr = serde_json::from_slice(data)?; Ok(cfg) } pub fn marshal(&self) -> std::result::Result<Bytes, std::io::Error> { let data = serde_json::to_vec(&self)?; let mut data = Bytes::from(data); Ok(data) } pub fn refreshed_at(&self) -> OffsetDateTime { self.last_refreshed_at } pub fn is_tier_valid(&self, tier_name: &str) -> bool { let (_, valid) = self.is_tier_name_in_use(tier_name); valid } pub fn is_tier_name_in_use(&self, tier_name: &str) -> (TierType, bool) { if let Some(t) = self.tiers.get(tier_name) { return (t.tier_type.clone(), true); } (TierType::Unsupported, false) } pub async fn add(&mut self, tier_config: TierConfig, force: bool) -> std::result::Result<(), AdminError> { let tier_name = &tier_config.name; if tier_name != tier_name.to_uppercase().as_str() { return Err(ERR_TIER_NAME_NOT_UPPERCASE.clone()); } let (_, b) = self.is_tier_name_in_use(tier_name); if b { return Err(ERR_TIER_ALREADY_EXISTS.clone()); } let d = new_warm_backend(&tier_config, true).await?; if !force { let in_use = d.in_use().await; match in_use { Ok(b) => { if b { return Err(ERR_TIER_BACKEND_IN_USE.clone()); } } Err(err) => { warn!("tier add failed, err: {:?}", err); if err.to_string().contains("connect") { return Err(ERR_TIER_CONNECT_ERR.clone()); } else if err.to_string().contains("authorization") { return Err(ERR_TIER_INVALID_CREDENTIALS.clone()); } else if err.to_string().contains("bucket") { return Err(ERR_TIER_BUCKET_NOT_FOUND.clone()); } let mut e = ERR_TIER_PERM_ERR.clone(); e.message.push('.'); e.message.push_str(&err.to_string()); return Err(e); } } } self.driver_cache.insert(tier_name.to_string(), d); self.tiers.insert(tier_name.to_string(), tier_config); Ok(()) } pub async fn remove(&mut self, tier_name: &str, force: bool) -> std::result::Result<(), AdminError> { let d = self.get_driver(tier_name).await; if let Err(err) = d { if err.code == ERR_TIER_NOT_FOUND.code { return Ok(()); } else { return Err(err); } } if !force { let inuse = d.expect("err").in_use().await; if let Err(err) = inuse { let mut e = ERR_TIER_PERM_ERR.clone(); e.message.push('.'); e.message.push_str(&err.to_string()); return Err(e); } else if inuse.expect("err") { return Err(ERR_TIER_BACKEND_NOT_EMPTY.clone()); } } self.tiers.remove(tier_name); self.driver_cache.remove(tier_name); Ok(()) } pub async fn verify(&mut self, tier_name: &str) -> std::result::Result<(), std::io::Error> { let d = match self.get_driver(tier_name).await { Ok(d) => d, Err(err) => { return Err(std::io::Error::other(err)); } }; if let Err(err) = check_warm_backend(Some(d)).await { return Err(std::io::Error::other(err)); } else { return Ok(()); } } pub fn empty(&self) -> bool { self.list_tiers().len() == 0 } pub fn tier_type(&self, tier_name: &str) -> String { let cfg = self.tiers.get(tier_name); if cfg.is_none() { return "internal".to_string(); } cfg.expect("err").tier_type.as_lowercase() } pub fn list_tiers(&self) -> Vec<TierConfig> { let mut tier_cfgs = Vec::<TierConfig>::new(); for (_, tier) in self.tiers.iter() { let tier = tier.clone(); tier_cfgs.push(tier); } tier_cfgs } pub fn get(&self, tier_name: &str) -> Option<TierConfig> { for (tier_name2, tier) in self.tiers.iter() { if tier_name == tier_name2 { return Some(tier.clone()); } } None } pub async fn edit(&mut self, tier_name: &str, creds: TierCreds) -> std::result::Result<(), AdminError> { let (tier_type, exists) = self.is_tier_name_in_use(tier_name); if !exists { return Err(ERR_TIER_NOT_FOUND.clone()); } let mut tier_config = self.tiers[tier_name].clone(); match tier_type { TierType::S3 => { let mut s3 = tier_config.s3.as_mut().expect("err"); if creds.aws_role { s3.aws_role = true } if creds.aws_role_web_identity_token_file != "" && creds.aws_role_arn != "" { s3.aws_role_arn = creds.aws_role_arn; s3.aws_role_web_identity_token_file = creds.aws_role_web_identity_token_file; } if creds.access_key != "" && creds.secret_key != "" { s3.access_key = creds.access_key; s3.secret_key = creds.secret_key; } } TierType::RustFS => { let mut rustfs = tier_config.rustfs.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } rustfs.access_key = creds.access_key; rustfs.secret_key = creds.secret_key; } TierType::MinIO => { let mut minio = tier_config.minio.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } minio.access_key = creds.access_key; minio.secret_key = creds.secret_key; } TierType::Aliyun => { let mut aliyun = tier_config.aliyun.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } aliyun.access_key = creds.access_key; aliyun.secret_key = creds.secret_key; } TierType::Tencent => { let mut tencent = tier_config.tencent.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } tencent.access_key = creds.access_key; tencent.secret_key = creds.secret_key; } TierType::Huaweicloud => { let mut huaweicloud = tier_config.huaweicloud.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } huaweicloud.access_key = creds.access_key; huaweicloud.secret_key = creds.secret_key; } TierType::Azure => { let mut azure = tier_config.azure.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } azure.access_key = creds.access_key; azure.secret_key = creds.secret_key; } TierType::GCS => { let mut gcs = tier_config.gcs.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } gcs.creds = creds.access_key; //creds.creds_json } TierType::R2 => { let mut r2 = tier_config.r2.as_mut().expect("err"); if creds.access_key == "" || creds.secret_key == "" { return Err(ERR_TIER_MISSING_CREDENTIALS.clone()); } r2.access_key = creds.access_key; r2.secret_key = creds.secret_key; } _ => (), } let d = new_warm_backend(&tier_config, true).await?; self.tiers.insert(tier_name.to_string(), tier_config); self.driver_cache.insert(tier_name.to_string(), d); Ok(()) } pub async fn get_driver<'a>(&'a mut self, tier_name: &str) -> std::result::Result<&'a WarmBackendImpl, AdminError> { // Return cached driver if present if self.driver_cache.contains_key(tier_name) { return Ok(self.driver_cache.get(tier_name).unwrap()); } // Get tier configuration and create new driver let tier_config = self.tiers.get(tier_name).ok_or_else(|| ERR_TIER_NOT_FOUND.clone())?; let driver = new_warm_backend(tier_config, false).await?; // Insert and return reference self.driver_cache.insert(tier_name.to_string(), driver); Ok(self.driver_cache.get(tier_name).unwrap()) } pub async fn reload(&mut self, api: Arc<ECStore>) -> std::result::Result<(), std::io::Error> { //let Some(api) = new_object_layer_fn() else { return Err(Error::msg("errServerNotInitialized")) }; let new_config = load_tier_config(api).await; match &new_config { Ok(_c) => {} Err(err) => { return Err(std::io::Error::other(err.to_string())); } } self.driver_cache.clear(); self.tiers.clear(); let new_config = new_config.expect("err"); for (tier, cfg) in new_config.tiers { self.tiers.insert(tier, cfg); } self.last_refreshed_at = OffsetDateTime::now_utc(); Ok(()) } pub async fn clear_tier(&mut self, force: bool) -> std::result::Result<(), AdminError> { self.tiers.clear(); self.driver_cache.clear(); Ok(()) } #[tracing::instrument(level = "debug", name = "tier_save", skip(self))] pub async fn save(&self) -> std::result::Result<(), std::io::Error> { let Some(api) = new_object_layer_fn() else { return Err(std::io::Error::other("errServerNotInitialized")); }; //let (pr, opts) = GLOBAL_TierConfigMgr.write().config_reader()?; self.save_tiering_config(api).await } pub async fn save_tiering_config<S: StorageAPI>(&self, api: Arc<S>) -> std::result::Result<(), std::io::Error> { let data = self.marshal()?; let config_file = format!("{}{}{}", CONFIG_PREFIX, SLASH_SEPARATOR, TIER_CONFIG_FILE); self.save_config(api, &config_file, data).await } pub async fn save_config<S: StorageAPI>( &self, api: Arc<S>, file: &str, data: Bytes, ) -> std::result::Result<(), std::io::Error> { self.save_config_with_opts( api, file, data, &ObjectOptions { max_parity: true, ..Default::default() }, ) .await } pub async fn save_config_with_opts<S: StorageAPI>( &self, api: Arc<S>, file: &str, data: Bytes, opts: &ObjectOptions, ) -> std::result::Result<(), std::io::Error> { debug!("save tier config:{}", file); let _ = api .put_object(RUSTFS_META_BUCKET, file, &mut PutObjReader::from_vec(data.to_vec()), opts) .await?; Ok(()) } pub async fn refresh_tier_config(&mut self, api: Arc<ECStore>) { //let r = rand.New(rand.NewSource(time.Now().UnixNano())); let mut rng = rand::rng(); let r = rng.random_range(0.0..1.0); let rand_interval = || Duration::from_secs((r * 60_f64).round() as u64); let mut t = interval(TIER_CFG_REFRESH + rand_interval()); loop { select! { _ = t.tick() => { if let Err(err) = self.reload(api.clone()).await { info!("{}", err); } } else => () } t.reset(); } } pub async fn init(&mut self, api: Arc<ECStore>) -> Result<()> { self.reload(api).await?; //if globalIsDistErasure { // self.refresh_tier_config(api).await; //} Ok(()) } } async fn new_and_save_tiering_config<S: StorageAPI>(api: Arc<S>) -> Result<TierConfigMgr> { let mut cfg = TierConfigMgr { driver_cache: HashMap::new(), tiers: HashMap::new(), last_refreshed_at: OffsetDateTime::now_utc(), }; //lookup_configs(&mut cfg, api.clone()).await; cfg.save_tiering_config(api).await?; Ok(cfg) } #[tracing::instrument(level = "debug", name = "load_tier_config", skip(api))] async fn load_tier_config(api: Arc<ECStore>) -> std::result::Result<TierConfigMgr, std::io::Error> { let config_file = format!("{}{}{}", CONFIG_PREFIX, SLASH_SEPARATOR, TIER_CONFIG_FILE); let data = read_config(api.clone(), config_file.as_str()).await; if let Err(err) = data { if is_err_config_not_found(&err) { warn!("config not found, start to init"); let cfg = new_and_save_tiering_config(api).await?; return Ok(cfg); } else { error!("read config err {:?}", &err); return Err(std::io::Error::other(err)); } } let cfg; let version = 1; //LittleEndian::read_u16(&data[2..4]); match version { TIER_CONFIG_V1/* | TIER_CONFIG_VERSION */ => { cfg = match TierConfigMgr::unmarshal(&data.unwrap()) { Ok(cfg) => cfg, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; } _ => { return Err(std::io::Error::other(format!("tierConfigInit: unknown version: {}", version))); } } Ok(cfg) } pub fn is_err_config_not_found(err: &StorageError) -> bool { matches!(err, StorageError::ObjectNotFound(_, _)) || err == &StorageError::ConfigNotFound }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/tier_handlers.rs
crates/ecstore/src/tier/tier_handlers.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::client::admin_handler_utils::AdminError; use http::status::StatusCode; use lazy_static::lazy_static; lazy_static! { pub static ref ERR_TIER_ALREADY_EXISTS: AdminError = AdminError { code: "XRustFSAdminTierAlreadyExists".to_string(), message: "Specified remote tier already exists".to_string(), status_code: StatusCode::CONFLICT, }; pub static ref ERR_TIER_NOT_FOUND: AdminError = AdminError { code: "XRustFSAdminTierNotFound".to_string(), message: "Specified remote tier was not found".to_string(), status_code: StatusCode::NOT_FOUND, }; pub static ref ERR_TIER_NAME_NOT_UPPERCASE: AdminError = AdminError { code: "XRustFSAdminTierNameNotUpperCase".to_string(), message: "Tier name must be in uppercase".to_string(), status_code: StatusCode::BAD_REQUEST, }; pub static ref ERR_TIER_BUCKET_NOT_FOUND: AdminError = AdminError { code: "XRustFSAdminTierBucketNotFound".to_string(), message: "Remote tier bucket not found".to_string(), status_code: StatusCode::BAD_REQUEST, }; pub static ref ERR_TIER_INVALID_CREDENTIALS: AdminError = AdminError { code: "XRustFSAdminTierInvalidCredentials".to_string(), message: "Invalid remote tier credentials".to_string(), status_code: StatusCode::BAD_REQUEST, }; pub static ref ERR_TIER_RESERVED_NAME: AdminError = AdminError { code: "XRustFSAdminTierReserved".to_string(), message: "Cannot use reserved tier name".to_string(), status_code: StatusCode::BAD_REQUEST, }; pub static ref ERR_TIER_PERM_ERR: AdminError = AdminError { code: "TierPermErr".to_string(), message: "Tier Perm Err".to_string(), status_code: StatusCode::OK, }; pub static ref ERR_TIER_CONNECT_ERR: AdminError = AdminError { code: "TierConnectErr".to_string(), message: "Tier Connect Err".to_string(), status_code: StatusCode::OK, }; }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/mod.rs
crates/ecstore/src/tier/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub mod tier; pub mod tier_admin; pub mod tier_config; pub mod tier_gen; pub mod tier_handlers; pub mod warm_backend; pub mod warm_backend_aliyun; pub mod warm_backend_azure; pub mod warm_backend_gcs; pub mod warm_backend_huaweicloud; pub mod warm_backend_minio; pub mod warm_backend_r2; pub mod warm_backend_rustfs; pub mod warm_backend_s3; pub mod warm_backend_s3sdk; pub mod warm_backend_tencent;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_s3sdk.rs
crates/ecstore/src/tier/warm_backend_s3sdk.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use url::Url; use aws_config::meta::region::RegionProviderChain; use aws_sdk_s3::Client; use aws_sdk_s3::config::{Credentials, Region}; use aws_sdk_s3::primitives::ByteStream; use crate::client::{ api_get_options::GetObjectOptions, api_put_object::PutObjectOptions, api_remove::RemoveObjectOptions, transition_api::{ReadCloser, ReaderImpl}, }; use crate::error::ErrorResponse; use crate::error::error_resp_to_object_err; use crate::tier::{ tier_config::TierS3, warm_backend::{WarmBackend, WarmBackendGetOpts}, }; use rustfs_utils::path::SLASH_SEPARATOR; pub struct WarmBackendS3 { pub client: Arc<Client>, pub bucket: String, pub prefix: String, pub storage_class: String, } impl WarmBackendS3 { pub async fn new(conf: &TierS3, tier: &str) -> Result<Self, std::io::Error> { let u = match Url::parse(&conf.endpoint) { Ok(u) => u, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; if conf.aws_role_web_identity_token_file == "" && conf.aws_role_arn != "" || conf.aws_role_web_identity_token_file != "" && conf.aws_role_arn == "" { return Err(std::io::Error::other("both the token file and the role ARN are required")); } else if conf.access_key == "" && conf.secret_key != "" || conf.access_key != "" && conf.secret_key == "" { return Err(std::io::Error::other("both the access and secret keys are required")); } else if conf.aws_role && (conf.aws_role_web_identity_token_file != "" || conf.aws_role_arn != "" || conf.access_key != "" || conf.secret_key != "") { return Err(std::io::Error::other( "AWS Role cannot be activated with static credentials or the web identity token file", )); } else if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let creds; if conf.access_key != "" && conf.secret_key != "" { creds = Credentials::new( conf.access_key.clone(), // access_key_id conf.secret_key.clone(), // secret_access_key None, // session_token (optional) None, "Static", ); } else { return Err(std::io::Error::other("insufficient parameters for S3 backend authentication")); } let region_provider = RegionProviderChain::default_provider().or_else(Region::new(conf.region.clone())); #[allow(deprecated)] let config = aws_config::from_env() .endpoint_url(conf.endpoint.clone()) .region(region_provider) .credentials_provider(creds) .load() .await; let client = Client::new(&config); let client = Arc::new(client); Ok(Self { client, bucket: conf.bucket.clone(), prefix: conf.prefix.clone().trim_matches('/').to_string(), storage_class: conf.storage_class.clone(), }) } pub fn get_dest(&self, object: &str) -> String { let mut dest_obj = object.to_string(); if self.prefix != "" { dest_obj = format!("{}/{}", &self.prefix, object); } return dest_obj; } } #[async_trait::async_trait] impl WarmBackend for WarmBackendS3 { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let client = self.client.clone(); let Ok(res) = client .put_object() .bucket(&self.bucket) .key(&self.get_dest(object)) .body(match r { ReaderImpl::Body(content_body) => ByteStream::from(content_body.to_vec()), ReaderImpl::ObjectBody(mut content_body) => ByteStream::from(content_body.read_all().await?), }) .send() .await else { return Err(std::io::Error::other("put_object error")); }; Ok(res.version_id().unwrap_or("").to_string()) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { let client = self.client.clone(); let Ok(res) = client .get_object() .bucket(&self.bucket) .key(&self.get_dest(object)) .send() .await else { return Err(std::io::Error::other("get_object error")); }; Ok(ReadCloser::new(std::io::Cursor::new( res.body.collect().await.map(|data| data.into_bytes().to_vec())?, ))) } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { let client = self.client.clone(); if let Err(_) = client .delete_object() .bucket(&self.bucket) .key(&self.get_dest(object)) .send() .await { return Err(std::io::Error::other("delete_object error")); } Ok(()) } async fn in_use(&self) -> Result<bool, std::io::Error> { let client = self.client.clone(); let Ok(res) = client .list_objects_v2() .bucket(&self.bucket) //.max_keys(10) //.into_paginator() .send() .await else { return Err(std::io::Error::other("list_objects_v2 error")); }; Ok(res.common_prefixes.unwrap().len() > 0 || res.contents.unwrap().len() > 0) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend.rs
crates/ecstore/src/tier/warm_backend.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::client::{ admin_handler_utils::AdminError, transition_api::{ReadCloser, ReaderImpl}, }; use crate::error::is_err_bucket_not_found; use crate::tier::{ tier::ERR_TIER_TYPE_UNSUPPORTED, tier_config::{TierConfig, TierType}, tier_handlers::{ERR_TIER_BUCKET_NOT_FOUND, ERR_TIER_PERM_ERR}, warm_backend_aliyun::WarmBackendAliyun, warm_backend_azure::WarmBackendAzure, warm_backend_gcs::WarmBackendGCS, warm_backend_huaweicloud::WarmBackendHuaweicloud, warm_backend_minio::WarmBackendMinIO, warm_backend_r2::WarmBackendR2, warm_backend_rustfs::WarmBackendRustFS, warm_backend_s3::WarmBackendS3, warm_backend_tencent::WarmBackendTencent, }; use bytes::Bytes; use http::StatusCode; use std::collections::HashMap; use tracing::{info, warn}; pub type WarmBackendImpl = Box<dyn WarmBackend + Send + Sync + 'static>; const PROBE_OBJECT: &str = "probeobject"; #[derive(Default)] pub struct WarmBackendGetOpts { pub start_offset: i64, pub length: i64, } #[async_trait::async_trait] pub trait WarmBackend { async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error>; async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error>; async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error>; async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error>; async fn in_use(&self) -> Result<bool, std::io::Error>; } pub async fn check_warm_backend(w: Option<&WarmBackendImpl>) -> Result<(), AdminError> { let w = w.expect("err"); let remote_version_id = w .put(PROBE_OBJECT, ReaderImpl::Body(Bytes::from("RustFS".as_bytes().to_vec())), 5) .await; if let Err(err) = remote_version_id { return Err(ERR_TIER_PERM_ERR.clone()); } let r = w.get(PROBE_OBJECT, "", WarmBackendGetOpts::default()).await; //xhttp.DrainBody(r); if let Err(err) = r { //if is_err_bucket_not_found(&err) { // return Err(ERR_TIER_BUCKET_NOT_FOUND); //} /*else if is_err_signature_does_not_match(err) { return Err(ERR_TIER_MISSING_CREDENTIALS); }*/ //else { return Err(ERR_TIER_PERM_ERR.clone()); //} } if let Err(err) = w.remove(PROBE_OBJECT, &remote_version_id.expect("err")).await { return Err(ERR_TIER_PERM_ERR.clone()); }; Ok(()) } pub async fn new_warm_backend(tier: &TierConfig, probe: bool) -> Result<WarmBackendImpl, AdminError> { let mut d: Option<WarmBackendImpl> = None; match tier.tier_type { TierType::S3 => { let dd = WarmBackendS3::new(tier.s3.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::RustFS => { let dd = WarmBackendRustFS::new(tier.rustfs.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::MinIO => { let dd = WarmBackendMinIO::new(tier.minio.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::Aliyun => { let dd = WarmBackendAliyun::new(tier.aliyun.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::Tencent => { let dd = WarmBackendTencent::new(tier.tencent.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::Huaweicloud => { let dd = WarmBackendHuaweicloud::new(tier.huaweicloud.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::Azure => { let dd = WarmBackendAzure::new(tier.azure.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::GCS => { let dd = WarmBackendGCS::new(tier.gcs.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } TierType::R2 => { let dd = WarmBackendR2::new(tier.r2.as_ref().expect("err"), &tier.name).await; if let Err(err) = dd { warn!("{}", err); return Err(AdminError { code: "XRustFSAdminTierInvalidConfig".to_string(), message: format!("Unable to setup remote tier, check tier configuration: {}", err.to_string()), status_code: StatusCode::BAD_REQUEST, }); } d = Some(Box::new(dd.expect("err"))); } _ => { return Err(ERR_TIER_TYPE_UNSUPPORTED.clone()); } } Ok(d.expect("err")) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_minio.rs
crates/ecstore/src/tier/warm_backend_minio.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierMinIO, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendMinIO(WarmBackendS3); impl WarmBackendMinIO { pub async fn new(conf: &TierMinIO, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => { return Err(std::io::Error::other(e.to_string())); } }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "minio", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendMinIO { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_huaweicloud.rs
crates/ecstore/src/tier/warm_backend_huaweicloud.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{BucketLookupType, Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierHuaweicloud, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendHuaweicloud(WarmBackendS3); impl WarmBackendHuaweicloud { pub async fn new(conf: &TierHuaweicloud, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => { return Err(std::io::Error::other(e.to_string())); } }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), bucket_lookup: BucketLookupType::BucketLookupDNS, ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "huaweicloud", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendHuaweicloud { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/tier_gen.rs
crates/ecstore/src/tier/tier_gen.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::tier::tier::TierConfigMgr; #[allow(dead_code)] impl TierConfigMgr { pub fn msg_size(&self) -> usize { 100 } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_gcs.rs
crates/ecstore/src/tier/warm_backend_gcs.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use bytes::Bytes; use google_cloud_auth::credentials::Credentials; use google_cloud_auth::credentials::user_account::Builder; use google_cloud_storage as gcs; use google_cloud_storage::client::Storage; use std::convert::TryFrom; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, transition_api::{Options, ReadCloser, ReaderImpl}, }; use crate::tier::{ tier_config::TierGCS, warm_backend::{WarmBackend, WarmBackendGetOpts}, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendGCS { pub client: Arc<Storage>, pub bucket: String, pub prefix: String, pub storage_class: String, } impl WarmBackendGCS { pub async fn new(conf: &TierGCS, tier: &str) -> Result<Self, std::io::Error> { if conf.creds == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let authorized_user = serde_json::from_str(&conf.creds)?; let credentials = Builder::new(authorized_user) //.with_retry_policy(AlwaysRetry.with_attempt_limit(3)) //.with_backoff_policy(backoff) .build() .map_err(|e| std::io::Error::other(format!("Invalid credentials JSON: {}", e)))?; let Ok(client) = Storage::builder() .with_endpoint(conf.endpoint.clone()) .with_credentials(credentials) .build() .await else { return Err(std::io::Error::other("Storage::builder error")); }; let client = Arc::new(client); Ok(Self { client, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), }) } pub fn get_dest(&self, object: &str) -> String { let mut dest_obj = object.to_string(); if self.prefix != "" { dest_obj = format!("{}/{}", &self.prefix, object); } return dest_obj; } } #[async_trait::async_trait] impl WarmBackend for WarmBackendGCS { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let d = match r { ReaderImpl::Body(content_body) => content_body.to_vec(), ReaderImpl::ObjectBody(mut content_body) => content_body.read_all().await?, }; let Ok(res) = self .client .write_object(&self.bucket, &self.get_dest(object), Bytes::from(d)) .send_buffered() .await else { return Err(std::io::Error::other("write_object error")); }; //self.ToObjectError(err, object) Ok(res.generation.to_string()) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { let Ok(mut reader) = self.client.read_object(&self.bucket, &self.get_dest(object)).send().await else { return Err(std::io::Error::other("read_object error")); }; let mut contents = Vec::new(); while let Ok(Some(chunk)) = reader.next().await.transpose() { contents.extend_from_slice(&chunk); } Ok(ReadCloser::new(std::io::Cursor::new(contents))) } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { /*self.client .delete_object() .set_bucket(&self.bucket) .set_object(&self.get_dest(object)) //.set_generation(object.generation) .send() .await?;*/ Ok(()) } async fn in_use(&self) -> Result<bool, std::io::Error> { /*let result = self.client .list_objects_v2(&self.bucket, &self.prefix, "", "", SLASH_SEPARATOR, 1) .await?; Ok(result.common_prefixes.len() > 0 || result.contents.len() > 0)*/ Ok(false) } } /*fn gcs_to_object_error(err: Error, params: Vec<String>) -> Option<Error> { if err == nil { return nil } bucket := "" object := "" uploadID := "" if len(params) >= 1 { bucket = params[0] } if len(params) == 2 { object = params[1] } if len(params) == 3 { uploadID = params[2] } // in some cases just a plain error is being returned switch err.Error() { case "storage: bucket doesn't exist": err = BucketNotFound{ Bucket: bucket, } return err case "storage: object doesn't exist": if uploadID != "" { err = InvalidUploadID{ UploadID: uploadID, } } else { err = ObjectNotFound{ Bucket: bucket, Object: object, } } return err } googleAPIErr, ok := err.(*googleapi.Error) if !ok { // We don't interpret non MinIO errors. As minio errors will // have StatusCode to help to convert to object errors. return err } if len(googleAPIErr.Errors) == 0 { return err } reason := googleAPIErr.Errors[0].Reason message := googleAPIErr.Errors[0].Message switch reason { case "required": // Anonymous users does not have storage.xyz access to project 123. fallthrough case "keyInvalid": fallthrough case "forbidden": err = PrefixAccessDenied{ Bucket: bucket, Object: object, } case "invalid": err = BucketNameInvalid{ Bucket: bucket, } case "notFound": if object != "" { err = ObjectNotFound{ Bucket: bucket, Object: object, } break } err = BucketNotFound{Bucket: bucket} case "conflict": if message == "You already own this bucket. Please select another name." { err = BucketAlreadyOwnedByYou{Bucket: bucket} break } if message == "Sorry, that name is not available. Please try a different one." { err = BucketAlreadyExists{Bucket: bucket} break } err = BucketNotEmpty{Bucket: bucket} } return err }*/
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_r2.rs
crates/ecstore/src/tier/warm_backend_r2.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierR2, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendR2(WarmBackendS3); impl WarmBackendR2 { pub async fn new(conf: &TierR2, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => { return Err(std::io::Error::other(e.to_string())); } }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "r2", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendR2 { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/tier/warm_backend_azure.rs
crates/ecstore/src/tier/warm_backend_azure.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::collections::HashMap; use std::sync::Arc; use crate::client::{ admin_handler_utils::AdminError, api_put_object::PutObjectOptions, credentials::{Credentials, SignatureType, Static, Value}, transition_api::{BucketLookupType, Options, ReadCloser, ReaderImpl, TransitionClient, TransitionCore}, }; use crate::tier::{ tier_config::TierAzure, warm_backend::{WarmBackend, WarmBackendGetOpts}, warm_backend_s3::WarmBackendS3, }; use tracing::warn; const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; const MAX_PARTS_COUNT: i64 = 10000; const _MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; const MIN_PART_SIZE: i64 = 1024 * 1024 * 128; pub struct WarmBackendAzure(WarmBackendS3); impl WarmBackendAzure { pub async fn new(conf: &TierAzure, tier: &str) -> Result<Self, std::io::Error> { if conf.access_key == "" || conf.secret_key == "" { return Err(std::io::Error::other("both access and secret keys are required")); } if conf.bucket == "" { return Err(std::io::Error::other("no bucket name was provided")); } let u = match url::Url::parse(&conf.endpoint) { Ok(u) => u, Err(e) => { return Err(std::io::Error::other(e.to_string())); } }; let creds = Credentials::new(Static(Value { access_key_id: conf.access_key.clone(), secret_access_key: conf.secret_key.clone(), session_token: "".to_string(), signer_type: SignatureType::SignatureV4, ..Default::default() })); let opts = Options { creds, secure: u.scheme() == "https", //transport: GLOBAL_RemoteTargetTransport, trailing_headers: true, region: conf.region.clone(), bucket_lookup: BucketLookupType::BucketLookupDNS, ..Default::default() }; let scheme = u.scheme(); let default_port = if scheme == "https" { 443 } else { 80 }; let client = TransitionClient::new( &format!("{}:{}", u.host_str().expect("err"), u.port().unwrap_or(default_port)), opts, "azure", ) .await?; let client = Arc::new(client); let core = TransitionCore(Arc::clone(&client)); Ok(Self(WarmBackendS3 { client, core, bucket: conf.bucket.clone(), prefix: conf.prefix.strip_suffix("/").unwrap_or(&conf.prefix).to_owned(), storage_class: "".to_string(), })) } } #[async_trait::async_trait] impl WarmBackend for WarmBackendAzure { async fn put_with_meta( &self, object: &str, r: ReaderImpl, length: i64, meta: HashMap<String, String>, ) -> Result<String, std::io::Error> { let part_size = optimal_part_size(length)?; let client = self.0.client.clone(); let res = client .put_object( &self.0.bucket, &self.0.get_dest(object), r, length, &PutObjectOptions { storage_class: self.0.storage_class.clone(), part_size: part_size as u64, disable_content_sha256: true, user_metadata: meta, ..Default::default() }, ) .await?; //self.ToObjectError(err, object) Ok(res.version_id) } async fn put(&self, object: &str, r: ReaderImpl, length: i64) -> Result<String, std::io::Error> { self.put_with_meta(object, r, length, HashMap::new()).await } async fn get(&self, object: &str, rv: &str, opts: WarmBackendGetOpts) -> Result<ReadCloser, std::io::Error> { self.0.get(object, rv, opts).await } async fn remove(&self, object: &str, rv: &str) -> Result<(), std::io::Error> { self.0.remove(object, rv).await } async fn in_use(&self) -> Result<bool, std::io::Error> { self.0.in_use().await } } fn optimal_part_size(object_size: i64) -> Result<i64, std::io::Error> { let mut object_size = object_size; if object_size == -1 { object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other("entity too large")); } let configured_part_size = MIN_PART_SIZE; let mut part_size_flt = object_size as f64 / MAX_PARTS_COUNT as f64; part_size_flt = (part_size_flt as f64 / configured_part_size as f64).ceil() * configured_part_size as f64; let part_size = part_size_flt as i64; if part_size == 0 { return Ok(MIN_PART_SIZE); } Ok(part_size) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/config/notify.rs
crates/ecstore/src/config/notify.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::config::{KV, KVS}; use rustfs_config::{ COMMENT_KEY, DEFAULT_LIMIT, ENABLE_KEY, EVENT_DEFAULT_DIR, EnableState, MQTT_BROKER, MQTT_KEEP_ALIVE_INTERVAL, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_RECONNECT_INTERVAL, MQTT_TOPIC, MQTT_USERNAME, WEBHOOK_AUTH_TOKEN, WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_KEY, WEBHOOK_ENDPOINT, WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT, }; use std::sync::LazyLock; /// The default configuration collection of webhooks, /// Initialized only once during the program life cycle, enabling high-performance lazy loading. pub static DEFAULT_NOTIFY_WEBHOOK_KVS: LazyLock<KVS> = LazyLock::new(|| { KVS(vec![ KV { key: ENABLE_KEY.to_owned(), value: EnableState::Off.to_string(), hidden_if_empty: false, }, KV { key: WEBHOOK_ENDPOINT.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, // Sensitive information such as authentication tokens is hidden when the value is empty, enhancing security KV { key: WEBHOOK_AUTH_TOKEN.to_owned(), value: "".to_owned(), hidden_if_empty: true, }, KV { key: WEBHOOK_QUEUE_LIMIT.to_owned(), value: DEFAULT_LIMIT.to_string(), hidden_if_empty: false, }, KV { key: WEBHOOK_QUEUE_DIR.to_owned(), value: EVENT_DEFAULT_DIR.to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_CLIENT_CERT.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_CLIENT_KEY.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: COMMENT_KEY.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, ]) }); /// MQTT's default configuration collection pub static DEFAULT_NOTIFY_MQTT_KVS: LazyLock<KVS> = LazyLock::new(|| { KVS(vec![ KV { key: ENABLE_KEY.to_owned(), value: EnableState::Off.to_string(), hidden_if_empty: false, }, KV { key: MQTT_BROKER.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_TOPIC.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, // Sensitive information such as passwords are hidden when the value is empty KV { key: MQTT_PASSWORD.to_owned(), value: "".to_owned(), hidden_if_empty: true, }, KV { key: MQTT_USERNAME.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_QOS.to_owned(), value: "0".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_KEEP_ALIVE_INTERVAL.to_owned(), value: "0s".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_RECONNECT_INTERVAL.to_owned(), value: "0s".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_QUEUE_DIR.to_owned(), value: EVENT_DEFAULT_DIR.to_owned(), hidden_if_empty: false, }, KV { key: MQTT_QUEUE_LIMIT.to_owned(), value: DEFAULT_LIMIT.to_string(), hidden_if_empty: false, }, KV { key: COMMENT_KEY.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, ]) });
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/config/audit.rs
crates/ecstore/src/config/audit.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::config::{KV, KVS}; use rustfs_config::{ COMMENT_KEY, DEFAULT_LIMIT, ENABLE_KEY, EVENT_DEFAULT_DIR, EnableState, MQTT_BROKER, MQTT_KEEP_ALIVE_INTERVAL, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_RECONNECT_INTERVAL, MQTT_TOPIC, MQTT_USERNAME, WEBHOOK_AUTH_TOKEN, WEBHOOK_BATCH_SIZE, WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_KEY, WEBHOOK_ENDPOINT, WEBHOOK_HTTP_TIMEOUT, WEBHOOK_MAX_RETRY, WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT, WEBHOOK_RETRY_INTERVAL, }; use std::sync::LazyLock; #[allow(dead_code)] #[allow(clippy::declare_interior_mutable_const)] /// Default KVS for audit webhook settings. pub static DEFAULT_AUDIT_WEBHOOK_KVS: LazyLock<KVS> = LazyLock::new(|| { KVS(vec![ KV { key: ENABLE_KEY.to_owned(), value: EnableState::Off.to_string(), hidden_if_empty: false, }, KV { key: WEBHOOK_ENDPOINT.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_AUTH_TOKEN.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_CLIENT_CERT.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_CLIENT_KEY.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_BATCH_SIZE.to_owned(), value: "1".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_QUEUE_LIMIT.to_owned(), value: DEFAULT_LIMIT.to_string(), hidden_if_empty: false, }, KV { key: WEBHOOK_QUEUE_DIR.to_owned(), value: EVENT_DEFAULT_DIR.to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_MAX_RETRY.to_owned(), value: "0".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_RETRY_INTERVAL.to_owned(), value: "3s".to_owned(), hidden_if_empty: false, }, KV { key: WEBHOOK_HTTP_TIMEOUT.to_owned(), value: "5s".to_owned(), hidden_if_empty: false, }, ]) }); #[allow(dead_code)] #[allow(clippy::declare_interior_mutable_const)] /// Default KVS for audit MQTT settings. pub static DEFAULT_AUDIT_MQTT_KVS: LazyLock<KVS> = LazyLock::new(|| { KVS(vec![ KV { key: ENABLE_KEY.to_owned(), value: EnableState::Off.to_string(), hidden_if_empty: false, }, KV { key: MQTT_BROKER.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_TOPIC.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_USERNAME.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_PASSWORD.to_owned(), value: "".to_owned(), hidden_if_empty: true, // Sensitive field }, KV { key: MQTT_QOS.to_owned(), value: "1".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_KEEP_ALIVE_INTERVAL.to_owned(), value: "60s".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_RECONNECT_INTERVAL.to_owned(), value: "5s".to_owned(), hidden_if_empty: false, }, KV { key: MQTT_QUEUE_DIR.to_owned(), value: EVENT_DEFAULT_DIR.to_owned(), hidden_if_empty: false, }, KV { key: MQTT_QUEUE_LIMIT.to_owned(), value: DEFAULT_LIMIT.to_string(), hidden_if_empty: false, }, KV { key: COMMENT_KEY.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, ]) });
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/config/com.rs
crates/ecstore/src/config/com.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::config::{Config, GLOBAL_STORAGE_CLASS, storageclass}; use crate::disk::RUSTFS_META_BUCKET; use crate::error::{Error, Result}; use crate::store_api::{ObjectInfo, ObjectOptions, PutObjReader, StorageAPI}; use http::HeaderMap; use rustfs_config::DEFAULT_DELIMITER; use rustfs_utils::path::SLASH_SEPARATOR; use std::collections::HashSet; use std::sync::Arc; use std::sync::LazyLock; use tracing::{error, warn}; pub const CONFIG_PREFIX: &str = "config"; const CONFIG_FILE: &str = "config.json"; pub const STORAGE_CLASS_SUB_SYS: &str = "storage_class"; static CONFIG_BUCKET: LazyLock<String> = LazyLock::new(|| format!("{RUSTFS_META_BUCKET}{SLASH_SEPARATOR}{CONFIG_PREFIX}")); static SUB_SYSTEMS_DYNAMIC: LazyLock<HashSet<String>> = LazyLock::new(|| { let mut h = HashSet::new(); h.insert(STORAGE_CLASS_SUB_SYS.to_owned()); h }); pub async fn read_config<S: StorageAPI>(api: Arc<S>, file: &str) -> Result<Vec<u8>> { let (data, _obj) = read_config_with_metadata(api, file, &ObjectOptions::default()).await?; Ok(data) } pub async fn read_config_with_metadata<S: StorageAPI>( api: Arc<S>, file: &str, opts: &ObjectOptions, ) -> Result<(Vec<u8>, ObjectInfo)> { let h = HeaderMap::new(); let mut rd = api .get_object_reader(RUSTFS_META_BUCKET, file, None, h, opts) .await .map_err(|err| { if err == Error::FileNotFound || matches!(err, Error::ObjectNotFound(_, _)) { Error::ConfigNotFound } else { warn!("read_config_with_metadata: err: {:?}, file: {}", err, file); err } })?; let data = rd.read_all().await?; if data.is_empty() { return Err(Error::ConfigNotFound); } Ok((data, rd.object_info)) } pub async fn save_config<S: StorageAPI>(api: Arc<S>, file: &str, data: Vec<u8>) -> Result<()> { save_config_with_opts( api, file, data, &ObjectOptions { max_parity: true, ..Default::default() }, ) .await } pub async fn delete_config<S: StorageAPI>(api: Arc<S>, file: &str) -> Result<()> { match api .delete_object( RUSTFS_META_BUCKET, file, ObjectOptions { delete_prefix: true, delete_prefix_object: true, ..Default::default() }, ) .await { Ok(_) => Ok(()), Err(err) => { if err == Error::FileNotFound || matches!(err, Error::ObjectNotFound(_, _)) { Err(Error::ConfigNotFound) } else { Err(err) } } } } pub async fn save_config_with_opts<S: StorageAPI>(api: Arc<S>, file: &str, data: Vec<u8>, opts: &ObjectOptions) -> Result<()> { if let Err(err) = api .put_object(RUSTFS_META_BUCKET, file, &mut PutObjReader::from_vec(data), opts) .await { error!("save_config_with_opts: err: {:?}, file: {}", err, file); return Err(err); } Ok(()) } fn new_server_config() -> Config { Config::new() } async fn new_and_save_server_config<S: StorageAPI>(api: Arc<S>) -> Result<Config> { let mut cfg = new_server_config(); lookup_configs(&mut cfg, api.clone()).await; save_server_config(api, &cfg).await?; Ok(cfg) } fn get_config_file() -> String { format!("{CONFIG_PREFIX}{SLASH_SEPARATOR}{CONFIG_FILE}") } /// Handle the situation where the configuration file does not exist, create and save a new configuration async fn handle_missing_config<S: StorageAPI>(api: Arc<S>, context: &str) -> Result<Config> { warn!("Configuration not found ({}): Start initializing new configuration", context); let cfg = new_and_save_server_config(api).await?; warn!("Configuration initialization complete ({})", context); Ok(cfg) } /// Handle configuration file read errors fn handle_config_read_error(err: Error, file_path: &str) -> Result<Config> { error!("Read configuration failed (path: '{}'): {:?}", file_path, err); Err(err) } pub async fn read_config_without_migrate<S: StorageAPI>(api: Arc<S>) -> Result<Config> { let config_file = get_config_file(); // Try to read the configuration file match read_config(api.clone(), &config_file).await { Ok(data) => read_server_config(api, &data).await, Err(Error::ConfigNotFound) => handle_missing_config(api, "Read the main configuration").await, Err(err) => handle_config_read_error(err, &config_file), } } async fn read_server_config<S: StorageAPI>(api: Arc<S>, data: &[u8]) -> Result<Config> { // If the provided data is empty, try to read from the file again if data.is_empty() { let config_file = get_config_file(); warn!("Received empty configuration data, try to reread from '{}'", config_file); // Try to read the configuration again match read_config(api.clone(), &config_file).await { Ok(cfg_data) => { // TODO: decrypt let cfg = Config::unmarshal(&cfg_data)?; return Ok(cfg.merge()); } Err(Error::ConfigNotFound) => return handle_missing_config(api, "Read alternate configuration").await, Err(err) => return handle_config_read_error(err, &config_file), } } // Process non-empty configuration data let cfg = Config::unmarshal(data)?; Ok(cfg.merge()) } pub async fn save_server_config<S: StorageAPI>(api: Arc<S>, cfg: &Config) -> Result<()> { let data = cfg.marshal()?; let config_file = get_config_file(); save_config(api, &config_file, data).await } pub async fn lookup_configs<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) { // TODO: from etcd if let Err(err) = apply_dynamic_config(cfg, api).await { error!("apply_dynamic_config err {:?}", &err); } } async fn apply_dynamic_config<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) -> Result<()> { for key in SUB_SYSTEMS_DYNAMIC.iter() { apply_dynamic_config_for_sub_sys(cfg, api.clone(), key).await?; } Ok(()) } async fn apply_dynamic_config_for_sub_sys<S: StorageAPI>(cfg: &mut Config, api: Arc<S>, subsys: &str) -> Result<()> { let set_drive_counts = api.set_drive_counts(); if subsys == STORAGE_CLASS_SUB_SYS { let kvs = cfg.get_value(STORAGE_CLASS_SUB_SYS, DEFAULT_DELIMITER).unwrap_or_default(); for (i, count) in set_drive_counts.iter().enumerate() { match storageclass::lookup_config(&kvs, *count) { Ok(res) => { if i == 0 && GLOBAL_STORAGE_CLASS.get().is_none() && let Err(r) = GLOBAL_STORAGE_CLASS.set(res) { error!("GLOBAL_STORAGE_CLASS.set failed {:?}", r); } } Err(err) => { error!("init storage class err:{:?}", &err); break; } } } } Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/config/heal.rs
crates/ecstore/src/config/heal.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::error::{Error, Result}; use rustfs_utils::string::parse_bool; use std::time::Duration; #[derive(Debug, Default)] pub struct Config { pub bitrot: String, pub sleep: Duration, pub io_count: usize, pub drive_workers: usize, pub cache: Duration, } impl Config { pub fn bitrot_scan_cycle(&self) -> Duration { self.cache } pub fn get_workers(&self) -> usize { self.drive_workers } pub fn update(&mut self, nopts: &Config) { self.bitrot = nopts.bitrot.clone(); self.io_count = nopts.io_count; self.sleep = nopts.sleep; self.drive_workers = nopts.drive_workers; } } const RUSTFS_BITROT_CYCLE_IN_MONTHS: u64 = 1; fn parse_bitrot_config(s: &str) -> Result<Duration> { match parse_bool(s) { Ok(enabled) => { if enabled { Ok(Duration::from_secs_f64(0.0)) } else { Ok(Duration::from_secs_f64(-1.0)) } } Err(_) => { if !s.ends_with("m") { return Err(Error::other("unknown format")); } match s.trim_end_matches('m').parse::<u64>() { Ok(months) => { if months < RUSTFS_BITROT_CYCLE_IN_MONTHS { return Err(Error::other(format!("minimum bitrot cycle is {RUSTFS_BITROT_CYCLE_IN_MONTHS} month(s)"))); } Ok(Duration::from_secs(months * 30 * 24 * 60)) } Err(err) => Err(Error::other(err)), } } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/config/mod.rs
crates/ecstore/src/config/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. mod audit; pub mod com; #[allow(dead_code)] pub mod heal; mod notify; pub mod storageclass; use crate::error::Result; use crate::store::ECStore; use com::{STORAGE_CLASS_SUB_SYS, lookup_configs, read_config_without_migrate}; use rustfs_config::COMMENT_KEY; use rustfs_config::DEFAULT_DELIMITER; use rustfs_config::audit::{AUDIT_MQTT_SUB_SYS, AUDIT_WEBHOOK_SUB_SYS}; use rustfs_config::notify::{NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::LazyLock; use std::sync::{Arc, OnceLock}; pub static GLOBAL_STORAGE_CLASS: LazyLock<OnceLock<storageclass::Config>> = LazyLock::new(OnceLock::new); pub static DEFAULT_KVS: LazyLock<OnceLock<HashMap<String, KVS>>> = LazyLock::new(OnceLock::new); pub static GLOBAL_SERVER_CONFIG: LazyLock<OnceLock<Config>> = LazyLock::new(OnceLock::new); pub static GLOBAL_CONFIG_SYS: LazyLock<ConfigSys> = LazyLock::new(ConfigSys::new); pub const ENV_ACCESS_KEY: &str = "RUSTFS_ACCESS_KEY"; pub const ENV_SECRET_KEY: &str = "RUSTFS_SECRET_KEY"; pub const ENV_ROOT_USER: &str = "RUSTFS_ROOT_USER"; pub const ENV_ROOT_PASSWORD: &str = "RUSTFS_ROOT_PASSWORD"; pub static RUSTFS_CONFIG_PREFIX: &str = "config"; pub struct ConfigSys {} impl Default for ConfigSys { fn default() -> Self { Self::new() } } impl ConfigSys { pub fn new() -> Self { Self {} } pub async fn init(&self, api: Arc<ECStore>) -> Result<()> { let mut cfg = read_config_without_migrate(api.clone().clone()).await?; lookup_configs(&mut cfg, api).await; let _ = GLOBAL_SERVER_CONFIG.set(cfg); Ok(()) } } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct KV { pub key: String, pub value: String, pub hidden_if_empty: bool, } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct KVS(pub Vec<KV>); impl Default for KVS { fn default() -> Self { Self::new() } } impl KVS { pub fn new() -> Self { KVS(Vec::new()) } pub fn get(&self, key: &str) -> String { if let Some(v) = self.lookup(key) { v } else { "".to_owned() } } pub fn lookup(&self, key: &str) -> Option<String> { for kv in self.0.iter() { if kv.key.as_str() == key { return Some(kv.value.clone()); } } None } ///Check if KVS is empty. pub fn is_empty(&self) -> bool { self.0.is_empty() } /// Returns a list of all keys for the current KVS. /// If the "comment" key does not exist, it will be added. pub fn keys(&self) -> Vec<String> { let mut found_comment = false; let mut keys: Vec<String> = self .0 .iter() .map(|kv| { if kv.key == COMMENT_KEY { found_comment = true; } kv.key.clone() }) .collect(); if !found_comment { keys.push(COMMENT_KEY.to_owned()); } keys } /// Insert or update a pair of key/values in KVS pub fn insert(&mut self, key: String, value: String) { for kv in self.0.iter_mut() { if kv.key == key { kv.value = value.clone(); return; } } self.0.push(KV { key, value, hidden_if_empty: false, }); } /// Merge all entries from another KVS to the current instance pub fn extend(&mut self, other: KVS) { for KV { key, value, .. } in other.0.into_iter() { self.insert(key, value); } } } #[derive(Debug, Clone)] pub struct Config(pub HashMap<String, HashMap<String, KVS>>); impl Default for Config { fn default() -> Self { Self::new() } } impl Config { pub fn new() -> Self { let mut cfg = Config(HashMap::new()); cfg.set_defaults(); cfg } pub fn get_value(&self, sub_sys: &str, key: &str) -> Option<KVS> { if let Some(m) = self.0.get(sub_sys) { m.get(key).cloned() } else { None } } pub fn set_defaults(&mut self) { if let Some(defaults) = DEFAULT_KVS.get() { for (k, v) in defaults.iter() { if !self.0.contains_key(k) { let mut default = HashMap::new(); default.insert(DEFAULT_DELIMITER.to_owned(), v.clone()); self.0.insert(k.clone(), default); } else if !self.0[k].contains_key(DEFAULT_DELIMITER) && let Some(m) = self.0.get_mut(k) { m.insert(DEFAULT_DELIMITER.to_owned(), v.clone()); } } } } pub fn unmarshal(data: &[u8]) -> Result<Config> { let m: HashMap<String, HashMap<String, KVS>> = serde_json::from_slice(data)?; let mut cfg = Config(m); cfg.set_defaults(); Ok(cfg) } pub fn marshal(&self) -> Result<Vec<u8>> { let data = serde_json::to_vec(&self.0)?; Ok(data) } pub fn merge(&self) -> Config { // TODO: merge default self.clone() } } pub fn register_default_kvs(kvs: HashMap<String, KVS>) { let mut p = HashMap::new(); for (k, v) in kvs { p.insert(k, v); } let _ = DEFAULT_KVS.set(p); } pub fn init() { let mut kvs = HashMap::new(); // Load storageclass default configuration kvs.insert(STORAGE_CLASS_SUB_SYS.to_owned(), storageclass::DEFAULT_KVS.clone()); // New: Loading default configurations for notify_webhook and notify_mqtt // Referring subsystem names through constants to improve the readability and maintainability of the code kvs.insert(NOTIFY_WEBHOOK_SUB_SYS.to_owned(), notify::DEFAULT_NOTIFY_WEBHOOK_KVS.clone()); kvs.insert(AUDIT_WEBHOOK_SUB_SYS.to_owned(), audit::DEFAULT_AUDIT_WEBHOOK_KVS.clone()); kvs.insert(NOTIFY_MQTT_SUB_SYS.to_owned(), notify::DEFAULT_NOTIFY_MQTT_KVS.clone()); kvs.insert(AUDIT_MQTT_SUB_SYS.to_owned(), audit::DEFAULT_AUDIT_MQTT_KVS.clone()); // Register all default configurations register_default_kvs(kvs) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/config/storageclass.rs
crates/ecstore/src/config/storageclass.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use super::KVS; use crate::config::KV; use crate::error::{Error, Result}; use serde::{Deserialize, Serialize}; use std::env; use std::sync::LazyLock; use tracing::warn; /// Default parity count for a given drive count /// The default configuration allocates the number of check disks based on the total number of disks pub fn default_parity_count(drive: usize) -> usize { match drive { 1 => 0, 2 | 3 => 1, 4 | 5 => 2, 6 | 7 => 3, _ => 4, } } // Standard constants for all storage class pub const RRS: &str = "REDUCED_REDUNDANCY"; pub const STANDARD: &str = "STANDARD"; // AWS S3 Storage Classes pub const DEEP_ARCHIVE: &str = "DEEP_ARCHIVE"; pub const EXPRESS_ONEZONE: &str = "EXPRESS_ONEZONE"; pub const GLACIER: &str = "GLACIER"; pub const GLACIER_IR: &str = "GLACIER_IR"; pub const INTELLIGENT_TIERING: &str = "INTELLIGENT_TIERING"; pub const ONEZONE_IA: &str = "ONEZONE_IA"; pub const OUTPOSTS: &str = "OUTPOSTS"; pub const SNOW: &str = "SNOW"; pub const STANDARD_IA: &str = "STANDARD_IA"; // Standard constants for config info storage class pub const CLASS_STANDARD: &str = "standard"; pub const CLASS_RRS: &str = "rrs"; pub const OPTIMIZE: &str = "optimize"; pub const INLINE_BLOCK: &str = "inline_block"; // Reduced redundancy storage class environment variable pub const RRS_ENV: &str = "RUSTFS_STORAGE_CLASS_RRS"; // Standard storage class environment variable pub const STANDARD_ENV: &str = "RUSTFS_STORAGE_CLASS_STANDARD"; // Optimize storage class environment variable pub const OPTIMIZE_ENV: &str = "RUSTFS_STORAGE_CLASS_OPTIMIZE"; // Inline block indicates the size of the shard that is considered for inlining pub const INLINE_BLOCK_ENV: &str = "RUSTFS_STORAGE_CLASS_INLINE_BLOCK"; // Supported storage class scheme is EC pub const SCHEME_PREFIX: &str = "EC"; // Min parity drives pub const MIN_PARITY_DRIVES: usize = 0; // Default RRS parity is always minimum parity. pub const DEFAULT_RRS_PARITY: usize = 1; pub static DEFAULT_INLINE_BLOCK: usize = 128 * 1024; pub static DEFAULT_KVS: LazyLock<KVS> = LazyLock::new(|| { let kvs = vec![ KV { key: CLASS_STANDARD.to_owned(), value: "".to_owned(), hidden_if_empty: false, }, KV { key: CLASS_RRS.to_owned(), value: "EC:1".to_owned(), hidden_if_empty: false, }, KV { key: OPTIMIZE.to_owned(), value: "availability".to_owned(), hidden_if_empty: false, }, KV { key: INLINE_BLOCK.to_owned(), value: "".to_owned(), hidden_if_empty: true, }, ]; KVS(kvs) }); // StorageClass - holds storage class information #[derive(Serialize, Deserialize, Debug, Default)] pub struct StorageClass { parity: usize, } // Config storage class configuration #[derive(Serialize, Deserialize, Debug, Default)] pub struct Config { standard: StorageClass, rrs: StorageClass, optimize: Option<String>, inline_block: usize, initialized: bool, } impl Config { pub fn get_parity_for_sc(&self, sc: &str) -> Option<usize> { match sc.trim() { RRS => { if self.initialized { Some(self.rrs.parity) } else { None } } // All these storage classes use standard parity configuration STANDARD | DEEP_ARCHIVE | EXPRESS_ONEZONE | GLACIER | GLACIER_IR | INTELLIGENT_TIERING | ONEZONE_IA | OUTPOSTS | SNOW | STANDARD_IA => { if self.initialized { Some(self.standard.parity) } else { None } } _ => { if self.initialized { Some(self.standard.parity) } else { None } } } } pub fn should_inline(&self, shard_size: i64, versioned: bool) -> bool { if shard_size < 0 { return false; } let shard_size = shard_size as usize; let mut inline_block = DEFAULT_INLINE_BLOCK; if self.initialized { inline_block = self.inline_block; } if versioned { shard_size <= inline_block / 8 } else { shard_size <= inline_block } } pub fn inline_block(&self) -> usize { if !self.initialized { DEFAULT_INLINE_BLOCK } else { self.inline_block } } pub fn capacity_optimized(&self) -> bool { if !self.initialized { false } else { self.optimize.as_ref().is_some_and(|v| v.as_str() == "capacity") } } } pub fn lookup_config(kvs: &KVS, set_drive_count: usize) -> Result<Config> { let standard = { let ssc_str = { if let Ok(ssc_str) = env::var(STANDARD_ENV) { ssc_str } else { kvs.get(CLASS_STANDARD) } }; if !ssc_str.is_empty() { parse_storage_class(&ssc_str)? } else { StorageClass { parity: default_parity_count(set_drive_count), } } }; let rrs = { let ssc_str = { if let Ok(ssc_str) = env::var(RRS_ENV) { ssc_str } else { kvs.get(RRS) } }; if !ssc_str.is_empty() { parse_storage_class(&ssc_str)? } else { StorageClass { parity: { if set_drive_count == 1 { 0 } else { DEFAULT_RRS_PARITY } }, } } }; validate_parity_inner(standard.parity, rrs.parity, set_drive_count)?; let optimize = { env::var(OPTIMIZE_ENV).ok() }; let inline_block = { if let Ok(ev) = env::var(INLINE_BLOCK_ENV) { if let Ok(block) = ev.parse::<bytesize::ByteSize>() { if block.as_u64() as usize > DEFAULT_INLINE_BLOCK { warn!( "inline block value bigger than recommended max of 128KiB -> {}, performance may degrade for PUT please benchmark the changes", block ); } block.as_u64() as usize } else { return Err(Error::other(format!("parse {INLINE_BLOCK_ENV} format failed"))); } } else { DEFAULT_INLINE_BLOCK } }; Ok(Config { standard, rrs, optimize, inline_block, initialized: true, }) } pub fn parse_storage_class(env: &str) -> Result<StorageClass> { let s: Vec<&str> = env.split(':').collect(); // only two elements allowed in the string - "scheme" and "number of parity drives" if s.len() != 2 { return Err(Error::other(format!( "Invalid storage class format: {env}. Expected 'Scheme:Number of parity drives'." ))); } // only allowed scheme is "EC" if s[0] != SCHEME_PREFIX { return Err(Error::other(format!("Unsupported scheme {}. Supported scheme is EC.", s[0]))); } // Number of parity drives should be integer let parity_drives: usize = match s[1].parse() { Ok(num) => num, Err(_) => return Err(Error::other(format!("Failed to parse parity value: {}.", s[1]))), }; Ok(StorageClass { parity: parity_drives }) } // ValidateParity validates standard storage class parity. pub fn validate_parity(ss_parity: usize, set_drive_count: usize) -> Result<()> { // if ss_parity > 0 && ss_parity < MIN_PARITY_DRIVES { // return Err(Error::other(format!( // "parity {} should be greater than or equal to {}", // ss_parity, MIN_PARITY_DRIVES // ))); // } if ss_parity > set_drive_count / 2 { return Err(Error::other(format!( "parity {} should be less than or equal to {}", ss_parity, set_drive_count / 2 ))); } Ok(()) } // Validates the parity drives. pub fn validate_parity_inner(ss_parity: usize, rrs_parity: usize, set_drive_count: usize) -> Result<()> { // if ss_parity > 0 && ss_parity < MIN_PARITY_DRIVES { // return Err(Error::other(format!( // "Standard storage class parity {} should be greater than or equal to {}", // ss_parity, MIN_PARITY_DRIVES // ))); // } // RRS parity drives should be greater than or equal to minParityDrives. // Parity below minParityDrives is not supported. // if rrs_parity > 0 && rrs_parity < MIN_PARITY_DRIVES { // return Err(Error::other(format!( // "Reduced redundancy storage class parity {} should be greater than or equal to {}", // rrs_parity, MIN_PARITY_DRIVES // ))); // } if set_drive_count > 2 { if ss_parity > set_drive_count / 2 { return Err(Error::other(format!( "Standard storage class parity {} should be less than or equal to {}", ss_parity, set_drive_count / 2 ))); } if rrs_parity > set_drive_count / 2 { return Err(Error::other(format!( "Reduced redundancy storage class parity {} should be less than or equal to {}", rrs_parity, set_drive_count / 2 ))); } } if ss_parity > 0 && rrs_parity > 0 && ss_parity < rrs_parity { return Err(Error::other(format!( "Standard storage class parity drives {ss_parity} should be greater than or equal to Reduced redundancy storage class parity drives {rrs_parity}" ))); } Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_get_options.rs
crates/ecstore/src/client/api_get_options.rs
#![allow(clippy::map_entry)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use http::{HeaderMap, HeaderName, HeaderValue}; use std::collections::HashMap; use time::OffsetDateTime; use tracing::warn; use crate::client::api_error_response::err_invalid_argument; #[derive(Default)] #[allow(dead_code)] pub struct AdvancedGetOptions { pub replication_delete_marker: bool, pub is_replication_ready_for_delete_marker: bool, pub replication_proxy_request: String, } pub struct GetObjectOptions { pub headers: HashMap<String, String>, pub req_params: HashMap<String, String>, //pub server_side_encryption: encrypt.ServerSide, pub version_id: String, pub part_number: i64, pub checksum: bool, pub internal: AdvancedGetOptions, } pub type StatObjectOptions = GetObjectOptions; impl Default for GetObjectOptions { fn default() -> Self { Self { headers: HashMap::new(), req_params: HashMap::new(), //server_side_encryption: encrypt.ServerSide::default(), version_id: "".to_string(), part_number: 0, checksum: false, internal: AdvancedGetOptions::default(), } } } impl GetObjectOptions { pub fn header(&self) -> HeaderMap { let mut headers: HeaderMap = HeaderMap::with_capacity(self.headers.len()); for (k, v) in &self.headers { if let Ok(header_name) = HeaderName::from_bytes(k.as_bytes()) { headers.insert(header_name, v.parse().expect("err")); } else { warn!("Invalid header name: {}", k); } } if self.checksum { headers.insert("x-amz-checksum-mode", "ENABLED".parse().expect("err")); } headers } pub fn set(&self, key: &str, value: &str) { //self.headers[http.CanonicalHeaderKey(key)] = value; } pub fn set_req_param(&mut self, key: &str, value: &str) { self.req_params.insert(key.to_string(), value.to_string()); } pub fn add_req_param(&mut self, key: &str, value: &str) { self.req_params.insert(key.to_string(), value.to_string()); } pub fn set_match_etag(&mut self, etag: &str) -> Result<(), std::io::Error> { self.set("If-Match", &format!("\"{etag}\"")); Ok(()) } pub fn set_match_etag_except(&mut self, etag: &str) -> Result<(), std::io::Error> { self.set("If-None-Match", &format!("\"{etag}\"")); Ok(()) } pub fn set_unmodified(&mut self, mod_time: OffsetDateTime) -> Result<(), std::io::Error> { if mod_time.unix_timestamp() == 0 { return Err(std::io::Error::other(err_invalid_argument("Modified since cannot be empty."))); } self.set("If-Unmodified-Since", &mod_time.to_string()); Ok(()) } pub fn set_modified(&mut self, mod_time: OffsetDateTime) -> Result<(), std::io::Error> { if mod_time.unix_timestamp() == 0 { return Err(std::io::Error::other(err_invalid_argument("Modified since cannot be empty."))); } self.set("If-Modified-Since", &mod_time.to_string()); Ok(()) } pub fn set_range(&mut self, start: i64, end: i64) -> Result<(), std::io::Error> { if start == 0 && end < 0 { self.set("Range", &format!("bytes={}", end)); } else if 0 < start && end == 0 { self.set("Range", &format!("bytes={}-", start)); } else if 0 <= start && start <= end { self.set("Range", &format!("bytes={}-{}", start, end)); } else { return Err(std::io::Error::other(err_invalid_argument(&format!( "Invalid range specified: start={} end={}", start, end )))); } Ok(()) } pub fn to_query_values(&self) -> HashMap<String, String> { let mut url_values = HashMap::new(); if self.version_id != "" { url_values.insert("versionId".to_string(), self.version_id.clone()); } if self.part_number > 0 { url_values.insert("partNumber".to_string(), self.part_number.to_string()); } for (key, value) in self.req_params.iter() { url_values.insert(key.to_string(), value.to_string()); } url_values } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_put_object_common.rs
crates/ecstore/src/client/api_put_object_common.rs
#![allow(clippy::map_entry)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::client::{ api_error_response::{err_entity_too_large, err_invalid_argument}, api_put_object::PutObjectOptions, constants::{ABS_MIN_PART_SIZE, MAX_MULTIPART_PUT_OBJECT_SIZE, MAX_PART_SIZE, MAX_PARTS_COUNT, MIN_PART_SIZE}, transition_api::ReaderImpl, transition_api::TransitionClient, }; pub fn is_object(reader: &ReaderImpl) -> bool { todo!(); } pub fn is_read_at(reader: ReaderImpl) -> bool { todo!(); } pub fn optimal_part_info(object_size: i64, configured_part_size: u64) -> Result<(i64, i64, i64), std::io::Error> { let unknown_size; let mut object_size = object_size; if object_size == -1 { unknown_size = true; object_size = MAX_MULTIPART_PUT_OBJECT_SIZE; } else { unknown_size = false; } if object_size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other(err_entity_too_large( object_size, MAX_MULTIPART_PUT_OBJECT_SIZE, "", "", ))); } let mut part_size_flt: f64; if configured_part_size > 0 { if configured_part_size as i64 > object_size { return Err(std::io::Error::other(err_entity_too_large( configured_part_size as i64, object_size, "", "", ))); } if !unknown_size && object_size > (configured_part_size as i64 * MAX_PARTS_COUNT) { return Err(std::io::Error::other(err_invalid_argument( "Part size * max_parts(10000) is lesser than input objectSize.", ))); } if (configured_part_size as i64) < ABS_MIN_PART_SIZE { return Err(std::io::Error::other(err_invalid_argument( "Input part size is smaller than allowed minimum of 5MiB.", ))); } if configured_part_size as i64 > MAX_PART_SIZE { return Err(std::io::Error::other(err_invalid_argument( "Input part size is bigger than allowed maximum of 5GiB.", ))); } part_size_flt = configured_part_size as f64; if unknown_size { object_size = configured_part_size as i64 * MAX_PARTS_COUNT; } } else { let mut configured_part_size = configured_part_size; configured_part_size = MIN_PART_SIZE as u64; part_size_flt = (object_size / MAX_PARTS_COUNT) as f64; part_size_flt = (part_size_flt / configured_part_size as f64) * configured_part_size as f64; } let total_parts_count = (object_size as f64 / part_size_flt).ceil() as i64; let part_size = part_size_flt.ceil() as i64; let last_part_size = object_size - (total_parts_count - 1) * part_size; Ok((total_parts_count, part_size, last_part_size)) } impl TransitionClient { pub async fn new_upload_id( &self, bucket_name: &str, object_name: &str, opts: &PutObjectOptions, ) -> Result<String, std::io::Error> { let init_multipart_upload_result = self.initiate_multipart_upload(bucket_name, object_name, opts).await?; Ok(init_multipart_upload_result.upload_id) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/object_api_utils.rs
crates/ecstore/src/client/object_api_utils.rs
#![allow(clippy::map_entry)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use http::HeaderMap; use s3s::dto::ETag; use std::{collections::HashMap, io::Cursor, sync::Arc}; use tokio::io::BufReader; use crate::error::ErrorResponse; use crate::store_api::{GetObjectReader, HTTPRangeSpec, ObjectInfo, ObjectOptions}; use rustfs_filemeta::ObjectPartInfo; use rustfs_rio::HashReader; use s3s::S3ErrorCode; //#[derive(Clone)] pub struct PutObjReader { pub reader: HashReader, pub raw_reader: HashReader, //pub sealMD5Fn: SealMD5CurrFn, } #[allow(dead_code)] impl PutObjReader { pub fn new(raw_reader: HashReader) -> Self { todo!(); } fn md5_current_hex_string(&self) -> String { todo!(); } fn with_encryption(&mut self, enc_reader: HashReader) -> Result<(), std::io::Error> { self.reader = enc_reader; Ok(()) } } pub type ObjReaderFn<'a> = Arc<dyn Fn(BufReader<Cursor<Vec<u8>>>, HeaderMap) -> GetObjectReader + Send + Sync + 'a>; fn part_number_to_rangespec(oi: ObjectInfo, part_number: usize) -> Option<HTTPRangeSpec> { if oi.size == 0 || oi.parts.len() == 0 { return None; } let mut start: i64 = 0; let mut end: i64 = -1; let mut i = 0; while i < oi.parts.len() && i < part_number { start = end + 1; end = start + oi.parts[i].actual_size as i64 - 1; i += 1; } Some(HTTPRangeSpec { start, end, is_suffix_length: false, }) } fn get_compressed_offsets(oi: ObjectInfo, offset: i64) -> (i64, i64, i64, i64, u64) { let mut skip_length: i64 = 0; let mut cumulative_actual_size: i64 = 0; let mut first_part_idx: i64 = 0; let mut compressed_offset: i64 = 0; let mut part_skip: i64 = 0; let mut decrypt_skip: i64 = 0; let mut seq_num: u64 = 0; for (i, part) in oi.parts.iter().enumerate() { cumulative_actual_size += part.actual_size as i64; if cumulative_actual_size <= offset { compressed_offset += part.size as i64; } else { first_part_idx = i as i64; skip_length = cumulative_actual_size - part.actual_size as i64; break; } } skip_length = offset - skip_length; let parts: &[ObjectPartInfo] = &oi.parts; if skip_length > 0 && parts.len() > first_part_idx as usize && parts[first_part_idx as usize].index.as_ref().expect("err").len() > 0 { todo!(); } (compressed_offset, part_skip, first_part_idx, decrypt_skip, seq_num) } pub fn new_getobjectreader<'a>( rs: &Option<HTTPRangeSpec>, oi: &'a ObjectInfo, opts: &ObjectOptions, _h: &HeaderMap, ) -> Result<(ObjReaderFn<'a>, i64, i64), ErrorResponse> { //let (_, mut is_encrypted) = crypto.is_encrypted(oi.user_defined)?; let mut is_encrypted = false; let is_compressed = false; //oi.is_compressed_ok(); let mut rs_ = None; if rs.is_none() && opts.part_number.is_some() && opts.part_number.unwrap() > 0 { rs_ = part_number_to_rangespec(oi.clone(), opts.part_number.unwrap()); } let mut get_fn: ObjReaderFn; let (off, length) = match rs_.unwrap().get_offset_length(oi.size) { Ok(x) => x, Err(err) => { return Err(ErrorResponse { code: S3ErrorCode::InvalidRange, message: err.to_string(), key: None, bucket_name: None, region: None, request_id: None, host_id: "".to_string(), }); } }; get_fn = Arc::new(move |input_reader: BufReader<Cursor<Vec<u8>>>, _: HeaderMap| { //Box::pin({ let r = GetObjectReader { object_info: oi.clone(), stream: Box::new(input_reader), }; r //}) }); Ok((get_fn, off as i64, length as i64)) } /// Convert a raw stored ETag into the strongly-typed `s3s::dto::ETag`. /// /// Supports already quoted (`"abc"`), weak (`W/"abc"`), or plain (`abc`) values. pub fn to_s3s_etag(etag: &str) -> ETag { if let Some(rest) = etag.strip_prefix("W/\"") { if let Some(body) = rest.strip_suffix('"') { return ETag::Weak(body.to_string()); } return ETag::Weak(rest.to_string()); } if let Some(body) = etag.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) { return ETag::Strong(body.to_string()); } ETag::Strong(etag.to_string()) } pub fn get_raw_etag(metadata: &HashMap<String, String>) -> String { metadata .get("etag") .cloned() .or_else(|| metadata.get("md5Sum").cloned()) .unwrap_or_default() } #[cfg(test)] mod tests { use super::*; #[test] fn test_to_s3s_etag() { // Test unquoted ETag - should become strong etag assert_eq!( to_s3s_etag("6af8d12c0c74b78094884349f3c8a079"), ETag::Strong("6af8d12c0c74b78094884349f3c8a079".to_string()) ); assert_eq!( to_s3s_etag("\"6af8d12c0c74b78094884349f3c8a079\""), ETag::Strong("6af8d12c0c74b78094884349f3c8a079".to_string()) ); assert_eq!( to_s3s_etag("W/\"6af8d12c0c74b78094884349f3c8a079\""), ETag::Weak("6af8d12c0c74b78094884349f3c8a079".to_string()) ); assert_eq!(to_s3s_etag(""), ETag::Strong(String::new())); assert_eq!(to_s3s_etag("\"incomplete"), ETag::Strong("\"incomplete".to_string())); assert_eq!(to_s3s_etag("incomplete\""), ETag::Strong("incomplete\"".to_string())); } #[test] fn test_extract_etag() { let mut metadata = HashMap::new(); // Test with etag field metadata.insert("etag".to_string(), "abc123".to_string()); assert_eq!(get_raw_etag(&metadata), "abc123"); metadata.insert("etag".to_string(), "\"def456\"".to_string()); assert_eq!(get_raw_etag(&metadata), "\"def456\""); // Test fallback to md5Sum metadata.remove("etag"); metadata.insert("md5Sum".to_string(), "xyz789".to_string()); assert_eq!(get_raw_etag(&metadata), "xyz789"); metadata.clear(); assert_eq!(get_raw_etag(&metadata), ""); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_get_object_acl.rs
crates/ecstore/src/client/api_get_object_acl.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::client::{ api_error_response::http_resp_to_error_response, api_get_options::GetObjectOptions, transition_api::{ObjectInfo, ReaderImpl, RequestMetadata, TransitionClient}, }; use bytes::Bytes; use http::{HeaderMap, HeaderValue}; use rustfs_config::MAX_S3_CLIENT_RESPONSE_SIZE; use rustfs_utils::EMPTY_STRING_SHA256_HASH; use s3s::dto::Owner; use std::collections::HashMap; #[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Grantee { pub id: String, pub display_name: String, pub uri: String, } #[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Grant { pub grantee: Grantee, pub permission: String, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct AccessControlList { pub grant: Vec<Grant>, pub permission: String, } #[derive(Debug, Default, serde::Deserialize)] pub struct AccessControlPolicy { #[serde(skip)] owner: Owner, pub access_control_list: AccessControlList, } impl TransitionClient { pub async fn get_object_acl(&self, bucket_name: &str, object_name: &str) -> Result<ObjectInfo, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("acl".to_string(), "".to_string()); let mut resp = self .execute_method( http::Method::GET, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: url_values, custom_header: HeaderMap::new(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; if resp.status() != http::StatusCode::OK { let b = resp.body().bytes().expect("err").to_vec(); return Err(std::io::Error::other(http_resp_to_error_response(&resp, b, bucket_name, object_name))); } let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); let mut res = match quick_xml::de::from_str::<AccessControlPolicy>(&String::from_utf8(b).unwrap()) { Ok(result) => result, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; let mut obj_info = self .stat_object(bucket_name, object_name, &GetObjectOptions::default()) .await?; obj_info.owner.display_name = res.owner.display_name.clone(); obj_info.owner.id = res.owner.id.clone(); //obj_info.grant.extend(res.access_control_list.grant); let canned_acl = get_canned_acl(&res); if canned_acl != "" { obj_info .metadata .insert("X-Amz-Acl", HeaderValue::from_str(&canned_acl).unwrap()); return Ok(obj_info); } let grant_acl = get_amz_grant_acl(&res); /*for (k, v) in grant_acl { obj_info.metadata.insert(HeaderName::from_bytes(k.as_bytes()).unwrap(), HeaderValue::from_str(&v.to_string()).unwrap()); }*/ Ok(obj_info) } } fn get_canned_acl(ac_policy: &AccessControlPolicy) -> String { let grants = ac_policy.access_control_list.grant.clone(); if grants.len() == 1 { if grants[0].grantee.uri == "" && grants[0].permission == "FULL_CONTROL" { return "private".to_string(); } } else if grants.len() == 2 { for g in grants { if g.grantee.uri == "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" && &g.permission == "READ" { return "authenticated-read".to_string(); } if g.grantee.uri == "http://acs.amazonaws.com/groups/global/AllUsers" && &g.permission == "READ" { return "public-read".to_string(); } if g.permission == "READ" && g.grantee.id == ac_policy.owner.id.clone().unwrap() { return "bucket-owner-read".to_string(); } } } else if grants.len() == 3 { for g in grants { if g.grantee.uri == "http://acs.amazonaws.com/groups/global/AllUsers" && g.permission == "WRITE" { return "public-read-write".to_string(); } } } "".to_string() } pub fn get_amz_grant_acl(ac_policy: &AccessControlPolicy) -> HashMap<String, Vec<String>> { let grants = ac_policy.access_control_list.grant.clone(); let mut res = HashMap::<String, Vec<String>>::new(); for g in grants { let mut id = "id=".to_string(); id.push_str(&g.grantee.id); let permission: &str = &g.permission; match permission { "READ" => { res.entry("X-Amz-Grant-Read".to_string()).or_insert(vec![]).push(id); } "WRITE" => { res.entry("X-Amz-Grant-Write".to_string()).or_insert(vec![]).push(id); } "READ_ACP" => { res.entry("X-Amz-Grant-Read-Acp".to_string()).or_insert(vec![]).push(id); } "WRITE_ACP" => { res.entry("X-Amz-Grant-Write-Acp".to_string()).or_insert(vec![]).push(id); } "FULL_CONTROL" => { res.entry("X-Amz-Grant-Full-Control".to_string()).or_insert(vec![]).push(id); } _ => (), } } res }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_get_object_file.rs
crates/ecstore/src/client/api_get_object_file.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::HeaderMap; use std::io::Cursor; #[cfg(not(windows))] use std::os::unix::fs::MetadataExt; #[cfg(not(windows))] use std::os::unix::fs::OpenOptionsExt; #[cfg(not(windows))] use std::os::unix::fs::PermissionsExt; #[cfg(windows)] use std::os::windows::fs::MetadataExt; use tokio::io::BufReader; use crate::client::{ api_error_response::err_invalid_argument, api_get_options::GetObjectOptions, transition_api::{ObjectInfo, ReadCloser, ReaderImpl, RequestMetadata, TransitionClient, to_object_info}, }; impl TransitionClient { pub async fn fget_object( &self, bucket_name: &str, object_name: &str, file_path: &str, opts: GetObjectOptions, ) -> Result<(), std::io::Error> { match std::fs::metadata(file_path) { Ok(file_path_stat) => { let ft = file_path_stat.file_type(); if ft.is_dir() { return Err(std::io::Error::other(err_invalid_argument("filename is a directory."))); } } Err(err) => { return Err(std::io::Error::other(err)); } } let path = std::path::Path::new(file_path); if let Some(parent) = path.parent() { if let Some(object_dir) = parent.file_name() { match std::fs::create_dir_all(object_dir) { Ok(_) => { let dir = std::path::Path::new(object_dir); if let Ok(dir_stat) = dir.metadata() { #[cfg(not(windows))] dir_stat.permissions().set_mode(0o700); } } Err(err) => { return Err(std::io::Error::other(err)); } } } } let object_stat = match self.stat_object(bucket_name, object_name, &opts).await { Ok(object_stat) => object_stat, Err(err) => { return Err(std::io::Error::other(err)); } }; let mut file_part_path = file_path.to_string(); file_part_path.push_str("" /*sum_sha256_hex(object_stat.etag.as_bytes())*/); file_part_path.push_str(".part.rustfs"); #[cfg(not(windows))] let file_part = match std::fs::OpenOptions::new().mode(0o600).open(file_part_path.clone()) { Ok(file_part) => file_part, Err(err) => { return Err(std::io::Error::other(err)); } }; #[cfg(windows)] let file_part = match std::fs::OpenOptions::new().open(file_part_path.clone()) { Ok(file_part) => file_part, Err(err) => { return Err(std::io::Error::other(err)); } }; let mut close_and_remove = true; /*defer(|| { if close_and_remove { _ = file_part.close(); let _ = std::fs::remove(file_part_path); } });*/ let st = match file_part.metadata() { Ok(st) => st, Err(err) => { return Err(std::io::Error::other(err)); } }; let mut opts = opts; #[cfg(windows)] if st.file_size() > 0 { opts.set_range(st.file_size() as i64, 0); } let object_reader = match self.get_object(bucket_name, object_name, &opts) { Ok(object_reader) => object_reader, Err(err) => { return Err(std::io::Error::other(err)); } }; /*if let Err(err) = std::fs::copy(file_part, object_reader) { return Err(std::io::Error::other(err)); }*/ close_and_remove = false; /*if let Err(err) = file_part.close() { return Err(std::io::Error::other(err)); }*/ if let Err(err) = std::fs::rename(file_part_path, file_path) { return Err(std::io::Error::other(err)); } Ok(()) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_list.rs
crates/ecstore/src/client/api_list.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::client::{ api_error_response::http_resp_to_error_response, api_s3_datatypes::{ ListBucketResult, ListBucketV2Result, ListMultipartUploadsResult, ListObjectPartsResult, ListVersionsResult, ObjectPart, }, credentials, transition_api::{ReaderImpl, RequestMetadata, TransitionClient}, }; use crate::store_api::BucketInfo; use bytes::Bytes; use http::{HeaderMap, StatusCode}; use rustfs_config::MAX_S3_CLIENT_RESPONSE_SIZE; use rustfs_utils::hash::EMPTY_STRING_SHA256_HASH; use std::collections::HashMap; impl TransitionClient { pub fn list_buckets(&self) -> Result<Vec<BucketInfo>, std::io::Error> { todo!(); } pub async fn list_objects_v2_query( &self, bucket_name: &str, object_prefix: &str, continuation_token: &str, fetch_owner: bool, metadata: bool, delimiter: &str, start_after: &str, max_keys: i64, headers: HeaderMap, ) -> Result<ListBucketV2Result, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("list-type".to_string(), "2".to_string()); if metadata { url_values.insert("metadata".to_string(), "true".to_string()); } if start_after != "" { url_values.insert("start-after".to_string(), start_after.to_string()); } url_values.insert("encoding-type".to_string(), "url".to_string()); url_values.insert("prefix".to_string(), object_prefix.to_string()); url_values.insert("delimiter".to_string(), delimiter.to_string()); if continuation_token != "" { url_values.insert("continuation-token".to_string(), continuation_token.to_string()); } if fetch_owner { url_values.insert("fetch-owner".to_string(), "true".to_string()); } if max_keys > 0 { url_values.insert("max-keys".to_string(), max_keys.to_string()); } let mut resp = self .execute_method( http::Method::GET, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: "".to_string(), query_values: url_values, content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), custom_header: headers, content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; if resp.status() != StatusCode::OK { return Err(std::io::Error::other(http_resp_to_error_response(&resp, vec![], bucket_name, ""))); } //let mut list_bucket_result = ListBucketV2Result::default(); let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); let mut list_bucket_result = match quick_xml::de::from_str::<ListBucketV2Result>(&String::from_utf8(b).unwrap()) { Ok(result) => result, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; //println!("list_bucket_result: {:?}", list_bucket_result); if list_bucket_result.is_truncated && list_bucket_result.next_continuation_token == "" { return Err(std::io::Error::other(credentials::ErrorResponse { sts_error: credentials::STSError { r#type: "".to_string(), code: "NotImplemented".to_string(), message: "Truncated response should have continuation token set".to_string(), }, request_id: "".to_string(), })); } for (i, obj) in list_bucket_result.contents.iter_mut().enumerate() { obj.name = decode_s3_name(&obj.name, &list_bucket_result.encoding_type)?; //list_bucket_result.contents[i].mod_time = list_bucket_result.contents[i].mod_time.Truncate(time.Millisecond); } for (i, obj) in list_bucket_result.common_prefixes.iter_mut().enumerate() { obj.prefix = decode_s3_name(&obj.prefix, &list_bucket_result.encoding_type)?; } Ok(list_bucket_result) } pub fn list_object_versions_query( &self, bucket_name: &str, opts: &ListObjectsOptions, key_marker: &str, version_id_marker: &str, delimiter: &str, ) -> Result<ListVersionsResult, std::io::Error> { /*if err := s3utils.CheckValidBucketName(bucketName); err != nil { return ListVersionsResult{}, err } if err := s3utils.CheckValidObjectNamePrefix(opts.Prefix); err != nil { return ListVersionsResult{}, err } urlValues := make(url.Values) urlValues.Set("versions", "") urlValues.Set("prefix", opts.Prefix) urlValues.Set("delimiter", delimiter) if keyMarker != "" { urlValues.Set("key-marker", keyMarker) } if opts.max_keys > 0 { urlValues.Set("max-keys", fmt.Sprintf("%d", opts.max_keys)) } if versionIDMarker != "" { urlValues.Set("version-id-marker", versionIDMarker) } if opts.WithMetadata { urlValues.Set("metadata", "true") } urlValues.Set("encoding-type", "url") let resp = self.executeMethod(http::Method::GET, &mut RequestMetadata{ bucketName: bucketName, queryValues: urlValues, contentSHA256Hex: emptySHA256Hex, customHeader: opts.headers, }).await?; defer closeResponse(resp) if err != nil { return ListVersionsResult{}, err } if resp != nil { if resp.StatusCode != http.StatusOK { return ListVersionsResult{}, httpRespToErrorResponse(resp, bucketName, "") } } listObjectVersionsOutput := ListVersionsResult{} err = xml_decoder(resp.Body, &listObjectVersionsOutput) if err != nil { return ListVersionsResult{}, err } for i, obj := range listObjectVersionsOutput.Versions { listObjectVersionsOutput.Versions[i].Key, err = decode_s3_name(obj.Key, listObjectVersionsOutput.EncodingType) if err != nil { return listObjectVersionsOutput, err } } for i, obj := range listObjectVersionsOutput.CommonPrefixes { listObjectVersionsOutput.CommonPrefixes[i].Prefix, err = decode_s3_name(obj.Prefix, listObjectVersionsOutput.EncodingType) if err != nil { return listObjectVersionsOutput, err } } if listObjectVersionsOutput.NextKeyMarker != "" { listObjectVersionsOutput.NextKeyMarker, err = decode_s3_name(listObjectVersionsOutput.NextKeyMarker, listObjectVersionsOutput.EncodingType) if err != nil { return listObjectVersionsOutput, err } } Ok(listObjectVersionsOutput)*/ todo!(); } pub fn list_objects_query( &self, bucket_name: &str, object_prefix: &str, object_marker: &str, delimiter: &str, max_keys: i64, headers: HeaderMap, ) -> Result<ListBucketResult, std::io::Error> { todo!(); } pub fn list_multipart_uploads_query( &self, bucket_name: &str, key_marker: &str, upload_id_marker: &str, prefix: &str, delimiter: &str, max_uploads: i64, ) -> Result<ListMultipartUploadsResult, std::io::Error> { todo!(); } pub fn list_object_parts( &self, bucket_name: &str, object_name: &str, upload_id: &str, ) -> Result<HashMap<i64, ObjectPart>, std::io::Error> { todo!(); } pub fn find_upload_ids(&self, bucket_name: &str, object_name: &str) -> Result<Vec<String>, std::io::Error> { todo!(); } pub async fn list_object_parts_query( &self, bucket_name: &str, object_name: &str, upload_id: &str, part_number_marker: i64, max_parts: i64, ) -> Result<ListObjectPartsResult, std::io::Error> { todo!(); } } #[allow(dead_code)] pub struct ListObjectsOptions { reverse_versions: bool, with_versions: bool, with_metadata: bool, prefix: String, recursive: bool, max_keys: i64, start_after: String, use_v1: bool, headers: HeaderMap, } impl ListObjectsOptions { pub fn set(&mut self, key: &str, value: &str) { todo!(); } } fn decode_s3_name(name: &str, encoding_type: &str) -> Result<String, std::io::Error> { match encoding_type { "url" => { //return url::QueryUnescape(name); return Ok(name.to_string()); } _ => { return Ok(name.to_string()); } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_get_object.rs
crates/ecstore/src/client/api_get_object.rs
#![allow(clippy::map_entry)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::HeaderMap; use std::io::Cursor; use tokio::io::BufReader; use crate::client::{ api_error_response::err_invalid_argument, api_get_options::GetObjectOptions, transition_api::{ObjectInfo, ReadCloser, ReaderImpl, RequestMetadata, TransitionClient, to_object_info}, }; use rustfs_utils::hash::EMPTY_STRING_SHA256_HASH; impl TransitionClient { pub fn get_object(&self, bucket_name: &str, object_name: &str, opts: &GetObjectOptions) -> Result<Object, std::io::Error> { todo!(); } pub async fn get_object_inner( &self, bucket_name: &str, object_name: &str, opts: &GetObjectOptions, ) -> Result<(ObjectInfo, HeaderMap, ReadCloser), std::io::Error> { let resp = self .execute_method( http::Method::GET, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: opts.to_query_values(), custom_header: opts.header(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; let resp = &resp; let object_stat = to_object_info(bucket_name, object_name, resp.headers())?; let b = resp.body().bytes().expect("err").to_vec(); Ok((object_stat, resp.headers().clone(), BufReader::new(Cursor::new(b)))) } } #[derive(Default)] #[allow(dead_code)] pub struct GetRequest { pub buffer: Vec<u8>, pub offset: i64, pub did_offset_change: bool, pub been_read: bool, pub is_read_at: bool, pub is_read_op: bool, pub is_first_req: bool, pub setting_object_info: bool, } #[allow(dead_code)] pub struct GetResponse { pub size: i64, //pub error: error, pub did_read: bool, pub object_info: ObjectInfo, } #[derive(Default)] pub struct Object { //pub reqch: chan<- getRequest, //pub resch: <-chan getResponse, //pub cancel: context.CancelFunc, pub curr_offset: i64, pub object_info: ObjectInfo, pub seek_data: bool, pub is_closed: bool, pub is_started: bool, //pub prev_err: error, pub been_read: bool, pub object_info_set: bool, } impl Object { pub fn new() -> Object { Self { ..Default::default() } } fn do_get_request(&self, request: &GetRequest) -> Result<GetResponse, std::io::Error> { todo!() } fn set_offset(&mut self, bytes_read: i64) -> Result<(), std::io::Error> { self.curr_offset += bytes_read; Ok(()) } fn read(&mut self, b: &[u8]) -> Result<i64, std::io::Error> { let mut read_req = GetRequest { is_read_op: true, been_read: self.been_read, buffer: b.to_vec(), ..Default::default() }; if !self.is_started { read_req.is_first_req = true; } read_req.did_offset_change = self.seek_data; read_req.offset = self.curr_offset; let response = self.do_get_request(&read_req)?; let bytes_read = response.size; let oerr = self.set_offset(bytes_read); Ok(response.size) } fn stat(&self) -> Result<ObjectInfo, std::io::Error> { if !self.is_started || !self.object_info_set { let _ = self.do_get_request(&GetRequest { is_first_req: !self.is_started, setting_object_info: !self.object_info_set, ..Default::default() })?; } Ok(self.object_info.clone()) } fn read_at(&mut self, b: &[u8], offset: i64) -> Result<i64, std::io::Error> { self.curr_offset = offset; let mut read_at_req = GetRequest { is_read_op: true, is_read_at: true, did_offset_change: true, been_read: self.been_read, offset, buffer: b.to_vec(), ..Default::default() }; if !self.is_started { read_at_req.is_first_req = true; } let response = self.do_get_request(&read_at_req)?; let bytes_read = response.size; if !self.object_info_set { self.curr_offset += bytes_read; } else { let oerr = self.set_offset(bytes_read); } Ok(response.size) } fn seek(&mut self, offset: i64, whence: i64) -> Result<i64, std::io::Error> { if !self.is_started || !self.object_info_set { let seek_req = GetRequest { is_read_op: false, offset, is_first_req: true, ..Default::default() }; let _ = self.do_get_request(&seek_req); } let mut new_offset = self.curr_offset; match whence { 0 => { new_offset = offset; } 1 => { new_offset += offset; } 2 => { new_offset = self.object_info.size as i64 + offset as i64; } _ => { return Err(std::io::Error::other(err_invalid_argument(&format!("Invalid whence {}", whence)))); } } self.seek_data = (new_offset != self.curr_offset) || self.seek_data; self.curr_offset = new_offset; Ok(self.curr_offset) } fn close(&mut self) -> Result<(), std::io::Error> { self.is_closed = true; Ok(()) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_put_object_streaming.rs
crates/ecstore/src/client/api_put_object_streaming.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use futures::future::join_all; use http::{HeaderMap, HeaderName, HeaderValue, StatusCode}; use std::sync::RwLock; use std::{collections::HashMap, sync::Arc}; use time::{OffsetDateTime, format_description}; use tokio::{select, sync::mpsc}; use tokio_util::sync::CancellationToken; use tracing::warn; use uuid::Uuid; use crate::client::checksum::{ChecksumMode, add_auto_checksum_headers, apply_auto_checksum}; use crate::client::{ api_error_response::{err_invalid_argument, err_unexpected_eof, http_resp_to_error_response}, api_put_object::PutObjectOptions, api_put_object_common::{is_object, optimal_part_info}, api_put_object_multipart::UploadPartParams, api_s3_datatypes::{CompleteMultipartUpload, CompletePart, ObjectPart}, constants::ISO8601_DATEFORMAT, transition_api::{ReaderImpl, RequestMetadata, TransitionClient, UploadInfo}, }; use crate::client::utils::base64_encode; use rustfs_utils::path::trim_etag; use s3s::header::{X_AMZ_EXPIRATION, X_AMZ_VERSION_ID}; pub struct UploadedPartRes { pub error: std::io::Error, pub part_num: i64, pub size: i64, pub part: ObjectPart, } pub struct UploadPartReq { pub part_num: i64, pub part: ObjectPart, } impl TransitionClient { pub async fn put_object_multipart_stream( self: Arc<Self>, bucket_name: &str, object_name: &str, reader: ReaderImpl, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let info: UploadInfo; if opts.concurrent_stream_parts && opts.num_threads > 1 { info = self .put_object_multipart_stream_parallel(bucket_name, object_name, reader, opts) .await?; } else if !is_object(&reader) && !opts.send_content_md5 { info = self .put_object_multipart_stream_from_readat(bucket_name, object_name, reader, size, opts) .await?; } else { info = self .put_object_multipart_stream_optional_checksum(bucket_name, object_name, reader, size, opts) .await?; } Ok(info) } pub async fn put_object_multipart_stream_from_readat( &self, bucket_name: &str, object_name: &str, reader: ReaderImpl, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let ret = optimal_part_info(size, opts.part_size)?; let (total_parts_count, part_size, lastpart_size) = ret; let mut opts = opts.clone(); if opts.checksum.is_set() { opts.auto_checksum = opts.checksum.clone(); } let with_checksum = self.trailing_header_support; let upload_id = self.new_upload_id(bucket_name, object_name, &opts).await?; opts.user_metadata.remove("X-Amz-Checksum-Algorithm"); todo!(); } pub async fn put_object_multipart_stream_optional_checksum( &self, bucket_name: &str, object_name: &str, mut reader: ReaderImpl, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let mut opts = opts.clone(); if opts.checksum.is_set() { opts.auto_checksum = opts.checksum.clone(); opts.send_content_md5 = false; } if !opts.send_content_md5 { add_auto_checksum_headers(&mut opts); } let ret = optimal_part_info(size, opts.part_size)?; let (total_parts_count, mut part_size, lastpart_size) = ret; let upload_id = self.new_upload_id(bucket_name, object_name, &opts).await?; opts.user_metadata.remove("X-Amz-Checksum-Algorithm"); let mut custom_header = opts.header().clone(); let mut total_uploaded_size: i64 = 0; let mut parts_info = HashMap::<i64, ObjectPart>::new(); let mut buf = Vec::<u8>::with_capacity(part_size as usize); let mut md5_base64: String = "".to_string(); for part_number in 1..=total_parts_count { if part_number == total_parts_count { part_size = lastpart_size; } match &mut reader { ReaderImpl::Body(content_body) => { buf = content_body.to_vec(); } ReaderImpl::ObjectBody(content_body) => { buf = content_body.read_all().await?; } } let length = buf.len(); if opts.send_content_md5 { let mut md5_hasher = self.md5_hasher.lock().unwrap(); let md5_hash = md5_hasher.as_mut().expect("err"); let hash = md5_hash.hash_encode(&buf[..length]); md5_base64 = base64_encode(hash.as_ref()); } else { let mut crc = opts.auto_checksum.hasher()?; crc.update(&buf[..length]); let csum = crc.finalize(); if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) { custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err")); } else { warn!("Invalid header name: {}", opts.auto_checksum.key()); } } let hooked = ReaderImpl::Body(Bytes::from(buf)); //newHook(BufferReader::new(buf), opts.progress); let mut p = UploadPartParams { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), upload_id: upload_id.clone(), reader: hooked, part_number, md5_base64: md5_base64.clone(), size: part_size, //sse: opts.server_side_encryption, stream_sha256: !opts.disable_content_sha256, custom_header: custom_header.clone(), sha256_hex: "".to_string(), trailer: HeaderMap::new(), }; let obj_part = self.upload_part(&mut p).await?; parts_info.entry(part_number).or_insert(obj_part); total_uploaded_size += part_size as i64; } if size > 0 && total_uploaded_size != size { return Err(std::io::Error::other(err_unexpected_eof( total_uploaded_size, size, bucket_name, object_name, ))); } let mut compl_multipart_upload = CompleteMultipartUpload::default(); let mut all_parts = Vec::<ObjectPart>::with_capacity(parts_info.len()); let part_number = total_parts_count; for i in 1..part_number { let part = parts_info[&i].clone(); all_parts.push(part.clone()); compl_multipart_upload.parts.push(CompletePart { etag: part.etag, part_num: part.part_num, checksum_crc32: part.checksum_crc32, checksum_crc32c: part.checksum_crc32c, checksum_sha1: part.checksum_sha1, checksum_sha256: part.checksum_sha256, checksum_crc64nvme: part.checksum_crc64nvme, }); } compl_multipart_upload.parts.sort(); let mut opts = PutObjectOptions { //server_side_encryption: opts.server_side_encryption, auto_checksum: opts.auto_checksum, ..Default::default() }; apply_auto_checksum(&mut opts, &mut all_parts); let mut upload_info = self .complete_multipart_upload(bucket_name, object_name, &upload_id, compl_multipart_upload, &opts) .await?; upload_info.size = total_uploaded_size; Ok(upload_info) } pub async fn put_object_multipart_stream_parallel( self: Arc<Self>, bucket_name: &str, object_name: &str, mut reader: ReaderImpl, /*GetObjectReader*/ opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let mut opts = opts.clone(); if opts.checksum.is_set() { opts.send_content_md5 = false; opts.auto_checksum = opts.checksum.clone(); } if !opts.send_content_md5 { add_auto_checksum_headers(&mut opts); } let ret = optimal_part_info(-1, opts.part_size)?; let (total_parts_count, part_size, _) = ret; let upload_id = self.new_upload_id(bucket_name, object_name, &opts).await?; opts.user_metadata.remove("X-Amz-Checksum-Algorithm"); let mut total_uploaded_size: i64 = 0; let parts_info = Arc::new(RwLock::new(HashMap::<i64, ObjectPart>::new())); let n_buffers = opts.num_threads; let (bufs_tx, mut bufs_rx) = mpsc::channel(n_buffers as usize); //let all = Vec::<u8>::with_capacity(n_buffers as usize * part_size as usize); for i in 0..n_buffers { //bufs_tx.send(&all[i * part_size..i * part_size + part_size]); bufs_tx.send(Vec::<u8>::with_capacity(part_size as usize)); } let mut futures = Vec::with_capacity(total_parts_count as usize); let (err_tx, mut err_rx) = mpsc::channel(opts.num_threads as usize); let cancel_token = CancellationToken::new(); //reader = newHook(reader, opts.progress); for part_number in 1..=total_parts_count { let mut buf = Vec::<u8>::new(); select! { buf = bufs_rx.recv() => {} err = err_rx.recv() => { //cancel_token.cancel(); //wg.Wait() return Err(err.expect("err")); } else => (), } if buf.len() != part_size as usize { return Err(std::io::Error::other(format!( "read buffer < {} than expected partSize: {}", buf.len(), part_size ))); } match &mut reader { ReaderImpl::Body(content_body) => { buf = content_body.to_vec(); } ReaderImpl::ObjectBody(content_body) => { buf = content_body.read_all().await?; } } let length = buf.len(); let mut custom_header = HeaderMap::new(); if !opts.send_content_md5 { let mut crc = opts.auto_checksum.hasher()?; crc.update(&buf[..length]); let csum = crc.finalize(); if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) { custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err")); } else { warn!("Invalid header name: {}", opts.auto_checksum.key()); } } let clone_bufs_tx = bufs_tx.clone(); let clone_parts_info = parts_info.clone(); let clone_upload_id = upload_id.clone(); let clone_self = self.clone(); futures.push(async move { let mut md5_base64: String = "".to_string(); if opts.send_content_md5 { let mut md5_hasher = clone_self.md5_hasher.lock().unwrap(); let md5_hash = md5_hasher.as_mut().expect("err"); let hash = md5_hash.hash_encode(&buf[..length]); md5_base64 = base64_encode(hash.as_ref()); } //defer wg.Done() let mut p = UploadPartParams { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), upload_id: clone_upload_id, reader: ReaderImpl::Body(Bytes::from(buf.clone())), part_number, md5_base64, size: length as i64, //sse: opts.server_side_encryption, stream_sha256: !opts.disable_content_sha256, custom_header, sha256_hex: "".to_string(), trailer: HeaderMap::new(), }; let obj_part = clone_self.upload_part(&mut p).await.expect("err"); let mut clone_parts_info = clone_parts_info.write().unwrap(); clone_parts_info.entry(part_number).or_insert(obj_part); clone_bufs_tx.send(buf); }); total_uploaded_size += length as i64; } let results = join_all(futures).await; select! { err = err_rx.recv() => { return Err(err.expect("err")); } else => (), } let mut compl_multipart_upload = CompleteMultipartUpload::default(); let part_number: i64 = total_parts_count; let mut all_parts = Vec::<ObjectPart>::with_capacity(parts_info.read().unwrap().len()); for i in 1..part_number { let part = parts_info.read().unwrap()[&i].clone(); all_parts.push(part.clone()); compl_multipart_upload.parts.push(CompletePart { etag: part.etag, part_num: part.part_num, checksum_crc32: part.checksum_crc32, checksum_crc32c: part.checksum_crc32c, checksum_sha1: part.checksum_sha1, checksum_sha256: part.checksum_sha256, checksum_crc64nvme: part.checksum_crc64nvme, ..Default::default() }); } compl_multipart_upload.parts.sort(); let mut opts = PutObjectOptions { //server_side_encryption: opts.server_side_encryption, auto_checksum: opts.auto_checksum, ..Default::default() }; apply_auto_checksum(&mut opts, &mut all_parts); let mut upload_info = self .complete_multipart_upload(bucket_name, object_name, &upload_id, compl_multipart_upload, &opts) .await?; upload_info.size = total_uploaded_size; Ok(upload_info) } pub async fn put_object_gcs( &self, bucket_name: &str, object_name: &str, reader: ReaderImpl, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let mut opts = opts.clone(); if opts.checksum.is_set() { opts.send_content_md5 = false; } let md5_base64: String = "".to_string(); let progress_reader = reader; //newHook(reader, opts.progress); self.put_object_do(bucket_name, object_name, progress_reader, &md5_base64, "", size, &opts) .await } pub async fn put_object_do( &self, bucket_name: &str, object_name: &str, reader: ReaderImpl, md5_base64: &str, sha256_hex: &str, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let custom_header = opts.header(); let mut req_metadata = RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), custom_header, content_body: reader, content_length: size, content_md5_base64: md5_base64.to_string(), content_sha256_hex: sha256_hex.to_string(), stream_sha256: !opts.disable_content_sha256, add_crc: Default::default(), bucket_location: Default::default(), pre_sign_url: Default::default(), query_values: Default::default(), extra_pre_sign_header: Default::default(), expires: Default::default(), trailer: Default::default(), }; let mut add_crc = false; //self.trailing_header_support && md5_base64 == "" && !s3utils.IsGoogleEndpoint(self.endpoint_url) && (opts.disable_content_sha256 || self.secure); let mut opts = opts.clone(); if opts.checksum.is_set() { req_metadata.add_crc = opts.checksum; } else if add_crc { for (k, _) in opts.user_metadata { if k.to_lowercase().starts_with("x-amz-checksum-") { add_crc = false; } } if add_crc { opts.auto_checksum.set_default(ChecksumMode::ChecksumCRC32C); req_metadata.add_crc = opts.auto_checksum; } } if opts.internal.source_version_id != "" { if !opts.internal.source_version_id.is_empty() { if let Err(err) = Uuid::parse_str(&opts.internal.source_version_id) { return Err(std::io::Error::other(err_invalid_argument(&err.to_string()))); } } let mut url_values = HashMap::new(); url_values.insert("versionId".to_string(), opts.internal.source_version_id); req_metadata.query_values = url_values; } let resp = self.execute_method(http::Method::PUT, &mut req_metadata).await?; if resp.status() != StatusCode::OK { return Err(std::io::Error::other(http_resp_to_error_response( &resp, vec![], bucket_name, object_name, ))); } let (exp_time, rule_id) = if let Some(h_x_amz_expiration) = resp.headers().get(X_AMZ_EXPIRATION) { ( OffsetDateTime::parse(h_x_amz_expiration.to_str().unwrap(), ISO8601_DATEFORMAT).unwrap(), "".to_string(), ) } else { (OffsetDateTime::now_utc(), "".to_string()) }; let h = resp.headers(); Ok(UploadInfo { bucket: bucket_name.to_string(), key: object_name.to_string(), etag: trim_etag(h.get("ETag").expect("err").to_str().expect("err")), version_id: if let Some(h_x_amz_version_id) = h.get(X_AMZ_VERSION_ID) { h_x_amz_version_id.to_str().expect("err").to_string() } else { "".to_string() }, size, expiration: exp_time, expiration_rule_id: rule_id, checksum_crc32: if let Some(h_checksum_crc32) = h.get(ChecksumMode::ChecksumCRC32.key()) { h_checksum_crc32.to_str().expect("err").to_string() } else { "".to_string() }, checksum_crc32c: if let Some(h_checksum_crc32c) = h.get(ChecksumMode::ChecksumCRC32C.key()) { h_checksum_crc32c.to_str().expect("err").to_string() } else { "".to_string() }, checksum_sha1: if let Some(h_checksum_sha1) = h.get(ChecksumMode::ChecksumSHA1.key()) { h_checksum_sha1.to_str().expect("err").to_string() } else { "".to_string() }, checksum_sha256: if let Some(h_checksum_sha256) = h.get(ChecksumMode::ChecksumSHA256.key()) { h_checksum_sha256.to_str().expect("err").to_string() } else { "".to_string() }, checksum_crc64nvme: if let Some(h_checksum_crc64nvme) = h.get(ChecksumMode::ChecksumCRC64NVME.key()) { h_checksum_crc64nvme.to_str().expect("err").to_string() } else { "".to_string() }, ..Default::default() }) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/object_handlers_common.rs
crates/ecstore/src/client/object_handlers_common.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::StorageAPI; use crate::bucket::lifecycle::lifecycle; use crate::bucket::versioning::VersioningApi; use crate::bucket::versioning_sys::BucketVersioningSys; use crate::store::ECStore; use crate::store_api::{ObjectOptions, ObjectToDelete}; use rustfs_lock::MAX_DELETE_LIST; pub async fn delete_object_versions(api: ECStore, bucket: &str, to_del: &[ObjectToDelete], _lc_event: lifecycle::Event) { let mut remaining = to_del; loop { let mut to_del = remaining; if to_del.len() > MAX_DELETE_LIST { remaining = &to_del[MAX_DELETE_LIST..]; to_del = &to_del[..MAX_DELETE_LIST]; } else { remaining = &[]; } let vc = BucketVersioningSys::get(bucket).await.expect("err!"); let _deleted_objs = api.delete_objects( bucket, to_del.to_vec(), ObjectOptions { //prefix_enabled_fn: vc.prefix_enabled(""), version_suspended: vc.suspended(), ..Default::default() }, ); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_put_object.rs
crates/ecstore/src/client/api_put_object.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::{HeaderMap, HeaderName, HeaderValue}; use std::{collections::HashMap, sync::Arc}; use time::{Duration, OffsetDateTime, macros::format_description}; use tracing::{error, info, warn}; use s3s::dto::{ObjectLockLegalHoldStatus, ObjectLockRetentionMode, ReplicationStatus}; use s3s::header::{ X_AMZ_OBJECT_LOCK_LEGAL_HOLD, X_AMZ_OBJECT_LOCK_MODE, X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE, X_AMZ_REPLICATION_STATUS, X_AMZ_STORAGE_CLASS, X_AMZ_WEBSITE_REDIRECT_LOCATION, }; //use crate::disk::{BufferReader, Reader}; use crate::client::checksum::ChecksumMode; use crate::client::utils::base64_encode; use crate::client::{ api_error_response::{err_entity_too_large, err_invalid_argument}, api_put_object_common::optimal_part_info, api_put_object_multipart::UploadPartParams, api_s3_datatypes::{CompleteMultipartUpload, CompletePart, ObjectPart}, constants::{ISO8601_DATEFORMAT, MAX_MULTIPART_PUT_OBJECT_SIZE, MIN_PART_SIZE, TOTAL_WORKERS}, credentials::SignatureType, transition_api::{ReaderImpl, TransitionClient, UploadInfo}, utils::{is_amz_header, is_minio_header, is_rustfs_header, is_standard_header, is_storageclass_header}, }; #[derive(Debug, Clone)] pub struct AdvancedPutOptions { pub source_version_id: String, pub source_etag: String, pub replication_status: ReplicationStatus, pub source_mtime: OffsetDateTime, pub replication_request: bool, pub retention_timestamp: OffsetDateTime, pub tagging_timestamp: OffsetDateTime, pub legalhold_timestamp: OffsetDateTime, pub replication_validity_check: bool, } impl Default for AdvancedPutOptions { fn default() -> Self { Self { source_version_id: "".to_string(), source_etag: "".to_string(), replication_status: ReplicationStatus::from_static(ReplicationStatus::PENDING), source_mtime: OffsetDateTime::now_utc(), replication_request: false, retention_timestamp: OffsetDateTime::now_utc(), tagging_timestamp: OffsetDateTime::now_utc(), legalhold_timestamp: OffsetDateTime::now_utc(), replication_validity_check: false, } } } #[derive(Clone)] pub struct PutObjectOptions { pub user_metadata: HashMap<String, String>, pub user_tags: HashMap<String, String>, //pub progress: ReaderImpl, pub content_type: String, pub content_encoding: String, pub content_disposition: String, pub content_language: String, pub cache_control: String, pub expires: OffsetDateTime, pub mode: ObjectLockRetentionMode, pub retain_until_date: OffsetDateTime, //pub server_side_encryption: encrypt::ServerSide, pub num_threads: u64, pub storage_class: String, pub website_redirect_location: String, pub part_size: u64, pub legalhold: ObjectLockLegalHoldStatus, pub send_content_md5: bool, pub disable_content_sha256: bool, pub disable_multipart: bool, pub auto_checksum: ChecksumMode, pub checksum: ChecksumMode, pub concurrent_stream_parts: bool, pub internal: AdvancedPutOptions, pub custom_header: HeaderMap, } impl Default for PutObjectOptions { fn default() -> Self { Self { user_metadata: HashMap::new(), user_tags: HashMap::new(), //progress: ReaderImpl::Body(Bytes::new()), content_type: "".to_string(), content_encoding: "".to_string(), content_disposition: "".to_string(), content_language: "".to_string(), cache_control: "".to_string(), expires: OffsetDateTime::UNIX_EPOCH, mode: ObjectLockRetentionMode::from_static(""), retain_until_date: OffsetDateTime::UNIX_EPOCH, //server_side_encryption: encrypt.ServerSide::default(), num_threads: 0, storage_class: "".to_string(), website_redirect_location: "".to_string(), part_size: 0, legalhold: ObjectLockLegalHoldStatus::from_static(ObjectLockLegalHoldStatus::OFF), send_content_md5: false, disable_content_sha256: false, disable_multipart: false, auto_checksum: ChecksumMode::ChecksumNone, checksum: ChecksumMode::ChecksumNone, concurrent_stream_parts: false, internal: AdvancedPutOptions::default(), custom_header: HeaderMap::new(), } } } #[allow(dead_code)] impl PutObjectOptions { fn set_match_etag(&mut self, etag: &str) { if etag == "*" { self.custom_header .insert("If-Match", HeaderValue::from_str("*").expect("err")); } else { self.custom_header .insert("If-Match", HeaderValue::from_str(&format!("\"{}\"", etag)).expect("err")); } } fn set_match_etag_except(&mut self, etag: &str) { if etag == "*" { self.custom_header .insert("If-None-Match", HeaderValue::from_str("*").expect("err")); } else { self.custom_header .insert("If-None-Match", HeaderValue::from_str(&format!("\"{etag}\"")).expect("err")); } } pub fn header(&self) -> HeaderMap { let mut header = HeaderMap::new(); let mut content_type = self.content_type.clone(); if content_type == "" { content_type = "application/octet-stream".to_string(); } header.insert("Content-Type", HeaderValue::from_str(&content_type).expect("err")); if self.content_encoding != "" { header.insert("Content-Encoding", HeaderValue::from_str(&self.content_encoding).expect("err")); } if self.content_disposition != "" { header.insert("Content-Disposition", HeaderValue::from_str(&self.content_disposition).expect("err")); } if self.content_language != "" { header.insert("Content-Language", HeaderValue::from_str(&self.content_language).expect("err")); } if self.cache_control != "" { header.insert("Cache-Control", HeaderValue::from_str(&self.cache_control).expect("err")); } if self.expires.unix_timestamp() != 0 { header.insert( "Expires", HeaderValue::from_str(&self.expires.format(ISO8601_DATEFORMAT).unwrap()).expect("err"), ); //rustfs invalid header } if self.mode.as_str() != "" { header.insert(X_AMZ_OBJECT_LOCK_MODE, HeaderValue::from_str(self.mode.as_str()).expect("err")); } if self.retain_until_date.unix_timestamp() != 0 { header.insert( X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE, HeaderValue::from_str(&self.retain_until_date.format(ISO8601_DATEFORMAT).unwrap()).expect("err"), ); } if self.legalhold.as_str() != "" { header.insert(X_AMZ_OBJECT_LOCK_LEGAL_HOLD, HeaderValue::from_str(self.legalhold.as_str()).expect("err")); } if self.storage_class != "" { header.insert(X_AMZ_STORAGE_CLASS, HeaderValue::from_str(&self.storage_class).expect("err")); } if self.website_redirect_location != "" { header.insert( X_AMZ_WEBSITE_REDIRECT_LOCATION, HeaderValue::from_str(&self.website_redirect_location).expect("err"), ); } if !self.internal.replication_status.as_str().is_empty() { header.insert( X_AMZ_REPLICATION_STATUS, HeaderValue::from_str(self.internal.replication_status.as_str()).expect("err"), ); } for (k, v) in &self.user_metadata { if is_amz_header(k) || is_standard_header(k) || is_storageclass_header(k) || is_rustfs_header(k) || is_minio_header(k) { if let Ok(header_name) = HeaderName::from_bytes(k.as_bytes()) { header.insert(header_name, HeaderValue::from_str(&v).unwrap()); } } else if let Ok(header_name) = HeaderName::from_bytes(format!("x-amz-meta-{}", k).as_bytes()) { header.insert(header_name, HeaderValue::from_str(&v).unwrap()); } } for (k, v) in self.custom_header.iter() { header.insert(k.clone(), v.clone()); } header } fn validate(&self, c: TransitionClient) -> Result<(), std::io::Error> { //if self.checksum.is_set() { /*if !self.trailing_header_support { return Err(Error::from(err_invalid_argument("Checksum requires Client with TrailingHeaders enabled"))); }*/ /*else if self.override_signer_type == SignatureType::SignatureV2 { return Err(Error::from(err_invalid_argument("Checksum cannot be used with v2 signatures"))); }*/ //} Ok(()) } } impl TransitionClient { pub async fn put_object( self: Arc<Self>, bucket_name: &str, object_name: &str, reader: ReaderImpl, object_size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { if object_size < 0 && opts.disable_multipart { return Err(std::io::Error::other("object size must be provided with disable multipart upload")); } self.put_object_common(bucket_name, object_name, reader, object_size, opts) .await } pub async fn put_object_common( self: Arc<Self>, bucket_name: &str, object_name: &str, reader: ReaderImpl, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { if size > MAX_MULTIPART_PUT_OBJECT_SIZE { return Err(std::io::Error::other(err_entity_too_large( size, MAX_MULTIPART_PUT_OBJECT_SIZE, bucket_name, object_name, ))); } let mut opts = opts.clone(); opts.auto_checksum.set_default(ChecksumMode::ChecksumCRC32C); let mut part_size = opts.part_size as i64; if opts.part_size == 0 { part_size = MIN_PART_SIZE; } if SignatureType::SignatureV2 == self.override_signer_type { if size >= 0 && size < part_size || opts.disable_multipart { return self.put_object_gcs(bucket_name, object_name, reader, size, &opts).await; } return self.put_object_multipart(bucket_name, object_name, reader, size, &opts).await; } if size < 0 { if opts.disable_multipart { return Err(std::io::Error::other("no length provided and multipart disabled")); } if opts.concurrent_stream_parts && opts.num_threads > 1 { return self .put_object_multipart_stream_parallel(bucket_name, object_name, reader, &opts) .await; } return self .put_object_multipart_stream_no_length(bucket_name, object_name, reader, &opts) .await; } if size <= part_size || opts.disable_multipart { return self.put_object_gcs(bucket_name, object_name, reader, size, &opts).await; } self.put_object_multipart_stream(bucket_name, object_name, reader, size, &opts) .await } pub async fn put_object_multipart_stream_no_length( &self, bucket_name: &str, object_name: &str, mut reader: ReaderImpl, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let mut total_uploaded_size: i64 = 0; let mut compl_multipart_upload = CompleteMultipartUpload::default(); let (total_parts_count, part_size, _) = optimal_part_info(-1, opts.part_size)?; let mut opts = opts.clone(); if opts.checksum.is_set() { opts.send_content_md5 = false; opts.auto_checksum = opts.checksum.clone(); } if !opts.send_content_md5 { //add_auto_checksum_headers(&mut opts); } let upload_id = self.new_upload_id(bucket_name, object_name, &opts).await?; opts.user_metadata.remove("X-Amz-Checksum-Algorithm"); let mut part_number = 1; let mut parts_info = HashMap::<i64, ObjectPart>::new(); let mut buf = Vec::<u8>::with_capacity(part_size as usize); let mut custom_header = HeaderMap::new(); while part_number <= total_parts_count { buf = match &mut reader { ReaderImpl::Body(content_body) => content_body.to_vec(), ReaderImpl::ObjectBody(content_body) => content_body.read_all().await?, }; let length = buf.len(); let mut md5_base64: String = "".to_string(); if opts.send_content_md5 { let mut md5_hasher = self.md5_hasher.lock().unwrap(); let hash = md5_hasher.as_mut().expect("err"); let hash = hash.hash_encode(&buf[..length]); md5_base64 = base64_encode(hash.as_ref()); } else { let mut crc = opts.auto_checksum.hasher()?; crc.update(&buf[..length]); let csum = crc.finalize(); if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) { custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err")); } else { warn!("Invalid header name: {}", opts.auto_checksum.key()); } } //let rd = newHook(bytes.NewReader(buf[..length]), opts.progress); let rd = ReaderImpl::Body(Bytes::from(buf)); let mut p = UploadPartParams { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), upload_id: upload_id.clone(), reader: rd, part_number, md5_base64, size: length as i64, //sse: opts.server_side_encryption, stream_sha256: !opts.disable_content_sha256, custom_header: custom_header.clone(), sha256_hex: Default::default(), trailer: Default::default(), }; let obj_part = self.upload_part(&mut p).await?; parts_info.entry(part_number).or_insert(obj_part); total_uploaded_size += length as i64; part_number += 1; } let mut all_parts = Vec::<ObjectPart>::with_capacity(parts_info.len()); for i in 1..part_number { let part = parts_info[&i].clone(); all_parts.push(part.clone()); compl_multipart_upload.parts.push(CompletePart { etag: part.etag, part_num: part.part_num, checksum_crc32: part.checksum_crc32, checksum_crc32c: part.checksum_crc32c, checksum_sha1: part.checksum_sha1, checksum_sha256: part.checksum_sha256, checksum_crc64nvme: part.checksum_crc64nvme, ..Default::default() }); } compl_multipart_upload.parts.sort(); let opts = PutObjectOptions { //server_side_encryption: opts.server_side_encryption, auto_checksum: opts.auto_checksum, ..Default::default() }; //apply_auto_checksum(&mut opts, all_parts); let mut upload_info = self .complete_multipart_upload(bucket_name, object_name, &upload_id, compl_multipart_upload, &opts) .await?; upload_info.size = total_uploaded_size; Ok(upload_info) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_get_object_attributes.rs
crates/ecstore/src/client/api_get_object_attributes.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::{HeaderMap, HeaderValue}; use std::collections::HashMap; use time::OffsetDateTime; use crate::client::constants::{GET_OBJECT_ATTRIBUTES_MAX_PARTS, GET_OBJECT_ATTRIBUTES_TAGS, ISO8601_DATEFORMAT}; use crate::client::{ api_get_object_acl::AccessControlPolicy, transition_api::{ReaderImpl, RequestMetadata, TransitionClient}, }; use rustfs_config::MAX_S3_CLIENT_RESPONSE_SIZE; use rustfs_utils::EMPTY_STRING_SHA256_HASH; use s3s::Body; use s3s::header::{X_AMZ_MAX_PARTS, X_AMZ_OBJECT_ATTRIBUTES, X_AMZ_PART_NUMBER_MARKER, X_AMZ_VERSION_ID}; pub struct ObjectAttributesOptions { pub max_parts: i64, pub version_id: String, pub part_number_marker: i64, //server_side_encryption: encrypt::ServerSide, } pub struct ObjectAttributes { pub version_id: String, pub last_modified: OffsetDateTime, pub object_attributes_response: ObjectAttributesResponse, } impl ObjectAttributes { fn new() -> Self { Self { version_id: "".to_string(), last_modified: OffsetDateTime::now_utc(), object_attributes_response: ObjectAttributesResponse::new(), } } } #[derive(Debug, Default, serde::Deserialize)] pub struct Checksum { checksum_crc32: String, checksum_crc32c: String, checksum_sha1: String, checksum_sha256: String, } impl Checksum { fn new() -> Self { Self { checksum_crc32: "".to_string(), checksum_crc32c: "".to_string(), checksum_sha1: "".to_string(), checksum_sha256: "".to_string(), } } } #[derive(Debug, Default, serde::Deserialize)] pub struct ObjectParts { pub parts_count: i64, pub part_number_marker: i64, pub next_part_number_marker: i64, pub max_parts: i64, is_truncated: bool, parts: Vec<ObjectAttributePart>, } impl ObjectParts { fn new() -> Self { Self { parts_count: 0, part_number_marker: 0, next_part_number_marker: 0, max_parts: 0, is_truncated: false, parts: Vec::new(), } } } #[derive(Debug, Default, serde::Deserialize)] pub struct ObjectAttributesResponse { pub etag: String, pub storage_class: String, pub object_size: i64, pub checksum: Checksum, pub object_parts: ObjectParts, } impl ObjectAttributesResponse { fn new() -> Self { Self { etag: "".to_string(), storage_class: "".to_string(), object_size: 0, checksum: Checksum::new(), object_parts: ObjectParts::new(), } } } #[derive(Debug, Default, serde::Deserialize)] struct ObjectAttributePart { checksum_crc32: String, checksum_crc32c: String, checksum_sha1: String, checksum_sha256: String, part_number: i64, size: i64, } impl ObjectAttributes { pub async fn parse_response(&mut self, resp: &mut http::Response<Body>) -> Result<(), std::io::Error> { let h = resp.headers(); let mod_time = OffsetDateTime::parse(h.get("Last-Modified").unwrap().to_str().unwrap(), ISO8601_DATEFORMAT).unwrap(); //RFC7231Time self.last_modified = mod_time; self.version_id = h.get(X_AMZ_VERSION_ID).unwrap().to_str().unwrap().to_string(); let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); let mut response = match quick_xml::de::from_str::<ObjectAttributesResponse>(&String::from_utf8(b).unwrap()) { Ok(result) => result, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; self.object_attributes_response = response; Ok(()) } } impl TransitionClient { pub async fn get_object_attributes( &self, bucket_name: &str, object_name: &str, opts: ObjectAttributesOptions, ) -> Result<ObjectAttributes, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("attributes".to_string(), "".to_string()); if opts.version_id != "" { url_values.insert("versionId".to_string(), opts.version_id); } let mut headers = HeaderMap::new(); headers.insert(X_AMZ_OBJECT_ATTRIBUTES, HeaderValue::from_str(GET_OBJECT_ATTRIBUTES_TAGS).unwrap()); if opts.part_number_marker > 0 { headers.insert( X_AMZ_PART_NUMBER_MARKER, HeaderValue::from_str(&opts.part_number_marker.to_string()).unwrap(), ); } if opts.max_parts > 0 { headers.insert(X_AMZ_MAX_PARTS, HeaderValue::from_str(&opts.max_parts.to_string()).unwrap()); } else { headers.insert( X_AMZ_MAX_PARTS, HeaderValue::from_str(&GET_OBJECT_ATTRIBUTES_MAX_PARTS.to_string()).unwrap(), ); } /*if opts.server_side_encryption.is_some() { opts.server_side_encryption.Marshal(headers); }*/ let mut resp = self .execute_method( http::Method::HEAD, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: url_values, custom_header: headers, content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), content_md5_base64: "".to_string(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; let h = resp.headers(); let has_etag = h.get("ETag").unwrap().to_str().unwrap(); if !has_etag.is_empty() { return Err(std::io::Error::other( "get_object_attributes is not supported by the current endpoint version", )); } if resp.status() != http::StatusCode::OK { let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); let err_body = String::from_utf8(b).unwrap(); let mut er = match quick_xml::de::from_str::<AccessControlPolicy>(&err_body) { Ok(result) => result, Err(err) => { return Err(std::io::Error::other(err.to_string())); } }; return Err(std::io::Error::other(er.access_control_list.permission)); } let mut oa = ObjectAttributes::new(); oa.parse_response(&mut resp).await?; Ok(oa) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_s3_datatypes.rs
crates/ecstore/src/client/api_s3_datatypes.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use s3s::dto::Owner; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use time::OffsetDateTime; use crate::client::checksum::ChecksumMode; use crate::client::transition_api::ObjectMultipartInfo; use crate::client::utils::base64_decode; use super::transition_api; pub struct ListAllMyBucketsResult { pub owner: Owner, } #[derive(Debug, Default, Serialize, Deserialize)] pub struct CommonPrefix { pub prefix: String, } #[derive(Debug, Default, Serialize, Deserialize)] #[serde(default, rename_all = "PascalCase")] pub struct ListBucketV2Result { pub common_prefixes: Vec<CommonPrefix>, pub contents: Vec<transition_api::ObjectInfo>, pub delimiter: String, pub encoding_type: String, pub is_truncated: bool, pub max_keys: i64, pub name: String, pub next_continuation_token: String, pub continuation_token: String, pub prefix: String, pub fetch_owner: String, pub start_after: String, } #[allow(dead_code)] pub struct Version { etag: String, is_latest: bool, key: String, last_modified: OffsetDateTime, owner: Owner, size: i64, storage_class: String, version_id: String, user_metadata: HashMap<String, String>, user_tags: HashMap<String, String>, is_delete_marker: bool, } pub struct ListVersionsResult { versions: Vec<Version>, common_prefixes: Vec<CommonPrefix>, name: String, prefix: String, delimiter: String, max_keys: i64, encoding_type: String, is_truncated: bool, key_marker: String, version_id_marker: String, next_key_marker: String, next_version_id_marker: String, } pub struct ListBucketResult { common_prefixes: Vec<CommonPrefix>, contents: Vec<transition_api::ObjectInfo>, delimiter: String, encoding_type: String, is_truncated: bool, marker: String, max_keys: i64, name: String, next_marker: String, prefix: String, } pub struct ListMultipartUploadsResult { bucket: String, key_marker: String, upload_id_marker: String, next_key_marker: String, next_upload_id_marker: String, encoding_type: String, max_uploads: i64, is_truncated: bool, uploads: Vec<ObjectMultipartInfo>, prefix: String, delimiter: String, common_prefixes: Vec<CommonPrefix>, } pub struct Initiator { id: String, display_name: String, } pub struct CopyObjectResult { pub etag: String, pub last_modified: OffsetDateTime, } #[derive(Debug, Clone)] pub struct ObjectPart { pub etag: String, pub part_num: i64, pub last_modified: OffsetDateTime, pub size: i64, pub checksum_crc32: String, pub checksum_crc32c: String, pub checksum_sha1: String, pub checksum_sha256: String, pub checksum_crc64nvme: String, } impl Default for ObjectPart { fn default() -> Self { ObjectPart { etag: Default::default(), part_num: 0, last_modified: OffsetDateTime::now_utc(), size: 0, checksum_crc32: Default::default(), checksum_crc32c: Default::default(), checksum_sha1: Default::default(), checksum_sha256: Default::default(), checksum_crc64nvme: Default::default(), } } } impl ObjectPart { fn checksum(&self, t: &ChecksumMode) -> String { match t { ChecksumMode::ChecksumCRC32C => { return self.checksum_crc32c.clone(); } ChecksumMode::ChecksumCRC32 => { return self.checksum_crc32.clone(); } ChecksumMode::ChecksumSHA1 => { return self.checksum_sha1.clone(); } ChecksumMode::ChecksumSHA256 => { return self.checksum_sha256.clone(); } ChecksumMode::ChecksumCRC64NVME => { return self.checksum_crc64nvme.clone(); } _ => { return "".to_string(); } } } pub fn checksum_raw(&self, t: &ChecksumMode) -> Result<Vec<u8>, std::io::Error> { let b = self.checksum(t); if b == "" { return Err(std::io::Error::other("no checksum set")); } let decoded = match base64_decode(b.as_bytes()) { Ok(b) => b, Err(e) => return Err(std::io::Error::other(e)), }; if decoded.len() != t.raw_byte_len() as usize { return Err(std::io::Error::other("checksum length mismatch")); } Ok(decoded) } } pub struct ListObjectPartsResult { pub bucket: String, pub key: String, pub upload_id: String, pub initiator: Initiator, pub owner: Owner, pub storage_class: String, pub part_number_marker: i32, pub next_part_number_marker: i32, pub max_parts: i32, pub checksum_algorithm: String, pub checksum_type: String, pub is_truncated: bool, pub object_parts: Vec<ObjectPart>, pub encoding_type: String, } #[derive(Debug, Default)] pub struct InitiateMultipartUploadResult { pub bucket: String, pub key: String, pub upload_id: String, } #[derive(Debug, Default)] pub struct CompleteMultipartUploadResult { pub location: String, pub bucket: String, pub key: String, pub etag: String, pub checksum_crc32: String, pub checksum_crc32c: String, pub checksum_sha1: String, pub checksum_sha256: String, pub checksum_crc64nvme: String, } #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)] pub struct CompletePart { //api has pub etag: String, pub part_num: i64, pub checksum_crc32: String, pub checksum_crc32c: String, pub checksum_sha1: String, pub checksum_sha256: String, pub checksum_crc64nvme: String, } impl CompletePart { fn checksum(&self, t: &ChecksumMode) -> String { match t { ChecksumMode::ChecksumCRC32C => { return self.checksum_crc32c.clone(); } ChecksumMode::ChecksumCRC32 => { return self.checksum_crc32.clone(); } ChecksumMode::ChecksumSHA1 => { return self.checksum_sha1.clone(); } ChecksumMode::ChecksumSHA256 => { return self.checksum_sha256.clone(); } ChecksumMode::ChecksumCRC64NVME => { return self.checksum_crc64nvme.clone(); } _ => { return "".to_string(); } } } } pub struct CopyObjectPartResult { pub etag: String, pub last_modified: OffsetDateTime, } #[derive(Debug, Default, serde::Serialize)] pub struct CompleteMultipartUpload { pub parts: Vec<CompletePart>, } impl CompleteMultipartUpload { pub fn marshal_msg(&self) -> Result<String, std::io::Error> { //let buf = serde_json::to_string(self)?; let buf = match quick_xml::se::to_string(self) { Ok(buf) => buf, Err(e) => { return Err(std::io::Error::other(e)); } }; Ok(buf) } pub fn unmarshal(buf: &[u8]) -> Result<Self, std::io::Error> { todo!(); } } pub struct CreateBucketConfiguration { pub location: String, } #[derive(serde::Serialize)] pub struct DeleteObject { //api has pub key: String, pub version_id: String, } pub struct DeletedObject { //s3s has pub key: String, pub version_id: String, pub deletemarker: bool, pub deletemarker_version_id: String, } pub struct NonDeletedObject { pub key: String, pub code: String, pub message: String, pub version_id: String, } #[derive(serde::Serialize)] pub struct DeleteMultiObjects { pub quiet: bool, pub objects: Vec<DeleteObject>, } impl DeleteMultiObjects { pub fn marshal_msg(&self) -> Result<String, std::io::Error> { //let buf = serde_json::to_string(self)?; let buf = match quick_xml::se::to_string(self) { Ok(buf) => buf, Err(e) => { return Err(std::io::Error::other(e)); } }; Ok(buf) } pub fn unmarshal(buf: &[u8]) -> Result<Self, std::io::Error> { todo!(); } } pub struct DeleteMultiObjectsResult { pub deleted_objects: Vec<DeletedObject>, pub undeleted_objects: Vec<NonDeletedObject>, }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/bucket_cache.rs
crates/ecstore/src/client/bucket_cache.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use super::constants::UNSIGNED_PAYLOAD; use super::credentials::SignatureType; use crate::client::{ api_error_response::http_resp_to_error_response, transition_api::{CreateBucketConfiguration, LocationConstraint, TransitionClient}, }; use http::Request; use hyper::StatusCode; use rustfs_config::MAX_S3_CLIENT_RESPONSE_SIZE; use rustfs_utils::hash::EMPTY_STRING_SHA256_HASH; use s3s::Body; use s3s::S3ErrorCode; use std::collections::HashMap; #[derive(Debug, Clone)] pub struct BucketLocationCache { items: HashMap<String, String>, } impl BucketLocationCache { pub fn new() -> BucketLocationCache { BucketLocationCache { items: HashMap::new() } } pub fn get(&self, bucket_name: &str) -> Option<String> { self.items.get(bucket_name).map(|s| s.clone()) } pub fn set(&mut self, bucket_name: &str, location: &str) { self.items.insert(bucket_name.to_string(), location.to_string()); } pub fn delete(&mut self, bucket_name: &str) { self.items.remove(bucket_name); } } impl TransitionClient { pub async fn get_bucket_location(&self, bucket_name: &str) -> Result<String, std::io::Error> { Ok(self.get_bucket_location_inner(bucket_name).await?) } async fn get_bucket_location_inner(&self, bucket_name: &str) -> Result<String, std::io::Error> { if self.region != "" { return Ok(self.region.clone()); } let mut location; { let mut bucket_loc_cache = self.bucket_loc_cache.lock().unwrap(); let ret = bucket_loc_cache.get(bucket_name); if let Some(location) = ret { return Ok(location); } //location = ret?; } let req = self.get_bucket_location_request(bucket_name)?; let mut resp = self.doit(req).await?; location = process_bucket_location_response(resp, bucket_name, &self.tier_type).await?; { let mut bucket_loc_cache = self.bucket_loc_cache.lock().unwrap(); bucket_loc_cache.set(bucket_name, &location); } Ok(location) } fn get_bucket_location_request(&self, bucket_name: &str) -> Result<http::Request<Body>, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("location".to_string(), "".to_string()); let mut target_url = self.endpoint_url.clone(); let scheme = self.endpoint_url.scheme(); let h = target_url.host().expect("host is none."); let default_port = if scheme == "https" { 443 } else { 80 }; let p = target_url.port().unwrap_or(default_port); let is_virtual_style = self.is_virtual_host_style_request(&target_url, bucket_name); let mut url_str: String = "".to_string(); if is_virtual_style { url_str = scheme.to_string(); url_str.push_str("://"); url_str.push_str(bucket_name); url_str.push_str("."); url_str.push_str(target_url.host_str().expect("err")); url_str.push_str("/?location"); } else { let mut path = bucket_name.to_string(); path.push_str("/"); target_url.set_path(&path); { let mut q = target_url.query_pairs_mut(); for (k, v) in url_values { q.append_pair(&k, &urlencoding::encode(&v)); } } url_str = target_url.to_string(); } let Ok(mut req) = Request::builder().method(http::Method::GET).uri(url_str).body(Body::empty()) else { return Err(std::io::Error::other("create request error")); }; self.set_user_agent(&mut req); let value; { let mut creds_provider = self.creds_provider.lock().unwrap(); value = match creds_provider.get_with_context(Some(self.cred_context())) { Ok(v) => v, Err(err) => { return Err(std::io::Error::other(err)); } }; } let mut signer_type = value.signer_type.clone(); let mut access_key_id = value.access_key_id; let mut secret_access_key = value.secret_access_key; let mut session_token = value.session_token; if self.override_signer_type != SignatureType::SignatureDefault { signer_type = self.override_signer_type.clone(); } if value.signer_type == SignatureType::SignatureAnonymous { signer_type = SignatureType::SignatureAnonymous } if signer_type == SignatureType::SignatureAnonymous { return Ok(req); } if signer_type == SignatureType::SignatureV2 { let req = rustfs_signer::sign_v2(req, 0, &access_key_id, &secret_access_key, is_virtual_style); return Ok(req); } let mut content_sha256 = EMPTY_STRING_SHA256_HASH.to_string(); if self.secure { content_sha256 = UNSIGNED_PAYLOAD.to_string(); } req.headers_mut() .insert("X-Amz-Content-Sha256", content_sha256.parse().unwrap()); let req = rustfs_signer::sign_v4(req, 0, &access_key_id, &secret_access_key, &session_token, "us-east-1"); Ok(req) } } async fn process_bucket_location_response( mut resp: http::Response<Body>, bucket_name: &str, tier_type: &str, ) -> Result<String, std::io::Error> { //if resp != nil { if resp.status() != StatusCode::OK { let err_resp = http_resp_to_error_response(&resp, vec![], bucket_name, ""); match err_resp.code { S3ErrorCode::NotImplemented => { match err_resp.server.as_str() { "AmazonSnowball" => { return Ok("snowball".to_string()); } "cloudflare" => { return Ok("us-east-1".to_string()); } _ => { return Err(std::io::Error::other(err_resp)); } } } S3ErrorCode::AuthorizationHeaderMalformed | //S3ErrorCode::InvalidRegion | S3ErrorCode::AccessDenied => { if err_resp.region == "" { return Ok("us-east-1".to_string()); } return Ok(err_resp.region); } _ => { return Err(std::io::Error::other(err_resp)); } } } //} let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); let mut location = "".to_string(); if tier_type == "huaweicloud" { let d = quick_xml::de::from_str::<CreateBucketConfiguration>(&String::from_utf8(b).unwrap()).unwrap(); location = d.location_constraint; } else { if let Ok(LocationConstraint { field }) = quick_xml::de::from_str::<LocationConstraint>(&String::from_utf8(b).unwrap()) { location = field; } } //debug!("location: {}", location); if location == "" { location = "us-east-1".to_string(); } if location == "EU" { location = "eu-west-1".to_string(); } Ok(location) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_put_object_multipart.rs
crates/ecstore/src/client/api_put_object_multipart.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::{HeaderMap, HeaderName, StatusCode}; use s3s::S3ErrorCode; use std::collections::HashMap; use time::OffsetDateTime; use tracing::warn; use uuid::Uuid; use crate::client::checksum::ChecksumMode; use crate::client::utils::base64_encode; use crate::client::{ api_error_response::{ err_entity_too_large, err_entity_too_small, err_invalid_argument, http_resp_to_error_response, to_error_response, }, api_put_object::PutObjectOptions, api_put_object_common::optimal_part_info, api_s3_datatypes::{ CompleteMultipartUpload, CompleteMultipartUploadResult, CompletePart, InitiateMultipartUploadResult, ObjectPart, }, constants::{ISO8601_DATEFORMAT, MAX_PART_SIZE, MAX_SINGLE_PUT_OBJECT_SIZE}, transition_api::{ReaderImpl, RequestMetadata, TransitionClient, UploadInfo}, }; use rustfs_utils::path::trim_etag; use s3s::header::{X_AMZ_EXPIRATION, X_AMZ_VERSION_ID}; impl TransitionClient { pub async fn put_object_multipart( &self, bucket_name: &str, object_name: &str, mut reader: ReaderImpl, size: i64, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let info = self .put_object_multipart_no_stream(bucket_name, object_name, &mut reader, opts) .await; if let Err(err) = &info { let err_resp = to_error_response(err); if err_resp.code == S3ErrorCode::AccessDenied && err_resp.message.contains("Access Denied") { if size > MAX_SINGLE_PUT_OBJECT_SIZE { return Err(std::io::Error::other(err_entity_too_large( size, MAX_SINGLE_PUT_OBJECT_SIZE, bucket_name, object_name, ))); } return self.put_object_gcs(bucket_name, object_name, reader, size, opts).await; } } Ok(info?) } pub async fn put_object_multipart_no_stream( &self, bucket_name: &str, object_name: &str, reader: &mut ReaderImpl, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let mut total_uploaded_size: i64 = 0; let mut compl_multipart_upload = CompleteMultipartUpload::default(); let ret = optimal_part_info(-1, opts.part_size)?; let (total_parts_count, part_size, _) = ret; let (mut hash_algos, mut hash_sums) = self.hash_materials(opts.send_content_md5, !opts.disable_content_sha256); let upload_id = self.new_upload_id(bucket_name, object_name, opts).await?; let mut opts = opts.clone(); opts.user_metadata.remove("X-Amz-Checksum-Algorithm"); let mut part_number = 1; let mut parts_info = HashMap::<i64, ObjectPart>::new(); let mut buf = Vec::<u8>::with_capacity(part_size as usize); let mut custom_header = HeaderMap::new(); while part_number <= total_parts_count { match reader { ReaderImpl::Body(content_body) => { buf = content_body.to_vec(); } ReaderImpl::ObjectBody(content_body) => { buf = content_body.read_all().await?; } } let length = buf.len(); for (k, v) in hash_algos.iter_mut() { let hash = v.hash_encode(&buf[..length]); hash_sums.insert(k.to_string(), hash.as_ref().to_vec()); } //let rd = newHook(bytes.NewReader(buf[..length]), opts.progress); let rd = Bytes::from(buf.clone()); let md5_base64: String; let sha256_hex: String; //if hash_sums["md5"] != nil { md5_base64 = base64_encode(&hash_sums["md5"]); //} //if hash_sums["sha256"] != nil { sha256_hex = hex_simd::encode_to_string(hash_sums["sha256"].clone(), hex_simd::AsciiCase::Lower); //} if hash_sums.len() == 0 { let mut crc = opts.auto_checksum.hasher()?; crc.update(&buf[..length]); let csum = crc.finalize(); if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) { custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err")); } else { warn!("Invalid header name: {}", opts.auto_checksum.key()); } } let mut p = UploadPartParams { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), upload_id: upload_id.clone(), reader: ReaderImpl::Body(rd), part_number, md5_base64, sha256_hex, size: length as i64, //sse: opts.server_side_encryption, stream_sha256: !opts.disable_content_sha256, custom_header: custom_header.clone(), trailer: HeaderMap::new(), }; let obj_part = self.upload_part(&mut p).await?; parts_info.insert(part_number, obj_part); total_uploaded_size += length as i64; part_number += 1; } let mut all_parts = Vec::<ObjectPart>::with_capacity(parts_info.len()); for i in 1..part_number { let part = parts_info[&i].clone(); all_parts.push(part.clone()); compl_multipart_upload.parts.push(CompletePart { etag: part.etag, part_num: part.part_num, checksum_crc32: part.checksum_crc32, checksum_crc32c: part.checksum_crc32c, checksum_sha1: part.checksum_sha1, checksum_sha256: part.checksum_sha256, checksum_crc64nvme: part.checksum_crc64nvme, ..Default::default() }); } compl_multipart_upload.parts.sort(); let opts = PutObjectOptions { //server_side_encryption: opts.server_side_encryption, auto_checksum: opts.auto_checksum, ..Default::default() }; //apply_auto_checksum(&mut opts, all_parts); let mut upload_info = self .complete_multipart_upload(bucket_name, object_name, &upload_id, compl_multipart_upload, &opts) .await?; upload_info.size = total_uploaded_size; Ok(upload_info) } pub async fn initiate_multipart_upload( &self, bucket_name: &str, object_name: &str, opts: &PutObjectOptions, ) -> Result<InitiateMultipartUploadResult, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("uploads".to_string(), "".to_string()); if opts.internal.source_version_id != "" { if !opts.internal.source_version_id.is_empty() { if let Err(err) = Uuid::parse_str(&opts.internal.source_version_id) { return Err(std::io::Error::other(err_invalid_argument(&err.to_string()))); } } url_values.insert("versionId".to_string(), opts.internal.source_version_id.clone()); } let custom_header = opts.header(); let mut req_metadata = RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: url_values, custom_header, content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), content_sha256_hex: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }; let resp = self.execute_method(http::Method::POST, &mut req_metadata).await?; //if resp.is_none() { if resp.status() != StatusCode::OK { return Err(std::io::Error::other(http_resp_to_error_response( &resp, vec![], bucket_name, object_name, ))); } //} let initiate_multipart_upload_result = InitiateMultipartUploadResult::default(); Ok(initiate_multipart_upload_result) } pub async fn upload_part(&self, p: &mut UploadPartParams) -> Result<ObjectPart, std::io::Error> { if p.size > MAX_PART_SIZE { return Err(std::io::Error::other(err_entity_too_large( p.size, MAX_PART_SIZE, &p.bucket_name, &p.object_name, ))); } if p.size <= -1 { return Err(std::io::Error::other(err_entity_too_small(p.size, &p.bucket_name, &p.object_name))); } if p.part_number <= 0 { return Err(std::io::Error::other(err_invalid_argument( "Part number cannot be negative or equal to zero.", ))); } if p.upload_id == "" { return Err(std::io::Error::other(err_invalid_argument("UploadID cannot be empty."))); } let mut url_values = HashMap::new(); url_values.insert("partNumber".to_string(), p.part_number.to_string()); url_values.insert("uploadId".to_string(), p.upload_id.clone()); let buf = match &mut p.reader { ReaderImpl::Body(content_body) => content_body.to_vec(), ReaderImpl::ObjectBody(content_body) => content_body.read_all().await?, }; let mut req_metadata = RequestMetadata { bucket_name: p.bucket_name.clone(), object_name: p.object_name.clone(), query_values: url_values, custom_header: p.custom_header.clone(), content_body: ReaderImpl::Body(Bytes::from(buf)), content_length: p.size, content_md5_base64: p.md5_base64.clone(), content_sha256_hex: p.sha256_hex.clone(), stream_sha256: p.stream_sha256, trailer: p.trailer.clone(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }; let resp = self.execute_method(http::Method::PUT, &mut req_metadata).await?; if resp.status() != StatusCode::OK { return Err(std::io::Error::other(http_resp_to_error_response( &resp, vec![], &p.bucket_name.clone(), &p.object_name, ))); } //} let h = resp.headers(); let mut obj_part = ObjectPart { checksum_crc32: if let Some(h_checksum_crc32) = h.get(ChecksumMode::ChecksumCRC32.key()) { h_checksum_crc32.to_str().expect("err").to_string() } else { "".to_string() }, checksum_crc32c: if let Some(h_checksum_crc32c) = h.get(ChecksumMode::ChecksumCRC32C.key()) { h_checksum_crc32c.to_str().expect("err").to_string() } else { "".to_string() }, checksum_sha1: if let Some(h_checksum_sha1) = h.get(ChecksumMode::ChecksumSHA1.key()) { h_checksum_sha1.to_str().expect("err").to_string() } else { "".to_string() }, checksum_sha256: if let Some(h_checksum_sha256) = h.get(ChecksumMode::ChecksumSHA256.key()) { h_checksum_sha256.to_str().expect("err").to_string() } else { "".to_string() }, checksum_crc64nvme: if let Some(h_checksum_crc64nvme) = h.get(ChecksumMode::ChecksumCRC64NVME.key()) { h_checksum_crc64nvme.to_str().expect("err").to_string() } else { "".to_string() }, ..Default::default() }; obj_part.size = p.size; obj_part.part_num = p.part_number; obj_part.etag = if let Some(h_etag) = h.get("ETag") { h_etag.to_str().expect("err").trim_matches('"').to_string() } else { "".to_string() }; Ok(obj_part) } pub async fn complete_multipart_upload( &self, bucket_name: &str, object_name: &str, upload_id: &str, complete: CompleteMultipartUpload, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("uploadId".to_string(), upload_id.to_string()); let complete_multipart_upload_bytes = complete.marshal_msg()?.as_bytes().to_vec(); let headers = opts.header(); let complete_multipart_upload_buffer = Bytes::from(complete_multipart_upload_bytes); let mut req_metadata = RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: url_values, custom_header: headers, content_body: ReaderImpl::Body(complete_multipart_upload_buffer), content_length: 100, //complete_multipart_upload_bytes.len(), content_sha256_hex: "".to_string(), //hex_simd::encode_to_string(complete_multipart_upload_bytes, hex_simd::AsciiCase::Lower), content_md5_base64: "".to_string(), stream_sha256: Default::default(), trailer: Default::default(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }; let resp = self.execute_method(http::Method::POST, &mut req_metadata).await?; let b = resp.body().bytes().expect("err").to_vec(); let complete_multipart_upload_result: CompleteMultipartUploadResult = CompleteMultipartUploadResult::default(); let (exp_time, rule_id) = if let Some(h_x_amz_expiration) = resp.headers().get(X_AMZ_EXPIRATION) { ( OffsetDateTime::parse(h_x_amz_expiration.to_str().unwrap(), ISO8601_DATEFORMAT).unwrap(), "".to_string(), ) } else { (OffsetDateTime::now_utc(), "".to_string()) }; let h = resp.headers(); Ok(UploadInfo { bucket: complete_multipart_upload_result.bucket, key: complete_multipart_upload_result.key, etag: trim_etag(&complete_multipart_upload_result.etag), version_id: if let Some(h_x_amz_version_id) = h.get(X_AMZ_VERSION_ID) { h_x_amz_version_id.to_str().expect("err").to_string() } else { "".to_string() }, location: complete_multipart_upload_result.location, expiration: exp_time, expiration_rule_id: rule_id, checksum_sha256: complete_multipart_upload_result.checksum_sha256, checksum_sha1: complete_multipart_upload_result.checksum_sha1, checksum_crc32: complete_multipart_upload_result.checksum_crc32, checksum_crc32c: complete_multipart_upload_result.checksum_crc32c, checksum_crc64nvme: complete_multipart_upload_result.checksum_crc64nvme, ..Default::default() }) } } pub struct UploadPartParams { pub bucket_name: String, pub object_name: String, pub upload_id: String, pub reader: ReaderImpl, pub part_number: i64, pub md5_base64: String, pub sha256_hex: String, pub size: i64, //pub sse: encrypt.ServerSide, pub stream_sha256: bool, pub custom_header: HeaderMap, pub trailer: HeaderMap, }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/checksum.rs
crates/ecstore/src/client/checksum.rs
#![allow(clippy::map_entry)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use lazy_static::lazy_static; use rustfs_checksums::ChecksumAlgorithm; use std::collections::HashMap; use crate::client::utils::base64_decode; use crate::client::utils::base64_encode; use crate::client::{api_put_object::PutObjectOptions, api_s3_datatypes::ObjectPart}; use crate::{disk::DiskAPI, store_api::GetObjectReader}; use s3s::header::{ X_AMZ_CHECKSUM_ALGORITHM, X_AMZ_CHECKSUM_CRC32, X_AMZ_CHECKSUM_CRC32C, X_AMZ_CHECKSUM_SHA1, X_AMZ_CHECKSUM_SHA256, }; use enumset::{EnumSet, EnumSetType, enum_set}; #[derive(Debug, EnumSetType, Default)] #[enumset(repr = "u8")] pub enum ChecksumMode { #[default] ChecksumNone, ChecksumSHA256, ChecksumSHA1, ChecksumCRC32, ChecksumCRC32C, ChecksumCRC64NVME, ChecksumFullObject, } lazy_static! { static ref C_ChecksumMask: EnumSet<ChecksumMode> = { let mut s = EnumSet::all(); s.remove(ChecksumMode::ChecksumFullObject); s }; static ref C_ChecksumFullObjectCRC32: EnumSet<ChecksumMode> = enum_set!(ChecksumMode::ChecksumCRC32 | ChecksumMode::ChecksumFullObject); static ref C_ChecksumFullObjectCRC32C: EnumSet<ChecksumMode> = enum_set!(ChecksumMode::ChecksumCRC32C | ChecksumMode::ChecksumFullObject); } const AMZ_CHECKSUM_CRC64NVME: &str = "x-amz-checksum-crc64nvme"; impl ChecksumMode { //pub const CRC64_NVME_POLYNOMIAL: i64 = 0xad93d23594c93659; pub fn base(&self) -> ChecksumMode { let s = EnumSet::from(*self).intersection(*C_ChecksumMask); match s.as_u8() { 1_u8 => ChecksumMode::ChecksumNone, 2_u8 => ChecksumMode::ChecksumSHA256, 4_u8 => ChecksumMode::ChecksumSHA1, 8_u8 => ChecksumMode::ChecksumCRC32, 16_u8 => ChecksumMode::ChecksumCRC32C, 32_u8 => ChecksumMode::ChecksumCRC64NVME, _ => panic!("enum err."), } } pub fn is(&self, t: ChecksumMode) -> bool { *self & t == t } pub fn key(&self) -> String { //match c & checksumMask { match self { ChecksumMode::ChecksumCRC32 => { return X_AMZ_CHECKSUM_CRC32.to_string(); } ChecksumMode::ChecksumCRC32C => { return X_AMZ_CHECKSUM_CRC32C.to_string(); } ChecksumMode::ChecksumSHA1 => { return X_AMZ_CHECKSUM_SHA1.to_string(); } ChecksumMode::ChecksumSHA256 => { return X_AMZ_CHECKSUM_SHA256.to_string(); } ChecksumMode::ChecksumCRC64NVME => { return AMZ_CHECKSUM_CRC64NVME.to_string(); } _ => { return "".to_string(); } } } pub fn can_composite(&self) -> bool { let s = EnumSet::from(*self).intersection(*C_ChecksumMask); match s.as_u8() { 2_u8 => true, 4_u8 => true, 8_u8 => true, 16_u8 => true, _ => false, } } pub fn can_merge_crc(&self) -> bool { let s = EnumSet::from(*self).intersection(*C_ChecksumMask); match s.as_u8() { 8_u8 => true, 16_u8 => true, 32_u8 => true, _ => false, } } pub fn full_object_requested(&self) -> bool { let s = EnumSet::from(*self).intersection(*C_ChecksumMask); match s.as_u8() { //C_ChecksumFullObjectCRC32 as u8 => true, //C_ChecksumFullObjectCRC32C as u8 => true, 32_u8 => true, _ => false, } } pub fn key_capitalized(&self) -> String { self.key() } pub fn raw_byte_len(&self) -> usize { let u = EnumSet::from(*self).intersection(*C_ChecksumMask).as_u8(); if u == ChecksumMode::ChecksumCRC32 as u8 || u == ChecksumMode::ChecksumCRC32C as u8 { 4 } else if u == ChecksumMode::ChecksumSHA1 as u8 { use sha1::Digest; sha1::Sha1::output_size() as usize } else if u == ChecksumMode::ChecksumSHA256 as u8 { use sha2::Digest; sha2::Sha256::output_size() as usize } else if u == ChecksumMode::ChecksumCRC64NVME as u8 { 8 } else { 0 } } pub fn hasher(&self) -> Result<Box<dyn rustfs_checksums::http::HttpChecksum>, std::io::Error> { match /*C_ChecksumMask & **/self { ChecksumMode::ChecksumCRC32 => { return Ok(ChecksumAlgorithm::Crc32.into_impl()); } ChecksumMode::ChecksumCRC32C => { return Ok(ChecksumAlgorithm::Crc32c.into_impl()); } ChecksumMode::ChecksumSHA1 => { return Ok(ChecksumAlgorithm::Sha1.into_impl()); } ChecksumMode::ChecksumSHA256 => { return Ok(ChecksumAlgorithm::Sha256.into_impl()); } ChecksumMode::ChecksumCRC64NVME => { return Ok(ChecksumAlgorithm::Crc64Nvme.into_impl()); } _ => return Err(std::io::Error::other("unsupported checksum type")), } } pub fn is_set(&self) -> bool { let s = EnumSet::from(*self).intersection(*C_ChecksumMask); s.len() == 1 } pub fn set_default(&mut self, t: ChecksumMode) { if !self.is_set() { *self = t; } } pub fn encode_to_string(&self, b: &[u8]) -> Result<String, std::io::Error> { if !self.is_set() { return Ok("".to_string()); } let mut h = self.hasher()?; h.update(b); let hash = h.finalize(); Ok(base64_encode(hash.as_ref())) } pub fn to_string(&self) -> String { //match c & checksumMask { match self { ChecksumMode::ChecksumCRC32 => { return "CRC32".to_string(); } ChecksumMode::ChecksumCRC32C => { return "CRC32C".to_string(); } ChecksumMode::ChecksumSHA1 => { return "SHA1".to_string(); } ChecksumMode::ChecksumSHA256 => { return "SHA256".to_string(); } ChecksumMode::ChecksumNone => { return "".to_string(); } ChecksumMode::ChecksumCRC64NVME => { return "CRC64NVME".to_string(); } _ => { return "<invalid>".to_string(); } } } // pub fn check_sum_reader(&self, r: GetObjectReader) -> Result<Checksum, std::io::Error> { // let mut h = self.hasher()?; // Ok(Checksum::new(self.clone(), h.sum().as_bytes())) // } // pub fn check_sum_bytes(&self, b: &[u8]) -> Result<Checksum, std::io::Error> { // let mut h = self.hasher()?; // Ok(Checksum::new(self.clone(), h.sum().as_bytes())) // } pub fn composite_checksum(&self, p: &mut [ObjectPart]) -> Result<Checksum, std::io::Error> { if !self.can_composite() { return Err(std::io::Error::other("cannot do composite checksum")); } p.sort_by(|i, j| { if i.part_num < j.part_num { std::cmp::Ordering::Less } else if i.part_num > j.part_num { std::cmp::Ordering::Greater } else { std::cmp::Ordering::Equal } }); let c = self.base(); let crc_bytes = Vec::<u8>::with_capacity(p.len() * self.raw_byte_len() as usize); let mut h = self.hasher()?; h.update(crc_bytes.as_ref()); let hash = h.finalize(); Ok(Checksum { checksum_type: self.clone(), r: hash.as_ref().to_vec(), computed: false, }) } pub fn full_object_checksum(&self, p: &mut [ObjectPart]) -> Result<Checksum, std::io::Error> { todo!(); } } #[derive(Default)] pub struct Checksum { checksum_type: ChecksumMode, r: Vec<u8>, computed: bool, } #[allow(dead_code)] impl Checksum { fn new(t: ChecksumMode, b: &[u8]) -> Checksum { if t.is_set() && b.len() == t.raw_byte_len() { return Checksum { checksum_type: t, r: b.to_vec(), computed: false, }; } Checksum::default() } #[allow(dead_code)] fn new_checksum_string(t: ChecksumMode, s: &str) -> Result<Checksum, std::io::Error> { let b = match base64_decode(s.as_bytes()) { Ok(b) => b, Err(err) => return Err(std::io::Error::other(err.to_string())), }; if t.is_set() && b.len() == t.raw_byte_len() { return Ok(Checksum { checksum_type: t, r: b, computed: false, }); } Ok(Checksum::default()) } fn is_set(&self) -> bool { self.checksum_type.is_set() && self.r.len() == self.checksum_type.raw_byte_len() } fn encoded(&self) -> String { if !self.is_set() { return "".to_string(); } base64_encode(&self.r) } #[allow(dead_code)] fn raw(&self) -> Option<Vec<u8>> { if !self.is_set() { return None; } Some(self.r.clone()) } } pub fn add_auto_checksum_headers(opts: &mut PutObjectOptions) { opts.user_metadata .insert("X-Amz-Checksum-Algorithm".to_string(), opts.auto_checksum.to_string()); if opts.auto_checksum.full_object_requested() { opts.user_metadata .insert("X-Amz-Checksum-Type".to_string(), "FULL_OBJECT".to_string()); } } pub fn apply_auto_checksum(opts: &mut PutObjectOptions, all_parts: &mut [ObjectPart]) -> Result<(), std::io::Error> { if opts.auto_checksum.can_composite() && !opts.auto_checksum.is(ChecksumMode::ChecksumFullObject) { let crc = opts.auto_checksum.composite_checksum(all_parts)?; opts.user_metadata = { let mut hm = HashMap::new(); hm.insert(opts.auto_checksum.key(), crc.encoded()); hm } } else if opts.auto_checksum.can_merge_crc() { let crc = opts.auto_checksum.full_object_checksum(all_parts)?; opts.user_metadata = { let mut hm = HashMap::new(); hm.insert(opts.auto_checksum.key_capitalized(), crc.encoded()); hm.insert("X-Amz-Checksum-Type".to_string(), "FULL_OBJECT".to_string()); hm } } Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_stat.rs
crates/ecstore/src/client/api_stat.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::{HeaderMap, HeaderValue}; use rustfs_utils::EMPTY_STRING_SHA256_HASH; use std::{collections::HashMap, str::FromStr}; use tokio::io::BufReader; use tracing::warn; use uuid::Uuid; use crate::client::{ api_error_response::{ErrorResponse, err_invalid_argument, http_resp_to_error_response}, api_get_options::GetObjectOptions, transition_api::{ObjectInfo, ReadCloser, ReaderImpl, RequestMetadata, TransitionClient, to_object_info}, }; use s3s::{ dto::VersioningConfiguration, header::{X_AMZ_DELETE_MARKER, X_AMZ_VERSION_ID}, }; impl TransitionClient { pub async fn bucket_exists(&self, bucket_name: &str) -> Result<bool, std::io::Error> { let resp = self .execute_method( http::Method::HEAD, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: "".to_string(), query_values: HashMap::new(), custom_header: HeaderMap::new(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), content_md5_base64: "".to_string(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await; if let Ok(resp) = resp { if resp.status() != http::StatusCode::OK { return Ok(false); } let b = resp.body().bytes().expect("err").to_vec(); let resperr = http_resp_to_error_response(&resp, b, bucket_name, ""); warn!("bucket exists, resp: {:?}, resperr: {:?}", resp, resperr); /*if to_error_response(resperr).code == "NoSuchBucket" { return Ok(false); } if resp.status_code() != http::StatusCode::OK { return Ok(false); }*/ } Ok(true) } pub async fn get_bucket_versioning(&self, bucket_name: &str) -> Result<VersioningConfiguration, std::io::Error> { let mut query_values = HashMap::new(); query_values.insert("versioning".to_string(), "".to_string()); let resp = self .execute_method( http::Method::GET, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: "".to_string(), query_values, custom_header: HeaderMap::new(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), content_md5_base64: "".to_string(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await; match resp { Ok(resp) => { let b = resp.body().bytes().expect("get bucket versioning err").to_vec(); let resperr = http_resp_to_error_response(&resp, b, bucket_name, ""); warn!("get bucket versioning, resp: {:?}, resperr: {:?}", resp, resperr); Ok(VersioningConfiguration::default()) } Err(err) => Err(std::io::Error::other(err)), } } pub async fn stat_object( &self, bucket_name: &str, object_name: &str, opts: &GetObjectOptions, ) -> Result<ObjectInfo, std::io::Error> { let mut headers = opts.header(); if opts.internal.replication_delete_marker { headers.insert("X-Source-DeleteMarker", HeaderValue::from_str("true").unwrap()); } if opts.internal.is_replication_ready_for_delete_marker { headers.insert("X-Check-Replication-Ready", HeaderValue::from_str("true").unwrap()); } let resp = self .execute_method( http::Method::HEAD, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: opts.to_query_values(), custom_header: headers, content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), content_md5_base64: "".to_string(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await; match resp { Ok(resp) => { let h = resp.headers(); let delete_marker = if let Some(x_amz_delete_marker) = h.get(X_AMZ_DELETE_MARKER.as_str()) { x_amz_delete_marker.to_str().unwrap() == "true" } else { false }; let replication_ready = if let Some(x_amz_delete_marker) = h.get("X-Replication-Ready") { x_amz_delete_marker.to_str().unwrap() == "true" } else { false }; if resp.status() != http::StatusCode::OK && resp.status() != http::StatusCode::PARTIAL_CONTENT { if resp.status() == http::StatusCode::METHOD_NOT_ALLOWED && opts.version_id != "" && delete_marker { let err_resp = ErrorResponse { status_code: resp.status(), code: s3s::S3ErrorCode::MethodNotAllowed, message: "the specified method is not allowed against this resource.".to_string(), bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() }; return Ok(ObjectInfo { version_id: h .get(X_AMZ_VERSION_ID) .and_then(|v| v.to_str().ok()) .and_then(|s| Uuid::from_str(s).ok()), is_delete_marker: delete_marker, ..Default::default() }); //err_resp } return Ok(ObjectInfo { version_id: h .get(X_AMZ_VERSION_ID) .and_then(|v| v.to_str().ok()) .and_then(|s| Uuid::from_str(s).ok()), is_delete_marker: delete_marker, replication_ready: replication_ready, ..Default::default() }); //http_resp_to_error_response(resp, bucket_name, object_name) } Ok(to_object_info(bucket_name, object_name, h).unwrap()) } Err(err) => { return Err(std::io::Error::other(err)); } } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/utils.rs
crates/ecstore/src/client/utils.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use lazy_static::lazy_static; use std::collections::HashMap; use s3s::header::X_AMZ_STORAGE_CLASS; lazy_static! { static ref SUPPORTED_QUERY_VALUES: HashMap<String, bool> = { let mut m = HashMap::new(); m.insert("attributes".to_string(), true); m.insert("partNumber".to_string(), true); m.insert("versionId".to_string(), true); m.insert("response-cache-control".to_string(), true); m.insert("response-content-disposition".to_string(), true); m.insert("response-content-encoding".to_string(), true); m.insert("response-content-language".to_string(), true); m.insert("response-content-type".to_string(), true); m.insert("response-expires".to_string(), true); m }; static ref SUPPORTED_HEADERS: HashMap<String, bool> = { let mut m = HashMap::new(); m.insert("content-type".to_string(), true); m.insert("cache-control".to_string(), true); m.insert("content-encoding".to_string(), true); m.insert("content-disposition".to_string(), true); m.insert("content-language".to_string(), true); m.insert("x-amz-website-redirect-location".to_string(), true); m.insert("x-amz-object-lock-mode".to_string(), true); m.insert("x-amz-metadata-directive".to_string(), true); m.insert("x-amz-object-lock-retain-until-date".to_string(), true); m.insert("expires".to_string(), true); m.insert("x-amz-replication-status".to_string(), true); m }; static ref SSE_HEADERS: HashMap<String, bool> = { let mut m = HashMap::new(); m.insert("x-amz-server-side-encryption".to_string(), true); m.insert("x-amz-server-side-encryption-aws-kms-key-id".to_string(), true); m.insert("x-amz-server-side-encryption-context".to_string(), true); m.insert("x-amz-server-side-encryption-customer-algorithm".to_string(), true); m.insert("x-amz-server-side-encryption-customer-key".to_string(), true); m.insert("x-amz-server-side-encryption-customer-key-md5".to_string(), true); m }; } pub fn is_standard_query_value(qs_key: &str) -> bool { SUPPORTED_QUERY_VALUES[qs_key] } pub fn is_storageclass_header(header_key: &str) -> bool { header_key.to_lowercase() == X_AMZ_STORAGE_CLASS.as_str().to_lowercase() } pub fn is_standard_header(header_key: &str) -> bool { *SUPPORTED_HEADERS.get(&header_key.to_lowercase()).unwrap_or(&false) } pub fn is_sse_header(header_key: &str) -> bool { *SSE_HEADERS.get(&header_key.to_lowercase()).unwrap_or(&false) } pub fn is_amz_header(header_key: &str) -> bool { let key = header_key.to_lowercase(); key.starts_with("x-amz-meta-") || key.starts_with("x-amz-grant-") || key == "x-amz-acl" || is_sse_header(header_key) || key.starts_with("x-amz-checksum-") } pub fn is_rustfs_header(header_key: &str) -> bool { header_key.to_lowercase().starts_with("x-rustfs-") } pub fn is_minio_header(header_key: &str) -> bool { header_key.to_lowercase().starts_with("x-minio-") } pub fn base64_encode(input: &[u8]) -> String { base64_simd::URL_SAFE_NO_PAD.encode_to_string(input) } pub fn base64_decode(input: &[u8]) -> Result<Vec<u8>, base64_simd::Error> { base64_simd::URL_SAFE_NO_PAD.decode_to_vec(input) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/mod.rs
crates/ecstore/src/client/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub mod admin_handler_utils; pub mod api_bucket_policy; pub mod api_error_response; pub mod api_get_object; pub mod api_get_object_acl; pub mod api_get_object_attributes; pub mod api_get_object_file; pub mod api_get_options; pub mod api_list; pub mod api_put_object; pub mod api_put_object_common; pub mod api_put_object_multipart; pub mod api_put_object_streaming; pub mod api_remove; pub mod api_restore; pub mod api_s3_datatypes; pub mod api_stat; pub mod bucket_cache; pub mod checksum; pub mod constants; pub mod credentials; pub mod object_api_utils; pub mod object_handlers_common; pub mod transition_api; pub mod utils;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_restore.rs
crates/ecstore/src/client/api_restore.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::client::{ api_error_response::{err_invalid_argument, http_resp_to_error_response}, api_get_object_acl::AccessControlList, api_get_options::GetObjectOptions, transition_api::{ObjectInfo, ReadCloser, ReaderImpl, RequestMetadata, TransitionClient, to_object_info}, }; use bytes::Bytes; use http::HeaderMap; use s3s::dto::RestoreRequest; use std::collections::HashMap; use std::io::Cursor; use tokio::io::BufReader; const TIER_STANDARD: &str = "Standard"; const TIER_BULK: &str = "Bulk"; const TIER_EXPEDITED: &str = "Expedited"; #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Encryption { pub encryption_type: String, pub kms_context: String, pub kms_key_id: String, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct MetadataEntry { pub name: String, pub value: String, } #[derive(Debug, Default, serde::Serialize)] pub struct S3 { pub access_control_list: AccessControlList, pub bucket_name: String, pub prefix: String, pub canned_acl: String, pub encryption: Encryption, pub storage_class: String, //tagging: Tags, pub user_metadata: MetadataEntry, } impl TransitionClient { pub async fn restore_object( &self, bucket_name: &str, object_name: &str, version_id: &str, restore_req: &RestoreRequest, ) -> Result<(), std::io::Error> { /*let restore_request = match quick_xml::se::to_string(restore_req) { Ok(buf) => buf, Err(e) => { return Err(std::io::Error::other(e)); } };*/ let restore_request = "".to_string(); let restore_request_bytes = restore_request.as_bytes().to_vec(); let mut url_values = HashMap::new(); url_values.insert("restore".to_string(), "".to_string()); if version_id != "" { url_values.insert("versionId".to_string(), version_id.to_string()); } let restore_request_buffer = Bytes::from(restore_request_bytes.clone()); let resp = self .execute_method( http::Method::HEAD, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: url_values, custom_header: HeaderMap::new(), content_sha256_hex: "".to_string(), //sum_sha256_hex(&restore_request_bytes), content_md5_base64: "".to_string(), //sum_md5_base64(&restore_request_bytes), content_body: ReaderImpl::Body(restore_request_buffer), content_length: restore_request_bytes.len() as i64, stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; let b = resp.body().bytes().expect("err").to_vec(); if resp.status() != http::StatusCode::ACCEPTED && resp.status() != http::StatusCode::OK { return Err(std::io::Error::other(http_resp_to_error_response(&resp, b, bucket_name, ""))); } Ok(()) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/credentials.rs
crates/ecstore/src/client/credentials.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use std::fmt::{Display, Formatter}; use time::OffsetDateTime; #[derive(Debug, Default, Clone, Eq, PartialEq)] pub enum SignatureType { #[default] SignatureDefault, SignatureV4, SignatureV2, SignatureV4Streaming, SignatureAnonymous, } #[derive(Debug, Clone, Default)] pub struct Credentials<P: Provider + Default> { creds: Value, force_refresh: bool, provider: P, } impl<P: Provider + Default> Credentials<P> { pub fn new(provider: P) -> Self { Self { provider, force_refresh: true, ..Default::default() } } pub fn get(&mut self) -> Result<Value, std::io::Error> { self.get_with_context(None) } pub fn get_with_context(&mut self, mut cc: Option<CredContext>) -> Result<Value, std::io::Error> { if self.is_expired() { let creds = self.provider.retrieve_with_cred_context(cc.expect("err")); self.creds = creds; self.force_refresh = false; } Ok(self.creds.clone()) } fn expire(&mut self) { self.force_refresh = true; } pub fn is_expired(&self) -> bool { self.force_refresh || self.provider.is_expired() } } #[derive(Debug, Clone)] pub struct Value { pub access_key_id: String, pub secret_access_key: String, pub session_token: String, pub expiration: OffsetDateTime, pub signer_type: SignatureType, } impl Default for Value { fn default() -> Self { Self { access_key_id: "".to_string(), secret_access_key: "".to_string(), session_token: "".to_string(), expiration: OffsetDateTime::now_utc(), signer_type: SignatureType::SignatureDefault, } } } pub struct CredContext { //pub client: SendRequest, pub endpoint: String, } pub trait Provider { fn retrieve(&self) -> Value; fn retrieve_with_cred_context(&self, _: CredContext) -> Value; fn is_expired(&self) -> bool; } #[derive(Debug, Clone, Default)] pub struct Static(pub Value); impl Provider for Static { fn retrieve(&self) -> Value { if self.0.access_key_id == "" || self.0.secret_access_key == "" { return Value { signer_type: SignatureType::SignatureAnonymous, ..Default::default() }; } self.0.clone() } fn retrieve_with_cred_context(&self, _: CredContext) -> Value { self.retrieve() } fn is_expired(&self) -> bool { false } } #[derive(Debug, Clone, Default)] pub struct STSError { pub r#type: String, pub code: String, pub message: String, } #[derive(Debug, Clone, thiserror::Error)] pub struct ErrorResponse { pub sts_error: STSError, pub request_id: String, } impl Display for ErrorResponse { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.error()) } } impl ErrorResponse { fn error(&self) -> String { if self.sts_error.message == "" { return format!("Error response code {}.", self.sts_error.code); } return self.sts_error.message.clone(); } } pub fn xml_decoder<T>(body: &[u8]) -> Result<T, std::io::Error> { todo!(); } pub fn xml_decode_and_body<T>(body_reader: &[u8]) -> Result<(Vec<u8>, T), std::io::Error> { todo!(); }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/transition_api.rs
crates/ecstore/src/client/transition_api.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use crate::client::bucket_cache::BucketLocationCache; use crate::client::{ api_error_response::{err_invalid_argument, http_resp_to_error_response, to_error_response}, api_get_options::GetObjectOptions, api_put_object::PutObjectOptions, api_put_object_multipart::UploadPartParams, api_s3_datatypes::{ CompleteMultipartUpload, CompletePart, ListBucketResult, ListBucketV2Result, ListMultipartUploadsResult, ListObjectPartsResult, ObjectPart, }, constants::{UNSIGNED_PAYLOAD, UNSIGNED_PAYLOAD_TRAILER}, credentials::{CredContext, Credentials, SignatureType, Static}, }; use crate::{client::checksum::ChecksumMode, store_api::GetObjectReader}; use bytes::Bytes; use futures::{Future, StreamExt}; use http::{HeaderMap, HeaderName}; use http::{ HeaderValue, Response, StatusCode, request::{Builder, Request}, }; use hyper_rustls::{ConfigBuilderExt, HttpsConnector}; use hyper_util::{client::legacy::Client, client::legacy::connect::HttpConnector, rt::TokioExecutor}; use md5::Digest; use md5::Md5; use rand::Rng; use rustfs_config::MAX_S3_CLIENT_RESPONSE_SIZE; use rustfs_rio::HashReader; use rustfs_utils::HashAlgorithm; use rustfs_utils::{ net::get_endpoint_url, retry::{ DEFAULT_RETRY_CAP, DEFAULT_RETRY_UNIT, MAX_JITTER, MAX_RETRY, RetryTimer, is_http_status_retryable, is_s3code_retryable, }, }; use s3s::S3ErrorCode; use s3s::dto::ReplicationStatus; use s3s::{Body, dto::Owner}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::io::Cursor; use std::pin::Pin; use std::sync::atomic::{AtomicI32, Ordering}; use std::task::{Context, Poll}; use std::{ collections::HashMap, sync::{Arc, Mutex}, }; use time::Duration; use time::OffsetDateTime; use tokio::io::BufReader; use tracing::{debug, error, warn}; use url::{Url, form_urlencoded}; use uuid::Uuid; const C_USER_AGENT: &str = "RustFS (linux; x86)"; const SUCCESS_STATUS: [StatusCode; 3] = [StatusCode::OK, StatusCode::NO_CONTENT, StatusCode::PARTIAL_CONTENT]; const C_UNKNOWN: i32 = -1; const C_OFFLINE: i32 = 0; const C_ONLINE: i32 = 1; //pub type ReaderImpl = Box<dyn Reader + Send + Sync + 'static>; pub enum ReaderImpl { Body(Bytes), ObjectBody(GetObjectReader), } pub type ReadCloser = BufReader<Cursor<Vec<u8>>>; #[derive(Debug)] pub struct TransitionClient { pub endpoint_url: Url, pub creds_provider: Arc<Mutex<Credentials<Static>>>, pub override_signer_type: SignatureType, pub secure: bool, pub http_client: Client<HttpsConnector<HttpConnector>, Body>, pub bucket_loc_cache: Arc<Mutex<BucketLocationCache>>, pub is_trace_enabled: Arc<Mutex<bool>>, pub trace_errors_only: Arc<Mutex<bool>>, pub s3_accelerate_endpoint: Arc<Mutex<String>>, pub s3_dual_stack_enabled: Arc<Mutex<bool>>, pub region: String, pub random: u64, pub lookup: BucketLookupType, pub md5_hasher: Arc<Mutex<Option<HashAlgorithm>>>, pub sha256_hasher: Option<HashAlgorithm>, pub health_status: AtomicI32, pub trailing_header_support: bool, pub max_retries: i64, pub tier_type: String, } #[derive(Debug, Default)] pub struct Options { pub creds: Credentials<Static>, pub secure: bool, pub region: String, pub bucket_lookup: BucketLookupType, pub trailing_headers: bool, pub custom_md5: Option<HashAlgorithm>, pub custom_sha256: Option<HashAlgorithm>, pub max_retries: i64, } #[derive(Clone, Debug, Default, PartialEq, Eq)] pub enum BucketLookupType { #[default] BucketLookupAuto, BucketLookupDNS, BucketLookupPath, } fn load_root_store_from_tls_path() -> Option<rustls::RootCertStore> { // Load the root certificate bundle from the path specified by the // RUSTFS_TLS_PATH environment variable. let tp = std::env::var("RUSTFS_TLS_PATH").ok()?; let ca = std::path::Path::new(&tp).join(rustfs_config::RUSTFS_CA_CERT); if !ca.exists() { return None; } let der_list = rustfs_utils::load_cert_bundle_der_bytes(ca.to_str().unwrap_or_default()).ok()?; let mut store = rustls::RootCertStore::empty(); for der in der_list { if let Err(e) = store.add(der.into()) { warn!("Warning: failed to add certificate from '{}' to root store: {e}", ca.display()); } } Some(store) } impl TransitionClient { pub async fn new(endpoint: &str, opts: Options, tier_type: &str) -> Result<TransitionClient, std::io::Error> { let clnt = Self::private_new(endpoint, opts, tier_type).await?; Ok(clnt) } async fn private_new(endpoint: &str, opts: Options, tier_type: &str) -> Result<TransitionClient, std::io::Error> { let endpoint_url = get_endpoint_url(endpoint, opts.secure)?; let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); let scheme = endpoint_url.scheme(); let client; let tls = if let Some(store) = load_root_store_from_tls_path() { rustls::ClientConfig::builder() .with_root_certificates(store) .with_no_client_auth() } else { rustls::ClientConfig::builder().with_native_roots()?.with_no_client_auth() }; let https = hyper_rustls::HttpsConnectorBuilder::new() .with_tls_config(tls) .https_or_http() .enable_http1() .enable_http2() .build(); client = Client::builder(TokioExecutor::new()).build(https); let mut clnt = TransitionClient { endpoint_url, creds_provider: Arc::new(Mutex::new(opts.creds)), override_signer_type: SignatureType::SignatureDefault, secure: opts.secure, http_client: client, bucket_loc_cache: Arc::new(Mutex::new(BucketLocationCache::new())), is_trace_enabled: Arc::new(Mutex::new(false)), trace_errors_only: Arc::new(Mutex::new(false)), s3_accelerate_endpoint: Arc::new(Mutex::new("".to_string())), s3_dual_stack_enabled: Arc::new(Mutex::new(false)), region: opts.region, random: rand::rng().random_range(10..=50), lookup: opts.bucket_lookup, md5_hasher: Arc::new(Mutex::new(opts.custom_md5)), sha256_hasher: opts.custom_sha256, health_status: AtomicI32::new(C_UNKNOWN), trailing_header_support: opts.trailing_headers, max_retries: opts.max_retries, tier_type: tier_type.to_string(), }; { let mut md5_hasher = clnt.md5_hasher.lock().unwrap(); if md5_hasher.is_none() { *md5_hasher = Some(HashAlgorithm::Md5); } } if clnt.sha256_hasher.is_none() { clnt.sha256_hasher = Some(HashAlgorithm::SHA256); } clnt.trailing_header_support = opts.trailing_headers && clnt.override_signer_type == SignatureType::SignatureV4; clnt.max_retries = MAX_RETRY; if opts.max_retries > 0 { clnt.max_retries = opts.max_retries; } Ok(clnt) } fn endpoint_url(&self) -> Url { self.endpoint_url.clone() } fn trace_errors_only_off(&self) { let mut trace_errors_only = self.trace_errors_only.lock().unwrap(); *trace_errors_only = false; } fn trace_off(&self) { let mut is_trace_enabled = self.is_trace_enabled.lock().unwrap(); *is_trace_enabled = false; let mut trace_errors_only = self.trace_errors_only.lock().unwrap(); *trace_errors_only = false; } fn set_s3_transfer_accelerate(&self, accelerate_endpoint: &str) { todo!(); } fn set_s3_enable_dual_stack(&self, enabled: bool) { todo!(); } pub fn hash_materials( &self, is_md5_requested: bool, is_sha256_requested: bool, ) -> (HashMap<String, HashAlgorithm>, HashMap<String, Vec<u8>>) { todo!() } fn is_online(&self) -> bool { !self.is_offline() } fn mark_offline(&self) { self.health_status .compare_exchange(C_ONLINE, C_OFFLINE, Ordering::SeqCst, Ordering::SeqCst); } fn is_offline(&self) -> bool { self.health_status.load(Ordering::SeqCst) == C_OFFLINE } fn health_check(hc_duration: Duration) { todo!(); } fn dump_http(&self, req: &http::Request<Body>, resp: &http::Response<Body>) -> Result<(), std::io::Error> { let mut resp_trace: Vec<u8>; //info!("{}{}", self.trace_output, "---------BEGIN-HTTP---------"); //info!("{}{}", self.trace_output, "---------END-HTTP---------"); Ok(()) } pub async fn doit(&self, req: http::Request<Body>) -> Result<http::Response<Body>, std::io::Error> { let req_method; let req_uri; let req_headers; let resp; let http_client = self.http_client.clone(); { //let mut http_client = http_client.lock().unwrap(); req_method = req.method().clone(); req_uri = req.uri().clone(); req_headers = req.headers().clone(); debug!("endpoint_url: {}", self.endpoint_url.as_str().to_string()); resp = http_client.request(req); } let resp = resp .await /*.map_err(Into::into)*/ .map(|res| res.map(Body::from)); debug!("http_client url: {} {}", req_method, req_uri); debug!("http_client headers: {:?}", req_headers); if let Err(err) = resp { error!("http_client call error: {:?}", err); return Err(std::io::Error::other(err)); } let mut resp = resp.unwrap(); debug!("http_resp: {:?}", resp); //let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec(); //debug!("http_resp_body: {}", String::from_utf8(b).unwrap()); //if self.is_trace_enabled && !(self.trace_errors_only && resp.status() == StatusCode::OK) { if resp.status() != StatusCode::OK { //self.dump_http(&cloned_req, &resp)?; let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); warn!("err_body: {}", String::from_utf8(b).unwrap()); } Ok(resp) } pub async fn execute_method( &self, method: http::Method, metadata: &mut RequestMetadata, ) -> Result<http::Response<Body>, std::io::Error> { if self.is_offline() { let mut s = self.endpoint_url.to_string(); s.push_str(" is offline."); return Err(std::io::Error::other(s)); } let retryable: bool; //let mut body_seeker: BufferReader; let mut req_retry = self.max_retries; let mut resp: http::Response<Body>; //if metadata.content_body != nil { //body_seeker = BufferReader::new(metadata.content_body.read_all().await?); retryable = true; if !retryable { req_retry = 1; } //} let mut retry_timer = RetryTimer::new(req_retry, DEFAULT_RETRY_UNIT, DEFAULT_RETRY_CAP, MAX_JITTER, self.random); while retry_timer.next().await.is_some() { let req = self.new_request(&method, metadata).await?; resp = self.doit(req).await?; for http_status in SUCCESS_STATUS { if http_status == resp.status() { return Ok(resp); } } let b = resp .body_mut() .store_all_limited(MAX_S3_CLIENT_RESPONSE_SIZE) .await .unwrap() .to_vec(); let mut err_response = http_resp_to_error_response(&resp, b.clone(), &metadata.bucket_name, &metadata.object_name); err_response.message = format!("remote tier error: {}", err_response.message); if self.region == "" { match err_response.code { S3ErrorCode::AuthorizationHeaderMalformed | S3ErrorCode::InvalidArgument /*S3ErrorCode::InvalidRegion*/ => { //break; return Err(std::io::Error::other(err_response)); } S3ErrorCode::AccessDenied => { if err_response.region == "" { return Err(std::io::Error::other(err_response)); } if metadata.bucket_name != "" { let mut bucket_loc_cache = self.bucket_loc_cache.lock().unwrap(); let location = bucket_loc_cache.get(&metadata.bucket_name); if location.is_some() && location.unwrap() != err_response.region { bucket_loc_cache.set(&metadata.bucket_name, &err_response.region); //continue; } } else if err_response.region != metadata.bucket_location { metadata.bucket_location = err_response.region.clone(); //continue; } return Err(std::io::Error::other(err_response)); } _ => { return Err(std::io::Error::other(err_response)); } } } if is_s3code_retryable(err_response.code.as_str()) { continue; } if is_http_status_retryable(&resp.status()) { continue; } break; } Err(std::io::Error::other("resp err")) } async fn new_request( &self, method: &http::Method, metadata: &mut RequestMetadata, ) -> Result<http::Request<Body>, std::io::Error> { let mut location = metadata.bucket_location.clone(); if location == "" && metadata.bucket_name != "" { location = self.get_bucket_location(&metadata.bucket_name).await?; } let is_makebucket = metadata.object_name == "" && method == http::Method::PUT && metadata.query_values.len() == 0; let is_virtual_host = self.is_virtual_host_style_request(&self.endpoint_url, &metadata.bucket_name) && !is_makebucket; let target_url = self.make_target_url( &metadata.bucket_name, &metadata.object_name, &location, is_virtual_host, &metadata.query_values, )?; let Ok(mut req) = Request::builder() .method(method) .uri(target_url.to_string()) .body(Body::empty()) else { return Err(std::io::Error::other("create request error")); }; let value; { let mut creds_provider = self.creds_provider.lock().unwrap(); value = creds_provider.get_with_context(Some(self.cred_context()))?; } let mut signer_type = value.signer_type.clone(); let access_key_id = value.access_key_id; let secret_access_key = value.secret_access_key; let session_token = value.session_token; if self.override_signer_type != SignatureType::SignatureDefault { signer_type = self.override_signer_type.clone(); } if value.signer_type == SignatureType::SignatureAnonymous { signer_type = SignatureType::SignatureAnonymous; } if metadata.expires != 0 && metadata.pre_sign_url { if signer_type == SignatureType::SignatureAnonymous { return Err(std::io::Error::other(err_invalid_argument( "presigned urls cannot be generated with anonymous credentials.", ))); } if metadata.extra_pre_sign_header.is_some() { if signer_type == SignatureType::SignatureV2 { return Err(std::io::Error::other(err_invalid_argument( "extra signed headers for presign with signature v2 is not supported.", ))); } let headers = req.headers_mut(); for (k, v) in metadata.extra_pre_sign_header.as_ref().unwrap() { headers.insert(k, v.clone()); } } if signer_type == SignatureType::SignatureV2 { req = rustfs_signer::pre_sign_v2(req, &access_key_id, &secret_access_key, metadata.expires, is_virtual_host); } else if signer_type == SignatureType::SignatureV4 { req = rustfs_signer::pre_sign_v4( req, &access_key_id, &secret_access_key, &session_token, &location, metadata.expires, OffsetDateTime::now_utc(), ); } return Ok(req); } self.set_user_agent(&mut req); for (k, v) in metadata.custom_header.clone() { req.headers_mut().insert(k.expect("err"), v); } //req.content_length = metadata.content_length; if metadata.content_length <= -1 { let chunked_value = HeaderValue::from_str(&vec!["chunked"].join(",")).expect("err"); req.headers_mut().insert(http::header::TRANSFER_ENCODING, chunked_value); } if metadata.content_md5_base64.len() > 0 { let md5_value = HeaderValue::from_str(&metadata.content_md5_base64).expect("err"); req.headers_mut().insert("Content-Md5", md5_value); } if signer_type == SignatureType::SignatureAnonymous { return Ok(req); } if signer_type == SignatureType::SignatureV2 { req = rustfs_signer::sign_v2(req, metadata.content_length, &access_key_id, &secret_access_key, is_virtual_host); } else if metadata.stream_sha256 && !self.secure { if metadata.trailer.len() > 0 { for (_, v) in &metadata.trailer { req.headers_mut().insert(http::header::TRAILER, v.clone()); } } } else { let mut sha_header = UNSIGNED_PAYLOAD.to_string(); if metadata.content_sha256_hex != "" { sha_header = metadata.content_sha256_hex.clone(); if metadata.trailer.len() > 0 { return Err(std::io::Error::other("internal error: content_sha256_hex with trailer not supported")); } } else if metadata.trailer.len() > 0 { sha_header = UNSIGNED_PAYLOAD_TRAILER.to_string(); } req.headers_mut() .insert("X-Amz-Content-Sha256".parse::<HeaderName>().unwrap(), sha_header.parse().expect("err")); req = rustfs_signer::sign_v4_trailer( req, &access_key_id, &secret_access_key, &session_token, &location, metadata.trailer.clone(), ); } if metadata.content_length > 0 { match &mut metadata.content_body { ReaderImpl::Body(content_body) => { *req.body_mut() = Body::from(content_body.clone()); } ReaderImpl::ObjectBody(content_body) => { *req.body_mut() = Body::from(content_body.read_all().await?); } } } Ok(req) } pub fn set_user_agent(&self, req: &mut Request<Body>) { let headers = req.headers_mut(); headers.insert("User-Agent", C_USER_AGENT.parse().expect("err")); } fn make_target_url( &self, bucket_name: &str, object_name: &str, bucket_location: &str, is_virtual_host_style: bool, query_values: &HashMap<String, String>, ) -> Result<Url, std::io::Error> { let scheme = self.endpoint_url.scheme(); let host = self.endpoint_url.host().unwrap(); let default_port = if scheme == "https" { 443 } else { 80 }; let port = self.endpoint_url.port().unwrap_or(default_port); let mut url_str = format!("{scheme}://{host}:{port}/"); if bucket_name != "" { if is_virtual_host_style { url_str = format!("{scheme}://{bucket_name}.{host}:{port}/"); if object_name != "" { url_str.push_str(object_name); } } else { url_str.push_str(bucket_name); url_str.push_str("/"); if object_name != "" { url_str.push_str(object_name); } } } if query_values.len() > 0 { let mut encoded = form_urlencoded::Serializer::new(String::new()); for (k, v) in query_values { encoded.append_pair(&k, &v); } url_str.push_str("?"); url_str.push_str(&encoded.finish()); } Url::parse(&url_str).map_err(|e| std::io::Error::other(e.to_string())) } pub fn is_virtual_host_style_request(&self, url: &Url, bucket_name: &str) -> bool { // Contract: // - return true if we should use virtual-hosted-style addressing (bucket as subdomain) // Heuristics (aligned with AWS S3/MinIO clients): // - explicit DNS mode => true // - explicit PATH mode => false // - AUTO: // - bucket must be non-empty and DNS compatible // - endpoint host must be a DNS name (not an IPv4/IPv6 literal) // - when using TLS (https), buckets with dots are avoided due to wildcard/cert issues if bucket_name.is_empty() { return false; } if self.lookup == BucketLookupType::BucketLookupDNS { return true; } if self.lookup == BucketLookupType::BucketLookupPath { return false; } false } pub fn cred_context(&self) -> CredContext { CredContext { //client: http_client, endpoint: self.endpoint_url.to_string(), } } } pub struct RequestMetadata { pub pre_sign_url: bool, pub bucket_name: String, pub object_name: String, pub query_values: HashMap<String, String>, pub custom_header: HeaderMap, pub extra_pre_sign_header: Option<HeaderMap>, pub expires: i64, pub bucket_location: String, pub content_body: ReaderImpl, pub content_length: i64, pub content_md5_base64: String, pub content_sha256_hex: String, pub stream_sha256: bool, pub add_crc: ChecksumMode, pub trailer: HeaderMap, } pub struct TransitionCore(pub Arc<TransitionClient>); impl TransitionCore { pub async fn new(endpoint: &str, opts: Options) -> Result<Self, std::io::Error> { let client = TransitionClient::new(endpoint, opts, "").await?; Ok(Self(Arc::new(client))) } pub fn list_objects( &self, bucket: &str, prefix: &str, marker: &str, delimiter: &str, max_keys: i64, ) -> Result<ListBucketResult, std::io::Error> { let client = self.0.clone(); client.list_objects_query(bucket, prefix, marker, delimiter, max_keys, HeaderMap::new()) } pub async fn list_objects_v2( &self, bucket_name: &str, object_prefix: &str, start_after: &str, continuation_token: &str, delimiter: &str, max_keys: i64, ) -> Result<ListBucketV2Result, std::io::Error> { let client = self.0.clone(); client .list_objects_v2_query( bucket_name, object_prefix, continuation_token, true, false, delimiter, start_after, max_keys, HeaderMap::new(), ) .await } /*pub fn copy_object(&self, source_bucket: &str, source_object: &str, dest_bucket: &str, dest_object: &str, metadata: HashMap<String, String>, src_opts: CopySrcOptions, dst_opts: PutObjectOptions) -> Result<ObjectInfo> { self.0.copy_object_do(source_bucket, source_object, dest_bucket, dest_object, metadata, src_opts, dst_opts) }*/ pub fn copy_object_part( &self, src_bucket: &str, src_object: &str, dest_bucket: &str, dest_object: &str, upload_id: &str, part_id: i32, start_offset: i32, length: i64, metadata: HashMap<String, String>, ) -> Result<CompletePart, std::io::Error> { //self.0.copy_object_part_do(src_bucket, src_object, dest_bucket, dest_object, upload_id, // part_id, start_offset, length, metadata) todo!(); } pub async fn put_object( &self, bucket: &str, object: &str, data: ReaderImpl, size: i64, md5_base64: &str, sha256_hex: &str, opts: &PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let hook_reader = data; //newHook(data, opts.progress); let client = self.0.clone(); client .put_object_do(bucket, object, hook_reader, md5_base64, sha256_hex, size, opts) .await } pub async fn new_multipart_upload( &self, bucket: &str, object: &str, opts: PutObjectOptions, ) -> Result<String, std::io::Error> { let client = self.0.clone(); let result = client.initiate_multipart_upload(bucket, object, &opts).await?; Ok(result.upload_id) } pub fn list_multipart_uploads( &self, bucket: &str, prefix: &str, key_marker: &str, upload_id_marker: &str, delimiter: &str, max_uploads: i64, ) -> Result<ListMultipartUploadsResult, std::io::Error> { let client = self.0.clone(); client.list_multipart_uploads_query(bucket, key_marker, upload_id_marker, prefix, delimiter, max_uploads) } pub async fn put_object_part( &self, bucket: &str, object: &str, upload_id: &str, part_id: i64, data: ReaderImpl, size: i64, opts: PutObjectPartOptions, ) -> Result<ObjectPart, std::io::Error> { let mut p = UploadPartParams { bucket_name: bucket.to_string(), object_name: object.to_string(), upload_id: upload_id.to_string(), reader: data, part_number: part_id, md5_base64: opts.md5_base64, sha256_hex: opts.sha256_hex, size, //sse: opts.sse, stream_sha256: !opts.disable_content_sha256, custom_header: opts.custom_header, trailer: opts.trailer, }; let client = self.0.clone(); client.upload_part(&mut p).await } pub async fn list_object_parts( &self, bucket: &str, object: &str, upload_id: &str, part_number_marker: i64, max_parts: i64, ) -> Result<ListObjectPartsResult, std::io::Error> { let client = self.0.clone(); client .list_object_parts_query(bucket, object, upload_id, part_number_marker, max_parts) .await } pub async fn complete_multipart_upload( &self, bucket: &str, object: &str, upload_id: &str, parts: &[CompletePart], opts: PutObjectOptions, ) -> Result<UploadInfo, std::io::Error> { let client = self.0.clone(); let res = client .complete_multipart_upload(bucket, object, upload_id, CompleteMultipartUpload { parts: parts.to_vec() }, &opts) .await?; Ok(res) } pub async fn abort_multipart_upload(&self, bucket_name: &str, object: &str, upload_id: &str) -> Result<(), std::io::Error> { let client = self.0.clone(); client.abort_multipart_upload(bucket_name, object, upload_id).await } pub async fn get_bucket_policy(&self, bucket_name: &str) -> Result<String, std::io::Error> { let client = self.0.clone(); client.get_bucket_policy(bucket_name).await } pub async fn put_bucket_policy(&self, bucket_name: &str, bucket_policy: &str) -> Result<(), std::io::Error> { let client = self.0.clone(); client.put_bucket_policy(bucket_name, bucket_policy).await } pub async fn get_object( &self, bucket_name: &str, object_name: &str, opts: &GetObjectOptions, ) -> Result<(ObjectInfo, HeaderMap, ReadCloser), std::io::Error> { let client = self.0.clone(); client.get_object_inner(bucket_name, object_name, opts).await } } #[derive(Debug, Clone, Default)] pub struct PutObjectPartOptions { pub md5_base64: String, pub sha256_hex: String, //pub sse: encrypt.ServerSide, pub custom_header: HeaderMap, pub trailer: HeaderMap, pub disable_content_sha256: bool, } #[derive(Debug, Clone, Deserialize, Serialize)] pub struct ObjectInfo { pub etag: Option<String>, pub name: String, pub mod_time: Option<OffsetDateTime>, pub size: i64, pub content_type: Option<String>, #[serde(skip)] pub metadata: HeaderMap, pub user_metadata: HashMap<String, String>, pub user_tags: String, pub user_tag_count: usize, #[serde(skip)] pub owner: Owner, //pub grant: Vec<Grant>, pub storage_class: String, pub is_latest: bool, pub is_delete_marker: bool, pub version_id: Option<Uuid>, #[serde(skip, default = "replication_status_default")] pub replication_status: ReplicationStatus, pub replication_ready: bool, pub expiration: OffsetDateTime, pub expiration_rule_id: String, pub num_versions: usize, pub restore: RestoreInfo, pub checksum_crc32: String, pub checksum_crc32c: String, pub checksum_sha1: String, pub checksum_sha256: String, pub checksum_crc64nvme: String, pub checksum_mode: String, } fn replication_status_default() -> ReplicationStatus { ReplicationStatus::from_static(ReplicationStatus::PENDING) } impl Default for ObjectInfo { fn default() -> Self { Self { etag: None, name: "".to_string(), mod_time: None, size: 0, content_type: None, metadata: HeaderMap::new(), user_metadata: HashMap::new(), user_tags: "".to_string(), user_tag_count: 0, owner: Owner::default(), storage_class: "".to_string(), is_latest: false, is_delete_marker: false, version_id: None, replication_status: ReplicationStatus::from_static(ReplicationStatus::PENDING), replication_ready: false, expiration: OffsetDateTime::now_utc(), expiration_rule_id: "".to_string(), num_versions: 0, restore: RestoreInfo::default(), checksum_crc32: "".to_string(), checksum_crc32c: "".to_string(), checksum_sha1: "".to_string(), checksum_sha256: "".to_string(), checksum_crc64nvme: "".to_string(), checksum_mode: "".to_string(), } } } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RestoreInfo { ongoing_restore: bool, expiry_time: OffsetDateTime, } impl Default for RestoreInfo { fn default() -> Self { Self { ongoing_restore: false, expiry_time: OffsetDateTime::now_utc(), } } } pub struct ObjectMultipartInfo { pub initiated: OffsetDateTime, //pub initiator: initiator,
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
true
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/constants.rs
crates/ecstore/src/client/constants.rs
#![allow(unused_imports)] // Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] use lazy_static::lazy_static; use std::{collections::HashMap, sync::Arc}; use time::{format_description::FormatItem, macros::format_description}; pub const ABS_MIN_PART_SIZE: i64 = 1024 * 1024 * 5; pub const MAX_PARTS_COUNT: i64 = 10000; pub const MAX_PART_SIZE: i64 = 1024 * 1024 * 1024 * 5; pub const MIN_PART_SIZE: i64 = 1024 * 1024 * 16; pub const MAX_SINGLE_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 5; pub const MAX_MULTIPART_PUT_OBJECT_SIZE: i64 = 1024 * 1024 * 1024 * 1024 * 5; pub const UNSIGNED_PAYLOAD: &str = "UNSIGNED-PAYLOAD"; pub const UNSIGNED_PAYLOAD_TRAILER: &str = "STREAMING-UNSIGNED-PAYLOAD-TRAILER"; pub const TOTAL_WORKERS: i64 = 4; pub const SIGN_V4_ALGORITHM: &str = "AWS4-HMAC-SHA256"; pub const ISO8601_DATEFORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond]Z"); pub const GET_OBJECT_ATTRIBUTES_TAGS: &str = "ETag,Checksum,StorageClass,ObjectSize,ObjectParts"; pub const GET_OBJECT_ATTRIBUTES_MAX_PARTS: i64 = 1000;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_error_response.rs
crates/ecstore/src/client/api_error_response.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use http::StatusCode; use serde::{Deserialize, Serialize}; use serde::{de::Deserializer, ser::Serializer}; use std::fmt::Display; use s3s::Body; use s3s::S3ErrorCode; const _REPORT_ISSUE: &str = "Please report this issue at https://github.com/rustfs/rustfs/issues."; #[derive(Serialize, Deserialize, Debug, Clone, thiserror::Error, PartialEq, Eq)] #[serde(default, rename_all = "PascalCase")] pub struct ErrorResponse { #[serde(serialize_with = "serialize_code", deserialize_with = "deserialize_code")] pub code: S3ErrorCode, pub message: String, pub bucket_name: String, pub key: String, pub resource: String, pub request_id: String, pub host_id: String, pub region: String, pub server: String, #[serde(skip)] pub status_code: StatusCode, } fn serialize_code<S>(_data: &S3ErrorCode, s: S) -> Result<S::Ok, S::Error> where S: Serializer, { s.serialize_str("") } fn deserialize_code<'de, D>(d: D) -> Result<S3ErrorCode, D::Error> where D: Deserializer<'de>, { Ok(S3ErrorCode::from_bytes(String::deserialize(d)?.as_bytes()).unwrap_or(S3ErrorCode::Custom("".into()))) } impl Default for ErrorResponse { fn default() -> Self { ErrorResponse { code: S3ErrorCode::Custom("".into()), message: Default::default(), bucket_name: Default::default(), key: Default::default(), resource: Default::default(), request_id: Default::default(), host_id: Default::default(), region: Default::default(), server: Default::default(), status_code: Default::default(), } } } impl Display for ErrorResponse { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.message) } } pub fn to_error_response(err: &std::io::Error) -> ErrorResponse { if let Some(err) = err.get_ref() { if err.is::<ErrorResponse>() { err.downcast_ref::<ErrorResponse>().expect("err!").clone() } else { ErrorResponse::default() } } else { ErrorResponse::default() } } pub fn http_resp_to_error_response( resp: &http::Response<Body>, b: Vec<u8>, bucket_name: &str, object_name: &str, ) -> ErrorResponse { let err_body = String::from_utf8(b).unwrap(); let err_resp_ = quick_xml::de::from_str::<ErrorResponse>(&err_body); let mut err_resp = ErrorResponse::default(); if err_resp_.is_err() { match resp.status() { StatusCode::NOT_FOUND => { if object_name == "" { err_resp = ErrorResponse { status_code: resp.status(), code: S3ErrorCode::NoSuchBucket, message: "The specified bucket does not exist.".to_string(), bucket_name: bucket_name.to_string(), ..Default::default() }; } else { err_resp = ErrorResponse { status_code: resp.status(), code: S3ErrorCode::NoSuchKey, message: "The specified key does not exist.".to_string(), bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() }; } } StatusCode::FORBIDDEN => { err_resp = ErrorResponse { status_code: resp.status(), code: S3ErrorCode::AccessDenied, message: "Access Denied.".to_string(), bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() }; } StatusCode::CONFLICT => { err_resp = ErrorResponse { status_code: resp.status(), code: S3ErrorCode::BucketNotEmpty, message: "Bucket not empty.".to_string(), bucket_name: bucket_name.to_string(), ..Default::default() }; } StatusCode::PRECONDITION_FAILED => { err_resp = ErrorResponse { status_code: resp.status(), code: S3ErrorCode::PreconditionFailed, message: "Pre condition failed.".to_string(), bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() }; } _ => { let mut msg = resp.status().to_string(); if err_body.len() > 0 { msg = err_body; } err_resp = ErrorResponse { status_code: resp.status(), code: S3ErrorCode::Custom(resp.status().to_string().into()), message: msg, bucket_name: bucket_name.to_string(), ..Default::default() }; } } } else { err_resp = err_resp_.unwrap(); } err_resp.status_code = resp.status(); if let Some(server_name) = resp.headers().get("Server") { err_resp.server = server_name.to_str().expect("err").to_string(); } let code = resp.headers().get("x-minio-error-code"); if code.is_some() { err_resp.code = S3ErrorCode::Custom(code.expect("err").to_str().expect("err").into()); } let desc = resp.headers().get("x-minio-error-desc"); if desc.is_some() { err_resp.message = desc.expect("err").to_str().expect("err").trim_matches('"').to_string(); } if err_resp.request_id == "" { if let Some(x_amz_request_id) = resp.headers().get("x-amz-request-id") { err_resp.request_id = x_amz_request_id.to_str().expect("err").to_string(); } } if err_resp.host_id == "" { if let Some(x_amz_id_2) = resp.headers().get("x-amz-id-2") { err_resp.host_id = x_amz_id_2.to_str().expect("err").to_string(); } } if err_resp.region == "" { if let Some(x_amz_bucket_region) = resp.headers().get("x-amz-bucket-region") { err_resp.region = x_amz_bucket_region.to_str().expect("err").to_string(); } } if err_resp.code == S3ErrorCode::InvalidLocationConstraint/*InvalidRegion*/ && err_resp.region != "" { err_resp.message = format!("Region does not match, expecting region β€˜{}’.", err_resp.region); } err_resp } pub fn err_transfer_acceleration_bucket(bucket_name: &str) -> ErrorResponse { ErrorResponse { status_code: StatusCode::BAD_REQUEST, code: S3ErrorCode::InvalidArgument, message: "The name of the bucket used for Transfer Acceleration must be DNS-compliant and must not contain periods β€˜.’." .to_string(), bucket_name: bucket_name.to_string(), ..Default::default() } } pub fn err_entity_too_large(total_size: i64, max_object_size: i64, bucket_name: &str, object_name: &str) -> ErrorResponse { let msg = format!( "Your proposed upload size β€˜{}’ exceeds the maximum allowed object size β€˜{}’ for single PUT operation.", total_size, max_object_size ); ErrorResponse { status_code: StatusCode::BAD_REQUEST, code: S3ErrorCode::EntityTooLarge, message: msg, bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() } } pub fn err_entity_too_small(total_size: i64, bucket_name: &str, object_name: &str) -> ErrorResponse { let msg = format!( "Your proposed upload size β€˜{}’ is below the minimum allowed object size β€˜0B’ for single PUT operation.", total_size ); ErrorResponse { status_code: StatusCode::BAD_REQUEST, code: S3ErrorCode::EntityTooSmall, message: msg, bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() } } pub fn err_unexpected_eof(total_read: i64, total_size: i64, bucket_name: &str, object_name: &str) -> ErrorResponse { let msg = format!( "Data read β€˜{}’ is not equal to the size β€˜{}’ of the input Reader.", total_read, total_size ); ErrorResponse { status_code: StatusCode::BAD_REQUEST, code: S3ErrorCode::Custom("UnexpectedEOF".into()), message: msg, bucket_name: bucket_name.to_string(), key: object_name.to_string(), ..Default::default() } } pub fn err_invalid_argument(message: &str) -> ErrorResponse { ErrorResponse { status_code: StatusCode::BAD_REQUEST, code: S3ErrorCode::InvalidArgument, message: message.to_string(), request_id: "rustfs".to_string(), ..Default::default() } } pub fn err_api_not_supported(message: &str) -> ErrorResponse { ErrorResponse { status_code: StatusCode::NOT_IMPLEMENTED, code: S3ErrorCode::Custom("APINotSupported".into()), message: message.to_string(), request_id: "rustfs".to_string(), ..Default::default() } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_bucket_policy.rs
crates/ecstore/src/client/api_bucket_policy.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::{HeaderMap, StatusCode}; use std::collections::HashMap; use crate::client::{ api_error_response::http_resp_to_error_response, transition_api::{ReaderImpl, RequestMetadata, TransitionClient}, }; use rustfs_utils::hash::EMPTY_STRING_SHA256_HASH; impl TransitionClient { pub async fn set_bucket_policy(&self, bucket_name: &str, policy: &str) -> Result<(), std::io::Error> { if policy == "" { return self.remove_bucket_policy(bucket_name).await; } self.put_bucket_policy(bucket_name, policy).await } pub async fn put_bucket_policy(&self, bucket_name: &str, policy: &str) -> Result<(), std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("policy".to_string(), "".to_string()); let mut req_metadata = RequestMetadata { bucket_name: bucket_name.to_string(), query_values: url_values, content_body: ReaderImpl::Body(Bytes::from(policy.as_bytes().to_vec())), content_length: policy.len() as i64, object_name: "".to_string(), custom_header: HeaderMap::new(), content_md5_base64: "".to_string(), content_sha256_hex: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }; let resp = self.execute_method(http::Method::PUT, &mut req_metadata).await?; //defer closeResponse(resp) //if resp != nil { if resp.status() != StatusCode::NO_CONTENT && resp.status() != StatusCode::OK { return Err(std::io::Error::other(http_resp_to_error_response(&resp, vec![], bucket_name, ""))); } //} Ok(()) } pub async fn remove_bucket_policy(&self, bucket_name: &str) -> Result<(), std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("policy".to_string(), "".to_string()); let resp = self .execute_method( http::Method::DELETE, &mut RequestMetadata { bucket_name: bucket_name.to_string(), query_values: url_values, content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), object_name: "".to_string(), custom_header: HeaderMap::new(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; //defer closeResponse(resp) if resp.status() != StatusCode::NO_CONTENT { return Err(std::io::Error::other(http_resp_to_error_response(&resp, vec![], bucket_name, ""))); } Ok(()) } pub async fn get_bucket_policy(&self, bucket_name: &str) -> Result<String, std::io::Error> { let bucket_policy = self.get_bucket_policy_inner(bucket_name).await?; Ok(bucket_policy) } pub async fn get_bucket_policy_inner(&self, bucket_name: &str) -> Result<String, std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("policy".to_string(), "".to_string()); let resp = self .execute_method( http::Method::GET, &mut RequestMetadata { bucket_name: bucket_name.to_string(), query_values: url_values, content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), object_name: "".to_string(), custom_header: HeaderMap::new(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; let policy = String::from_utf8_lossy(&resp.body().bytes().expect("err").to_vec()).to_string(); Ok(policy) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/api_remove.rs
crates/ecstore/src/client/api_remove.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #![allow(unused_imports)] #![allow(unused_variables)] #![allow(unused_mut)] #![allow(unused_assignments)] #![allow(unused_must_use)] #![allow(clippy::all)] use bytes::Bytes; use http::{HeaderMap, HeaderValue, Method, StatusCode}; use rustfs_utils::HashAlgorithm; use s3s::S3ErrorCode; use s3s::dto::ReplicationStatus; use s3s::header::X_AMZ_BYPASS_GOVERNANCE_RETENTION; use std::fmt::Display; use std::{collections::HashMap, sync::Arc}; use time::OffsetDateTime; use tokio::sync::mpsc::{self, Receiver, Sender}; use crate::client::utils::base64_encode; use crate::client::{ api_error_response::{ErrorResponse, http_resp_to_error_response, to_error_response}, transition_api::{ReaderImpl, RequestMetadata, TransitionClient}, }; use crate::{ disk::DiskAPI, store_api::{GetObjectReader, ObjectInfo, StorageAPI}, }; use rustfs_utils::hash::EMPTY_STRING_SHA256_HASH; pub struct RemoveBucketOptions { _forced_delete: bool, } #[derive(Debug)] #[allow(dead_code)] pub struct AdvancedRemoveOptions { pub replication_delete_marker: bool, pub replication_status: ReplicationStatus, pub replication_mtime: Option<OffsetDateTime>, pub replication_request: bool, pub replication_validity_check: bool, } impl Default for AdvancedRemoveOptions { fn default() -> Self { Self { replication_delete_marker: false, replication_status: ReplicationStatus::from_static(ReplicationStatus::PENDING), replication_mtime: None, replication_request: false, replication_validity_check: false, } } } #[derive(Debug, Default)] pub struct RemoveObjectOptions { pub force_delete: bool, pub governance_bypass: bool, pub version_id: String, pub internal: AdvancedRemoveOptions, } impl TransitionClient { pub async fn remove_bucket_with_options(&self, bucket_name: &str, opts: &RemoveBucketOptions) -> Result<(), std::io::Error> { let headers = HeaderMap::new(); let resp = self .execute_method( Method::DELETE, &mut RequestMetadata { bucket_name: bucket_name.to_string(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), custom_header: headers, object_name: "".to_string(), query_values: Default::default(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; { let mut bucket_loc_cache = self.bucket_loc_cache.lock().unwrap(); bucket_loc_cache.delete(bucket_name); } Ok(()) } pub async fn remove_bucket(&self, bucket_name: &str) -> Result<(), std::io::Error> { let resp = self .execute_method( http::Method::DELETE, &mut RequestMetadata { bucket_name: bucket_name.to_string(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), custom_header: Default::default(), object_name: "".to_string(), query_values: Default::default(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; { let mut bucket_loc_cache = self.bucket_loc_cache.lock().unwrap(); bucket_loc_cache.delete(bucket_name); } Ok(()) } pub async fn remove_object(&self, bucket_name: &str, object_name: &str, opts: RemoveObjectOptions) -> Option<std::io::Error> { self.remove_object_inner(bucket_name, object_name, opts).await.err() } pub async fn remove_object_inner( &self, bucket_name: &str, object_name: &str, opts: RemoveObjectOptions, ) -> Result<RemoveObjectResult, std::io::Error> { let mut url_values = HashMap::new(); if opts.version_id != "" { url_values.insert("versionId".to_string(), opts.version_id.clone()); } let mut headers = HeaderMap::new(); if opts.governance_bypass { headers.insert(X_AMZ_BYPASS_GOVERNANCE_RETENTION, "true".parse().expect("err")); //amzBypassGovernance } let resp = self .execute_method( http::Method::DELETE, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), query_values: url_values, custom_header: headers, content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; Ok(RemoveObjectResult { object_name: object_name.to_string(), object_version_id: opts.version_id, delete_marker: resp.headers().get("x-amz-delete-marker").expect("err") == "true", delete_marker_version_id: resp .headers() .get("x-amz-version-id") .expect("err") .to_str() .expect("err") .to_string(), ..Default::default() }) } pub async fn remove_objects_with_result( self: Arc<Self>, bucket_name: &str, objects_rx: Receiver<ObjectInfo>, opts: RemoveObjectsOptions, ) -> Receiver<RemoveObjectResult> { let (result_tx, result_rx) = mpsc::channel(1); let self_clone = Arc::clone(&self); let bucket_name_owned = bucket_name.to_string(); tokio::spawn(async move { self_clone .remove_objects_inner(&bucket_name_owned, objects_rx, &result_tx, opts) .await; }); result_rx } pub async fn remove_objects( self: Arc<Self>, bucket_name: &str, objects_rx: Receiver<ObjectInfo>, opts: RemoveObjectsOptions, ) -> Receiver<RemoveObjectError> { let (error_tx, error_rx) = mpsc::channel(1); let self_clone = Arc::clone(&self); let bucket_name_owned = bucket_name.to_string(); let (result_tx, mut result_rx) = mpsc::channel(1); tokio::spawn(async move { self_clone .remove_objects_inner(&bucket_name_owned, objects_rx, &result_tx, opts) .await; }); tokio::spawn(async move { while let Some(res) = result_rx.recv().await { if res.err.is_none() { continue; } error_tx .send(RemoveObjectError { object_name: res.object_name, version_id: res.object_version_id, err: res.err, ..Default::default() }) .await; } }); error_rx } pub async fn remove_objects_inner( &self, bucket_name: &str, mut objects_rx: Receiver<ObjectInfo>, result_tx: &Sender<RemoveObjectResult>, opts: RemoveObjectsOptions, ) -> Result<(), std::io::Error> { let max_entries = 1000; let mut finish = false; let mut url_values = HashMap::new(); url_values.insert("delete".to_string(), "".to_string()); loop { if finish { break; } let mut count = 0; let mut batch = Vec::<ObjectInfo>::new(); while let Some(object) = objects_rx.recv().await { if has_invalid_xml_char(&object.name) { let remove_result = self .remove_object_inner( bucket_name, &object.name, RemoveObjectOptions { version_id: object.version_id.expect("err").to_string(), governance_bypass: opts.governance_bypass, ..Default::default() }, ) .await?; let remove_result_clone = remove_result.clone(); if !remove_result.err.is_none() { match to_error_response(&remove_result.err.expect("err")).code { S3ErrorCode::InvalidArgument | S3ErrorCode::NoSuchVersion => { continue; } _ => (), } result_tx.send(remove_result_clone.clone()).await; } result_tx.send(remove_result_clone).await; continue; } batch.push(object); count += 1; if count >= max_entries { break; } } if count == 0 { break; } if count < max_entries { finish = true; } let mut headers = HeaderMap::new(); if opts.governance_bypass { headers.insert(X_AMZ_BYPASS_GOVERNANCE_RETENTION, "true".parse().expect("err")); } let remove_bytes = generate_remove_multi_objects_request(&batch); let resp = self .execute_method( http::Method::POST, &mut RequestMetadata { bucket_name: bucket_name.to_string(), query_values: url_values.clone(), content_body: ReaderImpl::Body(Bytes::from(remove_bytes.clone())), content_length: remove_bytes.len() as i64, content_md5_base64: base64_encode(&HashAlgorithm::Md5.hash_encode(&remove_bytes).as_ref()), content_sha256_hex: base64_encode(&HashAlgorithm::SHA256.hash_encode(&remove_bytes).as_ref()), custom_header: headers, object_name: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; let body_bytes: Vec<u8> = resp.body().bytes().expect("err").to_vec(); process_remove_multi_objects_response(ReaderImpl::Body(Bytes::from(body_bytes)), result_tx.clone()); } Ok(()) } pub async fn remove_incomplete_upload(&self, bucket_name: &str, object_name: &str) -> Result<(), std::io::Error> { let upload_ids = self.find_upload_ids(bucket_name, object_name)?; for upload_id in upload_ids { self.abort_multipart_upload(bucket_name, object_name, &upload_id).await?; } Ok(()) } pub async fn abort_multipart_upload( &self, bucket_name: &str, object_name: &str, upload_id: &str, ) -> Result<(), std::io::Error> { let mut url_values = HashMap::new(); url_values.insert("uploadId".to_string(), upload_id.to_string()); let resp = self .execute_method( http::Method::DELETE, &mut RequestMetadata { bucket_name: bucket_name.to_string(), object_name: object_name.to_string(), query_values: url_values, content_sha256_hex: EMPTY_STRING_SHA256_HASH.to_string(), custom_header: HeaderMap::new(), content_body: ReaderImpl::Body(Bytes::new()), content_length: 0, content_md5_base64: "".to_string(), stream_sha256: false, trailer: HeaderMap::new(), pre_sign_url: Default::default(), add_crc: Default::default(), extra_pre_sign_header: Default::default(), bucket_location: Default::default(), expires: Default::default(), }, ) .await?; //if resp.is_some() { if resp.status() != StatusCode::NO_CONTENT { let error_response: ErrorResponse; match resp.status() { StatusCode::NOT_FOUND => { error_response = ErrorResponse { code: S3ErrorCode::NoSuchUpload, message: "The specified multipart upload does not exist.".to_string(), bucket_name: bucket_name.to_string(), key: object_name.to_string(), request_id: resp .headers() .get("x-amz-request-id") .expect("err") .to_str() .expect("err") .to_string(), host_id: resp .headers() .get("x-amz-id-2") .expect("err") .to_str() .expect("err") .to_string(), region: resp .headers() .get("x-amz-bucket-region") .expect("err") .to_str() .expect("err") .to_string(), ..Default::default() }; } _ => { return Err(std::io::Error::other(http_resp_to_error_response( &resp, vec![], bucket_name, object_name, ))); } } return Err(std::io::Error::other(error_response)); } //} Ok(()) } } #[derive(Debug, Default)] #[allow(dead_code)] pub struct RemoveObjectError { object_name: String, #[allow(dead_code)] version_id: String, err: Option<std::io::Error>, } impl Display for RemoveObjectError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { if self.err.is_none() { return write!(f, "unexpected remove object error result"); } write!(f, "{}", self.err.as_ref().expect("err").to_string()) } } #[derive(Debug, Default)] pub struct RemoveObjectResult { pub object_name: String, pub object_version_id: String, pub delete_marker: bool, pub delete_marker_version_id: String, pub err: Option<std::io::Error>, } impl Clone for RemoveObjectResult { fn clone(&self) -> Self { Self { object_name: self.object_name.clone(), object_version_id: self.object_version_id.clone(), delete_marker: self.delete_marker, delete_marker_version_id: self.delete_marker_version_id.clone(), err: None, //err } } } pub struct RemoveObjectsOptions { pub governance_bypass: bool, } pub fn generate_remove_multi_objects_request(objects: &[ObjectInfo]) -> Vec<u8> { todo!(); } pub fn process_remove_multi_objects_response(body: ReaderImpl, result_tx: Sender<RemoveObjectResult>) { todo!(); } fn has_invalid_xml_char(str: &str) -> bool { false }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/client/admin_handler_utils.rs
crates/ecstore/src/client/admin_handler_utils.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use http::status::StatusCode; use std::fmt::{self, Display, Formatter}; #[derive(Default, thiserror::Error, Debug, Clone, PartialEq)] pub struct AdminError { pub code: String, pub message: String, pub status_code: StatusCode, } impl Display for AdminError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.message) } } impl AdminError { pub fn new(code: &str, message: &str, status_code: StatusCode) -> Self { Self { code: code.to_string(), message: message.to_string(), status_code, } } pub fn msg(message: &str) -> Self { Self { code: "InternalError".to_string(), message: message.to_string(), status_code: StatusCode::INTERNAL_SERVER_ERROR, } } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/data_usage/local_snapshot.rs
crates/ecstore/src/data_usage/local_snapshot.rs
use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::time::SystemTime; use serde::{Deserialize, Serialize}; use tokio::fs; use crate::data_usage::BucketUsageInfo; use crate::disk::RUSTFS_META_BUCKET; use crate::error::{Error, Result}; /// Directory used to store per-disk usage snapshots under the metadata bucket. pub const DATA_USAGE_DIR: &str = "datausage"; /// Directory used to store incremental scan state files under the metadata bucket. pub const DATA_USAGE_STATE_DIR: &str = "datausage/state"; /// Snapshot file format version, allows forward compatibility if the structure evolves. pub const LOCAL_USAGE_SNAPSHOT_VERSION: u32 = 1; /// Additional metadata describing which disk produced the snapshot. #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct LocalUsageSnapshotMeta { /// Disk UUID stored as a string for simpler serialization. pub disk_id: String, /// Pool index if this disk is bound to a specific pool. pub pool_index: Option<usize>, /// Set index if known. pub set_index: Option<usize>, /// Disk index inside the set if known. pub disk_index: Option<usize>, } /// Usage snapshot produced by a single disk. #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct LocalUsageSnapshot { /// Format version recorded in the snapshot. pub format_version: u32, /// Snapshot metadata, including disk identity. pub meta: LocalUsageSnapshotMeta, /// Wall-clock timestamp when the snapshot was produced. pub last_update: Option<SystemTime>, /// Per-bucket usage statistics. pub buckets_usage: HashMap<String, BucketUsageInfo>, /// Cached bucket count to speed up aggregations. pub buckets_count: u64, /// Total objects counted on this disk. pub objects_total_count: u64, /// Total versions counted on this disk. pub versions_total_count: u64, /// Total delete markers counted on this disk. pub delete_markers_total_count: u64, /// Total bytes occupied by objects on this disk. pub objects_total_size: u64, } impl LocalUsageSnapshot { /// Create an empty snapshot with the default format version filled in. pub fn new(meta: LocalUsageSnapshotMeta) -> Self { Self { format_version: LOCAL_USAGE_SNAPSHOT_VERSION, meta, ..Default::default() } } /// Recalculate cached totals from the per-bucket map. pub fn recompute_totals(&mut self) { let mut buckets_count = 0u64; let mut objects_total_count = 0u64; let mut versions_total_count = 0u64; let mut delete_markers_total_count = 0u64; let mut objects_total_size = 0u64; for usage in self.buckets_usage.values() { buckets_count = buckets_count.saturating_add(1); objects_total_count = objects_total_count.saturating_add(usage.objects_count); versions_total_count = versions_total_count.saturating_add(usage.versions_count); delete_markers_total_count = delete_markers_total_count.saturating_add(usage.delete_markers_count); objects_total_size = objects_total_size.saturating_add(usage.size); } self.buckets_count = buckets_count; self.objects_total_count = objects_total_count; self.versions_total_count = versions_total_count; self.delete_markers_total_count = delete_markers_total_count; self.objects_total_size = objects_total_size; } } /// Build the snapshot file name `<disk-id>.json`. pub fn snapshot_file_name(disk_id: &str) -> String { format!("{disk_id}.json") } /// Build the object path relative to `RUSTFS_META_BUCKET`, e.g. `datausage/<disk-id>.json`. pub fn snapshot_object_path(disk_id: &str) -> String { format!("{}/{}", DATA_USAGE_DIR, snapshot_file_name(disk_id)) } /// Return the absolute path to `.rustfs.sys/datausage` on the given disk root. pub fn data_usage_dir(root: &Path) -> PathBuf { root.join(RUSTFS_META_BUCKET).join(DATA_USAGE_DIR) } /// Return the absolute path to `.rustfs.sys/datausage/state` on the given disk root. pub fn data_usage_state_dir(root: &Path) -> PathBuf { root.join(RUSTFS_META_BUCKET).join(DATA_USAGE_STATE_DIR) } /// Build the absolute path to the snapshot file for the provided disk ID. pub fn snapshot_path(root: &Path, disk_id: &str) -> PathBuf { data_usage_dir(root).join(snapshot_file_name(disk_id)) } /// Read a snapshot from disk if it exists. pub async fn read_snapshot(root: &Path, disk_id: &str) -> Result<Option<LocalUsageSnapshot>> { let path = snapshot_path(root, disk_id); match fs::read(&path).await { Ok(content) => { let snapshot = serde_json::from_slice::<LocalUsageSnapshot>(&content) .map_err(|err| Error::other(format!("failed to deserialize snapshot {path:?}: {err}")))?; Ok(Some(snapshot)) } Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None), Err(err) => Err(Error::other(err)), } } /// Persist a snapshot to disk, creating directories as needed and overwriting any existing file. pub async fn write_snapshot(root: &Path, disk_id: &str, snapshot: &LocalUsageSnapshot) -> Result<()> { let dir = data_usage_dir(root); fs::create_dir_all(&dir).await.map_err(Error::other)?; let path = dir.join(snapshot_file_name(disk_id)); let data = serde_json::to_vec_pretty(snapshot) .map_err(|err| Error::other(format!("failed to serialize snapshot {path:?}: {err}")))?; fs::write(&path, data).await.map_err(Error::other) } /// Ensure that the data usage directory structure exists on this disk root. pub async fn ensure_data_usage_layout(root: &Path) -> Result<()> { let usage_dir = data_usage_dir(root); fs::create_dir_all(&usage_dir).await.map_err(Error::other)?; let state_dir = data_usage_state_dir(root); fs::create_dir_all(&state_dir).await.map_err(Error::other)?; Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/cache_value/metacache_set.rs
crates/ecstore/src/cache_value/metacache_set.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::disk::error::DiskError; use crate::disk::{self, DiskAPI, DiskStore, WalkDirOptions}; use futures::future::join_all; use rustfs_filemeta::{MetaCacheEntries, MetaCacheEntry, MetacacheReader, is_io_eof}; use std::{future::Future, pin::Pin}; use tokio::spawn; use tokio_util::sync::CancellationToken; use tracing::{error, info, warn}; pub type AgreedFn = Box<dyn Fn(MetaCacheEntry) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + 'static>; pub type PartialFn = Box<dyn Fn(MetaCacheEntries, &[Option<DiskError>]) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + 'static>; type FinishedFn = Box<dyn Fn(&[Option<DiskError>]) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + 'static>; #[derive(Default)] pub struct ListPathRawOptions { pub disks: Vec<Option<DiskStore>>, pub fallback_disks: Vec<Option<DiskStore>>, pub bucket: String, pub path: String, pub recursive: bool, pub filter_prefix: Option<String>, pub forward_to: Option<String>, pub min_disks: usize, pub report_not_found: bool, pub per_disk_limit: i32, pub agreed: Option<AgreedFn>, pub partial: Option<PartialFn>, pub finished: Option<FinishedFn>, // pub agreed: Option<Arc<dyn Fn(MetaCacheEntry) + Send + Sync>>, // pub partial: Option<Arc<dyn Fn(MetaCacheEntries, &[Option<Error>]) + Send + Sync>>, // pub finished: Option<Arc<dyn Fn(&[Option<Error>]) + Send + Sync>>, } impl Clone for ListPathRawOptions { fn clone(&self) -> Self { Self { disks: self.disks.clone(), fallback_disks: self.fallback_disks.clone(), bucket: self.bucket.clone(), path: self.path.clone(), recursive: self.recursive, filter_prefix: self.filter_prefix.clone(), forward_to: self.forward_to.clone(), min_disks: self.min_disks, report_not_found: self.report_not_found, per_disk_limit: self.per_disk_limit, ..Default::default() } } } pub async fn list_path_raw(rx: CancellationToken, opts: ListPathRawOptions) -> disk::error::Result<()> { if opts.disks.is_empty() { return Err(DiskError::other("list_path_raw: 0 drives provided")); } let mut jobs: Vec<tokio::task::JoinHandle<std::result::Result<(), DiskError>>> = Vec::new(); let mut readers = Vec::with_capacity(opts.disks.len()); let fds = opts.fallback_disks.iter().flatten().cloned().collect::<Vec<_>>(); let cancel_rx = CancellationToken::new(); for disk in opts.disks.iter() { let opdisk = disk.clone(); let opts_clone = opts.clone(); let mut fds_clone = fds.clone(); let cancel_rx_clone = cancel_rx.clone(); let (rd, mut wr) = tokio::io::duplex(64); readers.push(MetacacheReader::new(rd)); jobs.push(spawn(async move { let wakl_opts = WalkDirOptions { bucket: opts_clone.bucket.clone(), base_dir: opts_clone.path.clone(), recursive: opts_clone.recursive, report_notfound: opts_clone.report_not_found, filter_prefix: opts_clone.filter_prefix.clone(), forward_to: opts_clone.forward_to.clone(), limit: opts_clone.per_disk_limit, ..Default::default() }; let mut need_fallback = false; if let Some(disk) = opdisk { match disk.walk_dir(wakl_opts, &mut wr).await { Ok(_res) => {} Err(err) => { info!("walk dir err {:?}", &err); need_fallback = true; } } } else { need_fallback = true; } if cancel_rx_clone.is_cancelled() { // warn!("list_path_raw: cancel_rx_clone.is_cancelled()"); return Ok(()); } while need_fallback { let disk_op = { if fds_clone.is_empty() { None } else { let disk = fds_clone.remove(0); if disk.is_online().await { Some(disk.clone()) } else { None } } }; let Some(disk) = disk_op else { warn!("list_path_raw: fallback disk is none"); break; }; match disk .as_ref() .walk_dir( WalkDirOptions { bucket: opts_clone.bucket.clone(), base_dir: opts_clone.path.clone(), recursive: opts_clone.recursive, report_notfound: opts_clone.report_not_found, filter_prefix: opts_clone.filter_prefix.clone(), forward_to: opts_clone.forward_to.clone(), limit: opts_clone.per_disk_limit, ..Default::default() }, &mut wr, ) .await { Ok(_r) => { need_fallback = false; } Err(err) => { error!("walk dir2 err {:?}", &err); break; } } } // warn!("list_path_raw: while need_fallback done"); Ok(()) })); } let revjob = spawn(async move { let mut errs: Vec<Option<DiskError>> = Vec::with_capacity(readers.len()); for _ in 0..readers.len() { errs.push(None); } loop { let mut current = MetaCacheEntry::default(); // warn!( // "list_path_raw: loop start, bucket: {}, path: {}, current: {:?}", // opts.bucket, opts.path, &current.name // ); if rx.is_cancelled() { return Err(DiskError::other("canceled")); } let mut top_entries: Vec<Option<MetaCacheEntry>> = vec![None; readers.len()]; let mut at_eof = 0; let mut fnf = 0; let mut vnf = 0; let mut has_err = 0; let mut agree = 0; for (i, r) in readers.iter_mut().enumerate() { if errs[i].is_some() { has_err += 1; continue; } let entry = match r.peek().await { Ok(res) => { if let Some(entry) = res { // info!("read entry disk: {}, name: {}", i, entry.name); entry } else { // eof at_eof += 1; // warn!("list_path_raw: peek eof, disk: {}", i); continue; } } Err(err) => { if err == rustfs_filemeta::Error::Unexpected { at_eof += 1; // warn!("list_path_raw: peek err eof, disk: {}", i); continue; } // warn!("list_path_raw: peek err00, err: {:?}", err); if is_io_eof(&err) { at_eof += 1; // warn!("list_path_raw: peek eof, disk: {}", i); continue; } if err == rustfs_filemeta::Error::FileNotFound { at_eof += 1; fnf += 1; // warn!("list_path_raw: peek fnf, disk: {}", i); continue; } else if err == rustfs_filemeta::Error::VolumeNotFound { at_eof += 1; fnf += 1; vnf += 1; // warn!("list_path_raw: peek vnf, disk: {}", i); continue; } else { has_err += 1; errs[i] = Some(err.into()); // warn!("list_path_raw: peek err, disk: {}", i); continue; } } }; // warn!("list_path_raw: loop entry: {:?}, disk: {}", &entry.name, i); // If no current, add it. if current.name.is_empty() { top_entries[i] = Some(entry.clone()); current = entry; agree += 1; continue; } // If exact match, we agree. if let (_, true) = current.matches(Some(&entry), true) { top_entries[i] = Some(entry); agree += 1; continue; } // If only the name matches we didn't agree, but add it for resolution. if entry.name == current.name { top_entries[i] = Some(entry); continue; } // We got different entries if entry.name > current.name { continue; } for item in top_entries.iter_mut().take(i) { *item = None; } agree = 1; top_entries[i] = Some(entry.clone()); current = entry; } if vnf > 0 && vnf >= (readers.len() - opts.min_disks) { // warn!("list_path_raw: vnf > 0 && vnf >= (readers.len() - opts.min_disks) break"); return Err(DiskError::VolumeNotFound); } if fnf > 0 && fnf >= (readers.len() - opts.min_disks) { // warn!("list_path_raw: fnf > 0 && fnf >= (readers.len() - opts.min_disks) break"); return Err(DiskError::FileNotFound); } if has_err > 0 && has_err > opts.disks.len() - opts.min_disks { if let Some(finished_fn) = opts.finished.as_ref() { finished_fn(&errs).await; } let mut combined_err = Vec::new(); errs.iter().zip(opts.disks.iter()).for_each(|(err, disk)| match (err, disk) { (Some(err), Some(disk)) => { combined_err.push(format!("drive {} returned: {}", disk.to_string(), err)); } (Some(err), None) => { combined_err.push(err.to_string()); } _ => {} }); error!( "list_path_raw: has_err > 0 && has_err > opts.disks.len() - opts.min_disks break, err: {:?}", &combined_err.join(", ") ); return Err(DiskError::other(combined_err.join(", "))); } // Break if all at EOF or error. if at_eof + has_err == readers.len() { if has_err > 0 && let Some(finished_fn) = opts.finished.as_ref() { finished_fn(&errs).await; } // error!("list_path_raw: at_eof + has_err == readers.len() break {:?}", &errs); break; } if agree == readers.len() { for r in readers.iter_mut() { let _ = r.skip(1).await; } if let Some(agreed_fn) = opts.agreed.as_ref() { // warn!("list_path_raw: agreed_fn start, current: {:?}", &current.name); agreed_fn(current).await; // warn!("list_path_raw: agreed_fn done"); } continue; } // warn!("list_path_raw: skip start, current: {:?}", &current.name); for (i, r) in readers.iter_mut().enumerate() { if top_entries[i].is_some() { let _ = r.skip(1).await; } } if let Some(partial_fn) = opts.partial.as_ref() { partial_fn(MetaCacheEntries(top_entries), &errs).await; } } Ok(()) }); if let Err(err) = revjob.await.map_err(std::io::Error::other)? { error!("list_path_raw: revjob err {:?}", err); cancel_rx.cancel(); return Err(err); } let results = join_all(jobs).await; for result in results { if let Err(err) = result { error!("list_path_raw err {:?}", err); } } // warn!("list_path_raw: done"); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/src/cache_value/mod.rs
crates/ecstore/src/cache_value/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::sync::Arc; use lazy_static::lazy_static; use tokio_util::sync::CancellationToken; pub mod metacache_set; lazy_static! { pub static ref LIST_PATH_RAW_CANCEL_TOKEN: Arc<CancellationToken> = Arc::new(CancellationToken::new()); }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/benches/comparison_benchmark.rs
crates/ecstore/benches/comparison_benchmark.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Reed-Solomon SIMD performance analysis benchmarks //! //! This benchmark analyzes the performance characteristics of the SIMD Reed-Solomon implementation //! across different data sizes, shard configurations, and usage patterns. //! //! ## Running Performance Analysis //! //! ```bash //! # Run all SIMD performance tests //! cargo bench --bench comparison_benchmark //! //! # Generate detailed performance report //! cargo bench --bench comparison_benchmark -- --save-baseline simd_analysis //! //! # Run specific test categories //! cargo bench --bench comparison_benchmark encode_analysis //! cargo bench --bench comparison_benchmark decode_analysis //! cargo bench --bench comparison_benchmark shard_analysis //! ``` use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use rustfs_ecstore::erasure_coding::Erasure; use std::hint::black_box; use std::time::Duration; /// Performance test data configuration struct TestData { data: Vec<u8>, size_name: &'static str, } impl TestData { fn new(size: usize, size_name: &'static str) -> Self { let data = (0..size).map(|i| (i % 256) as u8).collect(); Self { data, size_name } } } /// Generate different sized test datasets for performance analysis fn generate_test_datasets() -> Vec<TestData> { vec![ TestData::new(1024, "1KB"), // Small data TestData::new(8 * 1024, "8KB"), // Medium-small data TestData::new(64 * 1024, "64KB"), // Medium data TestData::new(256 * 1024, "256KB"), // Medium-large data TestData::new(1024 * 1024, "1MB"), // Large data TestData::new(4 * 1024 * 1024, "4MB"), // Extra large data ] } /// SIMD encoding performance analysis fn bench_encode_analysis(c: &mut Criterion) { let datasets = generate_test_datasets(); let configs = vec![ (4, 2, "4+2"), // Common configuration (6, 3, "6+3"), // 50% redundancy (8, 4, "8+4"), // 50% redundancy, more shards ]; for dataset in &datasets { for (data_shards, parity_shards, config_name) in &configs { let test_name = format!("{}_{}_{}", dataset.size_name, config_name, "simd"); let mut group = c.benchmark_group("encode_analysis"); group.throughput(Throughput::Bytes(dataset.data.len() as u64)); group.sample_size(20); group.measurement_time(Duration::from_secs(10)); // Test SIMD encoding performance match Erasure::new(*data_shards, *parity_shards, dataset.data.len()).encode_data(&dataset.data) { Ok(_) => { group.bench_with_input( BenchmarkId::new("simd_encode", &test_name), &(&dataset.data, *data_shards, *parity_shards), |b, (data, data_shards, parity_shards)| { let erasure = Erasure::new(*data_shards, *parity_shards, data.len()); b.iter(|| { let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }, ); } Err(e) => { println!("⚠️ Skipping test {test_name} - configuration not supported: {e}"); } } group.finish(); } } } /// SIMD decoding performance analysis fn bench_decode_analysis(c: &mut Criterion) { let datasets = generate_test_datasets(); let configs = vec![(4, 2, "4+2"), (6, 3, "6+3"), (8, 4, "8+4")]; for dataset in &datasets { for (data_shards, parity_shards, config_name) in &configs { let test_name = format!("{}_{}_{}", dataset.size_name, config_name, "simd"); let erasure = Erasure::new(*data_shards, *parity_shards, dataset.data.len()); // Pre-encode data - check if this configuration is supported match erasure.encode_data(&dataset.data) { Ok(encoded_shards) => { let mut group = c.benchmark_group("decode_analysis"); group.throughput(Throughput::Bytes(dataset.data.len() as u64)); group.sample_size(20); group.measurement_time(Duration::from_secs(10)); group.bench_with_input( BenchmarkId::new("simd_decode", &test_name), &(&encoded_shards, *data_shards, *parity_shards), |b, (shards, data_shards, parity_shards)| { let erasure = Erasure::new(*data_shards, *parity_shards, dataset.data.len()); b.iter(|| { // Simulate maximum recoverable data loss let mut shards_opt: Vec<Option<Vec<u8>>> = shards.iter().map(|shard| Some(shard.to_vec())).collect(); // Lose up to parity_shards number of shards for item in shards_opt.iter_mut().take(*parity_shards) { *item = None; } erasure.decode_data(black_box(&mut shards_opt)).unwrap(); black_box(&shards_opt); }); }, ); group.finish(); } Err(e) => { println!("⚠️ Skipping decode test {test_name} - configuration not supported: {e}"); } } } } } /// Shard size sensitivity analysis for SIMD optimization fn bench_shard_size_analysis(c: &mut Criterion) { let data_shards = 4; let parity_shards = 2; // Test different shard sizes, focusing on SIMD optimization thresholds let shard_sizes = vec![32, 64, 128, 256, 512, 1024, 2048, 4096, 8192]; let mut group = c.benchmark_group("shard_size_analysis"); group.sample_size(15); group.measurement_time(Duration::from_secs(8)); for shard_size in shard_sizes { let total_size = shard_size * data_shards; let data = (0..total_size).map(|i| (i % 256) as u8).collect::<Vec<u8>>(); let test_name = format!("{shard_size}B_shard_simd"); group.throughput(Throughput::Bytes(total_size as u64)); // Check if this shard size is supported let erasure = Erasure::new(data_shards, parity_shards, data.len()); match erasure.encode_data(&data) { Ok(_) => { group.bench_with_input(BenchmarkId::new("shard_size", &test_name), &data, |b, data| { let erasure = Erasure::new(data_shards, parity_shards, data.len()); b.iter(|| { let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }); } Err(e) => { println!("⚠️ Skipping shard size test {test_name} - not supported: {e}"); } } } group.finish(); } /// High-load concurrent performance analysis fn bench_concurrent_analysis(c: &mut Criterion) { use std::sync::Arc; use std::thread; let data_size = 1024 * 1024; // 1MB let data = Arc::new((0..data_size).map(|i| (i % 256) as u8).collect::<Vec<u8>>()); let erasure = Arc::new(Erasure::new(4, 2, data_size)); let mut group = c.benchmark_group("concurrent_analysis"); group.throughput(Throughput::Bytes(data_size as u64)); group.sample_size(10); group.measurement_time(Duration::from_secs(15)); let test_name = "1MB_concurrent_simd"; group.bench_function(test_name, |b| { b.iter(|| { let handles: Vec<_> = (0..4) .map(|_| { let data_clone = data.clone(); let erasure_clone = erasure.clone(); thread::spawn(move || { let shards = erasure_clone.encode_data(&data_clone).unwrap(); black_box(shards); }) }) .collect(); for handle in handles { handle.join().unwrap(); } }); }); group.finish(); } /// Error recovery performance analysis fn bench_error_recovery_analysis(c: &mut Criterion) { let data_size = 512 * 1024; // 512KB let data = (0..data_size).map(|i| (i % 256) as u8).collect::<Vec<u8>>(); // Test different error recovery scenarios let scenarios = vec![ (4, 2, 1, "single_loss"), // Lose 1 shard (4, 2, 2, "double_loss"), // Lose 2 shards (maximum) (6, 3, 1, "single_loss_6_3"), // Lose 1 shard with 6+3 (6, 3, 3, "triple_loss_6_3"), // Lose 3 shards (maximum) (8, 4, 2, "double_loss_8_4"), // Lose 2 shards with 8+4 (8, 4, 4, "quad_loss_8_4"), // Lose 4 shards (maximum) ]; let mut group = c.benchmark_group("error_recovery_analysis"); group.throughput(Throughput::Bytes(data_size as u64)); group.sample_size(15); group.measurement_time(Duration::from_secs(10)); for (data_shards, parity_shards, loss_count, scenario_name) in scenarios { let erasure = Erasure::new(data_shards, parity_shards, data_size); match erasure.encode_data(&data) { Ok(encoded_shards) => { let test_name = format!("{data_shards}+{parity_shards}_{scenario_name}"); group.bench_with_input( BenchmarkId::new("recovery", &test_name), &(&encoded_shards, data_shards, parity_shards, loss_count), |b, (shards, data_shards, parity_shards, loss_count)| { let erasure = Erasure::new(*data_shards, *parity_shards, data_size); b.iter(|| { // Simulate specific number of shard losses let mut shards_opt: Vec<Option<Vec<u8>>> = shards.iter().map(|shard| Some(shard.to_vec())).collect(); // Lose the specified number of shards for item in shards_opt.iter_mut().take(*loss_count) { *item = None; } erasure.decode_data(black_box(&mut shards_opt)).unwrap(); black_box(&shards_opt); }); }, ); } Err(e) => { println!("⚠️ Skipping recovery test {scenario_name}: {e}"); } } } group.finish(); } /// Memory efficiency analysis fn bench_memory_analysis(c: &mut Criterion) { let data_sizes = vec![64 * 1024, 256 * 1024, 1024 * 1024]; // 64KB, 256KB, 1MB let config = (4, 2); // 4+2 configuration let mut group = c.benchmark_group("memory_analysis"); group.sample_size(15); group.measurement_time(Duration::from_secs(8)); for data_size in data_sizes { let data = (0..data_size).map(|i| (i % 256) as u8).collect::<Vec<u8>>(); let size_name = format!("{}KB", data_size / 1024); group.throughput(Throughput::Bytes(data_size as u64)); // Test instance reuse vs new instance creation group.bench_with_input(BenchmarkId::new("reuse_instance", &size_name), &data, |b, data| { let erasure = Erasure::new(config.0, config.1, data.len()); b.iter(|| { let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }); group.bench_with_input(BenchmarkId::new("new_instance", &size_name), &data, |b, data| { b.iter(|| { let erasure = Erasure::new(config.0, config.1, data.len()); let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }); } group.finish(); } // Benchmark group configuration criterion_group!( benches, bench_encode_analysis, bench_decode_analysis, bench_shard_size_analysis, bench_concurrent_analysis, bench_error_recovery_analysis, bench_memory_analysis ); criterion_main!(benches);
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/ecstore/benches/erasure_benchmark.rs
crates/ecstore/benches/erasure_benchmark.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Reed-Solomon SIMD erasure coding performance benchmarks. //! //! This benchmark tests the performance of the high-performance SIMD Reed-Solomon implementation. //! //! ## Running Benchmarks //! //! ```bash //! # Run all benchmarks //! cargo bench //! //! # Run specific benchmark //! cargo bench --bench erasure_benchmark //! //! # Generate HTML report //! cargo bench --bench erasure_benchmark -- --output-format html //! //! # Test encoding performance only //! cargo bench encode //! //! # Test decoding performance only //! cargo bench decode //! ``` //! //! ## Test Configurations //! //! The benchmarks test various scenarios: //! - Different data sizes: 1KB, 64KB, 1MB, 16MB //! - Different erasure coding configurations: (4,2), (6,3), (8,4) //! - Both encoding and decoding operations //! - SIMD optimization for different shard sizes use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use rustfs_ecstore::erasure_coding::{Erasure, calc_shard_size}; use std::hint::black_box; use std::time::Duration; /// Benchmark configuration structure #[derive(Clone, Debug)] struct BenchConfig { /// Number of data shards data_shards: usize, /// Number of parity shards parity_shards: usize, /// Test data size (bytes) data_size: usize, /// Block size (bytes) block_size: usize, /// Configuration name name: String, } impl BenchConfig { fn new(data_shards: usize, parity_shards: usize, data_size: usize, block_size: usize) -> Self { Self { data_shards, parity_shards, data_size, block_size, name: format!("{}+{}_{}KB_{}KB-block", data_shards, parity_shards, data_size / 1024, block_size / 1024), } } } /// Generate test data fn generate_test_data(size: usize) -> Vec<u8> { (0..size).map(|i| (i % 256) as u8).collect() } /// Benchmark: Encoding performance fn bench_encode_performance(c: &mut Criterion) { let configs = vec![ // Small data tests - 1KB BenchConfig::new(4, 2, 1024, 1024), BenchConfig::new(6, 3, 1024, 1024), BenchConfig::new(8, 4, 1024, 1024), // Medium data tests - 64KB BenchConfig::new(4, 2, 64 * 1024, 64 * 1024), BenchConfig::new(6, 3, 64 * 1024, 64 * 1024), BenchConfig::new(8, 4, 64 * 1024, 64 * 1024), // Large data tests - 1MB BenchConfig::new(4, 2, 1024 * 1024, 1024 * 1024), BenchConfig::new(6, 3, 1024 * 1024, 1024 * 1024), BenchConfig::new(8, 4, 1024 * 1024, 1024 * 1024), // Extra large data tests - 16MB BenchConfig::new(4, 2, 16 * 1024 * 1024, 16 * 1024 * 1024), BenchConfig::new(6, 3, 16 * 1024 * 1024, 16 * 1024 * 1024), ]; for config in configs { let data = generate_test_data(config.data_size); // Test SIMD encoding performance let mut group = c.benchmark_group("encode_simd"); group.throughput(Throughput::Bytes(config.data_size as u64)); group.sample_size(10); group.measurement_time(Duration::from_secs(5)); group.bench_with_input(BenchmarkId::new("simd_impl", &config.name), &(&data, &config), |b, (data, config)| { let erasure = Erasure::new(config.data_shards, config.parity_shards, config.block_size); b.iter(|| { let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }); group.finish(); // Test direct SIMD implementation for large shards (>= 512 bytes) let shard_size = calc_shard_size(config.data_size, config.data_shards); if shard_size >= 512 { let mut simd_group = c.benchmark_group("encode_simd_direct"); simd_group.throughput(Throughput::Bytes(config.data_size as u64)); simd_group.sample_size(10); simd_group.measurement_time(Duration::from_secs(5)); simd_group.bench_with_input(BenchmarkId::new("simd_direct", &config.name), &(&data, &config), |b, (data, config)| { b.iter(|| { // Direct SIMD implementation let per_shard_size = calc_shard_size(data.len(), config.data_shards); match reed_solomon_simd::ReedSolomonEncoder::new(config.data_shards, config.parity_shards, per_shard_size) { Ok(mut encoder) => { // Create properly sized buffer and fill with data let mut buffer = vec![0u8; per_shard_size * config.data_shards]; let copy_len = data.len().min(buffer.len()); buffer[..copy_len].copy_from_slice(&data[..copy_len]); // Add data shards with correct shard size for chunk in buffer.chunks_exact(per_shard_size) { encoder.add_original_shard(black_box(chunk)).unwrap(); } let result = encoder.encode().unwrap(); black_box(result); } Err(_) => { // SIMD doesn't support this configuration, skip black_box(()); } } }); }); simd_group.finish(); } } } /// Benchmark: Decoding performance fn bench_decode_performance(c: &mut Criterion) { let configs = vec![ // Medium data tests - 64KB BenchConfig::new(4, 2, 64 * 1024, 64 * 1024), BenchConfig::new(6, 3, 64 * 1024, 64 * 1024), // Large data tests - 1MB BenchConfig::new(4, 2, 1024 * 1024, 1024 * 1024), BenchConfig::new(6, 3, 1024 * 1024, 1024 * 1024), // Extra large data tests - 16MB BenchConfig::new(4, 2, 16 * 1024 * 1024, 16 * 1024 * 1024), ]; for config in configs { let data = generate_test_data(config.data_size); let erasure = Erasure::new(config.data_shards, config.parity_shards, config.block_size); // Pre-encode data let encoded_shards = erasure.encode_data(&data).unwrap(); // Test SIMD decoding performance let mut group = c.benchmark_group("decode_simd"); group.throughput(Throughput::Bytes(config.data_size as u64)); group.sample_size(10); group.measurement_time(Duration::from_secs(5)); group.bench_with_input( BenchmarkId::new("simd_impl", &config.name), &(&encoded_shards, &config), |b, (shards, config)| { let erasure = Erasure::new(config.data_shards, config.parity_shards, config.block_size); b.iter(|| { // Simulate data loss - lose one data shard and one parity shard let mut shards_opt: Vec<Option<Vec<u8>>> = shards.iter().map(|shard| Some(shard.to_vec())).collect(); // Lose last data shard and first parity shard shards_opt[config.data_shards - 1] = None; shards_opt[config.data_shards] = None; erasure.decode_data(black_box(&mut shards_opt)).unwrap(); black_box(&shards_opt); }); }, ); group.finish(); // Test direct SIMD decoding for large shards let shard_size = calc_shard_size(config.data_size, config.data_shards); if shard_size >= 512 { let mut simd_group = c.benchmark_group("decode_simd_direct"); simd_group.throughput(Throughput::Bytes(config.data_size as u64)); simd_group.sample_size(10); simd_group.measurement_time(Duration::from_secs(5)); simd_group.bench_with_input( BenchmarkId::new("simd_direct", &config.name), &(&encoded_shards, &config), |b, (shards, config)| { b.iter(|| { let per_shard_size = calc_shard_size(config.data_size, config.data_shards); match reed_solomon_simd::ReedSolomonDecoder::new(config.data_shards, config.parity_shards, per_shard_size) { Ok(mut decoder) => { // Add available shards (except lost ones) for (i, shard) in shards.iter().enumerate() { if i != config.data_shards - 1 && i != config.data_shards { if i < config.data_shards { decoder.add_original_shard(i, black_box(shard)).unwrap(); } else { let recovery_idx = i - config.data_shards; decoder.add_recovery_shard(recovery_idx, black_box(shard)).unwrap(); } } } let result = decoder.decode().unwrap(); black_box(result); } Err(_) => { // SIMD doesn't support this configuration, skip black_box(()); } } }); }, ); simd_group.finish(); } } } /// Benchmark: Impact of different shard sizes on performance fn bench_shard_size_impact(c: &mut Criterion) { let shard_sizes = vec![64, 128, 256, 512, 1024, 2048, 4096, 8192]; let data_shards = 4; let parity_shards = 2; let mut group = c.benchmark_group("shard_size_impact"); group.sample_size(10); group.measurement_time(Duration::from_secs(3)); for shard_size in shard_sizes { let total_data_size = shard_size * data_shards; let data = generate_test_data(total_data_size); group.throughput(Throughput::Bytes(total_data_size as u64)); // Test SIMD implementation group.bench_with_input(BenchmarkId::new("simd", format!("shard_{shard_size}B")), &data, |b, data| { let erasure = Erasure::new(data_shards, parity_shards, total_data_size); b.iter(|| { let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }); } group.finish(); } /// Benchmark: Impact of coding configurations on performance fn bench_coding_configurations(c: &mut Criterion) { let configs = vec![ (2, 1), // Minimal redundancy (3, 2), // Medium redundancy (4, 2), // Common configuration (6, 3), // 50% redundancy (8, 4), // 50% redundancy, more shards (10, 5), // 50% redundancy, many shards (12, 6), // 50% redundancy, very many shards ]; let data_size = 1024 * 1024; // 1MB test data let data = generate_test_data(data_size); let mut group = c.benchmark_group("coding_configurations"); group.throughput(Throughput::Bytes(data_size as u64)); group.sample_size(10); group.measurement_time(Duration::from_secs(5)); for (data_shards, parity_shards) in configs { let config_name = format!("{data_shards}+{parity_shards}"); group.bench_with_input(BenchmarkId::new("encode", &config_name), &data, |b, data| { let erasure = Erasure::new(data_shards, parity_shards, data_size); b.iter(|| { let shards = erasure.encode_data(black_box(data)).unwrap(); black_box(shards); }); }); } group.finish(); } /// Benchmark: Memory usage patterns fn bench_memory_patterns(c: &mut Criterion) { let data_shards = 4; let parity_shards = 2; let block_size = 1024 * 1024; // 1MB block let mut group = c.benchmark_group("memory_patterns"); group.sample_size(10); group.measurement_time(Duration::from_secs(5)); // Test reusing the same Erasure instance group.bench_function("reuse_erasure_instance", |b| { let erasure = Erasure::new(data_shards, parity_shards, block_size); let data = generate_test_data(block_size); b.iter(|| { let shards = erasure.encode_data(black_box(&data)).unwrap(); black_box(shards); }); }); // Test creating new Erasure instance each time group.bench_function("new_erasure_instance", |b| { let data = generate_test_data(block_size); b.iter(|| { let erasure = Erasure::new(data_shards, parity_shards, block_size); let shards = erasure.encode_data(black_box(&data)).unwrap(); black_box(shards); }); }); group.finish(); } // Benchmark group configuration criterion_group!( benches, bench_encode_performance, bench_decode_performance, bench_shard_size_impact, bench_coding_configurations, bench_memory_patterns ); criterion_main!(benches);
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/event_name.rs
crates/targets/src/event_name.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::fmt; /// Error returned when parsing event name string fails. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ParseEventNameError(String); impl fmt::Display for ParseEventNameError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Invalid event name:{}", self.0) } } impl std::error::Error for ParseEventNameError {} /// Represents the type of event that occurs on the object. /// Based on AWS S3 event type and includes RustFS extension. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] pub enum EventName { // Single event type (values are 1-32 for compatible mask logic) ObjectAccessedGet = 1, ObjectAccessedGetRetention = 2, ObjectAccessedGetLegalHold = 3, ObjectAccessedHead = 4, ObjectAccessedAttributes = 5, ObjectCreatedCompleteMultipartUpload = 6, ObjectCreatedCopy = 7, ObjectCreatedPost = 8, ObjectCreatedPut = 9, ObjectCreatedPutRetention = 10, ObjectCreatedPutLegalHold = 11, ObjectCreatedPutTagging = 12, ObjectCreatedDeleteTagging = 13, ObjectRemovedDelete = 14, ObjectRemovedDeleteMarkerCreated = 15, ObjectRemovedDeleteAllVersions = 16, ObjectRemovedNoOP = 17, BucketCreated = 18, BucketRemoved = 19, ObjectReplicationFailed = 20, ObjectReplicationComplete = 21, ObjectReplicationMissedThreshold = 22, ObjectReplicationReplicatedAfterThreshold = 23, ObjectReplicationNotTracked = 24, ObjectRestorePost = 25, ObjectRestoreCompleted = 26, ObjectTransitionFailed = 27, ObjectTransitionComplete = 28, ScannerManyVersions = 29, // ObjectManyVersions corresponding to Go ScannerLargeVersions = 30, // ObjectLargeVersions corresponding to Go ScannerBigPrefix = 31, // PrefixManyFolders corresponding to Go LifecycleDelMarkerExpirationDelete = 32, // ILMDelMarkerExpirationDelete corresponding to Go // Compound "All" event type (no sequential value for mask) ObjectAccessedAll, ObjectCreatedAll, ObjectRemovedAll, ObjectReplicationAll, ObjectRestoreAll, ObjectTransitionAll, ObjectScannerAll, // New, from Go #[default] Everything, // New, from Go } // Single event type sequential array for Everything.expand() const SINGLE_EVENT_NAMES_IN_ORDER: [EventName; 32] = [ EventName::ObjectAccessedGet, EventName::ObjectAccessedGetRetention, EventName::ObjectAccessedGetLegalHold, EventName::ObjectAccessedHead, EventName::ObjectAccessedAttributes, EventName::ObjectCreatedCompleteMultipartUpload, EventName::ObjectCreatedCopy, EventName::ObjectCreatedPost, EventName::ObjectCreatedPut, EventName::ObjectCreatedPutRetention, EventName::ObjectCreatedPutLegalHold, EventName::ObjectCreatedPutTagging, EventName::ObjectCreatedDeleteTagging, EventName::ObjectRemovedDelete, EventName::ObjectRemovedDeleteMarkerCreated, EventName::ObjectRemovedDeleteAllVersions, EventName::ObjectRemovedNoOP, EventName::BucketCreated, EventName::BucketRemoved, EventName::ObjectReplicationFailed, EventName::ObjectReplicationComplete, EventName::ObjectReplicationMissedThreshold, EventName::ObjectReplicationReplicatedAfterThreshold, EventName::ObjectReplicationNotTracked, EventName::ObjectRestorePost, EventName::ObjectRestoreCompleted, EventName::ObjectTransitionFailed, EventName::ObjectTransitionComplete, EventName::ScannerManyVersions, EventName::ScannerLargeVersions, EventName::ScannerBigPrefix, EventName::LifecycleDelMarkerExpirationDelete, ]; const LAST_SINGLE_TYPE_VALUE: u32 = EventName::LifecycleDelMarkerExpirationDelete as u32; impl EventName { /// The parsed string is EventName. pub fn parse(s: &str) -> Result<Self, ParseEventNameError> { match s { "s3:BucketCreated:*" => Ok(EventName::BucketCreated), "s3:BucketRemoved:*" => Ok(EventName::BucketRemoved), "s3:ObjectAccessed:*" => Ok(EventName::ObjectAccessedAll), "s3:ObjectAccessed:Get" => Ok(EventName::ObjectAccessedGet), "s3:ObjectAccessed:GetRetention" => Ok(EventName::ObjectAccessedGetRetention), "s3:ObjectAccessed:GetLegalHold" => Ok(EventName::ObjectAccessedGetLegalHold), "s3:ObjectAccessed:Head" => Ok(EventName::ObjectAccessedHead), "s3:ObjectAccessed:Attributes" => Ok(EventName::ObjectAccessedAttributes), "s3:ObjectCreated:*" => Ok(EventName::ObjectCreatedAll), "s3:ObjectCreated:CompleteMultipartUpload" => Ok(EventName::ObjectCreatedCompleteMultipartUpload), "s3:ObjectCreated:Copy" => Ok(EventName::ObjectCreatedCopy), "s3:ObjectCreated:Post" => Ok(EventName::ObjectCreatedPost), "s3:ObjectCreated:Put" => Ok(EventName::ObjectCreatedPut), "s3:ObjectCreated:PutRetention" => Ok(EventName::ObjectCreatedPutRetention), "s3:ObjectCreated:PutLegalHold" => Ok(EventName::ObjectCreatedPutLegalHold), "s3:ObjectCreated:PutTagging" => Ok(EventName::ObjectCreatedPutTagging), "s3:ObjectCreated:DeleteTagging" => Ok(EventName::ObjectCreatedDeleteTagging), "s3:ObjectRemoved:*" => Ok(EventName::ObjectRemovedAll), "s3:ObjectRemoved:Delete" => Ok(EventName::ObjectRemovedDelete), "s3:ObjectRemoved:DeleteMarkerCreated" => Ok(EventName::ObjectRemovedDeleteMarkerCreated), "s3:ObjectRemoved:NoOP" => Ok(EventName::ObjectRemovedNoOP), "s3:ObjectRemoved:DeleteAllVersions" => Ok(EventName::ObjectRemovedDeleteAllVersions), "s3:LifecycleDelMarkerExpiration:Delete" => Ok(EventName::LifecycleDelMarkerExpirationDelete), "s3:Replication:*" => Ok(EventName::ObjectReplicationAll), "s3:Replication:OperationFailedReplication" => Ok(EventName::ObjectReplicationFailed), "s3:Replication:OperationCompletedReplication" => Ok(EventName::ObjectReplicationComplete), "s3:Replication:OperationMissedThreshold" => Ok(EventName::ObjectReplicationMissedThreshold), "s3:Replication:OperationReplicatedAfterThreshold" => Ok(EventName::ObjectReplicationReplicatedAfterThreshold), "s3:Replication:OperationNotTracked" => Ok(EventName::ObjectReplicationNotTracked), "s3:ObjectRestore:*" => Ok(EventName::ObjectRestoreAll), "s3:ObjectRestore:Post" => Ok(EventName::ObjectRestorePost), "s3:ObjectRestore:Completed" => Ok(EventName::ObjectRestoreCompleted), "s3:ObjectTransition:Failed" => Ok(EventName::ObjectTransitionFailed), "s3:ObjectTransition:Complete" => Ok(EventName::ObjectTransitionComplete), "s3:ObjectTransition:*" => Ok(EventName::ObjectTransitionAll), "s3:Scanner:ManyVersions" => Ok(EventName::ScannerManyVersions), "s3:Scanner:LargeVersions" => Ok(EventName::ScannerLargeVersions), "s3:Scanner:BigPrefix" => Ok(EventName::ScannerBigPrefix), // ObjectScannerAll and Everything cannot be parsed from strings, because the Go version also does not define their string representation. _ => Err(ParseEventNameError(s.to_string())), } } /// Returns a string representation of the event type. pub fn as_str(&self) -> &'static str { match self { EventName::BucketCreated => "s3:BucketCreated:*", EventName::BucketRemoved => "s3:BucketRemoved:*", EventName::ObjectAccessedAll => "s3:ObjectAccessed:*", EventName::ObjectAccessedGet => "s3:ObjectAccessed:Get", EventName::ObjectAccessedGetRetention => "s3:ObjectAccessed:GetRetention", EventName::ObjectAccessedGetLegalHold => "s3:ObjectAccessed:GetLegalHold", EventName::ObjectAccessedHead => "s3:ObjectAccessed:Head", EventName::ObjectAccessedAttributes => "s3:ObjectAccessed:Attributes", EventName::ObjectCreatedAll => "s3:ObjectCreated:*", EventName::ObjectCreatedCompleteMultipartUpload => "s3:ObjectCreated:CompleteMultipartUpload", EventName::ObjectCreatedCopy => "s3:ObjectCreated:Copy", EventName::ObjectCreatedPost => "s3:ObjectCreated:Post", EventName::ObjectCreatedPut => "s3:ObjectCreated:Put", EventName::ObjectCreatedPutTagging => "s3:ObjectCreated:PutTagging", EventName::ObjectCreatedDeleteTagging => "s3:ObjectCreated:DeleteTagging", EventName::ObjectCreatedPutRetention => "s3:ObjectCreated:PutRetention", EventName::ObjectCreatedPutLegalHold => "s3:ObjectCreated:PutLegalHold", EventName::ObjectRemovedAll => "s3:ObjectRemoved:*", EventName::ObjectRemovedDelete => "s3:ObjectRemoved:Delete", EventName::ObjectRemovedDeleteMarkerCreated => "s3:ObjectRemoved:DeleteMarkerCreated", EventName::ObjectRemovedNoOP => "s3:ObjectRemoved:NoOP", EventName::ObjectRemovedDeleteAllVersions => "s3:ObjectRemoved:DeleteAllVersions", EventName::LifecycleDelMarkerExpirationDelete => "s3:LifecycleDelMarkerExpiration:Delete", EventName::ObjectReplicationAll => "s3:Replication:*", EventName::ObjectReplicationFailed => "s3:Replication:OperationFailedReplication", EventName::ObjectReplicationComplete => "s3:Replication:OperationCompletedReplication", EventName::ObjectReplicationNotTracked => "s3:Replication:OperationNotTracked", EventName::ObjectReplicationMissedThreshold => "s3:Replication:OperationMissedThreshold", EventName::ObjectReplicationReplicatedAfterThreshold => "s3:Replication:OperationReplicatedAfterThreshold", EventName::ObjectRestoreAll => "s3:ObjectRestore:*", EventName::ObjectRestorePost => "s3:ObjectRestore:Post", EventName::ObjectRestoreCompleted => "s3:ObjectRestore:Completed", EventName::ObjectTransitionAll => "s3:ObjectTransition:*", EventName::ObjectTransitionFailed => "s3:ObjectTransition:Failed", EventName::ObjectTransitionComplete => "s3:ObjectTransition:Complete", EventName::ScannerManyVersions => "s3:Scanner:ManyVersions", EventName::ScannerLargeVersions => "s3:Scanner:LargeVersions", EventName::ScannerBigPrefix => "s3:Scanner:BigPrefix", // Go's String() returns "" for ObjectScannerAll and Everything EventName::ObjectScannerAll => "s3:Scanner:*", // Follow the pattern in Go Expand EventName::Everything => "", // Go String() returns "" to unprocessed } } /// Returns the extended value of the abbreviation event type. pub fn expand(&self) -> Vec<Self> { match self { EventName::ObjectAccessedAll => vec![ EventName::ObjectAccessedGet, EventName::ObjectAccessedHead, EventName::ObjectAccessedGetRetention, EventName::ObjectAccessedGetLegalHold, EventName::ObjectAccessedAttributes, ], EventName::ObjectCreatedAll => vec![ EventName::ObjectCreatedCompleteMultipartUpload, EventName::ObjectCreatedCopy, EventName::ObjectCreatedPost, EventName::ObjectCreatedPut, EventName::ObjectCreatedPutRetention, EventName::ObjectCreatedPutLegalHold, EventName::ObjectCreatedPutTagging, EventName::ObjectCreatedDeleteTagging, ], EventName::ObjectRemovedAll => vec![ EventName::ObjectRemovedDelete, EventName::ObjectRemovedDeleteMarkerCreated, EventName::ObjectRemovedNoOP, EventName::ObjectRemovedDeleteAllVersions, ], EventName::ObjectReplicationAll => vec![ EventName::ObjectReplicationFailed, EventName::ObjectReplicationComplete, EventName::ObjectReplicationNotTracked, EventName::ObjectReplicationMissedThreshold, EventName::ObjectReplicationReplicatedAfterThreshold, ], EventName::ObjectRestoreAll => vec![EventName::ObjectRestorePost, EventName::ObjectRestoreCompleted], EventName::ObjectTransitionAll => vec![EventName::ObjectTransitionFailed, EventName::ObjectTransitionComplete], EventName::ObjectScannerAll => vec![ // New EventName::ScannerManyVersions, EventName::ScannerLargeVersions, EventName::ScannerBigPrefix, ], EventName::Everything => { // New SINGLE_EVENT_NAMES_IN_ORDER.to_vec() } // A single type returns to itself directly _ => vec![*self], } } /// Returns the mask of type. /// The compound "All" type will be expanded. pub fn mask(&self) -> u64 { let value = *self as u32; if value > 0 && value <= LAST_SINGLE_TYPE_VALUE { // It's a single type 1u64 << (value - 1) } else { // It's a compound type let mut mask = 0u64; for n in self.expand() { mask |= n.mask(); // Recursively call mask } mask } } } impl fmt::Display for EventName { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.as_str()) } } /// Convert to `EventName` according to string impl From<&str> for EventName { fn from(event_str: &str) -> Self { EventName::parse(event_str).unwrap_or_else(|e| panic!("{}", e)) } } impl serde::ser::Serialize for EventName { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::ser::Serializer, { serializer.serialize_str(self.as_str()) } } impl<'de> serde::de::Deserialize<'de> for EventName { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::de::Deserializer<'de>, { let s = String::deserialize(deserializer)?; let s = Self::parse(&s).map_err(serde::de::Error::custom)?; Ok(s) } } #[cfg(test)] mod tests { use super::*; // test serialization #[test] fn test_event_name_serialization_and_deserialization() { struct TestCase { event: EventName, serialized_str: &'static str, } let test_cases = vec![ TestCase { event: EventName::BucketCreated, serialized_str: "\"s3:BucketCreated:*\"", }, TestCase { event: EventName::ObjectCreatedAll, serialized_str: "\"s3:ObjectCreated:*\"", }, TestCase { event: EventName::ObjectCreatedPut, serialized_str: "\"s3:ObjectCreated:Put\"", }, ]; for case in &test_cases { let serialized = serde_json::to_string(&case.event); assert!(serialized.is_ok(), "Serialization failed for `{}`", case.serialized_str); assert_eq!(serialized.unwrap(), case.serialized_str); let deserialized = serde_json::from_str::<EventName>(case.serialized_str); assert!(deserialized.is_ok(), "Deserialization failed for `{}`", case.serialized_str); assert_eq!(deserialized.unwrap(), case.event); } } #[test] fn test_invalid_event_name_deserialization() { let invalid_str = "\"s3:InvalidEvent:Test\""; let deserialized = serde_json::from_str::<EventName>(invalid_str); assert!(deserialized.is_err(), "Deserialization should fail for invalid event name"); // Serializing EventName::Everything produces an empty string, but deserializing an empty string should fail. let event_name = EventName::Everything; let serialized_str = "\"\""; let serialized = serde_json::to_string(&event_name); assert!(serialized.is_ok(), "Serialization failed for `{serialized_str}`"); assert_eq!(serialized.unwrap(), serialized_str); let deserialized = serde_json::from_str::<EventName>(serialized_str); assert!(deserialized.is_err(), "Deserialization should fail for empty string"); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/lib.rs
crates/targets/src/lib.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. pub mod arn; mod check; pub mod error; mod event_name; pub mod store; pub mod target; pub use check::check_mqtt_broker_available; pub use error::{StoreError, TargetError}; pub use event_name::EventName; use serde::{Deserialize, Serialize}; pub use target::Target; /// Represents a log of events for sending to targets #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct TargetLog<E> { /// The event name pub event_name: EventName, /// The object key pub key: String, /// The list of events pub records: Vec<E>, }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/store.rs
crates/targets/src/store.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::error::StoreError; use rustfs_config::DEFAULT_LIMIT; use rustfs_config::notify::{COMPRESS_EXT, DEFAULT_EXT}; use serde::{Serialize, de::DeserializeOwned}; use snap::raw::{Decoder, Encoder}; use std::sync::{Arc, RwLock}; use std::{ collections::HashMap, marker::PhantomData, path::PathBuf, time::{SystemTime, UNIX_EPOCH}, }; use tracing::{debug, warn}; use uuid::Uuid; /// Represents a key for an entry in the store #[derive(Debug, Clone)] pub struct Key { /// The name of the key (UUID) pub name: String, /// The file extension for the entry pub extension: String, /// The number of items in the entry (for batch storage) pub item_count: usize, /// Whether the entry is compressed pub compress: bool, } impl Key { /// Converts the key to a string (filename) pub fn to_key_string(&self) -> String { let name_part = if self.item_count > 1 { format!("{}:{}", self.item_count, self.name) } else { self.name.clone() }; let mut file_name = name_part; if !self.extension.is_empty() { file_name.push_str(&self.extension); } if self.compress { file_name.push_str(COMPRESS_EXT); } file_name } } impl std::fmt::Display for Key { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let name_part = if self.item_count > 1 { format!("{}:{}", self.item_count, self.name) } else { self.name.clone() }; let mut file_name = name_part; if !self.extension.is_empty() { file_name.push_str(&self.extension); } if self.compress { file_name.push_str(COMPRESS_EXT); } write!(f, "{file_name}") } } /// Parses a string into a Key pub fn parse_key(s: &str) -> Key { debug!("Parsing key: {}", s); let mut name = s.to_string(); let mut extension = String::new(); let mut item_count = 1; let mut compress = false; // Check for compressed suffixes if name.ends_with(COMPRESS_EXT) { compress = true; name = name[..name.len() - COMPRESS_EXT.len()].to_string(); } // Number of batch items parsed if let Some(colon_pos) = name.find(':') && let Ok(count) = name[..colon_pos].parse::<usize>() { item_count = count; name = name[colon_pos + 1..].to_string(); } // Resolve extension if let Some(dot_pos) = name.rfind('.') { extension = name[dot_pos..].to_string(); name = name[..dot_pos].to_string(); } debug!( "Parsed key - name: {}, extension: {}, item_count: {}, compress: {}", name, extension, item_count, compress ); Key { name, extension, item_count, compress, } } /// Trait for a store that can store and retrieve items of type T pub trait Store<T>: Send + Sync where T: Send + Sync + 'static + Clone + Serialize, { /// The error type for the store type Error; /// The key type for the store type Key; /// Opens the store fn open(&self) -> Result<(), Self::Error>; /// Stores a single item fn put(&self, item: Arc<T>) -> Result<Self::Key, Self::Error>; /// Stores multiple items in a single batch fn put_multiple(&self, items: Vec<T>) -> Result<Self::Key, Self::Error>; /// Retrieves a single item by key fn get(&self, key: &Self::Key) -> Result<T, Self::Error>; /// Retrieves multiple items by key fn get_multiple(&self, key: &Self::Key) -> Result<Vec<T>, Self::Error>; /// Deletes an item by key fn del(&self, key: &Self::Key) -> Result<(), Self::Error>; /// Lists all keys in the store fn list(&self) -> Vec<Self::Key>; /// Returns the number of items in the store fn len(&self) -> usize; /// Returns true if the store is empty fn is_empty(&self) -> bool; /// Clones the store into a boxed trait object fn boxed_clone(&self) -> Box<dyn Store<T, Error = Self::Error, Key = Self::Key> + Send + Sync>; } /// A store that uses the filesystem to persist events in a queue pub struct QueueStore<T> { entry_limit: u64, directory: PathBuf, file_ext: String, entries: Arc<RwLock<HashMap<String, i64>>>, // key -> modtime as unix nano _phantom: PhantomData<T>, } impl<T> Clone for QueueStore<T> { fn clone(&self) -> Self { QueueStore { entry_limit: self.entry_limit, directory: self.directory.clone(), file_ext: self.file_ext.clone(), entries: Arc::clone(&self.entries), _phantom: PhantomData, } } } impl<T: Serialize + DeserializeOwned + Send + Sync> QueueStore<T> { /// Creates a new QueueStore pub fn new(directory: impl Into<PathBuf>, limit: u64, ext: &str) -> Self { let file_ext = if ext.is_empty() { DEFAULT_EXT } else { ext }; QueueStore { directory: directory.into(), entry_limit: if limit == 0 { DEFAULT_LIMIT } else { limit }, file_ext: file_ext.to_string(), entries: Arc::new(RwLock::new(HashMap::with_capacity(limit as usize))), _phantom: PhantomData, } } /// Returns the full path for a key fn file_path(&self, key: &Key) -> PathBuf { self.directory.join(key.to_string()) } /// Reads a file for the given key fn read_file(&self, key: &Key) -> Result<Vec<u8>, StoreError> { let path = self.file_path(key); debug!("Reading file for key: {},path: {}", key.to_string(), path.display()); let data = std::fs::read(&path).map_err(|e| { if e.kind() == std::io::ErrorKind::NotFound { StoreError::NotFound } else { StoreError::Io(e) } })?; if data.is_empty() { return Err(StoreError::NotFound); } if key.compress { let mut decoder = Decoder::new(); decoder .decompress_vec(&data) .map_err(|e| StoreError::Compression(e.to_string())) } else { Ok(data) } } /// Writes data to a file for the given key fn write_file(&self, key: &Key, data: &[u8]) -> Result<(), StoreError> { let path = self.file_path(key); // Create directory if it doesn't exist if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(StoreError::Io)?; } let data = if key.compress { let mut encoder = Encoder::new(); encoder .compress_vec(data) .map_err(|e| StoreError::Compression(e.to_string()))? } else { data.to_vec() }; std::fs::write(&path, &data).map_err(StoreError::Io)?; let modified = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos() as i64; let mut entries = self .entries .write() .map_err(|_| StoreError::Internal("Failed to acquire write lock on entries".to_string()))?; entries.insert(key.to_string(), modified); debug!("Wrote event to store: {}", key.to_string()); Ok(()) } } impl<T> Store<T> for QueueStore<T> where T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static, { type Error = StoreError; type Key = Key; fn open(&self) -> Result<(), Self::Error> { std::fs::create_dir_all(&self.directory).map_err(StoreError::Io)?; let entries = std::fs::read_dir(&self.directory).map_err(StoreError::Io)?; // Get the write lock to update the internal state let mut entries_map = self .entries .write() .map_err(|_| StoreError::Internal("Failed to acquire write lock on entries".to_string()))?; for entry in entries { let entry = entry.map_err(StoreError::Io)?; let metadata = entry.metadata().map_err(StoreError::Io)?; if metadata.is_file() { let modified = metadata.modified().map_err(StoreError::Io)?; let unix_nano = modified.duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos() as i64; let file_name = entry.file_name().to_string_lossy().to_string(); entries_map.insert(file_name, unix_nano); } } debug!("Opened store at: {:?}", self.directory); Ok(()) } fn put(&self, item: Arc<T>) -> Result<Self::Key, Self::Error> { // Check storage limits { let entries = self .entries .read() .map_err(|_| StoreError::Internal("Failed to acquire read lock on entries".to_string()))?; if entries.len() as u64 >= self.entry_limit { return Err(StoreError::LimitExceeded); } } let uuid = Uuid::new_v4(); let key = Key { name: uuid.to_string(), extension: self.file_ext.clone(), item_count: 1, compress: true, }; let data = serde_json::to_vec(&*item).map_err(|e| StoreError::Serialization(e.to_string()))?; self.write_file(&key, &data)?; Ok(key) } fn put_multiple(&self, items: Vec<T>) -> Result<Self::Key, Self::Error> { // Check storage limits { let entries = self .entries .read() .map_err(|_| StoreError::Internal("Failed to acquire read lock on entries".to_string()))?; if entries.len() as u64 >= self.entry_limit { return Err(StoreError::LimitExceeded); } } if items.is_empty() { // Or return an error, or a special key? return Err(StoreError::Internal("Cannot put_multiple with empty items list".to_string())); } let uuid = Uuid::new_v4(); let key = Key { name: uuid.to_string(), extension: self.file_ext.clone(), item_count: items.len(), compress: true, }; // Serialize all items into a single Vec<u8> // This current approach for get_multiple/put_multiple assumes items are concatenated JSON objects. // This might be problematic for deserialization if not handled carefully. // A better approach for multiple items might be to store them as a JSON array `Vec<T>`. // For now, sticking to current logic of concatenating. let mut buffer = Vec::new(); for item in items { // If items are Vec<Event>, and Event is large, this could be inefficient. // The current get_multiple deserializes one by one. let item_data = serde_json::to_vec(&item).map_err(|e| StoreError::Serialization(e.to_string()))?; buffer.extend_from_slice(&item_data); // If using JSON array: buffer = serde_json::to_vec(&items)? } self.write_file(&key, &buffer)?; Ok(key) } fn get(&self, key: &Self::Key) -> Result<T, Self::Error> { if key.item_count != 1 { return Err(StoreError::Internal(format!( "get() called on a batch key ({} items), use get_multiple()", key.item_count ))); } let items = self.get_multiple(key)?; items.into_iter().next().ok_or(StoreError::NotFound) } fn get_multiple(&self, key: &Self::Key) -> Result<Vec<T>, Self::Error> { debug!("Reading items from store for key: {}", key.to_string()); let data = self.read_file(key)?; if data.is_empty() { return Err(StoreError::Deserialization("Cannot deserialize empty data".to_string())); } let mut items = Vec::with_capacity(key.item_count); // let mut deserializer = serde_json::Deserializer::from_slice(&data); // while let Ok(item) = serde::Deserialize::deserialize(&mut deserializer) { // items.push(item); // } // This deserialization logic assumes multiple JSON objects are simply concatenated in the file. // This is fragile. It's better to store a JSON array `[item1, item2, ...]` // or use a streaming deserializer that can handle multiple top-level objects if that's the format. // For now, assuming serde_json::Deserializer::from_slice can handle this if input is well-formed. let mut deserializer = serde_json::Deserializer::from_slice(&data).into_iter::<T>(); for _ in 0..key.item_count { match deserializer.next() { Some(Ok(item)) => items.push(item), Some(Err(e)) => { return Err(StoreError::Deserialization(format!("Failed to deserialize item in batch: {e}"))); } None => { // Reached end of stream sooner than item_count if items.len() < key.item_count && !items.is_empty() { // Partial read warn!( "Expected {} items for key {}, but only found {}. Possible data corruption or incorrect item_count.", key.item_count, key.to_string(), items.len() ); // Depending on strictness, this could be an error. } else if items.is_empty() { // No items at all, but file existed return Err(StoreError::Deserialization(format!( "No items deserialized for key {key} though file existed." ))); } break; } } } if items.is_empty() && key.item_count > 0 { return Err(StoreError::Deserialization("No items found".to_string())); } Ok(items) } fn del(&self, key: &Self::Key) -> Result<(), Self::Error> { let path = self.file_path(key); std::fs::remove_file(&path).map_err(|e| { if e.kind() == std::io::ErrorKind::NotFound { // If file not found, still try to remove from entries map in case of inconsistency warn!( "File not found for key {} during del, but proceeding to remove from entries map.", key.to_string() ); StoreError::NotFound } else { StoreError::Io(e) } })?; // Get the write lock to update the internal state let mut entries = self .entries .write() .map_err(|_| StoreError::Internal("Failed to acquire write lock on entries".to_string()))?; if entries.remove(&key.to_string()).is_none() { // Key was not in the map, could be an inconsistency or already deleted. // This is not necessarily an error if the file deletion succeeded or was NotFound. debug!("Key {} not found in entries map during del, might have been already removed.", key); } debug!("Deleted event from store: {}", key.to_string()); Ok(()) } fn list(&self) -> Vec<Self::Key> { // Get the read lock to read the internal state let entries = match self.entries.read() { Ok(entries) => entries, Err(_) => { debug!("Failed to acquire read lock on entries for listing"); return Vec::new(); } }; let mut entries_vec: Vec<_> = entries.iter().collect(); // Sort by modtime (value in HashMap) to process oldest first entries_vec.sort_by(|a, b| a.1.cmp(b.1)); // Oldest first entries_vec.into_iter().map(|(k, _)| parse_key(k)).collect() } fn len(&self) -> usize { // Get the read lock to read the internal state match self.entries.read() { Ok(entries) => entries.len(), Err(_) => { debug!("Failed to acquire read lock on entries for len"); 0 } } } fn is_empty(&self) -> bool { self.len() == 0 } fn boxed_clone(&self) -> Box<dyn Store<T, Error = Self::Error, Key = Self::Key> + Send + Sync> { Box::new(self.clone()) as Box<dyn Store<T, Error = Self::Error, Key = Self::Key> + Send + Sync> } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/check.rs
crates/targets/src/check.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /// Check if MQTT Broker is available /// /// # Arguments /// * `broker_url` - URL of MQTT Broker, for example `mqtt://localhost:1883` /// * `topic` - Topic for testing connections /// # Returns /// * `Ok(())` - If the connection is successful /// * `Err(String)` - If the connection fails, contains an error message /// /// # Example /// ```rust,no_run /// #[tokio::main] /// async fn main() { /// let result = rustfs_targets::check_mqtt_broker_available("mqtt://localhost:1883", "test/topic").await; /// if result.is_ok() { /// println!("MQTT Broker is available"); /// } else { /// println!("MQTT Broker is not available: {}", result.err().unwrap()); /// } /// } /// ``` /// # Note /// Need to add `rumqttc` and `url` dependencies in `Cargo.toml` /// ```toml /// [dependencies] /// rumqttc = "0.25.0" /// url = "2.5.7" /// tokio = { version = "1", features = ["full"] } /// ``` /// pub async fn check_mqtt_broker_available(broker_url: &str, topic: &str) -> Result<(), String> { use rumqttc::{AsyncClient, MqttOptions, QoS}; let url = rustfs_utils::parse_url(broker_url).map_err(|e| format!("Broker URL parsing failed:{e}"))?; let url = url.url(); match url.scheme() { "tcp" | "ssl" | "ws" | "wss" | "mqtt" | "mqtts" | "tls" | "tcps" => {} _ => return Err("unsupported broker url scheme".to_string()), } let host = url.host_str().ok_or("Broker is missing host")?; let port = url.port().unwrap_or(1883); let mut mqtt_options = MqttOptions::new("rustfs_check", host, port); mqtt_options.set_keep_alive(std::time::Duration::from_secs(5)); let (client, mut eventloop) = AsyncClient::new(mqtt_options, 1); // Try to connect and subscribe client .subscribe(topic, QoS::AtLeastOnce) .await .map_err(|e| format!("MQTT subscription failed:{e}"))?; // Wait for eventloop to receive at least one event match tokio::time::timeout(std::time::Duration::from_secs(3), eventloop.poll()).await { Ok(Ok(_)) => Ok(()), Ok(Err(e)) => Err(format!("MQTT connection failed:{e}")), Err(_) => Err("MQTT connection timeout".to_string()), } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/error.rs
crates/targets/src/error.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::io; use thiserror::Error; /// Error types for the store #[derive(Debug, Error)] pub enum StoreError { #[error("I/O error: {0}")] Io(#[from] io::Error), #[error("Serialization error: {0}")] Serialization(String), #[error("Deserialization error: {0}")] Deserialization(String), #[error("Compression error: {0}")] Compression(String), #[error("Entry limit exceeded")] LimitExceeded, #[error("Entry not found")] NotFound, #[error("Invalid entry: {0}")] Internal(String), // Added internal error type } /// Error types for targets #[derive(Debug, Error)] pub enum TargetError { #[error("Storage error: {0}")] Storage(String), #[error("Network error: {0}")] Network(String), #[error("Request error: {0}")] Request(String), #[error("Timeout error: {0}")] Timeout(String), #[error("Authentication error: {0}")] Authentication(String), #[error("Configuration error: {0}")] Configuration(String), #[error("Encoding error: {0}")] Encoding(String), #[error("Serialization error: {0}")] Serialization(String), #[error("Target not connected")] NotConnected, #[error("Target initialization failed: {0}")] Initialization(String), #[error("Invalid ARN: {0}")] InvalidARN(String), #[error("Unknown error: {0}")] Unknown(String), #[error("Target is disabled")] Disabled, #[error("Configuration parsing error: {0}")] ParseError(String), #[error("Failed to save configuration: {0}")] SaveConfig(String), #[error("Server not initialized: {0}")] ServerNotInitialized(String), } impl From<url::ParseError> for TargetError { fn from(err: url::ParseError) -> Self { TargetError::Configuration(format!("URL parse error: {err}")) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/arn.rs
crates/targets/src/arn.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::TargetError; use rustfs_config::notify::{ARN_PREFIX, DEFAULT_ARN_PARTITION, DEFAULT_ARN_SERVICE}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; use std::str::FromStr; use thiserror::Error; #[derive(Debug, Error)] pub enum TargetIDError { #[error("Invalid TargetID format '{0}', expect 'ID:Name'")] InvalidFormat(String), } /// Target ID, used to identify notification targets #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] pub struct TargetID { pub id: String, pub name: String, } impl TargetID { pub fn new(id: String, name: String) -> Self { Self { id, name } } /// Convert to string representation pub fn to_id_string(&self) -> String { format!("{}:{}", self.id, self.name) } /// Create an ARN pub fn to_arn(&self, region: &str) -> ARN { ARN { target_id: self.clone(), region: region.to_string(), service: DEFAULT_ARN_SERVICE.to_string(), // Default Service partition: DEFAULT_ARN_PARTITION.to_string(), // Default partition } } } impl fmt::Display for TargetID { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}:{}", self.id, self.name) } } impl FromStr for TargetID { type Err = TargetIDError; fn from_str(s: &str) -> Result<Self, Self::Err> { let parts: Vec<&str> = s.splitn(2, ':').collect(); if parts.len() == 2 { Ok(TargetID { id: parts[0].to_string(), name: parts[1].to_string(), }) } else { Err(TargetIDError::InvalidFormat(s.to_string())) } } } impl Serialize for TargetID { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { serializer.serialize_str(&self.to_id_string()) } } impl<'de> Deserialize<'de> for TargetID { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; TargetID::from_str(&s).map_err(serde::de::Error::custom) } } #[derive(Debug, Error)] pub enum ArnError { #[error("Invalid ARN format '{0}'")] InvalidFormat(String), #[error("ARN component missing")] MissingComponents, } /// ARN - AWS resource name representation #[derive(Debug, Clone, Eq, PartialEq)] pub struct ARN { pub target_id: TargetID, pub region: String, // Service types, such as "sqs", "sns", "lambda", etc. This defaults to "sqs" to match the Go example. pub service: String, // Partitions such as "aws", "aws-cn", or customizations such as "rustfs", etc. pub partition: String, } impl ARN { pub fn new(target_id: TargetID, region: String) -> Self { ARN { target_id, region, service: DEFAULT_ARN_SERVICE.to_string(), // Default is sqs partition: DEFAULT_ARN_PARTITION.to_string(), // Default is rustfs partition } } /// Returns the string representation of ARN /// Returns the ARN string in the format "{ARN_PREFIX}:{region}:{target_id}" #[allow(clippy::inherent_to_string)] pub fn to_arn_string(&self) -> String { if self.target_id.id.is_empty() && self.target_id.name.is_empty() && self.region.is_empty() { return String::new(); } format!("{}:{}:{}", ARN_PREFIX, self.region, self.target_id.to_id_string()) } /// Parsing ARN from string pub fn parse(s: &str) -> Result<Self, TargetError> { if !s.starts_with(ARN_PREFIX) { return Err(TargetError::InvalidARN(s.to_string())); } let tokens: Vec<&str> = s.split(':').collect(); if tokens.len() != 6 { return Err(TargetError::InvalidARN(s.to_string())); } if tokens[4].is_empty() || tokens[5].is_empty() { return Err(TargetError::InvalidARN(s.to_string())); } Ok(ARN { region: tokens[3].to_string(), target_id: TargetID { id: tokens[4].to_string(), name: tokens[5].to_string(), }, service: tokens[2].to_string(), // Service Type partition: tokens[1].to_string(), // Partition }) } } impl fmt::Display for ARN { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.target_id.id.is_empty() && self.target_id.name.is_empty() && self.region.is_empty() { // Returns an empty string if all parts are empty return Ok(()); } write!( f, "arn:{}:{}:{}:{}:{}", self.partition, self.service, self.region, self.target_id.id, self.target_id.name ) } } impl FromStr for ARN { type Err = ArnError; fn from_str(s: &str) -> Result<Self, Self::Err> { let parts: Vec<&str> = s.split(':').collect(); if parts.len() < 6 { return Err(ArnError::InvalidFormat(s.to_string())); } if parts[0] != "arn" { return Err(ArnError::InvalidFormat(s.to_string())); } let partition = parts[1].to_string(); let service = parts[2].to_string(); let region = parts[3].to_string(); let id = parts[4].to_string(); let name = parts[5..].join(":"); // The name section may contain colons, although this is not usually the case in SQS ARN if id.is_empty() || name.is_empty() { return Err(ArnError::MissingComponents); } Ok(ARN { target_id: TargetID { id, name }, region, service, partition, }) } } // Serialization implementation impl Serialize for ARN { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { serializer.serialize_str(&self.to_string()) } } impl<'de> Deserialize<'de> for ARN { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { // deserializer.deserialize_str(ARNVisitor) let s = String::deserialize(deserializer)?; if s.is_empty() { // Handle an empty ARN string, for example, creating an empty or default Arn instance // Or return an error based on business logic // Here we create an empty TargetID and region Arn return Ok(ARN { target_id: TargetID { id: String::new(), name: String::new(), }, region: String::new(), service: DEFAULT_ARN_SERVICE.to_string(), partition: DEFAULT_ARN_PARTITION.to_string(), }); } ARN::from_str(&s).map_err(serde::de::Error::custom) } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/target/webhook.rs
crates/targets/src/target/webhook.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::{ StoreError, Target, TargetLog, arn::TargetID, error::TargetError, store::{Key, QueueStore, Store}, target::{ChannelTargetType, EntityTarget, TargetType}, }; use async_trait::async_trait; use reqwest::{Client, StatusCode, Url}; use rustfs_config::audit::AUDIT_STORE_EXTENSION; use rustfs_config::notify::NOTIFY_STORE_EXTENSION; use serde::Serialize; use serde::de::DeserializeOwned; use std::{ path::PathBuf, sync::{ Arc, atomic::{AtomicBool, Ordering}, }, time::Duration, }; use tokio::net::lookup_host; use tokio::sync::mpsc; use tracing::{debug, error, info, instrument}; /// Arguments for configuring a Webhook target #[derive(Debug, Clone)] pub struct WebhookArgs { /// Whether the target is enabled pub enable: bool, /// The endpoint URL to send events to pub endpoint: Url, /// The authorization token for the endpoint pub auth_token: String, /// The directory to store events in case of failure pub queue_dir: String, /// The maximum number of events to store pub queue_limit: u64, /// The client certificate for TLS (PEM format) pub client_cert: String, /// The client key for TLS (PEM format) pub client_key: String, /// the target type pub target_type: TargetType, } impl WebhookArgs { /// WebhookArgs verification method pub fn validate(&self) -> Result<(), TargetError> { if !self.enable { return Ok(()); } if self.endpoint.as_str().is_empty() { return Err(TargetError::Configuration("endpoint empty".to_string())); } if !self.queue_dir.is_empty() { let path = std::path::Path::new(&self.queue_dir); if !path.is_absolute() { return Err(TargetError::Configuration("webhook queueDir path should be absolute".to_string())); } } if !self.client_cert.is_empty() && self.client_key.is_empty() || self.client_cert.is_empty() && !self.client_key.is_empty() { return Err(TargetError::Configuration("cert and key must be specified as a pair".to_string())); } Ok(()) } } /// A target that sends events to a webhook pub struct WebhookTarget<E> where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { id: TargetID, args: WebhookArgs, http_client: Arc<Client>, // Add Send + Sync constraints to ensure thread safety store: Option<Box<dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync>>, initialized: AtomicBool, addr: String, cancel_sender: mpsc::Sender<()>, } impl<E> WebhookTarget<E> where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { /// Clones the WebhookTarget, creating a new instance with the same configuration pub fn clone_box(&self) -> Box<dyn Target<E> + Send + Sync> { Box::new(WebhookTarget { id: self.id.clone(), args: self.args.clone(), http_client: Arc::clone(&self.http_client), store: self.store.as_ref().map(|s| s.boxed_clone()), initialized: AtomicBool::new(self.initialized.load(Ordering::SeqCst)), addr: self.addr.clone(), cancel_sender: self.cancel_sender.clone(), }) } /// Creates a new WebhookTarget #[instrument(skip(args), fields(target_id = %id))] pub fn new(id: String, args: WebhookArgs) -> Result<Self, TargetError> { // First verify the parameters args.validate()?; // Create a TargetID let target_id = TargetID::new(id, ChannelTargetType::Webhook.as_str().to_string()); // Build HTTP client let mut client_builder = Client::builder() .timeout(Duration::from_secs(30)) .user_agent(rustfs_utils::get_user_agent(rustfs_utils::ServiceType::Basis)); // Supplementary certificate processing logic if !args.client_cert.is_empty() && !args.client_key.is_empty() { // Add client certificate let cert = std::fs::read(&args.client_cert) .map_err(|e| TargetError::Configuration(format!("Failed to read client cert: {e}")))?; let key = std::fs::read(&args.client_key) .map_err(|e| TargetError::Configuration(format!("Failed to read client key: {e}")))?; let identity = reqwest::Identity::from_pem(&[cert, key].concat()) .map_err(|e| TargetError::Configuration(format!("Failed to create identity: {e}")))?; client_builder = client_builder.identity(identity); } let http_client = Arc::new( client_builder .build() .map_err(|e| TargetError::Configuration(format!("Failed to build HTTP client: {e}")))?, ); // Build storage let queue_store = if !args.queue_dir.is_empty() { let queue_dir = PathBuf::from(&args.queue_dir).join(format!("rustfs-{}-{}", ChannelTargetType::Webhook.as_str(), target_id.id)); let extension = match args.target_type { TargetType::AuditLog => AUDIT_STORE_EXTENSION, TargetType::NotifyEvent => NOTIFY_STORE_EXTENSION, }; let store = QueueStore::<EntityTarget<E>>::new(queue_dir, args.queue_limit, extension); if let Err(e) = store.open() { error!("Failed to open store for Webhook target {}: {}", target_id.id, e); return Err(TargetError::Storage(format!("{e}"))); } // Make sure that the Store trait implemented by QueueStore matches the expected error type Some(Box::new(store) as Box<dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync>) } else { None }; // resolved address let addr = { let host = args.endpoint.host_str().unwrap_or("localhost"); let port = args .endpoint .port() .unwrap_or_else(|| if args.endpoint.scheme() == "https" { 443 } else { 80 }); format!("{host}:{port}") }; // Create a cancel channel let (cancel_sender, _) = mpsc::channel(1); info!(target_id = %target_id.id, "Webhook target created"); Ok(WebhookTarget { id: target_id, args, http_client, store: queue_store, initialized: AtomicBool::new(false), addr, cancel_sender, }) } async fn init(&self) -> Result<(), TargetError> { // Use CAS operations to ensure thread-safe initialization if !self.initialized.load(Ordering::SeqCst) { // Check the connection match self.is_active().await { Ok(true) => { info!("Webhook target {} is active", self.id); } Ok(false) => { return Err(TargetError::NotConnected); } Err(e) => { error!("Failed to check if Webhook target {} is active: {}", self.id, e); return Err(e); } } self.initialized.store(true, Ordering::SeqCst); info!("Webhook target {} initialized", self.id); } Ok(()) } async fn send(&self, event: &EntityTarget<E>) -> Result<(), TargetError> { info!("Webhook Sending event to webhook target: {}", self.id); // Decode form-urlencoded object name let object_name = crate::target::decode_object_name(&event.object_name)?; let key = format!("{}/{}", event.bucket_name, object_name); let log = TargetLog { event_name: event.event_name, key, records: vec![event.data.clone()], }; let data = serde_json::to_vec(&log).map_err(|e| TargetError::Serialization(format!("Failed to serialize event: {e}")))?; // Vec<u8> Convert to String let data_string = String::from_utf8(data.clone()) .map_err(|e| TargetError::Encoding(format!("Failed to convert event data to UTF-8: {e}")))?; debug!("Sending event to webhook target: {}, event log: {}", self.id, data_string); // build request let mut req_builder = self .http_client .post(self.args.endpoint.as_str()) .header("Content-Type", "application/json"); if !self.args.auth_token.is_empty() { // Split auth_token string to check if the authentication type is included let tokens: Vec<&str> = self.args.auth_token.split_whitespace().collect(); match tokens.len() { 2 => { // Already include authentication type and token, such as "Bearer token123" req_builder = req_builder.header("Authorization", &self.args.auth_token); } 1 => { // Only tokens, need to add "Bearer" prefix req_builder = req_builder.header("Authorization", format!("Bearer {}", self.args.auth_token)); } _ => { // Empty string or other situations, no authentication header is added } } } // Send a request let resp = req_builder.body(data).send().await.map_err(|e| { if e.is_timeout() || e.is_connect() { TargetError::NotConnected } else { TargetError::Request(format!("Failed to send request: {e}")) } })?; let status = resp.status(); if status.is_success() { debug!("Event sent to webhook target: {}", self.id); Ok(()) } else if status == StatusCode::FORBIDDEN { Err(TargetError::Authentication(format!( "{} returned '{}', please check if your auth token is correctly set", self.args.endpoint, status ))) } else { Err(TargetError::Request(format!( "{} returned '{}', please check your endpoint configuration", self.args.endpoint, status ))) } } } #[async_trait] impl<E> Target<E> for WebhookTarget<E> where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { fn id(&self) -> TargetID { self.id.clone() } async fn is_active(&self) -> Result<bool, TargetError> { let socket_addr = lookup_host(&self.addr) .await .map_err(|e| TargetError::Network(format!("Failed to resolve host: {e}")))? .next() .ok_or_else(|| TargetError::Network("No address found".to_string()))?; debug!("is_active socket addr: {},target id:{}", socket_addr, self.id.id); match tokio::time::timeout(Duration::from_secs(5), tokio::net::TcpStream::connect(socket_addr)).await { Ok(Ok(_)) => { debug!("Connection to {} is active", self.addr); Ok(true) } Ok(Err(e)) => { debug!("Connection to {} failed: {}", self.addr, e); if e.kind() == std::io::ErrorKind::ConnectionRefused { Err(TargetError::NotConnected) } else { Err(TargetError::Network(format!("Connection failed: {e}"))) } } Err(_) => Err(TargetError::Timeout("Connection timed out".to_string())), } } async fn save(&self, event: Arc<EntityTarget<E>>) -> Result<(), TargetError> { if let Some(store) = &self.store { // Call the store method directly, no longer need to acquire the lock store .put(event) .map_err(|e| TargetError::Storage(format!("Failed to save event to store: {e}")))?; debug!("Event saved to store for target: {}", self.id); Ok(()) } else { match self.init().await { Ok(_) => (), Err(e) => { error!("Failed to initialize Webhook target {}: {}", self.id.id, e); return Err(TargetError::NotConnected); } } self.send(&event).await } } async fn send_from_store(&self, key: Key) -> Result<(), TargetError> { debug!("Sending event from store for target: {}", self.id); match self.init().await { Ok(_) => { debug!("Event sent to store for target: {}", self.name()); } Err(e) => { error!("Failed to initialize Webhook target {}: {}", self.id.id, e); return Err(TargetError::NotConnected); } } let store = self .store .as_ref() .ok_or_else(|| TargetError::Configuration("No store configured".to_string()))?; // Get events directly from the store, no longer need to acquire locks let event = match store.get(&key) { Ok(event) => event, Err(StoreError::NotFound) => return Ok(()), Err(e) => { return Err(TargetError::Storage(format!("Failed to get event from store: {e}"))); } }; if let Err(e) = self.send(&event).await { if let TargetError::NotConnected = e { return Err(TargetError::NotConnected); } return Err(e); } // Use the immutable reference of the store to delete the event content corresponding to the key debug!("Deleting event from store for target: {}, key:{}, start", self.id, key.to_string()); match store.del(&key) { Ok(_) => debug!("Event deleted from store for target: {}, key:{}, end", self.id, key.to_string()), Err(e) => { error!("Failed to delete event from store: {}", e); return Err(TargetError::Storage(format!("Failed to delete event from store: {e}"))); } } debug!("Event sent from store and deleted for target: {}", self.id); Ok(()) } async fn close(&self) -> Result<(), TargetError> { // Send cancel signal to background tasks let _ = self.cancel_sender.try_send(()); info!("Webhook target closed: {}", self.id); Ok(()) } fn store(&self) -> Option<&(dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync)> { // Returns the reference to the internal store self.store.as_deref() } fn clone_dyn(&self) -> Box<dyn Target<E> + Send + Sync> { self.clone_box() } async fn init(&self) -> Result<(), TargetError> { // If the target is disabled, return to success directly if !self.is_enabled() { debug!("Webhook target {} is disabled, skipping initialization", self.id); return Ok(()); } // Use existing initialization logic WebhookTarget::init(self).await } fn is_enabled(&self) -> bool { self.args.enable } } #[cfg(test)] mod tests { use crate::target::decode_object_name; use url::form_urlencoded; #[test] fn test_decode_object_name_with_spaces() { // Test case from the issue: "greeting file (2).csv" let object_name = "greeting file (2).csv"; // Simulate what event.rs does: form-urlencoded encoding (spaces become +) let form_encoded = form_urlencoded::byte_serialize(object_name.as_bytes()).collect::<String>(); assert_eq!(form_encoded, "greeting+file+%282%29.csv"); // Test the decode_object_name helper function let decoded = decode_object_name(&form_encoded).unwrap(); assert_eq!(decoded, object_name); assert!(!decoded.contains('+'), "Decoded string should not contain + symbols"); } #[test] fn test_decode_object_name_with_special_chars() { // Test with various special characters let test_cases = vec![ ("folder/greeting file (2).csv", "folder%2Fgreeting+file+%282%29.csv"), ("test file.txt", "test+file.txt"), ("my file (copy).pdf", "my+file+%28copy%29.pdf"), ("file with spaces and (parentheses).doc", "file+with+spaces+and+%28parentheses%29.doc"), ]; for (original, form_encoded) in test_cases { // Test the decode_object_name helper function let decoded = decode_object_name(form_encoded).unwrap(); assert_eq!(decoded, original, "Failed to decode: {}", form_encoded); } } #[test] fn test_decode_object_name_without_spaces() { // Test that files without spaces still work correctly let object_name = "simple-file.txt"; let form_encoded = form_urlencoded::byte_serialize(object_name.as_bytes()).collect::<String>(); let decoded = decode_object_name(&form_encoded).unwrap(); assert_eq!(decoded, object_name); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/target/mod.rs
crates/targets/src/target/mod.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::arn::TargetID; use crate::store::{Key, Store}; use crate::{EventName, StoreError, TargetError}; use async_trait::async_trait; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use std::fmt::Formatter; use std::sync::Arc; pub mod mqtt; pub mod webhook; /// Trait for notification targets #[async_trait] pub trait Target<E>: Send + Sync + 'static where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { /// Returns the ID of the target fn id(&self) -> TargetID; /// Returns the name of the target fn name(&self) -> String { self.id().to_string() } /// Checks if the target is active and reachable async fn is_active(&self) -> Result<bool, TargetError>; /// Saves an event (either sends it immediately or stores it for later) async fn save(&self, event: Arc<EntityTarget<E>>) -> Result<(), TargetError>; /// Sends an event from the store async fn send_from_store(&self, key: Key) -> Result<(), TargetError>; /// Closes the target and releases resources async fn close(&self) -> Result<(), TargetError>; /// Returns the store associated with the target (if any) fn store(&self) -> Option<&(dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync)>; /// Returns the type of the target fn clone_dyn(&self) -> Box<dyn Target<E> + Send + Sync>; /// Initialize the target, such as establishing a connection, etc. async fn init(&self) -> Result<(), TargetError> { // The default implementation is empty Ok(()) } /// Check if the target is enabled fn is_enabled(&self) -> bool; } #[derive(Debug, Serialize, Clone, Deserialize)] pub struct EntityTarget<E> where E: Send + Sync + 'static + Clone + Serialize, { pub object_name: String, pub bucket_name: String, pub event_name: EventName, pub data: E, } /// The `ChannelTargetType` enum represents the different types of channel Target /// used in the notification system. /// /// It includes: /// - `Webhook`: Represents a webhook target for sending notifications via HTTP requests. /// - `Kafka`: Represents a Kafka target for sending notifications to a Kafka topic. /// - `Mqtt`: Represents an MQTT target for sending notifications via MQTT protocol. /// /// Each variant has an associated string representation that can be used for serialization /// or logging purposes. /// The `as_str` method returns the string representation of the target type, /// and the `Display` implementation allows for easy formatting of the target type as a string. /// /// example usage: /// ```rust /// use rustfs_targets::target::ChannelTargetType; /// /// let target_type = ChannelTargetType::Webhook; /// assert_eq!(target_type.as_str(), "webhook"); /// println!("Target type: {}", target_type); /// ``` /// /// example output: /// Target type: webhook pub enum ChannelTargetType { Webhook, Kafka, Mqtt, } impl ChannelTargetType { pub fn as_str(&self) -> &'static str { match self { ChannelTargetType::Webhook => "webhook", ChannelTargetType::Kafka => "kafka", ChannelTargetType::Mqtt => "mqtt", } } } impl std::fmt::Display for ChannelTargetType { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ChannelTargetType::Webhook => write!(f, "webhook"), ChannelTargetType::Kafka => write!(f, "kafka"), ChannelTargetType::Mqtt => write!(f, "mqtt"), } } } pub fn parse_bool(value: &str) -> Result<bool, TargetError> { match value.to_lowercase().as_str() { "true" | "on" | "yes" | "1" => Ok(true), "false" | "off" | "no" | "0" => Ok(false), _ => Err(TargetError::ParseError(format!("Unable to parse boolean: {value}"))), } } /// `TargetType` enum represents the type of target in the notification system. #[derive(Debug, Clone)] pub enum TargetType { AuditLog, NotifyEvent, } impl TargetType { pub fn as_str(&self) -> &'static str { match self { TargetType::AuditLog => "audit_log", TargetType::NotifyEvent => "notify_event", } } } impl std::fmt::Display for TargetType { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { TargetType::AuditLog => write!(f, "audit_log"), TargetType::NotifyEvent => write!(f, "notify_event"), } } } /// Decodes a form-urlencoded object name to its original form. /// /// This function properly handles form-urlencoded strings where spaces are /// represented as `+` symbols. It first replaces `+` with spaces, then /// performs standard percent-decoding. /// /// # Arguments /// * `encoded` - The form-urlencoded string to decode /// /// # Returns /// The decoded string, or an error if decoding fails /// /// # Example /// ``` /// use rustfs_targets::target::decode_object_name; /// /// let encoded = "greeting+file+%282%29.csv"; /// let decoded = decode_object_name(encoded).unwrap(); /// assert_eq!(decoded, "greeting file (2).csv"); /// ``` pub fn decode_object_name(encoded: &str) -> Result<String, TargetError> { let replaced = encoded.replace("+", " "); urlencoding::decode(&replaced) .map(|s| s.into_owned()) .map_err(|e| TargetError::Encoding(format!("Failed to decode object key: {e}"))) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/targets/src/target/mqtt.rs
crates/targets/src/target/mqtt.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::{ StoreError, Target, TargetLog, arn::TargetID, error::TargetError, store::{Key, QueueStore, Store}, target::{ChannelTargetType, EntityTarget, TargetType}, }; use async_trait::async_trait; use rumqttc::{AsyncClient, ConnectionError, EventLoop, MqttOptions, Outgoing, Packet, QoS, mqttbytes::Error as MqttBytesError}; use serde::Serialize; use serde::de::DeserializeOwned; use std::sync::Arc; use std::{ path::PathBuf, sync::atomic::{AtomicBool, Ordering}, time::Duration, }; use tokio::sync::{Mutex, OnceCell, mpsc}; use tracing::{debug, error, info, instrument, trace, warn}; use url::Url; const DEFAULT_CONNECTION_TIMEOUT: Duration = Duration::from_secs(15); const EVENT_LOOP_POLL_TIMEOUT: Duration = Duration::from_secs(10); // For initial connection check in task /// Arguments for configuring an MQTT target #[derive(Debug, Clone)] pub struct MQTTArgs { /// Whether the target is enabled pub enable: bool, /// The broker URL pub broker: Url, /// The topic to publish to pub topic: String, /// The quality of service level pub qos: QoS, /// The username for the broker pub username: String, /// The password for the broker pub password: String, /// The maximum interval for reconnection attempts (Note: rumqttc has internal strategy) pub max_reconnect_interval: Duration, /// The keep alive interval pub keep_alive: Duration, /// The directory to store events in case of failure pub queue_dir: String, /// The maximum number of events to store pub queue_limit: u64, /// the target type pub target_type: TargetType, } impl MQTTArgs { pub fn validate(&self) -> Result<(), TargetError> { if !self.enable { return Ok(()); } match self.broker.scheme() { "ws" | "wss" | "tcp" | "ssl" | "tls" | "tcps" | "mqtt" | "mqtts" => {} _ => { return Err(TargetError::Configuration("unknown protocol in broker address".to_string())); } } if self.topic.is_empty() { return Err(TargetError::Configuration("MQTT topic cannot be empty".to_string())); } if !self.queue_dir.is_empty() { let path = std::path::Path::new(&self.queue_dir); if !path.is_absolute() { return Err(TargetError::Configuration("mqtt queueDir path should be absolute".to_string())); } if self.qos == QoS::AtMostOnce { return Err(TargetError::Configuration( "QoS should be AtLeastOnce (1) or ExactlyOnce (2) if queueDir is set".to_string(), )); } } Ok(()) } } struct BgTaskManager { init_cell: OnceCell<tokio::task::JoinHandle<()>>, cancel_tx: mpsc::Sender<()>, initial_cancel_rx: Mutex<Option<mpsc::Receiver<()>>>, } /// A target that sends events to an MQTT broker pub struct MQTTTarget<E> where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { id: TargetID, args: MQTTArgs, client: Arc<Mutex<Option<AsyncClient>>>, store: Option<Box<dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync>>, connected: Arc<AtomicBool>, bg_task_manager: Arc<BgTaskManager>, } impl<E> MQTTTarget<E> where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { /// Creates a new MQTTTarget #[instrument(skip(args), fields(target_id_as_string = %id))] pub fn new(id: String, args: MQTTArgs) -> Result<Self, TargetError> { args.validate()?; let target_id = TargetID::new(id.clone(), ChannelTargetType::Mqtt.as_str().to_string()); let queue_store = if !args.queue_dir.is_empty() { let base_path = PathBuf::from(&args.queue_dir); let unique_dir_name = format!("rustfs-{}-{}", ChannelTargetType::Mqtt.as_str(), target_id.id).replace(":", "_"); // Ensure the directory name is valid for filesystem let specific_queue_path = base_path.join(unique_dir_name); debug!(target_id = %target_id, path = %specific_queue_path.display(), "Initializing queue store for MQTT target"); let extension = match args.target_type { TargetType::AuditLog => rustfs_config::audit::AUDIT_STORE_EXTENSION, TargetType::NotifyEvent => rustfs_config::notify::NOTIFY_STORE_EXTENSION, }; let store = QueueStore::<EntityTarget<E>>::new(specific_queue_path, args.queue_limit, extension); if let Err(e) = store.open() { error!( target_id = %target_id, error = %e, "Failed to open store for MQTT target" ); return Err(TargetError::Storage(format!("{e}"))); } Some(Box::new(store) as Box<dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync>) } else { None }; let (cancel_tx, cancel_rx) = mpsc::channel(1); let bg_task_manager = Arc::new(BgTaskManager { init_cell: OnceCell::new(), cancel_tx, initial_cancel_rx: Mutex::new(Some(cancel_rx)), }); info!(target_id = %target_id, "MQTT target created"); Ok(MQTTTarget { id: target_id, args, client: Arc::new(Mutex::new(None)), store: queue_store, connected: Arc::new(AtomicBool::new(false)), bg_task_manager, }) } #[instrument(skip(self), fields(target_id = %self.id))] async fn init(&self) -> Result<(), TargetError> { if self.connected.load(Ordering::SeqCst) { debug!(target_id = %self.id, "Already connected."); return Ok(()); } let bg_task_manager = Arc::clone(&self.bg_task_manager); let client_arc = Arc::clone(&self.client); let connected_arc = Arc::clone(&self.connected); let target_id_clone = self.id.clone(); let args_clone = self.args.clone(); let _ = bg_task_manager .init_cell .get_or_try_init(|| async { debug!(target_id = %target_id_clone, "Initializing MQTT background task."); let host = args_clone.broker.host_str().unwrap_or("localhost"); let port = args_clone.broker.port().unwrap_or(1883); let mut mqtt_options = MqttOptions::new(format!("rustfs_notify_{}", uuid::Uuid::new_v4()), host, port); mqtt_options .set_keep_alive(args_clone.keep_alive) .set_max_packet_size(100 * 1024 * 1024, 100 * 1024 * 1024); // 100MB if !args_clone.username.is_empty() { mqtt_options.set_credentials(args_clone.username.clone(), args_clone.password.clone()); } let (new_client, eventloop) = AsyncClient::new(mqtt_options, 10); if let Err(e) = new_client.subscribe(&args_clone.topic, args_clone.qos).await { error!(target_id = %target_id_clone, error = %e, "Failed to subscribe to MQTT topic during init"); return Err(TargetError::Network(format!("MQTT subscribe failed: {e}"))); } let mut rx_guard = bg_task_manager.initial_cancel_rx.lock().await; let cancel_rx = rx_guard.take().ok_or_else(|| { error!(target_id = %target_id_clone, "MQTT cancel receiver already taken for task."); TargetError::Configuration("MQTT cancel receiver already taken for task".to_string()) })?; drop(rx_guard); *client_arc.lock().await = Some(new_client.clone()); info!(target_id = %target_id_clone, "Spawning MQTT event loop task."); let task_handle = tokio::spawn(run_mqtt_event_loop(eventloop, connected_arc.clone(), target_id_clone.clone(), cancel_rx)); Ok(task_handle) }) .await .map_err(|e: TargetError| { error!(target_id = %self.id, error = %e, "Failed to initialize MQTT background task"); e })?; debug!(target_id = %self.id, "MQTT background task initialized successfully."); match tokio::time::timeout(DEFAULT_CONNECTION_TIMEOUT, async { while !self.connected.load(Ordering::SeqCst) { if let Some(handle) = self.bg_task_manager.init_cell.get() && handle.is_finished() && !self.connected.load(Ordering::SeqCst) { error!(target_id = %self.id, "MQTT background task exited prematurely before connection was established."); return Err(TargetError::Network("MQTT background task exited prematurely".to_string())); } tokio::time::sleep(Duration::from_millis(100)).await; } debug!(target_id = %self.id, "MQTT target connected successfully."); Ok(()) }) .await { Ok(Ok(_)) => { info!(target_id = %self.id, "MQTT target initialized and connected."); Ok(()) } Ok(Err(e)) => Err(e), Err(_) => { error!(target_id = %self.id, "Timeout waiting for MQTT connection after task spawn."); Err(TargetError::Network("Timeout waiting for MQTT connection".to_string())) } } } #[instrument(skip(self, event), fields(target_id = %self.id))] async fn send(&self, event: &EntityTarget<E>) -> Result<(), TargetError> { let client_guard = self.client.lock().await; let client = client_guard .as_ref() .ok_or_else(|| TargetError::Configuration("MQTT client not initialized".to_string()))?; // Decode form-urlencoded object name let object_name = crate::target::decode_object_name(&event.object_name)?; let key = format!("{}/{}", event.bucket_name, object_name); let log = TargetLog { event_name: event.event_name, key, records: vec![event.clone()], }; let data = serde_json::to_vec(&log).map_err(|e| TargetError::Serialization(format!("Failed to serialize event: {e}")))?; let data_string = String::from_utf8(data.clone()) .map_err(|e| TargetError::Encoding(format!("Failed to convert event data to UTF-8: {e}")))?; debug!("Sending event to mqtt target: {}, event log: {}", self.id, data_string); client .publish(&self.args.topic, self.args.qos, false, data) .await .map_err(|e| { if e.to_string().contains("Connection") || e.to_string().contains("Timeout") { self.connected.store(false, Ordering::SeqCst); warn!(target_id = %self.id, error = %e, "Publish failed due to connection issue, marking as not connected."); TargetError::NotConnected } else { TargetError::Request(format!("Failed to publish message: {e}")) } })?; debug!(target_id = %self.id, topic = %self.args.topic, "Event published to MQTT topic"); Ok(()) } pub fn clone_target(&self) -> Box<dyn Target<E> + Send + Sync> { Box::new(MQTTTarget { id: self.id.clone(), args: self.args.clone(), client: self.client.clone(), store: self.store.as_ref().map(|s| s.boxed_clone()), connected: self.connected.clone(), bg_task_manager: self.bg_task_manager.clone(), }) } } async fn run_mqtt_event_loop( mut eventloop: EventLoop, connected_status: Arc<AtomicBool>, target_id: TargetID, mut cancel_rx: mpsc::Receiver<()>, ) { info!(target_id = %target_id, "MQTT event loop task started."); let mut initial_connection_established = false; loop { tokio::select! { biased; _ = cancel_rx.recv() => { info!(target_id = %target_id, "MQTT event loop task received cancellation signal. Shutting down."); break; } polled_event_result = async { if !initial_connection_established || !connected_status.load(Ordering::SeqCst) { match tokio::time::timeout(EVENT_LOOP_POLL_TIMEOUT, eventloop.poll()).await { Ok(Ok(event)) => Ok(event), Ok(Err(e)) => Err(e), Err(_) => { debug!(target_id = %target_id, "MQTT poll timed out (EVENT_LOOP_POLL_TIMEOUT) while not connected or status pending."); Err(ConnectionError::NetworkTimeout) } } } else { eventloop.poll().await } } => { match polled_event_result { Ok(notification) => { trace!(target_id = %target_id, event = ?notification, "Received MQTT event"); match notification { rumqttc::Event::Incoming(Packet::ConnAck(_conn_ack)) => { info!(target_id = %target_id, "MQTT connected (ConnAck)."); connected_status.store(true, Ordering::SeqCst); initial_connection_established = true; } rumqttc::Event::Incoming(Packet::Publish(publish)) => { debug!(target_id = %target_id, topic = %publish.topic, payload_len = publish.payload.len(), "Received message on subscribed topic."); } rumqttc::Event::Incoming(Packet::Disconnect) => { info!(target_id = %target_id, "Received Disconnect packet from broker. MQTT connection lost."); connected_status.store(false, Ordering::SeqCst); } rumqttc::Event::Incoming(Packet::PingResp) => { trace!(target_id = %target_id, "Received PingResp from broker. Connection is alive."); } rumqttc::Event::Incoming(Packet::SubAck(suback)) => { trace!(target_id = %target_id, "Received SubAck for pkid: {}", suback.pkid); } rumqttc::Event::Incoming(Packet::PubAck(puback)) => { trace!(target_id = %target_id, "Received PubAck for pkid: {}", puback.pkid); } // Process other incoming packet types as needed (PubRec, PubRel, PubComp, UnsubAck) rumqttc::Event::Outgoing(Outgoing::Disconnect) => { info!(target_id = %target_id, "MQTT outgoing disconnect initiated by client."); connected_status.store(false, Ordering::SeqCst); } rumqttc::Event::Outgoing(Outgoing::PingReq) => { trace!(target_id = %target_id, "Client sent PingReq to broker."); } // Other Outgoing events (Subscribe, Unsubscribe, Publish) usually do not need to handle connection status here, // Because they are actions initiated by the client. _ => { // Log other unspecified MQTT events that are not handled, which helps debug trace!(target_id = %target_id, "Unhandled or generic MQTT event: {:?}", notification); } } } Err(e) => { connected_status.store(false, Ordering::SeqCst); error!(target_id = %target_id, error = %e, "Error from MQTT event loop poll"); if matches!(e, ConnectionError::NetworkTimeout) && (!initial_connection_established || !connected_status.load(Ordering::SeqCst)) { warn!(target_id = %target_id, "Timeout during initial poll or pending state, will retry."); continue; } if matches!(e, ConnectionError::Io(_) | ConnectionError::NetworkTimeout | ConnectionError::ConnectionRefused(_) | ConnectionError::Tls(_) ) { warn!(target_id = %target_id, error = %e, "MQTT connection error. Relying on rumqttc for reconnection if applicable."); } // Here you can decide whether to break loops based on the error type. // For example, for some unrecoverable errors. if is_fatal_mqtt_error(&e) { error!(target_id = %target_id, error = %e, "Fatal MQTT error, terminating event loop."); break; } // rumqttc's eventloop.poll() may return Err and terminate after some errors, // Or it will handle reconnection internally. To continue here will make select! wait again. // If the error is temporary and rumqttc is handling reconnection, poll() should eventually succeed or return a different error again. // Sleep briefly to avoid busy cycles in case of rapid failure. tokio::time::sleep(Duration::from_secs(1)).await; } } } } } connected_status.store(false, Ordering::SeqCst); info!(target_id = %target_id, "MQTT event loop task finished."); } /// Check whether the given MQTT connection error should be considered a fatal error, /// For fatal errors, the event loop should terminate. fn is_fatal_mqtt_error(err: &ConnectionError) -> bool { match err { // If the client request has been processed all (for example, AsyncClient is dropped), the event loop can end. ConnectionError::RequestsDone => true, // Check for the underlying MQTT status error ConnectionError::MqttState(state_err) => { // The type of state_err is &rumqttc::StateError match state_err { // If StateError is caused by deserialization issues, check the underlying MqttBytesError rumqttc::StateError::Deserialization(mqtt_bytes_err) => { // The type of mqtt_bytes_err is &rumqttc::mqttbytes::Error matches!( mqtt_bytes_err, MqttBytesError::InvalidProtocol // Invalid agreement | MqttBytesError::InvalidProtocolLevel(_) // Invalid protocol level | MqttBytesError::IncorrectPacketFormat // Package format is incorrect | MqttBytesError::InvalidPacketType(_) // Invalid package type | MqttBytesError::MalformedPacket // Package format error | MqttBytesError::PayloadTooLong // Too long load | MqttBytesError::PayloadSizeLimitExceeded(_) // Load size limit exceeded | MqttBytesError::TopicNotUtf8 // Topic Non-UTF-8 (Serious Agreement Violation) ) } // Others that are fatal StateError variants rumqttc::StateError::InvalidState // The internal state machine is in invalid state | rumqttc::StateError::WrongPacket // Agreement Violation: Unexpected Data Packet Received | rumqttc::StateError::Unsolicited(_) // Agreement Violation: Unsolicited ACK Received | rumqttc::StateError::CollisionTimeout // Agreement Violation (if this stage occurs) | rumqttc::StateError::EmptySubscription // Agreement violation (if this stage occurs) => true, // Other StateErrors (such as Io, AwaitPingResp, CollisionTimeout) are not considered deadly here. // They may be processed internally by rumqttc or upgraded to other ConnectionError types. _ => false, } } // Other types of ConnectionErrors (such as Io, Tls, NetworkTimeout, ConnectionRefused, NotConnAck, etc.) // It is usually considered temporary, or the reconnect logic inside rumqttc will be processed. _ => false, } } #[async_trait] impl<E> Target<E> for MQTTTarget<E> where E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned, { fn id(&self) -> TargetID { self.id.clone() } #[instrument(skip(self), fields(target_id = %self.id))] async fn is_active(&self) -> Result<bool, TargetError> { debug!(target_id = %self.id, "Checking if MQTT target is active."); if self.client.lock().await.is_none() && !self.connected.load(Ordering::SeqCst) { // Check if the background task is running and has not panicked if let Some(handle) = self.bg_task_manager.init_cell.get() && handle.is_finished() { error!(target_id = %self.id, "MQTT background task has finished, possibly due to an error. Target is not active."); return Err(TargetError::Network("MQTT background task terminated".to_string())); } debug!(target_id = %self.id, "MQTT client not yet initialized or task not running/connected."); return Err(TargetError::Configuration( "MQTT client not available or not initialized/connected".to_string(), )); } if self.connected.load(Ordering::SeqCst) { debug!(target_id = %self.id, "MQTT target is active (connected flag is true)."); Ok(true) } else { debug!(target_id = %self.id, "MQTT target is not connected (connected flag is false)."); Err(TargetError::NotConnected) } } #[instrument(skip(self, event), fields(target_id = %self.id))] async fn save(&self, event: Arc<EntityTarget<E>>) -> Result<(), TargetError> { if let Some(store) = &self.store { debug!(target_id = %self.id, "Event saved to store start"); // If store is configured, ONLY put the event into the store. // Do NOT send it directly here. match store.put(event.clone()) { Ok(_) => { debug!(target_id = %self.id, "Event saved to store for MQTT target successfully."); Ok(()) } Err(e) => { error!(target_id = %self.id, error = %e, "Failed to save event to store"); return Err(TargetError::Storage(format!("Failed to save event to store: {e}"))); } } } else { if !self.is_enabled() { return Err(TargetError::Disabled); } if !self.connected.load(Ordering::SeqCst) { warn!(target_id = %self.id, "Attempting to send directly but not connected; trying to init."); // Call the struct's init method, not the trait's default match MQTTTarget::init(self).await { Ok(_) => debug!(target_id = %self.id, "MQTT target initialized successfully."), Err(e) => { error!(target_id = %self.id, error = %e, "Failed to initialize MQTT target."); return Err(TargetError::NotConnected); } } if !self.connected.load(Ordering::SeqCst) { error!(target_id = %self.id, "Cannot save (send directly) as target is not active after init attempt."); return Err(TargetError::NotConnected); } } self.send(&event).await } } #[instrument(skip(self), fields(target_id = %self.id))] async fn send_from_store(&self, key: Key) -> Result<(), TargetError> { debug!(target_id = %self.id, ?key, "Attempting to send event from store with key."); if !self.is_enabled() { return Err(TargetError::Disabled); } if !self.connected.load(Ordering::SeqCst) { warn!(target_id = %self.id, "Not connected; trying to init before sending from store."); match MQTTTarget::init(self).await { Ok(_) => debug!(target_id = %self.id, "MQTT target initialized successfully."), Err(e) => { error!(target_id = %self.id, error = %e, "Failed to initialize MQTT target."); return Err(TargetError::NotConnected); } } if !self.connected.load(Ordering::SeqCst) { error!(target_id = %self.id, "Cannot send from store as target is not active after init attempt."); return Err(TargetError::NotConnected); } } let store = self .store .as_ref() .ok_or_else(|| TargetError::Configuration("No store configured".to_string()))?; let event = match store.get(&key) { Ok(event) => { debug!(target_id = %self.id, ?key, "Retrieved event from store for sending."); event } Err(StoreError::NotFound) => { // Assuming NotFound takes the key debug!(target_id = %self.id, ?key, "Event not found in store for sending."); return Ok(()); } Err(e) => { error!( target_id = %self.id, error = %e, "Failed to get event from store" ); return Err(TargetError::Storage(format!("Failed to get event from store: {e}"))); } }; debug!(target_id = %self.id, ?key, "Sending event from store."); if let Err(e) = self.send(&event).await { if matches!(e, TargetError::NotConnected) { warn!(target_id = %self.id, "Failed to send event from store: Not connected. Event remains in store."); return Err(TargetError::NotConnected); } error!(target_id = %self.id, error = %e, "Failed to send event from store with an unexpected error."); return Err(e); } debug!(target_id = %self.id, ?key, "Event sent from store successfully. deleting from store. "); match store.del(&key) { Ok(_) => { debug!(target_id = %self.id, ?key, "Event deleted from store after successful send.") } Err(StoreError::NotFound) => { debug!(target_id = %self.id, ?key, "Event already deleted from store."); } Err(e) => { error!(target_id = %self.id, error = %e, "Failed to delete event from store after send."); return Err(TargetError::Storage(format!("Failed to delete event from store: {e}"))); } } debug!(target_id = %self.id, ?key, "Event deleted from store."); Ok(()) } async fn close(&self) -> Result<(), TargetError> { info!(target_id = %self.id, "Attempting to close MQTT target."); if let Err(e) = self.bg_task_manager.cancel_tx.send(()).await { warn!(target_id = %self.id, error = %e, "Failed to send cancel signal to MQTT background task. It might have already exited."); } // Wait for the task to finish if it was initialized if let Some(_task_handle) = self.bg_task_manager.init_cell.get() { debug!(target_id = %self.id, "Waiting for MQTT background task to complete..."); // It's tricky to await here if close is called from a sync context or Drop // For async close, this is fine. Consider a timeout. // let _ = tokio::time::timeout(Duration::from_secs(5), task_handle.await).await; // If task_handle.await is directly used, ensure it's not awaited multiple times if close can be called multiple times. // For now, we rely on the signal and the task's self-termination. } if let Some(client_instance) = self.client.lock().await.take() { info!(target_id = %self.id, "Disconnecting MQTT client."); if let Err(e) = client_instance.disconnect().await { warn!(target_id = %self.id, error = %e, "Error during MQTT client disconnect."); } } self.connected.store(false, Ordering::SeqCst); info!(target_id = %self.id, "MQTT target close method finished."); Ok(()) } fn store(&self) -> Option<&(dyn Store<EntityTarget<E>, Error = StoreError, Key = Key> + Send + Sync)> { self.store.as_deref() } fn clone_dyn(&self) -> Box<dyn Target<E> + Send + Sync> { self.clone_target() } async fn init(&self) -> Result<(), TargetError> { if !self.is_enabled() { debug!(target_id = %self.id, "Target is disabled, skipping init."); return Ok(()); } // Call the internal init logic MQTTTarget::init(self).await } fn is_enabled(&self) -> bool { self.args.enable } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/lib.rs
crates/e2e_test/src/lib.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. mod reliant; // Common utilities for all E2E tests #[cfg(test)] pub mod common; #[cfg(test)] mod version_id_regression_test; // Data usage regression tests #[cfg(test)] mod data_usage_test; // KMS-specific test modules #[cfg(test)] mod kms; // Special characters in path test modules #[cfg(test)] mod special_chars_test; // Content-Encoding header preservation test #[cfg(test)] mod content_encoding_test; // Policy variables tests #[cfg(test)] mod policy; #[cfg(test)] mod protocols;
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/special_chars_test.rs
crates/e2e_test/src/special_chars_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! End-to-end tests for special characters in object paths //! //! This module tests the handling of various special characters in S3 object keys, //! including spaces, plus signs, percent signs, and other URL-encoded characters. //! //! ## Test Scenarios //! //! 1. **Spaces in paths**: `a f+/b/c/README.md` (encoded as `a%20f+/b/c/README.md`) //! 2. **Plus signs in paths**: `ES+net/file+name.txt` //! 3. **Mixed special characters**: Combinations of spaces, plus, percent, etc. //! 4. **Operations tested**: PUT, GET, LIST, DELETE #[cfg(test)] mod tests { use crate::common::{RustFSTestEnvironment, init_logging}; use aws_sdk_s3::Client; use aws_sdk_s3::primitives::ByteStream; use serial_test::serial; use tracing::{debug, info}; /// Helper function to create an S3 client for testing fn create_s3_client(env: &RustFSTestEnvironment) -> Client { env.create_s3_client() } /// Helper function to create a test bucket async fn create_bucket(client: &Client, bucket: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { match client.create_bucket().bucket(bucket).send().await { Ok(_) => { info!("Bucket {} created successfully", bucket); Ok(()) } Err(e) => { // Ignore if bucket already exists if e.to_string().contains("BucketAlreadyOwnedByYou") || e.to_string().contains("BucketAlreadyExists") { info!("Bucket {} already exists", bucket); Ok(()) } else { Err(Box::new(e)) } } } } /// Test PUT and GET with space character in path /// /// This reproduces Part A of the issue: /// ``` /// mc cp README.md "local/dummy/a%20f+/b/c/3/README.md" /// ``` #[tokio::test] #[serial] async fn test_object_with_space_in_path() { init_logging(); info!("Starting test: object with space in path"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-special-chars"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Test key with space: "a f+/b/c/3/README.md" // When URL-encoded by client: "a%20f+/b/c/3/README.md" let key = "a f+/b/c/3/README.md"; let content = b"Test content with space in path"; info!("Testing PUT object with key: {}", key); // PUT object let result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await; assert!(result.is_ok(), "Failed to PUT object with space in path: {:?}", result.err()); info!("βœ… PUT object with space in path succeeded"); // GET object info!("Testing GET object with key: {}", key); let result = client.get_object().bucket(bucket).key(key).send().await; assert!(result.is_ok(), "Failed to GET object with space in path: {:?}", result.err()); let output = result.unwrap(); let body_bytes = output.body.collect().await.unwrap().into_bytes(); assert_eq!(body_bytes.as_ref(), content, "Content mismatch"); info!("βœ… GET object with space in path succeeded"); // LIST objects with prefix containing space info!("Testing LIST objects with prefix: a f+/"); let result = client.list_objects_v2().bucket(bucket).prefix("a f+/").send().await; assert!(result.is_ok(), "Failed to LIST objects with space in prefix: {:?}", result.err()); let output = result.unwrap(); let contents = output.contents(); assert!(!contents.is_empty(), "LIST returned no objects"); assert!( contents.iter().any(|obj| obj.key().unwrap() == key), "Object with space not found in LIST results" ); info!("βœ… LIST objects with space in prefix succeeded"); // LIST objects with deeper prefix info!("Testing LIST objects with prefix: a f+/b/c/"); let result = client.list_objects_v2().bucket(bucket).prefix("a f+/b/c/").send().await; assert!(result.is_ok(), "Failed to LIST objects with deeper prefix: {:?}", result.err()); let output = result.unwrap(); let contents = output.contents(); assert!(!contents.is_empty(), "LIST with deeper prefix returned no objects"); info!("βœ… LIST objects with deeper prefix succeeded"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test PUT and GET with plus sign in path /// /// This reproduces Part B of the issue: /// ``` /// /test/data/org_main-org/dashboards/ES+net/LHC+Data+Challenge/firefly-details.json /// ``` #[tokio::test] #[serial] async fn test_object_with_plus_in_path() { init_logging(); info!("Starting test: object with plus sign in path"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-plus-chars"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Test key with plus signs let key = "dashboards/ES+net/LHC+Data+Challenge/firefly-details.json"; let content = b"Test content with plus signs in path"; info!("Testing PUT object with key: {}", key); // PUT object let result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await; assert!(result.is_ok(), "Failed to PUT object with plus in path: {:?}", result.err()); info!("βœ… PUT object with plus in path succeeded"); // GET object info!("Testing GET object with key: {}", key); let result = client.get_object().bucket(bucket).key(key).send().await; assert!(result.is_ok(), "Failed to GET object with plus in path: {:?}", result.err()); let output = result.unwrap(); let body_bytes = output.body.collect().await.unwrap().into_bytes(); assert_eq!(body_bytes.as_ref(), content, "Content mismatch"); info!("βœ… GET object with plus in path succeeded"); // LIST objects with prefix containing plus info!("Testing LIST objects with prefix: dashboards/ES+net/"); let result = client .list_objects_v2() .bucket(bucket) .prefix("dashboards/ES+net/") .send() .await; assert!(result.is_ok(), "Failed to LIST objects with plus in prefix: {:?}", result.err()); let output = result.unwrap(); let contents = output.contents(); assert!(!contents.is_empty(), "LIST returned no objects"); assert!( contents.iter().any(|obj| obj.key().unwrap() == key), "Object with plus not found in LIST results" ); info!("βœ… LIST objects with plus in prefix succeeded"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test with mixed special characters #[tokio::test] #[serial] async fn test_object_with_mixed_special_chars() { init_logging(); info!("Starting test: object with mixed special characters"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-mixed-chars"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Test various special characters let test_cases = vec![ ("path/with spaces/file.txt", b"Content 1" as &[u8]), ("path/with+plus/file.txt", b"Content 2"), ("path/with spaces+and+plus/file.txt", b"Content 3"), ("ES+net/folder name/file.txt", b"Content 4"), ]; for (key, content) in &test_cases { info!("Testing with key: {}", key); // PUT let result = client .put_object() .bucket(bucket) .key(*key) .body(ByteStream::from(content.to_vec())) .send() .await; assert!(result.is_ok(), "Failed to PUT object with key '{}': {:?}", key, result.err()); // GET let result = client.get_object().bucket(bucket).key(*key).send().await; assert!(result.is_ok(), "Failed to GET object with key '{}': {:?}", key, result.err()); let output = result.unwrap(); let body_bytes = output.body.collect().await.unwrap().into_bytes(); assert_eq!(body_bytes.as_ref(), *content, "Content mismatch for key '{key}'"); info!("βœ… PUT/GET succeeded for key: {}", key); } // LIST all objects let result = client.list_objects_v2().bucket(bucket).send().await; assert!(result.is_ok(), "Failed to LIST all objects"); let output = result.unwrap(); let contents = output.contents(); assert_eq!(contents.len(), test_cases.len(), "Number of objects mismatch"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test DELETE operation with special characters #[tokio::test] #[serial] async fn test_delete_object_with_special_chars() { init_logging(); info!("Starting test: DELETE object with special characters"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-delete-special"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); let key = "folder with spaces/ES+net/file.txt"; let content = b"Test content"; // PUT object client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await .expect("Failed to PUT object"); // Verify it exists let result = client.get_object().bucket(bucket).key(key).send().await; assert!(result.is_ok(), "Object should exist before DELETE"); // DELETE object info!("Testing DELETE object with key: {}", key); let result = client.delete_object().bucket(bucket).key(key).send().await; assert!(result.is_ok(), "Failed to DELETE object with special chars: {:?}", result.err()); info!("βœ… DELETE object succeeded"); // Verify it's deleted let result = client.get_object().bucket(bucket).key(key).send().await; assert!(result.is_err(), "Object should not exist after DELETE"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test exact scenario from the issue #[tokio::test] #[serial] async fn test_issue_scenario_exact() { init_logging(); info!("Starting test: Exact scenario from GitHub issue"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "dummy"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Exact key from issue: "a%20f+/b/c/3/README.md" // The decoded form should be: "a f+/b/c/3/README.md" let key = "a f+/b/c/3/README.md"; let content = b"README content"; info!("Reproducing exact issue scenario with key: {}", key); // Step 1: Upload file (like `mc cp README.md "local/dummy/a%20f+/b/c/3/README.md"`) let result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await; assert!(result.is_ok(), "Failed to upload file: {:?}", result.err()); info!("βœ… File uploaded successfully"); // Step 2: Navigate to folder (like navigating to "%20f+/" in UI) // This is equivalent to listing with prefix "a f+/" info!("Listing folder 'a f+/' (this should show subdirectories)"); let result = client .list_objects_v2() .bucket(bucket) .prefix("a f+/") .delimiter("/") .send() .await; assert!(result.is_ok(), "Failed to list folder: {:?}", result.err()); let output = result.unwrap(); debug!("List result: {:?}", output); // Should show "b/" as a common prefix (subdirectory) let common_prefixes = output.common_prefixes(); assert!( !common_prefixes.is_empty() || !output.contents().is_empty(), "Folder should show contents or subdirectories" ); info!("βœ… Folder listing succeeded"); // Step 3: List deeper (like `mc ls "local/dummy/a%20f+/b/c/3/"`) info!("Listing deeper folder 'a f+/b/c/3/'"); let result = client.list_objects_v2().bucket(bucket).prefix("a f+/b/c/3/").send().await; assert!(result.is_ok(), "Failed to list deep folder: {:?}", result.err()); let output = result.unwrap(); let contents = output.contents(); assert!(!contents.is_empty(), "Deep folder should show the file"); assert!(contents.iter().any(|obj| obj.key().unwrap() == key), "README.md should be in the list"); info!("βœ… Deep folder listing succeeded - file found"); // Cleanup env.stop_server(); info!("βœ… Exact issue scenario test completed successfully"); } /// Test HEAD object with special characters #[tokio::test] #[serial] async fn test_head_object_with_special_chars() { init_logging(); info!("Starting test: HEAD object with special characters"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-head-special"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); let key = "folder with spaces/ES+net/file.txt"; let content = b"Test content for HEAD"; // PUT object client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await .expect("Failed to PUT object"); info!("Testing HEAD object with key: {}", key); // HEAD object let result = client.head_object().bucket(bucket).key(key).send().await; assert!(result.is_ok(), "Failed to HEAD object with special chars: {:?}", result.err()); let output = result.unwrap(); assert_eq!(output.content_length().unwrap_or(0), content.len() as i64, "Content length mismatch"); info!("βœ… HEAD object with special characters succeeded"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test COPY object with special characters in both source and destination #[tokio::test] #[serial] async fn test_copy_object_with_special_chars() { init_logging(); info!("Starting test: COPY object with special characters"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-copy-special"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); let src_key = "source/folder with spaces/file.txt"; let dest_key = "dest/ES+net/copied file.txt"; let content = b"Test content for COPY"; // PUT source object client .put_object() .bucket(bucket) .key(src_key) .body(ByteStream::from_static(content)) .send() .await .expect("Failed to PUT source object"); info!("Testing COPY from '{}' to '{}'", src_key, dest_key); // COPY object let copy_source = format!("{bucket}/{src_key}"); let result = client .copy_object() .bucket(bucket) .key(dest_key) .copy_source(&copy_source) .send() .await; assert!(result.is_ok(), "Failed to COPY object with special chars: {:?}", result.err()); info!("βœ… COPY operation succeeded"); // Verify destination exists let result = client.get_object().bucket(bucket).key(dest_key).send().await; assert!(result.is_ok(), "Failed to GET copied object"); let output = result.unwrap(); let body_bytes = output.body.collect().await.unwrap().into_bytes(); assert_eq!(body_bytes.as_ref(), content, "Copied content mismatch"); info!("βœ… Copied object verified successfully"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test Unicode characters in object keys #[tokio::test] #[serial] async fn test_unicode_characters_in_path() { init_logging(); info!("Starting test: Unicode characters in object paths"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-unicode"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Test various Unicode characters let test_cases = vec![ ("ζ΅‹θ―•/ζ–‡δ»Ά.txt", b"Chinese characters" as &[u8]), ("γƒ†γ‚Ήγƒˆ/フゑむル.txt", b"Japanese characters"), ("ν…ŒμŠ€νŠΈ/파일.txt", b"Korean characters"), ("тСст/Ρ„Π°ΠΉΠ».txt", b"Cyrillic characters"), ("emoji/πŸ˜€/file.txt", b"Emoji in path"), ("mixed/ζ΅‹θ―• test/file.txt", b"Mixed languages"), ]; for (key, content) in &test_cases { info!("Testing Unicode key: {}", key); // PUT let result = client .put_object() .bucket(bucket) .key(*key) .body(ByteStream::from(content.to_vec())) .send() .await; assert!(result.is_ok(), "Failed to PUT object with Unicode key '{}': {:?}", key, result.err()); // GET let result = client.get_object().bucket(bucket).key(*key).send().await; assert!(result.is_ok(), "Failed to GET object with Unicode key '{}': {:?}", key, result.err()); let output = result.unwrap(); let body_bytes = output.body.collect().await.unwrap().into_bytes(); assert_eq!(body_bytes.as_ref(), *content, "Content mismatch for Unicode key '{key}'"); info!("βœ… PUT/GET succeeded for Unicode key: {}", key); } // LIST to verify all objects let result = client.list_objects_v2().bucket(bucket).send().await; assert!(result.is_ok(), "Failed to LIST objects with Unicode keys"); let output = result.unwrap(); let contents = output.contents(); assert_eq!(contents.len(), test_cases.len(), "Number of Unicode objects mismatch"); info!("βœ… All Unicode objects listed successfully"); // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test special characters in different parts of the path #[tokio::test] #[serial] async fn test_special_chars_in_different_path_positions() { init_logging(); info!("Starting test: Special characters in different path positions"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-path-positions"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Test special characters in different positions let test_cases = vec![ ("start with space/file.txt", b"Space at start" as &[u8]), ("folder/end with space /file.txt", b"Space at end of folder"), ("multiple spaces/file.txt", b"Multiple consecutive spaces"), ("folder/file with space.txt", b"Space in filename"), ("a+b/c+d/e+f.txt", b"Plus signs throughout"), ("a%b/c%d/e%f.txt", b"Percent signs throughout"), ("folder/!@#$%^&*()/file.txt", b"Multiple special chars"), ("(parentheses)/[brackets]/file.txt", b"Parentheses and brackets"), ("'quotes'/\"double\"/file.txt", b"Quote characters"), ]; for (key, content) in &test_cases { info!("Testing key: {}", key); // PUT let result = client .put_object() .bucket(bucket) .key(*key) .body(ByteStream::from(content.to_vec())) .send() .await; assert!(result.is_ok(), "Failed to PUT object with key '{}': {:?}", key, result.err()); // GET let result = client.get_object().bucket(bucket).key(*key).send().await; assert!(result.is_ok(), "Failed to GET object with key '{}': {:?}", key, result.err()); let output = result.unwrap(); let body_bytes = output.body.collect().await.unwrap().into_bytes(); assert_eq!(body_bytes.as_ref(), *content, "Content mismatch for key '{key}'"); info!("βœ… PUT/GET succeeded for key: {}", key); } // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test that control characters are properly rejected #[tokio::test] #[serial] async fn test_control_characters_rejected() { init_logging(); info!("Starting test: Control characters should be rejected"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-control-chars"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Test that control characters are rejected let invalid_keys = vec![ "file\0with\0null.txt", "file\nwith\nnewline.txt", "file\rwith\rcarriage.txt", "file\twith\ttab.txt", // Tab might be allowed, but let's test ]; for key in invalid_keys { info!("Testing rejection of control character in key: {:?}", key); let result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(b"test")) .send() .await; // Note: The validation happens on the server side, so we expect an error // For null byte, newline, and carriage return if key.contains('\0') || key.contains('\n') || key.contains('\r') { assert!(result.is_err(), "Control character should be rejected for key: {key:?}"); if let Err(e) = result { info!("βœ… Control character correctly rejected: {:?}", e); } } } // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test LIST with various special character prefixes #[tokio::test] #[serial] async fn test_list_with_special_char_prefixes() { init_logging(); info!("Starting test: LIST with special character prefixes"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-list-prefixes"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Create objects with various special characters let test_objects = vec![ "prefix with spaces/file1.txt", "prefix with spaces/file2.txt", "prefix+plus/file1.txt", "prefix+plus/file2.txt", "prefix%percent/file1.txt", "prefix%percent/file2.txt", ]; for key in &test_objects { client .put_object() .bucket(bucket) .key(*key) .body(ByteStream::from_static(b"test")) .send() .await .expect("Failed to PUT object"); } // Test LIST with different prefixes let prefix_tests = vec![ ("prefix with spaces/", 2), ("prefix+plus/", 2), ("prefix%percent/", 2), ("prefix", 6), // Should match all ]; for (prefix, expected_count) in prefix_tests { info!("Testing LIST with prefix: '{}'", prefix); let result = client.list_objects_v2().bucket(bucket).prefix(prefix).send().await; assert!(result.is_ok(), "Failed to LIST with prefix '{}': {:?}", prefix, result.err()); let output = result.unwrap(); let contents = output.contents(); assert_eq!( contents.len(), expected_count, "Expected {} objects with prefix '{}', got {}", expected_count, prefix, contents.len() ); info!("βœ… LIST with prefix '{}' returned {} objects", prefix, contents.len()); } // Cleanup env.stop_server(); info!("Test completed successfully"); } /// Test delimiter-based listing with special characters #[tokio::test] #[serial] async fn test_list_with_delimiter_and_special_chars() { init_logging(); info!("Starting test: LIST with delimiter and special characters"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-delimiter-special"; // Create bucket create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Create hierarchical structure with special characters let test_objects = vec![ "folder with spaces/subfolder1/file.txt", "folder with spaces/subfolder2/file.txt", "folder with spaces/file.txt", "folder+plus/subfolder1/file.txt", "folder+plus/file.txt", ]; for key in &test_objects { client .put_object() .bucket(bucket) .key(*key) .body(ByteStream::from_static(b"test")) .send() .await .expect("Failed to PUT object"); } // Test LIST with delimiter info!("Testing LIST with delimiter for 'folder with spaces/'"); let result = client .list_objects_v2() .bucket(bucket) .prefix("folder with spaces/") .delimiter("/") .send() .await; assert!(result.is_ok(), "Failed to LIST with delimiter"); let output = result.unwrap(); let common_prefixes = output.common_prefixes(); assert_eq!(common_prefixes.len(), 2, "Should have 2 common prefixes (subdirectories)"); info!("βœ… LIST with delimiter returned {} common prefixes", common_prefixes.len()); // Cleanup env.stop_server(); info!("Test completed successfully"); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/version_id_regression_test.rs
crates/e2e_test/src/version_id_regression_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Regression test for Issue #1066: Veeam VBR - S3 returned empty versionId //! //! This test verifies that: //! 1. PutObject returns version_id when versioning is enabled //! 2. CopyObject returns version_id when versioning is enabled //! 3. CompleteMultipartUpload returns version_id when versioning is enabled //! 4. Basic S3 operations still work correctly (no regression) //! 5. Operations on non-versioned buckets work as expected #[cfg(test)] mod tests { use crate::common::{RustFSTestEnvironment, init_logging}; use aws_sdk_s3::Client; use aws_sdk_s3::primitives::ByteStream; use aws_sdk_s3::types::{BucketVersioningStatus, CompletedMultipartUpload, CompletedPart, VersioningConfiguration}; use serial_test::serial; use tracing::info; fn create_s3_client(env: &RustFSTestEnvironment) -> Client { env.create_s3_client() } async fn create_bucket(client: &Client, bucket: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { match client.create_bucket().bucket(bucket).send().await { Ok(_) => { info!("βœ… Bucket {} created successfully", bucket); Ok(()) } Err(e) => { if e.to_string().contains("BucketAlreadyOwnedByYou") || e.to_string().contains("BucketAlreadyExists") { info!("ℹ️ Bucket {} already exists", bucket); Ok(()) } else { Err(Box::new(e)) } } } } async fn enable_versioning(client: &Client, bucket: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let versioning_config = VersioningConfiguration::builder() .status(BucketVersioningStatus::Enabled) .build(); client .put_bucket_versioning() .bucket(bucket) .versioning_configuration(versioning_config) .send() .await?; info!("βœ… Versioning enabled for bucket {}", bucket); Ok(()) } /// Test 1: PutObject should return version_id when versioning is enabled /// This directly addresses the Veeam issue from #1066 #[tokio::test] #[serial] async fn test_put_object_returns_version_id_with_versioning() { init_logging(); info!("πŸ§ͺ TEST: PutObject returns version_id with versioning enabled"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-put-version-id"; create_bucket(&client, bucket).await.expect("Failed to create bucket"); enable_versioning(&client, bucket).await.expect("Failed to enable versioning"); let key = "test-file.txt"; let content = b"Test content for version ID test"; info!("πŸ“€ Uploading object with key: {}", key); let result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await; assert!(result.is_ok(), "PutObject failed: {:?}", result.err()); let output = result.unwrap(); info!("πŸ“₯ PutObject response - version_id: {:?}", output.version_id); assert!( output.version_id.is_some(), "❌ FAILED: version_id should be present when versioning is enabled" ); assert!( !output.version_id.as_ref().unwrap().is_empty(), "❌ FAILED: version_id should not be empty" ); info!("βœ… PASSED: PutObject correctly returns version_id"); } /// Test 2: CopyObject should return version_id when versioning is enabled #[tokio::test] #[serial] async fn test_copy_object_returns_version_id_with_versioning() { init_logging(); info!("πŸ§ͺ TEST: CopyObject returns version_id with versioning enabled"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-copy-version-id"; create_bucket(&client, bucket).await.expect("Failed to create bucket"); enable_versioning(&client, bucket).await.expect("Failed to enable versioning"); let source_key = "source-file.txt"; let dest_key = "dest-file.txt"; let content = b"Content to copy"; // First, create source object client .put_object() .bucket(bucket) .key(source_key) .body(ByteStream::from_static(content)) .send() .await .expect("Failed to create source object"); info!("πŸ“€ Copying object from {} to {}", source_key, dest_key); let copy_result = client .copy_object() .bucket(bucket) .key(dest_key) .copy_source(format!("{}/{}", bucket, source_key)) .send() .await; assert!(copy_result.is_ok(), "CopyObject failed: {:?}", copy_result.err()); let output = copy_result.unwrap(); info!("πŸ“₯ CopyObject response - version_id: {:?}", output.version_id); assert!( output.version_id.is_some(), "❌ FAILED: version_id should be present when versioning is enabled" ); assert!( !output.version_id.as_ref().unwrap().is_empty(), "❌ FAILED: version_id should not be empty" ); info!("βœ… PASSED: CopyObject correctly returns version_id"); } /// Test 3: CompleteMultipartUpload should return version_id when versioning is enabled #[tokio::test] #[serial] async fn test_multipart_upload_returns_version_id_with_versioning() { init_logging(); info!("πŸ§ͺ TEST: CompleteMultipartUpload returns version_id with versioning enabled"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-multipart-version-id"; create_bucket(&client, bucket).await.expect("Failed to create bucket"); enable_versioning(&client, bucket).await.expect("Failed to enable versioning"); let key = "multipart-file.txt"; let content = b"Part 1 content for multipart upload test"; info!("πŸ“€ Creating multipart upload for key: {}", key); let create_result = client .create_multipart_upload() .bucket(bucket) .key(key) .send() .await .expect("Failed to create multipart upload"); let upload_id = create_result.upload_id().expect("No upload_id returned"); info!("πŸ“€ Uploading part 1"); let upload_part_result = client .upload_part() .bucket(bucket) .key(key) .upload_id(upload_id) .part_number(1) .body(ByteStream::from_static(content)) .send() .await .expect("Failed to upload part"); let etag = upload_part_result.e_tag().expect("No etag returned").to_string(); let completed_part = CompletedPart::builder().part_number(1).e_tag(etag).build(); let completed_upload = CompletedMultipartUpload::builder().parts(completed_part).build(); info!("πŸ“€ Completing multipart upload"); let complete_result = client .complete_multipart_upload() .bucket(bucket) .key(key) .upload_id(upload_id) .multipart_upload(completed_upload) .send() .await; assert!(complete_result.is_ok(), "CompleteMultipartUpload failed: {:?}", complete_result.err()); let output = complete_result.unwrap(); info!("πŸ“₯ CompleteMultipartUpload response - version_id: {:?}", output.version_id); assert!( output.version_id.is_some(), "❌ FAILED: version_id should be present when versioning is enabled" ); assert!( !output.version_id.as_ref().unwrap().is_empty(), "❌ FAILED: version_id should not be empty" ); info!("βœ… PASSED: CompleteMultipartUpload correctly returns version_id"); } /// Test 4: PutObject should NOT return version_id when versioning is NOT enabled /// This ensures we didn't break non-versioned buckets #[tokio::test] #[serial] async fn test_put_object_without_versioning() { init_logging(); info!("πŸ§ͺ TEST: PutObject behavior without versioning (no regression)"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-no-versioning"; create_bucket(&client, bucket).await.expect("Failed to create bucket"); // Note: NOT enabling versioning here let key = "test-file.txt"; let content = b"Test content without versioning"; info!("πŸ“€ Uploading object to non-versioned bucket"); let result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await; assert!(result.is_ok(), "PutObject failed: {:?}", result.err()); let output = result.unwrap(); info!("πŸ“₯ PutObject response - version_id: {:?}", output.version_id); // version_id can be None or Some("null") for non-versioned buckets info!("βœ… PASSED: PutObject works correctly without versioning"); } /// Test 5: Basic S3 operations still work correctly (no regression) #[tokio::test] #[serial] async fn test_basic_s3_operations_no_regression() { init_logging(); info!("πŸ§ͺ TEST: Basic S3 operations work correctly (no regression)"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "test-basic-operations"; create_bucket(&client, bucket).await.expect("Failed to create bucket"); enable_versioning(&client, bucket).await.expect("Failed to enable versioning"); let key = "test-basic-file.txt"; let content = b"Basic operations test content"; // Test PUT info!("πŸ“€ Testing PUT operation"); let put_result = client .put_object() .bucket(bucket) .key(key) .body(ByteStream::from_static(content)) .send() .await; assert!(put_result.is_ok(), "PUT operation failed"); let _version_id = put_result.unwrap().version_id; // Test GET info!("πŸ“₯ Testing GET operation"); let get_result = client.get_object().bucket(bucket).key(key).send().await; assert!(get_result.is_ok(), "GET operation failed"); let body = get_result.unwrap().body.collect().await.unwrap().to_vec(); assert_eq!(body, content, "Content mismatch after GET"); // Test HEAD info!("πŸ“‹ Testing HEAD operation"); let head_result = client.head_object().bucket(bucket).key(key).send().await; assert!(head_result.is_ok(), "HEAD operation failed"); // Test LIST info!("πŸ“ Testing LIST operation"); let list_result = client.list_objects_v2().bucket(bucket).send().await; assert!(list_result.is_ok(), "LIST operation failed"); let list_output = list_result.unwrap(); let objects = list_output.contents(); assert!(objects.iter().any(|obj| obj.key() == Some(key)), "Object not found in LIST"); // Test DELETE info!("πŸ—‘οΈ Testing DELETE operation"); let delete_result = client.delete_object().bucket(bucket).key(key).send().await; assert!(delete_result.is_ok(), "DELETE operation failed"); // Verify object is deleted (should return NoSuchKey or version marker) let get_after_delete = client.get_object().bucket(bucket).key(key).send().await; assert!( get_after_delete.is_err() || get_after_delete.unwrap().delete_marker == Some(true), "Object should be deleted or have delete marker" ); info!("βœ… PASSED: All basic S3 operations work correctly"); } /// Test 6: Veeam-specific scenario simulation /// Simulates the exact workflow that Veeam uses when backing up data #[tokio::test] #[serial] async fn test_veeam_backup_workflow_simulation() { init_logging(); info!("πŸ§ͺ TEST: Veeam VBR backup workflow simulation (Issue #1066)"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = create_s3_client(&env); let bucket = "veeam-backup-test"; create_bucket(&client, bucket).await.expect("Failed to create bucket"); enable_versioning(&client, bucket).await.expect("Failed to enable versioning"); // Veeam typically creates multiple objects in a backup session let test_paths = vec![ "Veeam/Backup/Clients/test-client-id/test-backup-id/CloudStg/Meta/Blocks/History/CheckpointHistory.dat", "Veeam/Backup/Clients/test-client-id/test-backup-id/Metadata/Lock/create.checkpoint/declare", ]; for path in test_paths { info!("πŸ“€ Simulating Veeam upload to: {}", path); let content = format!("Veeam backup data for {}", path); let put_result = client .put_object() .bucket(bucket) .key(path) .body(ByteStream::from(content.into_bytes())) .send() .await; assert!(put_result.is_ok(), "Veeam upload failed for path: {}", path); let output = put_result.unwrap(); info!("πŸ“₯ Response version_id: {:?}", output.version_id); assert!(output.version_id.is_some(), "❌ FAILED: Veeam expects version_id for path: {}", path); assert!( !output.version_id.as_ref().unwrap().is_empty(), "❌ FAILED: version_id should not be empty for path: {}", path ); info!("βœ… Veeam upload successful with version_id for: {}", path); } info!("βœ… PASSED: Veeam backup workflow simulation completed successfully"); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/data_usage_test.rs
crates/e2e_test/src/data_usage_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use aws_sdk_s3::primitives::ByteStream; use rustfs_common::data_usage::DataUsageInfo; use serial_test::serial; use crate::common::{RustFSTestEnvironment, TEST_BUCKET, awscurl_get, init_logging}; /// Regression test for data usage accuracy (issue #1012). /// Launches rustfs, writes 1000 objects, then asserts admin data usage reports the full count. #[tokio::test(flavor = "multi_thread")] #[serial] #[ignore = "Starts a rustfs server and requires awscurl; enable when running full E2E"] async fn data_usage_reports_all_objects() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); let mut env = RustFSTestEnvironment::new().await?; env.start_rustfs_server(vec![]).await?; let client = env.create_s3_client(); // Create bucket and upload objects client.create_bucket().bucket(TEST_BUCKET).send().await?; for i in 0..1000 { let key = format!("obj-{i:04}"); client .put_object() .bucket(TEST_BUCKET) .key(key) .body(ByteStream::from_static(b"hello-world")) .send() .await?; } // Query admin data usage API let url = format!("{}/rustfs/admin/v3/datausageinfo", env.url); let resp = awscurl_get(&url, &env.access_key, &env.secret_key).await?; let usage: DataUsageInfo = serde_json::from_str(&resp)?; // Assert total object count and per-bucket count are not truncated let bucket_usage = usage .buckets_usage .get(TEST_BUCKET) .cloned() .expect("bucket usage should exist"); assert!( usage.objects_total_count >= 1000, "total object count should be at least 1000, got {}", usage.objects_total_count ); assert!( bucket_usage.objects_count >= 1000, "bucket object count should be at least 1000, got {}", bucket_usage.objects_count ); env.stop_server(); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/common.rs
crates/e2e_test/src/common.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Common utilities for all E2E tests //! //! This module provides general-purpose functionality needed across //! different test modules, including: //! - RustFS server process management //! - AWS S3 client creation and configuration //! - Basic health checks and server readiness detection //! - Common test constants and utilities use aws_sdk_s3::config::{Credentials, Region}; use aws_sdk_s3::{Client, Config}; use std::path::PathBuf; use std::process::{Child, Command}; use std::sync::Once; use std::time::Duration; use tokio::fs; use tokio::net::TcpStream; use tokio::time::sleep; use tracing::{error, info, warn}; use uuid::Uuid; // Common constants for all E2E tests pub const DEFAULT_ACCESS_KEY: &str = "rustfsadmin"; pub const DEFAULT_SECRET_KEY: &str = "rustfsadmin"; pub const TEST_BUCKET: &str = "e2e-test-bucket"; pub fn workspace_root() -> PathBuf { let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); path.pop(); // e2e_test path.pop(); // crates path } /// Resolve the RustFS binary relative to the workspace. /// Always builds the binary to ensure it's up to date. pub fn rustfs_binary_path() -> PathBuf { if let Some(path) = std::env::var_os("CARGO_BIN_EXE_rustfs") { return PathBuf::from(path); } // Always build the binary to ensure it's up to date info!("Building RustFS binary to ensure it's up to date..."); build_rustfs_binary(); let mut binary_path = workspace_root(); binary_path.push("target"); let profile_dir = if cfg!(debug_assertions) { "debug" } else { "release" }; binary_path.push(profile_dir); binary_path.push(format!("rustfs{}", std::env::consts::EXE_SUFFIX)); info!("Using RustFS binary at {:?}", binary_path); binary_path } /// Build the RustFS binary using cargo fn build_rustfs_binary() { let workspace = workspace_root(); info!("Building RustFS binary from workspace: {:?}", workspace); let _profile = if cfg!(debug_assertions) { info!("Building in debug mode"); "dev" } else { info!("Building in release mode"); "release" }; let mut cmd = Command::new("cargo"); cmd.current_dir(&workspace).args(["build", "--bin", "rustfs"]); if !cfg!(debug_assertions) { cmd.arg("--release"); } info!( "Executing: cargo build --bin rustfs {}", if cfg!(debug_assertions) { "" } else { "--release" } ); let output = cmd.output().expect("Failed to execute cargo build command"); if !output.status.success() { let stderr = String::from_utf8_lossy(&output.stderr); panic!("Failed to build RustFS binary. Error: {stderr}"); } info!("βœ… RustFS binary built successfully"); } fn awscurl_binary_path() -> PathBuf { std::env::var_os("AWSCURL_PATH") .map(PathBuf::from) .unwrap_or_else(|| PathBuf::from("awscurl")) } // Global initialization static INIT: Once = Once::new(); /// Initialize tracing for all E2E tests pub fn init_logging() { INIT.call_once(|| { tracing_subscriber::fmt().with_env_filter("rustfs=info,e2e_test=debug").init(); }); } /// RustFS server environment for E2E testing pub struct RustFSTestEnvironment { pub temp_dir: String, pub address: String, pub url: String, pub access_key: String, pub secret_key: String, pub process: Option<Child>, } impl RustFSTestEnvironment { /// Create a new test environment with unique temporary directory and port pub async fn new() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> { let temp_dir = format!("/tmp/rustfs_e2e_test_{}", Uuid::new_v4()); fs::create_dir_all(&temp_dir).await?; // Use a unique port for each test environment let port = Self::find_available_port().await?; let address = format!("127.0.0.1:{port}"); let url = format!("http://{address}"); Ok(Self { temp_dir, address, url, access_key: DEFAULT_ACCESS_KEY.to_string(), secret_key: DEFAULT_SECRET_KEY.to_string(), process: None, }) } /// Create a new test environment with specific address pub async fn with_address(address: &str) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> { let temp_dir = format!("/tmp/rustfs_e2e_test_{}", Uuid::new_v4()); fs::create_dir_all(&temp_dir).await?; let url = format!("http://{address}"); Ok(Self { temp_dir, address: address.to_string(), url, access_key: DEFAULT_ACCESS_KEY.to_string(), secret_key: DEFAULT_SECRET_KEY.to_string(), process: None, }) } /// Find an available port for the test pub async fn find_available_port() -> Result<u16, Box<dyn std::error::Error + Send + Sync>> { use std::net::TcpListener; let listener = TcpListener::bind("127.0.0.1:0")?; let port = listener.local_addr()?.port(); drop(listener); Ok(port) } /// Kill any existing RustFS processes pub async fn cleanup_existing_processes(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { info!("Cleaning up any existing RustFS processes"); let output = Command::new("pkill").args(["-f", "rustfs"]).output(); if let Ok(output) = output && output.status.success() { info!("Killed existing RustFS processes"); sleep(Duration::from_millis(1000)).await; } Ok(()) } /// Start RustFS server with basic configuration pub async fn start_rustfs_server(&mut self, extra_args: Vec<&str>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { self.cleanup_existing_processes().await?; let mut args = vec![ "--address", &self.address, "--access-key", &self.access_key, "--secret-key", &self.secret_key, ]; // Add extra arguments args.extend(extra_args); // Add temp directory as the last argument args.push(&self.temp_dir); info!("Starting RustFS server with args: {:?}", args); let binary_path = rustfs_binary_path(); let process = Command::new(&binary_path).args(&args).spawn()?; self.process = Some(process); // Wait for server to be ready self.wait_for_server_ready().await?; Ok(()) } /// Wait for RustFS server to be ready by checking TCP connectivity pub async fn wait_for_server_ready(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { info!("Waiting for RustFS server to be ready on {}", self.address); for i in 0..30 { if TcpStream::connect(&self.address).await.is_ok() { info!("βœ… RustFS server is ready after {} attempts", i + 1); return Ok(()); } if i == 29 { return Err("RustFS server failed to become ready within 30 seconds".into()); } sleep(Duration::from_secs(1)).await; } Ok(()) } /// Create an AWS S3 client configured for this RustFS instance pub fn create_s3_client(&self) -> Client { let credentials = Credentials::new(&self.access_key, &self.secret_key, None, None, "e2e-test"); let config = Config::builder() .credentials_provider(credentials) .region(Region::new("us-east-1")) .endpoint_url(&self.url) .force_path_style(true) .behavior_version_latest() .build(); Client::from_conf(config) } /// Create test bucket pub async fn create_test_bucket(&self, bucket_name: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let s3_client = self.create_s3_client(); s3_client.create_bucket().bucket(bucket_name).send().await?; info!("Created test bucket: {}", bucket_name); Ok(()) } /// Delete test bucket pub async fn delete_test_bucket(&self, bucket_name: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let s3_client = self.create_s3_client(); let _ = s3_client.delete_bucket().bucket(bucket_name).send().await; info!("Deleted test bucket: {}", bucket_name); Ok(()) } /// Stop the RustFS server pub fn stop_server(&mut self) { if let Some(mut process) = self.process.take() { info!("Stopping RustFS server"); if let Err(e) = process.kill() { error!("Failed to kill RustFS process: {}", e); } else { let _ = process.wait(); info!("RustFS server stopped"); } } } } impl Drop for RustFSTestEnvironment { fn drop(&mut self) { self.stop_server(); // Clean up temp directory if let Err(e) = std::fs::remove_dir_all(&self.temp_dir) { warn!("Failed to clean up temp directory {}: {}", self.temp_dir, e); } } } /// Utility function to execute awscurl commands pub async fn execute_awscurl( url: &str, method: &str, body: Option<&str>, access_key: &str, secret_key: &str, ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { let mut args = vec![ "--fail-with-body", "--service", "s3", "--region", "us-east-1", "--access_key", access_key, "--secret_key", secret_key, "-X", method, url, ]; if let Some(body_content) = body { args.extend(&["-d", body_content]); } info!("Executing awscurl: {} {}", method, url); let awscurl_path = awscurl_binary_path(); let output = Command::new(&awscurl_path).args(&args).output()?; if !output.status.success() { let stderr = String::from_utf8_lossy(&output.stderr); let stdout = String::from_utf8_lossy(&output.stdout); return Err(format!("awscurl failed: stderr='{stderr}', stdout='{stdout}'").into()); } let response = String::from_utf8_lossy(&output.stdout).to_string(); Ok(response) } /// Helper function for POST requests pub async fn awscurl_post( url: &str, body: &str, access_key: &str, secret_key: &str, ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { execute_awscurl(url, "POST", Some(body), access_key, secret_key).await } /// Helper function for GET requests pub async fn awscurl_get( url: &str, access_key: &str, secret_key: &str, ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { execute_awscurl(url, "GET", None, access_key, secret_key).await } /// Helper function for PUT requests pub async fn awscurl_put( url: &str, body: &str, access_key: &str, secret_key: &str, ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { execute_awscurl(url, "PUT", Some(body), access_key, secret_key).await }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/content_encoding_test.rs
crates/e2e_test/src/content_encoding_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! End-to-end test for Content-Encoding header handling //! //! Tests that the Content-Encoding header is correctly stored during PUT //! and returned in GET/HEAD responses. This is important for clients that //! upload pre-compressed content and rely on the header for decompression. #[cfg(test)] mod tests { use crate::common::{RustFSTestEnvironment, init_logging}; use aws_sdk_s3::primitives::ByteStream; use serial_test::serial; use tracing::info; /// Verify Content-Encoding header roundtrips through PUT, GET, and HEAD operations #[tokio::test] #[serial] async fn test_content_encoding_roundtrip() { init_logging(); info!("Starting Content-Encoding roundtrip test"); let mut env = RustFSTestEnvironment::new().await.expect("Failed to create test environment"); env.start_rustfs_server(vec![]).await.expect("Failed to start RustFS"); let client = env.create_s3_client(); let bucket = "content-encoding-test"; let key = "logs/app.log.zst"; let content = b"2024-01-15 10:23:45 INFO Application started\n2024-01-15 10:23:46 DEBUG Loading config\n"; client .create_bucket() .bucket(bucket) .send() .await .expect("Failed to create bucket"); info!("Uploading object with Content-Encoding: zstd"); client .put_object() .bucket(bucket) .key(key) .content_type("text/plain") .content_encoding("zstd") .body(ByteStream::from_static(content)) .send() .await .expect("PUT failed"); info!("Verifying GET response includes Content-Encoding"); let get_resp = client.get_object().bucket(bucket).key(key).send().await.expect("GET failed"); assert_eq!(get_resp.content_encoding(), Some("zstd"), "GET should return Content-Encoding: zstd"); assert_eq!(get_resp.content_type(), Some("text/plain"), "GET should return correct Content-Type"); let body = get_resp.body.collect().await.unwrap().into_bytes(); assert_eq!(body.as_ref(), content, "Body content mismatch"); info!("Verifying HEAD response includes Content-Encoding"); let head_resp = client .head_object() .bucket(bucket) .key(key) .send() .await .expect("HEAD failed"); assert_eq!(head_resp.content_encoding(), Some("zstd"), "HEAD should return Content-Encoding: zstd"); assert_eq!(head_resp.content_type(), Some("text/plain"), "HEAD should return correct Content-Type"); env.stop_server(); } }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/kms/kms_comprehensive_test.rs
crates/e2e_test/src/kms/kms_comprehensive_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Comprehensive KMS integration tests //! //! This module contains comprehensive end-to-end tests that combine multiple KMS features //! and test real-world scenarios with mixed encryption types, large datasets, and //! complex workflows. use super::common::{ EncryptionType, LocalKMSTestEnvironment, MultipartTestConfig, create_sse_c_config, test_all_multipart_encryption_types, test_kms_key_management, test_multipart_upload_with_config, test_sse_c_encryption, test_sse_kms_encryption, test_sse_s3_encryption, }; use crate::common::{TEST_BUCKET, init_logging}; use serial_test::serial; use tokio::time::{Duration, sleep}; use tracing::info; /// Comprehensive test: Full KMS workflow with all encryption types #[tokio::test] #[serial] async fn test_comprehensive_kms_full_workflow() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("🏁 Start the KMS full-featured synthesis test"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Phase 1: Test all single encryption types info!("πŸ“‹ Phase 1: Test all single-file encryption types"); test_sse_s3_encryption(&s3_client, TEST_BUCKET).await?; test_sse_kms_encryption(&s3_client, TEST_BUCKET).await?; test_sse_c_encryption(&s3_client, TEST_BUCKET).await?; // Phase 2: Test KMS key management APIs info!("πŸ“‹ Phase 2: Test the KMS Key Management API"); test_kms_key_management(&kms_env.base_env.url, &kms_env.base_env.access_key, &kms_env.base_env.secret_key).await?; // Phase 3: Test all multipart encryption types info!("πŸ“‹ Phase 3: Test all shard upload encryption types"); test_all_multipart_encryption_types(&s3_client, TEST_BUCKET, "comprehensive-multipart-test").await?; // Phase 4: Mixed workload test info!("πŸ“‹ Phase 4: Mixed workload testing"); test_mixed_encryption_workload(&s3_client, TEST_BUCKET).await?; kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… KMS fully functional comprehensive test passed"); Ok(()) } /// Test mixed encryption workload with different file sizes and encryption types async fn test_mixed_encryption_workload( s3_client: &aws_sdk_s3::Client, bucket: &str, ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { info!("πŸ”„ Test hybrid crypto workloads"); // Test configuration: different sizes and encryption types let test_configs = vec![ // Small single-part uploads (S3 allows <5MB for the final part) MultipartTestConfig::new("mixed-small-none", 1024 * 1024, 1, EncryptionType::None), MultipartTestConfig::new("mixed-small-sse-s3", 1024 * 1024, 1, EncryptionType::SSES3), MultipartTestConfig::new("mixed-small-sse-kms", 1024 * 1024, 1, EncryptionType::SSEKMS), // SSE-C multipart uploads must respect the 5MB minimum part-size to avoid inline storage paths MultipartTestConfig::new("mixed-medium-sse-s3", 5 * 1024 * 1024, 3, EncryptionType::SSES3), MultipartTestConfig::new("mixed-medium-sse-kms", 5 * 1024 * 1024, 3, EncryptionType::SSEKMS), MultipartTestConfig::new("mixed-medium-sse-c", 5 * 1024 * 1024, 3, create_sse_c_config()), // Large multipart files MultipartTestConfig::new("mixed-large-sse-s3", 10 * 1024 * 1024, 2, EncryptionType::SSES3), MultipartTestConfig::new("mixed-large-sse-kms", 10 * 1024 * 1024, 2, EncryptionType::SSEKMS), MultipartTestConfig::new("mixed-large-sse-c", 10 * 1024 * 1024, 2, create_sse_c_config()), ]; for (i, config) in test_configs.iter().enumerate() { info!("πŸ”„ Perform hybrid testing {}/{}: {:?}", i + 1, test_configs.len(), config.encryption_type); test_multipart_upload_with_config(s3_client, bucket, config).await?; } info!("βœ… Hybrid cryptographic workload tests pass"); Ok(()) } /// Comprehensive stress test: Large dataset with multiple encryption types #[tokio::test] #[serial] async fn test_comprehensive_stress_test() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ’ͺ Start the KMS stress test"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Large multipart uploads with different encryption types let stress_configs = vec![ MultipartTestConfig::new("stress-sse-s3-large", 15 * 1024 * 1024, 4, EncryptionType::SSES3), MultipartTestConfig::new("stress-sse-kms-large", 15 * 1024 * 1024, 4, EncryptionType::SSEKMS), MultipartTestConfig::new("stress-sse-c-large", 15 * 1024 * 1024, 4, create_sse_c_config()), ]; for config in stress_configs { info!( "πŸ’ͺ Perform stress test: {:?}, Total size: {}MB", config.encryption_type, config.total_size() / (1024 * 1024) ); test_multipart_upload_with_config(&s3_client, TEST_BUCKET, &config).await?; } kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… KMS stress test passed"); Ok(()) } /// Test encryption key isolation and security #[tokio::test] #[serial] async fn test_comprehensive_key_isolation() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ” Begin the comprehensive test of encryption key isolation"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test different SSE-C keys to ensure isolation let key1 = "01234567890123456789012345678901"; let key2 = "98765432109876543210987654321098"; let key1_md5 = format!("{:x}", md5::compute(key1)); let key2_md5 = format!("{:x}", md5::compute(key2)); let config1 = MultipartTestConfig::new( "isolation-test-key1", 5 * 1024 * 1024, 2, EncryptionType::SSEC { key: key1.to_string(), key_md5: key1_md5, }, ); let config2 = MultipartTestConfig::new( "isolation-test-key2", 5 * 1024 * 1024, 2, EncryptionType::SSEC { key: key2.to_string(), key_md5: key2_md5, }, ); // Upload with different keys info!("πŸ” Key 1 for uploading files"); test_multipart_upload_with_config(&s3_client, TEST_BUCKET, &config1).await?; info!("πŸ” Key 2 for uploading files"); test_multipart_upload_with_config(&s3_client, TEST_BUCKET, &config2).await?; // Verify that files cannot be read with wrong keys info!("πŸ”’ Verify key isolation"); let wrong_key = "11111111111111111111111111111111"; let wrong_key_b64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, wrong_key); let wrong_key_md5 = format!("{:x}", md5::compute(wrong_key)); // Try to read file encrypted with key1 using wrong key let wrong_read_result = s3_client .get_object() .bucket(TEST_BUCKET) .key(&config1.object_key) .sse_customer_algorithm("AES256") .sse_customer_key(&wrong_key_b64) .sse_customer_key_md5(&wrong_key_md5) .send() .await; assert!(wrong_read_result.is_err(), "The encrypted file should not be readable with the wrong key"); info!("βœ… Confirm that key isolation is working correctly"); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Encryption key isolation comprehensive test passed"); Ok(()) } /// Test concurrent encryption operations #[tokio::test] #[serial] async fn test_comprehensive_concurrent_operations() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("⚑ Started comprehensive testing of concurrent encryption operations"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Create multiple concurrent upload tasks let multipart_part_size = 5 * 1024 * 1024; // honour S3 minimum part size for multipart uploads let concurrent_configs = vec![ MultipartTestConfig::new("concurrent-1-sse-s3", multipart_part_size, 2, EncryptionType::SSES3), MultipartTestConfig::new("concurrent-2-sse-kms", multipart_part_size, 2, EncryptionType::SSEKMS), MultipartTestConfig::new("concurrent-3-sse-c", multipart_part_size, 2, create_sse_c_config()), MultipartTestConfig::new("concurrent-4-none", multipart_part_size, 2, EncryptionType::None), ]; // Execute uploads concurrently info!("⚑ Start concurrent uploads"); let mut tasks = Vec::new(); for config in concurrent_configs { let client = s3_client.clone(); let bucket = TEST_BUCKET.to_string(); tasks.push(tokio::spawn( async move { test_multipart_upload_with_config(&client, &bucket, &config).await }, )); } // Wait for all tasks to complete for task in tasks { task.await??; } info!("βœ… All concurrent operations are completed"); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… The comprehensive test of concurrent encryption operation has passed"); Ok(()) } /// Test encryption/decryption performance with different file sizes #[tokio::test] #[serial] async fn test_comprehensive_performance_benchmark() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ“Š Start KMS performance benchmarking"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Performance test configurations with increasing file sizes let perf_configs = vec![ ("small", MultipartTestConfig::new("perf-small", 1024 * 1024, 1, EncryptionType::SSES3)), ( "medium", MultipartTestConfig::new("perf-medium", 5 * 1024 * 1024, 2, EncryptionType::SSES3), ), ( "large", MultipartTestConfig::new("perf-large", 10 * 1024 * 1024, 3, EncryptionType::SSES3), ), ]; for (size_name, config) in perf_configs { info!("πŸ“Š Test {} file performance ({}MB)", size_name, config.total_size() / (1024 * 1024)); let start_time = std::time::Instant::now(); test_multipart_upload_with_config(&s3_client, TEST_BUCKET, &config).await?; let duration = start_time.elapsed(); let throughput_mbps = (config.total_size() as f64 / (1024.0 * 1024.0)) / duration.as_secs_f64(); info!( "πŸ“Š {} file test completed: {:.2} seconds, throughput: {:.2} MB/s", size_name, duration.as_secs_f64(), throughput_mbps ); } kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… KMS performance benchmark passed"); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/kms/kms_local_test.rs
crates/e2e_test/src/kms/kms_local_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! End-to-end tests for Local KMS backend //! //! This test suite validates complete workflow including: //! - Dynamic KMS configuration via HTTP admin API //! - S3 object upload/download with SSE-S3, SSE-KMS, SSE-C encryption //! - Complete encryption/decryption lifecycle use super::common::{LocalKMSTestEnvironment, get_kms_status, test_kms_key_management, test_sse_c_encryption}; use crate::common::{TEST_BUCKET, init_logging}; use serial_test::serial; use tracing::{error, info}; #[tokio::test] #[serial] async fn test_local_kms_end_to_end() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Starting Local KMS End-to-End Test"); // Create LocalKMS test environment let mut kms_env = LocalKMSTestEnvironment::new() .await .expect("Failed to create LocalKMS test environment"); // Start RustFS with Local KMS backend (KMS should be auto-started with --kms-backend local) let default_key_id = kms_env .start_rustfs_for_local_kms() .await .expect("Failed to start RustFS with Local KMS"); // Wait a moment for RustFS to fully start up and initialize KMS tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; info!("RustFS started with KMS auto-configuration, default_key_id: {}", default_key_id); // Verify KMS status match get_kms_status(&kms_env.base_env.url, &kms_env.base_env.access_key, &kms_env.base_env.secret_key).await { Ok(status) => { info!("KMS Status after auto-configuration: {}", status); } Err(e) => { error!("Failed to get KMS status after auto-configuration: {}", e); return Err(e); } } // Create S3 client and test bucket let s3_client = kms_env.base_env.create_s3_client(); kms_env .base_env .create_test_bucket(TEST_BUCKET) .await .expect("Failed to create test bucket"); // Test KMS Key Management APIs test_kms_key_management(&kms_env.base_env.url, &kms_env.base_env.access_key, &kms_env.base_env.secret_key) .await .expect("KMS key management test failed"); // Test different encryption methods test_sse_c_encryption(&s3_client, TEST_BUCKET) .await .expect("SSE-C encryption test failed"); info!("SSE-C encryption test completed successfully, ending test early for debugging"); // TEMPORARILY COMMENTED OUT FOR DEBUGGING: // // Wait a moment and verify KMS is ready for SSE-S3 // tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; // match get_kms_status(&kms_env.base_env.url, &kms_env.base_env.access_key, &kms_env.base_env.secret_key).await { // Ok(status) => info!("KMS Status before SSE-S3 test: {}", status), // Err(e) => warn!("Failed to get KMS status before SSE-S3 test: {}", e), // } // test_sse_s3_encryption(&s3_client, TEST_BUCKET).await // .expect("SSE-S3 encryption test failed"); // // Test SSE-KMS encryption // test_sse_kms_encryption(&s3_client, TEST_BUCKET).await // .expect("SSE-KMS encryption test failed"); // // Test error scenarios // test_error_scenarios(&s3_client, TEST_BUCKET).await // .expect("Error scenarios test failed"); // Clean up kms_env .base_env .delete_test_bucket(TEST_BUCKET) .await .expect("Failed to delete test bucket"); info!("Local KMS End-to-End Test completed successfully"); Ok(()) } #[tokio::test] #[serial] async fn test_local_kms_key_isolation() { init_logging(); info!("Starting Local KMS Key Isolation Test"); let mut kms_env = LocalKMSTestEnvironment::new() .await .expect("Failed to create LocalKMS test environment"); // Start RustFS with Local KMS backend (KMS should be auto-started with --kms-backend local) let default_key_id = kms_env .start_rustfs_for_local_kms() .await .expect("Failed to start RustFS with Local KMS"); // Wait a moment for RustFS to fully start up and initialize KMS tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; info!("RustFS started with KMS auto-configuration, default_key_id: {}", default_key_id); let s3_client = kms_env.base_env.create_s3_client(); kms_env .base_env .create_test_bucket(TEST_BUCKET) .await .expect("Failed to create test bucket"); // Test that different SSE-C keys create isolated encrypted objects let key1 = "01234567890123456789012345678901"; let key2 = "98765432109876543210987654321098"; let key1_b64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, key1); let key2_b64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, key2); let key1_md5 = format!("{:x}", md5::compute(key1)); let key2_md5 = format!("{:x}", md5::compute(key2)); let data1 = b"Data encrypted with key 1"; let data2 = b"Data encrypted with key 2"; // Upload two objects with different SSE-C keys s3_client .put_object() .bucket(TEST_BUCKET) .key("object1") .body(aws_sdk_s3::primitives::ByteStream::from(data1.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&key1_b64) .sse_customer_key_md5(&key1_md5) .send() .await .expect("Failed to upload object1"); s3_client .put_object() .bucket(TEST_BUCKET) .key("object2") .body(aws_sdk_s3::primitives::ByteStream::from(data2.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&key2_b64) .sse_customer_key_md5(&key2_md5) .send() .await .expect("Failed to upload object2"); // Verify each object can only be decrypted with its own key let get1 = s3_client .get_object() .bucket(TEST_BUCKET) .key("object1") .sse_customer_algorithm("AES256") .sse_customer_key(&key1_b64) .sse_customer_key_md5(&key1_md5) .send() .await .expect("Failed to get object1 with key1"); let retrieved_data1 = get1.body.collect().await.expect("Failed to read object1 body").into_bytes(); assert_eq!(retrieved_data1.as_ref(), data1); // Try to access object1 with key2 - should fail let wrong_key_result = s3_client .get_object() .bucket(TEST_BUCKET) .key("object1") .sse_customer_algorithm("AES256") .sse_customer_key(&key2_b64) .sse_customer_key_md5(&key2_md5) .send() .await; assert!(wrong_key_result.is_err(), "Should not be able to decrypt object1 with key2"); kms_env .base_env .delete_test_bucket(TEST_BUCKET) .await .expect("Failed to delete test bucket"); info!("Local KMS Key Isolation Test completed successfully"); } #[tokio::test] #[serial] async fn test_local_kms_large_file() { init_logging(); info!("Starting Local KMS Large File Test"); let mut kms_env = LocalKMSTestEnvironment::new() .await .expect("Failed to create LocalKMS test environment"); // Start RustFS with Local KMS backend (KMS should be auto-started with --kms-backend local) let default_key_id = kms_env .start_rustfs_for_local_kms() .await .expect("Failed to start RustFS with Local KMS"); // Wait a moment for RustFS to fully start up and initialize KMS tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; info!("RustFS started with KMS auto-configuration, default_key_id: {}", default_key_id); let s3_client = kms_env.base_env.create_s3_client(); kms_env .base_env .create_test_bucket(TEST_BUCKET) .await .expect("Failed to create test bucket"); // Test progressively larger file sizes to find the exact threshold where encryption fails // Starting with 1MB to reproduce the issue first let large_data = vec![0xABu8; 1024 * 1024]; let object_key = "large-encrypted-file"; // Test SSE-S3 with large file let put_response = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key) .body(aws_sdk_s3::primitives::ByteStream::from(large_data.clone())) .server_side_encryption(aws_sdk_s3::types::ServerSideEncryption::Aes256) .send() .await .expect("Failed to upload large file with SSE-S3"); assert_eq!( put_response.server_side_encryption(), Some(&aws_sdk_s3::types::ServerSideEncryption::Aes256) ); // Download and verify let get_response = s3_client .get_object() .bucket(TEST_BUCKET) .key(object_key) .send() .await .expect("Failed to download large file"); // Verify SSE-S3 encryption header in GET response assert_eq!( get_response.server_side_encryption(), Some(&aws_sdk_s3::types::ServerSideEncryption::Aes256) ); let downloaded_data = get_response .body .collect() .await .expect("Failed to read large file body") .into_bytes(); assert_eq!(downloaded_data.len(), large_data.len()); assert_eq!(&downloaded_data[..], &large_data[..]); kms_env .base_env .delete_test_bucket(TEST_BUCKET) .await .expect("Failed to delete test bucket"); info!("Local KMS Large File Test completed successfully"); } #[tokio::test] #[serial] async fn test_local_kms_multipart_upload() { init_logging(); info!("Starting Local KMS Multipart Upload Test"); let mut kms_env = LocalKMSTestEnvironment::new() .await .expect("Failed to create LocalKMS test environment"); // Start RustFS with Local KMS backend let default_key_id = kms_env .start_rustfs_for_local_kms() .await .expect("Failed to start RustFS with Local KMS"); // Wait for KMS initialization tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; info!("RustFS started with KMS auto-configuration, default_key_id: {}", default_key_id); let s3_client = kms_env.base_env.create_s3_client(); kms_env .base_env .create_test_bucket(TEST_BUCKET) .await .expect("Failed to create test bucket"); // Test multipart upload with different encryption types // Test 1: Multipart upload with SSE-S3 (focus on this first) info!("Testing multipart upload with SSE-S3"); test_multipart_upload_with_sse_s3(&s3_client, TEST_BUCKET) .await .expect("SSE-S3 multipart upload test failed"); // Test 2: Multipart upload with SSE-KMS info!("Testing multipart upload with SSE-KMS"); test_multipart_upload_with_sse_kms(&s3_client, TEST_BUCKET) .await .expect("SSE-KMS multipart upload test failed"); // Test 3: Multipart upload with SSE-C info!("Testing multipart upload with SSE-C"); test_multipart_upload_with_sse_c(&s3_client, TEST_BUCKET) .await .expect("SSE-C multipart upload test failed"); // Test 4: Large multipart upload (test streaming encryption with multiple blocks) // TODO: Re-enable after fixing streaming encryption issues with large files // info!("Testing large multipart upload with streaming encryption"); // test_large_multipart_upload(&s3_client, TEST_BUCKET).await // .expect("Large multipart upload test failed"); // Clean up kms_env .base_env .delete_test_bucket(TEST_BUCKET) .await .expect("Failed to delete test bucket"); info!("Local KMS Multipart Upload Test completed successfully"); } /// Test multipart upload with SSE-S3 encryption async fn test_multipart_upload_with_sse_s3( s3_client: &aws_sdk_s3::Client, bucket: &str, ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let object_key = "multipart-sse-s3-test"; let part_size = 5 * 1024 * 1024; // 5MB per part (minimum S3 multipart size) let total_parts = 2; let total_size = part_size * total_parts; // Generate test data let test_data: Vec<u8> = (0..total_size).map(|i| (i % 256) as u8).collect(); // Step 1: Initiate multipart upload with SSE-S3 let create_multipart_output = s3_client .create_multipart_upload() .bucket(bucket) .key(object_key) .server_side_encryption(aws_sdk_s3::types::ServerSideEncryption::Aes256) .send() .await?; let upload_id = create_multipart_output.upload_id().unwrap(); info!("Created multipart upload with SSE-S3, upload_id: {}", upload_id); // Note: CreateMultipartUpload response may not include server_side_encryption header in some implementations // The encryption will be verified in the final GetObject response if let Some(sse) = create_multipart_output.server_side_encryption() { info!("CreateMultipartUpload response includes SSE: {:?}", sse); assert_eq!(sse, &aws_sdk_s3::types::ServerSideEncryption::Aes256); } else { info!("CreateMultipartUpload response does not include SSE header (implementation specific)"); } // Step 2: Upload parts info!("CLAUDE TEST DEBUG: Starting to upload {} parts", total_parts); let mut completed_parts = Vec::new(); for part_number in 1..=total_parts { let start = (part_number - 1) * part_size; let end = std::cmp::min(start + part_size, total_size); let part_data = &test_data[start..end]; let upload_part_output = s3_client .upload_part() .bucket(bucket) .key(object_key) .upload_id(upload_id) .part_number(part_number as i32) .body(aws_sdk_s3::primitives::ByteStream::from(part_data.to_vec())) .send() .await?; let etag = upload_part_output.e_tag().unwrap().to_string(); completed_parts.push( aws_sdk_s3::types::CompletedPart::builder() .part_number(part_number as i32) .e_tag(&etag) .build(), ); info!("CLAUDE TEST DEBUG: Uploaded part {} with etag: {}", part_number, etag); } // Step 3: Complete multipart upload let completed_multipart_upload = aws_sdk_s3::types::CompletedMultipartUpload::builder() .set_parts(Some(completed_parts)) .build(); info!("CLAUDE TEST DEBUG: About to call complete_multipart_upload"); let complete_output = s3_client .complete_multipart_upload() .bucket(bucket) .key(object_key) .upload_id(upload_id) .multipart_upload(completed_multipart_upload) .send() .await?; info!( "CLAUDE TEST DEBUG: complete_multipart_upload succeeded, etag: {:?}", complete_output.e_tag() ); // Step 4: Try a HEAD request to debug metadata before GET let head_response = s3_client.head_object().bucket(bucket).key(object_key).send().await?; info!("CLAUDE TEST DEBUG: HEAD response metadata: {:?}", head_response.metadata()); info!("CLAUDE TEST DEBUG: HEAD response SSE: {:?}", head_response.server_side_encryption()); // Step 5: Download and verify let get_response = s3_client.get_object().bucket(bucket).key(object_key).send().await?; // Verify encryption headers assert_eq!( get_response.server_side_encryption(), Some(&aws_sdk_s3::types::ServerSideEncryption::Aes256) ); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), total_size); assert_eq!(&downloaded_data[..], &test_data[..]); info!("βœ… SSE-S3 multipart upload test passed"); Ok(()) } /// Test multipart upload with SSE-KMS encryption async fn test_multipart_upload_with_sse_kms( s3_client: &aws_sdk_s3::Client, bucket: &str, ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let object_key = "multipart-sse-kms-test"; let part_size = 5 * 1024 * 1024; // 5MB per part (minimum S3 multipart size) let total_parts = 2; let total_size = part_size * total_parts; // Generate test data let test_data: Vec<u8> = (0..total_size).map(|i| ((i / 1000) % 256) as u8).collect(); // Step 1: Initiate multipart upload with SSE-KMS let create_multipart_output = s3_client .create_multipart_upload() .bucket(bucket) .key(object_key) .server_side_encryption(aws_sdk_s3::types::ServerSideEncryption::AwsKms) .send() .await?; let upload_id = create_multipart_output.upload_id().unwrap(); // Note: CreateMultipartUpload response may not include server_side_encryption header in some implementations if let Some(sse) = create_multipart_output.server_side_encryption() { info!("CreateMultipartUpload response includes SSE-KMS: {:?}", sse); assert_eq!(sse, &aws_sdk_s3::types::ServerSideEncryption::AwsKms); } else { info!("CreateMultipartUpload response does not include SSE-KMS header (implementation specific)"); } // Step 2: Upload parts let mut completed_parts = Vec::new(); for part_number in 1..=total_parts { let start = (part_number - 1) * part_size; let end = std::cmp::min(start + part_size, total_size); let part_data = &test_data[start..end]; let upload_part_output = s3_client .upload_part() .bucket(bucket) .key(object_key) .upload_id(upload_id) .part_number(part_number as i32) .body(aws_sdk_s3::primitives::ByteStream::from(part_data.to_vec())) .send() .await?; let etag = upload_part_output.e_tag().unwrap().to_string(); completed_parts.push( aws_sdk_s3::types::CompletedPart::builder() .part_number(part_number as i32) .e_tag(&etag) .build(), ); } // Step 3: Complete multipart upload let completed_multipart_upload = aws_sdk_s3::types::CompletedMultipartUpload::builder() .set_parts(Some(completed_parts)) .build(); let _complete_output = s3_client .complete_multipart_upload() .bucket(bucket) .key(object_key) .upload_id(upload_id) .multipart_upload(completed_multipart_upload) .send() .await?; // Step 4: Download and verify let get_response = s3_client.get_object().bucket(bucket).key(object_key).send().await?; assert_eq!( get_response.server_side_encryption(), Some(&aws_sdk_s3::types::ServerSideEncryption::AwsKms) ); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), total_size); assert_eq!(&downloaded_data[..], &test_data[..]); info!("βœ… SSE-KMS multipart upload test passed"); Ok(()) } /// Test multipart upload with SSE-C encryption async fn test_multipart_upload_with_sse_c( s3_client: &aws_sdk_s3::Client, bucket: &str, ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let object_key = "multipart-sse-c-test"; let part_size = 5 * 1024 * 1024; // 5MB per part (minimum S3 multipart size) let total_parts = 2; let total_size = part_size * total_parts; // SSE-C encryption key let encryption_key = "01234567890123456789012345678901"; let key_b64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, encryption_key); let key_md5 = format!("{:x}", md5::compute(encryption_key)); // Generate test data let test_data: Vec<u8> = (0..total_size).map(|i| ((i * 3) % 256) as u8).collect(); // Step 1: Initiate multipart upload with SSE-C let create_multipart_output = s3_client .create_multipart_upload() .bucket(bucket) .key(object_key) .sse_customer_algorithm("AES256") .sse_customer_key(&key_b64) .sse_customer_key_md5(&key_md5) .send() .await?; let upload_id = create_multipart_output.upload_id().unwrap(); // Step 2: Upload parts with same SSE-C key let mut completed_parts = Vec::new(); for part_number in 1..=total_parts { let start = (part_number - 1) * part_size; let end = std::cmp::min(start + part_size, total_size); let part_data = &test_data[start..end]; let upload_part_output = s3_client .upload_part() .bucket(bucket) .key(object_key) .upload_id(upload_id) .part_number(part_number as i32) .body(aws_sdk_s3::primitives::ByteStream::from(part_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&key_b64) .sse_customer_key_md5(&key_md5) .send() .await?; let etag = upload_part_output.e_tag().unwrap().to_string(); completed_parts.push( aws_sdk_s3::types::CompletedPart::builder() .part_number(part_number as i32) .e_tag(&etag) .build(), ); } // Step 3: Complete multipart upload let completed_multipart_upload = aws_sdk_s3::types::CompletedMultipartUpload::builder() .set_parts(Some(completed_parts)) .build(); let _complete_output = s3_client .complete_multipart_upload() .bucket(bucket) .key(object_key) .upload_id(upload_id) .multipart_upload(completed_multipart_upload) .send() .await?; // Step 4: Download and verify with same SSE-C key let get_response = s3_client .get_object() .bucket(bucket) .key(object_key) .sse_customer_algorithm("AES256") .sse_customer_key(&key_b64) .sse_customer_key_md5(&key_md5) .send() .await?; let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), total_size); assert_eq!(&downloaded_data[..], &test_data[..]); info!("βœ… SSE-C multipart upload test passed"); Ok(()) } /// Test large multipart upload to verify streaming encryption works correctly #[allow(dead_code)] async fn test_large_multipart_upload( s3_client: &aws_sdk_s3::Client, bucket: &str, ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { let object_key = "large-multipart-test"; let part_size = 6 * 1024 * 1024; // 6MB per part (larger than 1MB block size) let total_parts = 5; // Total: 30MB let total_size = part_size * total_parts; info!( "Testing large multipart upload: {} parts of {}MB each = {}MB total", total_parts, part_size / (1024 * 1024), total_size / (1024 * 1024) ); // Generate test data with pattern for verification let test_data: Vec<u8> = (0..total_size) .map(|i| { let part_num = i / part_size; let offset_in_part = i % part_size; ((part_num * 100 + offset_in_part / 1000) % 256) as u8 }) .collect(); // Step 1: Initiate multipart upload with SSE-S3 let create_multipart_output = s3_client .create_multipart_upload() .bucket(bucket) .key(object_key) .server_side_encryption(aws_sdk_s3::types::ServerSideEncryption::Aes256) .send() .await?; let upload_id = create_multipart_output.upload_id().unwrap(); // Step 2: Upload parts let mut completed_parts = Vec::new(); for part_number in 1..=total_parts { let start = (part_number - 1) * part_size; let end = std::cmp::min(start + part_size, total_size); let part_data = &test_data[start..end]; info!("Uploading part {} ({} bytes)", part_number, part_data.len()); let upload_part_output = s3_client .upload_part() .bucket(bucket) .key(object_key) .upload_id(upload_id) .part_number(part_number as i32) .body(aws_sdk_s3::primitives::ByteStream::from(part_data.to_vec())) .send() .await?; let etag = upload_part_output.e_tag().unwrap().to_string(); completed_parts.push( aws_sdk_s3::types::CompletedPart::builder() .part_number(part_number as i32) .e_tag(&etag) .build(), ); info!("Part {} uploaded successfully", part_number); } // Step 3: Complete multipart upload let completed_multipart_upload = aws_sdk_s3::types::CompletedMultipartUpload::builder() .set_parts(Some(completed_parts)) .build(); let _complete_output = s3_client .complete_multipart_upload() .bucket(bucket) .key(object_key) .upload_id(upload_id) .multipart_upload(completed_multipart_upload) .send() .await?; info!("Large multipart upload completed"); // Step 4: Download and verify (this tests streaming decryption) let get_response = s3_client.get_object().bucket(bucket).key(object_key).send().await?; assert_eq!( get_response.server_side_encryption(), Some(&aws_sdk_s3::types::ServerSideEncryption::Aes256) ); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), total_size); // Verify data integrity for (i, (&actual, &expected)) in downloaded_data.iter().zip(test_data.iter()).enumerate() { if actual != expected { panic!("Data mismatch at byte {i}: got {actual}, expected {expected}"); } } info!( "βœ… Large multipart upload test passed - streaming encryption/decryption works correctly for {}MB file", total_size / (1024 * 1024) ); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/kms/kms_edge_cases_test.rs
crates/e2e_test/src/kms/kms_edge_cases_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! KMS Edge Cases and Boundary Condition Tests //! //! This test suite validates KMS functionality under edge cases and boundary conditions: //! - Zero-byte and single-byte file encryption //! - Multipart boundary conditions (minimum size limits) //! - Invalid key scenarios and error handling //! - Concurrent encryption operations //! - Security validation tests use super::common::LocalKMSTestEnvironment; use crate::common::{TEST_BUCKET, init_logging}; use aws_sdk_s3::types::ServerSideEncryption; use base64::Engine; use md5::compute; use serial_test::serial; use std::sync::Arc; use tokio::sync::Semaphore; use tracing::{info, warn}; /// Test encryption of zero-byte files (empty files) #[tokio::test] #[serial] async fn test_kms_zero_byte_file_encryption() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS encryption with zero-byte files"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test SSE-S3 with zero-byte file info!("πŸ“€ Testing SSE-S3 with zero-byte file"); let empty_data = b""; let object_key = "zero-byte-sse-s3"; let put_response = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key) .body(aws_sdk_s3::primitives::ByteStream::from(empty_data.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; assert_eq!(put_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); // Verify download let get_response = s3_client.get_object().bucket(TEST_BUCKET).key(object_key).send().await?; assert_eq!(get_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), 0); // Test SSE-C with zero-byte file info!("πŸ“€ Testing SSE-C with zero-byte file"); let test_key = "01234567890123456789012345678901"; let test_key_b64 = base64::engine::general_purpose::STANDARD.encode(test_key); let test_key_md5 = format!("{:x}", compute(test_key)); let object_key_c = "zero-byte-sse-c"; let _put_response_c = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key_c) .body(aws_sdk_s3::primitives::ByteStream::from(empty_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&test_key_b64) .sse_customer_key_md5(&test_key_md5) .send() .await?; // Verify download with SSE-C let get_response_c = s3_client .get_object() .bucket(TEST_BUCKET) .key(object_key_c) .sse_customer_algorithm("AES256") .sse_customer_key(&test_key_b64) .sse_customer_key_md5(&test_key_md5) .send() .await?; let downloaded_data_c = get_response_c.body.collect().await?.into_bytes(); assert_eq!(downloaded_data_c.len(), 0); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Zero-byte file encryption test completed successfully"); Ok(()) } /// Test encryption of single-byte files #[tokio::test] #[serial] async fn test_kms_single_byte_file_encryption() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS encryption with single-byte files"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test all three encryption types with single byte let test_data = b"A"; let test_scenarios = vec![("single-byte-sse-s3", "SSE-S3"), ("single-byte-sse-kms", "SSE-KMS")]; for (object_key, encryption_type) in test_scenarios { info!("πŸ“€ Testing {} with single-byte file", encryption_type); let put_request = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())); let _put_response = match encryption_type { "SSE-S3" => { put_request .server_side_encryption(ServerSideEncryption::Aes256) .send() .await? } "SSE-KMS" => { put_request .server_side_encryption(ServerSideEncryption::AwsKms) .send() .await? } _ => unreachable!(), }; // Verify download let get_response = s3_client.get_object().bucket(TEST_BUCKET).key(object_key).send().await?; let expected_encryption = match encryption_type { "SSE-S3" => ServerSideEncryption::Aes256, "SSE-KMS" => ServerSideEncryption::AwsKms, _ => unreachable!(), }; assert_eq!(get_response.server_side_encryption(), Some(&expected_encryption)); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.as_ref(), test_data); } // Test SSE-C with single byte info!("πŸ“€ Testing SSE-C with single-byte file"); let test_key = "01234567890123456789012345678901"; let test_key_b64 = base64::engine::general_purpose::STANDARD.encode(test_key); let test_key_md5 = format!("{:x}", compute(test_key)); let object_key_c = "single-byte-sse-c"; s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key_c) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&test_key_b64) .sse_customer_key_md5(&test_key_md5) .send() .await?; let get_response_c = s3_client .get_object() .bucket(TEST_BUCKET) .key(object_key_c) .sse_customer_algorithm("AES256") .sse_customer_key(&test_key_b64) .sse_customer_key_md5(&test_key_md5) .send() .await?; let downloaded_data_c = get_response_c.body.collect().await?.into_bytes(); assert_eq!(downloaded_data_c.as_ref(), test_data); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Single-byte file encryption test completed successfully"); Ok(()) } /// Test multipart upload boundary conditions (minimum 5MB part size) #[tokio::test] #[serial] async fn test_kms_multipart_boundary_conditions() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS multipart upload boundary conditions"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test with exactly minimum part size (5MB) info!("πŸ“€ Testing with exactly 5MB part size"); let part_size = 5 * 1024 * 1024; // Exactly 5MB let test_data: Vec<u8> = (0..part_size).map(|i| (i % 256) as u8).collect(); let object_key = "multipart-boundary-5mb"; // Initiate multipart upload with SSE-S3 let create_multipart_output = s3_client .create_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; let upload_id = create_multipart_output.upload_id().unwrap(); // Upload single part with exactly 5MB let upload_part_output = s3_client .upload_part() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id) .part_number(1) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.clone())) .send() .await?; let etag = upload_part_output.e_tag().unwrap().to_string(); // Complete multipart upload let completed_part = aws_sdk_s3::types::CompletedPart::builder() .part_number(1) .e_tag(&etag) .build(); let completed_multipart_upload = aws_sdk_s3::types::CompletedMultipartUpload::builder() .parts(completed_part) .build(); s3_client .complete_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id) .multipart_upload(completed_multipart_upload) .send() .await?; // Verify download let get_response = s3_client.get_object().bucket(TEST_BUCKET).key(object_key).send().await?; assert_eq!(get_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), test_data.len()); assert_eq!(&downloaded_data[..], &test_data[..]); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Multipart boundary conditions test completed successfully"); Ok(()) } /// Test invalid key scenarios and error handling #[tokio::test] #[serial] async fn test_kms_invalid_key_scenarios() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS invalid key scenarios and error handling"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; let test_data = b"Test data for invalid key scenarios"; // Test 1: Invalid key length for SSE-C info!("πŸ” Testing invalid SSE-C key length"); let invalid_short_key = "short"; // Too short let invalid_key_b64 = base64::engine::general_purpose::STANDARD.encode(invalid_short_key); let invalid_key_md5 = format!("{:x}", compute(invalid_short_key)); let invalid_key_result = s3_client .put_object() .bucket(TEST_BUCKET) .key("test-invalid-key-length") .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&invalid_key_b64) .sse_customer_key_md5(&invalid_key_md5) .send() .await; assert!(invalid_key_result.is_err(), "Should reject invalid key length"); info!("βœ… Correctly rejected invalid key length"); // Test 2: Mismatched MD5 for SSE-C info!("πŸ” Testing mismatched MD5 for SSE-C key"); let valid_key = "01234567890123456789012345678901"; let valid_key_b64 = base64::engine::general_purpose::STANDARD.encode(valid_key); let wrong_md5 = "wrongmd5hash12345678901234567890"; // Wrong MD5 let wrong_md5_result = s3_client .put_object() .bucket(TEST_BUCKET) .key("test-wrong-md5") .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&valid_key_b64) .sse_customer_key_md5(wrong_md5) .send() .await; assert!(wrong_md5_result.is_err(), "Should reject mismatched MD5"); info!("βœ… Correctly rejected mismatched MD5"); // Test 3: Try to access SSE-C object without providing key info!("πŸ” Testing access to SSE-C object without key"); // First upload a valid SSE-C object let valid_key_md5 = format!("{:x}", compute(valid_key)); s3_client .put_object() .bucket(TEST_BUCKET) .key("test-sse-c-no-key-access") .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&valid_key_b64) .sse_customer_key_md5(&valid_key_md5) .send() .await?; // Try to access without providing key let no_key_result = s3_client .get_object() .bucket(TEST_BUCKET) .key("test-sse-c-no-key-access") .send() .await; assert!(no_key_result.is_err(), "Should require SSE-C key for access"); info!("βœ… Correctly required SSE-C key for access"); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Invalid key scenarios test completed successfully"); Ok(()) } /// Test concurrent encryption operations #[tokio::test] #[serial] async fn test_kms_concurrent_encryption() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS concurrent encryption operations"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = Arc::new(kms_env.base_env.create_s3_client()); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test concurrent uploads with different encryption types info!("πŸ“€ Testing concurrent uploads with different encryption types"); let num_concurrent = 5; let semaphore = Arc::new(Semaphore::new(num_concurrent)); let mut tasks = Vec::new(); for i in 0..num_concurrent { let client = Arc::clone(&s3_client); let sem = Arc::clone(&semaphore); let task = tokio::spawn(async move { let _permit = sem.acquire().await.unwrap(); let test_data = format!("Concurrent test data {i}").into_bytes(); let object_key = format!("concurrent-test-{i}"); // Alternate between different encryption types let result = match i % 3 { 0 => { // SSE-S3 client .put_object() .bucket(TEST_BUCKET) .key(&object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.clone())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await } 1 => { // SSE-KMS client .put_object() .bucket(TEST_BUCKET) .key(&object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.clone())) .server_side_encryption(ServerSideEncryption::AwsKms) .send() .await } 2 => { // SSE-C let key = format!("testkey{i:026}"); // 32-byte key let key_b64 = base64::engine::general_purpose::STANDARD.encode(&key); let key_md5 = format!("{:x}", compute(&key)); client .put_object() .bucket(TEST_BUCKET) .key(&object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.clone())) .sse_customer_algorithm("AES256") .sse_customer_key(&key_b64) .sse_customer_key_md5(&key_md5) .send() .await } _ => unreachable!(), }; (i, result) }); tasks.push(task); } // Wait for all tasks to complete let mut successful_uploads = 0; for task in tasks { let (task_id, result) = task.await.unwrap(); match result { Ok(_) => { successful_uploads += 1; info!("βœ… Concurrent upload {} completed successfully", task_id); } Err(e) => { warn!("❌ Concurrent upload {} failed: {}", task_id, e); } } } assert!( successful_uploads >= num_concurrent - 1, "Most concurrent uploads should succeed (got {successful_uploads}/{num_concurrent})" ); info!("βœ… Successfully completed {}/{} concurrent uploads", successful_uploads, num_concurrent); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Concurrent encryption test completed successfully"); Ok(()) } /// Test key validation and security properties #[tokio::test] #[serial] async fn test_kms_key_validation_security() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS key validation and security properties"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test 1: Verify that different keys produce different encrypted data info!("πŸ” Testing that different keys produce different encrypted data"); let test_data = b"Same plaintext data for encryption comparison"; let key1 = "key1key1key1key1key1key1key1key1"; // 32 bytes let key2 = "key2key2key2key2key2key2key2key2"; // 32 bytes let key1_b64 = base64::engine::general_purpose::STANDARD.encode(key1); let key2_b64 = base64::engine::general_purpose::STANDARD.encode(key2); let key1_md5 = format!("{:x}", compute(key1)); let key2_md5 = format!("{:x}", compute(key2)); // Upload same data with different keys s3_client .put_object() .bucket(TEST_BUCKET) .key("security-test-key1") .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&key1_b64) .sse_customer_key_md5(&key1_md5) .send() .await?; s3_client .put_object() .bucket(TEST_BUCKET) .key("security-test-key2") .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .sse_customer_algorithm("AES256") .sse_customer_key(&key2_b64) .sse_customer_key_md5(&key2_md5) .send() .await?; // Verify both can be decrypted with their respective keys let data1 = s3_client .get_object() .bucket(TEST_BUCKET) .key("security-test-key1") .sse_customer_algorithm("AES256") .sse_customer_key(&key1_b64) .sse_customer_key_md5(&key1_md5) .send() .await? .body .collect() .await? .into_bytes(); let data2 = s3_client .get_object() .bucket(TEST_BUCKET) .key("security-test-key2") .sse_customer_algorithm("AES256") .sse_customer_key(&key2_b64) .sse_customer_key_md5(&key2_md5) .send() .await? .body .collect() .await? .into_bytes(); assert_eq!(data1.as_ref(), test_data); assert_eq!(data2.as_ref(), test_data); info!("βœ… Different keys can decrypt their respective data correctly"); // Test 2: Verify key isolation (key1 cannot decrypt key2's data) info!("πŸ” Testing key isolation"); let wrong_key_result = s3_client .get_object() .bucket(TEST_BUCKET) .key("security-test-key2") .sse_customer_algorithm("AES256") .sse_customer_key(&key1_b64) // Wrong key .sse_customer_key_md5(&key1_md5) .send() .await; assert!(wrong_key_result.is_err(), "Should not be able to decrypt with wrong key"); info!("βœ… Key isolation verified - wrong key cannot decrypt data"); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Key validation and security test completed successfully"); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/kms/bucket_default_encryption_test.rs
crates/e2e_test/src/kms/bucket_default_encryption_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Bucket Default Encryption Configuration Integration Tests //! //! This test suite verifies that bucket-level default encryption configuration is properly integrated with: //! 1. put_object operations //! 2. create_multipart_upload operations //! 3. KMS service integration use super::common::LocalKMSTestEnvironment; use crate::common::{TEST_BUCKET, init_logging}; use aws_sdk_s3::types::{ ServerSideEncryption, ServerSideEncryptionByDefault, ServerSideEncryptionConfiguration, ServerSideEncryptionRule, }; use serial_test::serial; use tracing::{debug, info, warn}; /// Test 1: When bucket is configured with default SSE-S3 encryption, put_object should automatically apply encryption #[tokio::test] #[serial] async fn test_bucket_default_sse_s3_put_object() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Testing bucket default SSE-S3 encryption impact on put_object"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Step 1: Set bucket default encryption to SSE-S3 info!("Setting bucket default encryption configuration"); let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::Aes256) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await .expect("Failed to set bucket encryption"); info!("Bucket default encryption configuration set successfully"); // Verify bucket encryption configuration let get_encryption_response = s3_client .get_bucket_encryption() .bucket(TEST_BUCKET) .send() .await .expect("Failed to get bucket encryption"); debug!( "Bucket encryption configuration: {:?}", get_encryption_response.server_side_encryption_configuration() ); // Step 2: put_object without specifying encryption parameters should automatically use bucket default encryption info!("Uploading file (without specifying encryption parameters, should use bucket default encryption)"); let test_data = b"test-bucket-default-sse-s3-data"; let test_key = "test-bucket-default-sse-s3.txt"; let put_response = s3_client .put_object() .bucket(TEST_BUCKET) .key(test_key) .body(test_data.to_vec().into()) // Note: No server_side_encryption specified here, should use bucket default .send() .await .expect("Failed to put object"); debug!( "PUT response: ETag={:?}, SSE={:?}", put_response.e_tag(), put_response.server_side_encryption() ); // Verify: Response should contain SSE-S3 encryption information assert_eq!( put_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256), "put_object response should contain bucket default SSE-S3 encryption information" ); // Step 3: Download file and verify encryption status info!("Downloading file and verifying encryption status"); let get_response = s3_client .get_object() .bucket(TEST_BUCKET) .key(test_key) .send() .await .expect("Failed to get object"); debug!("GET response: SSE={:?}", get_response.server_side_encryption()); // Verify: GET response should contain encryption information assert_eq!( get_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256), "get_object response should contain SSE-S3 encryption information" ); // Verify data integrity let downloaded_data = get_response .body .collect() .await .expect("Failed to collect body") .into_bytes(); assert_eq!(&downloaded_data[..], test_data, "Downloaded data should match original data"); // Step 4: Explicitly specifying encryption parameters should override bucket default info!("Uploading file (explicitly specifying no encryption, should override bucket default)"); let _test_key_2 = "test-explicit-override.txt"; // Note: This test might temporarily fail because current implementation might not support explicit override // But this is the target behavior we want to implement warn!("Test for explicitly overriding bucket default encryption is temporarily skipped, this is a feature to be implemented"); // TODO: Add test for explicit override when implemented info!("Test passed: bucket default SSE-S3 encryption correctly applied to put_object"); Ok(()) } /// Test 2: When bucket is configured with default SSE-KMS encryption, put_object should automatically apply encryption and use the specified KMS key #[tokio::test] #[serial] async fn test_bucket_default_sse_kms_put_object() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Testing bucket default SSE-KMS encryption impact on put_object"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Step 1: Set bucket default encryption to SSE-KMS with specified KMS key info!("Setting bucket default encryption configuration to SSE-KMS"); let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::AwsKms) .kms_master_key_id(&default_key_id) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await .expect("Failed to set bucket SSE-KMS encryption"); info!("Bucket default SSE-KMS encryption configuration set successfully"); // Step 2: put_object without specifying encryption parameters should automatically use bucket default SSE-KMS info!("Uploading file (without specifying encryption parameters, should use bucket default SSE-KMS)"); let test_data = b"test-bucket-default-sse-kms-data"; let test_key = "test-bucket-default-sse-kms.txt"; let put_response = s3_client .put_object() .bucket(TEST_BUCKET) .key(test_key) .body(test_data.to_vec().into()) // Note: No encryption parameters specified here, should use bucket default SSE-KMS .send() .await .expect("Failed to put object with bucket default SSE-KMS"); debug!( "PUT response: ETag={:?}, SSE={:?}, KMS_Key={:?}", put_response.e_tag(), put_response.server_side_encryption(), put_response.ssekms_key_id() ); // Verify: Response should contain SSE-KMS encryption information assert_eq!( put_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "put_object response should contain bucket default SSE-KMS encryption information" ); assert_eq!( put_response.ssekms_key_id().unwrap(), &default_key_id, "put_object response should contain correct KMS key ID" ); // Step 3: Download file and verify encryption status info!("Downloading file and verifying encryption status"); let get_response = s3_client .get_object() .bucket(TEST_BUCKET) .key(test_key) .send() .await .expect("Failed to get object"); debug!( "GET response: SSE={:?}, KMS_Key={:?}", get_response.server_side_encryption(), get_response.ssekms_key_id() ); // Verify: GET response should contain encryption information assert_eq!( get_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "get_object response should contain SSE-KMS encryption information" ); assert_eq!( get_response.ssekms_key_id().unwrap(), &default_key_id, "get_object response should contain correct KMS key ID" ); // Verify data integrity let downloaded_data = get_response .body .collect() .await .expect("Failed to collect body") .into_bytes(); assert_eq!(&downloaded_data[..], test_data, "Downloaded data should match original data"); // Cleanup is handled automatically when the test environment is dropped info!("Test passed: bucket default SSE-KMS encryption correctly applied to put_object"); Ok(()) } /// Test 3: When bucket is configured with default encryption, create_multipart_upload should inherit the configuration #[tokio::test] #[serial] async fn test_bucket_default_encryption_multipart_upload() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Testing bucket default encryption impact on create_multipart_upload"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Step 1: Set bucket default encryption to SSE-KMS info!("Setting bucket default encryption configuration to SSE-KMS"); let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::AwsKms) .kms_master_key_id(&default_key_id) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await .expect("Failed to set bucket encryption"); // Step 2: Create multipart upload (without specifying encryption parameters) info!("Creating multipart upload (without specifying encryption parameters, should use bucket default configuration)"); let test_key = "test-multipart-bucket-default.txt"; let create_multipart_response = s3_client .create_multipart_upload() .bucket(TEST_BUCKET) .key(test_key) // Note: No encryption parameters specified here, should use bucket default configuration .send() .await .expect("Failed to create multipart upload"); let upload_id = create_multipart_response.upload_id().unwrap(); debug!( "CreateMultipartUpload response: UploadId={}, SSE={:?}, KMS_Key={:?}", upload_id, create_multipart_response.server_side_encryption(), create_multipart_response.ssekms_key_id() ); // Verify: create_multipart_upload response should contain bucket default encryption configuration assert_eq!( create_multipart_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "create_multipart_upload response should contain bucket default SSE-KMS encryption information" ); assert_eq!( create_multipart_response.ssekms_key_id().unwrap(), &default_key_id, "create_multipart_upload response should contain correct KMS key ID" ); // Step 3: Upload a part and complete multipart upload info!("Uploading part and completing multipart upload"); let test_data = b"test-multipart-bucket-default-encryption-data"; // Upload part 1 let upload_part_response = s3_client .upload_part() .bucket(TEST_BUCKET) .key(test_key) .upload_id(upload_id) .part_number(1) .body(test_data.to_vec().into()) .send() .await .expect("Failed to upload part"); let etag = upload_part_response.e_tag().unwrap().to_string(); // Complete multipart upload let completed_part = aws_sdk_s3::types::CompletedPart::builder() .part_number(1) .e_tag(&etag) .build(); let complete_multipart_response = s3_client .complete_multipart_upload() .bucket(TEST_BUCKET) .key(test_key) .upload_id(upload_id) .multipart_upload( aws_sdk_s3::types::CompletedMultipartUpload::builder() .parts(completed_part) .build(), ) .send() .await .expect("Failed to complete multipart upload"); debug!( "CompleteMultipartUpload response: ETag={:?}, SSE={:?}, KMS_Key={:?}", complete_multipart_response.e_tag(), complete_multipart_response.server_side_encryption(), complete_multipart_response.ssekms_key_id() ); assert_eq!( complete_multipart_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "complete_multipart_upload response should contain SSE-KMS encryption information" ); // Step 4: Download file and verify encryption status info!("Downloading file and verifying encryption status"); let get_response = s3_client .get_object() .bucket(TEST_BUCKET) .key(test_key) .send() .await .expect("Failed to get object"); // Verify: Final object should be properly encrypted assert_eq!( get_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "Final object should contain SSE-KMS encryption information" ); // Verify data integrity let downloaded_data = get_response .body .collect() .await .expect("Failed to collect body") .into_bytes(); assert_eq!(&downloaded_data[..], test_data, "Downloaded data should match original data"); // Cleanup is handled automatically when the test environment is dropped info!("Test passed: bucket default encryption correctly applied to multipart upload"); Ok(()) } /// Test 4: Explicitly specified encryption parameters in requests should override bucket default configuration #[tokio::test] #[serial] async fn test_explicit_encryption_overrides_bucket_default() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Testing explicitly specified encryption parameters override bucket default configuration"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Step 1: Set bucket default encryption to SSE-S3 info!("Setting bucket default encryption configuration to SSE-S3"); let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::Aes256) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await .expect("Failed to set bucket encryption"); // Step 2: Explicitly specify SSE-KMS encryption (should override bucket default SSE-S3) info!("Uploading file (explicitly specifying SSE-KMS, should override bucket default SSE-S3)"); let test_data = b"test-explicit-override-data"; let test_key = "test-explicit-override.txt"; let put_response = s3_client .put_object() .bucket(TEST_BUCKET) .key(test_key) .body(test_data.to_vec().into()) // Explicitly specify SSE-KMS, should override bucket default SSE-S3 .server_side_encryption(ServerSideEncryption::AwsKms) .ssekms_key_id(&default_key_id) .send() .await .expect("Failed to put object with explicit SSE-KMS"); debug!( "PUT response: SSE={:?}, KMS_Key={:?}", put_response.server_side_encryption(), put_response.ssekms_key_id() ); // Verify: Should use explicitly specified SSE-KMS, not bucket default SSE-S3 assert_eq!( put_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "Explicitly specified SSE-KMS should override bucket default SSE-S3" ); assert_eq!( put_response.ssekms_key_id().unwrap(), &default_key_id, "Should use explicitly specified KMS key ID" ); // Verify GET response let get_response = s3_client .get_object() .bucket(TEST_BUCKET) .key(test_key) .send() .await .expect("Failed to get object"); assert_eq!( get_response.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "GET response should reflect the actually used SSE-KMS encryption" ); // Cleanup is handled automatically when the test environment is dropped info!("Test passed: explicitly specified encryption parameters correctly override bucket default configuration"); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/kms/encryption_metadata_test.rs
crates/e2e_test/src/kms/encryption_metadata_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Integration tests that focus on surface headers/metadata emitted by the //! managed encryption pipeline (SSE-S3/SSE-KMS). use super::common::LocalKMSTestEnvironment; use crate::common::{TEST_BUCKET, init_logging}; use aws_sdk_s3::primitives::ByteStream; use aws_sdk_s3::types::{ CompletedMultipartUpload, CompletedPart, ServerSideEncryption, ServerSideEncryptionByDefault, ServerSideEncryptionConfiguration, ServerSideEncryptionRule, }; use serial_test::serial; use std::collections::{HashMap, VecDeque}; use tracing::info; fn assert_encryption_metadata(metadata: &HashMap<String, String>, expected_size: usize) { for key in [ "x-rustfs-encryption-key", "x-rustfs-encryption-iv", "x-rustfs-encryption-context", "x-rustfs-encryption-original-size", ] { assert!(metadata.contains_key(key), "expected managed encryption metadata '{key}' to be present"); assert!( !metadata.get(key).unwrap().is_empty(), "managed encryption metadata '{key}' should not be empty" ); } let size_value = metadata .get("x-rustfs-encryption-original-size") .expect("managed encryption metadata should include original size"); let parsed_size: usize = size_value .parse() .expect("x-rustfs-encryption-original-size should be numeric"); assert_eq!(parsed_size, expected_size, "recorded original size should match uploaded payload length"); } fn assert_storage_encrypted(storage_root: &std::path::Path, bucket: &str, key: &str, plaintext: &[u8]) { let mut stack = VecDeque::from([storage_root.to_path_buf()]); let mut scanned = 0; let mut plaintext_path: Option<std::path::PathBuf> = None; while let Some(current) = stack.pop_front() { let Ok(metadata) = std::fs::metadata(&current) else { continue }; if metadata.is_dir() { if let Ok(entries) = std::fs::read_dir(&current) { for entry in entries.flatten() { stack.push_back(entry.path()); } } continue; } let path_str = current.to_string_lossy(); if !(path_str.contains(bucket) || path_str.contains(key)) { continue; } scanned += 1; let Ok(bytes) = std::fs::read(&current) else { continue }; if bytes.len() < plaintext.len() { continue; } if bytes.windows(plaintext.len()).any(|window| window == plaintext) { plaintext_path = Some(current); break; } } assert!( scanned > 0, "Failed to locate stored data files for bucket '{bucket}' and key '{key}' under {storage_root:?}" ); assert!(plaintext_path.is_none(), "Plaintext detected on disk at {:?}", plaintext_path.unwrap()); } #[tokio::test] #[serial] async fn test_head_reports_managed_metadata_for_sse_s3() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Validating SSE-S3 managed encryption metadata exposure"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Bucket level default SSE-S3 configuration. let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::Aes256) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await?; let payload = b"metadata-sse-s3-payload"; let key = "metadata-sse-s3-object"; s3_client .put_object() .bucket(TEST_BUCKET) .key(key) .body(payload.to_vec().into()) .send() .await?; let head = s3_client.head_object().bucket(TEST_BUCKET).key(key).send().await?; assert_eq!( head.server_side_encryption(), Some(&ServerSideEncryption::Aes256), "head_object should advertise SSE-S3" ); let metadata = head .metadata() .expect("head_object should return managed encryption metadata"); assert_encryption_metadata(metadata, payload.len()); assert_storage_encrypted(std::path::Path::new(&kms_env.base_env.temp_dir), TEST_BUCKET, key, payload); Ok(()) } #[tokio::test] #[serial] async fn test_head_reports_managed_metadata_for_sse_kms_and_copy() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Validating SSE-KMS managed encryption metadata (including copy)"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::AwsKms) .kms_master_key_id(&default_key_id) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await?; let payload = b"metadata-sse-kms-payload"; let source_key = "metadata-sse-kms-object"; s3_client .put_object() .bucket(TEST_BUCKET) .key(source_key) .body(payload.to_vec().into()) .send() .await?; let head_source = s3_client.head_object().bucket(TEST_BUCKET).key(source_key).send().await?; assert_eq!( head_source.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "source object should report SSE-KMS" ); assert_eq!( head_source.ssekms_key_id().unwrap(), &default_key_id, "source object should maintain the configured KMS key id" ); let source_metadata = head_source .metadata() .expect("source object should include managed encryption metadata"); assert_encryption_metadata(source_metadata, payload.len()); let dest_key = "metadata-sse-kms-object-copy"; let copy_source = format!("{TEST_BUCKET}/{source_key}"); s3_client .copy_object() .bucket(TEST_BUCKET) .key(dest_key) .copy_source(copy_source) .send() .await?; let head_dest = s3_client.head_object().bucket(TEST_BUCKET).key(dest_key).send().await?; assert_eq!( head_dest.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "copied object should remain encrypted with SSE-KMS" ); assert_eq!( head_dest.ssekms_key_id().unwrap(), &default_key_id, "copied object should keep the default KMS key id" ); let dest_metadata = head_dest .metadata() .expect("copied object should include managed encryption metadata"); assert_encryption_metadata(dest_metadata, payload.len()); let copied_body = s3_client .get_object() .bucket(TEST_BUCKET) .key(dest_key) .send() .await? .body .collect() .await? .into_bytes(); assert_eq!(&copied_body[..], payload, "copied object payload should match source"); let storage_root = std::path::Path::new(&kms_env.base_env.temp_dir); assert_storage_encrypted(storage_root, TEST_BUCKET, source_key, payload); assert_storage_encrypted(storage_root, TEST_BUCKET, dest_key, payload); Ok(()) } #[tokio::test] #[serial] async fn test_multipart_upload_writes_encrypted_data() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("Validating ciphertext persistence for multipart SSE-KMS uploads"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; let encryption_config = ServerSideEncryptionConfiguration::builder() .rules( ServerSideEncryptionRule::builder() .apply_server_side_encryption_by_default( ServerSideEncryptionByDefault::builder() .sse_algorithm(ServerSideEncryption::AwsKms) .kms_master_key_id(&default_key_id) .build() .unwrap(), ) .build(), ) .build() .unwrap(); s3_client .put_bucket_encryption() .bucket(TEST_BUCKET) .server_side_encryption_configuration(encryption_config) .send() .await?; let key = "multipart-encryption-object"; let part_size = 5 * 1024 * 1024; // minimum part size required by S3 semantics let part_one = vec![0xA5; part_size]; let part_two = vec![0x5A; part_size]; let combined: Vec<u8> = part_one.iter().chain(part_two.iter()).copied().collect(); let create_output = s3_client .create_multipart_upload() .bucket(TEST_BUCKET) .key(key) .send() .await?; let upload_id = create_output.upload_id().unwrap(); let part1 = s3_client .upload_part() .bucket(TEST_BUCKET) .key(key) .upload_id(upload_id) .part_number(1) .body(ByteStream::from(part_one.clone())) .send() .await?; let part2 = s3_client .upload_part() .bucket(TEST_BUCKET) .key(key) .upload_id(upload_id) .part_number(2) .body(ByteStream::from(part_two.clone())) .send() .await?; let completed = CompletedMultipartUpload::builder() .parts(CompletedPart::builder().part_number(1).e_tag(part1.e_tag().unwrap()).build()) .parts(CompletedPart::builder().part_number(2).e_tag(part2.e_tag().unwrap()).build()) .build(); s3_client .complete_multipart_upload() .bucket(TEST_BUCKET) .key(key) .upload_id(upload_id) .multipart_upload(completed) .send() .await?; let head = s3_client.head_object().bucket(TEST_BUCKET).key(key).send().await?; assert_eq!( head.server_side_encryption(), Some(&ServerSideEncryption::AwsKms), "multipart head_object should expose SSE-KMS" ); assert_eq!( head.ssekms_key_id().unwrap(), &default_key_id, "multipart object should retain bucket default KMS key" ); assert_encryption_metadata( head.metadata().expect("multipart head_object should expose managed metadata"), combined.len(), ); // Data returned to clients should decrypt back to original payload let fetched = s3_client .get_object() .bucket(TEST_BUCKET) .key(key) .send() .await? .body .collect() .await? .into_bytes(); assert_eq!(&fetched[..], &combined[..]); assert_storage_encrypted(std::path::Path::new(&kms_env.base_env.temp_dir), TEST_BUCKET, key, &combined); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false
rustfs/rustfs
https://github.com/rustfs/rustfs/blob/666c0a9a38636eb6653dff7d9c98ff7122601ce2/crates/e2e_test/src/kms/kms_fault_recovery_test.rs
crates/e2e_test/src/kms/kms_fault_recovery_test.rs
// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! KMS Fault Recovery and Error Handling Tests //! //! This test suite validates KMS behavior under failure conditions: //! - KMS service unavailability //! - Network interruptions during multipart uploads //! - Disk space limitations //! - Corrupted key files //! - Recovery from transient failures use super::common::LocalKMSTestEnvironment; use crate::common::{TEST_BUCKET, init_logging}; use aws_sdk_s3::types::ServerSideEncryption; use serial_test::serial; use std::fs; use std::time::Duration; use tokio::time::sleep; use tracing::{info, warn}; /// Test KMS behavior when key directory is temporarily unavailable #[tokio::test] #[serial] async fn test_kms_key_directory_unavailable() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS behavior with unavailable key directory"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // First, upload a normal encrypted file to verify KMS is working info!("πŸ“€ Uploading test file with KMS encryption"); let test_data = b"Test data before key directory issue"; let object_key = "test-before-key-issue"; let put_response = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; assert_eq!(put_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); // Temporarily rename the key directory to simulate unavailability info!("πŸ”§ Simulating key directory unavailability"); let backup_dir = format!("{}.backup", kms_env.kms_keys_dir); fs::rename(&kms_env.kms_keys_dir, &backup_dir)?; // Try to upload another file - this should fail gracefully info!("πŸ“€ Attempting upload with unavailable key directory"); let test_data2 = b"Test data during key directory issue"; let object_key2 = "test-during-key-issue"; let put_result2 = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key2) .body(aws_sdk_s3::primitives::ByteStream::from(test_data2.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await; // This should fail, but the server should still be responsive if put_result2.is_err() { info!("βœ… Upload correctly failed when key directory unavailable"); } else { warn!("⚠️ Upload succeeded despite unavailable key directory (may be using cached keys)"); } // Restore the key directory info!("πŸ”§ Restoring key directory"); fs::rename(&backup_dir, &kms_env.kms_keys_dir)?; // Wait a moment for KMS to detect the restored directory sleep(Duration::from_secs(2)).await; // Try uploading again - this should work info!("πŸ“€ Uploading after key directory restoration"); let test_data3 = b"Test data after key directory restoration"; let object_key3 = "test-after-key-restoration"; let put_response3 = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key3) .body(aws_sdk_s3::primitives::ByteStream::from(test_data3.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; assert_eq!(put_response3.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); // Verify we can still access the original file info!("πŸ“₯ Verifying access to original encrypted file"); let get_response = s3_client.get_object().bucket(TEST_BUCKET).key(object_key).send().await?; let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.as_ref(), test_data); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Key directory unavailability test completed successfully"); Ok(()) } /// Test handling of corrupted key files #[tokio::test] #[serial] async fn test_kms_corrupted_key_files() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS behavior with corrupted key files"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Upload a file with valid key info!("πŸ“€ Uploading file with valid key"); let test_data = b"Test data before key corruption"; let object_key = "test-before-corruption"; s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; // Corrupt the default key file info!("πŸ”§ Corrupting default key file"); let key_file_path = format!("{}/{}.key", kms_env.kms_keys_dir, default_key_id); let backup_key_path = format!("{key_file_path}.backup"); // Backup the original key file fs::copy(&key_file_path, &backup_key_path)?; // Write corrupted data to the key file fs::write(&key_file_path, b"corrupted key data")?; // Wait for potential key cache to expire sleep(Duration::from_secs(1)).await; // Try to upload with corrupted key - this should fail info!("πŸ“€ Attempting upload with corrupted key"); let test_data2 = b"Test data with corrupted key"; let object_key2 = "test-with-corrupted-key"; let put_result2 = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key2) .body(aws_sdk_s3::primitives::ByteStream::from(test_data2.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await; // This might succeed if KMS uses cached keys, but should eventually fail if put_result2.is_err() { info!("βœ… Upload correctly failed with corrupted key"); } else { warn!("⚠️ Upload succeeded despite corrupted key (likely using cached key)"); } // Restore the original key file info!("πŸ”§ Restoring original key file"); fs::copy(&backup_key_path, &key_file_path)?; fs::remove_file(&backup_key_path)?; // Wait for KMS to detect the restored key sleep(Duration::from_secs(2)).await; // Try uploading again - this should work info!("πŸ“€ Uploading after key restoration"); let test_data3 = b"Test data after key restoration"; let object_key3 = "test-after-key-restoration"; let put_response3 = s3_client .put_object() .bucket(TEST_BUCKET) .key(object_key3) .body(aws_sdk_s3::primitives::ByteStream::from(test_data3.to_vec())) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; assert_eq!(put_response3.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Corrupted key files test completed successfully"); Ok(()) } /// Test multipart upload interruption and recovery #[tokio::test] #[serial] async fn test_kms_multipart_upload_interruption() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS multipart upload interruption and recovery"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test data for multipart upload let part_size = 5 * 1024 * 1024; // 5MB per part let total_parts = 3; let total_size = part_size * total_parts; let test_data: Vec<u8> = (0..total_size).map(|i| (i % 256) as u8).collect(); let object_key = "multipart-interruption-test"; info!("πŸ“€ Starting multipart upload with encryption"); // Initiate multipart upload let create_multipart_output = s3_client .create_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; let upload_id = create_multipart_output.upload_id().unwrap(); info!("βœ… Multipart upload initiated with ID: {}", upload_id); // Upload first part successfully info!("πŸ“€ Uploading part 1"); let part1_data = &test_data[0..part_size]; let upload_part1_output = s3_client .upload_part() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id) .part_number(1) .body(aws_sdk_s3::primitives::ByteStream::from(part1_data.to_vec())) .send() .await?; let part1_etag = upload_part1_output.e_tag().unwrap().to_string(); info!("βœ… Part 1 uploaded successfully"); // Upload second part successfully info!("πŸ“€ Uploading part 2"); let part2_data = &test_data[part_size..part_size * 2]; let upload_part2_output = s3_client .upload_part() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id) .part_number(2) .body(aws_sdk_s3::primitives::ByteStream::from(part2_data.to_vec())) .send() .await?; let part2_etag = upload_part2_output.e_tag().unwrap().to_string(); info!("βœ… Part 2 uploaded successfully"); // Simulate interruption - we'll NOT upload part 3 and instead abort the upload info!("πŸ”§ Simulating upload interruption"); // Abort the multipart upload let abort_result = s3_client .abort_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id) .send() .await; match abort_result { Ok(_) => info!("βœ… Multipart upload aborted successfully"), Err(e) => warn!("⚠️ Failed to abort multipart upload: {}", e), } // Try to complete the aborted upload - this should fail info!("πŸ” Attempting to complete aborted upload"); let completed_parts = vec![ aws_sdk_s3::types::CompletedPart::builder() .part_number(1) .e_tag(&part1_etag) .build(), aws_sdk_s3::types::CompletedPart::builder() .part_number(2) .e_tag(&part2_etag) .build(), ]; let completed_multipart_upload = aws_sdk_s3::types::CompletedMultipartUpload::builder() .set_parts(Some(completed_parts)) .build(); let complete_result = s3_client .complete_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id) .multipart_upload(completed_multipart_upload) .send() .await; assert!(complete_result.is_err(), "Should not be able to complete aborted upload"); info!("βœ… Correctly failed to complete aborted upload"); // Start a new multipart upload and complete it successfully info!("πŸ“€ Starting new multipart upload"); let create_multipart_output2 = s3_client .create_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await?; let upload_id2 = create_multipart_output2.upload_id().unwrap(); // Upload all parts for the new upload let mut completed_parts2 = Vec::new(); for part_number in 1..=total_parts { let start = (part_number - 1) * part_size; let end = std::cmp::min(start + part_size, total_size); let part_data = &test_data[start..end]; let upload_part_output = s3_client .upload_part() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id2) .part_number(part_number as i32) .body(aws_sdk_s3::primitives::ByteStream::from(part_data.to_vec())) .send() .await?; let etag = upload_part_output.e_tag().unwrap().to_string(); completed_parts2.push( aws_sdk_s3::types::CompletedPart::builder() .part_number(part_number as i32) .e_tag(&etag) .build(), ); info!("βœ… Part {} uploaded successfully", part_number); } // Complete the new multipart upload let completed_multipart_upload2 = aws_sdk_s3::types::CompletedMultipartUpload::builder() .set_parts(Some(completed_parts2)) .build(); let _complete_output2 = s3_client .complete_multipart_upload() .bucket(TEST_BUCKET) .key(object_key) .upload_id(upload_id2) .multipart_upload(completed_multipart_upload2) .send() .await?; info!("βœ… New multipart upload completed successfully"); // Verify the completed upload let get_response = s3_client.get_object().bucket(TEST_BUCKET).key(object_key).send().await?; assert_eq!(get_response.server_side_encryption(), Some(&ServerSideEncryption::Aes256)); let downloaded_data = get_response.body.collect().await?.into_bytes(); assert_eq!(downloaded_data.len(), total_size); assert_eq!(&downloaded_data[..], &test_data[..]); info!("βœ… Downloaded data matches original test data"); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Multipart upload interruption test completed successfully"); Ok(()) } /// Test KMS resilience to temporary resource constraints #[tokio::test] #[serial] async fn test_kms_resource_constraints() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { init_logging(); info!("πŸ§ͺ Testing KMS behavior under resource constraints"); let mut kms_env = LocalKMSTestEnvironment::new().await?; let _default_key_id = kms_env.start_rustfs_for_local_kms().await?; tokio::time::sleep(Duration::from_secs(3)).await; let s3_client = kms_env.base_env.create_s3_client(); kms_env.base_env.create_test_bucket(TEST_BUCKET).await?; // Test multiple rapid encryption requests info!("πŸ“€ Testing rapid successive encryption requests"); let mut upload_tasks = Vec::new(); for i in 0..10 { let client = s3_client.clone(); let test_data = format!("Rapid test data {i}").into_bytes(); let object_key = format!("rapid-test-{i}"); let task = tokio::spawn(async move { let result = client .put_object() .bucket(TEST_BUCKET) .key(&object_key) .body(aws_sdk_s3::primitives::ByteStream::from(test_data)) .server_side_encryption(ServerSideEncryption::Aes256) .send() .await; (object_key, result) }); upload_tasks.push(task); } // Wait for all uploads to complete let mut successful_uploads = 0; let mut failed_uploads = 0; for task in upload_tasks { let (object_key, result) = task.await.unwrap(); match result { Ok(_) => { successful_uploads += 1; info!("βœ… Rapid upload {} succeeded", object_key); } Err(e) => { failed_uploads += 1; warn!("❌ Rapid upload {} failed: {}", object_key, e); } } } info!("πŸ“Š Rapid upload results: {} succeeded, {} failed", successful_uploads, failed_uploads); // We expect most uploads to succeed even under load assert!(successful_uploads >= 7, "Expected at least 7/10 rapid uploads to succeed"); kms_env.base_env.delete_test_bucket(TEST_BUCKET).await?; info!("βœ… Resource constraints test completed successfully"); Ok(()) }
rust
Apache-2.0
666c0a9a38636eb6653dff7d9c98ff7122601ce2
2026-01-04T15:42:12.458416Z
false