aliyun-oss 0.2.0

aliyun oss sdk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Pre-signed URL generation (V1 and V4).

use std::time::Duration;

use hmac::{Hmac, KeyInit, Mac};
use sha1::Sha1;
use sha2::{Digest, Sha256};

use crate::config::credentials::Credentials;
use crate::error::{ErrorContext, OssError, OssErrorKind, Result};
use crate::util::uri::uri_encode;

type HmacSha256 = Hmac<Sha256>;
type HmacSha1 = Hmac<Sha1>;

const ALGORITHM: &str = "OSS4-HMAC-SHA256";
const MAX_EXPIRES: Duration = Duration::from_secs(7 * 24 * 3600);

fn derive_signing_key(secret: &str, date: &str, region: &str) -> Vec<u8> {
    let prefix = format!("aliyun_v4{}", secret);
    let mut mac =
        HmacSha256::new_from_slice(prefix.as_bytes()).expect("HMAC can take key of any size");
    mac.update(date.as_bytes());
    let date_key = mac.finalize().into_bytes();

    let mut mac = HmacSha256::new_from_slice(&date_key).expect("HMAC can take key of any size");
    mac.update(region.as_bytes());
    let date_region_key = mac.finalize().into_bytes();

    let mut mac =
        HmacSha256::new_from_slice(&date_region_key).expect("HMAC can take key of any size");
    mac.update(b"oss");
    let date_region_service_key = mac.finalize().into_bytes();

    let mut mac = HmacSha256::new_from_slice(&date_region_service_key)
        .expect("HMAC can take key of any size");
    mac.update(b"aliyun_v4_request");
    mac.finalize().into_bytes().to_vec()
}

fn compute_signature(signing_key: &[u8], string_to_sign: &str) -> String {
    let mut mac = HmacSha256::new_from_slice(signing_key).expect("HMAC can take key of any size");
    mac.update(string_to_sign.as_bytes());
    hex::encode(mac.finalize().into_bytes())
}

fn compute_v1_signature(secret: &str, string_to_sign: &str) -> String {
    let mut mac =
        HmacSha1::new_from_slice(secret.as_bytes()).expect("HMAC can take key of any size");
    mac.update(string_to_sign.as_bytes());
    base64::Engine::encode(
        &base64::engine::general_purpose::STANDARD,
        mac.finalize().into_bytes().as_slice(),
    )
}

fn query_encode(s: &str) -> String {
    let mut result = String::new();
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                result.push(b as char)
            }
            _ => result.push_str(&format!("%{:02X}", b)),
        }
    }
    result
}

fn url_escape(s: &str) -> String {
    s.replace(':', "%3A")
}

fn url_escape_path(s: &str) -> String {
    let mut result = String::new();
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => {
                result.push(b as char)
            }
            _ => result.push_str(&format!("%{:02X}", b)),
        }
    }
    result
}

/// Builder for generating pre-signed URLs for OSS access.
pub struct PreSignedUrlBuilder {
    credentials: Credentials,
    region: String,
    endpoint: String,
    bucket: String,
    key: String,
    method: String,
    expires: Duration,
    content_type: Option<String>,
    content_md5: Option<String>,
    additional_query: Vec<(String, String)>,
}

impl PreSignedUrlBuilder {
    pub(crate) fn new(
        credentials: Credentials,
        region: String,
        endpoint: String,
        bucket: String,
        key: String,
    ) -> Self {
        Self {
            credentials,
            region,
            endpoint,
            bucket,
            key,
            method: "GET".into(),
            expires: Duration::from_secs(3600),
            content_type: None,
            content_md5: None,
            additional_query: Vec::new(),
        }
    }

    /// Sets the HTTP method for the pre-signed URL.
    pub fn method(mut self, m: impl Into<String>) -> Self {
        self.method = m.into();
        self
    }

    /// Sets the expiration duration.
    pub fn expires(mut self, d: Duration) -> Self {
        self.expires = d;
        self
    }

    /// Sets the Content-Type header for the pre-signed request.
    pub fn content_type(mut self, ct: impl Into<String>) -> Self {
        self.content_type = Some(ct.into());
        self
    }

