Skip to content

Commit 07a62fe

Browse files
committed
Add Basic auth to proxy and fix configuration
This commit adds ability to specify Basic Authentication for a proxy, whch is sent with the Proxy-Authentication header. Includes a fix to configure proxy for all traffic passed to the Elasticsearch url.
1 parent fd1082c commit 07a62fe

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

elasticsearch/src/http/transport.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub struct TransportBuilder {
7676
conn_pool: Box<dyn ConnectionPool>,
7777
credentials: Option<Credentials>,
7878
proxy: Option<Url>,
79+
proxy_credentials: Option<Credentials>,
7980
disable_proxy: bool,
8081
}
8182

@@ -91,17 +92,28 @@ impl TransportBuilder {
9192
conn_pool: Box::new(conn_pool),
9293
credentials: None,
9394
proxy: None,
95+
proxy_credentials: None,
9496
disable_proxy: false,
9597
}
9698
}
9799

98-
/// Configures a proxy
99-
pub fn proxy(mut self, url: Url) -> Self {
100+
/// Configures a proxy.
101+
///
102+
/// An optional username and password will be used to set the
103+
/// `Proxy-Authorization` header using Basic Authentication.
104+
pub fn proxy(mut self, url: Url, username: Option<&str>, password: Option<&str>) -> Self {
100105
self.proxy = Some(url);
106+
if let Some(u) = username {
107+
let p = password.unwrap_or("");
108+
self.proxy_credentials = Some(Credentials::Basic(u.into(), p.into()));
109+
}
110+
101111
self
102112
}
103113

104-
/// Whether to disable proxies, including system proxies
114+
/// Whether to disable proxies, including system proxies.
115+
///
116+
/// NOTE: System proxies are enabled by default.
105117
pub fn disable_proxy(mut self) -> Self {
106118
self.disable_proxy = true;
107119
self
@@ -130,10 +142,14 @@ impl TransportBuilder {
130142
if self.disable_proxy {
131143
client_builder = client_builder.no_proxy();
132144
} else if let Some(url) = self.proxy {
133-
client_builder = match url.scheme() {
134-
"https" => client_builder.proxy(reqwest::Proxy::https(url)?),
135-
_ => client_builder.proxy(reqwest::Proxy::http(url)?),
136-
};
145+
let mut proxy = reqwest::Proxy::all(url)?;
146+
if let Some(c) = self.proxy_credentials {
147+
proxy = match c {
148+
Credentials::Basic(u, p) => proxy.basic_auth(&u, &p),
149+
_ => proxy,
150+
};
151+
}
152+
client_builder = client_builder.proxy(proxy);
137153
}
138154

139155
let client = client_builder.build()?;

elasticsearch/tests/common/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn create_for_url(url: &str) -> Elasticsearch {
4949
pub fn create(mut builder: TransportBuilder) -> Elasticsearch {
5050
if running_proxy() {
5151
let proxy_url = Url::parse("http://localhost:8888").unwrap();
52-
builder = builder.proxy(proxy_url);
52+
builder = builder.proxy(proxy_url, None, None);
5353
}
5454

5555
let transport = builder.build().unwrap();

0 commit comments

Comments
 (0)