From d91cdcf02c702374385c6b49b50a7d8fb25dbe02 Mon Sep 17 00:00:00 2001 From: Aditya Manthramurthy Date: Thu, 30 May 2019 15:13:35 -0700 Subject: [PATCH] Parse region from xml resp for get_bucket_location --- Cargo.toml | 9 +++++---- src/main.rs | 6 +++--- src/minio.rs | 25 +++++++------------------ src/minio/sign.rs | 4 ++-- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0bda4e1..64c9ed0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,11 @@ authors = ["Aditya Manthramurthy "] edition = "2018" [dependencies] +bytes = "0.4.12" +futures = "0.1.27" +http = "0.1.17" hyper = "0.12.28" +hyper-tls = "0.3.2" ring = "0.14.6" time = "0.1.42" -http = "0.1.17" -hyper-tls = "0.3.2" -futures = "0.1.27" -bytes = "0.4.12" +xml-rs = "0.8.0" diff --git a/src/main.rs b/src/main.rs index 4438099..ab8648e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,10 +15,10 @@ fn get_local_default_server() -> minio::Client { fn main() { rt::run(rt::lazy(|| { - let c = get_local_default_server(); - // let c = minio::Client::get_play_client(); + // let c = get_local_default_server(); + let c = minio::Client::get_play_client(); c.get_bucket_location("txp") - .map(|res| println!("{}", res)) + .map(|res| println!("{}", res.to_string())) .map_err(|err| println!("{:?}", err)) })); } diff --git a/src/minio.rs b/src/minio.rs index 1f0dc29..16fcaa6 100644 --- a/src/minio.rs +++ b/src/minio.rs @@ -1,5 +1,7 @@ mod api; mod sign; +mod types; +mod xml; use bytes::Bytes; use futures::future::{self, Future}; @@ -10,10 +12,10 @@ use hyper::{body::Body, client, header, header::HeaderMap, Method, Request, Resp use hyper_tls::HttpsConnector; use std::collections::HashMap; use std::env; -use std::sync::Arc; use std::{string, string::String}; use time; use time::Tm; +use types::{Err, Region}; #[derive(Debug, Clone)] pub struct Credentials { @@ -40,19 +42,6 @@ impl Credentials { } } -pub type Region = String; - -#[derive(Debug)] -pub enum Err { - InvalidUrl(String), - InvalidEnv(String), - HttpErr(http::Error), - HyperErr(hyper::Error), - FailStatusCodeErr(hyper::StatusCode, Bytes), - Utf8DecodingErr(string::FromUtf8Error), - RawSvcErr(hyper::StatusCode, Response), -} - #[derive(Clone)] enum ConnClient { HttpCC(client::Client), @@ -87,7 +76,7 @@ impl Client { } else { Ok(Client { server: s.clone(), - region: String::from(""), + region: Region::empty(), conn_client: if s.scheme_str() == Some("http") { ConnClient::HttpCC(client::Client::new()) } else { @@ -128,7 +117,7 @@ impl Client { pub fn get_play_client() -> Client { Client { server: "https://play.min.io:9000".parse::().unwrap(), - region: String::from(""), + region: Region::empty(), conn_client: { let https = HttpsConnector::new(4).unwrap(); ConnClient::HttpsCC(client::Client::builder().build::<_, hyper::Body>(https)) @@ -140,7 +129,7 @@ impl Client { } } - pub fn get_bucket_location(&self, b: &str) -> impl Future { + pub fn get_bucket_location(&self, b: &str) -> impl Future { let mut qp = HashMap::new(); qp.insert("location".to_string(), None); let mut hmap = HeaderMap::new(); @@ -170,7 +159,7 @@ impl Client { resp.into_body() .concat2() .map_err(|err| Err::HyperErr(err)) - .and_then(move |chunk| b2s(chunk.into_bytes())) + .and_then(move |chunk| xml::parse_bucket_location(chunk.into_bytes())) }) } } diff --git a/src/minio/sign.rs b/src/minio/sign.rs index 659e72b..8777c9c 100644 --- a/src/minio/sign.rs +++ b/src/minio/sign.rs @@ -17,7 +17,7 @@ fn aws_format_date(t: &Tm) -> String { fn mk_scope(t: &Tm, r: &minio::Region) -> String { let scope_time = t.strftime("%Y%m%d").unwrap().to_string(); - format!("{}/{}/s3/aws4_request", scope_time, r) + format!("{}/{}/s3/aws4_request", scope_time, r.to_string()) } // Returns list of SORTED headers that will be signed. TODO: verify @@ -162,7 +162,7 @@ pub fn sign_v4(r: &minio::S3Req, c: &minio::Client) -> Vec<(HeaderName, HeaderVa println!("canonicalreq: {}", cr); let s2s = string_to_sign(&r.ts, &scope, &cr); println!("s2s: {}", s2s); - let skey = get_signing_key(&r.ts, &c.region, &creds.secret_key); + let skey = get_signing_key(&r.ts, &c.region.to_string(), &creds.secret_key); println!("skey: {:?}", skey); let signature = compute_sign(&s2s, &skey); println!("sign: {}", signature);