bws-rs 0.1.1

rust s3 backend service framework, quick build yourself s3 gateway, or object storage
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
use std::io::Write;
extern crate hmac;
extern crate sha1;
use self::hmac::{Hmac, Mac};
use self::sha1::Digest;
pub trait VHeader {
    fn get_header(&self, key: &str) -> Option<String>;
    fn set_header(&mut self, key: &str, val: &str);
    fn delete_header(&mut self, key: &str);
    fn rng_header(&self, cb: impl FnMut(&str, &str) -> bool);
}
use crate::{error::Error, GenericResult};
#[derive(Debug)]
pub struct BaseArgs {
    pub region: String,
    pub service: String,
    pub access_key: String,
    pub content_hash: String,
    pub signed_headers: Vec<String>,
    pub signature: String,
    pub date: String,
}
pub fn extract_args<R: VHeader>(r: &R) -> Result<BaseArgs, ()> {
    let authorization = r.get_header("authorization").ok_or(())?;
    let authorization = authorization.trim();
    let heads = authorization.splitn(2, ' ').collect::<Vec<&str>>();
    if heads.len() != 2 {
        return Err(());
    }
    match heads[0] {
        "AWS4-HMAC-SHA256" => {
            let heads = heads[1].split(',').collect::<Vec<&str>>();
            if heads.len() != 3 {
                return Err(());
            }
            let mut credential = None;
            let mut singed_headers = None;
            let mut signature = None;
            for head in heads {
                let heads = head.trim().splitn(2, '=').collect::<Vec<&str>>();
                if heads.len() != 2 {
                    return Err(());
                }
                match heads[0] {
                    "Credential" => {
                        let heads = heads[1].split('/').collect::<Vec<&str>>();
                        if heads.len() != 5 {
                            return Err(());
                        }
                        credential = Some((heads[0], heads[1], heads[2], heads[3], heads[4]));
                    }
                    "SignedHeaders" => {
                        singed_headers = Some(heads[1].split(';').collect::<Vec<&str>>());
                    }
                    "Signature" => {
                        signature = Some(heads[1]);
                    }
                    _ => {
                        return Err(());
                    }
                }
            }
            if signature.is_none() || singed_headers.is_none() || credential.is_none() {
                return Err(());
            }
            let signature = signature.unwrap();
            let singed_headers = singed_headers.unwrap();
            let credential = credential.unwrap();
            Ok(BaseArgs {
                content_hash: r.get_header("x-amz-content-sha256").ok_or(())?,
                region: credential.2.to_string(),
                service: credential.3.to_string(),
                access_key: credential.0.to_string(),
                signed_headers: singed_headers.into_iter().map(|v| v.to_string()).collect(),
                signature: signature.to_string(),
                date: credential.1.to_string(),
            })
        }
        _ => Err(()),
    }
}
pub fn get_v4_signature<'a, T: VHeader, S: ToString>(
    req: &T,
    method: &str,
    region: &str,
    service: &str,
    url_path: &str,
    secretkey: &str,
    content_hash: &str,
    signed_headers: &[S],
    mut query: Vec<crate::utils::BaseKv<String, String>>,
) -> GenericResult<(String, HmacSha256CircleHasher)> {
    let xamz_date = req.get_header("x-amz-date");
    if xamz_date.is_none() {
        return Err(Error::Illegal.to_string());
    }
    let xamz_date = xamz_date.unwrap();
    let ans: Vec<String> = signed_headers
        .iter()
        .map(|v| {
            let v = v.to_string();
            let val = req.get_header(&v);
            match val {
                Some(val) => format!("{}:{val}", v),
                None => format!("{}:", v),
            }
        })
        .collect();
    query.sort_by(|a, b| a.key.cmp(&b.key));
    let query: Vec<String> = query
        .iter()
        .map(|v| format!("{}={}", v.key, v.val))
        .collect();
    let tosign = format!(
        "{method}\n{url_path}\n{}\n{}\n\n{}\n{}",
        query.join("&"),
        ans.join("\n"),
        signed_headers
            .iter()
            .map(|v| v.to_string())
            .collect::<Vec<String>>()
            .join(";"),
        content_hash
    );
    let ksign = get_v4_ksigning(secretkey, region, &xamz_date)?;
    let buff: &[u8] = &ksign;
    // println!("{tosign}");

    let mut hsh = sha2::Sha256::default();
    let _ = hsh.write_all(tosign.as_bytes());
    let ans = hsh.finalize();
    let canonical_hsh = hex::encode(ans);

    let tosign = format!(
        "AWS4-HMAC-SHA256\n{}\n{}/{region}/{service}/aws4_request\n{canonical_hsh}",
        xamz_date,
        &xamz_date[..8]
    );
    // println!("tosign:{tosign}");
    let ret = Hmac::<sha2::Sha256>::new_from_slice(buff);
    if let Err(err) = ret {
        return Err(err.to_string());
    }
    let mut hsh = ret.unwrap();
    hsh.update(tosign.as_bytes());
    let ans = hsh.finalize().into_bytes();
    let hsh = hex::encode(ans);
    Ok((
        hsh.clone(),
        HmacSha256CircleHasher::new(ksign, hsh, xamz_date, region.to_string()),
    ))
}
fn get_v4_ksigning(secretkey: &str, region: &str, xamz_date: &str) -> GenericResult<[u8; 32]> {
    let mut ksign = [0u8; 32];
    circle_hmac_sha256(
        format!("AWS4{secretkey}").as_str(),
        vec![
            xamz_date[..8].as_bytes(),
            region.as_bytes(),
            "s3".as_bytes(),
            "aws4_request".as_bytes(),
        ]
        .as_slice(),
        &mut ksign,
    )?;
    Ok(ksign)
}
fn circle_hmac_sha256(initkey: &str, values: &[&[u8]], target: &mut [u8]) -> GenericResult<()> {
    let ret = Hmac::<sha2::Sha256>::new_from_slice(initkey.as_bytes());
    if let Err(err) = ret {
        return Err(err.to_string());
    }
    let mut hsh = ret.unwrap();
    hsh.update(values[0]);
    let mut next = hsh.finalize().into_bytes();
    for i in 1..values.len() {
        match Hmac::<sha2::Sha256>::new_from_slice(&next) {
            Ok(mut hsh) => {
                hsh.update(values[i]);
                next = hsh.finalize().into_bytes();
            }
            Err(err) => {
                return Err(err.to_string());
            }
        }
    }
    target[0..next.len()].copy_from_slice(&next);
    Ok(())
}
#[derive(Clone)]
pub struct HmacSha256CircleHasher {
    ksigning: [u8; 32],
    last_hash: String,
    xamz_date: String,
    region: String,
    date: String,
}
impl HmacSha256CircleHasher {
    pub fn new(ksigning: [u8; 32], lasthash: String, xamz_date: String, region: String) -> Self {
        Self {
            ksigning: ksigning,
            last_hash: lasthash,
            region: region,
            xamz_date: xamz_date.clone(),
            date: xamz_date[..8].to_string(),
        }
    }
    pub fn next(&mut self, curr_hsh: &str) -> Result<String, Error> {
        let ans = Hmac::<sha2::Sha256>::new_from_slice(&self.ksigning);
        if let Err(err) = ans {
            return Err(Error::Illegal);
        }
        let mut hsh = ans.unwrap();
        let tosign = format!(
            "AWS4-HMAC-SHA256-PAYLOAD\n{}\n{}/{}/s3/aws4_request\n{}\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n{}",
            self.xamz_date, self.date, self.region, self.last_hash, curr_hsh
        );
        hsh.update(tosign.as_bytes());
        let ans = hsh.finalize().into_bytes();
        let ans = hex::encode(ans);
        self.last_hash = ans.clone();
        Ok(ans)
    }
}
#[derive(Clone)]
pub struct V4Head {
    signature: String,
    region: String,
    accesskey: String,
    circle_hasher:HmacSha256CircleHasher,
}
impl V4Head {
    pub fn new(signature: String, region: String, accesskey: String,hasher:HmacSha256CircleHasher) -> Self {
        Self {
            signature,
            region,
            accesskey,
            circle_hasher:hasher,
        }
    }
    pub fn signature(&self) -> &str {
        &self.signature
    }
    pub fn region(&self) -> &str {
        &self.region
    }
    pub fn accesskey(&self) -> &str {
        &self.accesskey
    }
    pub fn hasher(&mut self)->&mut HmacSha256CircleHasher{
        &mut self.circle_hasher
    }
}
#[cfg(test)]
mod v4test {
    use std::{collections::HashMap, io::Write};
    extern crate sha1;
    use self::sha1::Digest;

