diff --git a/Cargo.lock b/Cargo.lock index 92bd9f0..b8fd58b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,9 @@ dependencies = [ "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "custom_error 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper 0.12.33 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper-proxy 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper-tls 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.21 (registry+https://github.com/rust-lang/crates.io-index)", "rusoto_core 0.40.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -554,6 +557,22 @@ dependencies = [ "want 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hyper-proxy" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper 0.12.33 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper-tls 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-tls 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "typed-headers 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "hyper-tls" version = "0.3.2" @@ -1663,6 +1682,16 @@ dependencies = [ "tokio-executor 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-tls" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", + "native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-udp" version = "0.1.4" @@ -1715,6 +1744,18 @@ dependencies = [ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "typed-headers" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "typenum" version = "1.11.2" @@ -1936,6 +1977,7 @@ dependencies = [ "checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" "checksum hyper 0.12.33 (registry+https://github.com/rust-lang/crates.io-index)" = "7cb44cbce9d8ee4fb36e4c0ad7b794ac44ebaad924b9c8291a63215bb44c2c8f" +"checksum hyper-proxy 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5f3c9d4782c0eee5fe1e6333d72d37dbba6230dc50618117598ea5de9a90d017" "checksum hyper-tls 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3a800d6aa50af4b5850b2b0f659625ce9504df908e9733b635720483be26174f" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" @@ -2053,11 +2095,13 @@ dependencies = [ "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" "checksum tokio-threadpool 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "90ca01319dea1e376a001e8dc192d42ebde6dd532532a5bad988ac37db365b19" "checksum tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f2106812d500ed25a4f38235b9cae8f78a09edf43203e16e59c3b769a342a60e" +"checksum tokio-tls 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "354b8cd83825b3c20217a9dc174d6a0c67441a2fae5c41bcb1ea6679f6ae0f7c" "checksum tokio-udp 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "56775b287cda0fd8ca0c5d2f5b1d0646afbd360101e2eef91cd89365fcfc2f5f" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" "checksum toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7aabe75941d914b72bf3e5d3932ed92ce0664d49d8432305a8b547c37227724" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum try_from 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "283d3b89e1368717881a9d51dad843cc435380d8109c9e47d38780a324698d8b" +"checksum typed-headers 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bd6f5af532d859106afe9077c8f95bcaa09af272d5d9b338ec1ff05830b5803c" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2e2e6bd1e59e56598518beb94fd6db628ded570326f0a98c679a304bd9f00150" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" diff --git a/Cargo.toml b/Cargo.toml index 8022893..fb9e3da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,6 @@ chrono = "0.4" ansi_term = "0.12" linked-hash-map = { version = "0.5", features = ["serde_impl"] } reqwest = "0.9" +hyper = "*" +hyper-tls = "*" +hyper-proxy = "0.5" \ No newline at end of file diff --git a/src/assume/assumer.rs b/src/assume/assumer.rs index f815e35..26b9183 100644 --- a/src/assume/assumer.rs +++ b/src/assume/assumer.rs @@ -5,6 +5,10 @@ use crate::config::{Config, AssumeSubject}; use crate::credentials::{ProfileName, CredentialsFile}; use std::error::Error; use chrono::{Utc, Duration}; +use hyper_proxy::{Proxy, Intercept, ProxyConnector}; +use hyper_tls::HttpsConnector; +use hyper::Uri; +use hyper::client::HttpConnector; pub struct RoleAssumer<'a> { region: Region, @@ -60,7 +64,7 @@ impl<'a> RoleAssumer<'a> { let parent_cred = self.profile_credentials(&parent)?; let sub = self.config.assume_subject(profile)? .ok_or(format!("cannot get assume subject for profile {}", profile))?; - let parent_client = create_client(parent_cred, self.region.clone()); + let parent_client = create_client(parent_cred, self.region.clone())?; let new_cred = assume_subject(&parent_client, sub)?; let out_cred = (&new_cred).into(); self.store.put_credentials(profile.clone(), new_cred); @@ -94,9 +98,9 @@ fn assume_subject(client: &StsClient, subject: AssumeSubject) -> Result StsClient { - StsClient::new_with( - HttpClient::new().unwrap(), +fn create_client(credentials: Cred, region: Region) -> Result { + Ok(StsClient::new_with( + HttpClient::from_connector(get_https_connector()?), StaticProvider::new( credentials.key, credentials.secret, @@ -104,5 +108,25 @@ fn create_client(credentials: Cred, region: Region) -> StsClient { None, ), region, - ) + )) } + +fn get_https_proxy() -> Option { + std::env::var_os("https_proxy") + .or(std::env::var_os("HTTPS_PROXY")) + .map(|x| x.into_string().expect("https_proxy is utf8")) +} + +fn get_https_connector() -> Result>, String> { + let connector = HttpsConnector::new(2) + .expect("connector with 2 threads"); + Ok(match get_https_proxy() { + Some(proxy_url) => { + let url = proxy_url.parse::() + .map_err(|e| format!("cannot parse proxy URL({}): {}", &proxy_url, e))?; + let proxy = Proxy::new(Intercept::All, url); + ProxyConnector::from_proxy(connector, proxy).expect("proxy created") + } + None => ProxyConnector::new(connector).expect("transparent proxy created") + }) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d6a8e70..cd1f82f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,9 @@ extern crate chrono; extern crate ansi_term; extern crate linked_hash_map; extern crate reqwest; +extern crate hyper; +extern crate hyper_proxy; +extern crate hyper_tls; use crate::config::Config; use ansi_term::{Color, Style};