Skip to content

Extract credentials passed in Url #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions elasticsearch/src/http/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,31 @@ impl Transport {

/// Creates a new instance of a [Transport] configured with a
/// [SingleNodeConnectionPool].
/// If the url contains credentials, these are removed and added
/// as [Credentials::Basic] to the [Transport]
pub fn single_node(url: &str) -> Result<Transport, Error> {
let u = Url::parse(url)?;
let mut u = Url::parse(url)?;

// if username and password are specified in the url, remove them and use
// them to construct basic credentials. Not doing so can lead to a double
// Authorization header being sent by reqwest.
let credentials = if !u.username().is_empty() && u.password().is_some() {
let username = u.username().to_string();
let password = u.password().unwrap().to_string();
u.set_username("").unwrap();
u.set_password(None).unwrap();
Some(Credentials::Basic(username, password))
} else {
None
};

let conn_pool = SingleNodeConnectionPool::new(u);
let transport = TransportBuilder::new(conn_pool).build()?;
let mut transport_builder = TransportBuilder::new(conn_pool);
if let Some(c) = credentials {
transport_builder = transport_builder.auth(c);
}

let transport = transport_builder.build()?;
Ok(transport)
}

Expand Down
42 changes: 35 additions & 7 deletions elasticsearch/tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,44 @@ pub fn create_default_builder() -> TransportBuilder {
}

pub fn create_builder(addr: &str) -> TransportBuilder {
let url = Url::parse(addr).unwrap();
let mut url = Url::parse(addr).unwrap();

// if the url is https and specifies a username and password, remove from the url and set credentials
let credentials = if url.scheme() == "https" {
let username = if !url.username().is_empty() {
let u = url.username().to_string();
url.set_username("").unwrap();
u
} else {
"elastic".into()
};

let password = match url.password() {
Some(p) => {
let pass = p.to_string();
url.set_password(None).unwrap();
pass
}
None => "changeme".into(),
};

Some(Credentials::Basic(username, password))
} else {
None
};

let conn_pool = SingleNodeConnectionPool::new(url.clone());
let mut builder = TransportBuilder::new(conn_pool);
// assume if we're running with HTTPS then authentication is also enabled and disable
// certificate validation - we'll change this for tests that need to.
if url.scheme() == "https" {
builder = builder.auth(Credentials::Basic("elastic".into(), "changeme".into()));

#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
{
if let Some(c) = credentials {
builder = builder.auth(c);
}

// assume if we're running with HTTPS then disable
// certificate validation - we'll change this for tests that need to.
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
{
if url.scheme() == "https" {
builder = builder.cert_validation(CertificateValidation::None);
}
}
Expand Down