Skip to content
Merged
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions elasticsearch/src/http/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,20 @@ impl Transport {
pub fn set_auth(&self, credentials: Credentials) {
*self.credentials.write() = Some(credentials);
}

/// Creates a new `Transport` that is a clone of this one, except for authentication
/// credentials. This is the opposite of [`Transport::set_auth()`]. Typically used
/// when working in multi-tenant environments where credentials can vary with every
/// request.
pub fn clone_with_auth(&self, credentials: Option<Credentials>) -> Self {
Self {
client: self.client.clone(),
credentials: Arc::new(RwLock::new(credentials)),
conn_pool: self.conn_pool.clone(),
request_body_compression: self.request_body_compression,
send_meta: self.send_meta,
}
}
}

impl Default for Transport {
Expand Down Expand Up @@ -1298,4 +1312,28 @@ pub mod tests {

Ok(())
}

#[test]
fn clone_with_credentials() -> anyhow::Result<()> {
let t1: Transport = TransportBuilder::new(SingleNodeConnectionPool::default())
.auth(Credentials::Basic("foo".to_string(), "bar".to_string()))
.build()?;

let t2 = t1.clone_with_auth(Some(Credentials::Bearer("The bear".to_string())));

if let Some(Credentials::Basic(login, password)) = t1.credentials.read().as_ref() {
assert_eq!(login, "foo");
assert_eq!(password, "bar");
} else {
panic!("Expected Basic credentials");
}

if let Some(Credentials::Bearer(token)) = t2.credentials.read().as_ref() {
assert_eq!(token, "The bear");
} else {
panic!("Expected Bearer credentials");
}

Ok(())
}
}
Loading