This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/refactor-http-transport-cache-remaining-services in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 7da2a659eb6d4cfd0b59aab48bffde78e132d77d Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 23 21:24:39 2026 +0100 refactor(services): cache signers via raw::HttpTransportCache in remaining reqsign services --- core/services/azblob/src/backend.rs | 15 ++-- core/services/azblob/src/core.rs | 40 +++++---- core/services/azdls/src/backend.rs | 8 +- core/services/azdls/src/core.rs | 21 +++-- core/services/azfile/src/backend.rs | 18 ++--- core/services/azfile/src/core.rs | 40 ++++++--- core/services/gcs/src/backend.rs | 24 ++---- core/services/gcs/src/composer.rs | 10 +-- core/services/gcs/src/core.rs | 44 ++++++---- core/services/gcs/src/writer.rs | 15 ++-- core/services/ghac/src/writer.rs | 11 +-- core/services/obs/src/backend.rs | 15 +++- core/services/obs/src/core.rs | 31 ++++--- core/services/oss/src/backend.rs | 157 +++++++++++++++++++++++++++++++----- core/services/oss/src/core.rs | 30 +++++-- core/services/tos/src/backend.rs | 22 ++--- core/services/tos/src/core.rs | 35 +++++--- 17 files changed, 358 insertions(+), 178 deletions(-) diff --git a/core/services/azblob/src/backend.rs b/core/services/azblob/src/backend.rs index 4ec8fc8e51..4f9308431c 100644 --- a/core/services/azblob/src/backend.rs +++ b/core/services/azblob/src/backend.rs @@ -30,7 +30,6 @@ use reqsign_azure_storage::StaticCredentialProvider; use reqsign_core::Context; use reqsign_core::OsEnv; use reqsign_core::ProvideCredentialChain; -use reqsign_core::Signer; use reqsign_file_read_tokio::TokioFileRead; use sha2::Digest; use sha2::Sha256; @@ -529,11 +528,8 @@ impl AzblobBuilder { credential_providers = customized_credential_chain; } - let signer = Signer::new( - ctx, - credential_providers, - RequestSigner::new().with_service_sas_permissions("racwd"), - ); + let credential_provider = Arc::new(credential_providers); + let request_signer = Arc::new(RequestSigner::new().with_service_sas_permissions("racwd")); // SharedKey Service SAS signing in `reqsign-azure-storage` derives the // canonicalized resource from the first URL path segment, which only @@ -605,7 +601,10 @@ impl AzblobBuilder { encryption_algorithm, container: self.config.container.clone(), skip_signature: self.config.skip_signature, - signer, + sign_ctx: ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), }), }) } @@ -1169,7 +1168,7 @@ mod tests { let ctx = Context::new() .with_env(reqsign_core::StaticEnv::default()) .with_http_send(http.clone()); - let signer = backend.core.signer.clone().with_context(ctx); + let signer = backend.core.signer_with_context(ctx); let (mut parts, _) = http::Request::get("https://myaccount.blob.core.windows.net/test/a") .body(()) diff --git a/core/services/azblob/src/core.rs b/core/services/azblob/src/core.rs index 740600b7b0..c0b16a7e64 100644 --- a/core/services/azblob/src/core.rs +++ b/core/services/azblob/src/core.rs @@ -16,6 +16,7 @@ // under the License. use std::fmt::Debug; +use std::sync::Arc; use base64::Engine; use base64::prelude::BASE64_STANDARD; @@ -35,8 +36,8 @@ use http::header::IF_NONE_MATCH; use http::header::IF_UNMODIFIED_SINCE; use opendal_service_azure_common::with_azure_error_response_context; use quick_xml::de; -use reqsign_azure_storage::Credential; -use reqsign_core::{Context, Signer}; +use reqsign_azure_storage::{Credential, RequestSigner}; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; use serde::Deserialize; use serde::Serialize; use uuid::Uuid; @@ -82,7 +83,10 @@ pub struct AzblobCore { pub encryption_key_sha256: Option<HeaderValue>, pub encryption_algorithm: Option<HeaderValue>, pub skip_signature: bool, - pub signer: Signer<Credential>, + pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, } impl Debug for AzblobCore { @@ -97,11 +101,21 @@ impl Debug for AzblobCore { impl AzblobCore { fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - Context::new() - .with_file_read(reqsign_file_read_tokio::TokioFileRead) - .with_http_send(ctx.http_transport().clone()) - .with_env(reqsign_core::OsEnv), + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) + } + + #[cfg(test)] + pub(crate) fn signer_with_context(&self, ctx: Context) -> Signer<Credential> { + Signer::new( + ctx, + self.credential_provider.clone(), + self.request_signer.clone(), ) } @@ -944,7 +958,6 @@ mod tests { use super::*; use std::collections::HashMap; - use reqsign_azure_storage::RequestSigner; use reqsign_azure_storage::StaticCredentialProvider; fn test_core() -> AzblobCore { @@ -958,11 +971,10 @@ mod tests { encryption_key_sha256: None, encryption_algorithm: None, skip_signature: true, - signer: Signer::new( - Context::new(), - StaticCredentialProvider::new_shared_key("acc", "a2V5"), - RequestSigner::new(), - ), + sign_ctx: Context::new(), + credential_provider: Arc::new(StaticCredentialProvider::new_shared_key("acc", "a2V5")), + request_signer: Arc::new(RequestSigner::new()), + signers: HttpTransportCache::new(), } } diff --git a/core/services/azdls/src/backend.rs b/core/services/azdls/src/backend.rs index 2358d61552..bcd09cf79c 100644 --- a/core/services/azdls/src/backend.rs +++ b/core/services/azdls/src/backend.rs @@ -28,7 +28,6 @@ use reqsign_azure_storage::StaticCredentialProvider; use reqsign_core::Context; use reqsign_core::OsEnv; use reqsign_core::ProvideCredentialChain; -use reqsign_core::Signer; use reqsign_file_read_tokio::TokioFileRead; use super::AZDLS_SCHEME; @@ -317,7 +316,8 @@ impl Builder for AzdlsBuilder { } let sign_ctx = ctx; - let signer = Signer::new(sign_ctx.clone(), credential_providers, RequestSigner::new()); + let credential_provider = Arc::new(credential_providers); + let request_signer = Arc::new(RequestSigner::new()); let info = ServiceInfo::new(AZDLS_SCHEME, &root, filesystem); let capability = Capability { @@ -363,8 +363,10 @@ impl Builder for AzdlsBuilder { root, endpoint, enable_hns: self.config.enable_hns, - signer, sign_ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), }), }) } diff --git a/core/services/azdls/src/core.rs b/core/services/azdls/src/core.rs index 89b1f4b692..59467e6cd2 100644 --- a/core/services/azdls/src/core.rs +++ b/core/services/azdls/src/core.rs @@ -17,6 +17,7 @@ use std::collections::HashMap; use std::fmt::Debug; +use std::sync::Arc; use base64::Engine; use base64::prelude::BASE64_STANDARD; @@ -32,8 +33,8 @@ use http::header::IF_MATCH; use http::header::IF_MODIFIED_SINCE; use http::header::IF_NONE_MATCH; use http::header::IF_UNMODIFIED_SINCE; -use reqsign_azure_storage::Credential; -use reqsign_core::{Context, Signer}; +use reqsign_azure_storage::{Credential, RequestSigner}; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; use opendal_core::raw::*; use opendal_core::*; @@ -56,8 +57,10 @@ pub struct AzdlsCore { pub endpoint: String, pub enable_hns: bool, - pub signer: Signer<Credential>, pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, } impl Debug for AzdlsCore { @@ -73,11 +76,13 @@ impl Debug for AzdlsCore { impl AzdlsCore { fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - self.sign_ctx - .clone() - .with_http_send(ctx.http_transport().clone()), - ) + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) } pub async fn sign<T>(&self, ctx: &OperationContext, req: Request<T>) -> Result<Request<T>> { diff --git a/core/services/azfile/src/backend.rs b/core/services/azfile/src/backend.rs index 19dd059e6e..f4ea2f1d25 100644 --- a/core/services/azfile/src/backend.rs +++ b/core/services/azfile/src/backend.rs @@ -28,7 +28,6 @@ use reqsign_azure_storage::StaticCredentialProvider; use reqsign_core::Context; use reqsign_core::OsEnv; use reqsign_core::ProvideCredentialChain; -use reqsign_core::Signer; use reqsign_file_read_tokio::TokioFileRead; use super::AZFILE_SCHEME; @@ -330,7 +329,8 @@ impl AzfileBuilder { } let sign_ctx = ctx; - let signer = Signer::new(sign_ctx.clone(), credential_providers, RequestSigner::new()); + let credential_provider = Arc::new(credential_providers); + let request_signer = Arc::new(RequestSigner::new()); let info = ServiceInfo::new(AZFILE_SCHEME, &root, ""); let capability = Capability { @@ -361,8 +361,10 @@ impl AzfileBuilder { capability, root, endpoint, - signer, sign_ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), share_name: self.config.share_name.clone(), sas_token: rename_source_sas_token, skip_signature: self.config.skip_signature, @@ -602,9 +604,7 @@ mod tests { let signer = backend .core - .signer - .clone() - .with_context(Context::new().with_env(reqsign_core::StaticEnv::default())); + .signer_with_context(Context::new().with_env(reqsign_core::StaticEnv::default())); let (mut parts, _) = http::Request::get("http://127.0.0.1:10002/share/file.txt") .body(()) .unwrap() @@ -664,9 +664,7 @@ mod tests { let signer = backend .core - .signer - .clone() - .with_context(Context::new().with_env(reqsign_core::StaticEnv::default())); + .signer_with_context(Context::new().with_env(reqsign_core::StaticEnv::default())); let (mut parts, _) = http::Request::get("http://127.0.0.1:10002/share/file.txt") .body(()) .unwrap() @@ -695,7 +693,7 @@ mod tests { let ctx = Context::new() .with_env(reqsign_core::StaticEnv::default()) .with_http_send(http.clone()); - let signer = backend.core.signer.clone().with_context(ctx); + let signer = backend.core.signer_with_context(ctx); let (mut parts, _) = http::Request::get("http://127.0.0.1:10002/share/file.txt") .body(()) diff --git a/core/services/azfile/src/core.rs b/core/services/azfile/src/core.rs index 65ed66c077..9ee343e675 100644 --- a/core/services/azfile/src/core.rs +++ b/core/services/azfile/src/core.rs @@ -17,6 +17,7 @@ use std::collections::VecDeque; use std::fmt::Debug; +use std::sync::Arc; use http::HeaderName; use http::HeaderValue; @@ -27,8 +28,8 @@ use http::header::CONTENT_DISPOSITION; use http::header::CONTENT_LENGTH; use http::header::CONTENT_TYPE; use http::header::RANGE; -use reqsign_azure_storage::Credential; -use reqsign_core::{Context, Signer}; +use reqsign_azure_storage::{Credential, RequestSigner}; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; use opendal_core::raw::*; use opendal_core::*; @@ -56,8 +57,10 @@ pub struct AzfileCore { pub share_name: String, pub sas_token: Option<String>, pub skip_signature: bool, - pub signer: Signer<Credential>, pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, } impl Debug for AzfileCore { @@ -72,10 +75,21 @@ impl Debug for AzfileCore { impl AzfileCore { fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - self.sign_ctx - .clone() - .with_http_send(ctx.http_transport().clone()), + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) + } + + #[cfg(test)] + pub(crate) fn signer_with_context(&self, ctx: Context) -> Signer<Credential> { + Signer::new( + ctx, + self.credential_provider.clone(), + self.request_signer.clone(), ) } @@ -672,7 +686,7 @@ pub(crate) fn parse_error(ctx: ErrorContext, resp: Response<Buffer>) -> Error { #[cfg(test)] mod tests { - use reqsign_azure_storage::{RequestSigner, StaticCredentialProvider}; + use reqsign_azure_storage::StaticCredentialProvider; use super::*; @@ -687,12 +701,12 @@ mod tests { share_name: "share".to_string(), sas_token: normalize_sas_token(sas_token).map(str::to_string), skip_signature: false, - signer: Signer::new( - Context::new(), - StaticCredentialProvider::new_shared_key("acc", "a2V5"), - RequestSigner::new(), - ), sign_ctx: Context::new(), + credential_provider: Arc::new(StaticCredentialProvider::new_shared_key( + "acc", "a2V5", + )), + request_signer: Arc::new(RequestSigner::new()), + signers: HttpTransportCache::new(), }; let req = core.azfile_rename_request("a", "b").expect("must build"); let source = &req.headers()[X_MS_FILE_RENAME_SOURCE]; diff --git a/core/services/gcs/src/backend.rs b/core/services/gcs/src/backend.rs index cad6b434af..7a29848c52 100644 --- a/core/services/gcs/src/backend.rs +++ b/core/services/gcs/src/backend.rs @@ -23,7 +23,6 @@ use reqsign_core::Context; use reqsign_core::OsEnv; use reqsign_core::ProvideCredential; use reqsign_core::ProvideCredentialChain; -use reqsign_core::Signer; use reqsign_file_read_tokio::TokioFileRead; use reqsign_google::Credential; use reqsign_google::DefaultCredentialProvider; @@ -330,11 +329,8 @@ impl GcsBuilder { } let sign_ctx = ctx; - let signer = Signer::new( - sign_ctx.clone(), - credential_chain, - RequestSigner::new("storage").with_scope(&scope), - ); + let credential_provider = Arc::new(credential_chain); + let request_signer = Arc::new(RequestSigner::new("storage").with_scope(&scope)); let info = ServiceInfo::new(GCS_SCHEME, &root, bucket); let capability = Capability { @@ -429,8 +425,10 @@ impl GcsBuilder { endpoint, bucket: bucket.to_string(), root, - signer, sign_ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), predefined_acl: self.config.predefined_acl.clone(), default_storage_class: self.config.default_storage_class.clone(), skip_signature, @@ -770,9 +768,7 @@ mod tests { backend .core - .signer - .clone() - .with_context(ctx) + .signer_with_context(ctx) .sign(&mut parts, None) .await .unwrap(); @@ -836,9 +832,7 @@ mod tests { backend .core - .signer - .clone() - .with_context(ctx) + .signer_with_context(ctx) .sign(&mut parts, None) .await .unwrap(); @@ -901,9 +895,7 @@ mod tests { let err = backend .core - .signer - .clone() - .with_context(ctx) + .signer_with_context(ctx) .sign(&mut parts, None) .await .unwrap_err(); diff --git a/core/services/gcs/src/composer.rs b/core/services/gcs/src/composer.rs index debf9ecf80..1dd926f727 100644 --- a/core/services/gcs/src/composer.rs +++ b/core/services/gcs/src/composer.rs @@ -376,7 +376,6 @@ mod tests { use http::Response; use reqsign_core::Context; use reqsign_core::ProvideCredentialChain; - use reqsign_core::Signer; use reqsign_google::RequestSigner; use reqsign_google::TokenCredentialProvider; @@ -521,7 +520,6 @@ mod tests { } fn test_core() -> Arc<GcsCore> { - let sign_ctx = Context::new(); Arc::new(GcsCore { info: ServiceInfo::new("gcs", "/", "test-bucket"), capability: Capability { @@ -537,12 +535,12 @@ mod tests { endpoint: "https://storage.googleapis.com".to_string(), bucket: "test-bucket".to_string(), root: "/".to_string(), - signer: Signer::new( - sign_ctx.clone(), + sign_ctx: Context::new(), + credential_provider: Arc::new( ProvideCredentialChain::new().push(TokenCredentialProvider::new("test-token")), - RequestSigner::new("storage"), ), - sign_ctx, + request_signer: Arc::new(RequestSigner::new("storage")), + signers: HttpTransportCache::new(), predefined_acl: None, default_storage_class: None, skip_signature: true, diff --git a/core/services/gcs/src/core.rs b/core/services/gcs/src/core.rs index c8f69d9adf..cd78662cfd 100644 --- a/core/services/gcs/src/core.rs +++ b/core/services/gcs/src/core.rs @@ -18,6 +18,7 @@ use std::collections::HashMap; use std::fmt::Debug; use std::fmt::Write; +use std::sync::Arc; use bytes::Buf; use bytes::Bytes; @@ -37,8 +38,8 @@ use http::header::IF_MATCH; use http::header::IF_MODIFIED_SINCE; use http::header::IF_NONE_MATCH; use http::header::IF_UNMODIFIED_SINCE; -use reqsign_core::{Context, Signer}; -use reqsign_google::Credential; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; +use reqsign_google::{Credential, RequestSigner}; use serde::Deserialize; use serde::Serialize; use serde_json::de; @@ -73,8 +74,10 @@ pub struct GcsCore { pub bucket: String, pub root: String, - pub signer: Signer<Credential>, pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, pub predefined_acl: Option<String>, pub default_storage_class: Option<String>, @@ -94,10 +97,21 @@ impl Debug for GcsCore { impl GcsCore { fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - self.sign_ctx - .clone() - .with_http_send(ctx.http_transport().clone()), + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) + } + + #[cfg(test)] + pub(crate) fn signer_with_context(&self, ctx: Context) -> Signer<Credential> { + Signer::new( + ctx, + self.credential_provider.clone(), + self.request_signer.clone(), ) } @@ -1360,17 +1374,9 @@ struct GetObjectJsonResponse { mod tests { use super::*; use reqsign_core::ProvideCredentialChain; - use reqsign_google::RequestSigner; use reqsign_google::TokenCredentialProvider; fn test_core() -> GcsCore { - let sign_ctx = Context::new(); - let signer = Signer::new( - sign_ctx.clone(), - ProvideCredentialChain::new().push(TokenCredentialProvider::new("test-token")), - RequestSigner::new("storage"), - ); - GcsCore { info: ServiceInfo::new("gcs", "/", "test-bucket"), capability: Capability { @@ -1412,8 +1418,12 @@ mod tests { endpoint: "https://storage.googleapis.com".to_string(), bucket: "test-bucket".to_string(), root: "/".to_string(), - signer, - sign_ctx, + sign_ctx: Context::new(), + credential_provider: Arc::new( + ProvideCredentialChain::new().push(TokenCredentialProvider::new("test-token")), + ), + request_signer: Arc::new(RequestSigner::new("storage")), + signers: HttpTransportCache::new(), predefined_acl: None, default_storage_class: None, skip_signature: false, diff --git a/core/services/gcs/src/writer.rs b/core/services/gcs/src/writer.rs index 43fe6a63bb..f28104f152 100644 --- a/core/services/gcs/src/writer.rs +++ b/core/services/gcs/src/writer.rs @@ -531,7 +531,6 @@ mod tests { use opendal_core::raw::oio::Write as _; use reqsign_core::Context; use reqsign_core::ProvideCredentialChain; - use reqsign_core::Signer; use reqsign_google::RequestSigner; use reqsign_google::TokenCredentialProvider; @@ -836,12 +835,6 @@ mod tests { transport: MockResumableTransport, options: options::WriteOptions, ) -> GcsConditionalWriter { - let sign_ctx = Context::new(); - let signer = Signer::new( - sign_ctx.clone(), - ProvideCredentialChain::new().push(TokenCredentialProvider::new("test-token")), - RequestSigner::new("storage"), - ); let core = Arc::new(GcsCore { info: ServiceInfo::new("gcs", "/", "test-bucket"), capability: Capability { @@ -860,8 +853,12 @@ mod tests { endpoint: "https://storage.googleapis.com".to_string(), bucket: "test-bucket".to_string(), root: "/".to_string(), - signer, - sign_ctx, + sign_ctx: Context::new(), + credential_provider: Arc::new( + ProvideCredentialChain::new().push(TokenCredentialProvider::new("test-token")), + ), + request_signer: Arc::new(RequestSigner::new("storage")), + signers: HttpTransportCache::new(), predefined_acl: None, default_storage_class: None, skip_signature: true, diff --git a/core/services/ghac/src/writer.rs b/core/services/ghac/src/writer.rs index c56e820943..cb37cd10f1 100644 --- a/core/services/ghac/src/writer.rs +++ b/core/services/ghac/src/writer.rs @@ -27,7 +27,6 @@ use opendal_service_azblob::writer::AzblobWriter; use reqsign_azure_storage::RequestSigner; use reqsign_azure_storage::StaticCredentialProvider; use reqsign_core::Context; -use reqsign_core::Signer; pub struct GhacWriter(pub TwoWays<GhacWriterV1, GhacWriterV2>); @@ -76,11 +75,6 @@ impl GhacWriter { ) .with_context("url", &url)); }; - let signer = Signer::new( - Context::new(), - StaticCredentialProvider::new_sas_token(query), - RequestSigner::new(), - ); let azure_core = Arc::new(AzblobCore { info: ServiceInfo::new("azblob", "/", container), capability: Capability { @@ -122,7 +116,10 @@ impl GhacWriter { encryption_key_sha256: None, encryption_algorithm: None, skip_signature: false, - signer, + sign_ctx: Context::new(), + credential_provider: Arc::new(StaticCredentialProvider::new_sas_token(query)), + request_signer: Arc::new(RequestSigner::new()), + signers: HttpTransportCache::new(), }); let w = AzblobWriter::new( azure_core, diff --git a/core/services/obs/src/backend.rs b/core/services/obs/src/backend.rs index 6855d5a287..3b987dd986 100644 --- a/core/services/obs/src/backend.rs +++ b/core/services/obs/src/backend.rs @@ -26,7 +26,6 @@ use opendal_core::raw::*; use opendal_core::*; use reqsign_core::Context; use reqsign_core::OsEnv; -use reqsign_core::Signer; use reqsign_file_read_tokio::TokioFileRead; use reqsign_huaweicloud_obs::DefaultCredentialProvider; use reqsign_huaweicloud_obs::RequestSigner; @@ -220,8 +219,12 @@ impl Builder for ObsBuilder { // // Please refer to this doc for more details: // https://support.huaweicloud.com/intl/en-us/api-obs/obs_04_0010.html - let request_signer = RequestSigner::new(if is_obs_default { &bucket } else { &endpoint }); - let signer = Signer::new(ctx, provider, request_signer); + let credential_provider = Arc::new(provider); + let request_signer = Arc::new(RequestSigner::new(if is_obs_default { + &bucket + } else { + &endpoint + })); let info = ServiceInfo::new(OBS_SCHEME, &root, &bucket); let capability = Capability { @@ -282,7 +285,10 @@ impl Builder for ObsBuilder { root, endpoint: format!("{}://{}", scheme, endpoint), skip_signature: self.config.skip_signature, - signer, + sign_ctx: ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), }), }) } @@ -541,6 +547,7 @@ mod tests { use reqsign_core::Context; use reqsign_core::ProvideCredential; + use reqsign_core::Signer; use reqsign_core::StaticEnv; use super::*; diff --git a/core/services/obs/src/core.rs b/core/services/obs/src/core.rs index c3c90e3402..52f8789355 100644 --- a/core/services/obs/src/core.rs +++ b/core/services/obs/src/core.rs @@ -16,6 +16,7 @@ // under the License. use std::fmt::Debug; +use std::sync::Arc; use bytes::Bytes; use http::Request; @@ -30,8 +31,8 @@ use http::header::IF_NONE_MATCH; use http::header::IF_UNMODIFIED_SINCE; use opendal_core::raw::*; use opendal_core::*; -use reqsign_core::{Context, Signer}; -use reqsign_huaweicloud_obs::Credential; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; +use reqsign_huaweicloud_obs::{Credential, RequestSigner}; use serde::Deserialize; use serde::Serialize; @@ -48,7 +49,10 @@ pub struct ObsCore { pub endpoint: String, pub skip_signature: bool, - pub signer: Signer<Credential>, + pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, } impl Debug for ObsCore { @@ -63,12 +67,13 @@ impl Debug for ObsCore { impl ObsCore { fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - Context::new() - .with_file_read(reqsign_file_read_tokio::TokioFileRead) - .with_http_send(ctx.http_transport().clone()) - .with_env(reqsign_core::OsEnv), - ) + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) } pub async fn sign<T>(&self, ctx: &OperationContext, req: Request<T>) -> Result<Request<T>> { @@ -593,7 +598,6 @@ mod tests { use reqsign_core::Context; use reqsign_core::StaticEnv; use reqsign_huaweicloud_obs::DefaultCredentialProvider; - use reqsign_huaweicloud_obs::RequestSigner; use reqsign_huaweicloud_obs::StaticCredentialProvider; use super::*; @@ -601,7 +605,7 @@ mod tests { /// Build a core whose signer only sees the given provider, so tests never /// depend on the ambient process environment. fn test_core(skip_signature: bool, provider: DefaultCredentialProvider) -> ObsCore { - let ctx = Context::new().with_env(StaticEnv { + let sign_ctx = Context::new().with_env(StaticEnv { home_dir: None, envs: HashMap::new(), }); @@ -613,7 +617,10 @@ mod tests { root: "/".to_string(), endpoint: "https://example-bucket.obs.cn-north-4.myhuaweicloud.com".to_string(), skip_signature, - signer: Signer::new(ctx, provider, RequestSigner::new("example-bucket")), + sign_ctx, + credential_provider: Arc::new(provider), + request_signer: Arc::new(RequestSigner::new("example-bucket")), + signers: HttpTransportCache::new(), } } diff --git a/core/services/oss/src/backend.rs b/core/services/oss/src/backend.rs index 828bef33c2..e86f54a5aa 100644 --- a/core/services/oss/src/backend.rs +++ b/core/services/oss/src/backend.rs @@ -31,7 +31,6 @@ use reqsign_aliyun_oss::StaticCredentialProvider; use reqsign_core::Context; use reqsign_core::OsEnv; use reqsign_core::ProvideCredentialChain; -use reqsign_core::Signer; use reqsign_file_read_tokio::TokioFileRead; use super::OSS_SCHEME; @@ -597,8 +596,8 @@ impl Builder for OssBuilder { }; let sign_ctx = ctx; - let request_signer = RequestSigner::new(bucket); - let signer = Signer::new(sign_ctx.clone(), provider, request_signer); + let credential_provider = Arc::new(provider); + let request_signer = Arc::new(RequestSigner::new(bucket)); let info = ServiceInfo::new(OSS_SCHEME, &root, bucket); let capability = Capability { @@ -673,8 +672,10 @@ impl Builder for OssBuilder { host, presign_endpoint, skip_signature, - signer, sign_ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), server_side_encryption, server_side_encryption_key_id, }), @@ -915,11 +916,11 @@ impl Service for OssBackend { mod tests { use std::collections::{HashMap, VecDeque}; use std::sync::Mutex; - use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use bytes::Bytes; use reqsign_core::time::Timestamp; - use reqsign_core::{FileRead, HttpSend, ProvideCredential, StaticEnv}; + use reqsign_core::{FileRead, HttpSend, ProvideCredential, Signer, StaticEnv}; use super::*; @@ -1103,7 +1104,7 @@ mod tests { }) .with_file_read(TestFileRead::new([("/test/oidc-token", "test-oidc-token")])) .with_http_send(http.clone()); - let signer = backend.core.signer.clone().with_context(ctx); + let signer = backend.core.signer_with_context(ctx); let (mut parts, _) = http::Request::builder() .method(http::Method::PUT) .uri("https://test-bucket.oss-cn-hangzhou.aliyuncs.com/manifest.json") @@ -1200,7 +1201,7 @@ mod tests { }) .with_file_read(TestFileRead::new([("/test/oidc-token", "test-oidc-token")])) .with_http_send(http.clone()); - let signer = backend.core.signer.clone().with_context(ctx); + let signer = backend.core.signer_with_context(ctx); let parts = sign_test_request(&signer).await; @@ -1215,6 +1216,25 @@ mod tests { assert!(http.requests.lock().unwrap().is_empty()); } + #[derive(Clone, Default)] + struct PanicTransport { + dropped: Option<Arc<AtomicBool>>, + } + + impl Drop for PanicTransport { + fn drop(&mut self) { + if let Some(dropped) = &self.dropped { + dropped.store(true, Ordering::SeqCst); + } + } + } + + impl HttpTransport for PanicTransport { + async fn fetch(&self, req: http::Request<Buffer>) -> Result<http::Response<HttpBody>> { + panic!("unexpected HTTP request: {}", req.uri()) + } + } + #[tokio::test] async fn test_custom_credential_provider_refreshes_expiring_credentials() { let calls = Arc::new(AtomicUsize::new(0)); @@ -1244,10 +1264,10 @@ mod tests { let backend = (&backend as &dyn std::any::Any) .downcast_ref::<OssBackend>() .unwrap(); - let http = CredentialHttpSend::default(); - let ctx = Context::new().with_http_send(http.clone()); + let op_ctx = OperationContext::new() + .with_http_transport(HttpTransporter::new(PanicTransport::default())); - let first = sign_test_request(&backend.core.signer.clone().with_context(ctx.clone())).await; + let first = sign_test_request(&backend.core.signer(&op_ctx)).await; assert!( first.headers[http::header::AUTHORIZATION] .to_str() @@ -1257,8 +1277,7 @@ mod tests { assert_eq!(first.headers["x-oss-security-token"], "custom-token-1"); assert_eq!(calls.load(Ordering::SeqCst), 1); - let second = - sign_test_request(&backend.core.signer.clone().with_context(ctx.clone())).await; + let second = sign_test_request(&backend.core.signer(&op_ctx)).await; assert!( second.headers[http::header::AUTHORIZATION] .to_str() @@ -1268,7 +1287,7 @@ mod tests { assert_eq!(second.headers["x-oss-security-token"], "custom-token-2"); assert_eq!(calls.load(Ordering::SeqCst), 2); - let third = sign_test_request(&backend.core.signer.clone().with_context(ctx.clone())).await; + let third = sign_test_request(&backend.core.signer(&op_ctx)).await; assert!( third.headers[http::header::AUTHORIZATION] .to_str() @@ -1276,7 +1295,105 @@ mod tests { .starts_with("OSS custom-ak-2:") ); assert_eq!(calls.load(Ordering::SeqCst), 2); - assert!(http.requests.lock().unwrap().is_empty()); + } + + #[tokio::test] + async fn test_signer_cache_isolates_distinct_transports() { + let calls = Arc::new(AtomicUsize::new(0)); + let provider = TestCredentialProvider::new( + calls.clone(), + vec![ + Credential { + access_key_id: "ak-1".to_string(), + access_key_secret: "sk-1".to_string(), + security_token: Some("token-1".to_string()), + expires_in: Some(Timestamp::now() + std::time::Duration::from_secs(3600)), + }, + Credential { + access_key_id: "ak-2".to_string(), + access_key_secret: "sk-2".to_string(), + security_token: Some("token-2".to_string()), + expires_in: Some(Timestamp::now() + std::time::Duration::from_secs(3600)), + }, + Credential { + access_key_id: "ak-3".to_string(), + access_key_secret: "sk-3".to_string(), + security_token: Some("token-3".to_string()), + expires_in: Some(Timestamp::now() + std::time::Duration::from_secs(3600)), + }, + ], + ); + let core = build_test_core( + OssBuilder::default() + .bucket("test-bucket") + .endpoint("https://oss-cn-hangzhou.aliyuncs.com") + .credential_provider_chain(ProvideCredentialChain::new().push(provider)), + ); + + let ctx1 = OperationContext::new() + .with_http_transport(HttpTransporter::new(PanicTransport::default())); + let first = sign_test_request(&core.signer(&ctx1)).await; + let second = sign_test_request(&core.signer(&ctx1)).await; + assert!( + first.headers[http::header::AUTHORIZATION] + .to_str() + .unwrap() + .starts_with("OSS ak-1:") + ); + assert!( + second.headers[http::header::AUTHORIZATION] + .to_str() + .unwrap() + .starts_with("OSS ak-1:") + ); + assert_eq!(calls.load(Ordering::SeqCst), 1); + + let t2_dropped = Arc::new(AtomicBool::new(false)); + let ctx2 = + OperationContext::new().with_http_transport(HttpTransporter::new(PanicTransport { + dropped: Some(t2_dropped.clone()), + })); + let third = sign_test_request(&core.signer(&ctx2)).await; + let fourth = sign_test_request(&core.signer(&ctx2)).await; + assert!( + third.headers[http::header::AUTHORIZATION] + .to_str() + .unwrap() + .starts_with("OSS ak-2:") + ); + assert!( + fourth.headers[http::header::AUTHORIZATION] + .to_str() + .unwrap() + .starts_with("OSS ak-2:") + ); + assert_eq!(calls.load(Ordering::SeqCst), 2); + + // ctx1 must continue reusing its own cached signer and credential. + let fifth = sign_test_request(&core.signer(&ctx1)).await; + assert!( + fifth.headers[http::header::AUTHORIZATION] + .to_str() + .unwrap() + .starts_with("OSS ak-1:") + ); + assert_eq!(calls.load(Ordering::SeqCst), 2); + + // Dropping ctx2 must release t2 immediately (no strong reference cycle). + drop(ctx2); + assert!(t2_dropped.load(Ordering::SeqCst)); + + // A third transport prunes t2's dead entry and stays isolated. + let ctx3 = OperationContext::new() + .with_http_transport(HttpTransporter::new(PanicTransport::default())); + let sixth = sign_test_request(&core.signer(&ctx3)).await; + assert!( + sixth.headers[http::header::AUTHORIZATION] + .to_str() + .unwrap() + .starts_with("OSS ak-3:") + ); + assert_eq!(calls.load(Ordering::SeqCst), 3); } fn build_test_core(builder: OssBuilder) -> Arc<OssCore> { @@ -1325,7 +1442,7 @@ mod tests { .sts_endpoint("sts.example.com") .role_session_name("builder-session"), ); - let parts = sign_test_request(&core.signer.clone().with_context(ctx)).await; + let parts = sign_test_request(&core.signer_with_context(ctx)).await; // `role_arn` wraps the OIDC credential with an AK-based AssumeRole call. assert!( @@ -1374,7 +1491,7 @@ mod tests { .bucket("test-bucket") .endpoint("https://oss-cn-hangzhou.aliyuncs.com"), ); - let parts = sign_test_request(&core.signer.clone().with_context(ctx)).await; + let parts = sign_test_request(&core.signer_with_context(ctx)).await; assert!( parts.headers[http::header::AUTHORIZATION] .to_str() @@ -1412,7 +1529,7 @@ mod tests { .access_key_secret("static-secret") .security_token("static-token"), ); - let parts = sign_test_request(&core.signer.clone().with_context(ctx)).await; + let parts = sign_test_request(&core.signer_with_context(ctx)).await; assert!( parts.headers[http::header::AUTHORIZATION] .to_str() @@ -1477,9 +1594,7 @@ mod tests { .unwrap() .into_parts(); let err = core - .signer - .clone() - .with_context(ctx) + .signer_with_context(ctx) .sign(&mut parts, None) .await .expect_err("ambient credentials must not be loaded"); diff --git a/core/services/oss/src/core.rs b/core/services/oss/src/core.rs index 27e5030b3d..f8ee7fe9a3 100644 --- a/core/services/oss/src/core.rs +++ b/core/services/oss/src/core.rs @@ -16,6 +16,7 @@ // under the License. use std::fmt::Debug; +use std::sync::Arc; use bytes::Bytes; use constants::X_OSS_META_PREFIX; @@ -34,8 +35,8 @@ use http::header::IF_MODIFIED_SINCE; use http::header::IF_NONE_MATCH; use http::header::IF_UNMODIFIED_SINCE; use http::header::RANGE; -use reqsign_aliyun_oss::Credential; -use reqsign_core::{Context, Signer}; +use reqsign_aliyun_oss::{Credential, RequestSigner}; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; use serde::Deserialize; use serde::Serialize; @@ -76,8 +77,10 @@ pub struct OssCore { pub server_side_encryption: Option<HeaderValue>, pub server_side_encryption_key_id: Option<HeaderValue>, - pub signer: Signer<Credential>, pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, } impl Debug for OssCore { @@ -92,11 +95,22 @@ impl Debug for OssCore { } impl OssCore { - fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - self.sign_ctx - .clone() - .with_http_send(ctx.http_transport().clone()), + pub(crate) fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) + } + + #[cfg(test)] + pub(crate) fn signer_with_context(&self, ctx: Context) -> Signer<Credential> { + Signer::new( + ctx, + self.credential_provider.clone(), + self.request_signer.clone(), ) } diff --git a/core/services/tos/src/backend.rs b/core/services/tos/src/backend.rs index f20f00f61b..d1e8e37fc3 100644 --- a/core/services/tos/src/backend.rs +++ b/core/services/tos/src/backend.rs @@ -34,7 +34,7 @@ use http::StatusCode; use opendal_core::OperationContext; use opendal_core::raw::*; use opendal_core::{Buffer, Builder, Capability, Error, ErrorKind, Result}; -use reqsign_core::{Context, OsEnv, Signer}; +use reqsign_core::{Context, OsEnv}; use reqsign_file_read_tokio::TokioFileRead; use reqsign_volcengine_tos::{DefaultCredentialProvider, RequestSigner, StaticCredentialProvider}; @@ -179,8 +179,8 @@ impl Builder for TosBuilder { provider = provider.push_front(static_provider); } - let request_signer = RequestSigner::new(®ion); - let signer = Signer::new(ctx, provider, request_signer); + let credential_provider = Arc::new(provider); + let request_signer = Arc::new(RequestSigner::new(®ion)); let info = ServiceInfo::new(TOS_SCHEME, &root, &bucket); let capability = Capability { @@ -265,7 +265,10 @@ impl Builder for TosBuilder { root, default_storage_class: None, skip_signature: config.skip_signature, - signer, + sign_ctx: ctx, + credential_provider, + request_signer, + signers: HttpTransportCache::new(), }; Ok(TosBackend { @@ -501,13 +504,10 @@ mod tests { core: &TosCore, envs: HashMap<String, String>, ) -> reqsign_core::Result<http::request::Parts> { - let signer = core - .signer - .clone() - .with_context(Context::new().with_env(StaticEnv { - home_dir: None, - envs, - })); + let signer = core.signer_with_context(Context::new().with_env(StaticEnv { + home_dir: None, + envs, + })); let req = core .tos_put_object_request("test", None, &OpWrite::default(), Buffer::new()) diff --git a/core/services/tos/src/core.rs b/core/services/tos/src/core.rs index 62e3cb79e1..452a68e906 100644 --- a/core/services/tos/src/core.rs +++ b/core/services/tos/src/core.rs @@ -16,6 +16,7 @@ // under the License. use std::fmt::Debug; +use std::sync::Arc; use std::time::Duration; use http::Request; @@ -25,11 +26,10 @@ use http::header::CONTENT_TYPE; use http::header::HOST; use http::header::HeaderValue; use http::header::USER_AGENT; -use reqsign_core::Context; -use reqsign_core::OsEnv; -use reqsign_core::Signer; -use reqsign_volcengine_tos::Credential; -use reqsign_volcengine_tos::{percent_encode_path, percent_encode_query}; +use reqsign_core::{Context, ProvideCredentialDyn, Signer}; +use reqsign_volcengine_tos::{ + Credential, RequestSigner, percent_encode_path, percent_encode_query, +}; use serde::Deserialize; use serde::Serialize; @@ -69,7 +69,10 @@ pub struct TosCore { pub default_storage_class: Option<String>, pub skip_signature: bool, - pub signer: Signer<Credential>, + pub sign_ctx: Context, + pub credential_provider: Arc<dyn ProvideCredentialDyn<Credential = Credential>>, + pub request_signer: Arc<RequestSigner>, + pub signers: HttpTransportCache<Signer<Credential>>, } pub(crate) struct TosUploadPartCopyRequest<'a> { @@ -92,11 +95,21 @@ impl Debug for TosCore { impl TosCore { fn signer(&self, ctx: &OperationContext) -> Signer<Credential> { - self.signer.clone().with_context( - Context::new() - .with_file_read(reqsign_file_read_tokio::TokioFileRead) - .with_http_send(ctx.http_transport().clone()) - .with_env(OsEnv), + self.signers.get_or_init(ctx.http_transport(), |transport| { + Signer::new( + self.sign_ctx.clone().with_http_send(transport), + self.credential_provider.clone(), + self.request_signer.clone(), + ) + }) + } + + #[cfg(test)] + pub(crate) fn signer_with_context(&self, ctx: Context) -> Signer<Credential> { + Signer::new( + ctx, + self.credential_provider.clone(), + self.request_signer.clone(), ) }
