mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-15 16:28:30 +01:00
* remote: reuse one guarded remote storage client per configuration BuildGuardedRemoteStorageClient (the filer's lazy remote stat and list, remote deletes and renames, the volume server's cold reads, the S3 gateway's remote reads) built a new client, with its own http.Transport, on every call. Each transport kept its connection idle for IdleConnTimeout (60s) and was never reused or closed, so a steady stream of remote lookups held one open connection per request. An endpoint that caps connections per source then refuses every new connection. - remote_storage: GetRemoteStorage's per-name cache becomes a bounded LRU (WEED_REMOTE_CLIENT_CACHE_MAX, default 64) keyed by kind, name, a hash of the whole configuration and a credentials version, built outside the lock once per key (singleflight) from a copy of the configuration. A client leaving the cache has its idle connections closed. GetRemoteStorageWith exposes the cache to other builders. - BuildGuardedRemoteStorageClient caches the guarded client per configuration. The endpoint is checked against the deny-list when a client is built; every new connection still dials through the guarded dialer. A gcs credentials file, and for an S3-compatible remote using the default credential chain the shared credentials file and AWS_* variables, are part of the key by content. - The shared transport keeps 32 idle connections per host, caps connections per host (WEED_REMOTE_MAX_CONNS_PER_HOST, default 64) and has dial, TLS handshake and response header deadlines, so a hung remote frees its slots. - The filer drops the clients of removed remotes (and all but the newest client per name) on every /etc/remote reload, also when /etc/remote does not exist; reloads are serialized. * remote: drop clients whose build spans a reload or whose remote is gone * server: hash the shared AWS config file into the guarded credential version * server: bound a stalled body on the shared remote transport * remote: waive path-injection on the configured credentials file * remote: keep caching remotes the local filer does not name * remote: drop the inline codeql waiver the analyzer ignores * remote: version the plain gcs client's credentials file * remote: hash the environment's inline gcs credentials GOOGLE_APPLICATION_CREDENTIALS can hold the JSON itself, which the gcs client accepts; it is not in the configuration hash, so a changed value kept the old client. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * remote: version the well-known gcs credentials file With neither the configuration nor GOOGLE_APPLICATION_CREDENTIALS set, the SDK reads application_default_credentials.json; rotating it left the cache key unchanged and the old client behind. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * remote: share the gcs credentials version with the guarded client The guarded gcs client resolves the same environment and well-known credentials as the plain one; keying it only on a configured path left those rotations invisible to the cache. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * remote: version s3 keys resolved from the environment A remote whose access and secret keys both arrive through its type's environment fallback (aliyun, baidu, contabo, filebase, storj, tencent) never entered the default-chain branch of guardedCredentialsVersion, so an in-process environment change kept serving the stale cached client. The version now hashes the resolved keys for every S3-compatible remote. * volume: reuse one remote storage client per configuration (rust) fetch_and_write_needle built a fresh aws_sdk_s3::Client per request, so every cold read kept its connector's idle connections without ever reusing them — the same leak the Go side fixes. Cache clients keyed by the configuration's protobuf encoding in an LRU of WEED_REMOTE_CLIENT_CACHE_MAX entries (default 64), mirroring weed/remote_storage/client_cache.go. An evicted entry drops the client's last Arc and with it the connector's idle connections. The per-request endpoint validation is unchanged: Rust has no dial-time guard yet, so the up-front DNS check stays on every call. --------- Co-authored-by: user.email <chrislusf@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
249 lines
8.7 KiB
Rust
249 lines
8.7 KiB
Rust
//! Remote storage backends for tiered storage support.
|
|
//!
|
|
//! Provides a trait-based abstraction over cloud storage providers (S3, GCS, Azure, etc.)
|
|
//! and a registry to create clients from protobuf RemoteConf messages.
|
|
|
|
pub mod client_cache;
|
|
pub mod endpoint_guard;
|
|
pub mod s3;
|
|
pub mod s3_tier;
|
|
|
|
pub use client_cache::get_remote_storage_client;
|
|
pub use endpoint_guard::{guarded_tcp_connect, validate_remote_endpoint, validate_replica_target};
|
|
|
|
use crate::pb::remote_pb::{RemoteConf, RemoteStorageLocation};
|
|
|
|
/// Error type for remote storage operations.
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum RemoteStorageError {
|
|
#[error("remote storage type {0} not found")]
|
|
TypeNotFound(String),
|
|
#[error("remote object not found: {0}")]
|
|
ObjectNotFound(String),
|
|
#[error("remote storage error: {0}")]
|
|
Other(String),
|
|
#[error("io error: {0}")]
|
|
Io(#[from] std::io::Error),
|
|
}
|
|
|
|
/// Metadata about a remote file entry.
|
|
#[derive(Debug, Clone)]
|
|
pub struct RemoteEntry {
|
|
pub size: i64,
|
|
pub last_modified_at: i64, // Unix seconds
|
|
pub e_tag: String,
|
|
pub storage_name: String,
|
|
}
|
|
|
|
/// Trait for remote storage clients. Matches Go's RemoteStorageClient interface.
|
|
#[async_trait::async_trait]
|
|
pub trait RemoteStorageClient: Send + Sync {
|
|
/// Read (part of) a file from remote storage.
|
|
async fn read_file(
|
|
&self,
|
|
loc: &RemoteStorageLocation,
|
|
offset: i64,
|
|
size: i64,
|
|
) -> Result<Vec<u8>, RemoteStorageError>;
|
|
|
|
/// Write a file to remote storage.
|
|
async fn write_file(
|
|
&self,
|
|
loc: &RemoteStorageLocation,
|
|
data: &[u8],
|
|
) -> Result<RemoteEntry, RemoteStorageError>;
|
|
|
|
/// Get metadata for a file in remote storage.
|
|
async fn stat_file(
|
|
&self,
|
|
loc: &RemoteStorageLocation,
|
|
) -> Result<RemoteEntry, RemoteStorageError>;
|
|
|
|
/// Delete a file from remote storage.
|
|
async fn delete_file(&self, loc: &RemoteStorageLocation) -> Result<(), RemoteStorageError>;
|
|
|
|
/// List all buckets.
|
|
async fn list_buckets(&self) -> Result<Vec<String>, RemoteStorageError>;
|
|
|
|
/// The RemoteConf used to create this client.
|
|
fn remote_conf(&self) -> &RemoteConf;
|
|
}
|
|
|
|
/// Create a new remote storage client from a RemoteConf.
|
|
pub fn make_remote_storage_client(
|
|
conf: &RemoteConf,
|
|
) -> Result<Box<dyn RemoteStorageClient>, RemoteStorageError> {
|
|
match conf.r#type.as_str() {
|
|
// All S3-compatible backends use the same client with different credentials
|
|
"s3" | "wasabi" | "backblaze" | "aliyun" | "tencent" | "baidu" | "filebase" | "storj"
|
|
| "contabo" => {
|
|
let (access_key, secret_key, endpoint, region) = extract_s3_credentials(conf);
|
|
Ok(Box::new(s3::S3RemoteStorageClient::new(
|
|
conf.clone(),
|
|
&access_key,
|
|
&secret_key,
|
|
®ion,
|
|
&endpoint,
|
|
conf.s3_force_path_style,
|
|
)))
|
|
}
|
|
other => Err(RemoteStorageError::TypeNotFound(other.to_string())),
|
|
}
|
|
}
|
|
|
|
/// Endpoint URL that the volume server would dial directly for `conf`, or
|
|
/// `None` for non-S3-compatible backends. Every type handled here routes
|
|
/// through [`s3::S3RemoteStorageClient`] with a caller-supplied endpoint, so
|
|
/// the SSRF guard must validate each one. Keep this match in sync with
|
|
/// [`make_remote_storage_client`].
|
|
pub fn s3_compatible_endpoint(conf: &RemoteConf) -> Option<&str> {
|
|
match conf.r#type.as_str() {
|
|
"s3" => Some(&conf.s3_endpoint),
|
|
"wasabi" => Some(&conf.wasabi_endpoint),
|
|
"backblaze" => Some(&conf.backblaze_endpoint),
|
|
"aliyun" => Some(&conf.aliyun_endpoint),
|
|
"tencent" => Some(&conf.tencent_endpoint),
|
|
"baidu" => Some(&conf.baidu_endpoint),
|
|
"filebase" => Some(&conf.filebase_endpoint),
|
|
"storj" => Some(&conf.storj_endpoint),
|
|
"contabo" => Some(&conf.contabo_endpoint),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Extract S3-compatible credentials from a RemoteConf based on its type.
|
|
fn extract_s3_credentials(conf: &RemoteConf) -> (String, String, String, String) {
|
|
match conf.r#type.as_str() {
|
|
"s3" => (
|
|
conf.s3_access_key.clone(),
|
|
conf.s3_secret_key.clone(),
|
|
conf.s3_endpoint.clone(),
|
|
if conf.s3_region.is_empty() {
|
|
"us-east-1".to_string()
|
|
} else {
|
|
conf.s3_region.clone()
|
|
},
|
|
),
|
|
"wasabi" => (
|
|
conf.wasabi_access_key.clone(),
|
|
conf.wasabi_secret_key.clone(),
|
|
conf.wasabi_endpoint.clone(),
|
|
conf.wasabi_region.clone(),
|
|
),
|
|
"backblaze" => (
|
|
conf.backblaze_key_id.clone(),
|
|
conf.backblaze_application_key.clone(),
|
|
conf.backblaze_endpoint.clone(),
|
|
conf.backblaze_region.clone(),
|
|
),
|
|
"aliyun" => (
|
|
conf.aliyun_access_key.clone(),
|
|
conf.aliyun_secret_key.clone(),
|
|
conf.aliyun_endpoint.clone(),
|
|
conf.aliyun_region.clone(),
|
|
),
|
|
"tencent" => (
|
|
conf.tencent_secret_id.clone(),
|
|
conf.tencent_secret_key.clone(),
|
|
conf.tencent_endpoint.clone(),
|
|
String::new(),
|
|
),
|
|
"baidu" => (
|
|
conf.baidu_access_key.clone(),
|
|
conf.baidu_secret_key.clone(),
|
|
conf.baidu_endpoint.clone(),
|
|
conf.baidu_region.clone(),
|
|
),
|
|
"filebase" => (
|
|
conf.filebase_access_key.clone(),
|
|
conf.filebase_secret_key.clone(),
|
|
conf.filebase_endpoint.clone(),
|
|
String::new(),
|
|
),
|
|
"storj" => (
|
|
conf.storj_access_key.clone(),
|
|
conf.storj_secret_key.clone(),
|
|
conf.storj_endpoint.clone(),
|
|
String::new(),
|
|
),
|
|
"contabo" => (
|
|
conf.contabo_access_key.clone(),
|
|
conf.contabo_secret_key.clone(),
|
|
conf.contabo_endpoint.clone(),
|
|
conf.contabo_region.clone(),
|
|
),
|
|
_ => (
|
|
conf.s3_access_key.clone(),
|
|
conf.s3_secret_key.clone(),
|
|
conf.s3_endpoint.clone(),
|
|
conf.s3_region.clone(),
|
|
),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn s3_compatible_endpoint_covers_all_s3_backends() {
|
|
let s3 = RemoteConf {
|
|
r#type: "s3".to_string(),
|
|
s3_endpoint: "http://s3.internal".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert_eq!(s3_compatible_endpoint(&s3), Some("http://s3.internal"));
|
|
|
|
// A non-"s3" S3-compatible type still surfaces its own endpoint, so the
|
|
// SSRF guard cannot be bypassed by picking a different alias.
|
|
let wasabi = RemoteConf {
|
|
r#type: "wasabi".to_string(),
|
|
wasabi_endpoint: "http://wasabi.internal".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert_eq!(
|
|
s3_compatible_endpoint(&wasabi),
|
|
Some("http://wasabi.internal")
|
|
);
|
|
|
|
// Non-S3 backends do not dial a caller-supplied URL directly.
|
|
let gcs = RemoteConf {
|
|
r#type: "gcs".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert_eq!(s3_compatible_endpoint(&gcs), None);
|
|
}
|
|
|
|
#[test]
|
|
fn azure_endpoint_has_no_ssrf_path() {
|
|
// The Go volume server guards the caller-supplied azure endpoint against
|
|
// SSRF. This server has no azure backend, so there is nothing to dial:
|
|
// azure is not S3-compatible (the endpoint guard does not apply) and
|
|
// make_remote_storage_client rejects the type before building a client.
|
|
let azure = RemoteConf {
|
|
r#type: "azure".to_string(),
|
|
azure_endpoint: "https://169.254.169.254/".to_string(),
|
|
..Default::default()
|
|
};
|
|
assert_eq!(s3_compatible_endpoint(&azure), None);
|
|
assert!(make_remote_storage_client(&azure).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn gcs_credentials_have_no_ssrf_path() {
|
|
// The Go volume server accepts only static-key gcs credentials and puts
|
|
// their token endpoint behind the SSRF guard, because the SDK dials
|
|
// whatever url, file or executable the credentials name. This server has
|
|
// no gcs backend, so make_remote_storage_client rejects the type before
|
|
// any credentials are parsed. Anyone adding one must carry both guards
|
|
// over with it.
|
|
let gcs = RemoteConf {
|
|
r#type: "gcs".to_string(),
|
|
gcs_google_application_credentials: r#"{"type":"external_account","credential_source":{"url":"http://169.254.169.254/"}}"#.to_string(),
|
|
..Default::default()
|
|
};
|
|
assert_eq!(s3_compatible_endpoint(&gcs), None);
|
|
assert!(make_remote_storage_client(&gcs).is_err());
|
|
}
|
|
}
|