    use crate::{utils::BaseKv, GenericResult};

    use super::VHeader;
    impl VHeader for HashMap<String, String> {
        fn get_header(&self, key: &str) -> Option<String> {
            let ans = self.get(key);
            ans.cloned()
        }

        fn set_header(&mut self, key: &str, val: &str) {
            self.insert(key.to_string(), val.to_string());
        }

        fn delete_header(&mut self, key: &str) {
            self.remove(key);
        }

        fn rng_header(&self, mut cb: impl FnMut(&str, &str) -> bool) {
            self.iter().all(|(k, v)| cb(k, v));
        }
    }
    #[test]
    fn v4_signature_test() -> GenericResult<()> {
        //case1
        let mut hm = HashMap::new();
        hm.insert("x-amz-date".to_string(), "20250407T021123Z".to_string());
        hm.insert(
            "x-amz-content-sha256".to_string(),
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_string(),
        );
        hm.insert("host".to_string(), "127.0.0.1:9000".to_string());
        let (signature,_) = super::get_v4_signature(
            &hm,
            "GET",
            "us-east-1",
            "s3",
            "/",
            "root12345",
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            &["host", "x-amz-content-sha256", "x-amz-date"],
            vec![],
        )?;
        assert!(
            signature == "2e3e50b8ab771944088edcda925d886a078ec2442e8504f58e1ac3ef8a2f40fc",
            "expect 2e3e50b8ab771944088edcda925d886a078ec2442e8504f58e1ac3ef8a2f40fc, get {}",
            signature,
        );
        //case2
        let mut hm = HashMap::new();
        hm.insert("x-amz-date".to_string(), "20250407T060526Z".to_string());
        hm.insert(
            "x-amz-content-sha256".to_string(),
            "STREAMING-AWS4-HMAC-SHA256-PAYLOAD".to_string(),
        );
        hm.insert("host".to_string(), "127.0.0.1:9000".to_string());
        hm.insert("x-amz-decoded-content-length".to_string(), "6".to_string());
        let (signature,_) = super::get_v4_signature(
            &hm,
            "PUT",
            "us-east-1",
            "s3",
            "/test/hello.txt",
            "root12345",
            "STREAMING-AWS4-HMAC-SHA256-PAYLOAD",
            &[
                "host",
                "x-amz-content-sha256",
                "x-amz-date",
                "x-amz-decoded-content-length",
            ],
            vec![],
        )?;
        assert!(
            signature == "ae05fb994613c1a72e9f1d3bf14de119155587b955ca7d5589a056e7ffab680f",
            "expect ae05fb994613c1a72e9f1d3bf14de119155587b955ca7d5589a056e7ffab680f,get {}",
            signature
        );
        //case3
        let mut hm = HashMap::new();
        hm.insert("x-amz-date".to_string(), "20250410T124056Z".to_string());
        hm.insert(
            "x-amz-content-sha256".to_string(),
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_string(),
        );
        hm.insert("host".to_string(), "127.0.0.1:9000".to_string());
        let (signature,_) = super::get_v4_signature(
            &hm,
            "GET",
            "us-east-1",
            "s3",
            "/test/",
            "root12345",
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            &["host", "x-amz-content-sha256", "x-amz-date"],
            vec![BaseKv {
                key: "location".to_string(),
                val: "".to_string(),
            }],
        )?;
        assert!(
            signature == "f51cf31bf489474692475a74706f9382c7ba0e93e0d657dc9696efa83fc3906a",
            "expect f51cf31bf489474692475a74706f9382c7ba0e93e0d657dc9696efa83fc3906a, get {}",
            signature,
        );
        Ok(())
    }
    #[test]
    fn v4_chunk_signature_test() -> Result<(), Box<dyn std::error::Error>> {
        let mut hm = HashMap::new();
        hm.insert("x-amz-date".to_string(), "20250407T060526Z".to_string());
        hm.insert(
            "x-amz-content-sha256".to_string(),
            "STREAMING-AWS4-HMAC-SHA256-PAYLOAD".to_string(),
        );
        hm.insert("host".to_string(), "127.0.0.1:9000".to_string());
        hm.insert("x-amz-decoded-content-length".to_string(), "6".to_string());
        let (headersignature,_) = super::get_v4_signature(
            &hm,
            "PUT",
            "us-east-1",
            "s3",
            "/test/hello.txt",
            "root12345",
            "STREAMING-AWS4-HMAC-SHA256-PAYLOAD",
            &[
                "host",
                "x-amz-content-sha256",
                "x-amz-date",
                "x-amz-decoded-content-length",
            ],
            vec![],
        )?;
        let ksigning = super::get_v4_ksigning("root12345", "us-east-1", "20250407T060526Z")?;

        let mut hsch = super::HmacSha256CircleHasher::new(
            ksigning,
            headersignature,
            "20250407T060526Z".to_string(),
            "us-east-1".to_string(),
        );
        let mut hsh = sha2::Sha256::default();
        let _ = hsh.write_all("hello\n".as_bytes());
        let ans = hsh.finalize();
        let hsh = hsch.next(hex::encode(ans).as_str())?;
        assert!(
            hsh == "fe78329ef4be9a33af1ffb23c435cf9d985c79dc65911ac78a66317f5a0521bb",
            "expect fe78329ef4be9a33af1ffb23c435cf9d985c79dc65911ac78a66317f5a0521bb,get {}",
            hsh
        );
        let final_chunk_hsh =
            hsch.next("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")?;
        assert!(
            final_chunk_hsh == "9095844b0da3ae2e9fe65b372662c4beadfc38ebe5a709b16ea9b03d427d03ad",
            "expect 9095844b0da3ae2e9fe65b372662c4beadfc38ebe5a709b16ea9b03d427d03ad,get {}",
            final_chunk_hsh
        );
        Ok(())
    }
}