This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/publish-cloudflare-kv-signer in repository https://gitbox.apache.org/repos/asf/opendal.git
commit efb4a38a043176a0b0452b7d49722cbb756c12b3 Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 30 07:55:07 2026 +0100 refactor(services/cloudflare-kv): sign requests with Reqsign Pass the configured raw API token to the static Cloudflare provider. Sign the assembled request head before sending it through the operation context. Preserve the request-build error for malformed credentials. The published provider rejects whitespace and non-ASCII bytes before transport. --- core/Cargo.lock | 13 +++++++ core/services/cloudflare-kv/Cargo.toml | 2 + core/services/cloudflare-kv/src/backend.rs | 23 ++++++----- core/services/cloudflare-kv/src/core.rs | 61 +++++++++++++++--------------- core/services/cloudflare-kv/src/deleter.rs | 2 +- core/services/cloudflare-kv/src/lister.rs | 2 +- core/services/cloudflare-kv/src/reader.rs | 2 +- 7 files changed, 62 insertions(+), 43 deletions(-) diff --git a/core/Cargo.lock b/core/Cargo.lock index 282b9bde86..5f5ccf8714 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -7768,6 +7768,8 @@ dependencies = [ "futures", "http 1.5.0", "opendal-core", + "reqsign-cloudflare", + "reqsign-core", "serde", "serde_json", "tokio", @@ -10132,6 +10134,17 @@ dependencies = [ "reqsign-core", ] +[[package]] +name = "reqsign-cloudflare" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d79a1ed29fe66b9d13ae33170eb5bf473912ffdf361964a2d3627853eab794ee" +dependencies = [ + "http 1.5.0", + "log", + "reqsign-core", +] + [[package]] name = "reqsign-command-execute-tokio" version = "4.0.0" diff --git a/core/services/cloudflare-kv/Cargo.toml b/core/services/cloudflare-kv/Cargo.toml index c75797657a..a98ec777ec 100644 --- a/core/services/cloudflare-kv/Cargo.toml +++ b/core/services/cloudflare-kv/Cargo.toml @@ -35,6 +35,8 @@ all-features = true bytes = { workspace = true } http = { workspace = true } opendal-core = { path = "../../core", version = "0.59.5", default-features = false } +reqsign-cloudflare = "1.0.0" +reqsign-core = { version = "4.0.0", default-features = false } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } diff --git a/core/services/cloudflare-kv/src/backend.rs b/core/services/cloudflare-kv/src/backend.rs index ec078aae77..d5315bfeef 100644 --- a/core/services/cloudflare-kv/src/backend.rs +++ b/core/services/cloudflare-kv/src/backend.rs @@ -105,15 +105,18 @@ impl Builder for CloudflareKvBuilder { .map(signed_duration_to_duration) .transpose()?, }; - let api_token = match &self.config.api_token { - Some(api_token) => format_authorization_by_bearer(api_token)?, - None => { - return Err(Error::new( - ErrorKind::ConfigInvalid, - "api_token is required", - )); - } + let Some(api_token) = self.config.api_token else { + return Err(Error::new( + ErrorKind::ConfigInvalid, + "api_token is required", + )); }; + if api_token.is_empty() { + return Err(Error::new( + ErrorKind::Unexpected, + "can't build authorization header with empty token", + )); + } let Some(account_id) = self.config.account_id.clone() else { return Err(Error::new( @@ -149,7 +152,7 @@ impl Builder for CloudflareKvBuilder { Ok(CloudflareKvBackend { core: Arc::new(CloudflareKvCore { - api_token, + signer: CloudflareKvCore::new_signer(api_token), account_id, namespace_id, expiration_ttl: default_ttl, @@ -550,7 +553,7 @@ mod tests { fn test_backend() -> CloudflareKvBackend { CloudflareKvBackend { core: Arc::new(CloudflareKvCore { - api_token: "Bearer test-token".to_string(), + signer: CloudflareKvCore::new_signer("test-token"), account_id: "acc123".to_string(), namespace_id: "ns456".to_string(), expiration_ttl: None, diff --git a/core/services/cloudflare-kv/src/core.rs b/core/services/cloudflare-kv/src/core.rs index f7cca66c77..928f7ac534 100644 --- a/core/services/cloudflare-kv/src/core.rs +++ b/core/services/cloudflare-kv/src/core.rs @@ -20,17 +20,18 @@ use std::fmt::Debug; use http::Request; use http::Response; use http::header; -use http::request; use opendal_core::raw::*; use opendal_core::*; use opendal_core::{Buffer, Result}; +use reqsign_cloudflare::{Credential, RequestSigner, StaticCredentialProvider}; +use reqsign_core::{Context, Signer}; use serde_json::json; use crate::model::CfKvMetadata; #[derive(Clone)] pub struct CloudflareKvCore { - pub api_token: String, + pub signer: Signer<Credential>, pub account_id: String, pub namespace_id: String, pub expiration_ttl: Option<Duration>, @@ -51,13 +52,28 @@ impl Debug for CloudflareKvCore { } impl CloudflareKvCore { - #[inline] - async fn send(&self, ctx: &OperationContext, req: Request<Buffer>) -> Result<Response<Buffer>> { - ctx.http_transport().send(req).await + pub(super) fn new_signer(api_token: impl Into<String>) -> Signer<Credential> { + Signer::new( + Context::new(), + StaticCredentialProvider::new(api_token), + RequestSigner, + ) } - fn sign(&self, req: request::Builder) -> request::Builder { - req.header(header::AUTHORIZATION, &self.api_token) + async fn send(&self, ctx: &OperationContext, req: Request<Buffer>) -> Result<Response<Buffer>> { + let (mut parts, body) = req.into_parts(); + self.signer.sign(&mut parts, None).await.map_err(|err| { + if err.kind() == reqsign_core::ErrorKind::ConfigInvalid { + // Token validation used to fail while building the Authorization header. + return Error::new(ErrorKind::Unexpected, "building http request") + .with_operation("http::Request::build") + .set_source(err); + } + new_request_sign_error(err.into()) + })?; + ctx.http_transport() + .send(Request::from_parts(parts, body)) + .await } fn url_prefix(&self) -> String { @@ -77,10 +93,7 @@ impl CloudflareKvCore { percent_encode_path(path) ); - let req = Request::get(url); - let req = self.sign(req); - - let req = req + let req = Request::get(url) .extension(Operation::Stat) .extension(ServiceOperation("GetMetadata")) .body(Buffer::new()) @@ -91,11 +104,7 @@ impl CloudflareKvCore { pub async fn get(&self, ctx: &OperationContext, path: &str) -> Result<Response<Buffer>> { let url = format!("{}/values/{}", self.url_prefix(), percent_encode_path(path)); - let req = Request::get(url); - - let req = self.sign(req); - - let req = req + let req = Request::get(url) .extension(Operation::Read) .extension(ServiceOperation("GetValue")) .body(Buffer::new()) @@ -113,9 +122,7 @@ impl CloudflareKvCore { ) -> Result<Response<Buffer>> { let url = format!("{}/values/{}", self.url_prefix(), percent_encode_path(path)); - let req = Request::put(url); - let req = self.sign(req); - let req = req + let req = Request::put(url) .extension(Operation::Write) .extension(ServiceOperation("WriteValue")); @@ -144,11 +151,8 @@ impl CloudflareKvCore { ) -> Result<Response<Buffer>> { let url = format!("{}/bulk/delete", self.url_prefix()); - let req = Request::post(&url); - - let req = self.sign(req); let req_body = &json!(paths); - let req = req + let req = Request::post(&url) .extension(Operation::Delete) .extension(ServiceOperation("BulkDelete")) .header(header::CONTENT_TYPE, "application/json") @@ -175,10 +179,7 @@ impl CloudflareKvCore { url = url.push("limit", &limit.unwrap_or(1000).to_string()); url = url.push("prefix", &percent_encode_path(prefix)); - let req = Request::get(url.finish()); - - let req = self.sign(req); - let req = req + let req = Request::get(url.finish()) .extension(Operation::List) .extension(ServiceOperation("ListKeys")) .body(Buffer::new()) @@ -288,7 +289,7 @@ mod tests { fn test_core(api_token: &str) -> CloudflareKvCore { CloudflareKvCore { - api_token: api_token.to_string(), + signer: CloudflareKvCore::new_signer(api_token), account_id: "acc123".to_string(), namespace_id: "ns456".to_string(), expiration_ttl: None, @@ -300,7 +301,7 @@ mod tests { #[test] fn core_debug_redacts_api_token() { let sentinel = "cfkv-sentinel-secret-token"; - let core = test_core(&format!("Bearer {sentinel}")); + let core = test_core(sentinel); let debug = format!("{core:?}"); assert!(!debug.contains(sentinel)); assert!(debug.contains("acc123")); @@ -316,7 +317,7 @@ mod tests { uri: captured_uri.clone(), }, )); - let core = test_core("Bearer token"); + let core = test_core("test-token"); let resp = core .list(&ctx, "dir", Some(10), Some("a+b=c&d=e".to_string())) diff --git a/core/services/cloudflare-kv/src/deleter.rs b/core/services/cloudflare-kv/src/deleter.rs index 40d8ef3503..1201bdc269 100644 --- a/core/services/cloudflare-kv/src/deleter.rs +++ b/core/services/cloudflare-kv/src/deleter.rs @@ -176,7 +176,7 @@ mod tests { fn test_core(root: &str) -> Arc<CloudflareKvCore> { Arc::new(CloudflareKvCore { - api_token: "Bearer test-token".to_string(), + signer: CloudflareKvCore::new_signer("test-token"), account_id: "acc123".to_string(), namespace_id: "ns456".to_string(), expiration_ttl: None, diff --git a/core/services/cloudflare-kv/src/lister.rs b/core/services/cloudflare-kv/src/lister.rs index 9cfd17d917..af2fbbfa36 100644 --- a/core/services/cloudflare-kv/src/lister.rs +++ b/core/services/cloudflare-kv/src/lister.rs @@ -255,7 +255,7 @@ mod tests { fn test_core(root: &str) -> Arc<CloudflareKvCore> { Arc::new(CloudflareKvCore { - api_token: "Bearer test-token".to_string(), + signer: CloudflareKvCore::new_signer("test-token"), account_id: "acc123".to_string(), namespace_id: "ns456".to_string(), expiration_ttl: None, diff --git a/core/services/cloudflare-kv/src/reader.rs b/core/services/cloudflare-kv/src/reader.rs index 3189ba596b..78c9585426 100644 --- a/core/services/cloudflare-kv/src/reader.rs +++ b/core/services/cloudflare-kv/src/reader.rs @@ -202,7 +202,7 @@ mod tests { fn test_backend() -> CloudflareKvBackend { CloudflareKvBackend { core: Arc::new(CloudflareKvCore { - api_token: "Bearer test-token".to_string(), + signer: CloudflareKvCore::new_signer("test-token"), account_id: "acc123".to_string(), namespace_id: "ns456".to_string(), expiration_ttl: None,
