You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2026/03/18 17:04:23 UTC

(opendal-reqsign) 01/01: refactor(aliyun-oss): prepare signer for region-aware signing

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch xuanwo/oss-pr1-signer-api
in repository https://gitbox.apache.org/repos/asf/opendal-reqsign.git

commit 3c50dcf7ee674653c5d39ae12995900b575ba67a
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Thu Mar 19 01:04:11 2026 +0800

    refactor(aliyun-oss): prepare signer for region-aware signing
---
 services/aliyun-oss/README.md           | 21 ++++++++++
 services/aliyun-oss/src/lib.rs          |  5 +++
 services/aliyun-oss/src/sign_request.rs | 70 +++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)

diff --git a/services/aliyun-oss/README.md b/services/aliyun-oss/README.md
index b96df82..1b05c41 100644
--- a/services/aliyun-oss/README.md
+++ b/services/aliyun-oss/README.md
@@ -35,6 +35,12 @@ async fn main() -> Result<()> {
     // );
 
     let signer = Signer::new(ctx, loader, RequestSigner::new("bucket"));
+    // For future signing versions, region can be configured in advance:
+    // let signer = Signer::new(
+    //     ctx,
+    //     loader,
+    //     RequestSigner::new("bucket").with_region("oss-cn-beijing"),
+    // );
 
     let mut req = http::Request::get("https://bucket.oss-cn-beijing.aliyuncs.com/object.txt")
         .body(())
@@ -54,6 +60,21 @@ async fn main() -> Result<()> {
 - **STS Support**: Temporary credentials via Security Token Service
 - **All OSS Operations**: Object, bucket, and multipart operations
 
+## Signer Configuration
+
+`RequestSigner::new("bucket")` keeps the current V1 behavior.
+
+If you want to wire configuration that future signing versions will need, you
+can set the region ahead of time:
+
+```rust
+use reqsign_aliyun_oss::RequestSigner;
+
+let signer = RequestSigner::new("bucket").with_region("oss-cn-beijing");
+```
+
+The region is currently a no-op for V1 signing.
+
 ## Credential Sources
 
 ### Environment Variables
diff --git a/services/aliyun-oss/src/lib.rs b/services/aliyun-oss/src/lib.rs
index 1cf179a..93485fc 100644
--- a/services/aliyun-oss/src/lib.rs
+++ b/services/aliyun-oss/src/lib.rs
@@ -26,6 +26,9 @@
 //! the complete signing process along with credential loading from various sources
 //! including environment variables, configuration files, and STS tokens.
 //!
+//! `RequestSigner` defaults to V1 signing and also accepts an optional region
+//! for future signing versions that require it.
+//!
 //! ## Quick Start
 //!
 //! ```no_run
@@ -58,6 +61,8 @@
 //!
 //!     // Create request builder
 //!     let builder = RequestSigner::new("bucket");
+//!     // For future signing versions, region can be wired now:
+//!     // let builder = RequestSigner::new("bucket").with_region("oss-cn-beijing");
 //!
 //!     // Create the signer
 //!     let signer = Signer::new(ctx, loader, builder);
diff --git a/services/aliyun-oss/src/sign_request.rs b/services/aliyun-oss/src/sign_request.rs
index 35192dd..f33596e 100644
--- a/services/aliyun-oss/src/sign_request.rs
+++ b/services/aliyun-oss/src/sign_request.rs
@@ -34,6 +34,7 @@ const CONTENT_MD5: &str = "content-md5";
 #[derive(Debug)]
 pub struct RequestSigner {
     bucket: String,
+    region: Option<String>,
     time: Option<Timestamp>,
 }
 
@@ -42,10 +43,22 @@ impl RequestSigner {
     pub fn new(bucket: &str) -> Self {
         Self {
             bucket: bucket.to_string(),
+            region: None,
             time: None,
         }
     }
 
+    /// Set the OSS region.
+    ///
+    /// The current V1 signing flow does not use the region, but future signing
+    /// versions such as V4 require it. Keeping this setting on the signer lets
+    /// callers wire stable configuration now without changing the construction
+    /// flow later.
+    pub fn with_region(mut self, region: impl Into<String>) -> Self {
+        self.region = Some(region.into());
+        self
+    }
+
     /// Specify the signing time.
     ///
     /// # Note
@@ -438,3 +451,60 @@ static SUBRESOURCES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
         "metaQuery",
     ])
 });
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::Credential;
+
+    #[test]
+    fn test_request_signer_accepts_region_configuration() {
+        let signer = RequestSigner::new("bucket").with_region("oss-cn-beijing");
+
+        assert_eq!(signer.bucket, "bucket");
+        assert_eq!(signer.region.as_deref(), Some("oss-cn-beijing"));
+    }
+
+    #[test]
+    fn test_region_configuration_is_noop_for_v1_signature() {
+        let credential = Credential {
+            access_key_id: "access_key_id".to_string(),
+            access_key_secret: "access_key_secret".to_string(),
+            security_token: None,
+            expires_in: None,
+        };
+        let time = Timestamp::from_second(1_700_000_000).expect("timestamp must build");
+
+        let mut without_region =
+            http::Request::get("https://bucket.oss-cn-beijing.aliyuncs.com/object.txt")
+                .body(())
+                .expect("request must build")
+                .into_parts()
+                .0;
+        RequestSigner::new("bucket")
+            .with_time(time)
+            .sign_header(&mut without_region, &credential, time)
+            .expect("signing without region must succeed");
+
+        let mut with_region =
+            http::Request::get("https://bucket.oss-cn-beijing.aliyuncs.com/object.txt")
+                .body(())
+                .expect("request must build")
+                .into_parts()
+                .0;
+        RequestSigner::new("bucket")
+            .with_region("oss-cn-beijing")
+            .with_time(time)
+            .sign_header(&mut with_region, &credential, time)
+            .expect("signing with region must succeed");
+
+        assert_eq!(
+            without_region.headers.get(AUTHORIZATION),
+            with_region.headers.get(AUTHORIZATION)
+        );
+        assert_eq!(
+            without_region.headers.get(DATE),
+            with_region.headers.get(DATE)
+        );
+    }
+}