Skip to content

Commit

Permalink
Add support for https_proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sam701 committed Oct 17, 2019
1 parent 195be13 commit f4526af
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
34 changes: 29 additions & 5 deletions src/assume/assumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -94,15 +98,35 @@ fn assume_subject(client: &StsClient, subject: AssumeSubject) -> Result<AwsCrede
}


fn create_client(credentials: Cred, region: Region) -> StsClient {
StsClient::new_with(
HttpClient::new().unwrap(),
fn create_client(credentials: Cred, region: Region) -> Result<StsClient, String> {
Ok(StsClient::new_with(
HttpClient::from_connector(get_https_connector()?),
StaticProvider::new(
credentials.key,
credentials.secret,
credentials.token,
None,
),
region,
)
))
}

fn get_https_proxy() -> Option<String> {
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<ProxyConnector<HttpsConnector<HttpConnector>>, String> {
let connector = HttpsConnector::new(2)
.expect("connector with 2 threads");
Ok(match get_https_proxy() {
Some(proxy_url) => {
let url = proxy_url.parse::<Uri>()
.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")
})
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit f4526af

Please sign in to comment.