    /// Sets the Content-MD5 header for the pre-signed request.
    pub fn content_md5(mut self, md5: impl Into<String>) -> Self {
        self.content_md5 = Some(md5.into());
        self
    }

    /// Adds an additional query parameter.
    pub fn query_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.additional_query.push((key.into(), value.into()));
        self
    }

    /// Generates a pre-signed URL using the V4 signing algorithm.
    pub fn generate(self) -> Result<String> {
        self.generate_v4()
    }

    /// Generates a pre-signed URL using the V1 signing algorithm (legacy).
    pub fn generate_v1(self) -> Result<String> {
        if self.expires > MAX_EXPIRES {
            return Err(OssError {
                kind: OssErrorKind::ValidationError,
                context: Box::new(ErrorContext {
                    operation: Some("PreSignedUrl: expires must not exceed 7 days".into()),
                    ..Default::default()
                }),
                source: None,
            });
        }

        let now = chrono::Utc::now();
        let expires_unix = now.timestamp() + self.expires.as_secs() as i64;

        let bucket_str = url_escape(&self.bucket);
        let key_str = url_escape_path(&self.key);
        let resource = format!("/{}/{}", bucket_str, key_str);

        let ct = self.content_type.as_deref().unwrap_or("");
        let md5 = self.content_md5.as_deref().unwrap_or("");

        let string_to_sign = format!(
            "{}\n{}\n{}\n{}\n{}",
            self.method, md5, ct, expires_unix, resource
        );

        let signature = compute_v1_signature(self.credentials.access_key_secret(), &string_to_sign);

        let mut params: Vec<(String, String)> = vec![
            (
                "OSSAccessKeyId".into(),
                self.credentials.access_key_id().to_string(),
            ),
            ("Expires".into(), expires_unix.to_string()),
            ("Signature".into(), signature),
        ];

        if let Some(ref token) = self.credentials.security_token() {
            params.push(("security-token".into(), token.to_string()));
        }

        for (k, v) in &self.additional_query {
            params.push((k.clone(), v.clone()));
        }

        let qs: Vec<String> = params
            .iter()
            .map(|(k, v)| format!("{}={}", k, query_encode(v)))
            .collect();

        Ok(format!(
            "https://{}.{}/{}?{}",
            bucket_str,
            self.endpoint,
            key_str,
            qs.join("&")
        ))
    }

    /// Generates a pre-signed URL using the V4 signing algorithm.
    pub fn generate_v4(self) -> Result<String> {
        if self.expires > MAX_EXPIRES {
            return Err(OssError {
                kind: OssErrorKind::ValidationError,
                context: Box::new(ErrorContext {
                    operation: Some("PreSignedUrl: expires must not exceed 7 days".into()),
                    ..Default::default()
                }),
                source: None,
            });
        }

        let now = chrono::Utc::now();
        let timestamp = now.format("%Y%m%dT%H%M%SZ").to_string();
        let datestamp = &timestamp[..8];
        let expires_unix = now.timestamp() + self.expires.as_secs() as i64;

        let scope = format!("{}/{}/oss/aliyun_v4_request", datestamp, self.region);

        let bucket_str = url_escape(&self.bucket);
        let key_str = url_escape_path(&self.key);

        let mut query_pairs: Vec<(String, String)> = Vec::new();
        query_pairs.push((
            "x-oss-credential".into(),
            format!("{}/{}", self.credentials.access_key_id(), scope),
        ));
        query_pairs.push(("x-oss-date".into(), timestamp.clone()));
        query_pairs.push(("x-oss-expires".into(), expires_unix.to_string()));

        if let Some(ref sts_token) = self.credentials.security_token() {
            query_pairs.push(("x-oss-security-token".into(), sts_token.to_string()));
        }

        for (k, v) in &self.additional_query {
            query_pairs.push((k.clone(), v.clone()));
        }

        let canonical_uri = uri_encode(&format!("/{}/{}", bucket_str, key_str));

        let canonical_query = {
            let mut sorted: Vec<(&str, &str)> = query_pairs
                .iter()
                .map(|(k, v)| (k.as_str(), v.as_str()))
                .collect();
            sorted.sort_by_key(|(k, _)| *k);
            let parts: Vec<String> = sorted
                .iter()
                .map(|(k, v)| {
                    if v.is_empty() {
                        query_encode(k)
                    } else {
                        format!("{}={}", query_encode(k), query_encode(v))
                    }
                })
                .collect();
            parts.join("&")
        };

        let canonical_headers = {
            let mut headers: Vec<(&str, &str)> = vec![("host", &bucket_str)];
            if let Some(ref ct) = self.content_type {
                headers.push(("content-type", ct));
            }
            headers.sort_by_key(|(n, _)| *n);
            headers
                .iter()
                .map(|(n, v)| format!("{}:{}", n, v.trim()))
                .collect::<Vec<_>>()
                .join("\n")
        };

        let signed_headers = {
            let mut names: Vec<&str> = if self.content_type.is_some() {
                vec!["host", "content-type"]
            } else {
                vec!["host"]
            };
            names.sort();
            names.join(";")
        };

        let canonical_request = format!(
            "{}\n{}\n{}\n{}\n\n{}\nUNSIGNED-PAYLOAD",
            self.method, canonical_uri, canonical_query, canonical_headers, signed_headers
        );

        let hashed_canonical = hex::encode(Sha256::digest(canonical_request.as_bytes()));
        let string_to_sign = format!(
            "{}\n{}\n{}\n{}",
            ALGORITHM, timestamp, scope, hashed_canonical
        );

        let signing_key = derive_signing_key(
            self.credentials.access_key_secret(),
            datestamp,
            &self.region,
        );
        let signature = compute_signature(&signing_key, &string_to_sign);

        query_pairs.push(("x-oss-signature".into(), signature));

        if signed_headers != "host" {
            query_pairs.push(("x-oss-additional-headers".into(), signed_headers));
        }

        let query_string = {
            let mut sorted: Vec<(&str, &str)> = query_pairs
                .iter()
                .map(|(k, v)| (k.as_str(), v.as_str()))
                .collect();
            sorted.sort_by_key(|(k, _)| *k);
            sorted
                .iter()
                .map(|(k, v)| {
                    if v.is_empty() {
                        k.to_string()
                    } else {
                        format!("{}={}", k, query_encode(v))
                    }
                })
                .collect::<Vec<_>>()
                .join("&")
        };

        Ok(format!(
            "https://{}.{}/{}?{}",
            bucket_str, self.endpoint, key_str, query_string
        ))
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use crate::config::credentials::Credentials;
    use crate::types::region::Region;

    use super::*;

    fn test_credentials() -> Credentials {
        Credentials::builder()
            .access_key_id("test-ak")
            .access_key_secret("test-sk")
            .build()
            .unwrap()
    }

    fn test_builder() -> PreSignedUrlBuilder {
        PreSignedUrlBuilder::new(
            test_credentials(),
            "cn-hangzhou".into(),
            "oss-cn-hangzhou.aliyuncs.com".into(),
            "test-bucket".into(),
            "test-key.txt".into(),
        )
    }

    #[test]
    fn presigned_v1_url_contains_required_params() {
        let url = test_builder()
            .method("GET")
            .expires(Duration::from_secs(3600))
            .generate_v1()
            .unwrap();

        assert!(url.starts_with("https://test-bucket.oss-cn-hangzhou.aliyuncs.com/test-key.txt?"));
        assert!(url.contains("OSSAccessKeyId=test-ak"));
        assert!(url.contains("Expires="));
        assert!(url.contains("Signature="));
    }

    #[test]
    fn presigned_v4_url_contains_required_params() {
        let url = test_builder()
            .method("GET")
            .expires(Duration::from_secs(3600))
            .generate_v4()
            .unwrap();

        assert!(url.starts_with("https://test-bucket.oss-cn-hangzhou.aliyuncs.com/test-key.txt?"));
        assert!(url.contains("x-oss-signature="));
        assert!(url.contains("x-oss-credential=test-ak%2F"));
        assert!(url.contains("x-oss-date="));
        assert!(url.contains("x-oss-expires="));
    }

    #[test]
    fn presigned_url_with_additional_query_params() {
        let url = test_builder()
            .method("GET")
            .query_param("response-content-type", "image/jpeg")
            .expires(Duration::from_secs(3600))
            .generate_v1()
            .unwrap();

        assert!(url.contains("response-content-type=image%2Fjpeg"));
    }

    #[test]
    fn presigned_url_with_security_token() {
        let creds = Credentials::builder()
            .access_key_id("ak")
            .access_key_secret("sk")
            .security_token("test-token")
            .build()
            .unwrap();

        let url = PreSignedUrlBuilder::new(
            creds,
            "cn-hangzhou".into(),
            "oss-cn-hangzhou.aliyuncs.com".into(),
            "bucket".into(),
            "key".into(),
        )
        .expires(Duration::from_secs(3600))
        .generate_v1()
        .unwrap();

        assert!(url.contains("security-token=test-token"));
    }

    #[test]
    fn presigned_url_expires_too_long_returns_error() {
        let result = test_builder()
            .expires(Duration::from_secs(8 * 24 * 3600))
            .generate_v1();
        assert!(result.is_err());
    }

    #[tokio::test]
    #[ignore = "requires valid OSS credentials"]
    async fn e2e_presigned_get_url_v1() {
        let ak = std::env::var("OSS_ACCESS_KEY_ID").expect("OSS_ACCESS_KEY_ID not set");
        let sk = std::env::var("OSS_ACCESS_KEY_SECRET").expect("OSS_ACCESS_KEY_SECRET not set");
        let region_str = std::env::var("OSS_REGION").unwrap_or_else(|_| "cn-wulanchabu".into());
        let bucket_str = std::env::var("OSS_BUCKET").expect("OSS_BUCKET not set");

        let region = Region::from_str(&region_str).unwrap_or_else(|_| Region::Custom {
            endpoint: format!("oss-{}.aliyuncs.com", region_str),
            region_id: region_str.clone(),
        });

        let credentials = Credentials::builder()
            .access_key_id(ak)
            .access_key_secret(sk)
            .build()
            .unwrap();

        let url = PreSignedUrlBuilder::new(
            credentials,
            region.region_id().to_string(),
            region.external_endpoint().to_string(),
            bucket_str.clone(),
            "test.txt".into(),
        )
        .method("GET")
        .expires(Duration::from_secs(3600))
        .generate_v1()
        .unwrap();

        let resp = reqwest::get(&url).await.unwrap();
        eprintln!("V1 URL: {}", url);
        eprintln!("Status: {}", resp.status());
        assert!(resp.status().is_success());
    }

    #[tokio::test]
    #[ignore = "requires valid OSS credentials"]
    async fn e2e_presigned_get_url_v4() {
        let ak = std::env::var("OSS_ACCESS_KEY_ID").expect("OSS_ACCESS_KEY_ID not set");
        let sk = std::env::var("OSS_ACCESS_KEY_SECRET").expect("OSS_ACCESS_KEY_SECRET not set");
        let region_str = std::env::var("OSS_REGION").unwrap_or_else(|_| "cn-wulanchabu".into());
        let bucket_str = std::env::var("OSS_BUCKET").expect("OSS_BUCKET not set");

        let region = Region::from_str(&region_str).unwrap_or_else(|_| Region::Custom {
            endpoint: format!("oss-{}.aliyuncs.com", region_str),
            region_id: region_str.clone(),
        });

        let credentials = Credentials::builder()
            .access_key_id(ak)
            .access_key_secret(sk)
            .build()
            .unwrap();

        let url = PreSignedUrlBuilder::new(
            credentials,
            region.region_id().to_string(),
            region.external_endpoint().to_string(),
            bucket_str.clone(),
            "test.txt".into(),
        )
        .method("GET")
        .expires(Duration::from_secs(3600))
        .generate_v4()
        .unwrap();

        eprintln!("V4 URL: {}", url);
        let resp = reqwest::get(&url).await.unwrap();
        let status = resp.status();
        let body = resp.text().await.unwrap_or_default();
        eprintln!("Status: {}", status);
        if !status.is_success() {
            eprintln!("Body: {}", body);
        }
        assert!(status.is_success(), "V4 presigned failed: {}", body);
    }
}